KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > transaction > jta11 > TransactionSynchronizationRegistryTest


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.geronimo.transaction.jta11;
19
20 import javax.transaction.Synchronization JavaDoc;
21 import javax.transaction.HeuristicMixedException JavaDoc;
22 import javax.transaction.HeuristicRollbackException JavaDoc;
23 import javax.transaction.RollbackException JavaDoc;
24 import javax.transaction.SystemException JavaDoc;
25 import javax.transaction.NotSupportedException JavaDoc;
26
27 import junit.framework.TestCase;
28
29 /**
30  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
31  */

32 public class TransactionSynchronizationRegistryTest extends TestCase {
33
34     private static int beforeCounter = 0;
35     private static int afterCounter = 0;
36
37
38
39     private GeronimoTransactionManagerJTA11 tm;
40
41     private CountingSync interposedSync;
42     private CountingSync normalSync;
43
44     protected void setUp() throws Exception JavaDoc {
45         tm = new GeronimoTransactionManagerJTA11();
46     }
47
48     private void setUpInterposedSync() throws NotSupportedException JavaDoc, SystemException JavaDoc {
49         interposedSync = new CountingSync();
50         tm.begin();
51         tm.registerInterposedSynchronization(interposedSync);
52     }
53
54     private void setUpSyncs() throws Exception JavaDoc {
55         normalSync = new CountingSync();
56         setUpInterposedSync();
57         tm.getTransaction().registerSynchronization(normalSync);
58     }
59
60
61     public void testInterposedSynchIsCalledOnCommit() throws Exception JavaDoc {
62         setUpInterposedSync();
63         tm.commit();
64         checkInterposedSyncCalled();
65     }
66
67     private void checkInterposedSyncCalled() {
68         assertTrue("interposedSync beforeCompletion was not called", interposedSync.getBeforeCount() != -1);
69         assertTrue("interposedSync afterCompletion was not called", interposedSync.getAfterCount() != -1);
70     }
71
72     public void testInterposedSynchIsCalledOnRollback() throws Exception JavaDoc {
73         setUpInterposedSync();
74         tm.rollback();
75         checkInterposedSyncCalled();
76     }
77
78     public void testInterposedSynchIsCalledOnMarkRollback() throws Exception JavaDoc {
79         setUpInterposedSync();
80         tm.setRollbackOnly();
81         try {
82             tm.commit();
83             fail("expected a RollbackException");
84         } catch (HeuristicMixedException JavaDoc e) {
85             fail("expected a RollbackException not " + e.getClass());
86         } catch (HeuristicRollbackException JavaDoc e) {
87             fail("expected a RollbackException not " + e.getClass());
88         } catch (IllegalStateException JavaDoc e) {
89             fail("expected a RollbackException not " + e.getClass());
90         } catch (RollbackException JavaDoc e) {
91
92         } catch (SecurityException JavaDoc e) {
93             fail("expected a RollbackException not " + e.getClass());
94         } catch (SystemException JavaDoc e) {
95             fail("expected a RollbackException not " + e.getClass());
96         }
97         checkInterposedSyncCalled();
98     }
99
100     public void testSynchCallOrderOnCommit() throws Exception JavaDoc {
101         setUpSyncs();
102         tm.commit();
103         checkSyncCallOrder();
104     }
105
106     private void checkSyncCallOrder() {
107         checkInterposedSyncCalled();
108         assertTrue("interposedSync beforeCompletion was not called after normalSync beforeCompletion", interposedSync.getBeforeCount() > normalSync.getBeforeCount());
109         assertTrue("interposedSync afterCompletion was not called before normalSync beforeCompletion", interposedSync.getAfterCount() < normalSync.getAfterCount());
110     }
111
112     public void testSynchCallOrderOnRollback() throws Exception JavaDoc {
113         setUpSyncs();
114         tm.rollback();
115         checkSyncCallOrder();
116     }
117
118     public void testSynchCallOrderOnMarkRollback() throws Exception JavaDoc {
119         setUpSyncs();
120         tm.setRollbackOnly();
121         try {
122             tm.commit();
123             fail("expected a RollbackException");
124         } catch (HeuristicMixedException JavaDoc e) {
125             fail("expected a RollbackException not " + e.getClass());
126         } catch (HeuristicRollbackException JavaDoc e) {
127             fail("expected a RollbackException not " + e.getClass());
128         } catch (IllegalStateException JavaDoc e) {
129             fail("expected a RollbackException not " + e.getClass());
130         } catch (RollbackException JavaDoc e) {
131
132         } catch (SecurityException JavaDoc e) {
133             fail("expected a RollbackException not " + e.getClass());
134         } catch (SystemException JavaDoc e) {
135             fail("expected a RollbackException not " + e.getClass());
136         }
137         checkSyncCallOrder();
138     }
139
140     private class CountingSync implements Synchronization JavaDoc {
141
142         private int beforeCount = -1;
143         private int afterCount = -1;
144
145         public void beforeCompletion() {
146             beforeCount = beforeCounter++;
147         }
148
149         public void afterCompletion(int i) {
150             afterCount = afterCounter++;
151         }
152
153         public int getBeforeCount() {
154             return beforeCount;
155         }
156
157         public int getAfterCount() {
158             return afterCount;
159         }
160     }
161
162 }
163
Popular Tags