KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > transaction > context > GeronimoTransactionManagerTest


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.context;
19
20 import javax.transaction.xa.XAResource JavaDoc;
21 import javax.transaction.xa.Xid JavaDoc;
22
23 import edu.emory.mathcs.backport.java.util.concurrent.CountDownLatch;
24 import junit.framework.AssertionFailedError;
25 import junit.framework.TestCase;
26 import org.apache.geronimo.transaction.manager.ImportedTransactionActiveException;
27 import org.apache.geronimo.transaction.manager.GeronimoTransactionManager;
28 import org.apache.geronimo.transaction.manager.XidFactory;
29 import org.apache.geronimo.transaction.manager.XidFactoryImpl;
30
31 /**
32  *
33  *
34  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
35  *
36  */

37 public class GeronimoTransactionManagerTest extends TestCase {
38
39     private GeronimoTransactionManager geronimoTransactionManager;
40     private XidFactory xidFactory = new XidFactoryImpl("geronimo.test.tm".getBytes());
41
42     protected void setUp() throws Exception JavaDoc {
43         super.setUp();
44         geronimoTransactionManager = new GeronimoTransactionManager();
45     }
46
47     protected void tearDown() throws Exception JavaDoc {
48         geronimoTransactionManager = null;
49         super.tearDown();
50     }
51
52     public void testImportedTxLifecycle() throws Exception JavaDoc {
53         Xid JavaDoc xid = xidFactory.createXid();
54         geronimoTransactionManager.begin(xid, 1000);
55         geronimoTransactionManager.end(xid);
56         geronimoTransactionManager.begin(xid, 1000);
57         geronimoTransactionManager.end(xid);
58         int readOnly = geronimoTransactionManager.prepare(xid);
59         assertEquals(XAResource.XA_RDONLY, readOnly);
60 // geronimoTransactionManager.commit(xid, false);
61
}
62
63     public void testNoConcurrentWorkSameXid() throws Exception JavaDoc {
64         final Xid JavaDoc xid = xidFactory.createXid();
65
66         final CountDownLatch startSignal = new CountDownLatch(1);
67         final CountDownLatch cleanupSignal = new CountDownLatch(1);
68         final CountDownLatch endSignal = new CountDownLatch(1);
69
70         new Thread JavaDoc() {
71             public void run() {
72                 try {
73                     try {
74                         try {
75                             geronimoTransactionManager.begin(xid, 1000);
76                         } finally {
77                             startSignal.countDown();
78                         }
79                         cleanupSignal.await();
80                         geronimoTransactionManager.end(xid);
81                         geronimoTransactionManager.rollback(xid);
82                     } finally {
83                         endSignal.countDown();
84                     }
85                 } catch (Exception JavaDoc e) {
86                     throw (AssertionFailedError) new AssertionFailedError().initCause(e);
87                 }
88             }
89         }.start();
90
91         // wait for thread to begin the tx
92
startSignal.await();
93         try {
94             geronimoTransactionManager.begin(xid, 1000);
95             fail("should not be able begin same xid twice");
96         } catch (ImportedTransactionActiveException e) {
97             //expected
98
} finally {
99             // tell thread to start cleanup (e.g., end and rollback the tx)
100
cleanupSignal.countDown();
101
102             // wait for our thread to finish cleanup
103
endSignal.await();
104         }
105     }
106
107     public void testOnlyOneImportedTxAtATime() throws Exception JavaDoc {
108         Xid JavaDoc xid1 = xidFactory.createXid();
109         Xid JavaDoc xid2 = xidFactory.createXid();
110         geronimoTransactionManager.begin(xid1, 1000);
111         try {
112             geronimoTransactionManager.begin(xid2, 1000);
113             fail("should not be able to begin a 2nd tx without ending the first");
114         } catch (IllegalStateException JavaDoc e) {
115             //expected
116
} finally {
117             geronimoTransactionManager.end(xid1);
118             geronimoTransactionManager.rollback(xid1);
119         }
120     }
121 }
122
Popular Tags