From 77b349e1c11704ab3eae062ef5282cf5b90a59a6 Mon Sep 17 00:00:00 2001 From: dhgarrette Date: Sat, 30 May 2009 16:02:05 +0000 Subject: [PATCH] More clean up of FaultTolerantStepFactoryBean*Tests --- .../ExceptionThrowingItemHandlerStub.java | 25 +-- ...tTolerantStepFactoryBeanRollbackTests.java | 61 +++--- .../FaultTolerantStepFactoryBeanTests.java | 181 +++++++----------- .../core/step/item/SkipProcessorStub.java | 37 ++-- .../batch/core/step/item/SkipReaderStub.java | 22 +-- .../batch/core/step/item/SkipWriterStub.java | 20 +- 6 files changed, 145 insertions(+), 201 deletions(-) diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/step/item/ExceptionThrowingItemHandlerStub.java b/spring-batch-core/src/test/java/org/springframework/batch/core/step/item/ExceptionThrowingItemHandlerStub.java index e52533b5b..e92e9eaaf 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/step/item/ExceptionThrowingItemHandlerStub.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/step/item/ExceptionThrowingItemHandlerStub.java @@ -15,6 +15,7 @@ */ package org.springframework.batch.core.step.item; +import java.util.Arrays; import java.util.Collection; import java.util.Collections; @@ -28,33 +29,25 @@ public abstract class ExceptionThrowingItemHandlerStub { private boolean runtimeException = false; - public ExceptionThrowingItemHandlerStub() { - } - - public ExceptionThrowingItemHandlerStub(Collection failures) { - this.failures = failures; - } - - public ExceptionThrowingItemHandlerStub(Collection failures, boolean runtimeException) { - this(failures); - this.runtimeException = runtimeException; - } - - public void setFailures(Collection failures) { - this.failures = failures; + public void setFailures(T... failures) { + this.failures = Arrays.asList(failures); } public void setRuntimeException(boolean runtimeException) { this.runtimeException = runtimeException; } + public void clear() { + failures.clear(); + } + protected void checkFailure(T item) throws Exception { if (isFailure(item)) { if (runtimeException) { - throw new SkippableRuntimeException("should cause rollback in reader"); + throw new SkippableRuntimeException("Intended Failure"); } else { - throw new SkippableException("shouldn't cause rollback in reader"); + throw new SkippableException("Intended Failure"); } } } diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/step/item/FaultTolerantStepFactoryBeanRollbackTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/step/item/FaultTolerantStepFactoryBeanRollbackTests.java index ec7d02ca1..daa6970df 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/step/item/FaultTolerantStepFactoryBeanRollbackTests.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/step/item/FaultTolerantStepFactoryBeanRollbackTests.java @@ -36,7 +36,9 @@ public class FaultTolerantStepFactoryBeanRollbackTests { private FaultTolerantStepFactoryBean factory; - private SkipReaderStub reader = new SkipReaderStub("1", "2", "3", "4", "5"); + private SkipReaderStub reader = new SkipReaderStub(); + + private SkipProcessorStub processor = new SkipProcessorStub(); private SkipWriterStub writer = new SkipWriterStub(); @@ -53,14 +55,18 @@ public class FaultTolerantStepFactoryBeanRollbackTests { factory.setBeanName("stepName"); factory.setTransactionManager(new ResourcelessTransactionManager()); factory.setCommitInterval(2); + + reader.clear(); + reader.setItems("1", "2", "3", "4", "5"); factory.setItemReader(reader); + processor.clear(); + factory.setItemProcessor(processor); + writer.clear(); factory.setItemWriter(writer); + factory.setSkipLimit(2); - @SuppressWarnings("unchecked") - Collection> skippableExceptions = Arrays - .> asList(Exception.class); - factory.setSkippableExceptionClasses(skippableExceptions); + factory.setSkippableExceptionClasses(getExceptionList(Exception.class)); MapJobRepositoryFactoryBean.clear(); MapJobRepositoryFactoryBean repositoryFactory = new MapJobRepositoryFactoryBean(); @@ -109,8 +115,9 @@ public class FaultTolerantStepFactoryBeanRollbackTests { */ @Test public void testReaderDefaultNoRollbackOnCheckedException() throws Exception { - factory.setItemReader(new SkipReaderStub(new String[] { "1", "2", "3", "4" }, Arrays.asList("2", "3"), - false)); + reader.setItems("1", "2", "3", "4"); + reader.setFailures("2", "3"); + reader.setRuntimeException(false); Step step = (Step) factory.getObject(); @@ -125,8 +132,9 @@ public class FaultTolerantStepFactoryBeanRollbackTests { */ @Test public void testReaderAttributesOverrideSkippableNoRollback() throws Exception { - factory.setItemReader(new SkipReaderStub(new String[] { "1", "2", "3", "4" }, Arrays.asList("2", "3"), - false)); + reader.setFailures("2", "3"); + reader.setItems("1", "2", "3", "4"); + reader.setRuntimeException(false); // No skips by default factory.setSkippableExceptionClasses(new HashSet>()); @@ -147,11 +155,10 @@ public class FaultTolerantStepFactoryBeanRollbackTests { */ @Test public void testProcessorDefaultRollbackOnCheckedException() throws Exception { - SkipProcessorStub processor = new SkipProcessorStub(false, "1", "3"); - factory.setItemProcessor(processor); + reader.setItems("1", "2", "3", "4"); - factory.setItemReader(new SkipReaderStub(new String[] { "1", "2", "3", "4" })); - factory.setItemWriter(new SkipWriterStub()); + processor.setFailures("1", "3"); + processor.setRuntimeException(false); Step step = (Step) factory.getObject(); @@ -166,11 +173,10 @@ public class FaultTolerantStepFactoryBeanRollbackTests { */ @Test public void testProcessorDefaultRollbackOnRuntimeException() throws Exception { - SkipProcessorStub processor = new SkipProcessorStub(true, "1", "3"); - factory.setItemProcessor(processor); + reader.setItems("1", "2", "3", "4"); - factory.setItemReader(new SkipReaderStub(new String[] { "1", "2", "3", "4" })); - factory.setItemWriter(new SkipWriterStub()); + processor.setFailures("1", "3"); + processor.setRuntimeException(true); Step step = (Step) factory.getObject(); @@ -182,12 +188,11 @@ public class FaultTolerantStepFactoryBeanRollbackTests { @Test public void testProcessSkipWithNoRollbackForCheckedException() throws Exception { + processor.setFailures("4"); + processor.setRuntimeException(false); - reader = new SkipReaderStub(new String[] { "1", "2", "3", "4", "5" }); - factory.setItemReader(reader); factory.setNoRollbackExceptionClasses(getExceptionList(SkippableException.class)); - SkipProcessorStub processor = new SkipProcessorStub(false, "4"); - factory.setItemProcessor(processor); + Step step = (Step) factory.getObject(); step.execute(stepExecution); @@ -213,7 +218,8 @@ public class FaultTolerantStepFactoryBeanRollbackTests { */ @Test public void testWriterDefaultRollbackOnCheckedException() throws Exception { - factory.setItemWriter(new SkipWriterStub(false, "2", "3")); + writer.setFailures("2", "3"); + writer.setRuntimeException(false); Step step = (Step) factory.getObject(); @@ -228,7 +234,8 @@ public class FaultTolerantStepFactoryBeanRollbackTests { */ @Test public void testWriterDefaultRollbackOnRuntimeException() throws Exception { - factory.setItemWriter(new SkipWriterStub(true, "2", "3")); + writer.setFailures("2", "3"); + writer.setRuntimeException(true); Step step = (Step) factory.getObject(); @@ -244,7 +251,9 @@ public class FaultTolerantStepFactoryBeanRollbackTests { */ @Test public void testWriterNoRollbackOnRuntimeException() throws Exception { - factory.setItemWriter(new SkipWriterStub(true, "2", "3")); + writer.setFailures("2", "3"); + writer.setRuntimeException(true); + factory.setNoRollbackExceptionClasses(getExceptionList(SkippableRuntimeException.class)); Step step = (Step) factory.getObject(); @@ -263,7 +272,9 @@ public class FaultTolerantStepFactoryBeanRollbackTests { */ @Test public void testWriterNoRollbackOnCheckedException() throws Exception { - factory.setItemWriter(new SkipWriterStub(false, "2", "3")); + writer.setFailures("2", "3"); + writer.setRuntimeException(false); + factory.setNoRollbackExceptionClasses(getExceptionList(SkippableException.class)); Step step = (Step) factory.getObject(); diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/step/item/FaultTolerantStepFactoryBeanTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/step/item/FaultTolerantStepFactoryBeanTests.java index 31cac42a5..d07672549 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/step/item/FaultTolerantStepFactoryBeanTests.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/step/item/FaultTolerantStepFactoryBeanTests.java @@ -34,7 +34,6 @@ import org.springframework.batch.core.repository.JobRepository; import org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean; import org.springframework.batch.core.step.skip.SkipPolicy; import org.springframework.batch.item.ExecutionContext; -import org.springframework.batch.item.ItemProcessor; import org.springframework.batch.item.ItemReader; import org.springframework.batch.item.ItemStream; import org.springframework.batch.item.ItemStreamException; @@ -45,7 +44,6 @@ import org.springframework.batch.item.ParseException; import org.springframework.batch.item.UnexpectedInputException; import org.springframework.batch.item.WriteFailedException; import org.springframework.batch.item.WriterNotOpenException; -import org.springframework.batch.item.support.ListItemReader; import org.springframework.batch.support.transaction.ResourcelessTransactionManager; import org.springframework.beans.factory.FactoryBean; import org.springframework.scheduling.concurrent.ConcurrentTaskExecutor; @@ -61,10 +59,11 @@ public class FaultTolerantStepFactoryBeanTests { private FaultTolerantStepFactoryBean factory; - private SkipReaderStub reader = new SkipReaderStub(new String[] { "1", "2", "3", "4", "5" }, - Collections.singleton("2")); + private SkipReaderStub reader = new SkipReaderStub(); - private SkipWriterStub writer = new SkipWriterStub("4"); + private SkipProcessorStub processor = new SkipProcessorStub();; + + private SkipWriterStub writer = new SkipWriterStub(); private JobExecution jobExecution; @@ -72,10 +71,6 @@ public class FaultTolerantStepFactoryBeanTests { private JobRepository repository; - private List processed = new ArrayList(); - - private int count; - private boolean opened = false; private boolean closed = false; @@ -87,8 +82,15 @@ public class FaultTolerantStepFactoryBeanTests { factory.setBeanName("stepName"); factory.setTransactionManager(new ResourcelessTransactionManager()); factory.setCommitInterval(2); + + reader.clear(); + reader.setItems("1", "2", "3", "4", "5"); factory.setItemReader(reader); + processor.clear(); + factory.setItemProcessor(processor); + writer.clear(); factory.setItemWriter(writer); + factory.setSkipLimit(2); @SuppressWarnings("unchecked") @@ -114,18 +116,12 @@ public class FaultTolerantStepFactoryBeanTests { */ @Test public void testNonSkippableExceptionOnRead() throws Exception { + reader.setFailures("2"); // nothing is skippable Collection> empty = Collections.emptySet(); factory.setSkippableExceptionClasses(empty); - // no exceptions on write - factory.setItemWriter(new ItemWriter() { - public void write(List items) throws Exception { - logger.debug(items); - } - }); - Step step = (Step) factory.getObject(); step.execute(stepExecution); @@ -145,15 +141,8 @@ public class FaultTolerantStepFactoryBeanTests { factory.setCommitInterval(1); // no failures on read - reader = new SkipReaderStub(new String[] { "1", "2", "3", "4", "5" }); - factory.setItemReader(reader); - factory.setItemWriter(new ItemWriter() { - - public void write(List items) throws Exception { - throw new RuntimeException("non-skippable exception"); - } - - }); + reader.setItems("1", "2", "3", "4", "5"); + writer.setFailures("1"); Step step = (Step) factory.getObject(); @@ -161,7 +150,7 @@ public class FaultTolerantStepFactoryBeanTests { assertEquals(BatchStatus.FAILED, stepExecution.getStatus()); assertEquals(1, reader.getRead().size()); assertEquals(ExitStatus.FAILED.getExitCode(), stepExecution.getExitStatus().getExitCode()); - assertTrue(stepExecution.getExitStatus().getExitDescription().contains("non-skippable exception")); + assertTrue(stepExecution.getExitStatus().getExitDescription().contains("Intended Failure")); assertStepExecutionsAreEqual(stepExecution, repository.getLastStepExecution(jobExecution.getJobInstance(), step .getName())); } @@ -171,15 +160,12 @@ public class FaultTolerantStepFactoryBeanTests { */ @Test public void testReadSkip() throws Exception { + reader.setFailures("2"); - writer = new SkipWriterStub(); - factory.setItemWriter(writer); Step step = (Step) factory.getObject(); step.execute(stepExecution); - System.err.println(writer.getWritten()); - assertEquals(1, stepExecution.getSkipCount()); assertEquals(1, stepExecution.getReadSkipCount()); assertEquals(4, stepExecution.getReadCount()); @@ -203,13 +189,9 @@ public class FaultTolerantStepFactoryBeanTests { */ @Test public void testProcessSkip() throws Exception { + processor.setFailures("4"); + writer.setFailures("4"); - reader = new SkipReaderStub(new String[] { "1", "2", "3", "4", "5" }); - factory.setItemReader(reader); - writer = new SkipWriterStub(); - factory.setItemWriter(writer); - SkipProcessorStub processor = new SkipProcessorStub("4"); - factory.setItemProcessor(processor); Step step = (Step) factory.getObject(); step.execute(stepExecution); @@ -234,13 +216,8 @@ public class FaultTolerantStepFactoryBeanTests { @Test public void testProcessFilter() throws Exception { - - reader = new SkipReaderStub(new String[] { "1", "2", "3", "4", "5" }); - factory.setItemReader(reader); - writer = new SkipWriterStub(); - factory.setItemWriter(writer); - FilterProcessorStub processor = new FilterProcessorStub(Arrays.asList(new String[] { "4" })); - factory.setItemProcessor(processor); + processor.setFailures("4"); + processor.setFilter(true); ItemProcessListenerStub listenerStub = new ItemProcessListenerStub(); factory.setListeners(new StepListener[] { listenerStub }); Step step = (Step) factory.getObject(); @@ -271,9 +248,8 @@ public class FaultTolerantStepFactoryBeanTests { */ @Test public void testWriteSkip() throws Exception { + writer.setFailures("4"); - reader = new SkipReaderStub(new String[] { "1", "2", "3", "4", "5" }); - factory.setItemReader(reader); Step step = (Step) factory.getObject(); step.execute(stepExecution); @@ -302,9 +278,10 @@ public class FaultTolerantStepFactoryBeanTests { */ @Test public void testFatalException() throws Exception { - factory.setFatalExceptionClasses(Collections - .> singleton(FatalRuntimeException.class)); - factory.setItemWriter(new SkipWriterStub() { + reader.setFailures("2"); + + factory.setFatalExceptionClasses(getExceptionList(FatalRuntimeException.class)); + factory.setItemWriter(new ItemWriter() { public void write(List items) { throw new FatalRuntimeException("Ouch!"); } @@ -323,6 +300,8 @@ public class FaultTolerantStepFactoryBeanTests { */ @Test public void testSkipOverLimit() throws Exception { + reader.setFailures("2"); + writer.setFailures("4"); factory.setSkipLimit(1); @@ -348,13 +327,13 @@ public class FaultTolerantStepFactoryBeanTests { */ @Test public void testSkipOverLimitOnRead() throws Exception { + reader.setItems(StringUtils.commaDelimitedListToStringArray("1,2,3,4,5,6")); + reader.setFailures(StringUtils.commaDelimitedListToStringArray("2,3,5")); - reader = new SkipReaderStub(StringUtils.commaDelimitedListToStringArray("1,2,3,4,5,6"), Arrays - .asList(StringUtils.commaDelimitedListToStringArray("2,3,5"))); + writer.setFailures("4"); factory.setSkipLimit(3); - factory.setItemReader(reader); - factory.setSkippableExceptionClasses(Collections.> singleton(Exception.class)); + factory.setSkippableExceptionClasses(getExceptionList(Exception.class)); Step step = (Step) factory.getObject(); @@ -381,19 +360,19 @@ public class FaultTolerantStepFactoryBeanTests { */ @Test public void testSkipListenerFailsOnRead() throws Exception { + reader.setItems(StringUtils.commaDelimitedListToStringArray("1,2,3,4,5,6")); + reader.setFailures(StringUtils.commaDelimitedListToStringArray("2,3,5")); - reader = new SkipReaderStub(StringUtils.commaDelimitedListToStringArray("1,2,3,4,5,6"), Arrays - .asList(StringUtils.commaDelimitedListToStringArray("2,3,5"))); + writer.setFailures("4"); factory.setSkipLimit(3); - factory.setItemReader(reader); factory.setListeners(new StepListener[] { new SkipListenerSupport() { @Override public void onSkipInRead(Throwable t) { throw new RuntimeException("oops"); } } }); - factory.setSkippableExceptionClasses(Collections.> singleton(Exception.class)); + factory.setSkippableExceptionClasses(getExceptionList(Exception.class)); Step step = (Step) factory.getObject(); @@ -416,18 +395,18 @@ public class FaultTolerantStepFactoryBeanTests { */ @Test public void testSkipListenerFailsOnWrite() throws Exception { + reader.setItems(StringUtils.commaDelimitedListToStringArray("1,2,3,4,5,6")); - reader = new SkipReaderStub(StringUtils.commaDelimitedListToStringArray("1,2,3,4,5,6")); + writer.setFailures("4"); factory.setSkipLimit(3); - factory.setItemReader(reader); factory.setListeners(new StepListener[] { new SkipListenerSupport() { @Override public void onSkipInWrite(String item, Throwable t) { throw new RuntimeException("oops"); } } }); - factory.setSkippableExceptionClasses(Collections.> singleton(Exception.class)); + factory.setSkippableExceptionClasses(getExceptionList(Exception.class)); Step step = (Step) factory.getObject(); @@ -446,12 +425,12 @@ public class FaultTolerantStepFactoryBeanTests { */ @Test public void testSkipOnReadNotDoubleCounted() throws Exception { + reader.setItems(StringUtils.commaDelimitedListToStringArray("1,2,3,4,5,6")); + reader.setFailures(StringUtils.commaDelimitedListToStringArray("2,3,5")); - reader = new SkipReaderStub(StringUtils.commaDelimitedListToStringArray("1,2,3,4,5,6"), Arrays - .asList(StringUtils.commaDelimitedListToStringArray("2,3,5"))); + writer.setFailures("4"); factory.setSkipLimit(4); - factory.setItemReader(reader); Step step = (Step) factory.getObject(); @@ -476,15 +455,12 @@ public class FaultTolerantStepFactoryBeanTests { */ @Test public void testSkipOnWriteNotDoubleCounted() throws Exception { + reader.setItems(StringUtils.commaDelimitedListToStringArray("1,2,3,4,5,6,7")); + reader.setFailures(StringUtils.commaDelimitedListToStringArray("2,3")); - reader = new SkipReaderStub(StringUtils.commaDelimitedListToStringArray("1,2,3,4,5,6,7"), Arrays - .asList(StringUtils.commaDelimitedListToStringArray("2,3"))); - - writer = new SkipWriterStub("4", "5"); + writer.setFailures("4", "5"); factory.setSkipLimit(4); - factory.setItemReader(reader); - factory.setItemWriter(writer); factory.setCommitInterval(3); // includes all expected skips Step step = (Step) factory.getObject(); @@ -503,27 +479,18 @@ public class FaultTolerantStepFactoryBeanTests { @Test public void testDefaultSkipPolicy() throws Exception { - factory.setSkippableExceptionClasses(Collections.> singleton(Exception.class)); + reader.setItems("a", "b", "c"); + reader.setFailures("b"); + + factory.setSkippableExceptionClasses(getExceptionList(Exception.class)); factory.setSkipLimit(1); - List items = Arrays.asList(new String[] { "a", "b", "c" }); - ItemReader provider = new ListItemReader(items) { - public String read() { - String item = super.read(); - count++; - if ("b".equals(item)) { - throw new RuntimeException("Read error - planned failure."); - } - return item; - } - }; - factory.setItemReader(provider); + Step step = (Step) factory.getObject(); step.execute(stepExecution); assertEquals(1, stepExecution.getSkipCount()); - // b is processed once and skipped, plus 1, plus c, plus the null at end - assertEquals(4, count); + assertEquals("[a, c]", reader.getRead().toString()); assertStepExecutionsAreEqual(stepExecution, repository.getLastStepExecution(jobExecution.getJobInstance(), step .getName())); } @@ -533,15 +500,14 @@ public class FaultTolerantStepFactoryBeanTests { */ @Test public void testSkipOverLimitOnReadWithAllSkipsAtEnd() throws Exception { + reader.setItems(StringUtils.commaDelimitedListToStringArray("1,2,3,4,5,6,7,8,9,10,11,12,13,14,15")); + reader.setFailures(StringUtils.commaDelimitedListToStringArray("6,12,13,14,15")); - reader = new SkipReaderStub(StringUtils - .commaDelimitedListToStringArray("1,2,3,4,5,6,7,8,9,10,11,12,13,14,15"), Arrays.asList(StringUtils - .commaDelimitedListToStringArray("6,12,13,14,15"))); + writer.setFailures("4"); factory.setCommitInterval(5); factory.setSkipLimit(3); - factory.setItemReader(reader); - factory.setSkippableExceptionClasses(Collections.> singleton(Exception.class)); + factory.setSkippableExceptionClasses(getExceptionList(Exception.class)); Step step = (Step) factory.getObject(); @@ -564,13 +530,9 @@ public class FaultTolerantStepFactoryBeanTests { @Test public void testReprocessingAfterWriterRollback() throws Exception { - factory.setItemProcessor(new ItemProcessor() { - public String process(String item) throws Exception { - processed.add(item); - return item; - } - }); - factory.setItemReader(new SkipReaderStub(new String[] { "1", "2", "3", "4" })); + reader.setItems("1", "2", "3", "4"); + + writer.setFailures("4"); Step step = (Step) factory.getObject(); step.execute(stepExecution); @@ -580,15 +542,15 @@ public class FaultTolerantStepFactoryBeanTests { // 1,2,3,4,3,4,4 - two re-processing attempts until the item is // identified and finally skipped on the third attempt - assertEquals(7, processed.size()); - assertEquals("[1, 2, 3, 4, 3, 4, 4]", processed.toString()); + assertEquals(7, processor.getProcessed().size()); + assertEquals("[1, 2, 3, 4, 3, 4, 4]", processor.getProcessed().toString()); assertStepExecutionsAreEqual(stepExecution, repository.getLastStepExecution(jobExecution.getJobInstance(), step .getName())); - } @Test public void testAutoRegisterItemListeners() throws Exception { + reader.setFailures("2"); final List listenerCalls = new ArrayList(); @@ -667,6 +629,7 @@ public class FaultTolerantStepFactoryBeanTests { */ @Test public void testItemStreamOpenedEvenWithTaskExecutor() throws Exception { + writer.setFailures("4"); ItemStreamReader reader = new ItemStreamReader() { public void close() throws ItemStreamException { @@ -702,6 +665,7 @@ public class FaultTolerantStepFactoryBeanTests { */ @Test public void testNestedItemStreamOpened() throws Exception { + writer.setFailures("4"); ItemStreamReader reader = new ItemStreamReader() { public void close() throws ItemStreamException { @@ -753,6 +717,7 @@ public class FaultTolerantStepFactoryBeanTests { @SuppressWarnings("unchecked") @Test public void testProxiedItemStreamOpened() throws Exception { + writer.setFailures("4"); ItemStreamReader reader = new ItemStreamReader() { public void close() throws ItemStreamException { @@ -793,22 +758,6 @@ public class FaultTolerantStepFactoryBeanTests { assertEquals(BatchStatus.COMPLETED, stepExecution.getStatus()); } - private static class FilterProcessorStub implements ItemProcessor { - private final Collection failures; - - public FilterProcessorStub(Collection failures) { - this.failures = failures; - } - - public String process(String item) throws Exception { - if (failures.contains(item)) { - return null; - } - return item; - } - - } - private static class ItemProcessListenerStub implements ItemProcessListener { private boolean errorEncountered = false; @@ -950,4 +899,8 @@ public class FaultTolerantStepFactoryBeanTests { return (SkipPolicy) ReflectionTestUtils.getField(chunkProvider, "skipPolicy"); } + @SuppressWarnings("unchecked") + private Collection> getExceptionList(Class args) { + return Arrays.> asList(args); + } } diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/step/item/SkipProcessorStub.java b/spring-batch-core/src/test/java/org/springframework/batch/core/step/item/SkipProcessorStub.java index fab984191..b86719a18 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/step/item/SkipProcessorStub.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/step/item/SkipProcessorStub.java @@ -16,7 +16,6 @@ package org.springframework.batch.core.step.item; import java.util.ArrayList; -import java.util.Arrays; import java.util.List; import org.springframework.batch.item.ItemProcessor; @@ -32,18 +31,7 @@ public class SkipProcessorStub extends ExceptionThrowingItemHandlerStub im private List committed = TransactionAwareProxyFactory.createTransactionalList(); - public SkipProcessorStub() { - super(); - } - - public SkipProcessorStub(T... failures) { - super(Arrays.asList(failures)); - } - - public SkipProcessorStub(boolean runtimeException, T... failures) { - this(failures); - this.setRuntimeException(runtimeException); - } + private boolean filter = false; public List getProcessed() { return processed; @@ -53,16 +41,31 @@ public class SkipProcessorStub extends ExceptionThrowingItemHandlerStub im return committed; } + public void setFilter(boolean filter) { + this.filter = filter; + } + public void clear() { - processed = new ArrayList(); - committed = TransactionAwareProxyFactory.createTransactionalList(); - this.setFailures(new ArrayList()); + super.clear(); + processed.clear(); + committed.clear(); + filter = false; } public T process(T item) throws Exception { processed.add(item); committed.add(item); - checkFailure(item); + try { + checkFailure(item); + } + catch (Exception e) { + if(filter){ + return null; + } + else{ + throw e; + } + } return item; } } \ No newline at end of file diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/step/item/SkipReaderStub.java b/spring-batch-core/src/test/java/org/springframework/batch/core/step/item/SkipReaderStub.java index 5522104fb..1ed197543 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/step/item/SkipReaderStub.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/step/item/SkipReaderStub.java @@ -16,12 +16,12 @@ package org.springframework.batch.core.step.item; import java.util.ArrayList; -import java.util.Collection; import java.util.List; import org.springframework.batch.item.ItemReader; import org.springframework.batch.item.ParseException; import org.springframework.batch.item.UnexpectedInputException; +import org.springframework.util.Assert; /** * @author Dan Garrette @@ -29,34 +29,32 @@ import org.springframework.batch.item.UnexpectedInputException; */ public class SkipReaderStub extends ExceptionThrowingItemHandlerStub implements ItemReader { - private final T[] items; + private T[] items; private List read = new ArrayList(); private int counter = -1; + public SkipReaderStub() { + } + public SkipReaderStub(T... items) { - super(); this.items = items; } - public SkipReaderStub(T[] items, Collection failures) { - super(failures); + public void setItems(T... items) { + Assert.isTrue(counter < 0, "Items cannot be set once reading has started"); this.items = items; } - public SkipReaderStub(T[] items, Collection failures, boolean runtimeException) { - this(items, failures); - this.setRuntimeException(runtimeException); - } - public List getRead() { return read; } public void clear() { - read = new ArrayList(); - this.setFailures(new ArrayList()); + super.clear(); + counter = -1; + read.clear(); } public T read() throws Exception, UnexpectedInputException, ParseException { diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/step/item/SkipWriterStub.java b/spring-batch-core/src/test/java/org/springframework/batch/core/step/item/SkipWriterStub.java index e706bed79..b59f1c511 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/step/item/SkipWriterStub.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/step/item/SkipWriterStub.java @@ -16,7 +16,6 @@ package org.springframework.batch.core.step.item; import java.util.ArrayList; -import java.util.Arrays; import java.util.List; import org.springframework.batch.item.ItemWriter; @@ -32,19 +31,6 @@ public class SkipWriterStub extends ExceptionThrowingItemHandlerStub imple private List committed = TransactionAwareProxyFactory.createTransactionalList(); - public SkipWriterStub() { - super(); - } - - public SkipWriterStub(T... failures) { - super(Arrays.asList(failures)); - } - - public SkipWriterStub(boolean runtimeException, T... failures) { - this(failures); - this.setRuntimeException(runtimeException); - } - public List getWritten() { return written; } @@ -54,9 +40,9 @@ public class SkipWriterStub extends ExceptionThrowingItemHandlerStub imple } public void clear() { - written = new ArrayList(); - committed = TransactionAwareProxyFactory.createTransactionalList(); - this.setFailures(new ArrayList()); + super.clear(); + written.clear(); + committed.clear(); } public void write(List items) throws Exception {