Improve parameterisation of exception handler
This commit is contained in:
@@ -40,10 +40,9 @@ public class LimitCheckingItemSkipPolicyTests {
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
List<Class<?>> skippableExceptions = new ArrayList<Class<?>>();
|
||||
List<Class<? extends Throwable>> skippableExceptions = new ArrayList<Class<? extends Throwable>>();
|
||||
skippableExceptions.add(FlatFileParseException.class);
|
||||
List<Class<?>> fatalExceptions = new ArrayList<Class<?>>();
|
||||
|
||||
List<Class<? extends Throwable>> fatalExceptions = new ArrayList<Class<? extends Throwable>>();
|
||||
failurePolicy = new LimitCheckingItemSkipPolicy(1, skippableExceptions, fatalExceptions);
|
||||
}
|
||||
|
||||
|
||||
@@ -15,9 +15,11 @@
|
||||
*/
|
||||
package org.springframework.batch.core.step.item;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.HashSet;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.springframework.batch.core.step.item.SimpleRetryExceptionHandler;
|
||||
import org.springframework.batch.repeat.RepeatContext;
|
||||
import org.springframework.batch.repeat.context.RepeatContextSupport;
|
||||
import org.springframework.batch.repeat.exception.SimpleLimitExceptionHandler;
|
||||
@@ -37,6 +39,7 @@ public class SimpleRetryExceptionHandlerTests extends TestCase {
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see junit.framework.TestCase#setUp()
|
||||
*/
|
||||
protected void setUp() throws Exception {
|
||||
@@ -45,6 +48,7 @@ public class SimpleRetryExceptionHandlerTests extends TestCase {
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see junit.framework.TestCase#tearDown()
|
||||
*/
|
||||
protected void tearDown() throws Exception {
|
||||
@@ -53,14 +57,20 @@ public class SimpleRetryExceptionHandlerTests extends TestCase {
|
||||
|
||||
/**
|
||||
* Test method for
|
||||
* {@link org.springframework.batch.core.step.item.SimpleRetryExceptionHandler#handleException(org.springframework.batch.repeat.RepeatContext, java.lang.Throwable)}.
|
||||
* {@link org.springframework.batch.core.step.item.SimpleRetryExceptionHandler#handleException(org.springframework.batch.repeat.RepeatContext, java.lang.Throwable)}
|
||||
* .
|
||||
*/
|
||||
public void testRethrowWhenRetryExhausted() throws Throwable {
|
||||
|
||||
RetryPolicy retryPolicy = new NeverRetryPolicy();
|
||||
RuntimeException ex = new RuntimeException("foo");
|
||||
|
||||
SimpleRetryExceptionHandler handler = getHandlerAfterRetry(retryPolicy, ex, new Class[] { Error.class });
|
||||
SimpleRetryExceptionHandler handler = getHandlerAfterRetry(retryPolicy, ex,
|
||||
new HashSet<Class<? extends Throwable>>() {
|
||||
{
|
||||
add(Error.class);
|
||||
}
|
||||
});
|
||||
|
||||
// Then pretend to handle the exception in the parent context...
|
||||
try {
|
||||
@@ -79,14 +89,20 @@ public class SimpleRetryExceptionHandlerTests extends TestCase {
|
||||
|
||||
/**
|
||||
* Test method for
|
||||
* {@link org.springframework.batch.core.step.item.SimpleRetryExceptionHandler#handleException(org.springframework.batch.repeat.RepeatContext, java.lang.Throwable)}.
|
||||
* {@link org.springframework.batch.core.step.item.SimpleRetryExceptionHandler#handleException(org.springframework.batch.repeat.RepeatContext, java.lang.Throwable)}
|
||||
* .
|
||||
*/
|
||||
public void testNoRethrowWhenRetryNotExhausted() throws Throwable {
|
||||
|
||||
RetryPolicy retryPolicy = new AlwaysRetryPolicy();
|
||||
RuntimeException ex = new RuntimeException("foo");
|
||||
|
||||
SimpleRetryExceptionHandler handler = getHandlerAfterRetry(retryPolicy, ex, new Class[] { Error.class });
|
||||
SimpleRetryExceptionHandler handler = getHandlerAfterRetry(retryPolicy, ex,
|
||||
new HashSet<Class<? extends Throwable>>() {
|
||||
{
|
||||
add(Error.class);
|
||||
}
|
||||
});
|
||||
|
||||
// Then pretend to handle the exception in the parent context...
|
||||
handler.handleException(context.getParent(), ex);
|
||||
@@ -97,14 +113,20 @@ public class SimpleRetryExceptionHandlerTests extends TestCase {
|
||||
|
||||
/**
|
||||
* Test method for
|
||||
* {@link org.springframework.batch.core.step.item.SimpleRetryExceptionHandler#handleException(org.springframework.batch.repeat.RepeatContext, java.lang.Throwable)}.
|
||||
* {@link org.springframework.batch.core.step.item.SimpleRetryExceptionHandler#handleException(org.springframework.batch.repeat.RepeatContext, java.lang.Throwable)}
|
||||
* .
|
||||
*/
|
||||
public void testRethrowWhenFatal() throws Throwable {
|
||||
|
||||
RetryPolicy retryPolicy = new AlwaysRetryPolicy();
|
||||
RuntimeException ex = new RuntimeException("foo");
|
||||
|
||||
SimpleRetryExceptionHandler handler = getHandlerAfterRetry(retryPolicy, ex, new Class[] { RuntimeException.class });
|
||||
SimpleRetryExceptionHandler handler = getHandlerAfterRetry(retryPolicy, ex,
|
||||
new HashSet<Class<? extends Throwable>>() {
|
||||
{
|
||||
add(RuntimeException.class);
|
||||
}
|
||||
});
|
||||
|
||||
// Then pretend to handle the exception in the parent context...
|
||||
try {
|
||||
@@ -125,7 +147,8 @@ public class SimpleRetryExceptionHandlerTests extends TestCase {
|
||||
* @param ex
|
||||
* @return
|
||||
*/
|
||||
private SimpleRetryExceptionHandler getHandlerAfterRetry(RetryPolicy retryPolicy, RuntimeException ex, Class<?>[] fatalExceptions) {
|
||||
private SimpleRetryExceptionHandler getHandlerAfterRetry(RetryPolicy retryPolicy, RuntimeException ex,
|
||||
Collection<Class<? extends Throwable>> fatalExceptions) {
|
||||
|
||||
// Always rethrow if the retry is exhausted
|
||||
SimpleRetryExceptionHandler handler = new SimpleRetryExceptionHandler(retryPolicy,
|
||||
|
||||
@@ -9,6 +9,7 @@ import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
@@ -45,7 +46,12 @@ public class SkipLimitStepFactoryBeanTests {
|
||||
|
||||
private SkipLimitStepFactoryBean<String, String> factory = new SkipLimitStepFactoryBean<String, String>();
|
||||
|
||||
private Class<?>[] skippableExceptions = new Class[] { SkippableException.class, SkippableRuntimeException.class };
|
||||
private Collection<Class<? extends Throwable>> skippableExceptions = new HashSet<Class<? extends Throwable>>() {
|
||||
{
|
||||
add(SkippableException.class);
|
||||
add(SkippableRuntimeException.class);
|
||||
}
|
||||
};
|
||||
|
||||
private SkipReaderStub reader = new SkipReaderStub();
|
||||
|
||||
@@ -133,7 +139,11 @@ public class SkipLimitStepFactoryBeanTests {
|
||||
*/
|
||||
@Test
|
||||
public void testFatalException() throws Exception {
|
||||
factory.setFatalExceptionClasses(new Class[] { FatalRuntimeException.class });
|
||||
factory.setFatalExceptionClasses(new HashSet<Class<? extends Throwable>>() {
|
||||
{
|
||||
add(FatalRuntimeException.class);
|
||||
}
|
||||
});
|
||||
factory.setItemWriter(new SkipWriterStub() {
|
||||
public void write(List<? extends String> items) {
|
||||
throw new FatalRuntimeException("Ouch!");
|
||||
@@ -194,7 +204,11 @@ public class SkipLimitStepFactoryBeanTests {
|
||||
|
||||
factory.setSkipLimit(3);
|
||||
factory.setItemReader(reader);
|
||||
factory.setSkippableExceptionClasses(new Class[] { Exception.class });
|
||||
factory.setSkippableExceptionClasses(new HashSet<Class<? extends Throwable>>() {
|
||||
{
|
||||
add(Exception.class);
|
||||
}
|
||||
});
|
||||
|
||||
Step step = (Step) factory.getObject();
|
||||
|
||||
@@ -238,7 +252,11 @@ public class SkipLimitStepFactoryBeanTests {
|
||||
throw new RuntimeException("oops");
|
||||
}
|
||||
} });
|
||||
factory.setSkippableExceptionClasses(new Class[] { Exception.class });
|
||||
factory.setSkippableExceptionClasses(new HashSet<Class<? extends Throwable>>() {
|
||||
{
|
||||
add(Exception.class);
|
||||
}
|
||||
});
|
||||
|
||||
Step step = (Step) factory.getObject();
|
||||
|
||||
@@ -275,7 +293,11 @@ public class SkipLimitStepFactoryBeanTests {
|
||||
throw new RuntimeException("oops");
|
||||
}
|
||||
} });
|
||||
factory.setSkippableExceptionClasses(new Class[] { Exception.class });
|
||||
factory.setSkippableExceptionClasses(new HashSet<Class<? extends Throwable>>() {
|
||||
{
|
||||
add(Exception.class);
|
||||
}
|
||||
});
|
||||
|
||||
Step step = (Step) factory.getObject();
|
||||
|
||||
@@ -311,15 +333,16 @@ public class SkipLimitStepFactoryBeanTests {
|
||||
|
||||
StepExecution stepExecution = jobExecution.createStepExecution(step);
|
||||
|
||||
// TODO: uncomment this!
|
||||
// step.execute(stepExecution);
|
||||
// assertEquals(4, stepExecution.getSkipCount());
|
||||
// assertEquals(3, stepExecution.getReadSkipCount());
|
||||
// assertEquals(1, stepExecution.getWriteSkipCount());
|
||||
//
|
||||
// // skipped 2,3,4,5
|
||||
// List<String> expectedOutput = Arrays.asList(StringUtils.commaDelimitedListToStringArray("1,6"));
|
||||
// assertEquals(expectedOutput, writer.written);
|
||||
// TODO: uncomment this!
|
||||
// step.execute(stepExecution);
|
||||
// assertEquals(4, stepExecution.getSkipCount());
|
||||
// assertEquals(3, stepExecution.getReadSkipCount());
|
||||
// assertEquals(1, stepExecution.getWriteSkipCount());
|
||||
//
|
||||
// // skipped 2,3,4,5
|
||||
// List<String> expectedOutput =
|
||||
// Arrays.asList(StringUtils.commaDelimitedListToStringArray("1,6"));
|
||||
// assertEquals(expectedOutput, writer.written);
|
||||
|
||||
}
|
||||
|
||||
@@ -343,21 +366,26 @@ public class SkipLimitStepFactoryBeanTests {
|
||||
|
||||
StepExecution stepExecution = jobExecution.createStepExecution(step);
|
||||
|
||||
// TODO: uncomment this!
|
||||
// step.execute(stepExecution);
|
||||
// assertEquals(4, stepExecution.getSkipCount());
|
||||
// assertEquals(2, stepExecution.getReadSkipCount());
|
||||
// assertEquals(2, stepExecution.getWriteSkipCount());
|
||||
//
|
||||
// // skipped 2,3,4,5
|
||||
// List<String> expectedOutput = Arrays.asList(StringUtils.commaDelimitedListToStringArray("1,6,7"));
|
||||
// assertEquals(expectedOutput, writer.written);
|
||||
// TODO: uncomment this!
|
||||
// step.execute(stepExecution);
|
||||
// assertEquals(4, stepExecution.getSkipCount());
|
||||
// assertEquals(2, stepExecution.getReadSkipCount());
|
||||
// assertEquals(2, stepExecution.getWriteSkipCount());
|
||||
//
|
||||
// // skipped 2,3,4,5
|
||||
// List<String> expectedOutput =
|
||||
// Arrays.asList(StringUtils.commaDelimitedListToStringArray("1,6,7"));
|
||||
// assertEquals(expectedOutput, writer.written);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDefaultSkipPolicy() throws Exception {
|
||||
factory.setSkippableExceptionClasses(new Class[] { Exception.class });
|
||||
factory.setSkippableExceptionClasses(new HashSet<Class<? extends Throwable>>() {
|
||||
{
|
||||
add(Exception.class);
|
||||
}
|
||||
});
|
||||
factory.setSkipLimit(1);
|
||||
List<String> items = Arrays.asList(new String[] { "a", "b", "c" });
|
||||
ItemReader<String> provider = new ListItemReader<String>(items) {
|
||||
|
||||
@@ -22,6 +22,7 @@ import static org.junit.Assert.fail;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Date;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
@@ -100,7 +101,11 @@ public class StatefulRetryStepFactoryBeanTests {
|
||||
factory.setItemWriter(processor);
|
||||
factory.setJobRepository(repository);
|
||||
factory.setTransactionManager(new ResourcelessTransactionManager());
|
||||
factory.setRetryableExceptionClasses(new Class[] { Exception.class });
|
||||
factory.setRetryableExceptionClasses(new HashSet<Class<? extends Throwable>>() {
|
||||
{
|
||||
add(Exception.class);
|
||||
}
|
||||
});
|
||||
factory.setCommitInterval(1); // trivial by default
|
||||
|
||||
JobSupport job = new JobSupport("jobName");
|
||||
@@ -146,7 +151,7 @@ public class StatefulRetryStepFactoryBeanTests {
|
||||
};
|
||||
factory.setItemReader(provider);
|
||||
factory.setRetryLimit(10);
|
||||
factory.setSkippableExceptionClasses(new Class[0]);
|
||||
factory.setSkippableExceptionClasses(new HashSet<Class<? extends Throwable>>());
|
||||
Step step = (Step) factory.getObject();
|
||||
|
||||
StepExecution stepExecution = new StepExecution(step.getName(), jobExecution);
|
||||
@@ -166,7 +171,11 @@ public class StatefulRetryStepFactoryBeanTests {
|
||||
|
||||
@Test
|
||||
public void testSkipAndRetry() throws Exception {
|
||||
factory.setSkippableExceptionClasses(new Class[] { Exception.class });
|
||||
factory.setSkippableExceptionClasses(new HashSet<Class<? extends Throwable>>() {
|
||||
{
|
||||
add(Exception.class);
|
||||
}
|
||||
});
|
||||
factory.setSkipLimit(2);
|
||||
List<String> items = Arrays.asList(new String[] { "a", "b", "c", "d", "e", "f" });
|
||||
ItemReader<String> provider = new ListItemReader<String>(items) {
|
||||
@@ -195,7 +204,11 @@ public class StatefulRetryStepFactoryBeanTests {
|
||||
@Test
|
||||
public void testSkipAndRetryWithWriteFailure() throws Exception {
|
||||
|
||||
factory.setSkippableExceptionClasses(new Class[] { RetryException.class });
|
||||
factory.setSkippableExceptionClasses(new HashSet<Class<? extends Throwable>>() {
|
||||
{
|
||||
add(RetryException.class);
|
||||
}
|
||||
});
|
||||
factory.setListeners(new StepListener[] { new SkipListenerSupport() {
|
||||
public void onSkipInWrite(Object item, Throwable t) {
|
||||
recovered.add(item);
|
||||
@@ -226,7 +239,11 @@ public class StatefulRetryStepFactoryBeanTests {
|
||||
factory.setItemReader(provider);
|
||||
factory.setItemWriter(itemWriter);
|
||||
factory.setRetryLimit(5);
|
||||
factory.setRetryableExceptionClasses(new Class[] { RuntimeException.class });
|
||||
factory.setRetryableExceptionClasses(new HashSet<Class<? extends Throwable>>() {
|
||||
{
|
||||
add(RuntimeException.class);
|
||||
}
|
||||
});
|
||||
AbstractStep step = (AbstractStep) factory.getObject();
|
||||
step.setName("mytest");
|
||||
StepExecution stepExecution = new StepExecution(step.getName(), jobExecution);
|
||||
@@ -248,7 +265,11 @@ public class StatefulRetryStepFactoryBeanTests {
|
||||
public void testSkipAndRetryWithWriteFailureAndNonTrivialCommitInterval() throws Exception {
|
||||
|
||||
factory.setCommitInterval(3);
|
||||
factory.setSkippableExceptionClasses(new Class[] { RetryException.class });
|
||||
factory.setSkippableExceptionClasses(new HashSet<Class<? extends Throwable>>() {
|
||||
{
|
||||
add(RetryException.class);
|
||||
}
|
||||
});
|
||||
factory.setListeners(new StepListener[] { new SkipListenerSupport() {
|
||||
public void onSkipInWrite(Object item, Throwable t) {
|
||||
recovered.add(item);
|
||||
@@ -279,7 +300,11 @@ public class StatefulRetryStepFactoryBeanTests {
|
||||
factory.setItemReader(provider);
|
||||
factory.setItemWriter(itemWriter);
|
||||
factory.setRetryLimit(5);
|
||||
factory.setRetryableExceptionClasses(new Class[] { RuntimeException.class });
|
||||
factory.setRetryableExceptionClasses(new HashSet<Class<? extends Throwable>>() {
|
||||
{
|
||||
add(RuntimeException.class);
|
||||
}
|
||||
});
|
||||
AbstractStep step = (AbstractStep) factory.getObject();
|
||||
step.setName("mytest");
|
||||
StepExecution stepExecution = new StepExecution(step.getName(), jobExecution);
|
||||
@@ -300,7 +325,11 @@ public class StatefulRetryStepFactoryBeanTests {
|
||||
|
||||
@Test
|
||||
public void testRetryWithNoSkip() throws Exception {
|
||||
factory.setRetryableExceptionClasses(new Class[] { Exception.class });
|
||||
factory.setRetryableExceptionClasses(new HashSet<Class<? extends Throwable>>() {
|
||||
{
|
||||
add(Exception.class);
|
||||
}
|
||||
});
|
||||
factory.setRetryLimit(4);
|
||||
factory.setSkipLimit(0);
|
||||
List<String> items = Arrays.asList(new String[] { "b" });
|
||||
@@ -346,9 +375,13 @@ public class StatefulRetryStepFactoryBeanTests {
|
||||
public void testNonSkippableException() throws Exception {
|
||||
|
||||
// Very specific skippable exception
|
||||
factory.setSkippableExceptionClasses(new Class[] { UnsupportedOperationException.class });
|
||||
factory.setSkippableExceptionClasses(new HashSet<Class<? extends Throwable>>() {
|
||||
{
|
||||
add(UnsupportedOperationException.class);
|
||||
}
|
||||
});
|
||||
// ...which is not retryable...
|
||||
factory.setRetryableExceptionClasses(new Class<?>[0]);
|
||||
factory.setRetryableExceptionClasses(new HashSet<Class<? extends Throwable>>());
|
||||
|
||||
factory.setSkipLimit(1);
|
||||
List<String> items = Arrays.asList(new String[] { "b" });
|
||||
|
||||
Reference in New Issue
Block a user