From 7ac62e7ee1dd976ae9570eede328f5b9ef563a9b Mon Sep 17 00:00:00 2001 From: Marius Bogoevici Date: Fri, 11 Apr 2008 02:00:19 +0000 Subject: [PATCH] Completing revision 485 - missing from previous commit. --- .../config/MaxValueCompletionStrategy.java | 37 +++ ...tedEndpointWithCompletionStrategyOnly.java | 46 ++++ .../config/duplicateCompletionStrategy.xml | 33 +++ .../CompletionStrategyAdapterTests.java | 244 ++++++++++++++++++ 4 files changed, 360 insertions(+) create mode 100644 spring-integration-core/src/test/java/org/springframework/integration/config/MaxValueCompletionStrategy.java create mode 100644 spring-integration-core/src/test/java/org/springframework/integration/config/TestAnnotatedEndpointWithCompletionStrategyOnly.java create mode 100644 spring-integration-core/src/test/java/org/springframework/integration/config/duplicateCompletionStrategy.xml create mode 100644 spring-integration-core/src/test/java/org/springframework/integration/router/CompletionStrategyAdapterTests.java diff --git a/spring-integration-core/src/test/java/org/springframework/integration/config/MaxValueCompletionStrategy.java b/spring-integration-core/src/test/java/org/springframework/integration/config/MaxValueCompletionStrategy.java new file mode 100644 index 0000000000..7c4e5cb62f --- /dev/null +++ b/spring-integration-core/src/test/java/org/springframework/integration/config/MaxValueCompletionStrategy.java @@ -0,0 +1,37 @@ +/* + * 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.config; + +import java.util.List; + +public class MaxValueCompletionStrategy { + + private long maxValue; + + + public MaxValueCompletionStrategy(long maxValue){ + this.maxValue = maxValue; + } + + public boolean checkCompleteness(List numbers) { + int sum = 0; + for (long number: numbers) { + sum += number; + } + return sum >= maxValue; + } + +} diff --git a/spring-integration-core/src/test/java/org/springframework/integration/config/TestAnnotatedEndpointWithCompletionStrategyOnly.java b/spring-integration-core/src/test/java/org/springframework/integration/config/TestAnnotatedEndpointWithCompletionStrategyOnly.java new file mode 100644 index 0000000000..3ae863ca46 --- /dev/null +++ b/spring-integration-core/src/test/java/org/springframework/integration/config/TestAnnotatedEndpointWithCompletionStrategyOnly.java @@ -0,0 +1,46 @@ +/* + * Copyright 2002-2007 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.config; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.List; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; + +import org.springframework.integration.annotation.Aggregator; +import org.springframework.integration.annotation.CompletionStrategy; +import org.springframework.integration.annotation.MessageEndpoint; +import org.springframework.integration.message.Message; +import org.springframework.integration.message.StringMessage; +import org.springframework.integration.router.MessageSequenceComparator; +import org.springframework.stereotype.Component; + +/** + * @author Marius Bogoevici + */ +@MessageEndpoint(input="inputChannel") +@Component("endpointWithoutAggregatorAndWithCompletionStrategy") +public class TestAnnotatedEndpointWithCompletionStrategyOnly { + + @CompletionStrategy + public boolean checkCompleteness(List> messages) { + throw new UnsupportedOperationException("Not intended to being called"); + } + +} diff --git a/spring-integration-core/src/test/java/org/springframework/integration/config/duplicateCompletionStrategy.xml b/spring-integration-core/src/test/java/org/springframework/integration/config/duplicateCompletionStrategy.xml new file mode 100644 index 0000000000..f505000d86 --- /dev/null +++ b/spring-integration-core/src/test/java/org/springframework/integration/config/duplicateCompletionStrategy.xml @@ -0,0 +1,33 @@ + + + + + + + + + + + + + + + + + + + + + + diff --git a/spring-integration-core/src/test/java/org/springframework/integration/router/CompletionStrategyAdapterTests.java b/spring-integration-core/src/test/java/org/springframework/integration/router/CompletionStrategyAdapterTests.java new file mode 100644 index 0000000000..574faac978 --- /dev/null +++ b/spring-integration-core/src/test/java/org/springframework/integration/router/CompletionStrategyAdapterTests.java @@ -0,0 +1,244 @@ +/* + * 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.router; + +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.LinkedList; +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.message.GenericMessage; +import org.springframework.integration.message.Message; + +/** + * @author Marius Bogoevici + */ +public class CompletionStrategyAdapterTests { + + private SimpleCompletionStrategy simpleCompletionStrategy; + + @Before + public void setUp() { + simpleCompletionStrategy = new SimpleCompletionStrategy(); + } + + @Test + public void testTrueConvertedProperly() { + CompletionStrategyAdapter adapter = new CompletionStrategyAdapter(new AlwaysTrueCompletionStrategy(), + "checkCompleteness"); + Assert.assertTrue(adapter.isComplete(new ArrayList>())); + } + + @Test + public void testFalseConvertedProperly() { + CompletionStrategyAdapter adapter = new CompletionStrategyAdapter(new AlwaysFalseCompletionStrategy(), + "checkCompleteness"); + Assert.assertTrue(!adapter.isComplete(new ArrayList>())); + } + + @Test + public void testAdapterWithNonParameterizedMessageListBasedMethod() { + CompletionStrategy adapter = new CompletionStrategyAdapter(simpleCompletionStrategy, + "checkCompletenessOnNonParameterizedListOfMessages"); + List> messages = createListOfMessages(); + Assert.assertTrue(adapter.isComplete(messages)); + } + + @Test + public void testAdapterWithWildcardParametrizedMessageBasedMethod() { + CompletionStrategy adapter = new CompletionStrategyAdapter(simpleCompletionStrategy, + "checkCompletenessOnListOfMessagesParametrizedWithWildcard"); + List> messages = createListOfMessages(); + Assert.assertTrue(adapter.isComplete(messages)); + } + + @Test + public void testAdapterWithTypeParametrizedMessageBasedMethod() { + CompletionStrategy adapter = new CompletionStrategyAdapter(simpleCompletionStrategy, + "checkCompletenessOnListOfMessagesParametrizedWithString"); + List> messages = createListOfMessages(); + Assert.assertTrue(adapter.isComplete(messages)); + } + + @Test + public void testAdapterWithPojoBasedMethod() { + CompletionStrategy adapter = new CompletionStrategyAdapter(simpleCompletionStrategy, + "checkCompletenessOnListOfStrings"); + List> messages = createListOfMessages(); + Assert.assertTrue(adapter.isComplete(messages)); + } + + @Test + public void testAdapterWithPojoBasedMethodReturningObject() { + CompletionStrategy adapter = new CompletionStrategyAdapter(simpleCompletionStrategy, + "checkCompletenessOnListOfStrings"); + List> messages = createListOfMessages(); + Assert.assertTrue(adapter.isComplete(messages)); + } + + @Test(expected = ConfigurationException.class) + public void testAdapterWithWrongMethodName() { + new CompletionStrategyAdapter(simpleCompletionStrategy, "methodThatDoesNotExist"); + } + + @Test(expected = ConfigurationException.class) + public void testInvalidParameterTypeUsingMethodName() { + new CompletionStrategyAdapter(simpleCompletionStrategy, "invalidParameterType"); + } + + @Test(expected = ConfigurationException.class) + public void testTooManyParametersUsingMethodName() { + new CompletionStrategyAdapter(simpleCompletionStrategy, "tooManyParameters"); + } + + @Test(expected = ConfigurationException.class) + public void testNotEnoughParametersUsingMethodName() { + new CompletionStrategyAdapter(simpleCompletionStrategy, "notEnoughParameters"); + } + + @Test(expected = ConfigurationException.class) + public void testListSubclassParameterUsingMethodName() { + new CompletionStrategyAdapter(simpleCompletionStrategy, "ListSubclassParameter"); + } + + + @Test(expected = ConfigurationException.class) + public void testWrongReturnType() throws SecurityException, NoSuchMethodError { + new CompletionStrategyAdapter(simpleCompletionStrategy, "wrongReturnType"); + } + + @Test(expected = ConfigurationException.class) + public void testInvalidParameterTypeUsingMethodObject() throws SecurityException, NoSuchMethodException { + new AggregatorAdapter(simpleCompletionStrategy, simpleCompletionStrategy.getClass().getMethod( + "invalidParameterType", String.class)); + } + + @Test(expected = ConfigurationException.class) + public void testTooManyParametersUsingMethodObject() throws SecurityException, NoSuchMethodException { + new CompletionStrategyAdapter(simpleCompletionStrategy, simpleCompletionStrategy.getClass().getMethod( + "tooManyParameters", List.class, List.class)); + } + + @Test(expected = ConfigurationException.class) + public void testNotEnoughParametersUsingMethodObject() throws SecurityException, NoSuchMethodException { + new CompletionStrategyAdapter(simpleCompletionStrategy, simpleCompletionStrategy.getClass().getMethod( + "notEnoughParameters", new Class[] {})); + } + + @Test(expected = ConfigurationException.class) + public void testListSubclassParameterUsingMethodObject() throws SecurityException, NoSuchMethodException { + new CompletionStrategyAdapter(simpleCompletionStrategy, simpleCompletionStrategy.getClass().getMethod( + "ListSubclassParameter", new Class[] { LinkedList.class })); + } + + @Test(expected = ConfigurationException.class) + public void testWrongReturnTypeUsingMethodObject() throws SecurityException, NoSuchMethodException { + new CompletionStrategyAdapter(simpleCompletionStrategy, simpleCompletionStrategy.getClass().getMethod( + "wrongReturnType", new Class[] { List.class })); + } + + @Test(expected = IllegalArgumentException.class) + public void testNullObject() { + new AggregatorAdapter(null, "doesNotMatter"); + } + + @Test(expected = IllegalArgumentException.class) + public void testNullMethodName() { + String methodName = null; + new AggregatorAdapter(simpleCompletionStrategy, methodName); + } + + @Test(expected = IllegalArgumentException.class) + public void testNullMethodObject() { + Method method = null; + new AggregatorAdapter(simpleCompletionStrategy, method); + } + + private static List> createListOfMessages() { + List> messages = new ArrayList>(); + messages.add(new GenericMessage("123")); + messages.add(new GenericMessage("456")); + messages.add(new GenericMessage("789")); + return messages; + } + + private static class AlwaysTrueCompletionStrategy { + public boolean checkCompleteness(List> messages) { + return true; + } + } + + private static class AlwaysFalseCompletionStrategy { + public boolean checkCompleteness(List> messages) { + return false; + } + } + + private static class SimpleCompletionStrategy { + + public boolean checkCompletenessOnNonParameterizedListOfMessages(List messages) { + Assert.assertTrue(messages.size() > 0); + return messages.size() > messages.iterator().next().getHeader().getSequenceSize(); + } + + public boolean checkCompletenessOnListOfMessagesParametrizedWithWildcard(List> messages) { + Assert.assertTrue(messages.size() > 0); + return messages.size() > messages.iterator().next().getHeader().getSequenceSize(); + } + + public boolean checkCompletenessOnListOfMessagesParametrizedWithString( + List> messages) { + Assert.assertTrue(messages.size() > 0); + return messages.size() > messages.iterator().next().getHeader().getSequenceSize(); + } + + // Example for the case when completeness is checked on the structure of + // the data + public boolean checkCompletenessOnListOfStrings(List messages) { + StringBuffer buffer = new StringBuffer(); + for (String content : messages) { + buffer.append(content); + } + return buffer.length() >= 9; + } + + public String wrongReturnType(List> message) { + return ""; + } + + public boolean invalidParameterType(String invalid) { + return false; + } + + public boolean tooManyParameters(List c1, List c2) { + return false; + } + + public boolean notEnoughParameters() { + return false; + } + + public boolean ListSubclassParameter(LinkedList l1) { + return false; + } + } + +}