Removed the MethodResolver interface and the DefaultMethodResolver and AnnotationMethodResolver classes. Only MethodInvokingAggregator was still using those, so that logic is now encapsulated there. If we find a need for this in the future, we can consider exposing it again, but for now it's less clutter for the API.

This commit is contained in:
Mark Fisher
2008-11-26 15:36:25 +00:00
parent 41eca0eb8a
commit 2c2a1be8ae
7 changed files with 253 additions and 545 deletions

View File

@@ -0,0 +1,205 @@
/*
* 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.aggregator;
import static org.junit.Assert.assertEquals;
import java.lang.reflect.Method;
import java.util.List;
import org.junit.Test;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.beans.DirectFieldAccessor;
import org.springframework.integration.aggregator.MethodInvokingAggregator;
import org.springframework.integration.annotation.Aggregator;
import org.springframework.integration.channel.DirectChannel;
import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.core.Message;
import org.springframework.integration.endpoint.EventDrivenConsumer;
import org.springframework.integration.message.MessageBuilder;
/**
* @author Mark Fisher
*/
public class AggregatorMethodResolutionTests {
@Test
public void singleAnnotation() throws Exception {
SingleAnnotationTestBean bean = new SingleAnnotationTestBean();
MethodInvokingAggregator aggregator = new MethodInvokingAggregator(bean);
Method method = this.getMethod(aggregator);
Method expected = SingleAnnotationTestBean.class.getMethod("method1", new Class[] { List.class });
assertEquals(expected, method);
}
@Test(expected = IllegalArgumentException.class)
public void multipleAnnotations() {
MultipleAnnotationTestBean bean = new MultipleAnnotationTestBean();
new MethodInvokingAggregator(bean);
}
@Test
public void noAnnotations() throws Exception {
NoAnnotationTestBean bean = new NoAnnotationTestBean();
MethodInvokingAggregator aggregator = new MethodInvokingAggregator(bean);
Method method = this.getMethod(aggregator);
Method expected = NoAnnotationTestBean.class.getMethod("method1", new Class[] { List.class });
assertEquals(expected, method);
}
@Test(expected = IllegalArgumentException.class)
public void multiplePublicMethods() {
MultiplePublicMethodTestBean bean = new MultiplePublicMethodTestBean();
new MethodInvokingAggregator(bean);
}
@Test(expected = IllegalArgumentException.class)
public void noPublicMethods() {
NoPublicMethodTestBean bean = new NoPublicMethodTestBean();
new MethodInvokingAggregator(bean);
}
@Test
public void jdkProxy() {
DirectChannel input = new DirectChannel();
QueueChannel output = new QueueChannel();
GreetingService testBean = new GreetingBean();
ProxyFactory proxyFactory = new ProxyFactory(testBean);
proxyFactory.setProxyTargetClass(false);
testBean = (GreetingService) proxyFactory.getProxy();
MethodInvokingAggregator aggregator = new MethodInvokingAggregator(testBean);
aggregator.setAutoStartup(false);
aggregator.setOutputChannel(output);
EventDrivenConsumer endpoint = new EventDrivenConsumer(input, aggregator);
endpoint.start();
Message<?> message = MessageBuilder.withPayload("proxy")
.setCorrelationId("abc")
.build();
input.send(message);
assertEquals("hello proxy", output.receive(0).getPayload());;
}
@Test
public void cglibProxy() {
DirectChannel input = new DirectChannel();
QueueChannel output = new QueueChannel();
GreetingService testBean = new GreetingBean();
ProxyFactory proxyFactory = new ProxyFactory(testBean);
proxyFactory.setProxyTargetClass(true);
testBean = (GreetingService) proxyFactory.getProxy();
MethodInvokingAggregator aggregator = new MethodInvokingAggregator(testBean);
aggregator.setAutoStartup(false);
aggregator.setOutputChannel(output);
EventDrivenConsumer endpoint = new EventDrivenConsumer(input, aggregator);
endpoint.start();
Message<?> message = MessageBuilder.withPayload("proxy")
.setCorrelationId("abc")
.build();
input.send(message);
assertEquals("hello proxy", output.receive(0).getPayload());;
}
private Method getMethod(MethodInvokingAggregator aggregator) {
Object invoker = new DirectFieldAccessor(aggregator).getPropertyValue("methodInvoker");
return (Method) new DirectFieldAccessor(invoker).getPropertyValue("method");
}
private static class SingleAnnotationTestBean {
@Aggregator
public String method1(List<String> input) {
return input.get(0);
}
public String method2(List<String> input) {
return input.get(0);
}
}
private static class MultipleAnnotationTestBean {
@Aggregator
public String method1(List<String> input) {
return input.get(0);
}
@Aggregator
public String method2(List<String> input) {
return input.get(0);
}
}
private static class NoAnnotationTestBean {
public String method1(List<String> input) {
return input.get(0);
}
String method2(List<String> input) {
return input.get(0);
}
}
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();
}
}
public interface GreetingService {
String sayHello(List<String> names);
}
public static class GreetingBean implements GreetingService {
private String greeting = "hello";
public void setGreeting(String greeting) {
this.greeting = greeting;
}
@Aggregator
public String sayHello(List<String> names) {
return greeting + " " + names.get(0);
}
}
}

