Improve parameterisation of exception handler

This commit is contained in:
dsyer
2008-08-23 13:22:35 +00:00
parent c3ed34b3a8
commit 8e498f0d52
21 changed files with 731 additions and 655 deletions

View File

@@ -55,7 +55,7 @@ public class RethrowOnThresholdExceptionHandlerTests extends TestCase {
return "RuntimeException";
}
});
handler.setThresholds(Collections.singletonMap((Object)"RuntimeException", new Integer(1)));
handler.setThresholds(Collections.singletonMap("RuntimeException", new Integer(1)));
// No exception...
handler.handleException(context, new RuntimeException("Foo"));
RepeatContextCounter counter = new RepeatContextCounter(context, RethrowOnThresholdExceptionHandler.class.getName() + ".RuntimeException");
@@ -69,7 +69,7 @@ public class RethrowOnThresholdExceptionHandlerTests extends TestCase {
return "RuntimeException";
}
});
handler.setThresholds(Collections.singletonMap((Object)"RuntimeException", new Integer(2)));
handler.setThresholds(Collections.singletonMap("RuntimeException", new Integer(2)));
// No exception...
handler.handleException(context, new RuntimeException("Foo"));
handler.handleException(context, new RuntimeException("Foo"));
@@ -88,7 +88,7 @@ public class RethrowOnThresholdExceptionHandlerTests extends TestCase {
return "RuntimeException";
}
});
handler.setThresholds(Collections.singletonMap((Object)"RuntimeException", new Integer(1)));
handler.setThresholds(Collections.singletonMap("RuntimeException", new Integer(1)));
// No exception...
handler.handleException(context, new RuntimeException("Foo"));
context = new RepeatContextSupport(parent);
@@ -107,7 +107,7 @@ public class RethrowOnThresholdExceptionHandlerTests extends TestCase {
return "RuntimeException";
}
});
handler.setThresholds(Collections.singletonMap((Object)"RuntimeException", new Integer(1)));
handler.setThresholds(Collections.singletonMap("RuntimeException", new Integer(1)));
handler.setUseParent(true);
// No exception...
handler.handleException(context, new RuntimeException("Foo"));
@@ -120,16 +120,5 @@ public class RethrowOnThresholdExceptionHandlerTests extends TestCase {
assertEquals("Foo", e.getMessage());
}
}
public void testNotStringAsKey() throws Exception {
try {
handler.setThresholds(Collections.singletonMap((Object)RuntimeException.class, new Integer(1)));
// It's not an error, but not advised...
}
catch (RuntimeException e) {
throw e;
}
}
}

View File

