1 17 18 package org.apache.geronimo.transaction.manager; 19 20 import java.util.ArrayList ; 21 import java.util.Map ; 22 import javax.management.ObjectName ; 23 import javax.transaction.RollbackException ; 24 import javax.transaction.Status ; 25 import javax.transaction.Transaction ; 26 import javax.transaction.xa.XAResource ; 27 import javax.transaction.xa.Xid ; 28 29 import junit.framework.TestCase; 30 import org.apache.geronimo.gbean.ReferenceCollection; 31 import org.apache.geronimo.gbean.ReferenceCollectionEvent; 32 import org.apache.geronimo.gbean.ReferenceCollectionListener; 33 34 37 public class TransactionManagerImplTest extends TestCase { 38 39 MockResourceManager rm1 = new MockResourceManager(true); 40 MockResource r1_1 = rm1.getResource("rm1_1"); 41 MockResource r1_2 = rm1.getResource("rm1_2"); 42 MockResourceManager rm2 = new MockResourceManager(true); 43 MockResource r2_1 = rm2.getResource("rm2_1"); 44 MockResource r2_2 = rm2.getResource("rm2_2"); 45 46 TransactionLog transactionLog = new MockLog(); 47 48 ReferenceCollection resourceManagers = new TestReferenceCollection(); 49 TransactionManagerImpl tm; 50 51 protected void setUp() throws Exception { 52 tm = new TransactionManagerImplGBean(10, 53 new XidFactoryImpl("WHAT DO WE CALL IT?".getBytes()), transactionLog, resourceManagers); 54 } 55 56 protected void tearDown() throws Exception { 57 tm = null; 58 } 59 60 public void testNoResourcesCommit() throws Exception { 61 assertEquals(Status.STATUS_NO_TRANSACTION, tm.getStatus()); 62 tm.begin(); 63 assertEquals(Status.STATUS_ACTIVE, tm.getStatus()); 64 tm.commit(); 65 assertNull(tm.getTransaction()); 66 assertEquals(Status.STATUS_NO_TRANSACTION, tm.getStatus()); 67 tm.begin(); 68 assertEquals(Status.STATUS_ACTIVE, tm.getStatus()); 69 Transaction tx = tm.getTransaction(); 70 assertNotNull(tx); 71 tx.commit(); 72 assertNotNull(tm.getTransaction()); 73 assertEquals(Status.STATUS_NO_TRANSACTION, tm.getStatus()); 74 } 75 76 public void testNoResourcesMarkRollbackOnly() throws Exception { 77 assertEquals(Status.STATUS_NO_TRANSACTION, tm.getStatus()); 78 tm.begin(); 79 assertEquals(Status.STATUS_ACTIVE, tm.getStatus()); 80 tm.getTransaction().setRollbackOnly(); 81 assertEquals(Status.STATUS_MARKED_ROLLBACK, tm.getStatus()); 82 try { 83 tm.commit(); 84 fail("tx should not commit"); 85 } catch (RollbackException e) { 86 } 88 assertNull(tm.getTransaction()); 89 assertEquals(Status.STATUS_NO_TRANSACTION, tm.getStatus()); 90 tm.begin(); 91 assertEquals(Status.STATUS_ACTIVE, tm.getStatus()); 92 Transaction tx = tm.getTransaction(); 93 assertNotNull(tx); 94 tx.setRollbackOnly(); 95 assertEquals(Status.STATUS_MARKED_ROLLBACK, tx.getStatus()); 96 try { 97 tx.commit(); 98 fail("tx should not commit"); 99 } catch (RollbackException e) { 100 } 102 assertNotNull(tm.getTransaction()); 103 assertEquals(Status.STATUS_NO_TRANSACTION, tm.getStatus()); 104 } 105 106 public void testNoResourcesRollback() throws Exception { 107 assertEquals(Status.STATUS_NO_TRANSACTION, tm.getStatus()); 108 tm.begin(); 109 assertEquals(Status.STATUS_ACTIVE, tm.getStatus()); 110 tm.rollback(); 111 assertNull(tm.getTransaction()); 112 assertEquals(Status.STATUS_NO_TRANSACTION, tm.getStatus()); 113 tm.begin(); 114 assertEquals(Status.STATUS_ACTIVE, tm.getStatus()); 115 Transaction tx = tm.getTransaction(); 116 assertNotNull(tx); 117 tx.rollback(); 118 assertNotNull(tm.getTransaction()); 119 assertEquals(Status.STATUS_NO_TRANSACTION, tm.getStatus()); 120 121 tm.begin(); 123 assertEquals(Status.STATUS_ACTIVE, tm.getStatus()); 124 tm.getTransaction().setRollbackOnly(); 125 assertEquals(Status.STATUS_MARKED_ROLLBACK, tm.getStatus()); 126 tm.rollback(); 127 assertNull(tm.getTransaction()); 128 assertEquals(Status.STATUS_NO_TRANSACTION, tm.getStatus()); 129 } 130 131 public void testOneResourceCommit() throws Exception { 132 assertEquals(Status.STATUS_NO_TRANSACTION, tm.getStatus()); 133 tm.begin(); 134 Transaction tx = tm.getTransaction(); 135 tx.enlistResource(r1_1); 136 tx.delistResource(r1_1, XAResource.TMSUCCESS); 137 tx.commit(); 138 assertEquals(Status.STATUS_NO_TRANSACTION, tm.getStatus()); 139 assertTrue(r1_1.isCommitted()); 140 assertTrue(!r1_1.isPrepared()); 141 assertTrue(!r1_1.isRolledback()); 142 } 143 144 public void testOneResourceMarkedRollback() throws Exception { 145 assertEquals(Status.STATUS_NO_TRANSACTION, tm.getStatus()); 146 tm.begin(); 147 Transaction tx = tm.getTransaction(); 148 tx.enlistResource(r1_1); 149 tx.setRollbackOnly(); 150 tx.delistResource(r1_1, XAResource.TMSUCCESS); 151 try { 152 tx.commit(); 153 fail("tx should roll back"); 154 } catch (RollbackException e) { 155 } 157 assertEquals(Status.STATUS_NO_TRANSACTION, tm.getStatus()); 158 assertTrue(!r1_1.isCommitted()); 159 assertTrue(!r1_1.isPrepared()); 160 assertTrue(r1_1.isRolledback()); 161 } 162 163 public void testOneResourceRollback() throws Exception { 164 assertEquals(Status.STATUS_NO_TRANSACTION, tm.getStatus()); 165 tm.begin(); 166 Transaction tx = tm.getTransaction(); 167 tx.enlistResource(r1_1); 168 tx.delistResource(r1_1, XAResource.TMSUCCESS); 169 tx.rollback(); 170 assertEquals(Status.STATUS_NO_TRANSACTION, tm.getStatus()); 171 assertTrue(!r1_1.isCommitted()); 172 assertTrue(!r1_1.isPrepared()); 173 assertTrue(r1_1.isRolledback()); 174 } 175 176 public void testTwoResourceOneRMCommit() throws Exception { 177 assertEquals(Status.STATUS_NO_TRANSACTION, tm.getStatus()); 178 tm.begin(); 179 Transaction tx = tm.getTransaction(); 180 tx.enlistResource(r1_1); 181 tx.delistResource(r1_1, XAResource.TMSUCCESS); 182 tx.enlistResource(r1_2); 183 tx.delistResource(r1_2, XAResource.TMSUCCESS); 184 tx.commit(); 185 assertEquals(Status.STATUS_NO_TRANSACTION, tm.getStatus()); 186 assertTrue(r1_1.isCommitted() ^ r1_2.isCommitted()); 187 assertTrue(!r1_1.isPrepared() & !r1_2.isPrepared()); 188 assertTrue(!r1_1.isRolledback() & !r1_2.isRolledback()); 189 } 190 191 public void testTwoResourceOneRMMarkRollback() throws Exception { 192 assertEquals(Status.STATUS_NO_TRANSACTION, tm.getStatus()); 193 tm.begin(); 194 Transaction tx = tm.getTransaction(); 195 tx.enlistResource(r1_1); 196 tx.delistResource(r1_1, XAResource.TMSUCCESS); 197 tx.enlistResource(r1_2); 198 tx.delistResource(r1_2, XAResource.TMSUCCESS); 199 tx.setRollbackOnly(); 200 try { 201 tx.commit(); 202 fail("tx should roll back"); 203 } catch (RollbackException e) { 204 } 206 assertEquals(Status.STATUS_NO_TRANSACTION, tm.getStatus()); 207 assertTrue(!r1_1.isCommitted() & !r1_2.isCommitted()); 208 assertTrue(!r1_1.isPrepared() & !r1_2.isPrepared()); 209 assertTrue(r1_1.isRolledback() ^ r1_2.isRolledback()); 210 } 211 212 public void testTwoResourcesOneRMRollback() throws Exception { 213 tm.begin(); 214 Transaction tx = tm.getTransaction(); 215 tx.enlistResource(r1_1); 216 tx.delistResource(r1_1, XAResource.TMSUCCESS); 217 tx.enlistResource(r1_2); 218 tx.delistResource(r1_2, XAResource.TMSUCCESS); 219 tx.setRollbackOnly(); 220 tx.rollback(); 221 assertEquals(Status.STATUS_NO_TRANSACTION, tm.getStatus()); 222 assertTrue(!r1_1.isCommitted() & !r1_2.isCommitted()); 223 assertTrue(!r1_1.isPrepared() & !r1_2.isPrepared()); 224 assertTrue(r1_1.isRolledback() ^ r1_2.isRolledback()); 225 } 226 227 public void testFourResourceTwoRMCommit() throws Exception { 228 assertEquals(Status.STATUS_NO_TRANSACTION, tm.getStatus()); 229 tm.begin(); 230 Transaction tx = tm.getTransaction(); 231 tx.enlistResource(r1_1); 232 tx.enlistResource(r1_2); 233 tx.enlistResource(r2_1); 234 tx.enlistResource(r2_2); 235 tx.delistResource(r1_1, XAResource.TMSUCCESS); 236 tx.delistResource(r1_2, XAResource.TMSUCCESS); 237 tx.delistResource(r2_1, XAResource.TMSUCCESS); 238 tx.delistResource(r2_2, XAResource.TMSUCCESS); 239 tx.commit(); 240 assertEquals(Status.STATUS_NO_TRANSACTION, tm.getStatus()); 241 assertTrue((r1_1.isCommitted() & r1_1.isPrepared()) ^ (r1_2.isCommitted() & r1_2.isPrepared())); 242 assertTrue(!r1_1.isRolledback() & !r1_2.isRolledback()); 243 assertTrue((r2_1.isCommitted() & r2_1.isPrepared()) ^ (r2_2.isCommitted() & r2_2.isPrepared())); 244 assertTrue(!r2_1.isRolledback() & !r2_2.isRolledback()); 245 } 246 247 public void testSimpleRecovery() throws Exception { 250 Xid xid = tm.xidFactory.createXid(); 252 Transaction tx = tm.importXid(xid, 0); 253 tm.resume(tx); 254 assertSame(tx, tm.getTransaction()); 255 tx.enlistResource(r1_2); 256 tx.enlistResource(r2_2); 257 tx.delistResource(r1_2, XAResource.TMSUCCESS); 258 tx.delistResource(r2_2, XAResource.TMSUCCESS); 259 tm.suspend(); 260 tm.prepare(tx); 261 tm.recovery.recoverLog(); 263 resourceManagers.add(rm1); 264 assertTrue(r1_2.isCommitted()); 265 assertTrue(!r2_2.isCommitted()); 266 resourceManagers.add(rm2); 267 assertTrue(r2_2.isCommitted()); 268 assertTrue(tm.recovery.localRecoveryComplete()); 269 } 270 271 public void testImportedXidRecovery() throws Exception { 272 XidFactory xidFactory2 = new XidFactoryImpl("tm2".getBytes()); 274 Xid xid = xidFactory2.createXid(); 275 Transaction tx = tm.importXid(xid, 0); 276 tm.resume(tx); 277 assertSame(tx, tm.getTransaction()); 278 tx.enlistResource(r1_2); 279 tx.enlistResource(r2_2); 280 tx.delistResource(r1_2, XAResource.TMSUCCESS); 281 tx.delistResource(r2_2, XAResource.TMSUCCESS); 282 tm.suspend(); 283 tm.prepare(tx); 284 tm.recovery.recoverLog(); 286 resourceManagers.add(rm1); 287 assertTrue(!r1_2.isCommitted()); 288 assertTrue(!r2_2.isCommitted()); 289 resourceManagers.add(rm2); 290 assertTrue(!r2_2.isCommitted()); 291 assertTrue(tm.recovery.localRecoveryComplete()); 293 Map recovered = tm.getExternalXids(); 294 assertEquals(1, recovered.size()); 295 assertEquals(xid, recovered.keySet().iterator().next()); 296 } 297 298 public void testTimeout() throws Exception 299 { 300 long timeout = tm.getTransactionTimeoutMilliseconds(0L); 301 tm.setTransactionTimeout((int)timeout/4000); 302 tm.begin(); 303 System.out.println("Test to sleep for" + timeout + " secs"); 304 Thread.sleep(timeout); 305 try 306 { 307 tm.commit(); 308 fail("Tx Should get Rollback exception"); 309 }catch(RollbackException rex) 310 { 311 } 313 314 tm.begin(); 316 System.out.println("Test to sleep for" + (timeout/2) + " secs"); 317 Thread.sleep((timeout/2)); 318 tm.commit(); 319 } 321 322 public void testResourceManagerContract() throws Exception { 323 resourceManagers.add(rm1); 324 assertTrue(rm1.areAllResourcesReturned()); 325 } 326 327 328 private static class TestReferenceCollection extends ArrayList implements ReferenceCollection { 329 330 ReferenceCollectionListener referenceCollectionListener; 331 332 public void addReferenceCollectionListener(ReferenceCollectionListener listener) { 333 this.referenceCollectionListener = listener; 334 } 335 336 public void removeReferenceCollectionListener(ReferenceCollectionListener listener) { 337 this.referenceCollectionListener = null; 338 } 339 340 public boolean add(Object o) { 341 boolean result = super.add(o); 342 if (referenceCollectionListener != null) { 343 referenceCollectionListener.memberAdded(new ReferenceCollectionEvent(null, o)); 344 } 345 return result; 346 } 347 348 public boolean remove(Object o) { 349 boolean result = super.remove(o); 350 if (referenceCollectionListener != null) { 351 referenceCollectionListener.memberRemoved(new ReferenceCollectionEvent(null, o)); 352 } 353 return result; 354 } 355 356 public ObjectName [] getMemberObjectNames() {return new ObjectName [0];} 357 358 } 359 360 361 } 362 | Popular Tags |