diff --git a/spring-messaging/src/test/java/org/springframework/messaging/simp/annotation/support/SimpAnnotationMethodMessageHandlerTests.java b/spring-messaging/src/test/java/org/springframework/messaging/simp/annotation/support/SimpAnnotationMethodMessageHandlerTests.java index a2ee457b61d..c2f54494877 100644 --- a/spring-messaging/src/test/java/org/springframework/messaging/simp/annotation/support/SimpAnnotationMethodMessageHandlerTests.java +++ b/spring-messaging/src/test/java/org/springframework/messaging/simp/annotation/support/SimpAnnotationMethodMessageHandlerTests.java @@ -17,6 +17,7 @@ package org.springframework.messaging.simp.annotation.support; import java.util.Arrays; +import java.util.Collections; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.Map; @@ -51,7 +52,6 @@ import org.springframework.messaging.simp.SimpAttributes; import org.springframework.messaging.simp.SimpAttributesContextHolder; import org.springframework.messaging.simp.SimpMessageHeaderAccessor; import org.springframework.messaging.simp.SimpMessageSendingOperations; -import org.springframework.messaging.simp.SimpMessageType; import org.springframework.messaging.simp.SimpMessagingTemplate; import org.springframework.messaging.simp.annotation.SubscribeMapping; import org.springframework.messaging.support.MessageBuilder; @@ -75,10 +75,12 @@ import org.mockito.MockitoAnnotations; * @author Brian Clozel * @author Sebastien Deleuze */ +@SuppressWarnings("unused") public class SimpAnnotationMethodMessageHandlerTests { private static final String TEST_INVALID_VALUE = "invalidValue"; + private TestSimpAnnotationMethodMessageHandler messageHandler; private TestController testController; @@ -98,7 +100,7 @@ public class SimpAnnotationMethodMessageHandlerTests { MockitoAnnotations.initMocks(this); SimpMessagingTemplate brokerTemplate = new SimpMessagingTemplate(this.channel); - brokerTemplate.setMessageConverter(converter); + brokerTemplate.setMessageConverter(this.converter); this.messageHandler = new TestSimpAnnotationMethodMessageHandler(brokerTemplate, this.channel, this.channel); this.messageHandler.setApplicationContext(new StaticApplicationContext()); @@ -113,12 +115,8 @@ public class SimpAnnotationMethodMessageHandlerTests { @Test @SuppressWarnings("unchecked") public void headerArgumentResolution() { - SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.create(); - headers.setSessionId("session1"); - headers.setSessionAttributes(new ConcurrentHashMap<>()); - headers.setDestination("/pre/headers"); - headers.setHeader("foo", "bar"); - Message message = MessageBuilder.withPayload(new byte[0]).setHeaders(headers).build(); + Map headers = Collections.singletonMap("foo", "bar"); + Message message = createMessage("/pre/headers", headers); this.messageHandler.handleMessage(message); assertEquals("headers", this.testController.method); @@ -128,12 +126,8 @@ public class SimpAnnotationMethodMessageHandlerTests { @Test public void optionalHeaderArgumentResolutionWhenPresent() { - SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.create(); - headers.setSessionId("session1"); - headers.setSessionAttributes(new ConcurrentHashMap<>()); - headers.setDestination("/pre/optionalHeaders"); - headers.setHeader("foo", "bar"); - Message message = MessageBuilder.withPayload(new byte[0]).setHeaders(headers).build(); + Map headers = Collections.singletonMap("foo", "bar"); + Message message = createMessage("/pre/optionalHeaders", headers); this.messageHandler.handleMessage(message); assertEquals("optionalHeaders", this.testController.method); @@ -143,11 +137,7 @@ public class SimpAnnotationMethodMessageHandlerTests { @Test public void optionalHeaderArgumentResolutionWhenNotPresent() { - SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.create(); - headers.setSessionId("session1"); - headers.setSessionAttributes(new ConcurrentHashMap<>()); - headers.setDestination("/pre/optionalHeaders"); - Message message = MessageBuilder.withPayload(new byte[0]).setHeaders(headers).build(); + Message message = createMessage("/pre/optionalHeaders"); this.messageHandler.handleMessage(message); assertEquals("optionalHeaders", this.testController.method); @@ -157,11 +147,7 @@ public class SimpAnnotationMethodMessageHandlerTests { @Test public void messageMappingDestinationVariableResolution() { - SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.create(); - headers.setSessionId("session1"); - headers.setSessionAttributes(new ConcurrentHashMap<>()); - headers.setDestination("/pre/message/bar/value"); - Message message = MessageBuilder.withPayload(new byte[0]).setHeaders(headers).build(); + Message message = createMessage("/pre/message/bar/value"); this.messageHandler.handleMessage(message); assertEquals("messageMappingDestinationVariable", this.testController.method); @@ -171,11 +157,7 @@ public class SimpAnnotationMethodMessageHandlerTests { @Test public void subscribeEventDestinationVariableResolution() { - SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.create(SimpMessageType.SUBSCRIBE); - headers.setSessionId("session1"); - headers.setSessionAttributes(new ConcurrentHashMap<>()); - headers.setDestination("/pre/sub/bar/value"); - Message message = MessageBuilder.withPayload(new byte[0]).copyHeaders(headers.toMap()).build(); + Message message = createMessage("/pre/sub/bar/value"); this.messageHandler.handleMessage(message); assertEquals("subscribeEventDestinationVariable", this.testController.method); @@ -185,11 +167,7 @@ public class SimpAnnotationMethodMessageHandlerTests { @Test public void simpleBinding() { - SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.create(); - headers.setSessionId("session1"); - headers.setSessionAttributes(new ConcurrentHashMap<>()); - headers.setDestination("/pre/binding/id/12"); - Message message = MessageBuilder.withPayload(new byte[0]).setHeaders(headers).build(); + Message message = createMessage("/pre/binding/id/12"); this.messageHandler.handleMessage(message); assertEquals("simpleBinding", this.testController.method); @@ -199,23 +177,16 @@ public class SimpAnnotationMethodMessageHandlerTests { @Test public void validationError() { - SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.create(); - headers.setSessionId("session1"); - headers.setSessionAttributes(new ConcurrentHashMap<>()); - headers.setDestination("/pre/validation/payload"); - Message message = MessageBuilder.withPayload(TEST_INVALID_VALUE.getBytes()).setHeaders(headers).build(); + Message message = createMessage("/pre/validation/payload"); this.messageHandler.handleMessage(message); assertEquals("handleValidationException", this.testController.method); } @Test public void exceptionWithHandlerMethodArg() { - SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.create(); - headers.setSessionId("session1"); - headers.setSessionAttributes(new ConcurrentHashMap<>()); - headers.setDestination("/pre/illegalState"); - Message message = MessageBuilder.withPayload(new byte[0]).setHeaders(headers).build(); + Message message = createMessage("/pre/illegalState"); this.messageHandler.handleMessage(message); + assertEquals("handleExceptionWithHandlerMethodArg", this.testController.method); HandlerMethod handlerMethod = (HandlerMethod) this.testController.arguments.get("handlerMethod"); assertNotNull(handlerMethod); @@ -224,11 +195,12 @@ public class SimpAnnotationMethodMessageHandlerTests { @Test public void simpScope() { - Map map = new ConcurrentHashMap<>(); - map.put("name", "value"); + Map sessionAttributes = new ConcurrentHashMap<>(); + sessionAttributes.put("name", "value"); + SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.create(); headers.setSessionId("session1"); - headers.setSessionAttributes(map); + headers.setSessionAttributes(sessionAttributes); headers.setDestination("/pre/scope"); Message message = MessageBuilder.withPayload(new byte[0]).setHeaders(headers).build(); this.messageHandler.handleMessage(message); @@ -244,20 +216,12 @@ public class SimpAnnotationMethodMessageHandlerTests { this.messageHandler.registerHandler(controller); this.messageHandler.setDestinationPrefixes(Arrays.asList("/app1", "/app2/")); - SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.create(); - headers.setSessionId("session1"); - headers.setSessionAttributes(new HashMap<>()); - headers.setDestination("/app1/pre.foo"); - Message message = MessageBuilder.withPayload(new byte[0]).setHeaders(headers).build(); + Message message = createMessage("/app1/pre.foo"); this.messageHandler.handleMessage(message); assertEquals("handleFoo", controller.method); - headers = SimpMessageHeaderAccessor.create(); - headers.setSessionId("session1"); - headers.setSessionAttributes(new HashMap<>()); - headers.setDestination("/app2/pre.foo"); - message = MessageBuilder.withPayload(new byte[0]).setHeaders(headers).build(); + message = createMessage("/app2/pre.foo"); this.messageHandler.handleMessage(message); assertEquals("handleFoo", controller.method); @@ -266,20 +230,16 @@ public class SimpAnnotationMethodMessageHandlerTests { @Test @SuppressWarnings("unchecked") public void listenableFutureSuccess() { - + Message emptyMessage = (Message) MessageBuilder.withPayload(new byte[0]).build(); given(this.channel.send(any(Message.class))).willReturn(true); - given(this.converter.toMessage(anyObject(), any(MessageHeaders.class))) - .willReturn((Message) MessageBuilder.withPayload(new byte[0]).build()); + given(this.converter.toMessage(anyObject(), any(MessageHeaders.class))).willReturn(emptyMessage); ListenableFutureController controller = new ListenableFutureController(); this.messageHandler.registerHandler(controller); this.messageHandler.setDestinationPrefixes(Arrays.asList("/app1", "/app2/")); - SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.create(); - headers.setSessionId("session1"); - headers.setSessionAttributes(new HashMap<>()); - headers.setDestination("/app1/listenable-future/success"); - Message message = MessageBuilder.withPayload(new byte[0]).setHeaders(headers).build(); + + Message message = createMessage("/app1/listenable-future/success"); this.messageHandler.handleMessage(message); assertNotNull(controller.future); @@ -291,19 +251,15 @@ public class SimpAnnotationMethodMessageHandlerTests { @Test @SuppressWarnings("unchecked") public void listenableFutureFailure() { - + Message emptyMessage = (Message) MessageBuilder.withPayload(new byte[0]).build(); given(this.channel.send(any(Message.class))).willReturn(true); - given(this.converter.toMessage(anyObject(), any(MessageHeaders.class))) - .willReturn((Message) MessageBuilder.withPayload(new byte[0]).build()); + given(this.converter.toMessage(anyObject(), any(MessageHeaders.class))).willReturn(emptyMessage); ListenableFutureController controller = new ListenableFutureController(); this.messageHandler.registerHandler(controller); this.messageHandler.setDestinationPrefixes(Arrays.asList("/app1", "/app2/")); - SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.create(); - headers.setSessionId("session1"); - headers.setSessionAttributes(new HashMap<>()); - headers.setDestination("/app1/listenable-future/failure"); - Message message = MessageBuilder.withPayload(new byte[0]).setHeaders(headers).build(); + + Message message = createMessage("/app1/listenable-future/failure"); this.messageHandler.handleMessage(message); controller.future.run(); @@ -313,19 +269,15 @@ public class SimpAnnotationMethodMessageHandlerTests { @Test @SuppressWarnings("unchecked") public void completableFutureSuccess() { - + Message emptyMessage = (Message) MessageBuilder.withPayload(new byte[0]).build(); given(this.channel.send(any(Message.class))).willReturn(true); - given(this.converter.toMessage(anyObject(), any(MessageHeaders.class))) - .willReturn((Message) MessageBuilder.withPayload(new byte[0]).build()); + given(this.converter.toMessage(anyObject(), any(MessageHeaders.class))).willReturn(emptyMessage); CompletableFutureController controller = new CompletableFutureController(); this.messageHandler.registerHandler(controller); this.messageHandler.setDestinationPrefixes(Arrays.asList("/app1", "/app2/")); - SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.create(); - headers.setSessionId("session1"); - headers.setSessionAttributes(new HashMap<>()); - headers.setDestination("/app1/completable-future"); - Message message = MessageBuilder.withPayload(new byte[0]).setHeaders(headers).build(); + + Message message = createMessage("/app1/completable-future"); this.messageHandler.handleMessage(message); assertNotNull(controller.future); @@ -337,25 +289,40 @@ public class SimpAnnotationMethodMessageHandlerTests { @Test @SuppressWarnings("unchecked") public void completableFutureFailure() { - + Message emptyMessage = (Message) MessageBuilder.withPayload(new byte[0]).build(); given(this.channel.send(any(Message.class))).willReturn(true); - given(this.converter.toMessage(anyObject(), any(MessageHeaders.class))) - .willReturn((Message) MessageBuilder.withPayload(new byte[0]).build()); + given(this.converter.toMessage(anyObject(), any(MessageHeaders.class))).willReturn(emptyMessage); CompletableFutureController controller = new CompletableFutureController(); this.messageHandler.registerHandler(controller); this.messageHandler.setDestinationPrefixes(Arrays.asList("/app1", "/app2/")); - SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.create(); - headers.setSessionId("session1"); - headers.setSessionAttributes(new HashMap<>()); - headers.setDestination("/app1/completable-future"); - Message message = MessageBuilder.withPayload(new byte[0]).setHeaders(headers).build(); + + Message message = createMessage("/app1/completable-future"); this.messageHandler.handleMessage(message); controller.future.completeExceptionally(new IllegalStateException()); - assertTrue(controller.exceptionCatched); + assertTrue(controller.exceptionCaught); + } + + private Message createMessage(String destination) { + return createMessage(destination, null); } + private Message createMessage(String destination, Map headers) { + SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.create(); + accessor.setSessionId("session1"); + accessor.setSessionAttributes(new HashMap<>()); + accessor.setDestination(destination); + if (headers != null) { + for (Map.Entry entry : headers.entrySet()) { + accessor.setHeader(entry.getKey(), entry.getValue()); + } + } + return MessageBuilder.withPayload(new byte[0]).setHeaders(accessor).build(); + } + + + private static class TestSimpAnnotationMethodMessageHandler extends SimpAnnotationMethodMessageHandler { public TestSimpAnnotationMethodMessageHandler(SimpMessageSendingOperations brokerTemplate, @@ -489,7 +456,8 @@ public class SimpAnnotationMethodMessageHandlerTests { private static class CompletableFutureController { private CompletableFuture future; - private boolean exceptionCatched = false; + + private boolean exceptionCaught = false; @MessageMapping("completable-future") public CompletableFuture handleCompletableFuture() { @@ -499,7 +467,7 @@ public class SimpAnnotationMethodMessageHandlerTests { @MessageExceptionHandler(IllegalStateException.class) public void handleValidationException() { - this.exceptionCatched = true; + this.exceptionCaught = true; } } @@ -516,6 +484,7 @@ public class SimpAnnotationMethodMessageHandlerTests { public boolean supports(Class clazz) { return String.class.isAssignableFrom(clazz); } + @Override public void validate(Object target, Errors errors) { String value = (String) target;