Removed Splitter interface and added AbstractMessageSplitter base class. The MethodInvokingSplitter is now capable of resolving methods when only an Object is provided to its constructor - either a single method containing the @Splitter annotation or a single public Method as a fallback (or if neither is satisifed, an IllegalArgumentException will be thrown).
This commit is contained in:
@@ -20,8 +20,9 @@ import org.w3c.dom.Element;
|
||||
|
||||
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
|
||||
import org.springframework.beans.factory.xml.ParserContext;
|
||||
import org.springframework.integration.splitter.DefaultMessageSplitter;
|
||||
import org.springframework.integration.splitter.MethodInvokingSplitter;
|
||||
import org.springframework.integration.splitter.SplitterEndpoint;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* Parser for the <splitter/> element.
|
||||
@@ -32,12 +33,17 @@ public class SplitterParser extends AbstractEndpointParser {
|
||||
|
||||
@Override
|
||||
protected BeanDefinitionBuilder parseConsumer(Element element, ParserContext parserContext) {
|
||||
BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(SplitterEndpoint.class);
|
||||
if (element.hasAttribute("ref")) {
|
||||
String adapterBeanName = this.parseAdapter(element, parserContext, MethodInvokingSplitter.class);
|
||||
builder.addConstructorArgReference(adapterBeanName);
|
||||
if (element.hasAttribute(REF_ATTRIBUTE)) {
|
||||
String ref = element.getAttribute(REF_ATTRIBUTE);
|
||||
BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(MethodInvokingSplitter.class);
|
||||
builder.addConstructorArgReference(ref);
|
||||
if (StringUtils.hasText(element.getAttribute(METHOD_ATTRIBUTE))) {
|
||||
String method = element.getAttribute(METHOD_ATTRIBUTE);
|
||||
builder.addConstructorArgValue(method);
|
||||
}
|
||||
return builder;
|
||||
}
|
||||
return builder;
|
||||
return BeanDefinitionBuilder.genericBeanDefinition(DefaultMessageSplitter.class);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -22,7 +22,6 @@ import org.springframework.integration.annotation.Splitter;
|
||||
import org.springframework.integration.bus.MessageBus;
|
||||
import org.springframework.integration.message.MessageConsumer;
|
||||
import org.springframework.integration.splitter.MethodInvokingSplitter;
|
||||
import org.springframework.integration.splitter.SplitterEndpoint;
|
||||
|
||||
/**
|
||||
* Post-processor for Methods annotated with {@link Splitter @Splitter}.
|
||||
@@ -38,8 +37,7 @@ public class SplitterAnnotationPostProcessor extends AbstractMethodAnnotationPos
|
||||
|
||||
@Override
|
||||
protected MessageConsumer createConsumer(Object bean, Method method, Splitter annotation) {
|
||||
MethodInvokingSplitter splitter = new MethodInvokingSplitter(bean, method);
|
||||
return new SplitterEndpoint(splitter);
|
||||
return new MethodInvokingSplitter(bean, method);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -21,7 +21,6 @@ import java.util.Collections;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
/**
|
||||
* @author Mark Fisher
|
||||
*/
|
||||
|
||||
@@ -20,17 +20,30 @@ import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.integration.endpoint.AbstractReplyProducingMessageConsumer;
|
||||
import org.springframework.integration.message.CompositeMessage;
|
||||
import org.springframework.integration.message.Message;
|
||||
import org.springframework.integration.message.MessageBuilder;
|
||||
import org.springframework.integration.message.MessageHeaders;
|
||||
|
||||
/**
|
||||
* Base class for Message-splitting consumers.
|
||||
*
|
||||
* @author Mark Fisher
|
||||
*/
|
||||
public abstract class AbstractSplitter implements Splitter {
|
||||
public abstract class AbstractMessageSplitter extends AbstractReplyProducingMessageConsumer {
|
||||
|
||||
public List<Message<?>> split(Message<?> message) {
|
||||
@Override
|
||||
protected final boolean shouldSplitComposite() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected final Message<?> handle(Message<?> message) {
|
||||
Object result = this.splitMessage(message);
|
||||
if (result == null) {
|
||||
return null;
|
||||
}
|
||||
MessageHeaders requestHeaders = message.getHeaders();
|
||||
List<Message<?>> results = new ArrayList<Message<?>>();
|
||||
if (result instanceof Collection) {
|
||||
@@ -55,11 +68,20 @@ public abstract class AbstractSplitter implements Splitter {
|
||||
if (results.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
return results;
|
||||
return new CompositeMessage(results);
|
||||
}
|
||||
|
||||
/**
|
||||
* Subclasses must override this method to split the received Message. The
|
||||
* return value may be a Collection or Array. The individual elements may
|
||||
* be Messages, but it is not necessary. If the elements are not Messages,
|
||||
* each will be provided as the payload of a Message. It is also acceptable
|
||||
* to return a single Object or Message. In that case, a single reply
|
||||
* Message will be produced.
|
||||
*/
|
||||
protected abstract Object splitMessage(Message<?> message);
|
||||
|
||||
|
||||
private Message<?> createSplitMessage(Object item, MessageHeaders requestHeaders, int sequenceNumber, int sequenceSize) {
|
||||
if (item instanceof Message<?>) {
|
||||
return setSplitMessageHeaders(MessageBuilder.fromMessage((Message<?>) item),
|
||||
@@ -23,9 +23,14 @@ import java.util.StringTokenizer;
|
||||
import org.springframework.integration.message.Message;
|
||||
|
||||
/**
|
||||
* The default Message Splitter implementation. Returns individual Messages
|
||||
* after receiving an array or Collection. If a value is provided for the
|
||||
* 'delimiters' property, then String payloads will be tokenized based on
|
||||
* those delimiters.
|
||||
*
|
||||
* @author Mark Fisher
|
||||
*/
|
||||
public class DefaultSplitter extends AbstractSplitter {
|
||||
public class DefaultMessageSplitter extends AbstractMessageSplitter {
|
||||
|
||||
private volatile String delimiters;
|
||||
|
||||
@@ -39,7 +44,7 @@ public class DefaultSplitter extends AbstractSplitter {
|
||||
this.delimiters = delimiters;
|
||||
}
|
||||
|
||||
public Object splitMessage(Message<?> message) {
|
||||
protected final Object splitMessage(Message<?> message) {
|
||||
Object payload = message.getPayload();
|
||||
if (payload instanceof String && this.delimiters != null) {
|
||||
List<String> tokens = new ArrayList<String>();
|
||||
@@ -19,11 +19,15 @@ package org.springframework.integration.splitter;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.integration.annotation.Splitter;
|
||||
import org.springframework.integration.message.Message;
|
||||
import org.springframework.integration.message.MessageMappingMethodInvoker;
|
||||
import org.springframework.integration.util.DefaultMethodResolver;
|
||||
import org.springframework.integration.util.MethodResolver;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* A {@link Splitter} implementation that invokes the specified method
|
||||
* A Message Splitter implementation that invokes the specified method
|
||||
* on the given object. The method's return value will be split if it
|
||||
* is a Collection or Array. If the return value is not a Collection or
|
||||
* Array, then the single Object will be returned as the payload of a
|
||||
@@ -31,7 +35,9 @@ import org.springframework.integration.message.MessageMappingMethodInvoker;
|
||||
*
|
||||
* @author Mark Fisher
|
||||
*/
|
||||
public class MethodInvokingSplitter extends AbstractSplitter implements Splitter, InitializingBean {
|
||||
public class MethodInvokingSplitter extends AbstractMessageSplitter implements InitializingBean {
|
||||
|
||||
private final MethodResolver methodResolver = new DefaultMethodResolver(Splitter.class);
|
||||
|
||||
private final MessageMappingMethodInvoker invoker;
|
||||
|
||||
@@ -44,6 +50,14 @@ public class MethodInvokingSplitter extends AbstractSplitter implements Splitter
|
||||
this.invoker = new MessageMappingMethodInvoker(object, methodName);
|
||||
}
|
||||
|
||||
public MethodInvokingSplitter(Object object) {
|
||||
Assert.notNull(object, "object must not be null");
|
||||
Method method = this.methodResolver.findMethod(object.getClass());
|
||||
Assert.notNull(method, "unable to resolve Splitter method on target class ["
|
||||
+ object.getClass() + "]");
|
||||
this.invoker = new MessageMappingMethodInvoker(object, method);
|
||||
}
|
||||
|
||||
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
this.invoker.afterPropertiesSet();
|
||||
|
||||
@@ -1,33 +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.splitter;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.integration.message.Message;
|
||||
|
||||
/**
|
||||
* Strategy interface for splitting a single {@link Message}
|
||||
* into multiple Messages.
|
||||
*
|
||||
* @author Mark Fisher
|
||||
*/
|
||||
public interface Splitter {
|
||||
|
||||
List<Message<?>> split(Message<?> message);
|
||||
|
||||
}
|
||||
@@ -1,58 +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.splitter;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.integration.endpoint.AbstractReplyProducingMessageConsumer;
|
||||
import org.springframework.integration.message.CompositeMessage;
|
||||
import org.springframework.integration.message.Message;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* @author Mark Fisher
|
||||
*/
|
||||
public class SplitterEndpoint extends AbstractReplyProducingMessageConsumer {
|
||||
|
||||
private final Splitter splitter;
|
||||
|
||||
|
||||
public SplitterEndpoint() {
|
||||
this(new DefaultSplitter());
|
||||
}
|
||||
|
||||
public SplitterEndpoint(Splitter splitter) {
|
||||
Assert.notNull(splitter, "splitter must not be null");
|
||||
this.splitter = splitter;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected boolean shouldSplitComposite() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Message<?> handle(Message<?> message) {
|
||||
List<Message<?>> results = this.splitter.split(message);
|
||||
if (results == null || results.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
return new CompositeMessage(results);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -23,12 +23,10 @@ import org.junit.Test;
|
||||
|
||||
import org.springframework.integration.channel.DirectChannel;
|
||||
import org.springframework.integration.channel.QueueChannel;
|
||||
import org.springframework.integration.endpoint.ServiceActivatorEndpoint;
|
||||
import org.springframework.integration.message.Message;
|
||||
import org.springframework.integration.message.MessageBuilder;
|
||||
import org.springframework.integration.message.StringMessage;
|
||||
import org.springframework.integration.splitter.MethodInvokingSplitter;
|
||||
import org.springframework.integration.splitter.SplitterEndpoint;
|
||||
|
||||
/**
|
||||
* @author Mark Fisher
|
||||
@@ -118,10 +116,9 @@ public class CorrelationIdTests {
|
||||
QueueChannel testChannel = new QueueChannel();
|
||||
MethodInvokingSplitter splitter = new MethodInvokingSplitter(
|
||||
new TestBean(), TestBean.class.getMethod("split", String.class));
|
||||
SplitterEndpoint endpoint = new SplitterEndpoint(splitter);
|
||||
endpoint.setOutputChannel(testChannel);
|
||||
splitter.setOutputChannel(testChannel);
|
||||
splitter.afterPropertiesSet();
|
||||
endpoint.onMessage(message);
|
||||
splitter.onMessage(message);
|
||||
Message<?> reply1 = testChannel.receive(100);
|
||||
Message<?> reply2 = testChannel.receive(100);
|
||||
assertEquals(message.getHeaders().getId(), reply1.getHeaders().getCorrelationId());
|
||||
|
||||
@@ -24,6 +24,7 @@ import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.integration.channel.QueueChannel;
|
||||
import org.springframework.integration.message.Message;
|
||||
import org.springframework.integration.message.MessageBuilder;
|
||||
|
||||
@@ -36,8 +37,11 @@ public class DefaultSplitterTests {
|
||||
public void splitMessageWithArrayPayload() throws Exception {
|
||||
String[] payload = new String[] { "x", "y", "z" };
|
||||
Message<String[]> message = MessageBuilder.withPayload(payload).build();
|
||||
DefaultSplitter splitter = new DefaultSplitter();
|
||||
List<Message<?>> replies = splitter.split(message);
|
||||
QueueChannel replyChannel = new QueueChannel();
|
||||
DefaultMessageSplitter splitter = new DefaultMessageSplitter();
|
||||
splitter.setOutputChannel(replyChannel);
|
||||
splitter.onMessage(message);
|
||||
List<Message<?>> replies = replyChannel.clear();
|
||||
assertEquals(3, replies.size());
|
||||
Message<?> reply1 = replies.get(0);
|
||||
assertNotNull(reply1);
|
||||
@@ -54,8 +58,11 @@ public class DefaultSplitterTests {
|
||||
public void splitMessageWithCollectionPayload() throws Exception {
|
||||
List<String> payload = Arrays.asList(new String[] { "x", "y", "z" });
|
||||
Message<List<String>> message = MessageBuilder.withPayload(payload).build();
|
||||
DefaultSplitter splitter = new DefaultSplitter();
|
||||
List<Message<?>> replies = splitter.split(message);
|
||||
QueueChannel replyChannel = new QueueChannel();
|
||||
DefaultMessageSplitter splitter = new DefaultMessageSplitter();
|
||||
splitter.setOutputChannel(replyChannel);
|
||||
splitter.onMessage(message);
|
||||
List<Message<?>> replies = replyChannel.clear();
|
||||
assertEquals(3, replies.size());
|
||||
Message<?> reply1 = replies.get(0);
|
||||
assertNotNull(reply1);
|
||||
|
||||
@@ -27,6 +27,8 @@ import java.util.List;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.integration.annotation.Header;
|
||||
import org.springframework.integration.annotation.Splitter;
|
||||
import org.springframework.integration.channel.QueueChannel;
|
||||
import org.springframework.integration.message.Message;
|
||||
import org.springframework.integration.message.MessageBuilder;
|
||||
import org.springframework.integration.message.StringMessage;
|
||||
@@ -43,7 +45,10 @@ public class MethodInvokingSplitterTests {
|
||||
public void splitStringToStringArray() throws Exception {
|
||||
StringMessage message = new StringMessage("foo.bar");
|
||||
MethodInvokingSplitter splitter = this.getSplitter("stringToStringArray");
|
||||
List<Message<?>> replies = splitter.split(message);
|
||||
QueueChannel replyChannel = new QueueChannel();
|
||||
splitter.setOutputChannel(replyChannel);
|
||||
splitter.onMessage(message);
|
||||
List<Message<?>> replies = replyChannel.clear();
|
||||
Message<?> reply1 = replies.get(0);
|
||||
assertNotNull(reply1);
|
||||
assertEquals("foo", reply1.getPayload());
|
||||
@@ -56,7 +61,10 @@ public class MethodInvokingSplitterTests {
|
||||
public void splitStringToStringList() throws Exception {
|
||||
StringMessage message = new StringMessage("foo.bar");
|
||||
MethodInvokingSplitter splitter = this.getSplitter("stringToStringList");
|
||||
List<Message<?>> replies = splitter.split(message);
|
||||
QueueChannel replyChannel = new QueueChannel();
|
||||
splitter.setOutputChannel(replyChannel);
|
||||
splitter.onMessage(message);
|
||||
List<Message<?>> replies = replyChannel.clear();
|
||||
Message<?> reply1 = replies.get(0);
|
||||
assertNotNull(reply1);
|
||||
assertEquals("foo", reply1.getPayload());
|
||||
@@ -69,7 +77,10 @@ public class MethodInvokingSplitterTests {
|
||||
public void splitMessageToStringArray() throws Exception {
|
||||
StringMessage message = new StringMessage("foo.bar");
|
||||
MethodInvokingSplitter splitter = this.getSplitter("messageToStringArray");
|
||||
List<Message<?>> replies = splitter.split(message);
|
||||
QueueChannel replyChannel = new QueueChannel();
|
||||
splitter.setOutputChannel(replyChannel);
|
||||
splitter.onMessage(message);
|
||||
List<Message<?>> replies = replyChannel.clear();
|
||||
Message<?> reply1 = replies.get(0);
|
||||
assertNotNull(reply1);
|
||||
assertEquals("foo", reply1.getPayload());
|
||||
@@ -82,7 +93,10 @@ public class MethodInvokingSplitterTests {
|
||||
public void splitMessageToStringList() throws Exception {
|
||||
StringMessage message = new StringMessage("foo.bar");
|
||||
MethodInvokingSplitter splitter = this.getSplitter("messageToStringList");
|
||||
List<Message<?>> replies = splitter.split(message);
|
||||
QueueChannel replyChannel = new QueueChannel();
|
||||
splitter.setOutputChannel(replyChannel);
|
||||
splitter.onMessage(message);
|
||||
List<Message<?>> replies = replyChannel.clear();
|
||||
Message<?> reply1 = replies.get(0);
|
||||
assertNotNull(reply1);
|
||||
assertEquals("foo", reply1.getPayload());
|
||||
@@ -95,7 +109,10 @@ public class MethodInvokingSplitterTests {
|
||||
public void splitMessageToMessageArray() throws Exception {
|
||||
StringMessage message = new StringMessage("foo.bar");
|
||||
MethodInvokingSplitter splitter = this.getSplitter("messageToMessageArray");
|
||||
List<Message<?>> replies = splitter.split(message);
|
||||
QueueChannel replyChannel = new QueueChannel();
|
||||
splitter.setOutputChannel(replyChannel);
|
||||
splitter.onMessage(message);
|
||||
List<Message<?>> replies = replyChannel.clear();
|
||||
Message<?> reply1 = replies.get(0);
|
||||
assertNotNull(reply1);
|
||||
assertEquals("foo", reply1.getPayload());
|
||||
@@ -108,7 +125,10 @@ public class MethodInvokingSplitterTests {
|
||||
public void splitMessageToMessageList() throws Exception {
|
||||
StringMessage message = new StringMessage("foo.bar");
|
||||
MethodInvokingSplitter splitter = this.getSplitter("messageToMessageList");
|
||||
List<Message<?>> replies = splitter.split(message);
|
||||
QueueChannel replyChannel = new QueueChannel();
|
||||
splitter.setOutputChannel(replyChannel);
|
||||
splitter.onMessage(message);
|
||||
List<Message<?>> replies = replyChannel.clear();
|
||||
Message<?> reply1 = replies.get(0);
|
||||
assertNotNull(reply1);
|
||||
assertEquals("foo", reply1.getPayload());
|
||||
@@ -121,7 +141,10 @@ public class MethodInvokingSplitterTests {
|
||||
public void splitStringToMessageArray() throws Exception {
|
||||
StringMessage message = new StringMessage("foo.bar");
|
||||
MethodInvokingSplitter splitter = this.getSplitter("stringToMessageArray");
|
||||
List<Message<?>> replies = splitter.split(message);
|
||||
QueueChannel replyChannel = new QueueChannel();
|
||||
splitter.setOutputChannel(replyChannel);
|
||||
splitter.onMessage(message);
|
||||
List<Message<?>> replies = replyChannel.clear();
|
||||
Message<?> reply1 = replies.get(0);
|
||||
assertNotNull(reply1);
|
||||
assertEquals("foo", reply1.getPayload());
|
||||
@@ -134,7 +157,10 @@ public class MethodInvokingSplitterTests {
|
||||
public void splitStringToMessageList() throws Exception {
|
||||
StringMessage message = new StringMessage("foo.bar");
|
||||
MethodInvokingSplitter splitter = this.getSplitter("stringToMessageList");
|
||||
List<Message<?>> replies = splitter.split(message);
|
||||
QueueChannel replyChannel = new QueueChannel();
|
||||
splitter.setOutputChannel(replyChannel);
|
||||
splitter.onMessage(message);
|
||||
List<Message<?>> replies = replyChannel.clear();
|
||||
Message<?> reply1 = replies.get(0);
|
||||
assertNotNull(reply1);
|
||||
assertEquals("foo", reply1.getPayload());
|
||||
@@ -147,7 +173,10 @@ public class MethodInvokingSplitterTests {
|
||||
public void splitStringToStringArrayConfiguredByMethodName() throws Exception {
|
||||
StringMessage message = new StringMessage("foo.bar");
|
||||
MethodInvokingSplitter splitter = new MethodInvokingSplitter(testBean, "stringToStringArray");
|
||||
List<Message<?>> replies = splitter.split(message);
|
||||
QueueChannel replyChannel = new QueueChannel();
|
||||
splitter.setOutputChannel(replyChannel);
|
||||
splitter.onMessage(message);
|
||||
List<Message<?>> replies = replyChannel.clear();
|
||||
Message<?> reply1 = replies.get(0);
|
||||
assertNotNull(reply1);
|
||||
assertEquals("foo", reply1.getPayload());
|
||||
@@ -160,7 +189,10 @@ public class MethodInvokingSplitterTests {
|
||||
public void splitStringToStringListConfiguredByMethodName() throws Exception {
|
||||
StringMessage message = new StringMessage("foo.bar");
|
||||
MethodInvokingSplitter splitter = new MethodInvokingSplitter(testBean, "stringToStringList");
|
||||
List<Message<?>> replies = splitter.split(message);
|
||||
QueueChannel replyChannel = new QueueChannel();
|
||||
splitter.setOutputChannel(replyChannel);
|
||||
splitter.onMessage(message);
|
||||
List<Message<?>> replies = replyChannel.clear();
|
||||
Message<?> reply1 = replies.get(0);
|
||||
assertNotNull(reply1);
|
||||
assertEquals("foo", reply1.getPayload());
|
||||
@@ -173,7 +205,10 @@ public class MethodInvokingSplitterTests {
|
||||
public void splitMessageToStringArrayConfiguredByMethodName() throws Exception {
|
||||
StringMessage message = new StringMessage("foo.bar");
|
||||
MethodInvokingSplitter splitter = new MethodInvokingSplitter(testBean, "messageToStringArray");
|
||||
List<Message<?>> replies = splitter.split(message);
|
||||
QueueChannel replyChannel = new QueueChannel();
|
||||
splitter.setOutputChannel(replyChannel);
|
||||
splitter.onMessage(message);
|
||||
List<Message<?>> replies = replyChannel.clear();
|
||||
Message<?> reply1 = replies.get(0);
|
||||
assertNotNull(reply1);
|
||||
assertEquals("foo", reply1.getPayload());
|
||||
@@ -186,7 +221,10 @@ public class MethodInvokingSplitterTests {
|
||||
public void splitMessageToStringListConfiguredByMethodName() throws Exception {
|
||||
StringMessage message = new StringMessage("foo.bar");
|
||||
MethodInvokingSplitter splitter = new MethodInvokingSplitter(testBean, "messageToStringList");
|
||||
List<Message<?>> replies = splitter.split(message);
|
||||
QueueChannel replyChannel = new QueueChannel();
|
||||
splitter.setOutputChannel(replyChannel);
|
||||
splitter.onMessage(message);
|
||||
List<Message<?>> replies = replyChannel.clear();
|
||||
Message<?> reply1 = replies.get(0);
|
||||
assertNotNull(reply1);
|
||||
assertEquals("foo", reply1.getPayload());
|
||||
@@ -199,7 +237,10 @@ public class MethodInvokingSplitterTests {
|
||||
public void splitMessageToMessageArrayConfiguredByMethodName() throws Exception {
|
||||
StringMessage message = new StringMessage("foo.bar");
|
||||
MethodInvokingSplitter splitter = new MethodInvokingSplitter(testBean, "messageToMessageArray");
|
||||
List<Message<?>> replies = splitter.split(message);
|
||||
QueueChannel replyChannel = new QueueChannel();
|
||||
splitter.setOutputChannel(replyChannel);
|
||||
splitter.onMessage(message);
|
||||
List<Message<?>> replies = replyChannel.clear();
|
||||
Message<?> reply1 = replies.get(0);
|
||||
assertNotNull(reply1);
|
||||
assertEquals("foo", reply1.getPayload());
|
||||
@@ -212,7 +253,10 @@ public class MethodInvokingSplitterTests {
|
||||
public void splitMessageToMessageListConfiguredByMethodName() throws Exception {
|
||||
StringMessage message = new StringMessage("foo.bar");
|
||||
MethodInvokingSplitter splitter = new MethodInvokingSplitter(testBean, "messageToMessageList");
|
||||
List<Message<?>> replies = splitter.split(message);
|
||||
QueueChannel replyChannel = new QueueChannel();
|
||||
splitter.setOutputChannel(replyChannel);
|
||||
splitter.onMessage(message);
|
||||
List<Message<?>> replies = replyChannel.clear();
|
||||
Message<?> reply1 = replies.get(0);
|
||||
assertNotNull(reply1);
|
||||
assertEquals("foo", reply1.getPayload());
|
||||
@@ -225,7 +269,10 @@ public class MethodInvokingSplitterTests {
|
||||
public void splitStringToMessageArrayConfiguredByMethodName() throws Exception {
|
||||
StringMessage message = new StringMessage("foo.bar");
|
||||
MethodInvokingSplitter splitter = new MethodInvokingSplitter(testBean, "stringToMessageArray");
|
||||
List<Message<?>> replies = splitter.split(message);
|
||||
QueueChannel replyChannel = new QueueChannel();
|
||||
splitter.setOutputChannel(replyChannel);
|
||||
splitter.onMessage(message);
|
||||
List<Message<?>> replies = replyChannel.clear();
|
||||
Message<?> reply1 = replies.get(0);
|
||||
assertNotNull(reply1);
|
||||
assertEquals("foo", reply1.getPayload());
|
||||
@@ -238,7 +285,10 @@ public class MethodInvokingSplitterTests {
|
||||
public void splitStringToMessageListConfiguredByMethodName() throws Exception {
|
||||
StringMessage message = new StringMessage("foo.bar");
|
||||
MethodInvokingSplitter splitter = new MethodInvokingSplitter(testBean, "stringToMessageList");
|
||||
List<Message<?>> replies = splitter.split(message);
|
||||
QueueChannel replyChannel = new QueueChannel();
|
||||
splitter.setOutputChannel(replyChannel);
|
||||
splitter.onMessage(message);
|
||||
List<Message<?>> replies = replyChannel.clear();
|
||||
Message<?> reply1 = replies.get(0);
|
||||
assertNotNull(reply1);
|
||||
assertEquals("foo", reply1.getPayload());
|
||||
@@ -248,10 +298,13 @@ public class MethodInvokingSplitterTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHeaderForObjectReturnValues() throws Exception {
|
||||
public void headerForObjectReturnValues() throws Exception {
|
||||
StringMessage message = new StringMessage("foo.bar");
|
||||
MethodInvokingSplitter splitter = this.getSplitter("stringToStringArray");
|
||||
List<Message<?>> replies = splitter.split(message);
|
||||
QueueChannel replyChannel = new QueueChannel();
|
||||
splitter.setOutputChannel(replyChannel);
|
||||
splitter.onMessage(message);
|
||||
List<Message<?>> replies = replyChannel.clear();
|
||||
Message<?> reply1 = replies.get(0);
|
||||
assertNotNull(reply1);
|
||||
assertEquals(new Integer(2), reply1.getHeaders().getSequenceSize());
|
||||
@@ -265,10 +318,13 @@ public class MethodInvokingSplitterTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHeaderForMessageReturnValues() throws Exception {
|
||||
public void headerForMessageReturnValues() throws Exception {
|
||||
StringMessage message = new StringMessage("foo.bar");
|
||||
MethodInvokingSplitter splitter = this.getSplitter("messageToMessageList");
|
||||
List<Message<?>> replies = splitter.split(message);
|
||||
QueueChannel replyChannel = new QueueChannel();
|
||||
splitter.setOutputChannel(replyChannel);
|
||||
splitter.onMessage(message);
|
||||
List<Message<?>> replies = replyChannel.clear();
|
||||
Message<?> reply1 = replies.get(0);
|
||||
assertNotNull(reply1);
|
||||
assertEquals(new Integer(2), reply1.getHeaders().getSequenceSize());
|
||||
@@ -286,7 +342,10 @@ public class MethodInvokingSplitterTests {
|
||||
Message<String> message = MessageBuilder.withPayload("ignored")
|
||||
.setHeader("testHeader", "foo.bar").build();
|
||||
MethodInvokingSplitter splitter = this.getSplitter("splitHeader");
|
||||
List<Message<?>> replies = splitter.split(message);
|
||||
QueueChannel replyChannel = new QueueChannel();
|
||||
splitter.setOutputChannel(replyChannel);
|
||||
splitter.onMessage(message);
|
||||
List<Message<?>> replies = replyChannel.clear();
|
||||
Message<?> reply1 = replies.get(0);
|
||||
assertNotNull(reply1);
|
||||
assertEquals("foo", reply1.getPayload());
|
||||
@@ -301,7 +360,10 @@ public class MethodInvokingSplitterTests {
|
||||
.setHeader("testHeader", "c.d").build();
|
||||
Method splittingMethod = this.testBean.getClass().getMethod("splitPayloadAndHeader", String.class, String.class);
|
||||
MethodInvokingSplitter splitter = new MethodInvokingSplitter(testBean, splittingMethod);
|
||||
List<Message<?>> replies = splitter.split(message);
|
||||
QueueChannel replyChannel = new QueueChannel();
|
||||
splitter.setOutputChannel(replyChannel);
|
||||
splitter.onMessage(message);
|
||||
List<Message<?>> replies = replyChannel.clear();
|
||||
Message<?> reply1 = replies.get(0);
|
||||
assertNotNull(reply1);
|
||||
assertEquals("a", reply1.getPayload());
|
||||
@@ -316,6 +378,50 @@ public class MethodInvokingSplitterTests {
|
||||
assertEquals("d", reply4.getPayload());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void singleAnnotation() {
|
||||
StringMessage message = new StringMessage("foo.bar");
|
||||
SingleAnnotationTestBean annotatedBean = new SingleAnnotationTestBean();
|
||||
MethodInvokingSplitter splitter = new MethodInvokingSplitter(annotatedBean);
|
||||
QueueChannel replyChannel = new QueueChannel();
|
||||
splitter.setOutputChannel(replyChannel);
|
||||
splitter.onMessage(message);
|
||||
List<Message<?>> replies = replyChannel.clear();
|
||||
Message<?> reply1 = replies.get(0);
|
||||
assertNotNull(reply1);
|
||||
assertEquals("foo", reply1.getPayload());
|
||||
Message<?> reply2 = replies.get(1);
|
||||
assertNotNull(reply2);
|
||||
assertEquals("bar", reply2.getPayload());
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void multipleAnnotations() {
|
||||
new MethodInvokingSplitter(new MultipleAnnotationTestBean());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void singlePublicMethod() {
|
||||
StringMessage message = new StringMessage("foo.bar");
|
||||
SinglePublicMethodTestBean testBean = new SinglePublicMethodTestBean();
|
||||
MethodInvokingSplitter splitter = new MethodInvokingSplitter(testBean);
|
||||
QueueChannel replyChannel = new QueueChannel();
|
||||
splitter.setOutputChannel(replyChannel);
|
||||
splitter.onMessage(message);
|
||||
List<Message<?>> replies = replyChannel.clear();
|
||||
Message<?> reply1 = replies.get(0);
|
||||
assertNotNull(reply1);
|
||||
assertEquals("foo", reply1.getPayload());
|
||||
Message<?> reply2 = replies.get(1);
|
||||
assertNotNull(reply2);
|
||||
assertEquals("bar", reply2.getPayload());
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void multiplePublicMethods() {
|
||||
new MethodInvokingSplitter(new MultiplePublicMethodTestBean());
|
||||
}
|
||||
|
||||
|
||||
private MethodInvokingSplitter getSplitter(String methodName) throws Exception {
|
||||
Class<?> paramType = methodName.startsWith("message") ? Message.class : String.class;
|
||||
@@ -395,4 +501,55 @@ public class MethodInvokingSplitterTests {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static class SingleAnnotationTestBean {
|
||||
|
||||
@Splitter
|
||||
public String[] annotatedMethod(String input) {
|
||||
return input.split("\\.");
|
||||
}
|
||||
|
||||
public String[] anotherMethod(String input) {
|
||||
throw new UnsupportedOperationException("incorrect test invocation");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static class MultipleAnnotationTestBean {
|
||||
|
||||
@Splitter
|
||||
public String[] method1(String input) {
|
||||
throw new UnsupportedOperationException("incorrect test invocation");
|
||||
}
|
||||
|
||||
@Splitter
|
||||
public String[] method2(String input) {
|
||||
throw new UnsupportedOperationException("incorrect test invocation");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static class SinglePublicMethodTestBean {
|
||||
|
||||
public String[] publicMethod(String input) {
|
||||
return input.split("\\.");
|
||||
}
|
||||
|
||||
String[] anotherMethod(String input) {
|
||||
throw new UnsupportedOperationException("incorrect test invocation");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static class MultiplePublicMethodTestBean {
|
||||
|
||||
public String[] method1(String input) {
|
||||
throw new UnsupportedOperationException("incorrect test invocation");
|
||||
}
|
||||
|
||||
public String[] method2(String input) {
|
||||
throw new UnsupportedOperationException("incorrect test invocation");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user