removing HandlerMethodResolver and impls, since we now depend on SpEL for that functionality
This commit is contained in:
@@ -1,33 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2008 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.integration.handler;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
import org.springframework.integration.Message;
|
||||
|
||||
/**
|
||||
* Strategy interface for resolving a Method that should be responsible for
|
||||
* handling a given {@link Message}.
|
||||
*
|
||||
* @author Mark Fisher
|
||||
*/
|
||||
public interface HandlerMethodResolver {
|
||||
|
||||
Method resolveHandlerMethod(Message<?> message);
|
||||
|
||||
}
|
||||
@@ -1,157 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2009 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.integration.handler;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.ParameterizedType;
|
||||
import java.lang.reflect.Type;
|
||||
import java.lang.reflect.TypeVariable;
|
||||
import java.lang.reflect.WildcardType;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import org.springframework.integration.Message;
|
||||
import org.springframework.integration.util.ClassUtils;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* An implementation of {@link HandlerMethodResolver} that matches the payload
|
||||
* type of the Message against the expected type of its candidate methods.
|
||||
*
|
||||
* @author Mark Fisher
|
||||
*/
|
||||
public class PayloadTypeMatchingHandlerMethodResolver implements HandlerMethodResolver {
|
||||
|
||||
private final Map<Class<?>, Method> methodMap = new ConcurrentHashMap<Class<?>, Method>();
|
||||
|
||||
private volatile Method fallbackMethod;
|
||||
|
||||
|
||||
public PayloadTypeMatchingHandlerMethodResolver(Method... candidates) {
|
||||
Assert.notEmpty(candidates, "candidates must not be empty");
|
||||
this.initMethodMap(candidates);
|
||||
}
|
||||
|
||||
|
||||
public Method resolveHandlerMethod(Message<?> message) {
|
||||
Method method = this.methodMap.get(message.getClass());
|
||||
if (method == null) {
|
||||
Class<?> payloadType = message.getPayload().getClass();
|
||||
method = this.methodMap.get(payloadType);
|
||||
if (method == null) {
|
||||
method = this.findClosestMatch(payloadType);
|
||||
}
|
||||
if (method == null) {
|
||||
method = this.fallbackMethod;
|
||||
}
|
||||
}
|
||||
return method;
|
||||
}
|
||||
|
||||
private void initMethodMap(Method[] candidates) {
|
||||
for (Method method : candidates) {
|
||||
Class<?> expectedType = this.determineExpectedType(method);
|
||||
if (expectedType == null) {
|
||||
Assert.isTrue(fallbackMethod == null,
|
||||
"At most one method can expect only Message headers rather than a Message or payload, " +
|
||||
"but found two: [" + method + "] and [" + this.fallbackMethod + "]");
|
||||
this.fallbackMethod = method;
|
||||
}
|
||||
else {
|
||||
Assert.isTrue(!this.methodMap.containsKey(expectedType),
|
||||
"More than one method matches type [" + expectedType +
|
||||
"]. Consider using annotations or providing a method name.");
|
||||
this.methodMap.put(expectedType, method);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private Class<?> determineExpectedType(Method method) {
|
||||
Class<?> expectedType = null;
|
||||
Type[] parameterTypes = method.getGenericParameterTypes();
|
||||
Annotation[][] parameterAnnotations = method.getParameterAnnotations();
|
||||
for (int i = 0; i < parameterTypes.length; i++) {
|
||||
if (!HandlerMethodUtils.containsHeaderAnnotation(parameterAnnotations[i])) {
|
||||
Assert.isTrue(expectedType == null,
|
||||
"Message-handling method must only have one parameter expecting a Message or Message payload."
|
||||
+ " Other parameters may be included but only if they have @Header or @Headers annotations.");
|
||||
Type parameterType = extractRawTypeIfGeneric(parameterTypes[i]);
|
||||
if (parameterType instanceof ParameterizedType) {
|
||||
ParameterizedType parameterizedType = (ParameterizedType) parameterType;
|
||||
Type rawType = extractRawTypeIfGeneric(parameterizedType.getRawType());
|
||||
if (rawType instanceof Class<?>) {
|
||||
Class<?> rawTypeClass = (Class<?>) rawType;
|
||||
if (Message.class.isAssignableFrom(rawTypeClass)) {
|
||||
expectedType = this.determineExpectedTypeFromParameterizedMessageType(parameterizedType);
|
||||
}
|
||||
else {
|
||||
expectedType = rawTypeClass;
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (parameterType instanceof Class<?>) {
|
||||
expectedType = (Class<?>) parameterType;
|
||||
}
|
||||
Assert.notNull(expectedType, "Failed to determine expected type for parameter ["
|
||||
+ parameterType + "] on Method [" + method + "]");
|
||||
}
|
||||
}
|
||||
return expectedType;
|
||||
}
|
||||
|
||||
private Type extractRawTypeIfGeneric(Type parameterType) {
|
||||
if (parameterType instanceof TypeVariable<?>) {
|
||||
parameterType = ((TypeVariable<?>) parameterType).getBounds()[0];
|
||||
}
|
||||
return parameterType;
|
||||
}
|
||||
|
||||
private Method findClosestMatch(Class<?> payloadType) {
|
||||
Set<Class<?>> expectedTypes = this.methodMap.keySet();
|
||||
Class<?> match = ClassUtils.findClosestMatch(payloadType, expectedTypes, true);
|
||||
Method matchingMethod = null;
|
||||
if (match != null) {
|
||||
matchingMethod = this.methodMap.get(match);
|
||||
if (matchingMethod != null) {
|
||||
this.methodMap.put(payloadType, matchingMethod);
|
||||
}
|
||||
}
|
||||
return matchingMethod;
|
||||
}
|
||||
|
||||
private Class<?> determineExpectedTypeFromParameterizedMessageType(ParameterizedType parameterizedType) {
|
||||
Class<?> expectedType = null;
|
||||
Type actualType = extractRawTypeIfGeneric(parameterizedType.getActualTypeArguments()[0]);
|
||||
if (actualType instanceof WildcardType) {
|
||||
WildcardType wildcardType = (WildcardType) actualType;
|
||||
if (wildcardType.getUpperBounds().length == 1) {
|
||||
Type upperBound = wildcardType.getUpperBounds()[0];
|
||||
if (upperBound instanceof Class<?>) {
|
||||
expectedType = (Class<?>) upperBound;
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (actualType instanceof Class<?>) {
|
||||
expectedType = (Class<?>) actualType;
|
||||
}
|
||||
|
||||
return expectedType;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,49 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2008 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.integration.handler;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
import org.springframework.integration.Message;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* An implementation of {@link HandlerMethodResolver} that always returns the
|
||||
* same Method instance. Used when the exact Method is indicated explicitly
|
||||
* or otherwise resolvable in advance based on static metadata.
|
||||
*
|
||||
* @author Mark Fisher
|
||||
* @author Oleg Zhurakousky
|
||||
*/
|
||||
public class StaticHandlerMethodResolver implements HandlerMethodResolver {
|
||||
|
||||
private final Method method;
|
||||
|
||||
|
||||
public StaticHandlerMethodResolver(Method method) {
|
||||
Assert.notNull(method, "method must not be null");
|
||||
Assert.isTrue(HandlerMethodUtils.isValidHandlerMethod(method),
|
||||
"Invalid Message-handling method [" + method + "]");
|
||||
this.method = method;
|
||||
}
|
||||
|
||||
|
||||
public Method resolveHandlerMethod(Message<?> message) {
|
||||
return this.method;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,66 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2010 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.integration.handler;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.integration.message.GenericMessage;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
|
||||
/**
|
||||
* @author Mark Fisher
|
||||
*/
|
||||
public class HandlerMethodInheritanceTests {
|
||||
|
||||
@Test // INT-506
|
||||
public void overriddenMethodExcludedFromCandidateList() {
|
||||
Method[] candidates = HandlerMethodUtils.getCandidateHandlerMethods(new TestSubclass());
|
||||
assertEquals(1, candidates.length);
|
||||
Method expected = ReflectionUtils.findMethod(
|
||||
TestSubclass.class, "test", new Class<?>[] { String.class });
|
||||
assertEquals(expected, candidates[0]);
|
||||
}
|
||||
|
||||
@Test // INT-506
|
||||
public void overridingMethodResolves() {
|
||||
Method[] candidates = HandlerMethodUtils.getCandidateHandlerMethods(new TestSubclass());
|
||||
PayloadTypeMatchingHandlerMethodResolver resolver = new PayloadTypeMatchingHandlerMethodResolver(candidates);
|
||||
Method resolved = resolver.resolveHandlerMethod(new GenericMessage<String>("test"));
|
||||
Method expected = ReflectionUtils.findMethod(
|
||||
TestSubclass.class, "test", new Class<?>[] { String.class });
|
||||
assertEquals(expected, resolved);
|
||||
}
|
||||
|
||||
|
||||
public static class TestSuperclass {
|
||||
|
||||
public void test(String s) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static class TestSubclass extends TestSuperclass {
|
||||
|
||||
public void test(String s) {
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,147 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2010 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.integration.handler;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Date;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.integration.annotation.Header;
|
||||
import org.springframework.integration.message.GenericMessage;
|
||||
|
||||
/**
|
||||
* @author Mark Fisher
|
||||
*/
|
||||
public class PayloadTypeMatchingHandlerMethodResolverTests {
|
||||
|
||||
private PayloadTypeMatchingHandlerMethodResolver resolver;
|
||||
|
||||
|
||||
@Before
|
||||
public void initResolver() {
|
||||
Method[] candidates = HandlerMethodUtils.getCandidateHandlerMethods(new TestService());
|
||||
resolver = new PayloadTypeMatchingHandlerMethodResolver(candidates);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void stringPayload() throws Exception {
|
||||
Class<?>[] types = new Class<?>[] { String.class };
|
||||
Method expected = TestService.class.getMethod("stringPayload", types);
|
||||
Method resolved = resolver.resolveHandlerMethod(new GenericMessage<String>("foo"));
|
||||
assertEquals(expected, resolved);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void exactMatch() throws Exception {
|
||||
Class<?>[] types = new Class<?>[] { TestFooImpl1.class };
|
||||
Method expected = TestService.class.getMethod("fooImpl1Payload", types);
|
||||
Method resolved = resolver.resolveHandlerMethod(new GenericMessage<TestFoo>(new TestFooImpl1()));
|
||||
assertEquals(expected, resolved);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void interfaceMatch() throws Exception {
|
||||
Class<?>[] types = new Class<?>[] { TestFoo.class };
|
||||
Method expected = TestService.class.getMethod("fooInterfacePayload", types);
|
||||
Method resolved = resolver.resolveHandlerMethod(new GenericMessage<TestFoo>(new TestFooImpl2()));
|
||||
assertEquals(expected, resolved);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void superclassMatch() throws Exception {
|
||||
Class<?>[] types = new Class<?>[] { TestFooImpl1.class };
|
||||
Method expected = TestService.class.getMethod("fooImpl1Payload", types);
|
||||
Method resolved = resolver.resolveHandlerMethod(new GenericMessage<TestFoo>(new TestFooImpl1Subclass()));
|
||||
assertEquals(expected, resolved);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void interfaceOfSuperclassMatch() throws Exception {
|
||||
Class<?>[] types = new Class<?>[] { TestFoo.class };
|
||||
Method expected = TestService.class.getMethod("fooInterfacePayload", types);
|
||||
Method resolved = resolver.resolveHandlerMethod(new GenericMessage<TestFoo>(new TestFooImpl2Subclass()));
|
||||
assertEquals(expected, resolved);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void numberSuperclassMatch() throws Exception {
|
||||
Class<?>[] types = new Class<?>[] { Number.class };
|
||||
Method expected = TestService.class.getMethod("numberPayload", types);
|
||||
Method resolved = resolver.resolveHandlerMethod(new GenericMessage<Long>(new Long(99)));
|
||||
assertEquals(expected, resolved);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void payloadAndHeaderMethod() throws Exception {
|
||||
Class<?>[] types = new Class<?>[] { Integer.class, String.class };
|
||||
Method expected = TestService.class.getMethod("integerPayloadAndHeader", types);
|
||||
Method resolved = resolver.resolveHandlerMethod(new GenericMessage<Integer>(new Integer(123)));
|
||||
assertEquals(expected, resolved);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void fallbackToHeaderOnlyMethod() throws Exception {
|
||||
Class<?>[] types = new Class<?>[] { String.class };
|
||||
Method expected = TestService.class.getMethod("headerOnlyMethod", types);
|
||||
Method resolved = resolver.resolveHandlerMethod(new GenericMessage<Date>(new Date()));
|
||||
assertEquals(expected, resolved);
|
||||
}
|
||||
|
||||
|
||||
public static class TestService {
|
||||
|
||||
public void stringPayload(String s) {
|
||||
}
|
||||
|
||||
public void fooInterfacePayload(TestFoo foo) {
|
||||
}
|
||||
|
||||
public void fooImpl1Payload(TestFooImpl1 foo) {
|
||||
}
|
||||
|
||||
public void numberPayload(Number n) {
|
||||
}
|
||||
|
||||
public void headerOnlyMethod(@Header("testHeader") String s) {
|
||||
}
|
||||
|
||||
public void integerPayloadAndHeader(Integer n, @Header("testHeader") String s2) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public interface TestFoo {
|
||||
}
|
||||
|
||||
public class TestFooImpl1 implements TestFoo {
|
||||
}
|
||||
|
||||
public class TestFooImpl2 implements TestFoo {
|
||||
}
|
||||
|
||||
public class TestFooImpl1Subclass extends TestFooImpl1 {
|
||||
}
|
||||
|
||||
public class TestFooImpl2Subclass extends TestFooImpl2 {
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,260 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2010 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.integration.handler;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Date;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.integration.Message;
|
||||
import org.springframework.integration.annotation.Header;
|
||||
import org.springframework.integration.message.GenericMessage;
|
||||
import org.springframework.integration.support.MessageBuilder;
|
||||
|
||||
/**
|
||||
* @author Mark Fisher
|
||||
*/
|
||||
public class PayloadTypeMatchingHandlerMethodResolverWithMessageParameterTests {
|
||||
|
||||
private PayloadTypeMatchingHandlerMethodResolver resolver;
|
||||
|
||||
|
||||
@Before
|
||||
public void initResolver() {
|
||||
Method[] candidates = HandlerMethodUtils.getCandidateHandlerMethods(new TestService());
|
||||
resolver = new PayloadTypeMatchingHandlerMethodResolver(candidates);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void stringPayload() throws Exception {
|
||||
Class<?>[] types = new Class<?>[] { Message.class };
|
||||
Method expected = TestService.class.getMethod("stringPayload", types);
|
||||
Method resolved = resolver.resolveHandlerMethod(new TestStringMessage("foo"));
|
||||
assertEquals(expected, resolved);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void exactMatch() throws Exception {
|
||||
Class<?>[] types = new Class<?>[] { Message.class };
|
||||
Method expected = TestService.class.getMethod("fooImpl1Payload", types);
|
||||
Method resolved = resolver.resolveHandlerMethod(new GenericMessage<TestFoo>(new TestFooImpl1()));
|
||||
assertEquals(expected, resolved);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void interfaceMatch() throws Exception {
|
||||
Class<?>[] types = new Class<?>[] { Message.class };
|
||||
Method expected = TestService.class.getMethod("fooInterfacePayload", types);
|
||||
Method resolved = resolver.resolveHandlerMethod(new GenericMessage<TestFoo>(new TestFooImpl2()));
|
||||
assertEquals(expected, resolved);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void superclassMatch() throws Exception {
|
||||
Class<?>[] types = new Class<?>[] { Message.class };
|
||||
Method expected = TestService.class.getMethod("fooImpl1Payload", types);
|
||||
Method resolved = resolver.resolveHandlerMethod(new GenericMessage<TestFoo>(new TestFooImpl1Subclass()));
|
||||
assertEquals(expected, resolved);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void interfaceOfSuperclassMatch() throws Exception {
|
||||
Class<?>[] types = new Class<?>[] { Message.class };
|
||||
Method expected = TestService.class.getMethod("fooInterfacePayload", types);
|
||||
Method resolved = resolver.resolveHandlerMethod(new GenericMessage<TestFoo>(new TestFooImpl2Subclass()));
|
||||
assertEquals(expected, resolved);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void numberSuperclassMatch() throws Exception {
|
||||
Class<?>[] types = new Class<?>[] { Message.class };
|
||||
Method expected = TestService.class.getMethod("numberPayload", types);
|
||||
Method resolved = resolver.resolveHandlerMethod(new GenericMessage<Long>(new Long(99)));
|
||||
assertEquals(expected, resolved);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void payloadAndHeaderMethod() throws Exception {
|
||||
Class<?>[] types = new Class<?>[] { Message.class, String.class };
|
||||
Method expected = TestService.class.getMethod("integerPayloadAndHeader", types);
|
||||
Method resolved = resolver.resolveHandlerMethod(new GenericMessage<Integer>(new Integer(123)));
|
||||
assertEquals(expected, resolved);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void fallbackToHeaderOnlyMethod() throws Exception {
|
||||
Class<?>[] types = new Class<?>[] { String.class };
|
||||
Method expected = TestService.class.getMethod("headerOnlyMethod", types);
|
||||
Method resolved = resolver.resolveHandlerMethod(new GenericMessage<Date>(new Date()));
|
||||
assertEquals(expected, resolved);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStringMessageTypedParameter() throws Exception {
|
||||
Object service = new TestServiceWithMessageTypes();
|
||||
Method[] candidates = HandlerMethodUtils.getCandidateHandlerMethods(service);
|
||||
PayloadTypeMatchingHandlerMethodResolver methodResovler
|
||||
= new PayloadTypeMatchingHandlerMethodResolver(candidates);
|
||||
Class<?>[] types = new Class<?>[] { TestStringMessage.class };
|
||||
Method expected = TestServiceWithMessageTypes.class.getMethod("stringMessage", types);
|
||||
Message<?> message = new TestStringMessage("foo");
|
||||
Method resolved = methodResovler.resolveHandlerMethod(message);
|
||||
assertEquals(expected, resolved);
|
||||
assertEquals("foo", resolved.invoke(service, message));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMessageParameterizedWithString() throws Exception {
|
||||
Object service = new TestServiceWithMessageTypes();
|
||||
Method[] candidates = HandlerMethodUtils.getCandidateHandlerMethods(service);
|
||||
PayloadTypeMatchingHandlerMethodResolver methodResovler
|
||||
= new PayloadTypeMatchingHandlerMethodResolver(candidates);
|
||||
Class<?>[] types = new Class<?>[] { Message.class };
|
||||
Method expected = TestServiceWithMessageTypes.class.getMethod("stringParameterizedMessage", types);
|
||||
Message<?> message = new GenericMessage<String>("foo");
|
||||
Method resolved = methodResovler.resolveHandlerMethod(message);
|
||||
assertEquals(expected, resolved);
|
||||
assertEquals("foo", resolved.invoke(service, message));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUnboundedWildcard() throws Exception {
|
||||
Object service = new TestServiceWithMessageTypes();
|
||||
Method[] candidates = HandlerMethodUtils.getCandidateHandlerMethods(service);
|
||||
PayloadTypeMatchingHandlerMethodResolver methodResovler
|
||||
= new PayloadTypeMatchingHandlerMethodResolver(candidates);
|
||||
Class<?>[] types = new Class<?>[] { Message.class };
|
||||
Method expected = TestServiceWithMessageTypes.class.getMethod("unboundedWildcardMessage", types);
|
||||
Date date = new Date();
|
||||
Message<?> message = new GenericMessage<Date>(date);
|
||||
Method resolved = methodResovler.resolveHandlerMethod(message);
|
||||
assertEquals(expected, resolved);
|
||||
assertEquals(date, resolved.invoke(service, message));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBoundedWildcard() throws Exception {
|
||||
Object service = new TestServiceWithMessageTypes();
|
||||
Method[] candidates = HandlerMethodUtils.getCandidateHandlerMethods(service);
|
||||
PayloadTypeMatchingHandlerMethodResolver methodResovler
|
||||
= new PayloadTypeMatchingHandlerMethodResolver(candidates);
|
||||
Class<?>[] types = new Class<?>[] { Message.class };
|
||||
Method expected = TestServiceWithMessageTypes.class.getMethod("boundedWildcardMessage", types);
|
||||
Message<?> message = MessageBuilder.withPayload(new Integer(123)).build();
|
||||
Method resolved = methodResovler.resolveHandlerMethod(message);
|
||||
assertEquals(expected, resolved);
|
||||
assertEquals(new Integer(123), resolved.invoke(service, message));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGenericSuperclass() throws Exception {
|
||||
Object service = new ConcreteTestService();
|
||||
Method[] candidates = HandlerMethodUtils.getCandidateHandlerMethods(service);
|
||||
PayloadTypeMatchingHandlerMethodResolver methodResolver =
|
||||
new PayloadTypeMatchingHandlerMethodResolver(candidates);
|
||||
Method expected = ConcreteTestService.class.getMethod("genericMethod", Message.class);
|
||||
Message<?> message = MessageBuilder.withPayload("SomeString").build();
|
||||
Method resolved = methodResolver.resolveHandlerMethod(message);
|
||||
assertEquals(expected, resolved);
|
||||
assertEquals(message.getPayload(), resolved.invoke(service, message));
|
||||
}
|
||||
|
||||
|
||||
public static class GenericTestService<T extends Message<K>, K> {
|
||||
public K genericMethod(T message) {
|
||||
return message.getPayload();
|
||||
}
|
||||
}
|
||||
|
||||
public static class ConcreteTestService extends GenericTestService<Message<String>, String> {
|
||||
|
||||
}
|
||||
|
||||
|
||||
public static class TestService {
|
||||
|
||||
public void stringPayload(Message<String> message) {
|
||||
}
|
||||
|
||||
public void fooInterfacePayload(Message<TestFoo> message) {
|
||||
}
|
||||
|
||||
public void fooImpl1Payload(Message<TestFooImpl1> message) {
|
||||
}
|
||||
|
||||
public void numberPayload(Message<Number> message) {
|
||||
}
|
||||
|
||||
public void headerOnlyMethod(@Header("testHeader") String s) {
|
||||
}
|
||||
|
||||
public void integerPayloadAndHeader(Message<Integer> message, @Header("testHeader") String s) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static class TestServiceWithMessageTypes {
|
||||
|
||||
public String stringMessage(TestStringMessage message) {
|
||||
return message.getPayload();
|
||||
}
|
||||
|
||||
public String stringParameterizedMessage(Message<String> message) {
|
||||
return message.getPayload();
|
||||
}
|
||||
|
||||
public Object unboundedWildcardMessage(Message<?> message) {
|
||||
return message.getPayload();
|
||||
}
|
||||
|
||||
public Number boundedWildcardMessage(Message<? extends Number> message) {
|
||||
return message.getPayload();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public interface TestFoo {
|
||||
}
|
||||
|
||||
public class TestFooImpl1 implements TestFoo {
|
||||
}
|
||||
|
||||
public class TestFooImpl2 implements TestFoo {
|
||||
}
|
||||
|
||||
public class TestFooImpl1Subclass extends TestFooImpl1 {
|
||||
}
|
||||
|
||||
public class TestFooImpl2Subclass extends TestFooImpl2 {
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
private static class TestStringMessage extends GenericMessage<String> {
|
||||
|
||||
private TestStringMessage(String payload) {
|
||||
super(payload);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,125 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2010 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.integration.handler;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.integration.annotation.Header;
|
||||
import org.springframework.integration.annotation.Headers;
|
||||
import org.springframework.integration.message.GenericMessage;
|
||||
|
||||
/**
|
||||
* @author Mark Fisher
|
||||
* @author Oleg Zhurakousky
|
||||
*/
|
||||
public class StaticHandlerMethodResolverTests {
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void methodDeclaredOnObjectIsNotValid() throws Exception {
|
||||
Method method = Object.class.getDeclaredMethod("equals", new Class<?>[] {Object.class});
|
||||
new StaticHandlerMethodResolver(method);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void privateMethodIsNotValid() throws Exception {
|
||||
Method method = TestBean.class.getDeclaredMethod("privateMethod", new Class<?>[] {String.class});
|
||||
new StaticHandlerMethodResolver(method);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void nullMethodIsNotValid() throws Exception {
|
||||
new StaticHandlerMethodResolver(null);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void ambiguousMethodIsNotValid() throws Exception {
|
||||
Class<?>[] types = new Class<?>[] { String.class, String.class };
|
||||
Method method = TestBean.class.getDeclaredMethod("ambiguousMethod", types);
|
||||
new StaticHandlerMethodResolver(method);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void validPayloadMethod() throws Exception {
|
||||
Method method = TestBean.class.getDeclaredMethod("payloadMethod", new Class<?>[] {String.class});
|
||||
HandlerMethodResolver resolver = new StaticHandlerMethodResolver(method);
|
||||
Method resolved = resolver.resolveHandlerMethod(new GenericMessage<String>("foo"));
|
||||
assertEquals(method, resolved);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void validNoArgMethod() throws Exception {
|
||||
Method method = TestBean.class.getDeclaredMethod("noArgMethod", new Class<?>[0]);
|
||||
new StaticHandlerMethodResolver(method);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void validHeaderMethod() throws Exception {
|
||||
Method method = TestBean.class.getDeclaredMethod("headerMethod", new Class<?>[] {String.class});
|
||||
HandlerMethodResolver resolver = new StaticHandlerMethodResolver(method);
|
||||
Method resolved = resolver.resolveHandlerMethod(new GenericMessage<String>("foo"));
|
||||
assertEquals(method, resolved);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void validHeaderMapMethod() throws Exception {
|
||||
Method method = TestBean.class.getDeclaredMethod("headerMapMethod", new Class<?>[] {Map.class});
|
||||
HandlerMethodResolver resolver = new StaticHandlerMethodResolver(method);
|
||||
Method resolved = resolver.resolveHandlerMethod(new GenericMessage<String>("foo"));
|
||||
assertEquals(method, resolved);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void validPayloadAndHeaderMethod() throws Exception {
|
||||
Class<?>[] types = new Class<?>[] { String.class, String.class };
|
||||
Method method = TestBean.class.getDeclaredMethod("payloadAndHeaderMethod", types);
|
||||
HandlerMethodResolver resolver = new StaticHandlerMethodResolver(method);
|
||||
Method resolved = resolver.resolveHandlerMethod(new GenericMessage<String>("foo"));
|
||||
assertEquals(method, resolved);
|
||||
}
|
||||
|
||||
|
||||
public static class TestBean {
|
||||
|
||||
public void noArgMethod() {
|
||||
}
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
private void privateMethod(String s) {
|
||||
}
|
||||
|
||||
public void ambiguousMethod(String s1, String s2) {
|
||||
}
|
||||
|
||||
public void payloadMethod(String s) {
|
||||
}
|
||||
|
||||
public void headerMethod(@Header("test") String s) {
|
||||
}
|
||||
|
||||
public void headerMapMethod(@Headers Map<String, Object> headerMap) {
|
||||
}
|
||||
|
||||
public void payloadAndHeaderMethod(String s1, @Header("test") String s2) {
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user