1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.springframework.batch.retry.policy;
17
18 import static org.junit.Assert.assertEquals;
19 import static org.junit.Assert.assertFalse;
20 import static org.junit.Assert.assertNotNull;
21 import static org.junit.Assert.assertNull;
22 import static org.junit.Assert.assertTrue;
23 import static org.junit.Assert.fail;
24
25 import java.util.Collections;
26
27 import org.junit.Test;
28 import org.springframework.batch.retry.ExhaustedRetryException;
29 import org.springframework.batch.retry.RetryState;
30 import org.springframework.batch.retry.RetryCallback;
31 import org.springframework.batch.retry.RetryContext;
32 import org.springframework.batch.retry.support.DefaultRetryState;
33 import org.springframework.batch.retry.support.RetryTemplate;
34
35
36
37
38
39 public class StatefulRetryIntegrationTests {
40
41 @Test
42 public void testExternalRetryWithFailAndNoRetry() throws Exception {
43 MockRetryCallback callback = new MockRetryCallback();
44
45 RetryState retryState = new DefaultRetryState("foo");
46
47 RetryTemplate retryTemplate = new RetryTemplate();
48 MapRetryContextCache cache = new MapRetryContextCache();
49 retryTemplate.setRetryContextCache(cache);
50 retryTemplate.setRetryPolicy(new SimpleRetryPolicy(1, Collections
51 .<Class<? extends Throwable>, Boolean> singletonMap(Exception.class, true)));
52
53 assertFalse(cache.containsKey("foo"));
54
55 try {
56 retryTemplate.execute(callback, retryState);
57
58 fail("Expected RuntimeException");
59 }
60 catch (RuntimeException e) {
61 assertEquals(null, e.getMessage());
62 }
63
64 assertTrue(cache.containsKey("foo"));
65
66 try {
67 retryTemplate.execute(callback, retryState);
68
69 fail("Expected ExhaustedRetryException");
70 }
71 catch (ExhaustedRetryException e) {
72
73 assertNotNull(e.getMessage());
74 }
75
76 assertFalse(cache.containsKey("foo"));
77
78
79
80 assertEquals(1, callback.attempts);
81 }
82
83 @Test
84 public void testExternalRetryWithSuccessOnRetry() throws Exception {
85 MockRetryCallback callback = new MockRetryCallback();
86
87 RetryState retryState = new DefaultRetryState("foo");
88
89 RetryTemplate retryTemplate = new RetryTemplate();
90 MapRetryContextCache cache = new MapRetryContextCache();
91 retryTemplate.setRetryContextCache(cache);
92 retryTemplate.setRetryPolicy(new SimpleRetryPolicy(2, Collections
93 .<Class<? extends Throwable>, Boolean> singletonMap(Exception.class, true)));
94
95 assertFalse(cache.containsKey("foo"));
96
97 Object result = "start_foo";
98 try {
99 result = retryTemplate.execute(callback, retryState);
100
101 fail("Expected RuntimeException");
102 }
103 catch (RuntimeException e) {
104 assertNull(e.getMessage());
105 }
106
107 assertTrue(cache.containsKey("foo"));
108
109 result = retryTemplate.execute(callback, retryState);
110
111 assertFalse(cache.containsKey("foo"));
112
113 assertEquals(2, callback.attempts);
114 assertEquals("bar", result);
115 }
116
117
118
119
120
121 private static final class MockRetryCallback implements RetryCallback<String> {
122 int attempts = 0;
123
124 public String doWithRetry(RetryContext context) throws Exception {
125 attempts++;
126 if (attempts < 2) {
127 throw new RuntimeException();
128 }
129 return "bar";
130 }
131 }
132
133 }