SplitterMessageHandler replaces SplitterMessageHandlerAdapter.

This commit is contained in:
Mark Fisher
2008-08-12 00:22:02 +00:00
parent c63f65f256
commit c2e6be171d
14 changed files with 198 additions and 301 deletions

View File

@@ -30,7 +30,7 @@ import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.integration.bus.MessageBus;
import org.springframework.integration.config.MessageBusParser;
import org.springframework.integration.endpoint.HandlerEndpoint;
import org.springframework.integration.endpoint.SimpleEndpoint;
import org.springframework.integration.handler.MessageHandler;
import org.springframework.integration.router.AggregatingMessageHandler;
import org.springframework.integration.router.CompletionStrategyAdapter;
@@ -111,8 +111,8 @@ public class AggregatorAnnotationTests {
@SuppressWarnings("unchecked")
private DirectFieldAccessor getDirectFieldAccessorForAggregatingHandler(ApplicationContext context, final String endpointName) {
MessageBus messageBus = this.getMessageBus(context);
HandlerEndpoint endpoint = (HandlerEndpoint) messageBus.lookupEndpoint(endpointName + ".MessageHandler.endpoint");
MessageHandler handler = endpoint.getHandler();
SimpleEndpoint<?> endpoint = (SimpleEndpoint<?>) messageBus.lookupEndpoint(endpointName + ".MessageHandler.endpoint");
MessageHandler handler = (MessageHandler) new DirectFieldAccessor(endpoint).getPropertyValue("handler");
try {
if (AopUtils.isAopProxy(handler)) {
DelegatingIntroductionInterceptor interceptor = (DelegatingIntroductionInterceptor)
@@ -124,7 +124,7 @@ public class AggregatorAnnotationTests {
catch (Exception e) {
// will return the accessor for the handler
}
return new DirectFieldAccessor(endpoint.getHandler());
return new DirectFieldAccessor(handler);
}
private MessageBus getMessageBus(ApplicationContext context) {

View File

@@ -56,7 +56,7 @@ import org.springframework.integration.channel.MessageChannel;
import org.springframework.integration.channel.PollableChannel;
import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.endpoint.EndpointInterceptor;
import org.springframework.integration.endpoint.HandlerEndpoint;
import org.springframework.integration.endpoint.SimpleEndpoint;
import org.springframework.integration.handler.MessageHandler;
import org.springframework.integration.message.Message;
import org.springframework.integration.message.StringMessage;
@@ -185,7 +185,7 @@ public class MessagingAnnotationPostProcessorTests {
postProcessor.afterPropertiesSet();
ConcurrencyAnnotationTestBean testBean = new ConcurrencyAnnotationTestBean();
postProcessor.postProcessAfterInitialization(testBean, "testBean");
HandlerEndpoint endpoint = (HandlerEndpoint) messageBus.lookupEndpoint("testBean.MessageHandler.endpoint");
SimpleEndpoint<?> endpoint = (SimpleEndpoint<?>) messageBus.lookupEndpoint("testBean.MessageHandler.endpoint");
DirectFieldAccessor accessor = new DirectFieldAccessor(endpoint);
List<EndpointInterceptor> interceptors = (List<EndpointInterceptor>) accessor.getPropertyValue("interceptors");
assertEquals(1, interceptors.size());
@@ -363,7 +363,7 @@ public class MessagingAnnotationPostProcessorTests {
postProcessor.afterPropertiesSet();
AnnotatedEndpointWithPolledAnnotation endpoint = new AnnotatedEndpointWithPolledAnnotation();
postProcessor.postProcessAfterInitialization(endpoint, "testBean");
HandlerEndpoint processedEndpoint = (HandlerEndpoint) messageBus.lookupEndpoint("testBean.MessageHandler.endpoint");
SimpleEndpoint<?> processedEndpoint = (SimpleEndpoint<?>) messageBus.lookupEndpoint("testBean.MessageHandler.endpoint");
Schedule schedule = processedEndpoint.getSchedule();
assertEquals(PollingSchedule.class, schedule.getClass());
PollingSchedule pollingSchedule = (PollingSchedule) schedule;

View File

@@ -21,13 +21,12 @@ import static org.junit.Assert.assertTrue;
import org.junit.Test;
import org.springframework.integration.channel.ChannelRegistry;
import org.springframework.integration.channel.DefaultChannelRegistry;
import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.endpoint.SimpleEndpoint;
import org.springframework.integration.message.Message;
import org.springframework.integration.message.MessageBuilder;
import org.springframework.integration.message.StringMessage;
import org.springframework.integration.router.SplitterMessageHandlerAdapter;
import org.springframework.integration.splitter.SplitterMessageHandler;
/**
* @author Mark Fisher
@@ -112,19 +111,13 @@ public class CorrelationIdTests {
@Test
public void testCorrelationIdWithSplitter() throws Exception {
Message<?> message = new StringMessage("test1,test2");
DefaultMessageHandlerAdapter adapter = new DefaultMessageHandlerAdapter();
adapter.setObject(new TestBean());
adapter.setMethodName("upperCase");
adapter.afterPropertiesSet();
QueueChannel testChannel = new QueueChannel();
ChannelRegistry channelRegistry = new DefaultChannelRegistry();
channelRegistry.registerChannel("testChannel", testChannel);
SplitterMessageHandlerAdapter splitter = new SplitterMessageHandlerAdapter(
SplitterMessageHandler splitter = new SplitterMessageHandler(
new TestBean(), TestBean.class.getMethod("split", String.class));
splitter.setOutputChannelName("testChannel");
splitter.setChannelRegistry(channelRegistry);
SimpleEndpoint<SplitterMessageHandler> endpoint = new SimpleEndpoint<SplitterMessageHandler>(splitter);
endpoint.setOutputChannel(testChannel);
splitter.afterPropertiesSet();
splitter.handle(message);
endpoint.send(message);
Message<?> reply1 = testChannel.receive(100);
Message<?> reply2 = testChannel.receive(100);
assertEquals(message.getHeaders().getId(), reply1.getHeaders().getCorrelationId());

View File

@@ -26,192 +26,223 @@ import java.util.List;
import org.junit.Test;
import org.springframework.integration.channel.ChannelRegistry;
import org.springframework.integration.channel.DefaultChannelRegistry;
import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.message.CompositeMessage;
import org.springframework.integration.message.Message;
import org.springframework.integration.message.MessagingException;
import org.springframework.integration.message.StringMessage;
import org.springframework.integration.splitter.SplitterMessageHandler;
/**
* @author Mark Fisher
*/
public class SplitterMessageHandlerAdapterTests {
private QueueChannel testChannel = new QueueChannel();
private ChannelRegistry channelRegistry = new DefaultChannelRegistry();
public class SplitterMessageHandlerTests {
private SplitterTestBean testBean = new SplitterTestBean();
public SplitterMessageHandlerAdapterTests() {
this.channelRegistry.registerChannel("testChannel", testChannel);
}
@Test
public void testSplitPayloadToStringArray() throws Exception {
public void splitStringToStringArray() throws Exception {
StringMessage message = new StringMessage("foo.bar");
SplitterMessageHandlerAdapter adapter = this.getAdapter("stringToStringArray");
adapter.handle(message);
Message<?> reply1 = testChannel.receive(0);
SplitterMessageHandler handler = this.getHandler("stringToStringArray");
List<Message<?>> replies = invokeHandler(handler, message);
Message<?> reply1 = replies.get(0);
assertNotNull(reply1);
assertEquals("foo", reply1.getPayload());
Message<?> reply2 = testChannel.receive(0);
Message<?> reply2 = replies.get(1);
assertNotNull(reply2);
assertEquals("bar", reply2.getPayload());
}
@Test
public void testSplitPayloadToStringList() throws Exception {
public void splitStringToStringList() throws Exception {
StringMessage message = new StringMessage("foo.bar");
SplitterMessageHandlerAdapter adapter = this.getAdapter("stringToStringList");
adapter.handle(message);
Message<?> reply1 = testChannel.receive(0);
SplitterMessageHandler handler = this.getHandler("stringToStringList");
List<Message<?>> replies = invokeHandler(handler, message);
Message<?> reply1 = replies.get(0);
assertNotNull(reply1);
assertEquals("foo", reply1.getPayload());
Message<?> reply2 = testChannel.receive(0);
Message<?> reply2 = replies.get(1);
assertNotNull(reply2);
assertEquals("bar", reply2.getPayload());
}
@Test
public void testSplitMessageToStringArray() throws Exception {
public void splitMessageToStringArray() throws Exception {
StringMessage message = new StringMessage("foo.bar");
SplitterMessageHandlerAdapter adapter = this.getAdapter("messageToStringArray");
adapter.handle(message);
Message<?> reply1 = testChannel.receive(0);
SplitterMessageHandler handler = this.getHandler("messageToStringArray");
List<Message<?>> replies = invokeHandler(handler, message);
Message<?> reply1 = replies.get(0);
assertNotNull(reply1);
assertEquals("foo", reply1.getPayload());
Message<?> reply2 = testChannel.receive(0);
Message<?> reply2 = replies.get(1);
assertNotNull(reply2);
assertEquals("bar", reply2.getPayload());
}
@Test
public void testSplitMessageToStringList() throws Exception {
public void splitMessageToStringList() throws Exception {
StringMessage message = new StringMessage("foo.bar");
SplitterMessageHandlerAdapter adapter = this.getAdapter("messageToStringList");
adapter.handle(message);
Message<?> reply1 = testChannel.receive(0);
SplitterMessageHandler handler = this.getHandler("messageToStringList");
List<Message<?>> replies = invokeHandler(handler, message);
Message<?> reply1 = replies.get(0);
assertNotNull(reply1);
assertEquals("foo", reply1.getPayload());
Message<?> reply2 = testChannel.receive(0);
Message<?> reply2 = replies.get(1);
assertNotNull(reply2);
assertEquals("bar", reply2.getPayload());
}
@Test
public void testSplitMessageToMessageArray() throws Exception {
public void splitMessageToMessageArray() throws Exception {
StringMessage message = new StringMessage("foo.bar");
SplitterMessageHandlerAdapter adapter = this.getAdapter("messageToMessageArray");
adapter.handle(message);
Message<?> reply1 = testChannel.receive(0);
SplitterMessageHandler handler = this.getHandler("messageToMessageArray");
List<Message<?>> replies = invokeHandler(handler, message);
Message<?> reply1 = replies.get(0);
assertNotNull(reply1);
assertEquals("foo", reply1.getPayload());
Message<?> reply2 = testChannel.receive(0);
Message<?> reply2 = replies.get(1);
assertNotNull(reply2);
assertEquals("bar", reply2.getPayload());
}
@Test
public void testSplitMessageToMessageList() throws Exception {
public void splitMessageToMessageList() throws Exception {
StringMessage message = new StringMessage("foo.bar");
SplitterMessageHandlerAdapter adapter = this.getAdapter("messageToMessageList");
adapter.handle(message);
Message<?> reply1 = testChannel.receive(0);
SplitterMessageHandler handler = this.getHandler("messageToMessageList");
List<Message<?>> replies = invokeHandler(handler, message);
Message<?> reply1 = replies.get(0);
assertNotNull(reply1);
assertEquals("foo", reply1.getPayload());
Message<?> reply2 = testChannel.receive(0);
Message<?> reply2 = replies.get(1);
assertNotNull(reply2);
assertEquals("bar", reply2.getPayload());
}
@Test
public void testSplitStringToMessageArray() throws Exception {
public void splitStringToMessageArray() throws Exception {
StringMessage message = new StringMessage("foo.bar");
SplitterMessageHandlerAdapter adapter = this.getAdapter("stringToMessageArray");
adapter.handle(message);
Message<?> reply1 = testChannel.receive(0);
SplitterMessageHandler handler = this.getHandler("stringToMessageArray");
List<Message<?>> replies = invokeHandler(handler, message);
Message<?> reply1 = replies.get(0);
assertNotNull(reply1);
assertEquals("foo", reply1.getPayload());
Message<?> reply2 = testChannel.receive(0);
Message<?> reply2 = replies.get(1);
assertNotNull(reply2);
assertEquals("bar", reply2.getPayload());
}
@Test
public void testSplitStringToMessageList() throws Exception {
public void splitStringToMessageList() throws Exception {
StringMessage message = new StringMessage("foo.bar");
SplitterMessageHandlerAdapter adapter = this.getAdapter("stringToMessageList");
adapter.handle(message);
Message<?> reply1 = testChannel.receive(0);
SplitterMessageHandler handler = this.getHandler("stringToMessageList");
List<Message<?>> replies = invokeHandler(handler, message);
Message<?> reply1 = replies.get(0);
assertNotNull(reply1);
assertEquals("foo", reply1.getPayload());
Message<?> reply2 = testChannel.receive(0);
assertNotNull(reply2);
assertEquals("bar", reply2.getPayload());
}
@Test(expected=MessagingException.class)
public void testInvalidReturnType() throws Exception {
Method splittingMethod = this.testBean.getClass().getMethod("invalidParameterCount", String.class, String.class);
SplitterMessageHandlerAdapter adapter = new SplitterMessageHandlerAdapter(testBean, splittingMethod);
adapter.setOutputChannelName("testChannel");
adapter.setChannelRegistry(channelRegistry);
adapter.afterPropertiesSet();
StringMessage message = new StringMessage("foo.bar");
adapter.handle(message);
}
@Test
public void testSplitPayloadToStringArrayConfiguredByMethodName() throws Exception {
StringMessage message = new StringMessage("foo.bar");
SplitterMessageHandlerAdapter adapter =
new SplitterMessageHandlerAdapter(testBean, "stringToStringArray");
adapter.setOutputChannelName("testChannel");
adapter.setChannelRegistry(channelRegistry);
adapter.afterPropertiesSet();
adapter.handle(message);
Message<?> reply1 = testChannel.receive(0);
assertNotNull(reply1);
assertEquals("foo", reply1.getPayload());
Message<?> reply2 = testChannel.receive(0);
Message<?> reply2 = replies.get(1);
assertNotNull(reply2);
assertEquals("bar", reply2.getPayload());
}
@Test
public void testSplitMessageToStringArrayConfiguredByMethodName() throws Exception {
public void splitStringToStringArrayConfiguredByMethodName() throws Exception {
StringMessage message = new StringMessage("foo.bar");
SplitterMessageHandlerAdapter adapter =
new SplitterMessageHandlerAdapter(testBean, "messageToStringArray");
adapter.setOutputChannelName("testChannel");
adapter.setChannelRegistry(channelRegistry);
adapter.afterPropertiesSet();
adapter.handle(message);
Message<?> reply1 = testChannel.receive(0);
SplitterMessageHandler handler = new SplitterMessageHandler(testBean, "stringToStringArray");
List<Message<?>> replies = invokeHandler(handler, message);
Message<?> reply1 = replies.get(0);
assertNotNull(reply1);
assertEquals("foo", reply1.getPayload());
Message<?> reply2 = testChannel.receive(0);
Message<?> reply2 = replies.get(1);
assertNotNull(reply2);
assertEquals("bar", reply2.getPayload());
}
@Test
public void testSplitStringToMessageListConfiguredByMethodName() throws Exception {
public void splitStringToStringListConfiguredByMethodName() throws Exception {
StringMessage message = new StringMessage("foo.bar");
SplitterMessageHandlerAdapter adapter =
new SplitterMessageHandlerAdapter(testBean, "stringToMessageList");
adapter.setOutputChannelName("testChannel");
adapter.setChannelRegistry(channelRegistry);
adapter.afterPropertiesSet();
adapter.handle(message);
Message<?> reply1 = testChannel.receive(0);
SplitterMessageHandler handler = new SplitterMessageHandler(testBean, "stringToStringList");
List<Message<?>> replies = invokeHandler(handler, message);
Message<?> reply1 = replies.get(0);
assertNotNull(reply1);
assertEquals("foo", reply1.getPayload());
Message<?> reply2 = testChannel.receive(0);
Message<?> reply2 = replies.get(1);
assertNotNull(reply2);
assertEquals("bar", reply2.getPayload());
}
@Test
public void splitMessageToStringArrayConfiguredByMethodName() throws Exception {
StringMessage message = new StringMessage("foo.bar");
SplitterMessageHandler handler = new SplitterMessageHandler(testBean, "messageToStringArray");
List<Message<?>> replies = invokeHandler(handler, message);
Message<?> reply1 = replies.get(0);
assertNotNull(reply1);
assertEquals("foo", reply1.getPayload());
Message<?> reply2 = replies.get(1);
assertNotNull(reply2);
assertEquals("bar", reply2.getPayload());
}
@Test
public void splitMessageToStringListConfiguredByMethodName() throws Exception {
StringMessage message = new StringMessage("foo.bar");
SplitterMessageHandler handler = new SplitterMessageHandler(testBean, "messageToStringList");
List<Message<?>> replies = invokeHandler(handler, message);
Message<?> reply1 = replies.get(0);
assertNotNull(reply1);
assertEquals("foo", reply1.getPayload());
Message<?> reply2 = replies.get(1);
assertNotNull(reply2);
assertEquals("bar", reply2.getPayload());
}
@Test
public void splitMessageToMessageArrayConfiguredByMethodName() throws Exception {
StringMessage message = new StringMessage("foo.bar");
SplitterMessageHandler handler = new SplitterMessageHandler(testBean, "messageToMessageArray");
List<Message<?>> replies = invokeHandler(handler, message);
Message<?> reply1 = replies.get(0);
assertNotNull(reply1);
assertEquals("foo", reply1.getPayload());
Message<?> reply2 = replies.get(1);
assertNotNull(reply2);
assertEquals("bar", reply2.getPayload());
}
@Test
public void splitMessageToMessageListConfiguredByMethodName() throws Exception {
StringMessage message = new StringMessage("foo.bar");
SplitterMessageHandler handler = new SplitterMessageHandler(testBean, "messageToMessageList");
List<Message<?>> replies = invokeHandler(handler, message);
Message<?> reply1 = replies.get(0);
assertNotNull(reply1);
assertEquals("foo", reply1.getPayload());
Message<?> reply2 = replies.get(1);
assertNotNull(reply2);
assertEquals("bar", reply2.getPayload());
}
@Test
public void splitStringToMessageArrayConfiguredByMethodName() throws Exception {
StringMessage message = new StringMessage("foo.bar");
SplitterMessageHandler handler = new SplitterMessageHandler(testBean, "stringToMessageArray");
List<Message<?>> replies = invokeHandler(handler, message);
Message<?> reply1 = replies.get(0);
assertNotNull(reply1);
assertEquals("foo", reply1.getPayload());
Message<?> reply2 = replies.get(1);
assertNotNull(reply2);
assertEquals("bar", reply2.getPayload());
}
@Test
public void splitStringToMessageListConfiguredByMethodName() throws Exception {
StringMessage message = new StringMessage("foo.bar");
SplitterMessageHandler handler = new SplitterMessageHandler(testBean, "stringToMessageList");
List<Message<?>> replies = invokeHandler(handler, message);
Message<?> reply1 = replies.get(0);
assertNotNull(reply1);
assertEquals("foo", reply1.getPayload());
Message<?> reply2 = replies.get(1);
assertNotNull(reply2);
assertEquals("bar", reply2.getPayload());
}
@@ -219,14 +250,14 @@ public class SplitterMessageHandlerAdapterTests {
@Test
public void testHeaderForObjectReturnValues() throws Exception {
StringMessage message = new StringMessage("foo.bar");
SplitterMessageHandlerAdapter adapter = this.getAdapter("stringToStringArray");
adapter.handle(message);
Message<?> reply1 = testChannel.receive(0);
SplitterMessageHandler handler = this.getHandler("stringToStringArray");
List<Message<?>> replies = invokeHandler(handler, message);
Message<?> reply1 = replies.get(0);
assertNotNull(reply1);
assertEquals(new Integer(2), reply1.getHeaders().getSequenceSize());
assertEquals(new Integer(1), reply1.getHeaders().getSequenceNumber());
assertEquals(message.getHeaders().getId(), reply1.getHeaders().getCorrelationId());
Message<?> reply2 = testChannel.receive(0);
Message<?> reply2 = replies.get(1);
assertNotNull(reply2);
assertEquals(new Integer(2), reply2.getHeaders().getSequenceSize());
assertEquals(new Integer(2), reply2.getHeaders().getSequenceNumber());
@@ -236,14 +267,14 @@ public class SplitterMessageHandlerAdapterTests {
@Test
public void testHeaderForMessageReturnValues() throws Exception {
StringMessage message = new StringMessage("foo.bar");
SplitterMessageHandlerAdapter adapter = this.getAdapter("messageToMessageList");
adapter.handle(message);
Message<?> reply1 = testChannel.receive(0);
SplitterMessageHandler handler = this.getHandler("messageToMessageList");
List<Message<?>> replies = invokeHandler(handler, message);
Message<?> reply1 = replies.get(0);
assertNotNull(reply1);
assertEquals(new Integer(2), reply1.getHeaders().getSequenceSize());
assertEquals(new Integer(1), reply1.getHeaders().getSequenceNumber());
assertEquals(message.getHeaders().getId(), reply1.getHeaders().getCorrelationId());
Message<?> reply2 = testChannel.receive(0);
Message<?> reply2 = replies.get(1);
assertNotNull(reply2);
assertEquals(new Integer(2), reply2.getHeaders().getSequenceSize());
assertEquals(new Integer(2), reply2.getHeaders().getSequenceNumber());
@@ -251,15 +282,14 @@ public class SplitterMessageHandlerAdapterTests {
}
private SplitterMessageHandlerAdapter getAdapter(String methodName) throws Exception {
private SplitterMessageHandler getHandler(String methodName) throws Exception {
Class<?> paramType = methodName.startsWith("message") ? Message.class : String.class;
Method splittingMethod = this.testBean.getClass().getMethod(methodName, paramType);
SplitterMessageHandlerAdapter adapter =
new SplitterMessageHandlerAdapter(testBean, splittingMethod);
adapter.setOutputChannelName("testChannel");
adapter.setChannelRegistry(channelRegistry);
adapter.afterPropertiesSet();
return adapter;
return new SplitterMessageHandler(testBean, splittingMethod);
}
private static List<Message<?>> invokeHandler(SplitterMessageHandler handler, Message<?> message) {
return ((CompositeMessage) handler.handle(message)).getPayload();
}
@@ -316,10 +346,6 @@ public class SplitterMessageHandlerAdapterTests {
}
return messages;
}
public String[] invalidParameterCount(String param1, String param2) {
return null;
}
}
}