1 22 package org.jboss.test.jca.test; 23 24 import java.util.HashSet ; 25 import java.util.List ; 26 import java.util.Set ; 27 28 import javax.resource.spi.ConnectionRequestInfo ; 29 import javax.transaction.RollbackException ; 30 import javax.transaction.TransactionManager ; 31 import javax.transaction.xa.XAException ; 32 33 import junit.framework.Test; 34 import junit.framework.TestSuite; 35 36 import org.jboss.logging.Logger; 37 import org.jboss.resource.connectionmanager.CachedConnectionManager; 38 import org.jboss.resource.connectionmanager.InternalManagedConnectionPool; 39 import org.jboss.resource.connectionmanager.JBossManagedConnectionPool; 40 import org.jboss.resource.connectionmanager.ManagedConnectionPool; 41 import org.jboss.resource.connectionmanager.TransactionSynchronizer; 42 import org.jboss.resource.connectionmanager.TxConnectionManager; 43 import org.jboss.test.JBossTestCase; 44 import org.jboss.test.jca.adapter.TestConnection; 45 import org.jboss.test.jca.adapter.TestConnectionRequestInfo; 46 import org.jboss.test.jca.adapter.TestManagedConnection; 47 import org.jboss.test.jca.adapter.TestManagedConnectionFactory; 48 import org.jboss.tm.TransactionManagerLocator; 49 import org.jboss.tm.TxUtils; 50 import org.jboss.tm.usertx.client.ServerVMClientUserTransaction; 51 52 61 public class XATxConnectionManagerUnitTestCase extends JBossTestCase 62 { 63 Logger poolLog = Logger.getLogger(JBossManagedConnectionPool.class); 64 Logger log = Logger.getLogger(getClass()); 65 66 private TransactionManager tm; 67 private ServerVMClientUserTransaction ut; 68 private CachedConnectionManager ccm; 69 private TestManagedConnectionFactory mcf; 70 private TxConnectionManager cm; 71 private ConnectionRequestInfo cri; 72 73 private int poolSize = 5; 74 75 public static Test suite() throws Exception 76 { 77 TestSuite suite = new TestSuite(); 80 suite.addTest(new XATxConnectionManagerUnitTestCase("testGetConnection")); 81 suite.addTest(new XATxConnectionManagerUnitTestCase("testEnlistInExistingTx")); 82 suite.addTest(new XATxConnectionManagerUnitTestCase("testEnlistCheckedOutConnectionInNewTx")); 83 suite.addTest(new XATxConnectionManagerUnitTestCase("testReconnectConnectionHandlesOnNotification")); 84 suite.addTest(new XATxConnectionManagerUnitTestCase("testEnlistAfterMarkRollback")); 85 suite.addTest(new XATxConnectionManagerUnitTestCase("testBrokenConnectionAndTrackByTx")); 86 suite.addTest(new XATxConnectionManagerUnitTestCase("testFailedStartTx")); 87 suite.addTest(new XATxConnectionManagerUnitTestCase("testFailedEndTx")); 88 89 return suite; 90 } 91 92 public XATxConnectionManagerUnitTestCase (String name) 93 { 94 super(name); 95 } 96 97 protected void setUp() throws Exception 98 { 99 log.debug("================> Start " + getName()); 100 tm = TransactionManagerLocator.getInstance().locate(); 101 TransactionSynchronizer.setTransactionManager(tm); 102 ut = new ServerVMClientUserTransaction(tm); 103 ccm = new CachedConnectionManager(); 104 ut.registerTxStartedListener(ccm); 105 106 mcf = new TestManagedConnectionFactory(); 107 InternalManagedConnectionPool.PoolParams pp = new InternalManagedConnectionPool.PoolParams(); 108 pp.minSize = 0; 109 pp.maxSize = poolSize; 110 pp.blockingTimeout = 100; 111 pp.idleTimeout = 500; 112 ManagedConnectionPool poolingStrategy = new JBossManagedConnectionPool.OnePool(mcf, pp, false, poolLog); 113 cri = new TestConnectionRequestInfo(); 114 cm = new TxConnectionManager(ccm, poolingStrategy, tm); 115 cm.setLocalTransactions(false); 116 poolingStrategy.setConnectionListenerFactory(cm); 117 } 118 119 protected void tearDown() throws Exception 120 { 121 JBossManagedConnectionPool.OnePool pool = (JBossManagedConnectionPool.OnePool) cm.getPoolingStrategy(); 122 pool.shutdown(); 123 ut = null; 124 log.debug("================> End " + getName()); 125 } 126 127 public void testGetConnection() throws Exception 128 { 129 getLog().info("testGetConnection"); 130 TestConnection c = (TestConnection)cm.allocateConnection(mcf, cri); 131 assertTrue("Connection is null", c != null); 132 c.close(); 133 } 134 135 public void testEnlistInExistingTx() throws Exception 136 { 137 getLog().info("testEnlistInExistingTx"); 138 ut.begin(); 139 TestConnection c = null; 140 try 141 { 142 c = (TestConnection)cm.allocateConnection(mcf, cri); 143 try 144 { 145 assertTrue("Connection not enlisted in tx!", c.isInTx()); 146 } 147 finally 148 { 149 c.close(); 150 } 151 assertTrue("Connection still enlisted in tx!", !c.isInTx()); 152 } 153 finally 154 { 155 if (TxUtils.isActive(ut)) 156 ut.commit(); 157 else 158 ut.rollback(); 159 } 160 assertTrue("Connection still enlisted in tx!", !c.isInTx()); 161 } 162 163 public void testEnlistCheckedOutConnectionInNewTx() throws Exception 164 { 165 getLog().info("testEnlistCheckedOutConnectionInNewTx"); 166 Object key = this; 167 Set unshared = new HashSet (); 168 ccm.pushMetaAwareObject(key, unshared); 169 try 170 { 171 TestConnection c = (TestConnection)cm.allocateConnection(mcf, cri); 172 try 173 { 174 assertTrue("Connection already enlisted in tx!", !c.isInTx()); 175 ut.begin(); 176 try 177 { 178 assertTrue("Connection not enlisted in tx!", c.isInTx()); 179 } 180 finally 181 { 182 if (TxUtils.isActive(ut)) 183 ut.commit(); 184 else 185 ut.rollback(); 186 } 187 assertTrue("Connection still enlisted in tx!", !c.isInTx()); 188 } 189 finally 190 { 191 c.close(); 192 } 193 } 194 finally 195 { 196 ccm.popMetaAwareObject(unshared); 197 198 } 199 } 200 201 206 public void testReconnectConnectionHandlesOnNotification() throws Exception 207 { 208 getLog().info("testReconnectConnectionHandlesOnNotification"); 209 ccm.setSpecCompliant(true); 210 Object key1 = new Object (); 211 Object key2 = new Object (); 212 Set unshared = new HashSet (); 213 ccm.pushMetaAwareObject(key1, unshared); 214 try 215 { 216 TestConnection c = null; 217 ut.begin(); 218 try 219 { 220 ccm.pushMetaAwareObject(key2, unshared); 221 try 222 { 223 c = (TestConnection)cm.allocateConnection(mcf, cri); 224 assertTrue("Connection not enlisted in tx!", c.isInTx()); 225 } 226 finally 227 { 228 ccm.popMetaAwareObject(unshared); } 230 } 231 finally 232 { 233 if (TxUtils.isActive(ut)) 234 ut.commit(); 235 else 236 ut.rollback(); 237 } 238 ut.begin(); 239 try 240 { 241 ccm.pushMetaAwareObject(key2, unshared); 242 try 243 { 244 assertTrue("Connection not enlisted in tx!", c.isInTx()); 245 } 246 finally 247 { 248 ccm.popMetaAwareObject(unshared); } 250 } 251 finally 252 { 253 if (TxUtils.isActive(ut)) 254 ut.commit(); 255 else 256 ut.rollback(); 257 } 258 assertTrue("Connection still enlisted in tx!", !c.isInTx()); 259 ccm.pushMetaAwareObject(key2, unshared); 260 try 261 { 262 if (c != null) 263 c.close(); 264 } 265 finally 266 { 267 ccm.popMetaAwareObject(unshared); } 269 } 270 finally 271 { 272 ccm.popMetaAwareObject(unshared); } 274 } 275 276 public void testEnlistAfterMarkRollback() throws Exception 277 { 278 tm.begin(); 280 try 281 { 282 tm.setRollbackOnly(); 283 for (int i = 0; i < poolSize; ++i) 285 { 286 try 287 { 288 cm.allocateConnection(mcf, cri); 289 fail("Should not be allowed to allocate a connection with setRollbackOnly()"); 290 } 291 catch (Exception e) 292 { 293 log.debug("Error allocating connection", e); 294 } 295 } 296 } 297 finally 298 { 299 tm.rollback(); 300 } 301 302 testGetConnection(); 304 } 305 306 public void testBrokenConnectionAndTrackByTx() throws Exception 307 { 308 getLog().info("testBrokenConnectionAndTrackByTx"); 309 cm.setTrackConnectionByTx(true); 310 ut.begin(); 311 TestConnection c = (TestConnection)cm.allocateConnection(mcf, cri); 312 c.fireConnectionError(); 313 try 314 { 315 c.close(); 316 } 317 catch (Exception ignored) 318 { 319 } 320 try 321 { 322 ut.commit(); 323 fail("Should not be here"); 324 } 325 catch (RollbackException expected) 326 { 327 } 328 assertTrue("Connection still enlisted in tx!", !c.isInTx()); 329 } 330 331 public void testFailedStartTx() throws Exception 332 { 333 TestManagedConnection.setFailInStart(false, XAException.XAER_RMFAIL); 334 tm.begin(); 335 TestConnection conn = null; 336 TestConnection conn2 = null; 337 338 try 339 { 340 assertTrue("Connection in pool!", cm.getPoolingStrategy().getConnectionCount() == 0); 341 conn = (TestConnection)cm.allocateConnection(mcf, cri); 342 343 assertTrue(cm.getPoolingStrategy().getConnectionCount() == 1); 345 346 TestManagedConnection.setFailInStart(true, XAException.XAER_RMFAIL); 347 348 conn2 = (TestConnection)cm.allocateConnection(mcf, cri); 349 350 fail("Should not be here."); 351 352 } 353 catch (Throwable e) 354 { 355 } 356 conn.close(); 357 tm.rollback(); 358 assertTrue(conn2 == null); 359 assertTrue(cm.getPoolingStrategy().getConnectionCount() == 1); 360 } 361 362 public void testFailedEndTx() throws Exception 363 { 364 TestManagedConnection.setFailInStart(false, XAException.XAER_RMFAIL); 365 TestManagedConnection.setFailInEnd(false, XAException.XAER_RMFAIL); 366 tm.begin(); 367 TestConnection conn = null; 368 TestConnection conn2 = null; 369 370 try 371 { 372 assertTrue("Connection in pool!", cm.getPoolingStrategy().getConnectionCount() == 0); 373 conn = (TestConnection)cm.allocateConnection(mcf, cri); 374 375 assertTrue(cm.getPoolingStrategy().getConnectionCount() == 1); 377 conn.close(); 378 379 TestManagedConnection.setFailInEnd(true, XAException.XAER_RMFAIL); 380 381 conn2 = (TestConnection)cm.allocateConnection(mcf, cri); 382 conn2.close(); 383 tm.commit(); 384 385 fail("Should not be here."); 386 387 } 388 catch (Throwable e) 389 { 390 } 391 392 TestManagedConnection.setFailInEnd(false, 0); 393 TestManagedConnection.setFailInStart(false, 0); 394 395 assertTrue(conn2.getMCIsNull()); 396 assertTrue("Connection count" + cm.getPoolingStrategy().getConnectionCount(), cm.getPoolingStrategy().getConnectionCount() == 0); 397 assertTrue("Failed endTx should destroy Connection", cm.getPoolingStrategy().getConnectionDestroyedCount() > 0); 398 399 } 400 401 402 } 403 | Popular Tags |