1 17 18 package org.apache.geronimo.connector.outbound; 19 20 import javax.resource.ResourceException ; 21 import javax.transaction.xa.XAException ; 22 import javax.transaction.xa.XAResource ; 23 import javax.transaction.xa.Xid ; 24 import javax.transaction.TransactionManager ; 25 26 import org.apache.geronimo.transaction.manager.NamedXAResource; 27 import org.apache.geronimo.transaction.manager.TransactionManagerImpl; 28 import org.apache.geronimo.transaction.manager.XidFactoryImpl; 29 30 36 public class TransactionEnlistingInterceptorTest extends ConnectionInterceptorTestUtils implements NamedXAResource { 37 private TransactionEnlistingInterceptor transactionEnlistingInterceptor; 38 private boolean started; 39 private boolean ended; 40 private boolean returned; 41 private boolean committed; 42 private TransactionManager transactionManager; 43 44 protected void setUp() throws Exception { 45 super.setUp(); 46 transactionManager = new TransactionManagerImpl(); 47 transactionEnlistingInterceptor = new TransactionEnlistingInterceptor(this, this.transactionManager); 48 } 49 50 protected void tearDown() throws Exception { 51 super.tearDown(); 52 transactionEnlistingInterceptor = null; 53 started = false; 54 ended = false; 55 returned = false; 56 committed = false; 57 } 58 59 public void testNoTransaction() throws Exception { 60 ConnectionInfo connectionInfo = makeConnectionInfo(); 61 transactionEnlistingInterceptor.getConnection(connectionInfo); 62 assertTrue("Expected not started", !started); 63 assertTrue("Expected not ended", !ended); 64 transactionEnlistingInterceptor.returnConnection(connectionInfo, ConnectionReturnAction.RETURN_HANDLE); 65 assertTrue("Expected returned", returned); 66 assertTrue("Expected not committed", !committed); 67 } 68 69 public void testTransactionShareableConnection() throws Exception { 70 transactionManager.begin(); 71 ConnectionInfo connectionInfo = makeConnectionInfo(); 72 transactionEnlistingInterceptor.getConnection(connectionInfo); 73 assertTrue("Expected started", started); 74 assertTrue("Expected not ended", !ended); 75 started = false; 76 transactionEnlistingInterceptor.returnConnection(connectionInfo, ConnectionReturnAction.RETURN_HANDLE); 77 assertTrue("Expected not started", !started); 78 assertTrue("Expected ended", ended); 79 assertTrue("Expected returned", returned); 80 transactionManager.commit(); 81 assertTrue("Expected committed", committed); 82 } 83 84 public void testTransactionUnshareableConnection() throws Exception { 85 transactionManager.begin(); 86 ConnectionInfo connectionInfo = makeConnectionInfo(); 87 connectionInfo.setUnshareable(true); 88 transactionEnlistingInterceptor.getConnection(connectionInfo); 89 assertTrue("Expected started", started); 90 assertTrue("Expected not ended", !ended); 91 started = false; 92 transactionEnlistingInterceptor.returnConnection(connectionInfo, ConnectionReturnAction.RETURN_HANDLE); 93 assertTrue("Expected not started", !started); 94 assertTrue("Expected ended", ended); 95 assertTrue("Expected returned", returned); 96 transactionManager.commit(); 97 assertTrue("Expected committed", committed); 98 } 99 100 public void getConnection(ConnectionInfo connectionInfo) throws ResourceException { 102 ManagedConnectionInfo managedConnectionInfo = connectionInfo.getManagedConnectionInfo(); 103 managedConnectionInfo.setXAResource(this); 104 } 105 106 public void returnConnection(ConnectionInfo connectionInfo, ConnectionReturnAction connectionReturnAction) { 107 returned = true; 108 } 109 110 public void commit(Xid xid, boolean onePhase) throws XAException { 112 committed = true; 113 } 114 115 public void end(Xid xid, int flags) throws XAException { 116 ended = true; 117 } 118 119 public void forget(Xid xid) throws XAException { 120 } 121 122 public int getTransactionTimeout() throws XAException { 123 return 0; 124 } 125 126 public boolean isSameRM(XAResource xaResource) throws XAException { 127 return false; 128 } 129 130 public int prepare(Xid xid) throws XAException { 131 return 0; 132 } 133 134 public Xid [] recover(int flag) throws XAException { 135 return new Xid [0]; 136 } 137 138 public void rollback(Xid xid) throws XAException { 139 } 140 141 public boolean setTransactionTimeout(int seconds) throws XAException { 142 return false; 143 } 144 145 public void start(Xid xid, int flags) throws XAException { 146 started = true; 147 } 148 149 150 } 151 | Popular Tags |