1 22 package org.jboss.test.jca.test; 23 24 import javax.resource.spi.ConnectionRequestInfo ; 25 import javax.transaction.TransactionManager ; 26 27 import org.jboss.logging.Logger; 28 import org.jboss.resource.connectionmanager.CachedConnectionManager; 29 import org.jboss.resource.connectionmanager.InternalManagedConnectionPool; 30 import org.jboss.resource.connectionmanager.JBossManagedConnectionPool; 31 import org.jboss.resource.connectionmanager.ManagedConnectionPool; 32 import org.jboss.resource.connectionmanager.TransactionSynchronizer; 33 import org.jboss.resource.connectionmanager.TxConnectionManager; 34 import org.jboss.test.JBossTestCase; 35 import org.jboss.test.jca.adapter.TestConnection; 36 import org.jboss.test.jca.adapter.TestConnectionRequestInfo; 37 import org.jboss.test.jca.adapter.TestManagedConnection; 38 import org.jboss.test.jca.adapter.TestManagedConnectionFactory; 39 import org.jboss.tm.TransactionManagerLocator; 40 import org.jboss.tm.usertx.client.ServerVMClientUserTransaction; 41 42 48 public class LocalTransactionTidyupUnitTestCase extends JBossTestCase 49 { 50 Logger poolLog = Logger.getLogger(JBossManagedConnectionPool.class); 51 Logger log = Logger.getLogger(getClass()); 52 53 private TransactionManager tm; 54 private ServerVMClientUserTransaction ut; 55 private CachedConnectionManager ccm; 56 private TestManagedConnectionFactory mcf; 57 private TxConnectionManager cm; 58 private ConnectionRequestInfo cri; 59 60 private int poolSize = 5; 61 62 public LocalTransactionTidyupUnitTestCase (String name) 63 { 64 super(name); 65 } 66 67 protected void setUp() throws Exception 68 { 69 log.debug("================> Start " + getName()); 70 tm = TransactionManagerLocator.getInstance().locate(); 71 TransactionSynchronizer.setTransactionManager(tm); 72 ut = new ServerVMClientUserTransaction(tm); 73 ccm = new CachedConnectionManager(); 74 ut.registerTxStartedListener(ccm); 75 76 mcf = new TestManagedConnectionFactory(); 77 InternalManagedConnectionPool.PoolParams pp = new InternalManagedConnectionPool.PoolParams(); 78 pp.minSize = 0; 79 pp.maxSize = poolSize; 80 pp.blockingTimeout = 100; 81 pp.idleTimeout = 500; 82 ManagedConnectionPool poolingStrategy = new JBossManagedConnectionPool.OnePool(mcf, pp, false, poolLog); 83 cri = new TestConnectionRequestInfo(); 84 cm = new TxConnectionManager(ccm, poolingStrategy, tm); 85 cm.setLocalTransactions(true); 86 poolingStrategy.setConnectionListenerFactory(cm); 87 } 88 89 protected void tearDown() throws Exception 90 { 91 JBossManagedConnectionPool.OnePool pool = (JBossManagedConnectionPool.OnePool) cm.getPoolingStrategy(); 92 pool.shutdown(); 93 ut = null; 94 log.debug("================> End " + getName()); 95 } 96 97 public void testSimple() throws Exception 98 { 99 TestConnection c = (TestConnection)cm.allocateConnection(mcf, cri); 100 assertNotNull(c); 101 c.close(); 102 } 103 104 public void testManualTransactionCommit() throws Exception 105 { 106 TestConnection c = (TestConnection)cm.allocateConnection(mcf, cri); 107 assertNotNull(c); 108 try 109 { 110 c.begin(); 111 assertEquals(TestManagedConnection.LOCAL_TRANSACTION, c.getLocalState()); 112 c.commit(); 113 assertEquals(TestManagedConnection.LOCAL_COMMITTED, c.getLocalState()); 114 } 115 finally 116 { 117 c.close(); 118 } 119 } 120 121 public void testManualTransactionRollback() throws Exception 122 { 123 TestConnection c = (TestConnection)cm.allocateConnection(mcf, cri); 124 assertNotNull(c); 125 try 126 { 127 c.begin(); 128 assertEquals(TestManagedConnection.LOCAL_TRANSACTION, c.getLocalState()); 129 c.rollback(); 130 assertEquals(TestManagedConnection.LOCAL_ROLLEDBACK, c.getLocalState()); 131 } 132 finally 133 { 134 c.close(); 135 } 136 } 137 138 public void testManualTransactionForgetToCommitRollback() throws Exception 139 { 140 TestConnection c = (TestConnection)cm.allocateConnection(mcf, cri); 141 assertNotNull(c); 142 try 143 { 144 c.begin(); 145 assertEquals(TestManagedConnection.LOCAL_TRANSACTION, c.getLocalState()); 146 } 147 finally 148 { 149 c.close(); 150 assertEquals(TestManagedConnection.LOCAL_ROLLEDBACK, c.getLocalState()); 151 } 152 } 153 154 public void testJTATransactionCommit() throws Exception 155 { 156 tm.begin(); 157 TestConnection c = null; 158 try 159 { 160 c = (TestConnection)cm.allocateConnection(mcf, cri); 161 assertNotNull(c); 162 try 163 { 164 assertEquals(TestManagedConnection.LOCAL_TRANSACTION, c.getLocalState()); 165 } 166 finally 167 { 168 c.close(); 169 assertEquals(TestManagedConnection.LOCAL_TRANSACTION, c.getLocalState()); 170 } 171 } 172 finally 173 { 174 tm.commit(); 175 if (c != null) 176 assertEquals(TestManagedConnection.LOCAL_COMMITTED, c.getLocalState()); 177 } 178 } 179 180 public void testJTATransactionRollback() throws Exception 181 { 182 tm.begin(); 183 TestConnection c = null; 184 try 185 { 186 c = (TestConnection)cm.allocateConnection(mcf, cri); 187 assertNotNull(c); 188 try 189 { 190 assertEquals(TestManagedConnection.LOCAL_TRANSACTION, c.getLocalState()); 191 } 192 finally 193 { 194 c.close(); 195 assertEquals(TestManagedConnection.LOCAL_TRANSACTION, c.getLocalState()); 196 } 197 } 198 finally 199 { 200 tm.rollback(); 201 if (c != null) 202 assertEquals(TestManagedConnection.LOCAL_ROLLEDBACK, c.getLocalState()); 203 } 204 } 205 206 public void testUserTransactionCommit() throws Exception 207 { 208 ut.begin(); 209 TestConnection c = null; 210 try 211 { 212 c = (TestConnection)cm.allocateConnection(mcf, cri); 213 assertNotNull(c); 214 try 215 { 216 assertEquals(TestManagedConnection.LOCAL_TRANSACTION, c.getLocalState()); 217 } 218 finally 219 { 220 c.close(); 221 assertEquals(TestManagedConnection.LOCAL_TRANSACTION, c.getLocalState()); 222 } 223 } 224 finally 225 { 226 ut.commit(); 227 if (c != null) 228 assertEquals(TestManagedConnection.LOCAL_COMMITTED, c.getLocalState()); 229 } 230 } 231 232 public void testUserTransactionRollback() throws Exception 233 { 234 ut.begin(); 235 TestConnection c = null; 236 try 237 { 238 c = (TestConnection)cm.allocateConnection(mcf, cri); 239 assertNotNull(c); 240 try 241 { 242 assertEquals(TestManagedConnection.LOCAL_TRANSACTION, c.getLocalState()); 243 } 244 finally 245 { 246 c.close(); 247 assertEquals(TestManagedConnection.LOCAL_TRANSACTION, c.getLocalState()); 248 } 249 } 250 finally 251 { 252 ut.rollback(); 253 if (c != null) 254 assertEquals(TestManagedConnection.LOCAL_ROLLEDBACK, c.getLocalState()); 255 } 256 } 257 258 public void testLazyUserTransactionCommit() throws Exception 259 { 260 ccm.pushMetaAwareObject(this, null); 261 try 262 { 263 TestConnection c = (TestConnection)cm.allocateConnection(mcf, cri); 264 assertNotNull(c); 265 try 266 { 267 assertEquals(TestManagedConnection.LOCAL_NONE, c.getLocalState()); 268 ut.begin(); 269 try 270 { 271 assertEquals(TestManagedConnection.LOCAL_TRANSACTION, c.getLocalState()); 272 } 273 finally 274 { 275 ut.commit(); 276 assertEquals(TestManagedConnection.LOCAL_COMMITTED, c.getLocalState()); 277 } 278 } 279 finally 280 { 281 c.close(); 282 assertEquals(TestManagedConnection.LOCAL_COMMITTED, c.getLocalState()); 283 } 284 } 285 finally 286 { 287 ccm.popMetaAwareObject(null); 288 } 289 } 290 291 public void testLazyUserTransactionRollback() throws Exception 292 { 293 ccm.pushMetaAwareObject(this, null); 294 try 295 { 296 TestConnection c = (TestConnection)cm.allocateConnection(mcf, cri); 297 assertNotNull(c); 298 try 299 { 300 assertEquals(TestManagedConnection.LOCAL_NONE, c.getLocalState()); 301 ut.begin(); 302 try 303 { 304 assertEquals(TestManagedConnection.LOCAL_TRANSACTION, c.getLocalState()); 305 } 306 finally 307 { 308 ut.rollback(); 309 assertEquals(TestManagedConnection.LOCAL_ROLLEDBACK, c.getLocalState()); 310 } 311 } 312 finally 313 { 314 c.close(); 315 assertEquals(TestManagedConnection.LOCAL_ROLLEDBACK, c.getLocalState()); 316 } 317 } 318 finally 319 { 320 ccm.popMetaAwareObject(null); 321 } 322 } 323 } 324 | Popular Tags |