From 3e0b8a72c8c211873d6d37323ed45d45286d8664 Mon Sep 17 00:00:00 2001 From: dhgarrette Date: Mon, 6 Jul 2009 06:32:38 +0000 Subject: [PATCH] BATCH-1298 & BATCH-1318: Added additional tests for exception classes --- .../item/ExceptionThrowingTaskletStub.java | 4 ++ .../FaultTolerantExceptionClassesTests.java | 70 +++++++++++++++++++ .../core/step/item/SkipProcessorStub.java | 2 +- .../batch/core/step/item/SkipReaderStub.java | 2 +- .../batch/core/step/item/SkipWriterStub.java | 2 +- ...tTolerantExceptionClassesTests-context.xml | 34 ++++++++- 6 files changed, 109 insertions(+), 5 deletions(-) diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/step/item/ExceptionThrowingTaskletStub.java b/spring-batch-core/src/test/java/org/springframework/batch/core/step/item/ExceptionThrowingTaskletStub.java index 77e5f9c87..4c25f0100 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/step/item/ExceptionThrowingTaskletStub.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/step/item/ExceptionThrowingTaskletStub.java @@ -50,6 +50,10 @@ public class ExceptionThrowingTaskletStub implements Tasklet { return committed; } + public void clear() { + committed.clear(); + } + public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception { committed.add(1); throw exception.newInstance("Expected exception"); diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/step/item/FaultTolerantExceptionClassesTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/step/item/FaultTolerantExceptionClassesTests.java index bb5d4aa44..53606be15 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/step/item/FaultTolerantExceptionClassesTests.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/step/item/FaultTolerantExceptionClassesTests.java @@ -189,6 +189,76 @@ public class FaultTolerantExceptionClassesTests { assertEquals("[]", writer.getCommitted().toString()); } + @Test + public void testNoRollbackDefaultRollbackException() throws Exception { + writer.setExceptionType(RuntimeException.class); + StepExecution stepExecution = launchStep("noRollbackDefault"); + assertEquals(BatchStatus.FAILED, stepExecution.getStatus()); + // TODO BATCH-1318: assertEquals("[1, 2, 3]", writer.getWritten().toString()); + // TODO BATCH-1318: assertEquals("[]", writer.getCommitted().toString()); + } + + @Test + public void testNoRollbackDefaultNoRollbackException() throws Exception { + writer.setExceptionType(SkippableRuntimeException.class); + StepExecution stepExecution = launchStep("noRollbackDefault"); + // TODO BATCH-1318: assertEquals(BatchStatus.FAILED, stepExecution.getStatus()); + // TODO BATCH-1318: assertEquals("[1, 2, 3]", writer.getWritten().toString()); + // TODO BATCH-1318: assertEquals("[1, 2, 3]", writer.getCommitted().toString()); + } + + @Test + public void testNoRollbackSkippableRollbackException() throws Exception { + writer.setExceptionType(SkippableRuntimeException.class); + StepExecution stepExecution = launchStep("noRollbackSkippable"); + assertEquals(BatchStatus.COMPLETED, stepExecution.getStatus()); + assertEquals("[1, 2, 3, 1, 2, 3, 4]", writer.getWritten().toString()); + assertEquals("[1, 2, 4]", writer.getCommitted().toString()); + } + + @Test + public void testNoRollbackSkippableNoRollbackException() throws Exception { + writer.setExceptionType(FatalRuntimeException.class); + StepExecution stepExecution = launchStep("noRollbackSkippable"); + assertEquals(BatchStatus.COMPLETED, stepExecution.getStatus()); + assertEquals("[1, 2, 3, 1, 2, 3, 4]", writer.getWritten().toString()); + // TODO BATCH-1318: assertEquals("[1, 2, 3, 1, 2, 3, 4]", writer.getCommitted().toString()); + } + + @Test + public void testNoRollbackFatalRollbackException() throws Exception { + writer.setExceptionType(SkippableRuntimeException.class); + StepExecution stepExecution = launchStep("noRollbackFatal"); + assertEquals(BatchStatus.FAILED, stepExecution.getStatus()); + assertEquals("[1, 2, 3]", writer.getWritten().toString()); + assertEquals("[]", writer.getCommitted().toString()); + } + + @Test + public void testNoRollbackFatalNoRollbackException() throws Exception { + writer.setExceptionType(FatalRuntimeException.class); + StepExecution stepExecution = launchStep("noRollbackFatal"); + assertEquals(BatchStatus.FAILED, stepExecution.getStatus()); + // TODO BATCH-1318: assertEquals("[1, 2, 3]", writer.getWritten().toString()); + // TODO BATCH-1318: assertEquals("[1, 2, 3]", writer.getCommitted().toString()); + } + + @Test + public void testNoRollbackTaskletRollbackException() throws Exception { + tasklet.setExceptionType(FatalRuntimeException.class); + StepExecution stepExecution = launchStep("noRollbackTasklet"); + assertEquals(BatchStatus.FAILED, stepExecution.getStatus()); + assertEquals("[]", tasklet.getCommitted().toString()); + } + + @Test + public void testNoRollbackTaskletNoRollbackException() throws Exception { + tasklet.setExceptionType(SkippableRuntimeException.class); + StepExecution stepExecution = launchStep("noRollbackTasklet"); + assertEquals(BatchStatus.FAILED, stepExecution.getStatus()); + // TODO BATCH-1298: assertEquals("[1]", tasklet.getCommitted().toString()); + } + private StepExecution launchStep(String stepName) throws JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException { SimpleJob job = new SimpleJob(); 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 b2d960c5b..613624de6 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 @@ -57,6 +57,7 @@ public class SkipProcessorStub extends AbstractExceptionThrowingItemHandlerSt public T process(T item) throws Exception { processed.add(item); + committed.add(item); try { checkFailure(item); } @@ -68,7 +69,6 @@ public class SkipProcessorStub extends AbstractExceptionThrowingItemHandlerSt throw e; } } - committed.add(item); 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 9c3bf9257..dd7ea45d3 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 @@ -64,8 +64,8 @@ public class SkipReaderStub extends AbstractExceptionThrowingItemHandlerStub< return null; } T item = items[counter]; - checkFailure(item); read.add(item); + checkFailure(item); return item; } } 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 20aab5b0d..103eef785 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 @@ -52,8 +52,8 @@ public class SkipWriterStub extends AbstractExceptionThrowingItemHandlerStub< logger.debug("Writing: " + items); for (T item : items) { written.add(item); - checkFailure(item); committed.add(item); + checkFailure(item); } } } diff --git a/spring-batch-core/src/test/resources/org/springframework/batch/core/step/item/FaultTolerantExceptionClassesTests-context.xml b/spring-batch-core/src/test/resources/org/springframework/batch/core/step/item/FaultTolerantExceptionClassesTests-context.xml index 497adf05f..38aa5067a 100644 --- a/spring-batch-core/src/test/resources/org/springframework/batch/core/step/item/FaultTolerantExceptionClassesTests-context.xml +++ b/spring-batch-core/src/test/resources/org/springframework/batch/core/step/item/FaultTolerantExceptionClassesTests-context.xml @@ -44,20 +44,50 @@ - + + + + + org.springframework.batch.core.step.item.FatalRuntimeException + + + + org.springframework.batch.core.step.item.SkippableRuntimeException + + + + + org.springframework.batch.core.step.item.SkippableRuntimeException org.springframework.batch.core.step.item.SkippableException + org.springframework.batch.core.step.item.FatalRuntimeException + org.springframework.batch.core.step.item.FatalException + + + + org.springframework.batch.core.step.item.FatalRuntimeException + + + + + + + + + java.lang.Exception + org.springframework.batch.core.step.item.SkippableRuntimeException + org.springframework.batch.core.step.item.SkippableException org.springframework.batch.core.step.item.FatalRuntimeException org.springframework.batch.core.step.item.FatalException - org.springframework.batch.core.step.item.SkippableRuntimeException + org.springframework.batch.core.step.item.FatalRuntimeException