@@ -16,35 +16,30 @@
package org.springframework.batch.retry.policy;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import junit.framework.TestCase;
import org.springframework.batch.retry.RetryContext;
public class SimpleRetryPolicyTests extends TestCase {
public void testSetInvalidExceptionClass() throws Exception {
try {
new SimpleRetryPolicy().setRetryableExceptionClasses(new Class[] { String.class });
fail("Should only be able to set Exception classes.");
}
catch (IllegalArgumentException ex) {
}
}
public void testCanRetryIfNoException() throws Exception {
SimpleRetryPolicy policy = new SimpleRetryPolicy();
RetryContext context = policy.open(null, null);
assertTrue(policy.canRetry(context));
}
@SuppressWarnings("unchecked")
public void testEmptyExceptionsNeverRetry() throws Exception {
SimpleRetryPolicy policy = new SimpleRetryPolicy();
RetryContext context = policy.open(null, null);
// We can't retry any exceptions...
policy.setRetryableExceptionClasses(new Class[0]);
policy.setRetryableExceptionClasses(Collections.EMPTY_SET);
// ...so we can't retry this one...
policy.registerThrowable(context, new IllegalStateException());
@@ -92,14 +87,24 @@ public class SimpleRetryPolicyTests extends TestCase {
public void testFatalOverridesRetryable() throws Exception {
SimpleRetryPolicy policy = new SimpleRetryPolicy();
policy.setFatalExceptionClasses(new Class[] {Exception.class});
policy.setRetryableExceptionClasses(new Class[] {RuntimeException.class});
policy.setFatalExceptionClasses(getClasses(Exception.class));
policy.setRetryableExceptionClasses(getClasses(RuntimeException.class));
RetryContext context = policy.open(null, null);
assertNotNull(context);
policy.registerThrowable(context, new RuntimeException("foo"));
assertFalse(policy.canRetry(context));
}
/**
* @param cls
* @return
*/
private Collection<Class<? extends Throwable>> getClasses(Class<? extends Throwable> cls) {
Collection<Class<? extends Throwable>> classes = new HashSet<Class<? extends Throwable>>();
classes.add(cls);
return classes;
}
public void testParent() throws Exception {
SimpleRetryPolicy policy = new SimpleRetryPolicy();
RetryContext context = policy.open(null, null);

View File

@@ -16,6 +16,8 @@
package org.springframework.batch.retry.support;
import java.util.HashSet;
import junit.framework.TestCase;
import org.springframework.batch.retry.ExhaustedRetryException;
@@ -96,7 +98,11 @@ public class RetryTemplateTests extends TestCase {
RetryTemplate template = new RetryTemplate();
SimpleRetryPolicy policy = new SimpleRetryPolicy();
template.setRetryPolicy(policy);
policy.setRetryableExceptionClasses(new Class[] { RuntimeException.class });
policy.setRetryableExceptionClasses(new HashSet<Class<? extends Throwable>>() {
{
add(RuntimeException.class);
}
});
int attempts = 3;

View File

@@ -16,7 +16,7 @@
package org.springframework.batch.support;
import org.springframework.batch.support.BinaryExceptionClassifier;
import java.util.HashSet;
import junit.framework.TestCase;
@@ -33,8 +33,11 @@ public class BinaryExceptionClassifierTests extends TestCase {
}
public void testClassifyExactMatch() {
classifier.setExceptionClasses(new Class[] {IllegalStateException.class});
classifier.setExceptionClasses(new HashSet<Class<? extends Throwable>>() {
{
add(IllegalStateException.class);
}
});
assertEquals(false, classifier.isDefault(new IllegalStateException("Foo")));
}
}

View File

@@ -33,28 +33,28 @@ public class SubclassExceptionClassifierTests extends TestCase {
}
public void testClassifyExactMatch() {
classifier.setTypeMap(new LinkedHashMap<Class<?>, String>() {{
classifier.setTypeMap(new LinkedHashMap<Class<? extends Throwable>, String>() {{
put(IllegalStateException.class, "foo");
}});
assertEquals("foo", classifier.classify(new IllegalStateException("Foo")));
}
public void testClassifySubclassMatch() {
classifier.setTypeMap(new LinkedHashMap<Class<?>, String>() {{
classifier.setTypeMap(new LinkedHashMap<Class<? extends Throwable>, String>() {{
put(RuntimeException.class, "foo");
}});
assertEquals("foo", classifier.classify(new IllegalStateException("Foo")));
}
public void testClassifySuperclassDoesNotMatch() {
classifier.setTypeMap(new LinkedHashMap<Class<?>, String>() {{
classifier.setTypeMap(new LinkedHashMap<Class<? extends Throwable>, String>() {{
put(IllegalStateException.class, "foo");
}});
assertEquals(classifier.getDefault(), classifier.classify(new RuntimeException("Foo")));
}
public void testClassifyAncestorMatch() {
classifier.setTypeMap(new LinkedHashMap<Class<?>, String>() {{
classifier.setTypeMap(new LinkedHashMap<Class<? extends Throwable>, String>() {{
put(Exception.class, "bar");
put(IllegalArgumentException.class, "foo");
put(RuntimeException.class, "bucket");