Added MethodResolver strategy interface and two implementations: AnnotationMethodResolver and DefaultMethodResolver. The MessageMappingMethodInvoker no longer resolves based on Annotations. Instead, the component that delegates to the invoker is responsible for resolving the Method instance (e.g. ServiceActivator now does this when its single Object constructor is invoked).

This commit is contained in:
Mark Fisher
2008-10-05 19:40:53 +00:00
parent 1f5e0b6cf1
commit 95c8b83a51
8 changed files with 433 additions and 88 deletions

View File

@@ -0,0 +1,120 @@
/*
* 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.endpoint;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.message.Message;
import org.springframework.integration.message.StringMessage;
/**
* @author Mark Fisher
*/
public class ServiceActivatorMethodResolutionTests {
@Test
public void singleAnnotationMatches() {
SingleAnnotationTestBean testBean = new SingleAnnotationTestBean();
ServiceActivatorEndpoint endpoint = new ServiceActivatorEndpoint(testBean);
QueueChannel outputChannel = new QueueChannel();
endpoint.setOutputChannel(outputChannel);
endpoint.afterPropertiesSet();
endpoint.onMessage(new StringMessage("foo"));
Message<?> result = outputChannel.receive(0);
assertEquals("FOO", result.getPayload());
}
@Test(expected = IllegalArgumentException.class)
public void multipleAnnotationFails() {
MultipleAnnotationTestBean testBean = new MultipleAnnotationTestBean();
new ServiceActivatorEndpoint(testBean);
}
@Test
public void singlePublicMethodMatches() {
SinglePublicMethodTestBean testBean = new SinglePublicMethodTestBean();
ServiceActivatorEndpoint endpoint = new ServiceActivatorEndpoint(testBean);
QueueChannel outputChannel = new QueueChannel();
endpoint.setOutputChannel(outputChannel);
endpoint.afterPropertiesSet();
endpoint.onMessage(new StringMessage("foo"));
Message<?> result = outputChannel.receive(0);
assertEquals("FOO", result.getPayload());
}
@Test(expected = IllegalArgumentException.class)
public void multiplePublicMethodFails() {
MultiplePublicMethodTestBean testBean = new MultiplePublicMethodTestBean();
new ServiceActivatorEndpoint(testBean);
}
private static class SingleAnnotationTestBean {
@ServiceActivator
public String upperCase(String s) {
return s.toUpperCase();
}
public String lowerCase(String s) {
return s.toLowerCase();
}
}
private static class MultipleAnnotationTestBean {
@ServiceActivator
public String upperCase(String s) {
return s.toUpperCase();
}
@ServiceActivator
public String lowerCase(String s) {
return s.toLowerCase();
}
}
private static class SinglePublicMethodTestBean {
public String upperCase(String s) {
return s.toUpperCase();
}
String lowerCase(String s) {
return s.toLowerCase();
}
}
private static class MultiplePublicMethodTestBean {
public String upperCase(String s) {
return s.toUpperCase();
}
public String lowerCase(String s) {
return s.toLowerCase();
}
}
}

View File

@@ -14,38 +14,42 @@
* limitations under the License.
*/
package org.springframework.integration.message;
package org.springframework.integration.util;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Method;
import org.junit.Test;
/**
* @author Mark Fisher
*/
public class MessageMappingMethodInvokerAnnotationTests {
public class AnnotationMethodResolverTests {
@Test
public void singleAnnotationMatches() {
SingleAnnotationTestBean testBean = new SingleAnnotationTestBean();
MessageMappingMethodInvoker invoker =
new MessageMappingMethodInvoker(testBean, TestAnnotation.class);
invoker.afterPropertiesSet();
String result = (String) invoker.invokeMethod("foo");
assertEquals("FOO", result);
public void singleAnnotation() {
AnnotationMethodResolver resolver = new AnnotationMethodResolver(TestAnnotation.class);
Method method = resolver.findMethod(SingleAnnotationTestBean.class);
assertNotNull(method);
}
@Test(expected = IllegalArgumentException.class)
public void multipleAnnotationMatches() {
MultipleAnnotationTestBean testBean = new MultipleAnnotationTestBean();
MessageMappingMethodInvoker invoker =
new MessageMappingMethodInvoker(testBean, TestAnnotation.class);
invoker.afterPropertiesSet();
public void multipleAnnotations() {
AnnotationMethodResolver resolver = new AnnotationMethodResolver(TestAnnotation.class);
resolver.findMethod(MultipleAnnotationTestBean.class);
}
@Test
public void noAnnotations() {
AnnotationMethodResolver resolver = new AnnotationMethodResolver(TestAnnotation.class);
Method method = resolver.findMethod(NoAnnotationTestBean.class);
assertNull(method);
}
@@ -61,6 +65,10 @@ public class MessageMappingMethodInvokerAnnotationTests {
public String upperCase(String s) {
return s.toUpperCase();
}
public String lowerCase(String s) {
return s.toLowerCase();
}
}
@@ -77,4 +85,16 @@ public class MessageMappingMethodInvokerAnnotationTests {
}
}
private static class NoAnnotationTestBean {
public String upperCase(String s) {
return s.toUpperCase();
}
String lowerCase(String s) {
return s.toLowerCase();
}
}
}

View File

@@ -0,0 +1,133 @@
/*
* 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.util;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Method;
import org.junit.Test;
/**
* @author Mark Fisher
*/
public class DefaultMethodResolverTests {
@Test
public void singleAnnotation() {
DefaultMethodResolver resolver = new DefaultMethodResolver(TestAnnotation.class);
Method method = resolver.findMethod(SingleAnnotationTestBean.class);
assertNotNull(method);
}
@Test(expected = IllegalArgumentException.class)
public void multipleAnnotations() {
DefaultMethodResolver resolver = new DefaultMethodResolver(TestAnnotation.class);
resolver.findMethod(MultipleAnnotationTestBean.class);
}
@Test
public void singlePublicMethod() {
DefaultMethodResolver resolver = new DefaultMethodResolver(TestAnnotation.class);
Method method = resolver.findMethod(SinglePublicMethodTestBean.class);
assertNotNull(method);
}
@Test(expected = IllegalArgumentException.class)
public void multiplePublicMethods() {
DefaultMethodResolver resolver = new DefaultMethodResolver(TestAnnotation.class);
resolver.findMethod(MultiplePublicMethodTestBean.class);
}
@Test
public void noPublicMethods() {
DefaultMethodResolver resolver = new DefaultMethodResolver(TestAnnotation.class);
Method method = resolver.findMethod(NoPublicMethodTestBean.class);
assertNull(method);
}
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
private static @interface TestAnnotation {
}
private static class SingleAnnotationTestBean {
@TestAnnotation
public String upperCase(String s) {
return s.toUpperCase();
}
public String lowerCase(String s) {
return s.toLowerCase();
}
}
private static class MultipleAnnotationTestBean {
@TestAnnotation
public String upperCase(String s) {
return s.toUpperCase();
}
@TestAnnotation
public String lowerCase(String s) {
return s.toLowerCase();
}
}
private static class SinglePublicMethodTestBean {
public String upperCase(String s) {
return s.toUpperCase();
}
String lowerCase(String s) {
return s.toLowerCase();
}
}
private static class MultiplePublicMethodTestBean {
public String upperCase(String s) {
return s.toUpperCase();
}
public String lowerCase(String s) {
return s.toLowerCase();
}
}
private static class NoPublicMethodTestBean {
String lowerCase(String s) {
return s.toLowerCase();
}
}
}