Using Asserts with IllegalArgumentException/IllegalStateException instead of ConfigurationException.

This commit is contained in:
Mark Fisher
2008-09-29 13:25:43 +00:00
parent 5bf96eeb08
commit e6f22df1bb
4 changed files with 73 additions and 82 deletions

View File

@@ -19,11 +19,12 @@ package org.springframework.integration.aggregator;
import java.lang.reflect.Method;
import java.util.List;
import org.springframework.integration.ConfigurationException;
import org.springframework.integration.message.Message;
import org.springframework.util.Assert;
/**
* Adapter for methods annotated with {@link org.springframework.integration.annotation.CompletionStrategy @CompletionStrategy}
* Adapter for methods annotated with
* {@link org.springframework.integration.annotation.CompletionStrategy @CompletionStrategy}
* and for '<code>completion-strategy</code>' elements that include a '<code>method</code>'
* attribute (e.g. &lt;completion-strategy ref="beanReference" method="methodName"/&gt;).
*
@@ -42,16 +43,14 @@ public class CompletionStrategyAdapter extends MessageListMethodAdapter implemen
}
private void assertMethodReturnsBoolean() {
if (!Boolean.class.equals(this.getMethod().getReturnType())
&& !boolean.class.equals(this.getMethod().getReturnType())) {
throw new ConfigurationException("Method '" + getMethod().getName()
+ "' does not return a boolean value");
}
}
public boolean isComplete(List<Message<?>> messages) {
return ((Boolean) executeMethod(messages)).booleanValue();
}
private void assertMethodReturnsBoolean() {
Assert.isTrue(Boolean.class.equals(this.getMethod().getReturnType())
|| boolean.class.equals(this.getMethod().getReturnType()),
"Method '" + getMethod().getName() + "' does not return a boolean value");
}
}

View File

@@ -23,7 +23,6 @@ import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import org.springframework.integration.ConfigurationException;
import org.springframework.integration.message.Message;
import org.springframework.integration.message.MessagingException;
import org.springframework.integration.util.DefaultMethodInvoker;
@@ -40,31 +39,33 @@ public abstract class MessageListMethodAdapter {
private final DefaultMethodInvoker invoker;
protected volatile Method method;
protected final Method method;
public MessageListMethodAdapter(Object object, String methodName) {
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<?>[] { List.class });
if (this.method == null) {
throw new ConfigurationException("Method '" + methodName +
"(List<?> args)' not found on '" + object.getClass().getName() + "'.");
}
Assert.notNull(this.method, "Method '" + methodName +
"(List<?> args)' not found on '" + object.getClass().getName() + "'.");
this.invoker = new DefaultMethodInvoker(object, this.method);
}
public MessageListMethodAdapter(Object object, Method method) {
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(List.class)) {
throw new ConfigurationException("Method must accept exactly one parameter, and it must be a List.");
}
Assert.isTrue(method.getParameterTypes().length == 1
&& method.getParameterTypes()[0].equals(List.class),
"Method must accept exactly one parameter, and it must be a List.");
this.method = method;
this.invoker = new DefaultMethodInvoker(object, this.method);
}
protected Method getMethod() {
return method;
}
private static boolean isActualTypeParameterizedMessage(Method method) {
return (getCollectionActualType(method) instanceof ParameterizedType)
&& Message.class.isAssignableFrom((Class<?>) ((ParameterizedType) getCollectionActualType(method)).getRawType());
@@ -77,11 +78,12 @@ public abstract class MessageListMethodAdapter {
return this.invoker.invokeMethod(messages);
}
return this.invoker.invokeMethod(extractPayloadsFromMessages(messages));
} catch (InvocationTargetException e) {
}
catch (InvocationTargetException e) {
throw new MessagingException(
"Method '" + this.method + "' threw an Exception.", e.getTargetException());
}
catch (Throwable e) {
catch (Exception e) {
throw new MessagingException("Failed to invoke method '" + this.method + "'.");
}
}
@@ -111,12 +113,4 @@ public abstract class MessageListMethodAdapter {
&& method.getGenericParameterTypes()[0] instanceof ParameterizedType;
}
public Method getMethod() {
return method;
}
public void setMethod(Method method) {
this.method = method;
}
}

View File

@@ -24,10 +24,7 @@ import java.util.List;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.springframework.integration.ConfigurationException;
import org.springframework.integration.aggregator.MethodInvokingAggregator;
import org.springframework.integration.aggregator.CompletionStrategy;
import org.springframework.integration.aggregator.CompletionStrategyAdapter;
import org.springframework.integration.message.GenericMessage;
import org.springframework.integration.message.Message;
@@ -38,11 +35,13 @@ public class CompletionStrategyAdapterTests {
private SimpleCompletionStrategy simpleCompletionStrategy;
@Before
public void setUp() {
simpleCompletionStrategy = new SimpleCompletionStrategy();
}
@Test
public void testTrueConvertedProperly() {
CompletionStrategyAdapter adapter = new CompletionStrategyAdapter(new AlwaysTrueCompletionStrategy(),
@@ -97,62 +96,62 @@ public class CompletionStrategyAdapterTests {
Assert.assertTrue(adapter.isComplete(messages));
}
@Test(expected = ConfigurationException.class)
@Test(expected = IllegalArgumentException.class)
public void testAdapterWithWrongMethodName() {
new CompletionStrategyAdapter(simpleCompletionStrategy, "methodThatDoesNotExist");
}
@Test(expected = ConfigurationException.class)
@Test(expected = IllegalArgumentException.class)
public void testInvalidParameterTypeUsingMethodName() {
new CompletionStrategyAdapter(simpleCompletionStrategy, "invalidParameterType");
}
@Test(expected = ConfigurationException.class)
@Test(expected = IllegalArgumentException.class)
public void testTooManyParametersUsingMethodName() {
new CompletionStrategyAdapter(simpleCompletionStrategy, "tooManyParameters");
}
@Test(expected = ConfigurationException.class)
@Test(expected = IllegalArgumentException.class)
public void testNotEnoughParametersUsingMethodName() {
new CompletionStrategyAdapter(simpleCompletionStrategy, "notEnoughParameters");
}
@Test(expected = ConfigurationException.class)
@Test(expected = IllegalArgumentException.class)
public void testListSubclassParameterUsingMethodName() {
new CompletionStrategyAdapter(simpleCompletionStrategy, "ListSubclassParameter");
}
@Test(expected = ConfigurationException.class)
@Test(expected = IllegalArgumentException.class)
public void testWrongReturnType() throws SecurityException, NoSuchMethodError {
new CompletionStrategyAdapter(simpleCompletionStrategy, "wrongReturnType");
}
@Test(expected = ConfigurationException.class)
@Test(expected = IllegalArgumentException.class)
public void testInvalidParameterTypeUsingMethodObject() throws SecurityException, NoSuchMethodException {
new MethodInvokingAggregator(simpleCompletionStrategy, simpleCompletionStrategy.getClass().getMethod(
"invalidParameterType", String.class));
}
@Test(expected = ConfigurationException.class)
@Test(expected = IllegalArgumentException.class)
public void testTooManyParametersUsingMethodObject() throws SecurityException, NoSuchMethodException {
new CompletionStrategyAdapter(simpleCompletionStrategy, simpleCompletionStrategy.getClass().getMethod(
"tooManyParameters", List.class, List.class));
}
@Test(expected = ConfigurationException.class)
@Test(expected = IllegalArgumentException.class)
public void testNotEnoughParametersUsingMethodObject() throws SecurityException, NoSuchMethodException {
new CompletionStrategyAdapter(simpleCompletionStrategy, simpleCompletionStrategy.getClass().getMethod(
"notEnoughParameters", new Class[] {}));
}
@Test(expected = ConfigurationException.class)
@Test(expected = IllegalArgumentException.class)
public void testListSubclassParameterUsingMethodObject() throws SecurityException, NoSuchMethodException {
new CompletionStrategyAdapter(simpleCompletionStrategy, simpleCompletionStrategy.getClass().getMethod(
"ListSubclassParameter", new Class[] { LinkedList.class }));
}
@Test(expected = ConfigurationException.class)
@Test(expected = IllegalArgumentException.class)
public void testWrongReturnTypeUsingMethodObject() throws SecurityException, NoSuchMethodException {
new CompletionStrategyAdapter(simpleCompletionStrategy, simpleCompletionStrategy.getClass().getMethod(
"wrongReturnType", new Class[] { List.class }));
@@ -175,6 +174,7 @@ public class CompletionStrategyAdapterTests {
new MethodInvokingAggregator(simpleCompletionStrategy, method);
}
private static List<Message<?>> createListOfMessages() {
List<Message<?>> messages = new ArrayList<Message<?>>();
messages.add(new GenericMessage<String>("123"));
@@ -213,8 +213,7 @@ public class CompletionStrategyAdapterTests {
return messages.size() > messages.iterator().next().getHeaders().getSequenceSize();
}
// Example for the case when completeness is checked on the structure of
// the data
// Example for the case when completeness is checked on the structure of the data
public boolean checkCompletenessOnListOfStrings(List<String> messages) {
StringBuffer buffer = new StringBuffer();
for (String content : messages) {

View File

@@ -24,9 +24,7 @@ import java.util.List;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.springframework.integration.ConfigurationException;
import org.springframework.integration.aggregator.Aggregator;
import org.springframework.integration.aggregator.MethodInvokingAggregator;
import org.springframework.integration.message.GenericMessage;
import org.springframework.integration.message.Message;
@@ -44,8 +42,9 @@ public class MethodInvokingAggregatorTests {
simpleAggregator = new SimpleAggregator();
}
@Test
public void testAdapterWithNonParameterizedMessageListBasedMethod() {
public void adapterWithNonParameterizedMessageListBasedMethod() {
Aggregator aggregator = new MethodInvokingAggregator(simpleAggregator, "doAggregationOnNonParameterizedListOfMessages");
List<Message<?>> messages = createListOfMessages();
Message<?> returnedMessge = aggregator.aggregate(messages);
@@ -54,7 +53,7 @@ public class MethodInvokingAggregatorTests {
}
@Test
public void testAdapterWithWildcardParametrizedMessageBasedMethod() {
public void adapterWithWildcardParameterizedMessageBasedMethod() {
Aggregator aggregator = new MethodInvokingAggregator(simpleAggregator, "doAggregationOnListOfMessagesParametrizedWithWildcard");
List<Message<?>> messages = createListOfMessages();
Message<?> returnedMessge = aggregator.aggregate(messages);
@@ -63,16 +62,16 @@ public class MethodInvokingAggregatorTests {
}
@Test
public void testAdapterWithTypeParametrizedMessageBasedMethod() {
public void adapterWithTypeParameterizedMessageBasedMethod() {
Aggregator aggregator = new MethodInvokingAggregator(simpleAggregator, "doAggregationOnListOfMessagesParametrizedWithString");
List<Message<?>> messages = createListOfMessages();
Message<?> returnedMessge = aggregator.aggregate(messages);
Assert.assertTrue(simpleAggregator.isAggregationPerformed());
Assert.assertEquals("123456789", returnedMessge.getPayload());
}
@Test
public void testAdapterWithPojoBasedMethod() {
public void adapterWithPojoBasedMethod() {
Aggregator aggregator = new MethodInvokingAggregator(simpleAggregator, "doAggregationOnListOfStrings");
List<Message<?>> messages = createListOfMessages();
Message<?> returnedMessge = aggregator.aggregate(messages);
@@ -81,16 +80,16 @@ public class MethodInvokingAggregatorTests {
}
@Test
public void testAdapterWithPojoBasedMethodReturningObject() {
public void adapterWithPojoBasedMethodReturningObject() {
Aggregator aggregator = new MethodInvokingAggregator(simpleAggregator, "doAggregationOnListOfStringsReturningLong");
List<Message<?>> messages = createListOfMessages();
Message<?> returnedMessge = aggregator.aggregate(messages);
Assert.assertTrue(simpleAggregator.isAggregationPerformed());
Assert.assertEquals(123456789l, returnedMessge.getPayload());
}
@Test
public void testAdapterWithVoidReturnType() {
public void adapterWithVoidReturnType() {
Aggregator aggregator = new MethodInvokingAggregator(simpleAggregator, "doAggregationWithNoReturn");
List<Message<?>> messages = createListOfMessages();
Message<?> returnedMessage = aggregator.aggregate(messages);
@@ -99,7 +98,7 @@ public class MethodInvokingAggregatorTests {
}
@Test
public void testAdapterWithNullReturn() {
public void adapterWithNullReturn() {
Aggregator aggregator = new MethodInvokingAggregator(simpleAggregator, "doAggregationWithNullReturn");
List<Message<?>> messages = createListOfMessages();
Message<?> returnedMessage = aggregator.aggregate(messages);
@@ -107,68 +106,68 @@ public class MethodInvokingAggregatorTests {
Assert.assertNull(returnedMessage);
}
@Test(expected=ConfigurationException.class)
public void testAdapterWithWrongMethodName() {
@Test(expected = IllegalArgumentException.class)
public void adapterWithWrongMethodName() {
new MethodInvokingAggregator(simpleAggregator, "methodThatDoesNotExist");
}
@Test(expected=ConfigurationException.class)
public void testInvalidParameterTypeUsingMethodName() {
@Test(expected = IllegalArgumentException.class)
public void invalidParameterTypeUsingMethodName() {
new MethodInvokingAggregator(simpleAggregator, "invalidParameterType");
}
@Test(expected=ConfigurationException.class)
public void testTooManyParametersUsingMethodName() {
@Test(expected = IllegalArgumentException.class)
public void tooManyParametersUsingMethodName() {
new MethodInvokingAggregator(simpleAggregator, "tooManyParameters");
}
@Test(expected=ConfigurationException.class)
public void testNotEnoughParametersUsingMethodName() {
@Test(expected = IllegalArgumentException.class)
public void notEnoughParametersUsingMethodName() {
new MethodInvokingAggregator(simpleAggregator, "notEnoughParameters");
}
@Test(expected=ConfigurationException.class)
public void testListSubclassParameterUsingMethodName() {
@Test(expected = IllegalArgumentException.class)
public void listSubclassParameterUsingMethodName() {
new MethodInvokingAggregator(simpleAggregator, "ListSubclassParameter");
}
@Test(expected=ConfigurationException.class)
public void testInvalidParameterTypeUsingMethodObject() throws SecurityException, NoSuchMethodException {
@Test(expected = IllegalArgumentException.class)
public void invalidParameterTypeUsingMethodObject() throws SecurityException, NoSuchMethodException {
new MethodInvokingAggregator(simpleAggregator, simpleAggregator.getClass().getMethod(
"invalidParameterType", String.class));
}
@Test(expected=ConfigurationException.class)
public void testTooManyParametersUsingMethodObject() throws SecurityException, NoSuchMethodException {
@Test(expected = IllegalArgumentException.class)
public void tooManyParametersUsingMethodObject() throws SecurityException, NoSuchMethodException {
new MethodInvokingAggregator(simpleAggregator, simpleAggregator.getClass().getMethod(
"tooManyParameters", List.class, List.class));
}
@Test(expected=ConfigurationException.class)
public void testNotEnoughParametersUsingMethodObject() throws SecurityException, NoSuchMethodException {
@Test(expected = IllegalArgumentException.class)
public void notEnoughParametersUsingMethodObject() throws SecurityException, NoSuchMethodException {
new MethodInvokingAggregator(simpleAggregator, simpleAggregator.getClass().getMethod(
"notEnoughParameters", new Class[] {} ));
}
@Test(expected= ConfigurationException.class)
public void testListSubclassParameterUsingMethodObject() throws SecurityException, NoSuchMethodException {
@Test(expected = IllegalArgumentException.class)
public void listSubclassParameterUsingMethodObject() throws SecurityException, NoSuchMethodException {
new MethodInvokingAggregator(simpleAggregator, simpleAggregator.getClass().getMethod(
"listSubclassParameter", new Class[] {LinkedList.class} ));
}
@Test(expected=IllegalArgumentException.class)
public void testNullObject() {
@Test(expected = IllegalArgumentException.class)
public void nullObject() {
new MethodInvokingAggregator(null, "doesNotMatter");
}
@Test(expected=IllegalArgumentException.class)
public void testNullMethodName() {
@Test(expected = IllegalArgumentException.class)
public void nullMethodName() {
String methodName = null;
new MethodInvokingAggregator(simpleAggregator, methodName);
}
@Test(expected=IllegalArgumentException.class)
public void testNullMethodObject() {
@Test(expected = IllegalArgumentException.class)
public void nullMethodObject() {
Method method = null;
new MethodInvokingAggregator(simpleAggregator, method);
}