diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/step/item/FaultTolerantStepFactoryBeanRetryTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/step/item/FaultTolerantStepFactoryBeanRetryTests.java index 21decea49..486168c44 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/step/item/FaultTolerantStepFactoryBeanRetryTests.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/step/item/FaultTolerantStepFactoryBeanRetryTests.java @@ -31,6 +31,7 @@ import org.apache.commons.logging.LogFactory; import org.junit.Before; import org.junit.Test; import org.springframework.batch.core.BatchStatus; +import org.springframework.batch.core.ExitStatus; import org.springframework.batch.core.JobExecution; import org.springframework.batch.core.JobParameters; import org.springframework.batch.core.JobParametersBuilder; @@ -45,6 +46,7 @@ import org.springframework.batch.core.repository.dao.MapStepExecutionDao; import org.springframework.batch.core.repository.support.SimpleJobRepository; import org.springframework.batch.core.step.AbstractStep; 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.ItemWriter; @@ -123,6 +125,135 @@ public class FaultTolerantStepFactoryBeanRetryTests { assertTrue(factory.getObject() instanceof Step); } + @Test + public void testProcessAllItemsWhenErrorInWriterTransformation() throws Exception{ + FaultTolerantStepFactoryBean factory = new FaultTolerantStepFactoryBean(); + factory.setBeanName("step"); + + factory.setItemReader(new ListItemReader(new ArrayList())); + factory.setJobRepository(repository); + factory.setTransactionManager(new ResourcelessTransactionManager()); + @SuppressWarnings("unchecked") + Map, Boolean> exceptionMap = getExceptionMap(Exception.class); + factory.setRetryableExceptionClasses(exceptionMap); + ItemWriter failingWriter = new ItemWriter() { + public void write(List data) throws Exception { + int count = 0; + for (Integer item : data) { + if (count++ == 2) { + throw new Exception("Planned failure in writer"); + } + written.add(item); + } + }}; + + ItemProcessor processor = new ItemProcessor() { + public Integer process(String item) throws Exception { + processed.add(item); + return Integer.parseInt(item); + } + }; + ItemReader reader = new ListItemReader(Arrays.asList("1", "2", "3")); + factory.setCommitInterval(3); + factory.setRetryLimit(3); + factory.setSkippableExceptionClasses(new HashMap, Boolean>()); + factory.setItemReader(reader); + factory.setItemProcessor(processor); + factory.setItemWriter(failingWriter); + Step step = (Step) factory.getObject(); + + StepExecution stepExecution = new StepExecution(step.getName(), jobExecution); + repository.add(stepExecution); + step.execute(stepExecution); +// System.out.println(stepExecution.getWriteCount()); +// System.out.println(processed.size()); +// System.out.println(processed); +// System.out.println(written); + assertEquals((1+3)*3, processed.size()); //(Initial try + retry limit)*item count + } + + @Test + public void testProcessAllItemsWhenErrorInWriter() throws Exception { + final int RETRY_LIMIT = 3; + final List ITEM_LIST = Arrays.asList("a", " b", "c"); + ItemWriter failingWriter = new ItemWriter() { + public void write(List data) throws Exception { + int count = 0; + for (String item : data) { + if (count++ == 2) { + throw new Exception("Planned failure in writer"); + } + written.add(item); + } + } + }; + + ItemProcessor processor = new ItemProcessor() { + public String process(String item) throws Exception { + processed.add(item); + return item; + } + }; + ItemReader reader = new ListItemReader(ITEM_LIST); + factory.setCommitInterval(3); + factory.setRetryLimit(RETRY_LIMIT); + factory.setSkipLimit(1); + @SuppressWarnings("unchecked") + Map, Boolean> exceptionMap = getExceptionMap(Exception.class); + factory.setSkippableExceptionClasses(exceptionMap); + factory.setItemReader(reader); + factory.setItemProcessor(processor); + factory.setItemWriter(failingWriter); + Step step = (Step) factory.getObject(); + + StepExecution stepExecution = new StepExecution(step.getName(), jobExecution); + repository.add(stepExecution); + step.execute(stepExecution); +// System.out.println(processed); + assertEquals(ExitStatus.COMPLETED.getExitCode(), stepExecution.getExitStatus().getExitCode()); + assertEquals((1 + RETRY_LIMIT) * ITEM_LIST.size(), processed.size()); + } + + @Test + public void testNoItemsReprocessedWhenErrorInWriterAndProcessorNotTransactional() throws Exception{ + ItemWriter failingWriter = new ItemWriter() { + public void write(List data) throws Exception { + int count = 0; + for (String item : data) { + if (count++ == 2) { + throw new Exception("Planned failure in writer"); + } + written.add(item); + } + }}; + + ItemProcessor processor = new ItemProcessor() { + public String process(String item) throws Exception { + processed.add(item); + return item; + } + }; + ItemReader reader = new ListItemReader(Arrays.asList("a", "b", "c")); + factory.setProcessorTransactional(false); + factory.setCommitInterval(3); + factory.setRetryLimit(3); + factory.setSkippableExceptionClasses(new HashMap, Boolean>()); + factory.setItemReader(reader); + factory.setItemProcessor(processor); + factory.setItemWriter(failingWriter); + Step step = (Step) factory.getObject(); + + StepExecution stepExecution = new StepExecution(step.getName(), jobExecution); + repository.add(stepExecution); + step.execute(stepExecution); + System.out.println(stepExecution.getWriteCount()); + System.out.println(processed.size()); + System.out.println(processed); + System.out.println(written); + assertEquals(3, processed.size()); //(Initial try + retry limit)*item count + } + + /** * N.B. this doesn't really test retry, since the retry is only on write * failures, but it does test that read errors are re-presented for another diff --git a/spring-batch-parent/pom.xml b/spring-batch-parent/pom.xml index 424fe167c..3ae382078 100644 --- a/spring-batch-parent/pom.xml +++ b/spring-batch-parent/pom.xml @@ -184,6 +184,45 @@ + + + org.eclipse.m2e + lifecycle-mapping + 1.0.0 + + + + + + com.springsource.bundlor + com.springsource.bundlor.maven + [1.0,) + + bundlor + + + + + + + + + org.apache.maven.plugins + maven-antrun-plugin + [1.0,) + + run + + + + + + + + + + maven-site-plugin 2.0