From 0db2f79bdb886b6fc9bc75c59b31df28696e6a22 Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Wed, 22 Jan 2014 11:29:38 -0500 Subject: [PATCH] Polish message method handling tests --- ...ts.java => MethodMessageHandlerTests.java} | 65 +++++++++---------- ...mpAnnotationMethodMessageHandlerTests.java | 9 ++- ...va => StompWebSocketIntegrationTests.java} | 5 +- 3 files changed, 38 insertions(+), 41 deletions(-) rename spring-messaging/src/test/java/org/springframework/messaging/handler/invocation/{AbstractMethodMessageHandlerTests.java => MethodMessageHandlerTests.java} (75%) rename spring-websocket/src/test/java/org/springframework/web/socket/messaging/{SimpAnnotationMethodIntegrationTests.java => StompWebSocketIntegrationTests.java} (97%) diff --git a/spring-messaging/src/test/java/org/springframework/messaging/handler/invocation/AbstractMethodMessageHandlerTests.java b/spring-messaging/src/test/java/org/springframework/messaging/handler/invocation/MethodMessageHandlerTests.java similarity index 75% rename from spring-messaging/src/test/java/org/springframework/messaging/handler/invocation/AbstractMethodMessageHandlerTests.java rename to spring-messaging/src/test/java/org/springframework/messaging/handler/invocation/MethodMessageHandlerTests.java index 09dd89ad95..10f8f42338 100644 --- a/spring-messaging/src/test/java/org/springframework/messaging/handler/invocation/AbstractMethodMessageHandlerTests.java +++ b/spring-messaging/src/test/java/org/springframework/messaging/handler/invocation/MethodMessageHandlerTests.java @@ -20,7 +20,6 @@ import org.hamcrest.Matchers; import org.junit.Before; import org.junit.Test; -import org.springframework.beans.DirectFieldAccessor; import org.springframework.context.support.StaticApplicationContext; import org.springframework.messaging.Message; import org.springframework.messaging.handler.DestinationPatternsMessageCondition; @@ -28,7 +27,6 @@ import org.springframework.messaging.handler.HandlerMethod; import org.springframework.messaging.handler.HandlerMethodSelector; import org.springframework.messaging.handler.annotation.support.MessageMethodArgumentResolver; import org.springframework.messaging.support.MessageBuilder; -import org.springframework.messaging.support.MessageHeaderAccessor; import org.springframework.util.AntPathMatcher; import org.springframework.util.PathMatcher; import org.springframework.util.ReflectionUtils.MethodFilter; @@ -41,26 +39,31 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertThat; /** - * Test fixture for {@link org.springframework.messaging.handler.invocation.AbstractMethodMessageHandler}. + * Test fixture for + * {@link org.springframework.messaging.handler.invocation.AbstractMethodMessageHandler}. + * * @author Brian Clozel + * @author Rossen Stoyanchev */ -public class AbstractMethodMessageHandlerTests { +public class MethodMessageHandlerTests { - private static final String DESTINATION_HEADER = "testDestination"; + private static final String DESTINATION_HEADER = "destination"; - private MyMethodMessageHandler messageHandler; + private TestMethodMessageHandler messageHandler; private TestController testController; + @Before public void setup() { - List destinationPrefixes = new ArrayList(); - destinationPrefixes.add("/test/"); - this.messageHandler = new MyMethodMessageHandler(); + List destinationPrefixes = Arrays.asList("/test"); + + this.messageHandler = new TestMethodMessageHandler(); this.messageHandler.setApplicationContext(new StaticApplicationContext()); this.messageHandler.setDestinationPrefixes(destinationPrefixes); this.messageHandler.afterPropertiesSet(); + this.testController = new TestController(); this.messageHandler.registerHandler(this.testController); } @@ -73,9 +76,7 @@ public class AbstractMethodMessageHandlerTests { @Test public void registeredMappings() { - DirectFieldAccessor fieldAccessor = new DirectFieldAccessor(this.messageHandler); - Map handlerMethods = (Map) - fieldAccessor.getPropertyValue("handlerMethods"); + Map handlerMethods = this.messageHandler.getHandlerMethods(); assertNotNull(handlerMethods); assertThat(handlerMethods.keySet(), Matchers.hasSize(3)); @@ -85,12 +86,9 @@ public class AbstractMethodMessageHandlerTests { public void antPatchMatchWildcard() throws Exception { Method method = this.testController.getClass().getMethod("handlerPathMatchWildcard"); - this.messageHandler.registerHandlerMethod(this.testController, method, "/handlerPathMatch**"); + this.messageHandler.registerHandlerMethod(this.testController, method, "/handlerPathMatch*"); - MessageHeaderAccessor headers = new MessageHeaderAccessor(); - headers.setHeader(DESTINATION_HEADER, "/test/handlerPathMatchFoo"); - Message message = MessageBuilder.withPayload(new byte[0]).setHeaders(headers).build(); - this.messageHandler.handleMessage(message); + this.messageHandler.handleMessage(toDestination("/test/handlerPathMatchFoo")); assertEquals("pathMatchWildcard", this.testController.method); } @@ -104,39 +102,34 @@ public class AbstractMethodMessageHandlerTests { method = this.testController.getClass().getMethod("secondBestMatch"); this.messageHandler.registerHandlerMethod(this.testController, method, "/bestmatch/*/*"); - MessageHeaderAccessor headers = new MessageHeaderAccessor(); - headers.setHeader(DESTINATION_HEADER, "/test/bestmatch/bar/path"); - Message message = MessageBuilder.withPayload(new byte[0]).setHeaders(headers).build(); - this.messageHandler.handleMessage(message); + this.messageHandler.handleMessage(toDestination("/test/bestmatch/bar/path")); assertEquals("bestMatch", this.testController.method); } @Test - public void argumentResolver() { + public void argumentResolution() { - MessageHeaderAccessor headers = new MessageHeaderAccessor(); - headers.setHeader(DESTINATION_HEADER, "/test/handlerArgumentResolver"); - Message message = MessageBuilder.withPayload(new byte[0]).setHeaders(headers).build(); - this.messageHandler.handleMessage(message); + this.messageHandler.handleMessage(toDestination("/test/handlerArgumentResolver")); assertEquals("handlerArgumentResolver", this.testController.method); assertNotNull(this.testController.arguments.get("message")); } @Test - public void exceptionResolver() { + public void exceptionHandled() { - MessageHeaderAccessor headers = new MessageHeaderAccessor(); - headers.setHeader(DESTINATION_HEADER, "/test/handlerThrowsExc"); - Message message = MessageBuilder.withPayload(new byte[0]).setHeaders(headers).build(); - this.messageHandler.handleMessage(message); + this.messageHandler.handleMessage(toDestination("/test/handlerThrowsExc")); assertEquals("illegalStateException", this.testController.method); assertNotNull(this.testController.arguments.get("exception")); + } + private Message toDestination(String destination) { + return MessageBuilder.withPayload(new byte[0]).setHeader(DESTINATION_HEADER, destination).build(); } + private static class TestController { public String method; @@ -179,7 +172,7 @@ public class AbstractMethodMessageHandlerTests { } - private static class MyMethodMessageHandler extends AbstractMethodMessageHandler { + private static class TestMethodMessageHandler extends AbstractMethodMessageHandler { private PathMatcher pathMatcher = new AntPathMatcher(); @@ -245,7 +238,7 @@ public class AbstractMethodMessageHandlerTests { } @Override - protected Comparator getMappingComparator(Message message) { + protected Comparator getMappingComparator(final Message message) { return new Comparator() { @Override public int compare(String info1, String info2) { @@ -258,13 +251,13 @@ public class AbstractMethodMessageHandlerTests { @Override protected AbstractExceptionHandlerMethodResolver createExceptionHandlerMethodResolverFor(Class beanType) { - return new MyExceptionHandlerMethodResolver(beanType); + return new TestExceptionHandlerMethodResolver(beanType); } } - private static class MyExceptionHandlerMethodResolver extends AbstractExceptionHandlerMethodResolver { + private static class TestExceptionHandlerMethodResolver extends AbstractExceptionHandlerMethodResolver { - public MyExceptionHandlerMethodResolver(Class handlerType) { + public TestExceptionHandlerMethodResolver(Class handlerType) { super(initExceptionMappings(handlerType)); } 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 4b751179e0..b118c6dcbf 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 @@ -43,7 +43,9 @@ import org.springframework.validation.annotation.Validated; import static org.junit.Assert.*; /** - * Test fixture for {@link org.springframework.messaging.simp.annotation.support.SimpAnnotationMethodMessageHandler}. + * Test fixture for + * {@link org.springframework.messaging.simp.annotation.support.SimpAnnotationMethodMessageHandler}. + * * @author Rossen Stoyanchev * @author Brian Clozel */ @@ -56,8 +58,10 @@ public class SimpAnnotationMethodMessageHandlerTests { @Before public void setup() { + SubscribableChannel channel = Mockito.mock(SubscribableChannel.class); SimpMessageSendingOperations brokerTemplate = new SimpMessagingTemplate(channel); + this.messageHandler = new TestSimpAnnotationMethodMessageHandler(brokerTemplate, channel, channel); this.messageHandler.setApplicationContext(new StaticApplicationContext()); this.messageHandler.setValidator(new StringNotEmptyValidator()); @@ -98,8 +102,7 @@ public class SimpAnnotationMethodMessageHandlerTests { public void subscribeEventDestinationVariableResolution() { SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.create(SimpMessageType.SUBSCRIBE); headers.setDestination("/pre/sub/bar/value"); - Message message = MessageBuilder.withPayload(new byte[0]) - .copyHeaders(headers.toMap()).build(); + Message message = MessageBuilder.withPayload(new byte[0]).copyHeaders(headers.toMap()).build(); this.messageHandler.handleMessage(message); assertEquals("subscribeEventDestinationVariable", this.testController.method); diff --git a/spring-websocket/src/test/java/org/springframework/web/socket/messaging/SimpAnnotationMethodIntegrationTests.java b/spring-websocket/src/test/java/org/springframework/web/socket/messaging/StompWebSocketIntegrationTests.java similarity index 97% rename from spring-websocket/src/test/java/org/springframework/web/socket/messaging/SimpAnnotationMethodIntegrationTests.java rename to spring-websocket/src/test/java/org/springframework/web/socket/messaging/StompWebSocketIntegrationTests.java index 066c94843a..8559b5a86b 100644 --- a/spring-websocket/src/test/java/org/springframework/web/socket/messaging/SimpAnnotationMethodIntegrationTests.java +++ b/spring-websocket/src/test/java/org/springframework/web/socket/messaging/StompWebSocketIntegrationTests.java @@ -55,10 +55,11 @@ import static org.springframework.web.socket.messaging.StompTextMessageBuilder.* /** * Integration tests with annotated message-handling methods. + * * @author Rossen Stoyanchev */ @RunWith(Parameterized.class) -public class SimpAnnotationMethodIntegrationTests extends AbstractWebSocketIntegrationTests { +public class StompWebSocketIntegrationTests extends AbstractWebSocketIntegrationTests { @Parameters public static Iterable arguments() { @@ -198,7 +199,7 @@ public class SimpAnnotationMethodIntegrationTests extends AbstractWebSocketInteg } @Configuration - @ComponentScan(basePackageClasses=SimpAnnotationMethodIntegrationTests.class, + @ComponentScan(basePackageClasses=StompWebSocketIntegrationTests.class, useDefaultFilters=false, includeFilters=@ComponentScan.Filter(IntegrationTestController.class)) static class TestMessageBrokerConfigurer extends AbstractWebSocketMessageBrokerConfigurer {