1 17 18 package org.apache.geronimo.connector.outbound; 19 20 import javax.resource.ResourceException ; 21 import javax.transaction.Transaction ; 22 import javax.transaction.TransactionManager ; 23 import javax.transaction.Status ; 24 25 import org.apache.geronimo.connector.ConnectorTransactionContext; 26 import org.apache.geronimo.transaction.manager.TransactionManagerImpl; 27 28 34 public class TransactionCachingInterceptorTest extends ConnectionInterceptorTestUtils { 35 private TransactionManager transactionManager; 36 private TransactionCachingInterceptor transactionCachingInterceptor; 37 38 protected void setUp() throws Exception { 39 super.setUp(); 40 transactionManager = new TransactionManagerImpl(); 41 transactionCachingInterceptor = new TransactionCachingInterceptor(this, transactionManager); 42 } 43 44 protected void tearDown() throws Exception { 45 super.tearDown(); 46 transactionManager = null; 47 transactionCachingInterceptor = null; 48 } 49 50 public void testGetConnectionInTransaction() throws Exception { 51 transactionManager.begin(); 52 ConnectionInfo connectionInfo1 = makeConnectionInfo(); 53 transactionCachingInterceptor.getConnection(connectionInfo1); 54 assertTrue("Expected to get an initial connection", obtainedConnectionInfo != null); 55 assertTrue("Expected nothing returned yet", returnedConnectionInfo == null); 56 assertTrue("Expected the same ManagedConnectionInfo in the TransactionContext as was returned", 57 connectionInfo1.getManagedConnectionInfo() 58 == getSharedManagedConnectionInfo(transactionManager.getTransaction())); 59 obtainedConnectionInfo = null; 60 ConnectionInfo connectionInfo2 = new ConnectionInfo(); 61 transactionCachingInterceptor.getConnection(connectionInfo2); 62 assertTrue("Expected to not get a second connection", obtainedConnectionInfo == null); 63 assertTrue("Expected nothing returned yet", returnedConnectionInfo == null); 64 assertTrue("Expected the same ManagedConnectionInfo in both ConnectionInfos", 65 connectionInfo1.getManagedConnectionInfo() == connectionInfo2.getManagedConnectionInfo()); 66 assertTrue("Expected the same ManagedConnectionInfo in the TransactionContext as was returned", 67 connectionInfo1.getManagedConnectionInfo() == getSharedManagedConnectionInfo(transactionManager.getTransaction())); 68 assertTrue("Expected TransactionContext to report active", TxUtil.isTransactionActive(transactionManager)); 71 transactionManager.commit(); 72 assertTrue("Expected connection to be returned", returnedConnectionInfo != null); 73 assertTrue("Expected TransactionContext to report inactive", !TxUtil.isTransactionActive(transactionManager)); 74 75 } 76 77 public void testGetUnshareableConnectionsInTransaction() throws Exception { 78 transactionManager.begin(); 79 ConnectionInfo connectionInfo1 = makeConnectionInfo(); 80 connectionInfo1.setUnshareable(true); 81 transactionCachingInterceptor.getConnection(connectionInfo1); 82 assertTrue("Expected to get an initial connection", obtainedConnectionInfo != null); 83 assertTrue("Expected nothing returned yet", returnedConnectionInfo == null); 84 85 obtainedConnectionInfo = null; 87 ConnectionInfo connectionInfo2 = makeConnectionInfo(); 88 transactionCachingInterceptor.getConnection(connectionInfo2); 89 assertTrue("Expected to get a second connection", obtainedConnectionInfo != null); 90 assertTrue("Expected nothing returned yet", returnedConnectionInfo == null); 91 assertTrue("Expected the same ManagedConnectionInfo in both ConnectionInfos", 92 connectionInfo1.getManagedConnectionInfo() != connectionInfo2.getManagedConnectionInfo()); 93 94 obtainedConnectionInfo = null; 96 ConnectionInfo connectionInfo3 = makeConnectionInfo(); 97 connectionInfo3.setUnshareable(true); 98 transactionCachingInterceptor.getConnection(connectionInfo3); 99 assertTrue("Expected to get a third connection", obtainedConnectionInfo != null); 100 assertTrue("Expected nothing returned yet", returnedConnectionInfo == null); 101 assertTrue("Expected different ManagedConnectionInfo in both unshared ConnectionInfos", 102 connectionInfo1.getManagedConnectionInfo() != connectionInfo3.getManagedConnectionInfo()); 103 104 assertTrue("Expected TransactionContext to report active", transactionManager.getStatus() == Status.STATUS_ACTIVE); 107 transactionManager.commit(); 108 assertTrue("Expected connection to be returned", returnedConnectionInfo != null); 109 } 110 111 private ManagedConnectionInfo getSharedManagedConnectionInfo(Transaction transaction) { 112 if (transaction == null) return null; 113 return ConnectorTransactionContext.get(transaction, transactionCachingInterceptor).getShared(); 114 } 115 116 public void testGetConnectionOutsideTransaction() throws Exception { 117 ConnectionInfo connectionInfo1 = makeConnectionInfo(); 118 transactionCachingInterceptor.getConnection(connectionInfo1); 119 assertTrue("Expected to get an initial connection", obtainedConnectionInfo != null); 120 assertTrue("Expected nothing returned yet", returnedConnectionInfo == null); 121 obtainedConnectionInfo = null; 122 ConnectionInfo connectionInfo2 = makeConnectionInfo(); 123 transactionCachingInterceptor.getConnection(connectionInfo2); 124 assertTrue("Expected to get a second connection", obtainedConnectionInfo != null); 125 assertTrue("Expected nothing returned yet", returnedConnectionInfo == null); 126 assertTrue("Expected different ManagedConnectionInfo in both ConnectionInfos", 127 connectionInfo1.getManagedConnectionInfo() != connectionInfo2.getManagedConnectionInfo()); 128 transactionCachingInterceptor.returnConnection(connectionInfo1, ConnectionReturnAction.RETURN_HANDLE); 130 assertTrue("Expected connection to be returned", returnedConnectionInfo != null); 131 returnedConnectionInfo = null; 132 transactionCachingInterceptor.returnConnection(connectionInfo2, ConnectionReturnAction.RETURN_HANDLE); 133 assertTrue("Expected connection to be returned", returnedConnectionInfo != null); 134 } 135 136 public void testTransactionIndependence() throws Exception { 137 transactionManager.begin(); 138 ConnectionInfo connectionInfo1 = makeConnectionInfo(); 139 transactionCachingInterceptor.getConnection(connectionInfo1); 140 obtainedConnectionInfo = null; 141 142 Transaction suspendedTransaction = transactionManager.suspend(); 144 transactionManager.begin(); 145 ConnectionInfo connectionInfo2 = makeConnectionInfo(); 146 transactionCachingInterceptor.getConnection(connectionInfo2); 147 assertTrue("Expected to get a second connection", obtainedConnectionInfo != null); 148 assertTrue("Expected nothing returned yet", returnedConnectionInfo == null); 149 assertTrue("Expected different ManagedConnectionInfo in each ConnectionInfos", 150 connectionInfo1.getManagedConnectionInfo() != connectionInfo2.getManagedConnectionInfo()); 151 assertTrue("Expected the same ManagedConnectionInfo in the TransactionContext as was returned", 152 connectionInfo2.getManagedConnectionInfo() == getSharedManagedConnectionInfo(transactionManager.getTransaction())); 153 assertTrue("Expected TransactionContext to report active", transactionManager.getStatus() == Status.STATUS_ACTIVE); 156 transactionManager.commit(); 157 assertTrue("Expected connection to be returned", returnedConnectionInfo != null); 158 assertTrue("Expected TransactionContext to report inactive", transactionManager.getStatus() == Status.STATUS_NO_TRANSACTION); 159 returnedConnectionInfo = null; 160 transactionManager.resume(suspendedTransaction); 162 transactionManager.commit(); 163 assertTrue("Expected connection to be returned", returnedConnectionInfo != null); 164 assertTrue("Expected TransactionContext to report inactive", transactionManager.getStatus() == Status.STATUS_NO_TRANSACTION); 165 } 166 167 public void getConnection(ConnectionInfo connectionInfo) throws ResourceException { 169 super.getConnection(connectionInfo); 170 ManagedConnectionInfo managedConnectionInfo = connectionInfo.getManagedConnectionInfo(); 171 managedConnectionInfo.setConnectionEventListener(new GeronimoConnectionEventListener(null, managedConnectionInfo)); 172 } 173 174 175 public void handleObtained( 176 ConnectionTrackingInterceptor connectionTrackingInterceptor, 177 ConnectionInfo connectionInfo) { 178 } 179 180 public void handleReleased( 181 ConnectionTrackingInterceptor connectionTrackingInterceptor, 182 ConnectionInfo connectionInfo) { 183 } 184 185 } 186 | Popular Tags |