OPEN - BATCH-554: *StepFactoryBeans should have a list of exceptions that do and don't cause rollback.
added txValidExceptionClasses property to SkipLimitStepFactoryBean (passed to ItemSkipPolicyItemHandler#doNotRethrowExceptionClasses)
This commit is contained in:
@@ -124,11 +124,12 @@ public class ItemSkipPolicyItemHandlerTests extends TestCase {
|
||||
handler.handle(contribution);
|
||||
contribution.combineSkipCounts();
|
||||
assertEquals(2, contribution.getSkipCount());
|
||||
// 2 is skipped so 3 was last one processed and now we are at 4, which was previously skipped
|
||||
// 2 is skipped so 3 was last one processed and now we are at 4, which
|
||||
// was previously skipped
|
||||
handler.handle(contribution);
|
||||
assertEquals(null, handler.read(contribution));
|
||||
assertEquals(2, contribution.getSkipCount());
|
||||
|
||||
|
||||
assertEquals(1, TransactionSynchronizationManager.getResourceMap().size());
|
||||
Set removed = (Set) TransactionSynchronizationManager.getResourceMap().values().iterator().next();
|
||||
// one skipped item was detected on read
|
||||
@@ -199,7 +200,7 @@ public class ItemSkipPolicyItemHandlerTests extends TestCase {
|
||||
catch (UnexpectedJobExecutionException e) {
|
||||
// expected
|
||||
String message = e.getMessage();
|
||||
assertTrue("Message does not contain 'capacity': "+message, message.indexOf("capacity")>=0);
|
||||
assertTrue("Message does not contain 'capacity': " + message, message.indexOf("capacity") >= 0);
|
||||
}
|
||||
assertEquals(2, contribution.getSkipCount());
|
||||
// No "4" because it was skipped on write, even though it is mutating
|
||||
@@ -207,6 +208,30 @@ public class ItemSkipPolicyItemHandlerTests extends TestCase {
|
||||
assertEquals(new Holder("5"), handler.read(contribution));
|
||||
}
|
||||
|
||||
/**
|
||||
* Skippable write exceptions are not re-thrown when included in the
|
||||
* {@link ItemSkipPolicyItemHandler#setDoNotRethrowExceptionClasses(Class[])}
|
||||
*/
|
||||
public void testWriteWithSkipAndDoNotRethrow() throws Exception {
|
||||
|
||||
handler.setItemSkipPolicy(new AlwaysSkipItemSkipPolicy());
|
||||
handler.setDoNotRethrowExceptionClasses(new Class[] { SkippableException.class });
|
||||
|
||||
handler.handle(contribution);
|
||||
handler.handle(contribution);
|
||||
contribution.combineSkipCounts();
|
||||
assertEquals(1, contribution.getSkipCount());
|
||||
|
||||
// skippable exception thrown in writer at this point, but it won't be
|
||||
// re-thrown
|
||||
handler.handle(contribution);
|
||||
|
||||
assertEquals(2, contribution.getSkipCount());
|
||||
// No "4" because it was skipped on write, even though it is mutating
|
||||
// its key
|
||||
assertEquals(new Holder("5"), handler.read(contribution));
|
||||
}
|
||||
|
||||
/**
|
||||
* Simple item reader that supports skip functionality.
|
||||
*/
|
||||
|
||||
@@ -34,19 +34,19 @@ import org.springframework.util.StringUtils;
|
||||
*/
|
||||
public class SkipLimitStepFactoryBeanTests extends TestCase {
|
||||
|
||||
SkipLimitStepFactoryBean factory = new SkipLimitStepFactoryBean();
|
||||
private SkipLimitStepFactoryBean factory = new SkipLimitStepFactoryBean();
|
||||
|
||||
Class[] skippableExceptions = new Class[] { SkippableException.class, SkippableRuntimeException.class };
|
||||
private Class[] skippableExceptions = new Class[] { SkippableException.class, SkippableRuntimeException.class };
|
||||
|
||||
final int SKIP_LIMIT = 2;
|
||||
private final int SKIP_LIMIT = 2;
|
||||
|
||||
final int COMMIT_INTERVAL = 2;
|
||||
private final int COMMIT_INTERVAL = 2;
|
||||
|
||||
SkipReaderStub reader = new SkipReaderStub();
|
||||
private SkipReaderStub reader = new SkipReaderStub();
|
||||
|
||||
SkipWriterStub writer = new SkipWriterStub();
|
||||
private SkipWriterStub writer = new SkipWriterStub();
|
||||
|
||||
JobExecution jobExecution;
|
||||
private JobExecution jobExecution;
|
||||
|
||||
protected int count;
|
||||
|
||||
@@ -75,6 +75,9 @@ public class SkipLimitStepFactoryBeanTests extends TestCase {
|
||||
|
||||
assertEquals(2, stepExecution.getSkipCount());
|
||||
|
||||
// only write exception caused rollback
|
||||
assertEquals(1, stepExecution.getRollbackCount().intValue());
|
||||
|
||||
// writer did not skip "2" as it never made it to writer, only "4" did
|
||||
assertTrue(reader.processed.contains("4"));
|
||||
assertFalse(writer.written.contains("4"));
|
||||
@@ -84,6 +87,24 @@ public class SkipLimitStepFactoryBeanTests extends TestCase {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Check skippable write exception does not cause rollback when included on
|
||||
* {@link SkipLimitStepFactoryBean#setTxValidExceptionClasses(Class[])}.
|
||||
*/
|
||||
public void testSkipWithoutRethrow() throws Exception {
|
||||
factory.setTxValidExceptionClasses(new Class[] { SkippableRuntimeException.class });
|
||||
AbstractStep step = (AbstractStep) factory.getObject();
|
||||
|
||||
StepExecution stepExecution = new StepExecution(step.getName(), jobExecution);
|
||||
step.execute(stepExecution);
|
||||
|
||||
assertEquals(2, stepExecution.getSkipCount());
|
||||
|
||||
// no rollbacks
|
||||
assertEquals(0, stepExecution.getRollbackCount().intValue());
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Fatal exception should cause immediate termination regardless of other
|
||||
* skip settings (note the fatal exception is also classified as skippable).
|
||||
@@ -227,7 +248,7 @@ public class SkipLimitStepFactoryBeanTests extends TestCase {
|
||||
}
|
||||
|
||||
public void testDefaultSkipPolicy() throws Exception {
|
||||
factory.setSkippableExceptionClasses(new Class[] {Exception.class});
|
||||
factory.setSkippableExceptionClasses(new Class[] { Exception.class });
|
||||
factory.setSkipLimit(1);
|
||||
List items = TransactionAwareProxyFactory.createTransactionalList();
|
||||
items.addAll(Arrays.asList(new String[] { "a", "b", "c" }));
|
||||
@@ -246,7 +267,7 @@ public class SkipLimitStepFactoryBeanTests extends TestCase {
|
||||
|
||||
StepExecution stepExecution = new StepExecution(step.getName(), jobExecution);
|
||||
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);
|
||||
|
||||
Reference in New Issue
Block a user