From e37e22eb7677709a2f79091092c67dcbcd572fd7 Mon Sep 17 00:00:00 2001 From: Mark Fisher Date: Tue, 4 Mar 2008 16:24:21 +0000 Subject: [PATCH] AggregatorAdapter throws a MessagingConfigurationException if the method is invalid (wrong name provided or non-conforming signature). The IllegalArgumentExceptions are only thrown in the case of a null object, method, or methodName. --- .../integration/router/AggregatorAdapter.java | 9 ++-- .../router/AggregatorAdapterTests.java | 42 ++++++++++++++----- 2 files changed, 37 insertions(+), 14 deletions(-) diff --git a/spring-integration-core/src/main/java/org/springframework/integration/router/AggregatorAdapter.java b/spring-integration-core/src/main/java/org/springframework/integration/router/AggregatorAdapter.java index 5546c00817..42575bc2bb 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/router/AggregatorAdapter.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/router/AggregatorAdapter.java @@ -23,6 +23,7 @@ import java.util.ArrayList; import java.util.Collection; import java.util.List; +import org.springframework.integration.MessagingConfigurationException; import org.springframework.integration.message.GenericMessage; import org.springframework.integration.message.Message; import org.springframework.integration.util.SimpleMethodInvoker; @@ -48,8 +49,10 @@ public class AggregatorAdapter implements Aggregator { Assert.notNull(object, "'object' must not be null"); Assert.notNull(methodName, "'methodName' must not be null"); this.method = ReflectionUtils.findMethod(object.getClass(), methodName, new Class[] { Collection.class }); - Assert.notNull(this.method, "Method '" + methodName + "(Collection args)' not found on '" + - object.getClass().getName() + "'."); + if (this.method == null) { + throw new MessagingConfigurationException("Method '" + methodName + + "(Collection args)' not found on '" + object.getClass().getName() + "'."); + } this.invoker = new SimpleMethodInvoker(object, this.method.getName()); } @@ -57,7 +60,7 @@ public class AggregatorAdapter implements Aggregator { Assert.notNull(object, "'object' must not be null"); Assert.notNull(method, "'method' must not be null"); if (method.getParameterTypes().length != 1 || !method.getParameterTypes()[0].equals(Collection.class)) { - throw new IllegalArgumentException( + throw new MessagingConfigurationException( "Aggregator method must accept exactly one parameter, and it must be a Collection."); } this.method = method; diff --git a/spring-integration-core/src/test/java/org/springframework/integration/router/AggregatorAdapterTests.java b/spring-integration-core/src/test/java/org/springframework/integration/router/AggregatorAdapterTests.java index bef924558d..a8a5e74e93 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/router/AggregatorAdapterTests.java +++ b/spring-integration-core/src/test/java/org/springframework/integration/router/AggregatorAdapterTests.java @@ -16,6 +16,7 @@ package org.springframework.integration.router; +import java.lang.reflect.Method; import java.util.ArrayList; import java.util.Collection; @@ -23,6 +24,7 @@ import org.junit.Assert; import org.junit.Before; import org.junit.Test; +import org.springframework.integration.MessagingConfigurationException; import org.springframework.integration.message.GenericMessage; import org.springframework.integration.message.Message; @@ -41,8 +43,8 @@ public class AggregatorAdapterTests { } @Test - public void testAdapterWithMessageCollectionBasedMethod() { - Aggregator aggregator = new AggregatorAdapter(simpleAggregator, "doAggregationOnCollectionOfMessages"); + public void testAdapterWithNonParameterizedMessageCollectionBasedMethod() { + Aggregator aggregator = new AggregatorAdapter(simpleAggregator, "doAggregationOnNonParameterizedCollectionOfMessages"); Collection> messages = createCollectionOfMessages(); Message returnedMessge = aggregator.aggregate(messages); Assert.assertTrue(simpleAggregator.isAggregationPerformed()); @@ -85,42 +87,59 @@ public class AggregatorAdapterTests { Assert.assertEquals(123456789l, returnedMessge.getPayload()); } - @Test(expected=IllegalArgumentException.class) + @Test(expected=MessagingConfigurationException.class) public void testAdapterWithWrongMethodName() { new AggregatorAdapter(simpleAggregator, "methodThatDoesNotExist"); } - @Test(expected=IllegalArgumentException.class) + @Test(expected=MessagingConfigurationException.class) public void testInvalidParameterTypeUsingMethodName() { new AggregatorAdapter(simpleAggregator, "invalidParameterType"); } - @Test(expected=IllegalArgumentException.class) + @Test(expected=MessagingConfigurationException.class) public void testTooManyParametersUsingMethodName() { new AggregatorAdapter(simpleAggregator, "tooManyParameters"); } - @Test(expected=IllegalArgumentException.class) + @Test(expected=MessagingConfigurationException.class) public void testNotEnoughParametersUsingMethodName() { new AggregatorAdapter(simpleAggregator, "notEnoughParameters"); } - @Test(expected=IllegalArgumentException.class) + @Test(expected=MessagingConfigurationException.class) public void testInvalidParameterTypeUsingMethodObject() throws SecurityException, NoSuchMethodException { new AggregatorAdapter(simpleAggregator, simpleAggregator.getClass().getMethod( "invalidParameterType", String.class)); } - @Test(expected=IllegalArgumentException.class) + @Test(expected=MessagingConfigurationException.class) public void testTooManyParametersUsingMethodObject() throws SecurityException, NoSuchMethodException { new AggregatorAdapter(simpleAggregator, simpleAggregator.getClass().getMethod( "tooManyParameters", Collection.class, Collection.class)); } - @Test(expected=IllegalArgumentException.class) + @Test(expected=MessagingConfigurationException.class) public void testNotEnoughParametersUsingMethodObject() throws SecurityException, NoSuchMethodException { new AggregatorAdapter(simpleAggregator, simpleAggregator.getClass().getMethod( - "notEnoughParameters", null)); + "notEnoughParameters", new Class[] {} )); + } + + @Test(expected=IllegalArgumentException.class) + public void testNullObject() { + new AggregatorAdapter(null, "doesNotMatter"); + } + + @Test(expected=IllegalArgumentException.class) + public void testNullMethodName() { + String methodName = null; + new AggregatorAdapter(simpleAggregator, methodName); + } + + @Test(expected=IllegalArgumentException.class) + public void testNullMethodObject() { + Method method = null; + new AggregatorAdapter(simpleAggregator, method); } @@ -146,7 +165,8 @@ public class AggregatorAdapterTests { return this.aggregationPerformed; } - public Message doAggregationOnCollectionOfMessages(Collection messages) { + @SuppressWarnings("unchecked") + public Message doAggregationOnNonParameterizedCollectionOfMessages(Collection messages) { this.aggregationPerformed = true; StringBuffer buffer = new StringBuffer(); for (Message message : messages) {