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  
17  package org.springframework.batch.retry.support;
18  
19  import junit.framework.TestCase;
20  
21  import org.springframework.batch.retry.RetryCallback;
22  import org.springframework.batch.retry.RetryContext;
23  import org.springframework.batch.retry.context.RetryContextSupport;
24  
25  /**
26   * @author Dave Syer
27   */
28  public class RetrySynchronizationManagerTests extends TestCase {
29  
30  	RetryTemplate template = new RetryTemplate();
31  
32  	protected void setUp() throws Exception {
33  		super.setUp();
34  		RetrySynchronizationManagerTests.clearAll();
35  		RetryContext status = RetrySynchronizationManager.getContext();
36  		assertNull(status);
37  	}
38  
39  	public void testStatusIsStoredByTemplate() throws Exception {
40  
41  		RetryContext status = RetrySynchronizationManager.getContext();
42  		assertNull(status);
43  
44  		template.execute(new RetryCallback<Object>() {
45  			public Object doWithRetry(RetryContext status) throws Exception {
46  				RetryContext global = RetrySynchronizationManager.getContext();
47  				assertNotNull(status);
48  				assertEquals(global, status);
49  				return null;
50  			}
51  		});
52  
53  		status = RetrySynchronizationManager.getContext();
54  		assertNull(status);
55  	}
56  
57  	public void testStatusRegistration() throws Exception {
58  		RetryContext status = new RetryContextSupport(null);
59  		RetryContext value = RetrySynchronizationManager.register(status);
60  		assertNull(value);
61  		value = RetrySynchronizationManager.register(status);
62  		assertEquals(status, value);
63  	}
64  
65  	public void testClear() throws Exception {
66  		RetryContext status = new RetryContextSupport(null);
67  		RetryContext value = RetrySynchronizationManager.register(status);
68  		assertNull(value);
69  		RetrySynchronizationManager.clear();
70  		value = RetrySynchronizationManager.register(status);
71  		assertNull(value);
72  	}
73  
74  	public void testParent() throws Exception {
75  		RetryContext parent = new RetryContextSupport(null);
76  		RetryContext child = new RetryContextSupport(parent);
77  		assertSame(parent, child.getParent());
78  	}
79  
80  	/**
81  	 * Clear all contexts starting with the current one and continuing until
82  	 * {@link RetrySynchronizationManager#clear()} returns null.
83  	 */
84  	public static RetryContext clearAll() {
85  		RetryContext result = null;
86  		RetryContext context = RetrySynchronizationManager.clear();
87  		while (context != null) {
88  			result = context;
89  			context = RetrySynchronizationManager.clear();
90  		}
91  		return result;
92  	}
93  }