RESOLVED - issue BATCH-1354: Infinite loop caused by throwing an Error from the ItemWriter of a skippable step

http://jira.springframework.org/browse/BATCH-1354

Previous change reverted: Error should be fatal, so it has to be added to the exception classifiers in FaultTolerantStepFactoryBean.
This commit is contained in:
dsyer
2009-08-20 16:41:38 +00:00
parent a293b6ff21
commit ecddd61b47
6 changed files with 49 additions and 35 deletions

View File

@@ -34,7 +34,7 @@ public abstract class AbstractExceptionThrowingItemHandlerStub<T> {
private Collection<T> failures = Collections.emptyList();
private Constructor<? extends Exception> exception;
private Constructor<? extends Throwable> exception;
public AbstractExceptionThrowingItemHandlerStub() throws Exception {
exception = SkippableRuntimeException.class.getConstructor(String.class);
@@ -44,8 +44,12 @@ public abstract class AbstractExceptionThrowingItemHandlerStub<T> {
this.failures = new ArrayList<T>(Arrays.asList(failures));
}
public void setExceptionType(Class<? extends Exception> exceptionType) throws Exception {
exception = exceptionType.getConstructor(String.class);
public void setExceptionType(Class<? extends Throwable> exceptionType) throws Exception {
try {
exception = exceptionType.getConstructor(String.class);
} catch (NoSuchMethodException e) {
exception = exceptionType.getConstructor(Object.class);
}
}
public void clearFailures() {
@@ -54,7 +58,14 @@ public abstract class AbstractExceptionThrowingItemHandlerStub<T> {
protected void checkFailure(T item) throws Exception {
if (isFailure(item)) {
throw exception.newInstance("Intended Failure: " + item);
Throwable t = exception.newInstance("Intended Failure: " + item);
if (t instanceof Exception) {
throw (Exception) t;
}
if (t instanceof Error) {
throw (Error) t;
}
throw new IllegalStateException("Unexpected non-Error Throwable");
}
}

View File

@@ -98,7 +98,11 @@ public class FaultTolerantChunkProcessorTests {
assertEquals(1, contribution.getFilterCount());
}
@Test
/**
* An Error pops right back up (no skips, no retry)
* @throws Exception
*/
@Test(expected=AssertionError.class)
public void testWriteSkipOnError() throws Exception {
processor.setWriteSkipPolicy(new AlwaysSkipItemSkipPolicy());
processor.setItemWriter(new ItemWriter<String>() {
@@ -113,19 +117,10 @@ public class FaultTolerantChunkProcessorTests {
processor.process(contribution, inputs);
fail("Expected Error");
}
catch (IllegalStateException e) {
assertEquals("Expected Error!", e.getCause().getMessage());
catch (Error e) {
assertEquals("Expected Error!", e.getMessage());
}
processor.process(contribution, inputs);
try {
processor.process(contribution, inputs);
fail("Expected Error");
}
catch (IllegalStateException e) {
assertEquals("Expected Error!", e.getCause().getMessage());
}
assertEquals(1, contribution.getSkipCount());
assertEquals(1, contribution.getWriteCount());
}
@Test

View File

@@ -236,6 +236,22 @@ public class FaultTolerantStepFactoryBeanRollbackTests {
assertEquals(4, stepExecution.getRollbackCount());
}
/**
* Scenario: Exception in writer that should not cause rollback and scan
*/
@Test
public void testWriterDefaultRollbackOnError() throws Exception {
writer.setFailures("2", "3");
writer.setExceptionType(AssertionError.class);
Step step = (Step) factory.getObject();
step.execute(stepExecution);
assertEquals(BatchStatus.FAILED, stepExecution.getStatus());
assertEquals(0, stepExecution.getSkipCount());
assertEquals(1, stepExecution.getRollbackCount());
}
/**
* Scenario: Exception in writer that should not cause rollback and scan
*/

View File

@@ -26,7 +26,6 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.apache.commons.lang.mutable.MutableInt;
import org.junit.Before;
import org.junit.Test;
import org.springframework.batch.core.BatchStatus;