View File

@@ -1,100 +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.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 AnnotationMethodResolverTests {
@Test
public void singleAnnotation() {
AnnotationMethodResolver resolver = new AnnotationMethodResolver(TestAnnotation.class);
Method method = resolver.findMethod(SingleAnnotationTestBean.class);
assertNotNull(method);
}
@Test(expected = IllegalArgumentException.class)
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);
}
@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 NoAnnotationTestBean {
public String upperCase(String s) {
return s.toUpperCase();
}
String lowerCase(String s) {
return s.toLowerCase();
}
}
}

View File

@@ -1,197 +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.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;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.integration.channel.DirectChannel;
import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.endpoint.EventDrivenConsumer;
import org.springframework.integration.handler.ServiceActivatingHandler;
import org.springframework.integration.message.StringMessage;
/**
* @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);
}
@Test
public void jdkProxy() {
DirectChannel input = new DirectChannel();
QueueChannel output = new QueueChannel();
GreetingService testBean = new GreetingBean();
ProxyFactory proxyFactory = new ProxyFactory(testBean);
proxyFactory.setProxyTargetClass(false);
testBean = (GreetingService) proxyFactory.getProxy();
ServiceActivatingHandler handler = new ServiceActivatingHandler(testBean);
handler.setOutputChannel(output);
EventDrivenConsumer endpoint = new EventDrivenConsumer(input, handler);
endpoint.start();
input.send(new StringMessage("proxy"));
assertEquals("hello proxy", output.receive(0).getPayload());;
}
@Test
public void cglibProxy() {
DirectChannel input = new DirectChannel();
QueueChannel output = new QueueChannel();
GreetingService testBean = new GreetingBean();
ProxyFactory proxyFactory = new ProxyFactory(testBean);
proxyFactory.setProxyTargetClass(true);
testBean = (GreetingService) proxyFactory.getProxy();
ServiceActivatingHandler handler = new ServiceActivatingHandler(testBean);
handler.setOutputChannel(output);
EventDrivenConsumer endpoint = new EventDrivenConsumer(input, handler);
endpoint.start();
input.send(new StringMessage("proxy"));
assertEquals("hello proxy", output.receive(0).getPayload());;
}
@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();
}
}
public interface GreetingService {
String sayHello(String s);
}
public static class GreetingBean implements GreetingService {
private String greeting = "hello";
public void setGreeting(String greeting) {
this.greeting = greeting;
}
@ServiceActivator
public String sayHello(String name) {
return greeting + " " + name;
}
}
}