INT-1442: Added check for empty collection to AbstractMessageSplitter.

- also introduced testcase based on suggestion from Jettro (see issue)
- leaving a TODO for the awkwardly placed return statement (AbstractMessageSplitter needs more rigorous cleanup which I'll tackle separately)
This commit is contained in:
Iwein Fuld
2010-09-13 14:50:01 +02:00
parent 0c247c3a84
commit 014ba8d8f2
2 changed files with 28 additions and 14 deletions

View File

@@ -16,22 +16,19 @@
package org.springframework.integration.splitter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.UUID;
import org.springframework.integration.Message;
import org.springframework.integration.MessageHeaders;
import org.springframework.integration.handler.AbstractReplyProducingMessageHandler;
import org.springframework.integration.support.MessageBuilder;
import java.util.*;
/**
* Base class for Message-splitting handlers.
*
* @author Mark Fisher
* @author Dave Syer
* @author Iwein Fuld
*/
public abstract class AbstractMessageSplitter extends AbstractReplyProducingMessageHandler {
@@ -62,6 +59,10 @@ public abstract class AbstractMessageSplitter extends AbstractReplyProducingMess
List<MessageBuilder<?>> messageBuilders = new ArrayList<MessageBuilder<?>>();
if (result instanceof Collection) {
Collection<?> items = (Collection<?>) result;
//TODO put this return statement in a more obvious place
if(items.isEmpty()){
return null;
}
int sequenceNumber = 0;
int sequenceSize = items.size();
for (Object item : items) {

View File

@@ -16,23 +16,25 @@
package org.springframework.integration.splitter;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.util.Arrays;
import java.util.List;
import org.junit.Test;
import org.springframework.integration.Message;
import org.springframework.integration.channel.DirectChannel;
import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.endpoint.EventDrivenConsumer;
import org.springframework.integration.support.MessageBuilder;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import static junit.framework.Assert.assertEquals;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.*;
/**
* @author Mark Fisher
* @author Iwein Fuld
*/
public class DefaultSplitterTests {
@@ -92,4 +94,15 @@ public class DefaultSplitterTests {
assertEquals(message.getHeaders().getId(), reply.getHeaders().getCorrelationId());
}
@Test
public void splitMessageWithEmptyCollectionPayload() throws Exception {
Message<List<String>> message = MessageBuilder.withPayload(Collections.<String>emptyList()).build();
QueueChannel replyChannel = new QueueChannel();
DefaultMessageSplitter splitter = new DefaultMessageSplitter();
splitter.setOutputChannel(replyChannel);
splitter.handleMessage(message);
Message<?> output = replyChannel.receive(15);
assertThat(output, is(nullValue()));
}
}