OPEN - issue BATCH-777: Parametrise RetryCallback and related interfaces

Downgrade throws clause in RetryCallback - it's not reasonable to retry arbitrary Throwable
This commit is contained in:
dsyer
2008-08-24 11:44:50 +00:00
parent edd3f47be3
commit 05015c598d
16 changed files with 93 additions and 182 deletions

View File

@@ -57,7 +57,7 @@ public class RecoveryRetryCallbackTests extends TestCase {
}
};
callback = new RecoveryRetryCallback("foo", new RetryCallback() {
public Object doWithRetry(RetryContext context) throws Throwable {
public Object doWithRetry(RetryContext context) throws Exception {
count++;
return null;
}
@@ -73,7 +73,7 @@ public class RecoveryRetryCallbackTests extends TestCase {
// We can use the policy to intercept the context and do something with
// the item...
callback = new RecoveryRetryCallback("bar", new RetryCallback() {
public Object doWithRetry(RetryContext context) throws Throwable {
public Object doWithRetry(RetryContext context) throws Exception {
count++;
throw new IllegalStateException("Detected bar");
}
@@ -109,7 +109,7 @@ public class RecoveryRetryCallbackTests extends TestCase {
// We can use the policy to intercept the context and do something with
// the item...
callback = new RecoveryRetryCallback("bar", new RetryCallback() {
public Object doWithRetry(RetryContext context) throws Throwable {
public Object doWithRetry(RetryContext context) throws Exception {
count++;
throw new IllegalStateException("Detected bar");
}

View File

@@ -51,7 +51,7 @@ public class RetryListenerTests extends TestCase {
}
} });
template.execute(new RetryCallback() {
public Object doWithRetry(RetryContext context) throws Throwable {
public Object doWithRetry(RetryContext context) throws Exception {
return null;
}
});
@@ -69,7 +69,7 @@ public class RetryListenerTests extends TestCase {
});
try {
template.execute(new RetryCallback() {
public Object doWithRetry(RetryContext context) throws Throwable {
public Object doWithRetry(RetryContext context) throws Exception {
count++;
return null;
}
@@ -97,7 +97,7 @@ public class RetryListenerTests extends TestCase {
}
} });
template.execute(new RetryCallback() {
public Object doWithRetry(RetryContext context) throws Throwable {
public Object doWithRetry(RetryContext context) throws Exception {
return null;
}
});
@@ -120,7 +120,7 @@ public class RetryListenerTests extends TestCase {
} });
try {
template.execute(new RetryCallback() {
public Object doWithRetry(RetryContext context) throws Throwable {
public Object doWithRetry(RetryContext context) throws Exception {
count++;
throw new IllegalStateException("foo");
}
@@ -147,7 +147,7 @@ public class RetryListenerTests extends TestCase {
}
});
template.execute(new RetryCallback() {
public Object doWithRetry(RetryContext context) throws Throwable {
public Object doWithRetry(RetryContext context) throws Exception {
if (count++ < 1)
throw new RuntimeException("Retry!");
return null;

View File

@@ -121,7 +121,7 @@ public class ExternalRetryIntergrationTests {
private final class MockRetryCallback implements RetryCallback {
int attempts = 0;
public Object doWithRetry(RetryContext context) throws Throwable {
public Object doWithRetry(RetryContext context) throws Exception {
attempts++;
if (attempts < 2) {
throw new RuntimeException();

View File

@@ -43,7 +43,7 @@ public class RecoveryRetryPolicyTests extends TestCase {
final StringHolder item = new StringHolder("foo");
RetryCallback writer = new RetryCallback() {
public Object doWithRetry(RetryContext context) throws Throwable {
public Object doWithRetry(RetryContext context) throws Exception {
count++;
list.add(item.string);
return item;
@@ -59,7 +59,7 @@ public class RecoveryRetryPolicyTests extends TestCase {
public void testOpenWithWrongCallbackType() {
try {
policy.open(new RetryCallback() {
public Object doWithRetry(RetryContext context) throws Throwable {
public Object doWithRetry(RetryContext context) throws Exception {
return null;
}
}, null);
@@ -74,7 +74,7 @@ public class RecoveryRetryPolicyTests extends TestCase {
policy.setDelegate(new AlwaysRetryPolicy());
RetryContext context = policy.open(new RecoveryRetryCallback("foo", new RetryCallback() {
public Object doWithRetry(RetryContext context) throws Throwable {
public Object doWithRetry(RetryContext context) throws Exception {
count++;
return null;
}
@@ -88,7 +88,7 @@ public class RecoveryRetryPolicyTests extends TestCase {
public void testRegisterThrowable() {
policy.setDelegate(new NeverRetryPolicy());
RetryContext context = policy.open(new RecoveryRetryCallback("foo", new RetryCallback() {
public Object doWithRetry(RetryContext context) throws Throwable {
public Object doWithRetry(RetryContext context) throws Exception {
count++;
return null;
}
@@ -101,7 +101,7 @@ public class RecoveryRetryPolicyTests extends TestCase {
public void testClose() throws Exception {
policy.setDelegate(new NeverRetryPolicy());
RetryContext context = policy.open(new RecoveryRetryCallback("foo", new RetryCallback() {
public Object doWithRetry(RetryContext context) throws Throwable {
public Object doWithRetry(RetryContext context) throws Exception {
count++;
return null;
}
@@ -117,7 +117,7 @@ public class RecoveryRetryPolicyTests extends TestCase {
public void testOpenTwice() throws Exception {
RecoveryRetryCallback callback = new RecoveryRetryCallback("foo", new RetryCallback() {
public Object doWithRetry(RetryContext context) throws Throwable {
public Object doWithRetry(RetryContext context) throws Exception {
count++;
return null;
}
@@ -145,7 +145,7 @@ public class RecoveryRetryPolicyTests extends TestCase {
policy.setDelegate(new SimpleRetryPolicy(1));
final String input = "foo";
RecoveryRetryCallback callback = new RecoveryRetryCallback(input, new RetryCallback() {
public Object doWithRetry(RetryContext context) throws Throwable {
public Object doWithRetry(RetryContext context) throws Exception {
return null;
}
});
@@ -184,7 +184,7 @@ public class RecoveryRetryPolicyTests extends TestCase {
policy.setDelegate(new SimpleRetryPolicy(1));
final String input = "foo";
RecoveryRetryCallback callback = new RecoveryRetryCallback(input, new RetryCallback() {
public Object doWithRetry(RetryContext context) throws Throwable {
public Object doWithRetry(RetryContext context) throws Exception {
throw new RuntimeException("Barf!");
}
});
@@ -214,7 +214,7 @@ public class RecoveryRetryPolicyTests extends TestCase {
public void testExhaustedClearsHistoryAfterLastAttempt() throws Exception {
RecoveryRetryCallback callback = new RecoveryRetryCallback("foo", new RetryCallback() {
public Object doWithRetry(RetryContext context) throws Throwable {
public Object doWithRetry(RetryContext context) throws Exception {
count++;
return null;
}
@@ -242,7 +242,7 @@ public class RecoveryRetryPolicyTests extends TestCase {
policy = new RecoveryCallbackRetryPolicy();
policy.setDelegate(new SimpleRetryPolicy(1));
RetryContext context = policy.open(new RecoveryRetryCallback("foo", new RetryCallback() {
public Object doWithRetry(RetryContext context) throws Throwable {
public Object doWithRetry(RetryContext context) throws Exception {
count++;
return null;
}
@@ -257,7 +257,7 @@ public class RecoveryRetryPolicyTests extends TestCase {
public void testRetryCountPreservedBetweenRetries() throws Exception {
RecoveryRetryCallback callback = new RecoveryRetryCallback("bar", new RetryCallback() {
public Object doWithRetry(RetryContext context) throws Throwable {
public Object doWithRetry(RetryContext context) throws Exception {
count++;
return null;
}
@@ -282,7 +282,7 @@ public class RecoveryRetryPolicyTests extends TestCase {
final StringHolder item = new StringHolder("bar");
RetryCallback writer = new RetryCallback() {
public Object doWithRetry(RetryContext context) throws Throwable {
public Object doWithRetry(RetryContext context) throws Exception {
// This simulates what happens if someone uses a primary key
// for hasCode and equals and then relies on default key
// generator
@@ -320,7 +320,7 @@ public class RecoveryRetryPolicyTests extends TestCase {
final StringHolder item = new StringHolder("foo");
RetryCallback writer = new RetryCallback() {
public Object doWithRetry(RetryContext context) throws Throwable {
public Object doWithRetry(RetryContext context) throws Exception {
count++;
list.add(item.string);
return item;
@@ -349,7 +349,7 @@ public class RecoveryRetryPolicyTests extends TestCase {
final StringHolder item = new StringHolder("foo");
RetryCallback writer = new RetryCallback() {
public Object doWithRetry(RetryContext context) throws Throwable {
public Object doWithRetry(RetryContext context) throws Exception {
count++;
list.add(item.string);
return item;

View File

@@ -23,13 +23,10 @@ import junit.framework.TestCase;
import org.springframework.batch.retry.ExhaustedRetryException;
import org.springframework.batch.retry.RetryCallback;
import org.springframework.batch.retry.RetryContext;
import org.springframework.batch.retry.RetryException;
import org.springframework.batch.retry.RetryListener;
import org.springframework.batch.retry.backoff.BackOffContext;
import org.springframework.batch.retry.backoff.BackOffInterruptedException;
import org.springframework.batch.retry.backoff.BackOffPolicy;
import org.springframework.batch.retry.backoff.StatelessBackOffPolicy;
import org.springframework.batch.retry.listener.RetryListenerSupport;
import org.springframework.batch.retry.policy.NeverRetryPolicy;
import org.springframework.batch.retry.policy.SimpleRetryPolicy;
@@ -141,7 +138,7 @@ public class RetryTemplateTests extends TestCase {
try {
RetryTemplate retryTemplate = new RetryTemplate();
retryTemplate.execute(new RetryCallback() {
public Object doWithRetry(RetryContext status) throws Throwable {
public Object doWithRetry(RetryContext status) throws Exception {
status.setExhaustedOnly();
throw new IllegalStateException("Retry this operation");
}
@@ -159,11 +156,11 @@ public class RetryTemplateTests extends TestCase {
RetryTemplate outer = new RetryTemplate();
final RetryTemplate inner = new RetryTemplate();
outer.execute(new RetryCallback() {
public Object doWithRetry(RetryContext status) throws Throwable {
public Object doWithRetry(RetryContext status) throws Exception {
context = status;
count++;
Object result = inner.execute(new RetryCallback() {
public Object doWithRetry(RetryContext status) throws Throwable {
public Object doWithRetry(RetryContext status) throws Exception {
count++;
assertNotNull(context);
assertNotSame(status, context);
@@ -184,7 +181,7 @@ public class RetryTemplateTests extends TestCase {
retryTemplate.setRetryPolicy(new NeverRetryPolicy());
try {
retryTemplate.execute(new RetryCallback() {
public Object doWithRetry(RetryContext context) throws Throwable {
public Object doWithRetry(RetryContext context) throws Exception {
throw new Error("Realllly bad!");
}
});
@@ -204,7 +201,7 @@ public class RetryTemplateTests extends TestCase {
});
try {
retryTemplate.execute(new RetryCallback() {
public Object doWithRetry(RetryContext context) throws Throwable {
public Object doWithRetry(RetryContext context) throws Exception {
throw new RuntimeException("Bad!");
}
});
@@ -239,74 +236,6 @@ public class RetryTemplateTests extends TestCase {
}
}
/**
* Throwables that aren't Exception nor Error are wrapped into
* RetryException.
*/
public void testThrowableWrapping() throws Exception {
RetryCallback callback = new RetryCallback() {
public Object doWithRetry(RetryContext context) throws Throwable {
throw new Throwable("throwable in callback");
}
};
RetryTemplate template = new RetryTemplate();
try {
template.execute(callback);
fail();
}
catch (RetryException expected) {
assertTrue(expected.getMessage().contains("Unclassified Throwable encountered"));
assertEquals("throwable in callback", expected.getCause().getMessage());
}
}
/**
* If nested template wraps unclassified Throwable into RetryException the
* Throwable is unwrapped before passed to collaborators.
*/
public void testThrowableUnwrapping() throws Exception {
final RetryCallback throwingCallback = new RetryCallback() {
public Object doWithRetry(RetryContext context) throws Throwable {
throw new Throwable("Crashed terribly");
}
};
final RetryTemplate nested = new RetryTemplate();
RetryCallback callNested = new RetryCallback() {
public Object doWithRetry(RetryContext context) throws Throwable {
return nested.execute(throwingCallback);
}
};
ExceptionCheckingListener listener = new ExceptionCheckingListener();
RetryTemplate template = new RetryTemplate();
template.setListeners(new RetryListener[] { listener });
try {
template.execute(callNested);
fail();
}
catch (RetryException expected) {
assertTrue(expected.getMessage().contains("Unclassified Throwable encountered"));
assertEquals("Crashed terribly", expected.getCause().getMessage());
}
assertTrue(listener.called);
}
private static class ExceptionCheckingListener extends RetryListenerSupport {
boolean called = false;
public void onError(RetryContext context, RetryCallback callback, Throwable throwable) {
called = true;
assertFalse(throwable instanceof Exception);
assertFalse(throwable instanceof Error);
assertEquals("Crashed terribly", throwable.getMessage());
}
}
private static class MockRetryCallback implements RetryCallback {
private int attempts;