Polish message method handling tests
This commit is contained in:
parent
db9b6fa28e
commit
0db2f79bdb
|
@ -20,7 +20,6 @@ import org.hamcrest.Matchers;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import org.springframework.beans.DirectFieldAccessor;
|
|
||||||
import org.springframework.context.support.StaticApplicationContext;
|
import org.springframework.context.support.StaticApplicationContext;
|
||||||
import org.springframework.messaging.Message;
|
import org.springframework.messaging.Message;
|
||||||
import org.springframework.messaging.handler.DestinationPatternsMessageCondition;
|
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.HandlerMethodSelector;
|
||||||
import org.springframework.messaging.handler.annotation.support.MessageMethodArgumentResolver;
|
import org.springframework.messaging.handler.annotation.support.MessageMethodArgumentResolver;
|
||||||
import org.springframework.messaging.support.MessageBuilder;
|
import org.springframework.messaging.support.MessageBuilder;
|
||||||
import org.springframework.messaging.support.MessageHeaderAccessor;
|
|
||||||
import org.springframework.util.AntPathMatcher;
|
import org.springframework.util.AntPathMatcher;
|
||||||
import org.springframework.util.PathMatcher;
|
import org.springframework.util.PathMatcher;
|
||||||
import org.springframework.util.ReflectionUtils.MethodFilter;
|
import org.springframework.util.ReflectionUtils.MethodFilter;
|
||||||
|
@ -41,26 +39,31 @@ import static org.junit.Assert.assertNotNull;
|
||||||
import static org.junit.Assert.assertThat;
|
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 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;
|
private TestController testController;
|
||||||
|
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setup() {
|
public void setup() {
|
||||||
List<String> destinationPrefixes = new ArrayList<String>();
|
|
||||||
destinationPrefixes.add("/test/");
|
|
||||||
|
|
||||||
this.messageHandler = new MyMethodMessageHandler();
|
List<String> destinationPrefixes = Arrays.asList("/test");
|
||||||
|
|
||||||
|
this.messageHandler = new TestMethodMessageHandler();
|
||||||
this.messageHandler.setApplicationContext(new StaticApplicationContext());
|
this.messageHandler.setApplicationContext(new StaticApplicationContext());
|
||||||
this.messageHandler.setDestinationPrefixes(destinationPrefixes);
|
this.messageHandler.setDestinationPrefixes(destinationPrefixes);
|
||||||
this.messageHandler.afterPropertiesSet();
|
this.messageHandler.afterPropertiesSet();
|
||||||
|
|
||||||
this.testController = new TestController();
|
this.testController = new TestController();
|
||||||
this.messageHandler.registerHandler(this.testController);
|
this.messageHandler.registerHandler(this.testController);
|
||||||
}
|
}
|
||||||
|
@ -73,9 +76,7 @@ public class AbstractMethodMessageHandlerTests {
|
||||||
@Test
|
@Test
|
||||||
public void registeredMappings() {
|
public void registeredMappings() {
|
||||||
|
|
||||||
DirectFieldAccessor fieldAccessor = new DirectFieldAccessor(this.messageHandler);
|
Map<String, HandlerMethod> handlerMethods = this.messageHandler.getHandlerMethods();
|
||||||
Map<String, HandlerMethod> handlerMethods = (Map<String, HandlerMethod>)
|
|
||||||
fieldAccessor.getPropertyValue("handlerMethods");
|
|
||||||
|
|
||||||
assertNotNull(handlerMethods);
|
assertNotNull(handlerMethods);
|
||||||
assertThat(handlerMethods.keySet(), Matchers.hasSize(3));
|
assertThat(handlerMethods.keySet(), Matchers.hasSize(3));
|
||||||
|
@ -85,12 +86,9 @@ public class AbstractMethodMessageHandlerTests {
|
||||||
public void antPatchMatchWildcard() throws Exception {
|
public void antPatchMatchWildcard() throws Exception {
|
||||||
|
|
||||||
Method method = this.testController.getClass().getMethod("handlerPathMatchWildcard");
|
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();
|
this.messageHandler.handleMessage(toDestination("/test/handlerPathMatchFoo"));
|
||||||
headers.setHeader(DESTINATION_HEADER, "/test/handlerPathMatchFoo");
|
|
||||||
Message<?> message = MessageBuilder.withPayload(new byte[0]).setHeaders(headers).build();
|
|
||||||
this.messageHandler.handleMessage(message);
|
|
||||||
|
|
||||||
assertEquals("pathMatchWildcard", this.testController.method);
|
assertEquals("pathMatchWildcard", this.testController.method);
|
||||||
}
|
}
|
||||||
|
@ -104,39 +102,34 @@ public class AbstractMethodMessageHandlerTests {
|
||||||
method = this.testController.getClass().getMethod("secondBestMatch");
|
method = this.testController.getClass().getMethod("secondBestMatch");
|
||||||
this.messageHandler.registerHandlerMethod(this.testController, method, "/bestmatch/*/*");
|
this.messageHandler.registerHandlerMethod(this.testController, method, "/bestmatch/*/*");
|
||||||
|
|
||||||
MessageHeaderAccessor headers = new MessageHeaderAccessor();
|
this.messageHandler.handleMessage(toDestination("/test/bestmatch/bar/path"));
|
||||||
headers.setHeader(DESTINATION_HEADER, "/test/bestmatch/bar/path");
|
|
||||||
Message<?> message = MessageBuilder.withPayload(new byte[0]).setHeaders(headers).build();
|
|
||||||
this.messageHandler.handleMessage(message);
|
|
||||||
|
|
||||||
assertEquals("bestMatch", this.testController.method);
|
assertEquals("bestMatch", this.testController.method);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void argumentResolver() {
|
public void argumentResolution() {
|
||||||
|
|
||||||
MessageHeaderAccessor headers = new MessageHeaderAccessor();
|
this.messageHandler.handleMessage(toDestination("/test/handlerArgumentResolver"));
|
||||||
headers.setHeader(DESTINATION_HEADER, "/test/handlerArgumentResolver");
|
|
||||||
Message<?> message = MessageBuilder.withPayload(new byte[0]).setHeaders(headers).build();
|
|
||||||
this.messageHandler.handleMessage(message);
|
|
||||||
|
|
||||||
assertEquals("handlerArgumentResolver", this.testController.method);
|
assertEquals("handlerArgumentResolver", this.testController.method);
|
||||||
assertNotNull(this.testController.arguments.get("message"));
|
assertNotNull(this.testController.arguments.get("message"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void exceptionResolver() {
|
public void exceptionHandled() {
|
||||||
|
|
||||||
MessageHeaderAccessor headers = new MessageHeaderAccessor();
|
this.messageHandler.handleMessage(toDestination("/test/handlerThrowsExc"));
|
||||||
headers.setHeader(DESTINATION_HEADER, "/test/handlerThrowsExc");
|
|
||||||
Message<?> message = MessageBuilder.withPayload(new byte[0]).setHeaders(headers).build();
|
|
||||||
this.messageHandler.handleMessage(message);
|
|
||||||
|
|
||||||
assertEquals("illegalStateException", this.testController.method);
|
assertEquals("illegalStateException", this.testController.method);
|
||||||
assertNotNull(this.testController.arguments.get("exception"));
|
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 {
|
private static class TestController {
|
||||||
|
|
||||||
public String method;
|
public String method;
|
||||||
|
@ -179,7 +172,7 @@ public class AbstractMethodMessageHandlerTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private static class MyMethodMessageHandler extends AbstractMethodMessageHandler<String> {
|
private static class TestMethodMessageHandler extends AbstractMethodMessageHandler<String> {
|
||||||
|
|
||||||
private PathMatcher pathMatcher = new AntPathMatcher();
|
private PathMatcher pathMatcher = new AntPathMatcher();
|
||||||
|
|
||||||
|
@ -245,7 +238,7 @@ public class AbstractMethodMessageHandlerTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected Comparator<String> getMappingComparator(Message<?> message) {
|
protected Comparator<String> getMappingComparator(final Message<?> message) {
|
||||||
return new Comparator<String>() {
|
return new Comparator<String>() {
|
||||||
@Override
|
@Override
|
||||||
public int compare(String info1, String info2) {
|
public int compare(String info1, String info2) {
|
||||||
|
@ -258,13 +251,13 @@ public class AbstractMethodMessageHandlerTests {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected AbstractExceptionHandlerMethodResolver createExceptionHandlerMethodResolverFor(Class<?> beanType) {
|
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));
|
super(initExceptionMappings(handlerType));
|
||||||
}
|
}
|
||||||
|
|
|
@ -43,7 +43,9 @@ import org.springframework.validation.annotation.Validated;
|
||||||
import static org.junit.Assert.*;
|
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 Rossen Stoyanchev
|
||||||
* @author Brian Clozel
|
* @author Brian Clozel
|
||||||
*/
|
*/
|
||||||
|
@ -56,8 +58,10 @@ public class SimpAnnotationMethodMessageHandlerTests {
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setup() {
|
public void setup() {
|
||||||
|
|
||||||
SubscribableChannel channel = Mockito.mock(SubscribableChannel.class);
|
SubscribableChannel channel = Mockito.mock(SubscribableChannel.class);
|
||||||
SimpMessageSendingOperations brokerTemplate = new SimpMessagingTemplate(channel);
|
SimpMessageSendingOperations brokerTemplate = new SimpMessagingTemplate(channel);
|
||||||
|
|
||||||
this.messageHandler = new TestSimpAnnotationMethodMessageHandler(brokerTemplate, channel, channel);
|
this.messageHandler = new TestSimpAnnotationMethodMessageHandler(brokerTemplate, channel, channel);
|
||||||
this.messageHandler.setApplicationContext(new StaticApplicationContext());
|
this.messageHandler.setApplicationContext(new StaticApplicationContext());
|
||||||
this.messageHandler.setValidator(new StringNotEmptyValidator());
|
this.messageHandler.setValidator(new StringNotEmptyValidator());
|
||||||
|
@ -98,8 +102,7 @@ public class SimpAnnotationMethodMessageHandlerTests {
|
||||||
public void subscribeEventDestinationVariableResolution() {
|
public void subscribeEventDestinationVariableResolution() {
|
||||||
SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.create(SimpMessageType.SUBSCRIBE);
|
SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.create(SimpMessageType.SUBSCRIBE);
|
||||||
headers.setDestination("/pre/sub/bar/value");
|
headers.setDestination("/pre/sub/bar/value");
|
||||||
Message<?> message = MessageBuilder.withPayload(new byte[0])
|
Message<?> message = MessageBuilder.withPayload(new byte[0]).copyHeaders(headers.toMap()).build();
|
||||||
.copyHeaders(headers.toMap()).build();
|
|
||||||
this.messageHandler.handleMessage(message);
|
this.messageHandler.handleMessage(message);
|
||||||
|
|
||||||
assertEquals("subscribeEventDestinationVariable", this.testController.method);
|
assertEquals("subscribeEventDestinationVariable", this.testController.method);
|
||||||
|
|
|
@ -55,10 +55,11 @@ import static org.springframework.web.socket.messaging.StompTextMessageBuilder.*
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Integration tests with annotated message-handling methods.
|
* Integration tests with annotated message-handling methods.
|
||||||
|
*
|
||||||
* @author Rossen Stoyanchev
|
* @author Rossen Stoyanchev
|
||||||
*/
|
*/
|
||||||
@RunWith(Parameterized.class)
|
@RunWith(Parameterized.class)
|
||||||
public class SimpAnnotationMethodIntegrationTests extends AbstractWebSocketIntegrationTests {
|
public class StompWebSocketIntegrationTests extends AbstractWebSocketIntegrationTests {
|
||||||
|
|
||||||
@Parameters
|
@Parameters
|
||||||
public static Iterable<Object[]> arguments() {
|
public static Iterable<Object[]> arguments() {
|
||||||
|
@ -198,7 +199,7 @@ public class SimpAnnotationMethodIntegrationTests extends AbstractWebSocketInteg
|
||||||
}
|
}
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
@ComponentScan(basePackageClasses=SimpAnnotationMethodIntegrationTests.class,
|
@ComponentScan(basePackageClasses=StompWebSocketIntegrationTests.class,
|
||||||
useDefaultFilters=false,
|
useDefaultFilters=false,
|
||||||
includeFilters=@ComponentScan.Filter(IntegrationTestController.class))
|
includeFilters=@ComponentScan.Filter(IntegrationTestController.class))
|
||||||
static class TestMessageBrokerConfigurer extends AbstractWebSocketMessageBrokerConfigurer {
|
static class TestMessageBrokerConfigurer extends AbstractWebSocketMessageBrokerConfigurer {
|
Loading…
Reference in New Issue