KickJava   Java API By Example, From Geeks To Geeks.

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


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 javax.transaction.HeuristicMixedException JavaDoc;
20 import javax.transaction.HeuristicRollbackException JavaDoc;
21 import javax.transaction.NotSupportedException JavaDoc;
22 import javax.transaction.RollbackException JavaDoc;
23 import javax.transaction.Status JavaDoc;
24 import javax.transaction.SystemException JavaDoc;
25 import javax.transaction.Transaction JavaDoc;
26 import javax.transaction.TransactionManager JavaDoc;
27 import javax.transaction.UserTransaction JavaDoc;
28
29 import junit.framework.TestCase;
30 import org.easymock.MockControl;
31
32 import org.springframework.transaction.jta.JtaTransactionManager;
33 import org.springframework.transaction.support.DefaultTransactionDefinition;
34 import org.springframework.transaction.support.TransactionCallbackWithoutResult;
35 import org.springframework.transaction.support.TransactionSynchronization;
36 import org.springframework.transaction.support.TransactionSynchronizationAdapter;
37 import org.springframework.transaction.support.TransactionSynchronizationManager;
38 import org.springframework.transaction.support.TransactionTemplate;
39
40 /**
41  * @author Juergen Hoeller
42  * @since 12.05.2003
43  */

44 public class JtaTransactionManagerTests extends TestCase {
45     
46     public void testJtaTransactionManagerWithCommit() throws Exception JavaDoc {
47         MockControl utControl = MockControl.createControl(UserTransaction JavaDoc.class);
48         UserTransaction JavaDoc ut = (UserTransaction JavaDoc) utControl.getMock();
49         ut.getStatus();
50         utControl.setReturnValue(Status.STATUS_NO_TRANSACTION, 1);
51         ut.getStatus();
52         utControl.setReturnValue(Status.STATUS_ACTIVE, 1);
53         ut.begin();
54         utControl.setVoidCallable(1);
55         ut.commit();
56         utControl.setVoidCallable(1);
57         utControl.replay();
58
59         MockControl synchControl = MockControl.createControl(TransactionSynchronization.class);
60         final TransactionSynchronization synch = (TransactionSynchronization) synchControl.getMock();
61         synch.beforeCommit(false);
62         synchControl.setVoidCallable(1);
63         synch.beforeCompletion();
64         synchControl.setVoidCallable(1);
65         synch.afterCompletion(TransactionSynchronization.STATUS_COMMITTED);
66         synchControl.setVoidCallable(1);
67         synchControl.replay();
68
69         JtaTransactionManager ptm = new JtaTransactionManager(ut);
70         TransactionTemplate tt = new TransactionTemplate(ptm);
71         tt.setName("txName");
72
73         assertEquals(JtaTransactionManager.SYNCHRONIZATION_ALWAYS, ptm.getTransactionSynchronization());
74         assertTrue(!TransactionSynchronizationManager.isSynchronizationActive());
75         assertNull(TransactionSynchronizationManager.getCurrentTransactionName());
76         assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
77         tt.execute(new TransactionCallbackWithoutResult() {
78             protected void doInTransactionWithoutResult(TransactionStatus status) {
79                 // something transactional
80
assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
81                 TransactionSynchronizationManager.registerSynchronization(synch);
82                 assertEquals("txName", TransactionSynchronizationManager.getCurrentTransactionName());
83                 assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
84             }
85         });
86         assertTrue(!TransactionSynchronizationManager.isSynchronizationActive());
87         assertNull(TransactionSynchronizationManager.getCurrentTransactionName());
88         assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
89
90         utControl.verify();
91         synchControl.verify();
92     }
93
94     public void testJtaTransactionManagerWithCommitAndSynchronizationOnActual() throws Exception JavaDoc {
95         MockControl utControl = MockControl.createControl(UserTransaction JavaDoc.class);
96         UserTransaction JavaDoc ut = (UserTransaction JavaDoc) utControl.getMock();
97         ut.getStatus();
98         utControl.setReturnValue(Status.STATUS_NO_TRANSACTION, 1);
99         ut.getStatus();
100         utControl.setReturnValue(Status.STATUS_ACTIVE, 1);
101         ut.begin();
102         utControl.setVoidCallable(1);
103         ut.commit();
104         utControl.setVoidCallable(1);
105         utControl.replay();
106
107         MockControl synchControl = MockControl.createControl(TransactionSynchronization.class);
108         final TransactionSynchronization synch = (TransactionSynchronization) synchControl.getMock();
109         synch.beforeCommit(false);
110         synchControl.setVoidCallable(1);
111         synch.beforeCompletion();
112         synchControl.setVoidCallable(1);
113         synch.afterCompletion(TransactionSynchronization.STATUS_COMMITTED);
114         synchControl.setVoidCallable(1);
115         synchControl.replay();
116
117         JtaTransactionManager ptm = new JtaTransactionManager(ut);
118         TransactionTemplate tt = new TransactionTemplate(ptm);
119         ptm.setTransactionSynchronization(JtaTransactionManager.SYNCHRONIZATION_ON_ACTUAL_TRANSACTION);
120         assertTrue(!TransactionSynchronizationManager.isSynchronizationActive());
121         tt.execute(new TransactionCallbackWithoutResult() {
122             protected void doInTransactionWithoutResult(TransactionStatus status) {
123                 // something transactional
124
assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
125                 TransactionSynchronizationManager.registerSynchronization(synch);
126             }
127         });
128         assertTrue(!TransactionSynchronizationManager.isSynchronizationActive());
129
130         utControl.verify();
131         synchControl.verify();
132     }
133
134     public void testJtaTransactionManagerWithCommitAndSynchronizationNever() throws Exception JavaDoc {
135         MockControl utControl = MockControl.createControl(UserTransaction JavaDoc.class);
136         UserTransaction JavaDoc ut = (UserTransaction JavaDoc) utControl.getMock();
137         ut.getStatus();
138         utControl.setReturnValue(Status.STATUS_NO_TRANSACTION, 1);
139         ut.getStatus();
140         utControl.setReturnValue(Status.STATUS_ACTIVE, 1);
141         ut.begin();
142         utControl.setVoidCallable(1);
143         ut.commit();
144         utControl.setVoidCallable(1);
145         utControl.replay();
146
147         JtaTransactionManager ptm = new JtaTransactionManager(ut);
148         TransactionTemplate tt = new TransactionTemplate(ptm);
149         ptm.setTransactionSynchronization(JtaTransactionManager.SYNCHRONIZATION_NEVER);
150         assertTrue(!TransactionSynchronizationManager.isSynchronizationActive());
151         tt.execute(new TransactionCallbackWithoutResult() {
152             protected void doInTransactionWithoutResult(TransactionStatus status) {
153                 assertTrue(!TransactionSynchronizationManager.isSynchronizationActive());
154             }
155         });
156         assertTrue(!TransactionSynchronizationManager.isSynchronizationActive());
157
158         utControl.verify();
159     }
160
161     public void testJtaTransactionManagerWithRollback() throws Exception JavaDoc {
162         MockControl utControl = MockControl.createControl(UserTransaction JavaDoc.class);
163         UserTransaction JavaDoc ut = (UserTransaction JavaDoc) utControl.getMock();
164         ut.getStatus();
165         utControl.setReturnValue(Status.STATUS_NO_TRANSACTION, 1);
166         ut.setTransactionTimeout(10);
167         utControl.setVoidCallable(1);
168         ut.begin();
169         utControl.setVoidCallable(1);
170         ut.rollback();
171         utControl.setVoidCallable(1);
172         utControl.replay();
173
174         MockControl synchControl = MockControl.createControl(TransactionSynchronization.class);
175         final TransactionSynchronization synch = (TransactionSynchronization) synchControl.getMock();
176         synch.beforeCompletion();
177         synchControl.setVoidCallable(1);
178         synch.afterCompletion(TransactionSynchronization.STATUS_ROLLED_BACK);
179         synchControl.setVoidCallable(1);
180         synchControl.replay();
181
182         JtaTransactionManager ptm = new JtaTransactionManager(ut);
183         TransactionTemplate tt = new TransactionTemplate(ptm);
184         tt.setTimeout(10);
185         tt.setName("txName");
186
187         assertTrue(!TransactionSynchronizationManager.isSynchronizationActive());
188         assertNull(TransactionSynchronizationManager.getCurrentTransactionName());
189         assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
190         tt.execute(new TransactionCallbackWithoutResult() {
191             protected void doInTransactionWithoutResult(TransactionStatus status) {
192                 assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
193                 TransactionSynchronizationManager.registerSynchronization(synch);
194                 assertEquals("txName", TransactionSynchronizationManager.getCurrentTransactionName());
195                 assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
196                 status.setRollbackOnly();
197             }
198         });
199         assertTrue(!TransactionSynchronizationManager.isSynchronizationActive());
200         assertNull(TransactionSynchronizationManager.getCurrentTransactionName());
201         assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
202
203         utControl.verify();
204         synchControl.verify();
205     }
206
207     public void testJtaTransactionManagerWithRollbackAndSynchronizationOnActual() throws Exception JavaDoc {
208         MockControl utControl = MockControl.createControl(UserTransaction JavaDoc.class);
209         UserTransaction JavaDoc ut = (UserTransaction JavaDoc) utControl.getMock();
210         ut.getStatus();
211         utControl.setReturnValue(Status.STATUS_NO_TRANSACTION, 1);
212         ut.setTransactionTimeout(10);
213         utControl.setVoidCallable(1);
214         ut.begin();
215         utControl.setVoidCallable(1);
216         ut.rollback();
217         utControl.setVoidCallable(1);
218         utControl.replay();
219
220         MockControl synchControl = MockControl.createControl(TransactionSynchronization.class);
221         final TransactionSynchronization synch = (TransactionSynchronization) synchControl.getMock();
222         synch.beforeCompletion();
223         synchControl.setVoidCallable(1);
224         synch.afterCompletion(TransactionSynchronization.STATUS_ROLLED_BACK);
225         synchControl.setVoidCallable(1);
226         synchControl.replay();
227
228         JtaTransactionManager ptm = new JtaTransactionManager(ut);
229         TransactionTemplate tt = new TransactionTemplate(ptm);
230         ptm.setTransactionSynchronization(JtaTransactionManager.SYNCHRONIZATION_ON_ACTUAL_TRANSACTION);
231         tt.setTimeout(10);
232         assertTrue(!TransactionSynchronizationManager.isSynchronizationActive());
233         tt.execute(new TransactionCallbackWithoutResult() {
234             protected void doInTransactionWithoutResult(TransactionStatus status) {
235                 assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
236                 TransactionSynchronizationManager.registerSynchronization(synch);
237                 status.setRollbackOnly();
238             }
239         });
240         assertTrue(!TransactionSynchronizationManager.isSynchronizationActive());
241
242         utControl.verify();
243         synchControl.verify();
244     }
245
246     public void testJtaTransactionManagerWithRollbackAndSynchronizationNever() throws Exception JavaDoc {
247         MockControl utControl = MockControl.createControl(UserTransaction JavaDoc.class);
248         UserTransaction JavaDoc ut = (UserTransaction JavaDoc) utControl.getMock();
249         ut.getStatus();
250         utControl.setReturnValue(Status.STATUS_NO_TRANSACTION, 1);
251         ut.setTransactionTimeout(10);
252         utControl.setVoidCallable(1);
253         ut.begin();
254         utControl.setVoidCallable(1);
255         ut.rollback();
256         utControl.setVoidCallable(1);
257         utControl.replay();
258
259         JtaTransactionManager ptm = new JtaTransactionManager(ut);
260         TransactionTemplate tt = new TransactionTemplate(ptm);
261         ptm.setTransactionSynchronizationName("SYNCHRONIZATION_NEVER");
262         tt.setTimeout(10);
263         assertTrue(!TransactionSynchronizationManager.isSynchronizationActive());
264         tt.execute(new TransactionCallbackWithoutResult() {
265             protected void doInTransactionWithoutResult(TransactionStatus status) {
266                 assertTrue(!TransactionSynchronizationManager.isSynchronizationActive());
267                 status.setRollbackOnly();
268             }
269         });
270         assertTrue(!TransactionSynchronizationManager.isSynchronizationActive());
271
272         utControl.verify();
273     }
274
275     public void testJtaTransactionManagerWithExistingTransaction() throws Exception JavaDoc {
276         MockControl utControl = MockControl.createControl(UserTransaction JavaDoc.class);
277         UserTransaction JavaDoc ut = (UserTransaction JavaDoc) utControl.getMock();
278         ut.getStatus();
279         utControl.setReturnValue(Status.STATUS_ACTIVE, 1);
280         ut.setRollbackOnly();
281         utControl.setVoidCallable(1);
282         utControl.replay();
283
284         MockControl synchControl = MockControl.createControl(TransactionSynchronization.class);
285         final TransactionSynchronization synch = (TransactionSynchronization) synchControl.getMock();
286         synch.beforeCompletion();
287         synchControl.setVoidCallable(1);
288         synch.afterCompletion(TransactionSynchronization.STATUS_UNKNOWN);
289         synchControl.setVoidCallable(1);
290         synchControl.replay();
291
292         JtaTransactionManager ptm = new JtaTransactionManager(ut);
293         TransactionTemplate tt = new TransactionTemplate(ptm);
294         assertTrue(!TransactionSynchronizationManager.isSynchronizationActive());
295         tt.execute(new TransactionCallbackWithoutResult() {
296             protected void doInTransactionWithoutResult(TransactionStatus status) {
297                 assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
298                 TransactionSynchronizationManager.registerSynchronization(synch);
299                 status.setRollbackOnly();
300             }
301         });
302         assertTrue(!TransactionSynchronizationManager.isSynchronizationActive());
303
304         utControl.verify();
305         synchControl.verify();
306     }
307
308     public void testJtaTransactionManagerWithExistingTransactionAndJtaSynchronization() throws Exception JavaDoc {
309         MockControl utControl = MockControl.createControl(UserTransaction JavaDoc.class);
310         UserTransaction JavaDoc ut = (UserTransaction JavaDoc) utControl.getMock();
311         MockControl tmControl = MockControl.createControl(TransactionManager JavaDoc.class);
312         TransactionManager JavaDoc tm = (TransactionManager JavaDoc) tmControl.getMock();
313         MockJtaTransaction tx = new MockJtaTransaction();
314
315         ut.getStatus();
316         utControl.setReturnValue(Status.STATUS_ACTIVE, 1);
317         ut.setRollbackOnly();
318         utControl.setVoidCallable(1);
319         tm.getTransaction();
320         tmControl.setReturnValue(tx, 1);
321
322         utControl.replay();
323         tmControl.replay();
324
325         MockControl synchControl = MockControl.createControl(TransactionSynchronization.class);
326         final TransactionSynchronization synch = (TransactionSynchronization) synchControl.getMock();
327         synch.beforeCompletion();
328         synchControl.setVoidCallable(1);
329         synch.afterCompletion(TransactionSynchronization.STATUS_ROLLED_BACK);
330         synchControl.setVoidCallable(1);
331         synchControl.replay();
332
333         JtaTransactionManager ptm = new JtaTransactionManager(ut, tm);
334         TransactionTemplate tt = new TransactionTemplate(ptm);
335         assertTrue(!TransactionSynchronizationManager.isSynchronizationActive());
336         tt.execute(new TransactionCallbackWithoutResult() {
337             protected void doInTransactionWithoutResult(TransactionStatus status) {
338                 assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
339                 TransactionSynchronizationManager.registerSynchronization(synch);
340                 status.setRollbackOnly();
341             }
342         });
343         assertTrue(!TransactionSynchronizationManager.isSynchronizationActive());
344         assertNotNull(tx.getSynchronization());
345         tx.getSynchronization().beforeCompletion();
346         tx.getSynchronization().afterCompletion(Status.STATUS_ROLLEDBACK);
347
348         utControl.verify();
349         tmControl.verify();
350         synchControl.verify();
351     }
352
353     public void testJtaTransactionManagerWithExistingTransactionAndSynchronizationOnActual() throws Exception JavaDoc {
354         MockControl utControl = MockControl.createControl(UserTransaction JavaDoc.class);
355         UserTransaction JavaDoc ut = (UserTransaction JavaDoc) utControl.getMock();
356         ut.getStatus();
357         utControl.setReturnValue(Status.STATUS_ACTIVE, 1);
358         ut.setRollbackOnly();
359         utControl.setVoidCallable(1);
360         utControl.replay();
361
362         MockControl synchControl = MockControl.createControl(TransactionSynchronization.class);
363         final TransactionSynchronization synch = (TransactionSynchronization) synchControl.getMock();
364         synch.beforeCompletion();
365         synchControl.setVoidCallable(1);
366         synch.afterCompletion(TransactionSynchronization.STATUS_UNKNOWN);
367         synchControl.setVoidCallable(1);
368         synchControl.replay();
369
370         JtaTransactionManager ptm = new JtaTransactionManager(ut);
371         TransactionTemplate tt = new TransactionTemplate(ptm);
372         ptm.setTransactionSynchronization(JtaTransactionManager.SYNCHRONIZATION_ON_ACTUAL_TRANSACTION);
373         assertTrue(!TransactionSynchronizationManager.isSynchronizationActive());
374         tt.execute(new TransactionCallbackWithoutResult() {
375             protected void doInTransactionWithoutResult(TransactionStatus status) {
376                 assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
377                 TransactionSynchronizationManager.registerSynchronization(synch);
378                 status.setRollbackOnly();
379             }
380         });
381         assertTrue(!TransactionSynchronizationManager.isSynchronizationActive());
382
383         utControl.verify();
384         synchControl.verify();
385     }
386
387     public void testJtaTransactionManagerWithExistingTransactionAndSynchronizationNever() throws Exception JavaDoc {
388         MockControl utControl = MockControl.createControl(UserTransaction JavaDoc.class);
389         UserTransaction JavaDoc ut = (UserTransaction JavaDoc) utControl.getMock();
390         ut.getStatus();
391         utControl.setReturnValue(Status.STATUS_ACTIVE, 1);
392         ut.setRollbackOnly();
393         utControl.setVoidCallable(1);
394         utControl.replay();
395
396         JtaTransactionManager ptm = new JtaTransactionManager(ut);
397         TransactionTemplate tt = new TransactionTemplate(ptm);
398         ptm.setTransactionSynchronization(JtaTransactionManager.SYNCHRONIZATION_NEVER);
399         assertTrue(!TransactionSynchronizationManager.isSynchronizationActive());
400         tt.execute(new TransactionCallbackWithoutResult() {
401             protected void doInTransactionWithoutResult(TransactionStatus status) {
402                 assertTrue(!TransactionSynchronizationManager.isSynchronizationActive());
403                 status.setRollbackOnly();
404             }
405         });
406         assertTrue(!TransactionSynchronizationManager.isSynchronizationActive());
407
408         utControl.verify();
409     }
410
411     public void testJtaTransactionManagerWithExistingAndPropagationSupports() throws Exception JavaDoc {
412         MockControl utControl = MockControl.createControl(UserTransaction JavaDoc.class);
413         UserTransaction JavaDoc ut = (UserTransaction JavaDoc) utControl.getMock();
414         ut.getStatus();
415         utControl.setReturnValue(Status.STATUS_ACTIVE, 1);
416         ut.setRollbackOnly();
417         utControl.setVoidCallable(1);
418         utControl.replay();
419
420         MockControl synchControl = MockControl.createControl(TransactionSynchronization.class);
421         final TransactionSynchronization synch = (TransactionSynchronization) synchControl.getMock();
422         synch.beforeCompletion();
423         synchControl.setVoidCallable(1);
424         synch.afterCompletion(TransactionSynchronization.STATUS_UNKNOWN);
425         synchControl.setVoidCallable(1);
426         synchControl.replay();
427
428         JtaTransactionManager ptm = new JtaTransactionManager(ut);
429         TransactionTemplate tt = new TransactionTemplate(ptm);
430         tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_SUPPORTS);
431         assertTrue(!TransactionSynchronizationManager.isSynchronizationActive());
432         tt.execute(new TransactionCallbackWithoutResult() {
433             protected void doInTransactionWithoutResult(TransactionStatus status) {
434                 assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
435                 TransactionSynchronizationManager.registerSynchronization(synch);
436                 status.setRollbackOnly();
437             }
438         });
439         assertTrue(!TransactionSynchronizationManager.isSynchronizationActive());
440
441         utControl.verify();
442         synchControl.verify();
443     }
444
445     public void testJtaTransactionManagerWithPropagationSupports() throws Exception JavaDoc {
446         MockControl utControl = MockControl.createControl(UserTransaction JavaDoc.class);
447         UserTransaction JavaDoc ut = (UserTransaction JavaDoc) utControl.getMock();
448         ut.getStatus();
449         utControl.setReturnValue(Status.STATUS_NO_TRANSACTION, 1);
450         utControl.replay();
451
452         MockControl synchControl = MockControl.createControl(TransactionSynchronization.class);
453         final TransactionSynchronization synch = (TransactionSynchronization) synchControl.getMock();
454         synch.beforeCompletion();
455         synchControl.setVoidCallable(1);
456         synch.afterCompletion(TransactionSynchronization.STATUS_ROLLED_BACK);
457         synchControl.setVoidCallable(1);
458         synchControl.replay();
459
460         JtaTransactionManager ptm = new JtaTransactionManager(ut);
461         TransactionTemplate tt = new TransactionTemplate(ptm);
462         tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_SUPPORTS);
463         assertTrue(!TransactionSynchronizationManager.isSynchronizationActive());
464         tt.execute(new TransactionCallbackWithoutResult() {
465             protected void doInTransactionWithoutResult(TransactionStatus status) {
466                 assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
467                 TransactionSynchronizationManager.registerSynchronization(synch);
468                 status.setRollbackOnly();
469             }
470         });
471         assertTrue(!TransactionSynchronizationManager.isSynchronizationActive());
472
473         utControl.verify();
474         synchControl.verify();
475     }
476
477     public void testJtaTransactionManagerWithPropagationSupportsAndSynchronizationOnActual() throws Exception JavaDoc {
478         MockControl utControl = MockControl.createControl(UserTransaction JavaDoc.class);
479         UserTransaction JavaDoc ut = (UserTransaction JavaDoc) utControl.getMock();
480         ut.getStatus();
481         utControl.setReturnValue(Status.STATUS_NO_TRANSACTION, 1);
482         utControl.replay();
483
484         JtaTransactionManager ptm = new JtaTransactionManager(ut);
485         TransactionTemplate tt = new TransactionTemplate(ptm);
486         ptm.setTransactionSynchronization(JtaTransactionManager.SYNCHRONIZATION_ON_ACTUAL_TRANSACTION);
487         tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_SUPPORTS);
488         assertTrue(!TransactionSynchronizationManager.isSynchronizationActive());
489         tt.execute(new TransactionCallbackWithoutResult() {
490             protected void doInTransactionWithoutResult(TransactionStatus status) {
491                 assertTrue(!TransactionSynchronizationManager.isSynchronizationActive());
492                 status.setRollbackOnly();
493             }
494         });
495         assertTrue(!TransactionSynchronizationManager.isSynchronizationActive());
496
497         utControl.verify();
498     }
499
500     public void testJtaTransactionManagerWithPropagationSupportsAndSynchronizationNever() throws Exception JavaDoc {
501         MockControl utControl = MockControl.createControl(UserTransaction JavaDoc.class);
502         UserTransaction JavaDoc ut = (UserTransaction JavaDoc) utControl.getMock();
503         ut.getStatus();
504         utControl.setReturnValue(Status.STATUS_NO_TRANSACTION, 1);
505         utControl.replay();
506
507         JtaTransactionManager ptm = new JtaTransactionManager(ut);
508         TransactionTemplate tt = new TransactionTemplate(ptm);
509         ptm.setTransactionSynchronization(JtaTransactionManager.SYNCHRONIZATION_NEVER);
510         tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_SUPPORTS);
511         assertTrue(!TransactionSynchronizationManager.isSynchronizationActive());
512         tt.execute(new TransactionCallbackWithoutResult() {
513             protected void doInTransactionWithoutResult(TransactionStatus status) {
514                 assertTrue(!TransactionSynchronizationManager.isSynchronizationActive());
515                 status.setRollbackOnly();
516             }
517         });
518         assertTrue(!TransactionSynchronizationManager.isSynchronizationActive());
519
520         utControl.verify();
521     }
522
523     public void testJtaTransactionManagerWithPropagationNotSupported() throws Exception JavaDoc {
524         MockControl utControl = MockControl.createControl(UserTransaction JavaDoc.class);
525         UserTransaction JavaDoc ut = (UserTransaction JavaDoc) utControl.getMock();
526         MockControl tmControl = MockControl.createControl(TransactionManager JavaDoc.class);
527         TransactionManager JavaDoc tm = (TransactionManager JavaDoc) tmControl.getMock();
528         MockControl txControl = MockControl.createControl(Transaction JavaDoc.class);
529         Transaction JavaDoc tx = (Transaction JavaDoc) txControl.getMock();
530         ut.getStatus();
531         utControl.setReturnValue(Status.STATUS_ACTIVE, 1);
532         tm.suspend();
533         tmControl.setReturnValue(tx, 1);
534         tm.resume(tx);
535         tmControl.setVoidCallable(1);
536         utControl.replay();
537         tmControl.replay();
538
539         JtaTransactionManager ptm = new JtaTransactionManager(ut, tm);
540         TransactionTemplate tt = new TransactionTemplate(ptm);
541         tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_NOT_SUPPORTED);
542         assertTrue(!TransactionSynchronizationManager.isSynchronizationActive());
543         tt.execute(new TransactionCallbackWithoutResult() {
544             protected void doInTransactionWithoutResult(TransactionStatus status) {
545                 assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
546                 status.setRollbackOnly();
547             }
548         });
549         assertTrue(!TransactionSynchronizationManager.isSynchronizationActive());
550
551         utControl.verify();
552         tmControl.verify();
553     }
554
555     public void testJtaTransactionManagerWithPropagationRequiresNew() throws Exception JavaDoc {
556         MockControl utControl = MockControl.createControl(UserTransaction JavaDoc.class);
557         UserTransaction JavaDoc ut = (UserTransaction JavaDoc) utControl.getMock();
558         MockControl tmControl = MockControl.createControl(TransactionManager JavaDoc.class);
559         TransactionManager JavaDoc tm = (TransactionManager JavaDoc) tmControl.getMock();
560         MockControl txControl = MockControl.createControl(Transaction JavaDoc.class);
561         Transaction JavaDoc tx = (Transaction JavaDoc) txControl.getMock();
562         ut.getStatus();
563         utControl.setReturnValue(Status.STATUS_NO_TRANSACTION, 1);
564         ut.begin();
565         utControl.setVoidCallable(1);
566         ut.getStatus();
567         utControl.setReturnValue(Status.STATUS_ACTIVE, 3);
568         tm.suspend();
569         tmControl.setReturnValue(tx, 1);
570         ut.begin();
571         utControl.setVoidCallable(1);
572         ut.commit();
573         utControl.setVoidCallable(1);
574         tm.resume(tx);
575         tmControl.setVoidCallable(1);
576         ut.commit();
577         utControl.setVoidCallable(1);
578         utControl.replay();
579         tmControl.replay();
580
581         final JtaTransactionManager ptm = new JtaTransactionManager(ut, tm);
582         TransactionTemplate tt = new TransactionTemplate(ptm);
583         tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
584         tt.setName("txName");
585
586         assertTrue(!TransactionSynchronizationManager.isSynchronizationActive());
587         tt.execute(new TransactionCallbackWithoutResult() {
588             protected void doInTransactionWithoutResult(TransactionStatus status) {
589                 assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
590                 assertEquals("txName", TransactionSynchronizationManager.getCurrentTransactionName());
591                 assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
592
593                 TransactionTemplate tt2 = new TransactionTemplate(ptm);
594                 tt2.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
595                 tt2.setReadOnly(true);
596                 tt2.setName("txName2");
597                 tt2.execute(new TransactionCallbackWithoutResult() {
598                     protected void doInTransactionWithoutResult(TransactionStatus status) {
599                         assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
600                         assertEquals("txName2", TransactionSynchronizationManager.getCurrentTransactionName());
601                         assertTrue(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
602                     }
603                 });
604
605                 assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
606                 assertEquals("txName", TransactionSynchronizationManager.getCurrentTransactionName());
607                 assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
608             }
609         });
610         assertTrue(!TransactionSynchronizationManager.isSynchronizationActive());
611
612         utControl.verify();
613         tmControl.verify();
614     }
615
616     public void testJtaTransactionManagerWithPropagationRequiresNewAndExisting() throws Exception JavaDoc {
617         MockControl utControl = MockControl.createControl(UserTransaction JavaDoc.class);
618         UserTransaction JavaDoc ut = (UserTransaction JavaDoc) utControl.getMock();
619         MockControl tmControl = MockControl.createControl(TransactionManager JavaDoc.class);
620         TransactionManager JavaDoc tm = (TransactionManager JavaDoc) tmControl.getMock();
621         MockControl txControl = MockControl.createControl(Transaction JavaDoc.class);
622         Transaction JavaDoc tx = (Transaction JavaDoc) txControl.getMock();
623         ut.getStatus();
624         utControl.setReturnValue(Status.STATUS_ACTIVE, 2);
625         tm.suspend();
626         tmControl.setReturnValue(tx, 1);
627         ut.begin();
628         utControl.setVoidCallable(1);
629         ut.commit();
630         utControl.setVoidCallable(1);
631         tm.resume(tx);
632         tmControl.setVoidCallable(1);
633         utControl.replay();
634         tmControl.replay();
635
636         JtaTransactionManager ptm = new JtaTransactionManager(ut, tm);
637         TransactionTemplate tt = new TransactionTemplate(ptm);
638         tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
639         assertTrue(!TransactionSynchronizationManager.isSynchronizationActive());
640         tt.execute(new TransactionCallbackWithoutResult() {
641             protected void doInTransactionWithoutResult(TransactionStatus status) {
642                 assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
643             }
644         });
645         assertTrue(!TransactionSynchronizationManager.isSynchronizationActive());
646
647         utControl.verify();
648         tmControl.verify();
649     }
650
651     public void testJtaTransactionManagerWithPropagationRequiresNewAndAdapter() throws Exception JavaDoc {
652         MockControl tmControl = MockControl.createControl(TransactionManager JavaDoc.class);
653         TransactionManager JavaDoc tm = (TransactionManager JavaDoc) tmControl.getMock();
654         MockControl txControl = MockControl.createControl(Transaction JavaDoc.class);
655         Transaction JavaDoc tx = (Transaction JavaDoc) txControl.getMock();
656         tm.getStatus();
657         tmControl.setReturnValue(Status.STATUS_ACTIVE, 2);
658         tm.suspend();
659         tmControl.setReturnValue(tx, 1);
660         tm.begin();
661         tmControl.setVoidCallable(1);
662         tm.commit();
663         tmControl.setVoidCallable(1);
664         tm.resume(tx);
665         tmControl.setVoidCallable(1);
666         tmControl.replay();
667
668         JtaTransactionManager ptm = new JtaTransactionManager(tm);
669         TransactionTemplate tt = new TransactionTemplate(ptm);
670         tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
671         assertTrue(!TransactionSynchronizationManager.isSynchronizationActive());
672         tt.execute(new TransactionCallbackWithoutResult() {
673             protected void doInTransactionWithoutResult(TransactionStatus status) {
674                 assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
675             }
676         });
677         assertTrue(!TransactionSynchronizationManager.isSynchronizationActive());
678
679         tmControl.verify();
680     }
681
682     public void testJtaTransactionManagerWithPropagationRequiresNewAndSuspensionNotSupported() throws Exception JavaDoc {
683         MockControl utControl = MockControl.createControl(UserTransaction JavaDoc.class);
684         UserTransaction JavaDoc ut = (UserTransaction JavaDoc) utControl.getMock();
685         ut.getStatus();
686         utControl.setReturnValue(Status.STATUS_ACTIVE, 1);
687         utControl.replay();
688
689         JtaTransactionManager ptm = new JtaTransactionManager(ut);
690         TransactionTemplate tt = new TransactionTemplate(ptm);
691         tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
692         assertTrue(!TransactionSynchronizationManager.isSynchronizationActive());
693         try {
694             tt.execute(new TransactionCallbackWithoutResult() {
695                 protected void doInTransactionWithoutResult(TransactionStatus status) {
696                 }
697             });
698             fail("Should have thrown TransactionSuspensionNotSupportedException");
699         }
700         catch (TransactionSuspensionNotSupportedException ex) {
701             // expected
702
}
703         assertTrue(!TransactionSynchronizationManager.isSynchronizationActive());
704
705         utControl.verify();
706     }
707
708     public void testJtaTransactionManagerWithIsolationLevel() throws Exception JavaDoc {
709         MockControl utControl = MockControl.createControl(UserTransaction JavaDoc.class);
710         UserTransaction JavaDoc ut = (UserTransaction JavaDoc) utControl.getMock();
711         ut.getStatus();
712         utControl.setReturnValue(Status.STATUS_NO_TRANSACTION, 1);
713         utControl.replay();
714
715         try {
716             JtaTransactionManager ptm = new JtaTransactionManager(ut);
717             TransactionTemplate tt = new TransactionTemplate(ptm);
718             tt.setIsolationLevel(TransactionDefinition.ISOLATION_SERIALIZABLE);
719             tt.execute(new TransactionCallbackWithoutResult() {
720                 protected void doInTransactionWithoutResult(TransactionStatus status) {
721                     // something transactional
722
}
723             });
724             fail("Should have thrown InvalidIsolationLevelException");
725         }
726         catch (InvalidIsolationLevelException ex) {
727             // expected
728
}
729
730         utControl.verify();
731     }
732
733     public void testJtaTransactionManagerWithSystemExceptionOnIsExisting() throws Exception JavaDoc {
734         MockControl utControl = MockControl.createControl(UserTransaction JavaDoc.class);
735         UserTransaction JavaDoc ut = (UserTransaction JavaDoc) utControl.getMock();
736         ut.getStatus();
737         utControl.setThrowable(new SystemException JavaDoc("system exception"));
738         utControl.replay();
739
740         try {
741             JtaTransactionManager ptm = new JtaTransactionManager(ut);
742             TransactionTemplate tt = new TransactionTemplate(ptm);
743             tt.execute(new TransactionCallbackWithoutResult() {
744                 protected void doInTransactionWithoutResult(TransactionStatus status) {
745                     // something transactional
746
}
747             });
748             fail("Should have thrown TransactionSystemException");
749         }
750         catch (TransactionSystemException ex) {
751             // expected
752
}
753
754         utControl.verify();
755     }
756
757     public void testJtaTransactionManagerWithNestedBegin() throws Exception JavaDoc {
758         MockControl utControl = MockControl.createControl(UserTransaction JavaDoc.class);
759         UserTransaction JavaDoc ut = (UserTransaction JavaDoc) utControl.getMock();
760         ut.getStatus();
761         utControl.setReturnValue(Status.STATUS_ACTIVE, 2);
762         ut.begin();
763         utControl.setVoidCallable(1);
764         ut.commit();
765         utControl.setVoidCallable(1);
766         utControl.replay();
767
768         JtaTransactionManager ptm = new JtaTransactionManager(ut);
769         TransactionTemplate tt = new TransactionTemplate(ptm);
770         tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_NESTED);
771         tt.execute(new TransactionCallbackWithoutResult() {
772             protected void doInTransactionWithoutResult(TransactionStatus status) {
773                 // something transactional
774
}
775         });
776
777         utControl.verify();
778     }
779
780     public void testJtaTransactionManagerWithNotSupportedExceptionOnNestedBegin() throws Exception JavaDoc {
781         MockControl utControl = MockControl.createControl(UserTransaction JavaDoc.class);
782         UserTransaction JavaDoc ut = (UserTransaction JavaDoc) utControl.getMock();
783         ut.getStatus();
784         utControl.setReturnValue(Status.STATUS_ACTIVE, 1);
785         ut.begin();
786         utControl.setThrowable(new NotSupportedException JavaDoc("not supported"));
787         utControl.replay();
788
789         try {
790             JtaTransactionManager ptm = new JtaTransactionManager(ut);
791             TransactionTemplate tt = new TransactionTemplate(ptm);
792             tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_NESTED);
793             tt.execute(new TransactionCallbackWithoutResult() {
794                 protected void doInTransactionWithoutResult(TransactionStatus status) {
795                     // something transactional
796
}
797             });
798             fail("Should have thrown NestedTransactionNotSupportedException");
799         }
800         catch (NestedTransactionNotSupportedException ex) {
801             // expected
802
}
803
804         utControl.verify();
805     }
806
807     public void testJtaTransactionManagerWithUnsupportedOperationExceptionOnNestedBegin() throws Exception JavaDoc {
808         MockControl utControl = MockControl.createControl(UserTransaction JavaDoc.class);
809         UserTransaction JavaDoc ut = (UserTransaction JavaDoc) utControl.getMock();
810         ut.getStatus();
811         utControl.setReturnValue(Status.STATUS_ACTIVE, 1);
812         ut.begin();
813         utControl.setThrowable(new UnsupportedOperationException JavaDoc("not supported"));
814         utControl.replay();
815
816         try {
817             JtaTransactionManager ptm = new JtaTransactionManager(ut);
818             TransactionTemplate tt = new TransactionTemplate(ptm);
819             tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_NESTED);
820             tt.execute(new TransactionCallbackWithoutResult() {
821                 protected void doInTransactionWithoutResult(TransactionStatus status) {
822                     // something transactional
823
}
824             });
825             fail("Should have thrown NestedTransactionNotSupportedException");
826         }
827         catch (NestedTransactionNotSupportedException ex) {
828             // expected
829
}
830
831         utControl.verify();
832     }
833
834     public void testJtaTransactionManagerWithSystemExceptionOnBegin() throws Exception JavaDoc {
835         MockControl utControl = MockControl.createControl(UserTransaction JavaDoc.class);
836         UserTransaction JavaDoc ut = (UserTransaction JavaDoc) utControl.getMock();
837         ut.getStatus();
838         utControl.setReturnValue(Status.STATUS_NO_TRANSACTION, 1);
839         ut.begin();
840         utControl.setThrowable(new SystemException JavaDoc("system exception"));
841         utControl.replay();
842
843         try {
844             JtaTransactionManager ptm = new JtaTransactionManager(ut);
845             TransactionTemplate tt = new TransactionTemplate(ptm);
846             tt.execute(new TransactionCallbackWithoutResult() {
847                 protected void doInTransactionWithoutResult(TransactionStatus status) {
848                     // something transactional
849
}
850             });
851             fail("Should have thrown CannotCreateTransactionException");
852         }
853         catch (CannotCreateTransactionException ex) {
854             // expected
855
}
856
857         utControl.verify();
858     }
859
860     public void testJtaTransactionManagerWithRollbackExceptionOnCommit() throws Exception JavaDoc {
861         MockControl utControl = MockControl.createControl(UserTransaction JavaDoc.class);
862         UserTransaction JavaDoc ut = (UserTransaction JavaDoc) utControl.getMock();
863         ut.getStatus();
864         utControl.setReturnValue(Status.STATUS_NO_TRANSACTION, 1);
865         ut.getStatus();
866         utControl.setReturnValue(Status.STATUS_ACTIVE, 1);
867         ut.begin();
868         utControl.setVoidCallable(1);
869         ut.commit();
870         utControl.setThrowable(new RollbackException JavaDoc("unexpected rollback"));
871         utControl.replay();
872
873         try {
874             JtaTransactionManager ptm = new JtaTransactionManager(ut);
875             TransactionTemplate tt = new TransactionTemplate(ptm);
876             tt.execute(new TransactionCallbackWithoutResult() {
877                 protected void doInTransactionWithoutResult(TransactionStatus status) {
878                     // something transactional
879
TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {
880                         public void afterCompletion(int status) {
881                             assertTrue("Correct completion status", status == TransactionSynchronization.STATUS_ROLLED_BACK);
882                         }
883                     });
884                 }
885             });
886             fail("Should have thrown UnexpectedRollbackException");
887         }
888         catch (UnexpectedRollbackException ex) {
889             // expected
890
}
891
892         utControl.verify();
893     }
894
895     public void testJtaTransactionManagerWithHeuristicMixedExceptionOnCommit() throws Exception JavaDoc {
896         MockControl utControl = MockControl.createControl(UserTransaction JavaDoc.class);
897         UserTransaction JavaDoc ut = (UserTransaction JavaDoc) utControl.getMock();
898         ut.getStatus();
899         utControl.setReturnValue(Status.STATUS_NO_TRANSACTION, 1);
900         ut.getStatus();
901         utControl.setReturnValue(Status.STATUS_ACTIVE, 1);
902         ut.begin();
903         utControl.setVoidCallable(1);
904         ut.commit();
905         utControl.setThrowable(new HeuristicMixedException JavaDoc("heuristic exception"));
906         utControl.replay();
907
908         try {
909             JtaTransactionManager ptm = new JtaTransactionManager(ut);
910             TransactionTemplate tt = new TransactionTemplate(ptm);
911             tt.execute(new TransactionCallbackWithoutResult() {
912                 protected void doInTransactionWithoutResult(TransactionStatus status) {
913                     // something transactional
914
TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {
915                         public void afterCompletion(int status) {
916                             assertTrue("Correct completion status", status == TransactionSynchronization.STATUS_UNKNOWN);
917                         }
918                     });
919                 }
920             });
921             fail("Should have thrown HeuristicCompletionException");
922         }
923         catch (HeuristicCompletionException ex) {
924             // expected
925
assertTrue(ex.getOutcomeState() == HeuristicCompletionException.STATE_MIXED);
926         }
927
928         utControl.verify();
929     }
930
931     public void testJtaTransactionManagerWithHeuristicRollbackExceptionOnCommit() throws Exception JavaDoc {
932         MockControl utControl = MockControl.createControl(UserTransaction JavaDoc.class);
933         UserTransaction JavaDoc ut = (UserTransaction JavaDoc) utControl.getMock();
934         ut.getStatus();
935         utControl.setReturnValue(Status.STATUS_NO_TRANSACTION, 1);
936         ut.getStatus();
937         utControl.setReturnValue(Status.STATUS_ACTIVE, 1);
938         ut.begin();
939         utControl.setVoidCallable(1);
940         ut.commit();
941         utControl.setThrowable(new HeuristicRollbackException JavaDoc("heuristic exception"));
942         utControl.replay();
943
944         try {
945             JtaTransactionManager ptm = new JtaTransactionManager(ut);
946             TransactionTemplate tt = new TransactionTemplate(ptm);
947             tt.execute(new TransactionCallbackWithoutResult() {
948                 protected void doInTransactionWithoutResult(TransactionStatus status) {
949                     // something transactional
950
TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {
951                         public void afterCompletion(int status) {
952                             assertTrue("Correct completion status", status == TransactionSynchronization.STATUS_UNKNOWN);
953                         }
954                     });
955                 }
956             });
957             fail("Should have thrown HeuristicCompletionException");
958         }
959         catch (HeuristicCompletionException ex) {
960             // expected
961
assertTrue(ex.getOutcomeState() == HeuristicCompletionException.STATE_ROLLED_BACK);
962         }
963
964         utControl.verify();
965     }
966
967     public void testJtaTransactionManagerWithSystemExceptionOnCommit() throws Exception JavaDoc {
968         MockControl utControl = MockControl.createControl(UserTransaction JavaDoc.class);
969         UserTransaction JavaDoc ut = (UserTransaction JavaDoc) utControl.getMock();
970         ut.getStatus();
971         utControl.setReturnValue(Status.STATUS_NO_TRANSACTION, 1);
972         ut.getStatus();
973         utControl.setReturnValue(Status.STATUS_ACTIVE, 1);
974         ut.begin();
975         utControl.setVoidCallable(1);
976         ut.commit();
977         utControl.setThrowable(new SystemException JavaDoc("system exception"));
978         utControl.replay();
979
980         try {
981             JtaTransactionManager ptm = new JtaTransactionManager(ut);
982             TransactionTemplate tt = new TransactionTemplate(ptm);
983             tt.execute(new TransactionCallbackWithoutResult() {
984                 protected void doInTransactionWithoutResult(TransactionStatus status) {
985                     // something transactional
986
TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {
987                         public void afterCompletion(int status) {
988                             assertTrue("Correct completion status", status == TransactionSynchronization.STATUS_UNKNOWN);
989                         }
990                     });
991                 }
992             });
993             fail("Should have thrown TransactionSystemException");
994         }
995         catch (TransactionSystemException ex) {
996             // expected
997
}
998
999         utControl.verify();
1000    }
1001
1002    public void testJtaTransactionManagerWithSystemExceptionOnRollback() throws Exception JavaDoc {
1003        MockControl utControl = MockControl.createControl(UserTransaction JavaDoc.class);
1004        UserTransaction JavaDoc ut = (UserTransaction JavaDoc) utControl.getMock();
1005        ut.getStatus();
1006        utControl.setReturnValue(Status.STATUS_NO_TRANSACTION, 1);
1007        ut.begin();
1008        utControl.setVoidCallable(1);
1009        ut.rollback();
1010        utControl.setThrowable(new SystemException JavaDoc("system exception"));
1011        utControl.replay();
1012
1013        try {
1014            JtaTransactionManager ptm = new JtaTransactionManager(ut);
1015            TransactionTemplate tt = new TransactionTemplate(ptm);
1016            tt.execute(new TransactionCallbackWithoutResult() {
1017                protected void doInTransactionWithoutResult(TransactionStatus status) {
1018                    TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {
1019                        public void afterCompletion(int status) {
1020                            assertTrue("Correct completion status", status == TransactionSynchronization.STATUS_UNKNOWN);
1021                        }
1022                    });
1023                    status.setRollbackOnly();
1024                }
1025            });
1026            fail("Should have thrown TransactionSystemException");
1027        }
1028        catch (TransactionSystemException ex) {
1029            // expected
1030
}
1031
1032        utControl.verify();
1033    }
1034
1035    public void testJtaTransactionManagerWithIllegalStateExceptionOnRollbackOnly() throws Exception JavaDoc {
1036        MockControl utControl = MockControl.createControl(UserTransaction JavaDoc.class);
1037        UserTransaction JavaDoc ut = (UserTransaction JavaDoc) utControl.getMock();
1038        ut.getStatus();
1039        utControl.setReturnValue(Status.STATUS_ACTIVE, 1);
1040        ut.setRollbackOnly();
1041        utControl.setThrowable(new IllegalStateException JavaDoc("no existing transaction"));
1042        utControl.replay();
1043
1044        try {
1045            JtaTransactionManager ptm = new JtaTransactionManager(ut);
1046            TransactionTemplate tt = new TransactionTemplate(ptm);
1047            tt.execute(new TransactionCallbackWithoutResult() {
1048                protected void doInTransactionWithoutResult(TransactionStatus status) {
1049                    status.setRollbackOnly();
1050                }
1051            });
1052            fail("Should have thrown NoTransactionException");
1053        }
1054        catch (NoTransactionException ex) {
1055            // expected
1056
}
1057
1058        utControl.verify();
1059    }
1060
1061    public void testJtaTransactionManagerWithSystemExceptionOnRollbackOnly() throws Exception JavaDoc {
1062        MockControl utControl = MockControl.createControl(UserTransaction JavaDoc.class);
1063        UserTransaction JavaDoc ut = (UserTransaction JavaDoc) utControl.getMock();
1064        ut.getStatus();
1065        utControl.setReturnValue(Status.STATUS_ACTIVE, 1);
1066        ut.setRollbackOnly();
1067        utControl.setThrowable(new SystemException JavaDoc("system exception"));
1068        utControl.replay();
1069
1070        try {
1071            JtaTransactionManager ptm = new JtaTransactionManager(ut);
1072            TransactionTemplate tt = new TransactionTemplate(ptm);
1073            tt.execute(new TransactionCallbackWithoutResult() {
1074                protected void doInTransactionWithoutResult(TransactionStatus status) {
1075                    status.setRollbackOnly();
1076                    TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {
1077                        public void afterCompletion(int status) {
1078                            assertTrue("Correct completion status", status == TransactionSynchronization.STATUS_UNKNOWN);
1079                        }
1080                    });
1081                }
1082            });
1083            fail("Should have thrown TransactionSystemException");
1084        }
1085        catch (TransactionSystemException ex) {
1086            // expected
1087
}
1088
1089        utControl.verify();
1090    }
1091
1092    public void testJtaTransactionManagerWithDoubleCommit() throws Exception JavaDoc {
1093        MockControl utControl = MockControl.createControl(UserTransaction JavaDoc.class);
1094        UserTransaction JavaDoc ut = (UserTransaction JavaDoc) utControl.getMock();
1095        ut.getStatus();
1096        utControl.setReturnValue(Status.STATUS_NO_TRANSACTION, 1);
1097        ut.getStatus();
1098        utControl.setReturnValue(Status.STATUS_ACTIVE, 1);
1099        ut.begin();
1100        utControl.setVoidCallable(1);
1101        ut.commit();
1102        utControl.setVoidCallable(1);
1103        utControl.replay();
1104
1105        JtaTransactionManager ptm = new JtaTransactionManager(ut);
1106        assertTrue(!TransactionSynchronizationManager.isSynchronizationActive());
1107        TransactionStatus status = ptm.getTransaction(new DefaultTransactionDefinition());
1108        assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
1109        // first commit
1110
ptm.commit(status);
1111        assertTrue(!TransactionSynchronizationManager.isSynchronizationActive());
1112        try {
1113            // second commit attempt
1114
ptm.commit(status);
1115            fail("Should have thrown IllegalTransactionStateException");
1116        }
1117        catch (IllegalTransactionStateException ex) {
1118            // expected
1119
}
1120
1121        utControl.verify();
1122    }
1123
1124    public void testJtaTransactionManagerWithDoubleRollback() throws Exception JavaDoc {
1125        MockControl utControl = MockControl.createControl(UserTransaction JavaDoc.class);
1126        UserTransaction JavaDoc ut = (UserTransaction JavaDoc) utControl.getMock();
1127        ut.getStatus();
1128        utControl.setReturnValue(Status.STATUS_NO_TRANSACTION, 1);
1129        ut.begin();
1130        utControl.setVoidCallable(1);
1131        ut.rollback();
1132        utControl.setVoidCallable(1);
1133        utControl.replay();
1134
1135        JtaTransactionManager ptm = new JtaTransactionManager(ut);
1136        assertTrue(!TransactionSynchronizationManager.isSynchronizationActive());
1137        TransactionStatus status = ptm.getTransaction(new DefaultTransactionDefinition());
1138        assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
1139        // first rollback
1140
ptm.rollback(status);
1141        assertTrue(!TransactionSynchronizationManager.isSynchronizationActive());
1142        try {
1143            // second rollback attempt
1144
ptm.rollback(status);
1145            fail("Should have thrown IllegalTransactionStateException");
1146        }
1147        catch (IllegalTransactionStateException ex) {
1148            // expected
1149
}
1150
1151        utControl.verify();
1152    }
1153
1154    public void testJtaTransactionManagerWithRollbackAndCommit() throws Exception JavaDoc {
1155        MockControl utControl = MockControl.createControl(UserTransaction JavaDoc.class);
1156        UserTransaction JavaDoc ut = (UserTransaction JavaDoc) utControl.getMock();
1157        ut.getStatus();
1158        utControl.setReturnValue(Status.STATUS_NO_TRANSACTION, 1);
1159        ut.begin();
1160        utControl.setVoidCallable(1);
1161        ut.rollback();
1162        utControl.setVoidCallable(1);
1163        utControl.replay();
1164
1165        JtaTransactionManager ptm = new JtaTransactionManager(ut);
1166        assertTrue(!TransactionSynchronizationManager.isSynchronizationActive());
1167        TransactionStatus status = ptm.getTransaction(new DefaultTransactionDefinition());
1168        assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
1169        // first: rollback
1170
ptm.rollback(status);
1171        assertTrue(!TransactionSynchronizationManager.isSynchronizationActive());
1172        try {
1173            // second: commit attempt
1174
ptm.commit(status);
1175            fail("Should have thrown IllegalTransactionStateException");
1176        }
1177        catch (IllegalTransactionStateException ex) {
1178            // expected
1179
}
1180
1181        utControl.verify();
1182    }
1183
1184    /**
1185     * Prevent any side-effects due to this test modifying ThreadLocals that might
1186     * affect subsequent tests when all tests are run in the same JVM, as with Eclipse.
1187     */

1188    protected void tearDown() {
1189        assertTrue(TransactionSynchronizationManager.getResourceMap().isEmpty());
1190        assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
1191        assertNull(TransactionSynchronizationManager.getCurrentTransactionName());
1192        assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
1193        assertFalse(TransactionSynchronizationManager.isActualTransactionActive());
1194    }
1195
1196}
1197
Popular Tags