View Javadoc

1   /*
2    * Copyright 2006-2007 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * @author Dave Syer
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  			// The first failed attempt we expect to retry...
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  			// We don't get a second attempt...
69  			fail("Expected ExhaustedRetryException");
70  		}
71  		catch (ExhaustedRetryException e) {
72  			// This is now the "exhausted" message:
73  			assertNotNull(e.getMessage());
74  		}
75  
76  		assertFalse(cache.containsKey("foo"));
77  
78  		// Callback is called once: the recovery path should be called in
79  		// handleRetryExhausted (so not in this test)...
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 			// The first failed attempt we expect to retry...
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 	 * @author Dave Syer
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 }