diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/support/CompositeItemWriter.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/support/CompositeItemWriter.java index a8bc61fdf..f511b63e7 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/support/CompositeItemWriter.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/support/CompositeItemWriter.java @@ -18,6 +18,10 @@ package org.springframework.batch.item.support; import java.util.List; +import org.springframework.batch.item.ExecutionContext; +import org.springframework.batch.item.ItemStream; +import org.springframework.batch.item.ItemStreamException; +import org.springframework.batch.item.ItemStreamWriter; import org.springframework.batch.item.ItemWriter; import org.springframework.beans.factory.InitializingBean; import org.springframework.util.Assert; @@ -31,10 +35,16 @@ import org.springframework.util.Assert; * @author Robert Kasanicky * @author Dave Syer */ -public class CompositeItemWriter implements ItemWriter, InitializingBean { +public class CompositeItemWriter implements ItemStreamWriter, InitializingBean { private List> delegates; + private boolean ignoreItemStream = false; + + public void setIgnoreItemStream(boolean ignoreItemStream) { + this.ignoreItemStream = ignoreItemStream; + } + public void write(List item) throws Exception { for (ItemWriter writer : delegates) { writer.write(item); @@ -50,4 +60,28 @@ public class CompositeItemWriter implements ItemWriter, InitializingBean { this.delegates = delegates; } + public void close() throws ItemStreamException { + for (ItemWriter writer : delegates) { + if (!ignoreItemStream && (writer instanceof ItemStream)) { + ((ItemStream) writer).close(); + } + } + } + + public void open(ExecutionContext executionContext) throws ItemStreamException { + for (ItemWriter writer : delegates) { + if (!ignoreItemStream && (writer instanceof ItemStream)) { + ((ItemStream) writer).open(executionContext); + } + } + } + + public void update(ExecutionContext executionContext) throws ItemStreamException { + for (ItemWriter writer : delegates) { + if (!ignoreItemStream && (writer instanceof ItemStream)) { + ((ItemStream) writer).update(executionContext); + } + } + } + } diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/support/CompositeItemWriterTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/support/CompositeItemWriterTests.java index 461b6475b..ea0b9c618 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/support/CompositeItemWriterTests.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/support/CompositeItemWriterTests.java @@ -9,8 +9,9 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; -import junit.framework.TestCase; - +import org.junit.Test; +import org.springframework.batch.item.ExecutionContext; +import org.springframework.batch.item.ItemStreamWriter; import org.springframework.batch.item.ItemWriter; /** @@ -18,7 +19,7 @@ import org.springframework.batch.item.ItemWriter; * * @author Robert Kasanicky */ -public class CompositeItemWriterTests extends TestCase { +public class CompositeItemWriterTests { // object under test private CompositeItemWriter itemWriter = new CompositeItemWriter(); @@ -26,7 +27,7 @@ public class CompositeItemWriterTests extends TestCase { /** * Regular usage scenario. All injected processors should be called. */ - + @Test public void testProcess() throws Exception { final int NUMBER_OF_WRITERS = 10; @@ -53,4 +54,39 @@ public class CompositeItemWriterTests extends TestCase { } } + @Test + public void testItemStreamCalled() throws Exception { + doTestItemStream(true); + } + + @Test + public void testItemStreamNotCalled() throws Exception { + doTestItemStream(false); + } + + private void doTestItemStream(boolean expectOpen) throws Exception { + @SuppressWarnings("unchecked") + ItemStreamWriter writer = createStrictMock(ItemStreamWriter.class); + List data = Collections.singletonList(new Object()); + ExecutionContext executionContext = new ExecutionContext(); + if (expectOpen) { + writer.open(executionContext); + expectLastCall().once(); + } + writer.write(data); + expectLastCall().once(); + replay(writer); + + List> writers = new ArrayList>(); + writers.add(writer); + + itemWriter.setDelegates(writers); + if (expectOpen) { + itemWriter.open(executionContext); + } + itemWriter.write(data); + + verify(writer); + } + }