|
|
@ -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)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|