BATCH-836: added ItemStream interface to CompositeItemWriter

This commit is contained in:
dsyer
2010-09-07 11:34:10 +00:00
parent 45a1982dfe
commit 4c25159040
2 changed files with 75 additions and 5 deletions

View File

@@ -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<T> implements ItemWriter<T>, InitializingBean {
public class CompositeItemWriter<T> implements ItemStreamWriter<T>, InitializingBean {
private List<ItemWriter<? super T>> delegates;
private boolean ignoreItemStream = false;
public void setIgnoreItemStream(boolean ignoreItemStream) {
this.ignoreItemStream = ignoreItemStream;
}
public void write(List<? extends T> item) throws Exception {
for (ItemWriter<? super T> writer : delegates) {
writer.write(item);
@@ -50,4 +60,28 @@ public class CompositeItemWriter<T> implements ItemWriter<T>, InitializingBean {
this.delegates = delegates;
}
public void close() throws ItemStreamException {
for (ItemWriter<? super T> writer : delegates) {
if (!ignoreItemStream && (writer instanceof ItemStream)) {
((ItemStream) writer).close();
}
}
}
public void open(ExecutionContext executionContext) throws ItemStreamException {
for (ItemWriter<? super T> writer : delegates) {
if (!ignoreItemStream && (writer instanceof ItemStream)) {
((ItemStream) writer).open(executionContext);
}
}
}
public void update(ExecutionContext executionContext) throws ItemStreamException {
for (ItemWriter<? super T> writer : delegates) {
if (!ignoreItemStream && (writer instanceof ItemStream)) {
((ItemStream) writer).update(executionContext);
}
}
}
}

View File

@@ -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<Object> itemWriter = new CompositeItemWriter<Object>();
@@ -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<? super Object> writer = createStrictMock(ItemStreamWriter.class);
List<Object> data = Collections.singletonList(new Object());
ExecutionContext executionContext = new ExecutionContext();
if (expectOpen) {
writer.open(executionContext);
expectLastCall().once();
}
writer.write(data);
expectLastCall().once();
replay(writer);
List<ItemWriter<? super Object>> writers = new ArrayList<ItemWriter<? super Object>>();
writers.add(writer);
itemWriter.setDelegates(writers);
if (expectOpen) {
itemWriter.open(executionContext);
}
itemWriter.write(data);
verify(writer);
}
}