KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > transaction > TransactionSupportTests


1 /*
2  * Copyright 2002-2005 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.transaction;
18
19 import junit.framework.TestCase;
20
21 import org.springframework.transaction.support.DefaultTransactionDefinition;
22 import org.springframework.transaction.support.TransactionCallbackWithoutResult;
23 import org.springframework.transaction.support.TransactionTemplate;
24 import org.springframework.transaction.support.TransactionSynchronizationManager;
25 import org.springframework.transaction.support.DefaultTransactionStatus;
26
27 /**
28  * @author Juergen Hoeller
29  * @since 29.04.2003
30  */

31 public class TransactionSupportTests extends TestCase {
32
33     public void testNoExistingTransaction() {
34         PlatformTransactionManager tm = new TestTransactionManager(false, true);
35         DefaultTransactionStatus status1 = (DefaultTransactionStatus)
36                 tm.getTransaction(new DefaultTransactionDefinition(TransactionDefinition.PROPAGATION_SUPPORTS));
37         assertTrue("Must not have transaction", status1.getTransaction() == null);
38
39         DefaultTransactionStatus status2 = (DefaultTransactionStatus)
40                 tm.getTransaction(new DefaultTransactionDefinition(TransactionDefinition.PROPAGATION_REQUIRED));
41         assertTrue("Must have transaction", status2.getTransaction() != null);
42         assertTrue("Must be new transaction", status2.isNewTransaction());
43
44         try {
45             tm.getTransaction(new DefaultTransactionDefinition(TransactionDefinition.PROPAGATION_MANDATORY));
46             fail("Should not have thrown NoTransactionException");
47         }
48         catch (IllegalTransactionStateException ex) {
49             // expected
50
}
51     }
52
53     public void testExistingTransaction() {
54         PlatformTransactionManager tm = new TestTransactionManager(true, true);
55         DefaultTransactionStatus status1 = (DefaultTransactionStatus)
56                 tm.getTransaction(new DefaultTransactionDefinition(TransactionDefinition.PROPAGATION_SUPPORTS));
57         assertTrue("Must have transaction", status1.getTransaction() != null);
58         assertTrue("Must not be new transaction", !status1.isNewTransaction());
59
60         DefaultTransactionStatus status2 = (DefaultTransactionStatus)
61                 tm.getTransaction(new DefaultTransactionDefinition(TransactionDefinition.PROPAGATION_REQUIRED));
62         assertTrue("Must have transaction", status2.getTransaction() != null);
63         assertTrue("Must not be new transaction", !status2.isNewTransaction());
64
65         try {
66             DefaultTransactionStatus status3 = (DefaultTransactionStatus)
67                     tm.getTransaction(new DefaultTransactionDefinition(TransactionDefinition.PROPAGATION_MANDATORY));
68             assertTrue("Must have transaction", status3.getTransaction() != null);
69             assertTrue("Must not be new transaction", !status3.isNewTransaction());
70         }
71         catch (NoTransactionException ex) {
72             fail("Should not have thrown NoTransactionException");
73         }
74     }
75
76     public void testCommitWithoutExistingTransaction() {
77         TestTransactionManager tm = new TestTransactionManager(false, true);
78         TransactionStatus status = tm.getTransaction(null);
79         tm.commit(status);
80         assertTrue("triggered begin", tm.begin);
81         assertTrue("triggered commit", tm.commit);
82         assertTrue("no rollback", !tm.rollback);
83         assertTrue("no rollbackOnly", !tm.rollbackOnly);
84     }
85
86     public void testRollbackWithoutExistingTransaction() {
87         TestTransactionManager tm = new TestTransactionManager(false, true);
88         TransactionStatus status = tm.getTransaction(null);
89         tm.rollback(status);
90         assertTrue("triggered begin", tm.begin);
91         assertTrue("no commit", !tm.commit);
92         assertTrue("triggered rollback", tm.rollback);
93         assertTrue("no rollbackOnly", !tm.rollbackOnly);
94     }
95
96     public void testRollbackOnlyWithoutExistingTransaction() {
97         TestTransactionManager tm = new TestTransactionManager(false, true);
98         TransactionStatus status = tm.getTransaction(null);
99         status.setRollbackOnly();
100         tm.commit(status);
101         assertTrue("triggered begin", tm.begin);
102         assertTrue("no commit", !tm.commit);
103         assertTrue("triggered rollback", tm.rollback);
104         assertTrue("no rollbackOnly", !tm.rollbackOnly);
105     }
106
107     public void testCommitWithExistingTransaction() {
108         TestTransactionManager tm = new TestTransactionManager(true, true);
109         TransactionStatus status = tm.getTransaction(null);
110         tm.commit(status);
111         assertTrue("no begin", !tm.begin);
112         assertTrue("no commit", !tm.commit);
113         assertTrue("no rollback", !tm.rollback);
114         assertTrue("no rollbackOnly", !tm.rollbackOnly);
115     }
116
117     public void testRollbackWithExistingTransaction() {
118         TestTransactionManager tm = new TestTransactionManager(true, true);
119         TransactionStatus status = tm.getTransaction(null);
120         tm.rollback(status);
121         assertTrue("no begin", !tm.begin);
122         assertTrue("no commit", !tm.commit);
123         assertTrue("no rollback", !tm.rollback);
124         assertTrue("triggered rollbackOnly", tm.rollbackOnly);
125     }
126
127     public void testRollbackOnlyWithExistingTransaction() {
128         TestTransactionManager tm = new TestTransactionManager(true, true);
129         TransactionStatus status = tm.getTransaction(null);
130         status.setRollbackOnly();
131         tm.commit(status);
132         assertTrue("no begin", !tm.begin);
133         assertTrue("no commit", !tm.commit);
134         assertTrue("no rollback", !tm.rollback);
135         assertTrue("triggered rollbackOnly", tm.rollbackOnly);
136     }
137
138     public void testTransactionTemplate() {
139         TestTransactionManager tm = new TestTransactionManager(false, true);
140         TransactionTemplate template = new TransactionTemplate(tm);
141         try {
142             template.execute(new TransactionCallbackWithoutResult() {
143                 protected void doInTransactionWithoutResult(TransactionStatus status) {
144                 }
145             });
146             assertTrue("triggered begin", tm.begin);
147             assertTrue("triggered commit", tm.commit);
148             assertTrue("no rollback", !tm.rollback);
149             assertTrue("no rollbackOnly", !tm.rollbackOnly);
150         }
151         catch (RuntimeException JavaDoc ex) {
152             fail("Should not have thrown RuntimeException");
153         }
154     }
155
156     public void testTransactionTemplateWithException() {
157         TestTransactionManager tm = new TestTransactionManager(false, true);
158         TransactionTemplate template = new TransactionTemplate(tm);
159         final RuntimeException JavaDoc ex = new RuntimeException JavaDoc("Some application exception");
160         try {
161             template.execute(new TransactionCallbackWithoutResult() {
162                 protected void doInTransactionWithoutResult(TransactionStatus status) {
163                     throw ex;
164                 }
165             });
166             fail("Should have propagated RuntimeException");
167         }
168         catch (RuntimeException JavaDoc caught) {
169             // expected
170
assertTrue("Correct exception", caught == ex);
171             assertTrue("triggered begin", tm.begin);
172             assertTrue("no commit", !tm.commit);
173             assertTrue("triggered rollback", tm.rollback);
174             assertTrue("no rollbackOnly", !tm.rollbackOnly);
175         }
176     }
177
178     public void testTransactionTemplateWithRollbackException() {
179         final TransactionSystemException tex = new TransactionSystemException("system exception");
180         TestTransactionManager tm = new TestTransactionManager(false, true) {
181             protected void doRollback(DefaultTransactionStatus status) {
182                 super.doRollback(status);
183                 throw tex;
184             }
185         };
186         TransactionTemplate template = new TransactionTemplate(tm);
187         final RuntimeException JavaDoc ex = new RuntimeException JavaDoc("Some application exception");
188         try {
189             template.execute(new TransactionCallbackWithoutResult() {
190                 protected void doInTransactionWithoutResult(TransactionStatus status) {
191                     throw ex;
192                 }
193             });
194             fail("Should have propagated RuntimeException");
195         }
196         catch (RuntimeException JavaDoc caught) {
197             // expected
198
assertTrue("Correct exception", caught == tex);
199             assertTrue("triggered begin", tm.begin);
200             assertTrue("no commit", !tm.commit);
201             assertTrue("triggered rollback", tm.rollback);
202             assertTrue("no rollbackOnly", !tm.rollbackOnly);
203         }
204     }
205
206     public void testTransactionTemplateWithError() {
207         TestTransactionManager tm = new TestTransactionManager(false, true);
208         TransactionTemplate template = new TransactionTemplate(tm);
209         try {
210             template.execute(new TransactionCallbackWithoutResult() {
211                 protected void doInTransactionWithoutResult(TransactionStatus status) {
212                     throw new Error JavaDoc("Some application error");
213                 }
214             });
215             fail("Should have propagated Error");
216         }
217         catch (Error JavaDoc err) {
218             // expected
219
assertTrue("triggered begin", tm.begin);
220             assertTrue("no commit", !tm.commit);
221             assertTrue("triggered rollback", tm.rollback);
222             assertTrue("no rollbackOnly", !tm.rollbackOnly);
223         }
224     }
225
226     public void testTransactionTemplateInitialization() {
227         TestTransactionManager tm = new TestTransactionManager(false, true);
228         TransactionTemplate template = new TransactionTemplate();
229         template.setTransactionManager(tm);
230         assertTrue("correct transaction manager set", template.getTransactionManager() == tm);
231
232         try {
233             template.setPropagationBehaviorName("TIMEOUT_DEFAULT");
234             fail("Should have thrown IllegalArgumentException");
235         }
236         catch (IllegalArgumentException JavaDoc ex) {
237             // expected
238
}
239         template.setPropagationBehaviorName("PROPAGATION_SUPPORTS");
240         assertTrue("Correct propagation behavior set", template.getPropagationBehavior() == TransactionDefinition.PROPAGATION_SUPPORTS);
241
242         try {
243             template.setPropagationBehavior(999);
244             fail("Should have thrown IllegalArgumentException");
245         }
246         catch (IllegalArgumentException JavaDoc ex) {
247             // expected
248
}
249         template.setPropagationBehavior(TransactionDefinition.PROPAGATION_MANDATORY);
250         assertTrue("Correct propagation behavior set", template.getPropagationBehavior() == TransactionDefinition.PROPAGATION_MANDATORY);
251
252         try {
253             template.setIsolationLevelName("TIMEOUT_DEFAULT");
254             fail("Should have thrown IllegalArgumentException");
255         }
256         catch (IllegalArgumentException JavaDoc ex) {
257             // expected
258
}
259         template.setIsolationLevelName("ISOLATION_SERIALIZABLE");
260         assertTrue("Correct isolation level set", template.getIsolationLevel() == TransactionDefinition.ISOLATION_SERIALIZABLE);
261
262         try {
263             template.setIsolationLevel(999);
264             fail("Should have thrown IllegalArgumentException");
265         }
266         catch (IllegalArgumentException JavaDoc ex) {
267             // expected
268
}
269         template.setIsolationLevel(TransactionDefinition.ISOLATION_REPEATABLE_READ);
270         assertTrue("Correct isolation level set", template.getIsolationLevel() == TransactionDefinition.ISOLATION_REPEATABLE_READ);
271     }
272
273     protected void tearDown() {
274         assertTrue(TransactionSynchronizationManager.getResourceMap().isEmpty());
275         assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
276     }
277
278 }
279
Popular Tags