1 17 18 package org.apache.geronimo.transaction.manager; 19 20 import javax.transaction.xa.Xid ; 21 import javax.transaction.xa.XAResource ; 22 import javax.transaction.xa.XAException ; 23 import javax.transaction.Transaction ; 24 import javax.transaction.Status ; 25 26 import junit.framework.TestCase; 27 28 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 { 47 tm = new TransactionManagerImpl(); 48 } 49 50 public void testImportXid() throws Exception { 51 Xid externalXid = xidFactory.createXid(); 52 Transaction tx = tm.importXid(externalXid, 0); 53 assertNotNull(tx); 54 assertEquals(Status.STATUS_ACTIVE, tx.getStatus()); 55 } 56 57 public void testNoResourcesCommitOnePhase() throws Exception { 58 Xid externalXid = xidFactory.createXid(); 59 Transaction 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 { 65 Xid externalXid = xidFactory.createXid(); 66 Transaction 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 { 72 Xid externalXid = xidFactory.createXid(); 73 Transaction 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 e) { 79 } 80 assertEquals(Status.STATUS_NO_TRANSACTION, tx.getStatus()); 81 } 82 83 public void testNoResourcesRollback() throws Exception { 84 Xid externalXid = xidFactory.createXid(); 85 Transaction 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 { 91 Xid externalXid = xidFactory.createXid(); 92 Transaction 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 { 100 Xid externalXid = xidFactory.createXid(); 101 Transaction 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 { 116 Xid externalXid = xidFactory.createXid(); 117 Transaction 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 |