The 'correlationId' is now being handled by the AbstractMessageHandlerAdapter (see INT-127). SplitterMessageHandlerAdapter now properly sets the 'sequenceNumber' and 'sequenceSize' properties on the reply's MessageHeader (INT-128).

This commit is contained in:
Mark Fisher
2008-02-22 15:22:52 +00:00
parent 4226a980de
commit 094a131b1a
6 changed files with 198 additions and 54 deletions

View File

@@ -0,0 +1,140 @@
/*
* 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.handler;
import static org.junit.Assert.assertEquals;
import java.util.HashMap;
import java.util.Map;
import org.junit.Test;
import org.springframework.integration.channel.ChannelRegistry;
import org.springframework.integration.channel.DefaultChannelRegistry;
import org.springframework.integration.channel.MessageChannel;
import org.springframework.integration.channel.SimpleChannel;
import org.springframework.integration.message.GenericMessage;
import org.springframework.integration.message.Message;
import org.springframework.integration.message.StringMessage;
import org.springframework.integration.router.SplitterMessageHandlerAdapter;
import org.springframework.integration.util.SimpleMethodInvoker;
/**
* @author Mark Fisher
*/
public class CorrelationIdTests {
@Test
public void testCorrelationIdPassedIfAvailable() {
Object correlationId = "123-ABC";
Message<?> message = new StringMessage("test");
message.getHeader().setCorrelationId(correlationId);
DefaultMessageHandlerAdapter<TestBean> adapter = new DefaultMessageHandlerAdapter<TestBean>();
adapter.setObject(new TestBean());
adapter.setMethodName("upperCase");
adapter.afterPropertiesSet();
Message<?> reply = adapter.handle(message);
assertEquals(correlationId, reply.getHeader().getCorrelationId());
}
@Test
public void testCorrelationIdCopiedFromMessageIdByDefault() {
Message<?> message = new StringMessage("test");
DefaultMessageHandlerAdapter<TestBean> adapter = new DefaultMessageHandlerAdapter<TestBean>();
adapter.setObject(new TestBean());
adapter.setMethodName("upperCase");
adapter.afterPropertiesSet();
Message<?> reply = adapter.handle(message);
assertEquals(message.getId(), reply.getHeader().getCorrelationId());
}
@Test
public void testCorrelationNotPassedIfAlreadySetByHandler() throws Exception {
Object correlationId = "123-ABC";
Message<?> message = new StringMessage("test");
message.getHeader().setCorrelationId(correlationId);
AbstractMessageHandlerAdapter<TestBean> adapter = new AbstractMessageHandlerAdapter<TestBean>() {
@Override
protected Object doHandle(Message message, SimpleMethodInvoker invoker) {
Object result = invoker.invokeMethod(message.getPayload());
Message<?> resultMessage = new GenericMessage<Object>(result);
resultMessage.getHeader().setCorrelationId("456-XYZ");
return resultMessage;
}
};
adapter.setObject(new TestBean());
adapter.setMethodName("upperCase");
adapter.afterPropertiesSet();
Message<?> reply = adapter.handle(message);
assertEquals("456-XYZ", reply.getHeader().getCorrelationId());
}
@Test
public void testCorrelationNotCopiedIfAlreadySetByHandler() throws Exception {
Message<?> message = new StringMessage("test");
AbstractMessageHandlerAdapter<TestBean> adapter = new AbstractMessageHandlerAdapter<TestBean>() {
@Override
protected Object doHandle(Message message, SimpleMethodInvoker invoker) {
Object result = invoker.invokeMethod(message.getPayload());
Message<?> resultMessage = new GenericMessage<Object>(result);
resultMessage.getHeader().setCorrelationId("456-XYZ");
return resultMessage;
}
};
adapter.setObject(new TestBean());
adapter.setMethodName("upperCase");
adapter.afterPropertiesSet();
Message<?> reply = adapter.handle(message);
assertEquals("456-XYZ", reply.getHeader().getCorrelationId());
}
@Test
public void testCorrelationIdWithSplitter() throws Exception {
Message<?> message = new StringMessage("test1,test2");
DefaultMessageHandlerAdapter<TestBean> adapter = new DefaultMessageHandlerAdapter<TestBean>();
adapter.setObject(new TestBean());
adapter.setMethodName("upperCase");
adapter.afterPropertiesSet();
MessageChannel testChannel = new SimpleChannel();
ChannelRegistry channelRegistry = new DefaultChannelRegistry();
channelRegistry.registerChannel("testChannel", testChannel);
Map<String, String> attributes = new HashMap<String, String>();
attributes.put("channel", "testChannel");
SplitterMessageHandlerAdapter<TestBean> splitter = new SplitterMessageHandlerAdapter<TestBean>(
new TestBean(), TestBean.class.getMethod("split", String.class), attributes);
splitter.setChannelRegistry(channelRegistry);
splitter.afterPropertiesSet();
splitter.handle(message);
Message<?> reply1 = testChannel.receive(100);
Message<?> reply2 = testChannel.receive(100);
assertEquals(message.getId(), reply1.getHeader().getCorrelationId());
assertEquals(message.getId(), reply2.getHeader().getCorrelationId());
}
private static class TestBean {
public String upperCase(String input) {
return input.toUpperCase();
}
public String[] split(String input) {
return input.split(",");
}
}
}