KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > transaction > manager > TransactionManagerImplTest


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.manager;
19
20 import java.util.ArrayList JavaDoc;
21 import java.util.Map JavaDoc;
22 import javax.management.ObjectName JavaDoc;
23 import javax.transaction.RollbackException JavaDoc;
24 import javax.transaction.Status JavaDoc;
25 import javax.transaction.Transaction JavaDoc;
26 import javax.transaction.xa.XAResource JavaDoc;
27 import javax.transaction.xa.Xid JavaDoc;
28
29 import junit.framework.TestCase;
30 import org.apache.geronimo.gbean.ReferenceCollection;
31 import org.apache.geronimo.gbean.ReferenceCollectionEvent;
32 import org.apache.geronimo.gbean.ReferenceCollectionListener;
33
34 /**
35  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
36  */

37 public class TransactionManagerImplTest extends TestCase {
38
39     MockResourceManager rm1 = new MockResourceManager(true);
40     MockResource r1_1 = rm1.getResource("rm1_1");
41     MockResource r1_2 = rm1.getResource("rm1_2");
42     MockResourceManager rm2 = new MockResourceManager(true);
43     MockResource r2_1 = rm2.getResource("rm2_1");
44     MockResource r2_2 = rm2.getResource("rm2_2");
45
46     TransactionLog transactionLog = new MockLog();
47
48     ReferenceCollection resourceManagers = new TestReferenceCollection();
49     TransactionManagerImpl tm;
50
51     protected void setUp() throws Exception JavaDoc {
52         tm = new TransactionManagerImplGBean(10,
53                 new XidFactoryImpl("WHAT DO WE CALL IT?".getBytes()), transactionLog, resourceManagers);
54     }
55
56     protected void tearDown() throws Exception JavaDoc {
57         tm = null;
58     }
59
60     public void testNoResourcesCommit() throws Exception JavaDoc {
61         assertEquals(Status.STATUS_NO_TRANSACTION, tm.getStatus());
62         tm.begin();
63         assertEquals(Status.STATUS_ACTIVE, tm.getStatus());
64         tm.commit();
65         assertNull(tm.getTransaction());
66         assertEquals(Status.STATUS_NO_TRANSACTION, tm.getStatus());
67         tm.begin();
68         assertEquals(Status.STATUS_ACTIVE, tm.getStatus());
69         Transaction JavaDoc tx = tm.getTransaction();
70         assertNotNull(tx);
71         tx.commit();
72         assertNotNull(tm.getTransaction());
73         assertEquals(Status.STATUS_NO_TRANSACTION, tm.getStatus());
74     }
75
76     public void testNoResourcesMarkRollbackOnly() throws Exception JavaDoc {
77         assertEquals(Status.STATUS_NO_TRANSACTION, tm.getStatus());
78         tm.begin();
79         assertEquals(Status.STATUS_ACTIVE, tm.getStatus());
80         tm.getTransaction().setRollbackOnly();
81         assertEquals(Status.STATUS_MARKED_ROLLBACK, tm.getStatus());
82         try {
83             tm.commit();
84             fail("tx should not commit");
85         } catch (RollbackException JavaDoc e) {
86             //expected
87
}
88         assertNull(tm.getTransaction());
89         assertEquals(Status.STATUS_NO_TRANSACTION, tm.getStatus());
90         tm.begin();
91         assertEquals(Status.STATUS_ACTIVE, tm.getStatus());
92         Transaction JavaDoc tx = tm.getTransaction();
93         assertNotNull(tx);
94         tx.setRollbackOnly();
95         assertEquals(Status.STATUS_MARKED_ROLLBACK, tx.getStatus());
96         try {
97             tx.commit();
98             fail("tx should not commit");
99         } catch (RollbackException JavaDoc e) {
100             //expected
101
}
102         assertNotNull(tm.getTransaction());
103         assertEquals(Status.STATUS_NO_TRANSACTION, tm.getStatus());
104     }
105
106     public void testNoResourcesRollback() throws Exception JavaDoc {
107         assertEquals(Status.STATUS_NO_TRANSACTION, tm.getStatus());
108         tm.begin();
109         assertEquals(Status.STATUS_ACTIVE, tm.getStatus());
110         tm.rollback();
111         assertNull(tm.getTransaction());
112         assertEquals(Status.STATUS_NO_TRANSACTION, tm.getStatus());
113         tm.begin();
114         assertEquals(Status.STATUS_ACTIVE, tm.getStatus());
115         Transaction JavaDoc tx = tm.getTransaction();
116         assertNotNull(tx);
117         tx.rollback();
118         assertNotNull(tm.getTransaction());
119         assertEquals(Status.STATUS_NO_TRANSACTION, tm.getStatus());
120
121         //check rollback when marked rollback only
122
tm.begin();
123         assertEquals(Status.STATUS_ACTIVE, tm.getStatus());
124         tm.getTransaction().setRollbackOnly();
125         assertEquals(Status.STATUS_MARKED_ROLLBACK, tm.getStatus());
126         tm.rollback();
127         assertNull(tm.getTransaction());
128         assertEquals(Status.STATUS_NO_TRANSACTION, tm.getStatus());
129     }
130
131     public void testOneResourceCommit() throws Exception JavaDoc {
132         assertEquals(Status.STATUS_NO_TRANSACTION, tm.getStatus());
133         tm.begin();
134         Transaction JavaDoc tx = tm.getTransaction();
135         tx.enlistResource(r1_1);
136         tx.delistResource(r1_1, XAResource.TMSUCCESS);
137         tx.commit();
138         assertEquals(Status.STATUS_NO_TRANSACTION, tm.getStatus());
139         assertTrue(r1_1.isCommitted());
140         assertTrue(!r1_1.isPrepared());
141         assertTrue(!r1_1.isRolledback());
142     }
143
144     public void testOneResourceMarkedRollback() throws Exception JavaDoc {
145         assertEquals(Status.STATUS_NO_TRANSACTION, tm.getStatus());
146         tm.begin();
147         Transaction JavaDoc tx = tm.getTransaction();
148         tx.enlistResource(r1_1);
149         tx.setRollbackOnly();
150         tx.delistResource(r1_1, XAResource.TMSUCCESS);
151         try {
152             tx.commit();
153             fail("tx should roll back");
154         } catch (RollbackException JavaDoc e) {
155             //expected
156
}
157         assertEquals(Status.STATUS_NO_TRANSACTION, tm.getStatus());
158         assertTrue(!r1_1.isCommitted());
159         assertTrue(!r1_1.isPrepared());
160         assertTrue(r1_1.isRolledback());
161     }
162
163     public void testOneResourceRollback() throws Exception JavaDoc {
164         assertEquals(Status.STATUS_NO_TRANSACTION, tm.getStatus());
165         tm.begin();
166         Transaction JavaDoc tx = tm.getTransaction();
167         tx.enlistResource(r1_1);
168         tx.delistResource(r1_1, XAResource.TMSUCCESS);
169         tx.rollback();
170         assertEquals(Status.STATUS_NO_TRANSACTION, tm.getStatus());
171         assertTrue(!r1_1.isCommitted());
172         assertTrue(!r1_1.isPrepared());
173         assertTrue(r1_1.isRolledback());
174     }
175
176     public void testTwoResourceOneRMCommit() throws Exception JavaDoc {
177         assertEquals(Status.STATUS_NO_TRANSACTION, tm.getStatus());
178         tm.begin();
179         Transaction JavaDoc tx = tm.getTransaction();
180         tx.enlistResource(r1_1);
181         tx.delistResource(r1_1, XAResource.TMSUCCESS);
182         tx.enlistResource(r1_2);
183         tx.delistResource(r1_2, XAResource.TMSUCCESS);
184         tx.commit();
185         assertEquals(Status.STATUS_NO_TRANSACTION, tm.getStatus());
186         assertTrue(r1_1.isCommitted() ^ r1_2.isCommitted());
187         assertTrue(!r1_1.isPrepared() & !r1_2.isPrepared());
188         assertTrue(!r1_1.isRolledback() & !r1_2.isRolledback());
189     }
190
191     public void testTwoResourceOneRMMarkRollback() throws Exception JavaDoc {
192         assertEquals(Status.STATUS_NO_TRANSACTION, tm.getStatus());
193         tm.begin();
194         Transaction JavaDoc tx = tm.getTransaction();
195         tx.enlistResource(r1_1);
196         tx.delistResource(r1_1, XAResource.TMSUCCESS);
197         tx.enlistResource(r1_2);
198         tx.delistResource(r1_2, XAResource.TMSUCCESS);
199         tx.setRollbackOnly();
200         try {
201             tx.commit();
202             fail("tx should roll back");
203         } catch (RollbackException JavaDoc e) {
204             //expected
205
}
206         assertEquals(Status.STATUS_NO_TRANSACTION, tm.getStatus());
207         assertTrue(!r1_1.isCommitted() & !r1_2.isCommitted());
208         assertTrue(!r1_1.isPrepared() & !r1_2.isPrepared());
209         assertTrue(r1_1.isRolledback() ^ r1_2.isRolledback());
210     }
211
212     public void testTwoResourcesOneRMRollback() throws Exception JavaDoc {
213         tm.begin();
214         Transaction JavaDoc tx = tm.getTransaction();
215         tx.enlistResource(r1_1);
216         tx.delistResource(r1_1, XAResource.TMSUCCESS);
217         tx.enlistResource(r1_2);
218         tx.delistResource(r1_2, XAResource.TMSUCCESS);
219         tx.setRollbackOnly();
220         tx.rollback();
221         assertEquals(Status.STATUS_NO_TRANSACTION, tm.getStatus());
222         assertTrue(!r1_1.isCommitted() & !r1_2.isCommitted());
223         assertTrue(!r1_1.isPrepared() & !r1_2.isPrepared());
224         assertTrue(r1_1.isRolledback() ^ r1_2.isRolledback());
225     }
226
227     public void testFourResourceTwoRMCommit() throws Exception JavaDoc {
228         assertEquals(Status.STATUS_NO_TRANSACTION, tm.getStatus());
229         tm.begin();
230         Transaction JavaDoc tx = tm.getTransaction();
231         tx.enlistResource(r1_1);
232         tx.enlistResource(r1_2);
233         tx.enlistResource(r2_1);
234         tx.enlistResource(r2_2);
235         tx.delistResource(r1_1, XAResource.TMSUCCESS);
236         tx.delistResource(r1_2, XAResource.TMSUCCESS);
237         tx.delistResource(r2_1, XAResource.TMSUCCESS);
238         tx.delistResource(r2_2, XAResource.TMSUCCESS);
239         tx.commit();
240         assertEquals(Status.STATUS_NO_TRANSACTION, tm.getStatus());
241         assertTrue((r1_1.isCommitted() & r1_1.isPrepared()) ^ (r1_2.isCommitted() & r1_2.isPrepared()));
242         assertTrue(!r1_1.isRolledback() & !r1_2.isRolledback());
243         assertTrue((r2_1.isCommitted() & r2_1.isPrepared()) ^ (r2_2.isCommitted() & r2_2.isPrepared()));
244         assertTrue(!r2_1.isRolledback() & !r2_2.isRolledback());
245     }
246
247     //BE VERY CAREFUL!! the ResourceManager only "recovers" the LAST resource it creates.
248
//This test depends on using the resource that will be recovered by the resource manager.
249
public void testSimpleRecovery() throws Exception JavaDoc {
250         //create a transaction in our own transaction manager
251
Xid JavaDoc xid = tm.xidFactory.createXid();
252         Transaction JavaDoc tx = tm.importXid(xid, 0);
253         tm.resume(tx);
254         assertSame(tx, tm.getTransaction());
255         tx.enlistResource(r1_2);
256         tx.enlistResource(r2_2);
257         tx.delistResource(r1_2, XAResource.TMSUCCESS);
258         tx.delistResource(r2_2, XAResource.TMSUCCESS);
259         tm.suspend();
260         tm.prepare(tx);
261         //recover
262
tm.recovery.recoverLog();
263         resourceManagers.add(rm1);
264         assertTrue(r1_2.isCommitted());
265         assertTrue(!r2_2.isCommitted());
266         resourceManagers.add(rm2);
267         assertTrue(r2_2.isCommitted());
268         assertTrue(tm.recovery.localRecoveryComplete());
269     }
270
271     public void testImportedXidRecovery() throws Exception JavaDoc {
272         //create a transaction from an external transaction manager.
273
XidFactory xidFactory2 = new XidFactoryImpl("tm2".getBytes());
274         Xid JavaDoc xid = xidFactory2.createXid();
275         Transaction JavaDoc tx = tm.importXid(xid, 0);
276         tm.resume(tx);
277         assertSame(tx, tm.getTransaction());
278         tx.enlistResource(r1_2);
279         tx.enlistResource(r2_2);
280         tx.delistResource(r1_2, XAResource.TMSUCCESS);
281         tx.delistResource(r2_2, XAResource.TMSUCCESS);
282         tm.suspend();
283         tm.prepare(tx);
284         //recover
285
tm.recovery.recoverLog();
286         resourceManagers.add(rm1);
287         assertTrue(!r1_2.isCommitted());
288         assertTrue(!r2_2.isCommitted());
289         resourceManagers.add(rm2);
290         assertTrue(!r2_2.isCommitted());
291         //there are no transactions started here, so local recovery is complete
292
assertTrue(tm.recovery.localRecoveryComplete());
293         Map JavaDoc recovered = tm.getExternalXids();
294         assertEquals(1, recovered.size());
295         assertEquals(xid, recovered.keySet().iterator().next());
296     }
297
298       public void testTimeout() throws Exception JavaDoc
299       {
300           long timeout = tm.getTransactionTimeoutMilliseconds(0L);
301           tm.setTransactionTimeout((int)timeout/4000);
302           tm.begin();
303           System.out.println("Test to sleep for" + timeout + " secs");
304           Thread.sleep(timeout);
305           try
306           {
307               tm.commit();
308               fail("Tx Should get Rollback exception");
309           }catch(RollbackException JavaDoc rex)
310           {
311               // Caught expected exception
312
}
313
314           // Now test if the default timeout is active
315
tm.begin();
316           System.out.println("Test to sleep for" + (timeout/2) + " secs");
317           Thread.sleep((timeout/2));
318           tm.commit();
319           // Its a failure if exception occurs.
320
}
321
322     public void testResourceManagerContract() throws Exception JavaDoc {
323         resourceManagers.add(rm1);
324         assertTrue(rm1.areAllResourcesReturned());
325     }
326
327
328     private static class TestReferenceCollection extends ArrayList JavaDoc implements ReferenceCollection {
329
330         ReferenceCollectionListener referenceCollectionListener;
331
332         public void addReferenceCollectionListener(ReferenceCollectionListener listener) {
333             this.referenceCollectionListener = listener;
334         }
335
336         public void removeReferenceCollectionListener(ReferenceCollectionListener listener) {
337             this.referenceCollectionListener = null;
338         }
339
340         public boolean add(Object JavaDoc o) {
341             boolean result = super.add(o);
342             if (referenceCollectionListener != null) {
343                 referenceCollectionListener.memberAdded(new ReferenceCollectionEvent(null, o));
344             }
345             return result;
346         }
347
348         public boolean remove(Object JavaDoc o) {
349             boolean result = super.remove(o);
350             if (referenceCollectionListener != null) {
351                 referenceCollectionListener.memberRemoved(new ReferenceCollectionEvent(null, o));
352             }
353             return result;
354         }
355
356     public ObjectName JavaDoc[] getMemberObjectNames() {return new ObjectName JavaDoc[0];}
357
358     }
359
360
361 }
362
Popular Tags