1 17 18 package org.apache.geronimo.transaction.context; 19 20 import javax.transaction.xa.XAResource ; 21 import javax.transaction.xa.Xid ; 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 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 { 43 super.setUp(); 44 geronimoTransactionManager = new GeronimoTransactionManager(); 45 } 46 47 protected void tearDown() throws Exception { 48 geronimoTransactionManager = null; 49 super.tearDown(); 50 } 51 52 public void testImportedTxLifecycle() throws Exception { 53 Xid 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 } 62 63 public void testNoConcurrentWorkSameXid() throws Exception { 64 final Xid 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 () { 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 e) { 86 throw (AssertionFailedError) new AssertionFailedError().initCause(e); 87 } 88 } 89 }.start(); 90 91 startSignal.await(); 93 try { 94 geronimoTransactionManager.begin(xid, 1000); 95 fail("should not be able begin same xid twice"); 96 } catch (ImportedTransactionActiveException e) { 97 } finally { 99 cleanupSignal.countDown(); 101 102 endSignal.await(); 104 } 105 } 106 107 public void testOnlyOneImportedTxAtATime() throws Exception { 108 Xid xid1 = xidFactory.createXid(); 109 Xid 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 e) { 115 } finally { 117 geronimoTransactionManager.end(xid1); 118 geronimoTransactionManager.rollback(xid1); 119 } 120 } 121 } 122 | Popular Tags |