KickJava   Java API By Example, From Geeks To Geeks.

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


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 javax.transaction.xa.Xid JavaDoc;
21 import javax.transaction.xa.XAResource JavaDoc;
22 import javax.transaction.xa.XAException JavaDoc;
23 import javax.transaction.Transaction JavaDoc;
24 import javax.transaction.Status JavaDoc;
25
26 import junit.framework.TestCase;
27
28 /**
29  *
30  *
31  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
32  *
33  * */

34 public class XidImporterTest extends TestCase{
35
36     MockResourceManager rm1 = new MockResourceManager(true);
37     MockResource r1_1 = new MockResource(rm1, "rm1");
38     MockResource r1_2 = new MockResource(rm1, "rm1");
39     MockResourceManager rm2 = new MockResourceManager(true);
40     MockResource r2_1 = new MockResource(rm2, "rm2");
41     MockResource r2_2 = new MockResource(rm2, "rm2");
42
43     XidImporter tm;
44     XidFactory xidFactory = new XidFactoryImpl();
45
46     protected void setUp() throws Exception JavaDoc {
47         tm = new TransactionManagerImpl();
48     }
49
50     public void testImportXid() throws Exception JavaDoc {
51         Xid JavaDoc externalXid = xidFactory.createXid();
52         Transaction JavaDoc tx = tm.importXid(externalXid, 0);
53         assertNotNull(tx);
54         assertEquals(Status.STATUS_ACTIVE, tx.getStatus());
55     }
56
57     public void testNoResourcesCommitOnePhase() throws Exception JavaDoc {
58         Xid JavaDoc externalXid = xidFactory.createXid();
59         Transaction JavaDoc tx = tm.importXid(externalXid, 0);
60         tm.commit(tx, true);
61         assertEquals(Status.STATUS_NO_TRANSACTION, tx.getStatus());
62     }
63
64     public void testNoResourcesCommitTwoPhase() throws Exception JavaDoc {
65         Xid JavaDoc externalXid = xidFactory.createXid();
66         Transaction JavaDoc tx = tm.importXid(externalXid, 0);
67         assertEquals(XAResource.XA_RDONLY, tm.prepare(tx));
68         assertEquals(Status.STATUS_NO_TRANSACTION, tx.getStatus());
69     }
70
71     public void testNoResourcesMarkRollback() throws Exception JavaDoc {
72         Xid JavaDoc externalXid = xidFactory.createXid();
73         Transaction JavaDoc tx = tm.importXid(externalXid, 0);
74         tx.setRollbackOnly();
75         try {
76             tm.prepare(tx);
77             fail("should throw rollback exception in an XAException");
78         } catch (XAException JavaDoc e) {
79         }
80         assertEquals(Status.STATUS_NO_TRANSACTION, tx.getStatus());
81     }
82
83     public void testNoResourcesRollback() throws Exception JavaDoc {
84         Xid JavaDoc externalXid = xidFactory.createXid();
85         Transaction JavaDoc tx = tm.importXid(externalXid, 0);
86         tm.rollback(tx);
87         assertEquals(Status.STATUS_NO_TRANSACTION, tx.getStatus());
88     }
89
90     public void testOneResourceOnePhaseCommit() throws Exception JavaDoc {
91         Xid JavaDoc externalXid = xidFactory.createXid();
92         Transaction JavaDoc tx = tm.importXid(externalXid, 0);
93         tx.enlistResource(r1_1);
94         tx.delistResource(r1_1, XAResource.TMSUCCESS);
95         tm.commit(tx, true);
96         assertEquals(Status.STATUS_NO_TRANSACTION, tx.getStatus());
97     }
98
99     public void testOneResourceTwoPhaseCommit() throws Exception JavaDoc {
100         Xid JavaDoc externalXid = xidFactory.createXid();
101         Transaction JavaDoc tx = tm.importXid(externalXid, 0);
102         tx.enlistResource(r1_1);
103         tx.delistResource(r1_1, XAResource.TMSUCCESS);
104         assertEquals(XAResource.XA_OK, tm.prepare(tx));
105         assertTrue(!r1_1.isCommitted());
106         assertTrue(r1_1.isPrepared());
107         assertTrue(!r1_1.isRolledback());
108         tm.commit(tx, false);
109         assertEquals(Status.STATUS_NO_TRANSACTION, tx.getStatus());
110         assertTrue(r1_1.isCommitted());
111         assertTrue(r1_1.isPrepared());
112         assertTrue(!r1_1.isRolledback());
113     }
114
115     public void testFourResourceTwoPhaseCommit() throws Exception JavaDoc {
116         Xid JavaDoc externalXid = xidFactory.createXid();
117         Transaction JavaDoc tx = tm.importXid(externalXid, 0);
118         tx.enlistResource(r1_1);
119         tx.enlistResource(r1_2);
120         tx.enlistResource(r2_1);
121         tx.enlistResource(r2_2);
122         tx.delistResource(r1_1, XAResource.TMSUCCESS);
123         tx.delistResource(r1_2, XAResource.TMSUCCESS);
124         tx.delistResource(r2_1, XAResource.TMSUCCESS);
125         tx.delistResource(r2_2, XAResource.TMSUCCESS);
126         assertEquals(XAResource.XA_OK, tm.prepare(tx));
127         assertTrue(!r1_1.isCommitted() & !r1_2.isCommitted());
128         assertTrue(r1_1.isPrepared() ^ r1_2.isPrepared());
129         assertTrue(!r1_1.isRolledback() & !r1_2.isRolledback());
130         assertTrue(!r2_1.isCommitted() & !r2_2.isCommitted());
131         assertTrue(r2_1.isPrepared() ^ r2_2.isPrepared());
132         assertTrue(!r2_1.isRolledback() & !r2_2.isRolledback());
133         tm.commit(tx, false);
134         assertEquals(Status.STATUS_NO_TRANSACTION, tx.getStatus());
135         assertTrue((r1_1.isCommitted() & r1_1.isPrepared()) ^ (r1_2.isCommitted() & r1_2.isPrepared()));
136         assertTrue(!r1_1.isRolledback() & !r1_2.isRolledback());
137         assertTrue((r2_1.isCommitted() & r2_1.isPrepared()) ^ (r2_2.isCommitted() & r2_2.isPrepared()));
138         assertTrue(!r2_1.isRolledback() & !r2_2.isRolledback());
139     }
140
141 }
142
Popular Tags