Clover coverage report -
Coverage timestamp: Wed Jan 31 2007 15:38:53 EST
file stats: LOC: 823   Methods: 15
NCLOC: 526   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
TxInterceptorTest.java - 96.8% 100% 96.9%
coverage coverage
 1    /*
 2    * Created on 17-Feb-2005
 3    *
 4    *
 5    *
 6    */
 7    package org.jboss.cache.optimistic;
 8   
 9    import org.jboss.cache.CacheImpl;
 10    import org.jboss.cache.GlobalTransaction;
 11    import org.jboss.cache.OptimisticTransactionEntry;
 12    import org.jboss.cache.TransactionTable;
 13    import org.jboss.cache.loader.SamplePojo;
 14    import org.jboss.cache.marshall.MethodCall;
 15    import org.jboss.cache.marshall.MethodCallFactory;
 16    import org.jboss.cache.marshall.MethodDeclarations;
 17    import org.jboss.cache.transaction.DummyTransactionManager;
 18   
 19    import javax.transaction.Transaction;
 20    import java.util.List;
 21   
 22    public class TxInterceptorTest extends AbstractOptimisticTestCase
 23    {
 24  14 public TxInterceptorTest(String name)
 25    {
 26  14 super(name);
 27    }
 28   
 29  1 public void testNoTransaction() throws Exception
 30    {
 31   
 32  1 CacheImpl cache = createCache();
 33  1 MockInterceptor dummy = new MockInterceptor();
 34  1 dummy.setCache(cache);
 35   
 36  1 cache.setInterceptorChain(getAlteredInterceptorChain(dummy, cache, false));
 37  1 DummyTransactionManager mgr = DummyTransactionManager.getInstance();
 38  1 assertNull(mgr.getTransaction());
 39  1 assertEquals(0, cache.getTransactionTable().getNumGlobalTransactions());
 40  1 assertEquals(0, cache.getTransactionTable().getNumLocalTransactions());
 41   
 42  1 SamplePojo pojo = new SamplePojo(21, "test");
 43   
 44  1 cache.put("/one/two", "key1", pojo);
 45  1 assertNull(mgr.getTransaction());
 46  1 assertEquals(0, cache.getTransactionTable().getNumGlobalTransactions());
 47  1 assertEquals(0, cache.getTransactionTable().getNumLocalTransactions());
 48   
 49    //make sure all calls were done in right order
 50   
 51  1 List calls = dummy.getAllCalled();
 52   
 53  1 assertEquals(MethodDeclarations.optimisticPrepareMethod, calls.get(0));
 54  1 assertEquals(MethodDeclarations.commitMethod, calls.get(1));
 55    //flesh this out a bit more
 56   
 57    }
 58   
 59  1 public void testLocalTransactionExists() throws Exception
 60    {
 61   
 62  1 CacheImpl cache = createCache();
 63  1 MockInterceptor dummy = new MockInterceptor();
 64  1 dummy.setCache(cache);
 65   
 66  1 cache.setInterceptorChain(getAlteredInterceptorChain(dummy, cache, false));
 67   
 68  1 DummyTransactionManager mgr = DummyTransactionManager.getInstance();
 69   
 70    //start local transaction
 71  1 mgr.begin();
 72  1 Transaction tx = mgr.getTransaction();
 73   
 74  1 SamplePojo pojo = new SamplePojo(21, "test");
 75   
 76  1 assertNotNull(mgr.getTransaction());
 77  1 TransactionTable txTable = cache.getTransactionTable();
 78  1 assertNull(txTable.get(tx));
 79   
 80  1 cache.put("/one/two", "key1", pojo);
 81   
 82  1 assertNotNull(mgr.getTransaction());
 83  1 mgr.commit();
 84   
 85  1 assertNull(mgr.getTransaction());
 86   
 87  1 List calls = dummy.getAllCalled();
 88  1 assertEquals(MethodDeclarations.optimisticPrepareMethod, calls.get(0));
 89  1 assertEquals(MethodDeclarations.commitMethod, calls.get(1));
 90   
 91  1 assertEquals(0, cache.getTransactionTable().getNumGlobalTransactions());
 92  1 assertEquals(0, cache.getTransactionTable().getNumLocalTransactions());
 93  1 cache.stop();
 94   
 95    }
 96   
 97  1 public void testRollbackTransaction() throws Exception
 98    {
 99   
 100  1 CacheImpl cache = createCache();
 101  1 MockInterceptor dummy = new MockInterceptor();
 102  1 dummy.setCache(cache);
 103   
 104  1 cache.setInterceptorChain(getAlteredInterceptorChain(dummy, cache, false));
 105  1 DummyTransactionManager mgr = DummyTransactionManager.getInstance();
 106   
 107    //start local transaction
 108  1 mgr.begin();
 109   
 110  1 SamplePojo pojo = new SamplePojo(21, "test");
 111   
 112  1 cache.put("/one/two", "key1", pojo);
 113   
 114  1 assertNotNull(mgr.getTransaction());
 115  1 mgr.rollback();
 116   
 117  1 assertNull(mgr.getTransaction());
 118   
 119  1 List calls = dummy.getAllCalled();
 120  1 assertEquals(1, calls.size());
 121  1 assertEquals(MethodDeclarations.rollbackMethod, calls.get(0));
 122   
 123   
 124  1 assertEquals(0, cache.getTransactionTable().getNumGlobalTransactions());
 125  1 assertEquals(0, cache.getTransactionTable().getNumLocalTransactions());
 126  1 cache.stop();
 127   
 128    }
 129   
 130  1 public void testEmptyLocalTransaction() throws Exception
 131    {
 132  1 CacheImpl cache = createCache();
 133  1 MockInterceptor dummy = new MockInterceptor();
 134  1 dummy.setCache(cache);
 135   
 136  1 cache.setInterceptorChain(getAlteredInterceptorChain(dummy, cache, false));
 137  1 DummyTransactionManager mgr = DummyTransactionManager.getInstance();
 138   
 139    //start local transaction
 140  1 mgr.begin();
 141   
 142  1 assertNotNull(mgr.getTransaction());
 143  1 mgr.commit();
 144   
 145  1 assertNull(mgr.getTransaction());
 146   
 147  1 List calls = dummy.getAllCalled();
 148  1 assertEquals(0, calls.size());
 149   
 150  1 assertNull(mgr.getTransaction());
 151   
 152  1 assertEquals(0, cache.getTransactionTable().getNumGlobalTransactions());
 153  1 assertEquals(0, cache.getTransactionTable().getNumLocalTransactions());
 154   
 155  1 cache.stop();
 156    }
 157   
 158  1 public void testEmptyRollbackLocalTransaction() throws Exception
 159    {
 160   
 161  1 CacheImpl cache = createCache();
 162  1 MockInterceptor dummy = new MockInterceptor();
 163  1 dummy.setCache(cache);
 164   
 165  1 cache.setInterceptorChain(getAlteredInterceptorChain(dummy, cache, false));
 166   
 167  1 DummyTransactionManager mgr = DummyTransactionManager.getInstance();
 168   
 169    //start local transaction
 170  1 mgr.begin();
 171   
 172  1 assertNotNull(mgr.getTransaction());
 173  1 mgr.rollback();
 174   
 175  1 assertNull(mgr.getTransaction());
 176   
 177  1 List calls = dummy.getAllCalled();
 178  1 assertEquals(0, calls.size());
 179   
 180  1 assertNull(mgr.getTransaction());
 181   
 182  1 assertEquals(0, cache.getTransactionTable().getNumGlobalTransactions());
 183  1 assertEquals(0, cache.getTransactionTable().getNumLocalTransactions());
 184  1 cache.stop();
 185   
 186    }
 187   
 188  1 public void testLocalRollbackAftercommitTransaction() throws Exception
 189    {
 190   
 191  1 CacheImpl cache = createCache();
 192  1 MockInterceptor dummy = new MockInterceptor();
 193  1 dummy.setCache(cache);
 194   
 195  1 cache.setInterceptorChain(getAlteredInterceptorChain(dummy, cache, false));
 196   
 197  1 DummyTransactionManager mgr = DummyTransactionManager.getInstance();
 198   
 199    //start local transaction
 200  1 mgr.begin();
 201   
 202  1 SamplePojo pojo = new SamplePojo(21, "test");
 203   
 204  1 cache.put("/one/two", "key1", pojo);
 205   
 206  1 assertNotNull(mgr.getTransaction());
 207  1 mgr.commit();
 208   
 209  1 assertNull(mgr.getTransaction());
 210   
 211  1 List calls = dummy.getAllCalled();
 212  1 assertEquals(MethodDeclarations.optimisticPrepareMethod, calls.get(0));
 213  1 assertEquals(MethodDeclarations.commitMethod, calls.get(1));
 214  1 boolean failed = false;
 215  1 try
 216    {
 217  1 mgr.rollback();
 218  0 fail();
 219    }
 220    catch (Exception e)
 221    {
 222  1 e.printStackTrace();
 223  1 failed = true;
 224  1 assertTrue(true);
 225    }
 226  1 assertTrue(failed);
 227  1 assertNull(mgr.getTransaction());
 228  1 assertEquals(0, cache.getTransactionTable().getNumGlobalTransactions());
 229  1 assertEquals(0, cache.getTransactionTable().getNumLocalTransactions());
 230  1 cache.stop();
 231    }
 232   
 233   
 234  1 public void testgtxTransactionExists() throws Exception
 235    {
 236  1 CacheImpl cache = createCache();
 237  1 MockInterceptor dummy = new MockInterceptor();
 238  1 dummy.setCache(cache);
 239   
 240  1 cache.setInterceptorChain(getAlteredInterceptorChain(dummy, cache, false));
 241  1 DummyTransactionManager mgr = DummyTransactionManager.getInstance();
 242   
 243    //start local transaction
 244  1 mgr.begin();
 245  1 Transaction tx = mgr.getTransaction();
 246   
 247    //this sets
 248  1 cache.getCurrentTransaction(tx);
 249   
 250  1 SamplePojo pojo = new SamplePojo(21, "test");
 251   
 252  1 cache.put("/one/two", "key1", pojo);
 253   
 254  1 assertNotNull(mgr.getTransaction());
 255  1 mgr.commit();
 256   
 257  1 assertNull(mgr.getTransaction());
 258   
 259  1 List calls = dummy.getAllCalled();
 260  1 assertEquals(MethodDeclarations.optimisticPrepareMethod, calls.get(0));
 261  1 assertEquals(MethodDeclarations.commitMethod, calls.get(1));
 262   
 263   
 264  1 assertEquals(0, cache.getTransactionTable().getNumGlobalTransactions());
 265  1 assertEquals(0, cache.getTransactionTable().getNumLocalTransactions());
 266   
 267  1 cache.stop();
 268    }
 269   
 270   
 271  1 public void testRemotePrepareTransaction() throws Exception
 272    {
 273   
 274  1 CacheImpl cache = createCache();
 275  1 MockInterceptor dummy = new MockInterceptor();
 276  1 dummy.setCache(cache);
 277   
 278  1 cache.setInterceptorChain(getAlteredInterceptorChain(dummy, cache, false));
 279  1 DummyTransactionManager mgr = DummyTransactionManager.getInstance();
 280   
 281    //start local transaction
 282  1 mgr.begin();
 283  1 Transaction tx = mgr.getTransaction();
 284   
 285   
 286  1 SamplePojo pojo = new SamplePojo(21, "test");
 287   
 288  1 cache.put("/one/two", "key1", pojo);
 289   
 290  1 GlobalTransaction gtx = cache.getCurrentTransaction(tx);
 291  1 TransactionTable table = cache.getTransactionTable();
 292  1 OptimisticTransactionEntry entry = (OptimisticTransactionEntry) table.get(gtx);
 293  1 assertNotNull(mgr.getTransaction());
 294  1 mgr.commit();
 295   
 296    //test local calls
 297  1 List calls = dummy.getAllCalled();
 298  1 assertEquals(MethodDeclarations.optimisticPrepareMethod, calls.get(0));
 299  1 assertEquals(MethodDeclarations.commitMethod, calls.get(1));
 300  1 assertNull(mgr.getTransaction());
 301   
 302  1 assertEquals(0, cache.getTransactionTable().getNumGlobalTransactions());
 303  1 assertEquals(0, cache.getTransactionTable().getNumLocalTransactions());
 304   
 305   
 306  1 GlobalTransaction remoteGtx = new GlobalTransaction();
 307   
 308  1 remoteGtx.setAddress(new DummyAddress());
 309    //hack the method call to make it have the remote gtx
 310  1 MethodCall meth = entry.getModifications().get(0);
 311   
 312  1 meth.getArgs()[0] = remoteGtx;
 313    //call our remote method
 314  1 MethodCall prepareMethod = MethodCallFactory.create(MethodDeclarations.optimisticPrepareMethod, remoteGtx, injectDataVersion(entry.getModifications()), null, remoteGtx.getAddress(), Boolean.FALSE);
 315  1 try
 316    {
 317  1 cache._replicate(prepareMethod);
 318    }
 319    catch (Throwable t)
 320    {
 321  0 fail();
 322    }
 323   
 324    //our thread should still be null
 325  1 assertNull(mgr.getTransaction());
 326   
 327    //there should be a registration for the remote gtx
 328  1 assertNotNull(table.get(remoteGtx));
 329  1 assertNotNull(table.getLocalTransaction(remoteGtx));
 330   
 331    //assert that the method has been passed up the stack
 332  1 calls = dummy.getAllCalled();
 333  1 assertEquals(MethodDeclarations.optimisticPrepareMethod, calls.get(2));
 334   
 335    //assert we have the tx in th table
 336  1 assertEquals(1, cache.getTransactionTable().getNumGlobalTransactions());
 337  1 assertEquals(1, cache.getTransactionTable().getNumLocalTransactions());
 338  1 cache.stop();
 339    }
 340   
 341  1 public void testRemotePrepareSuspendTransaction() throws Exception
 342    {
 343   
 344  1 CacheImpl cache = createCache();
 345  1 MockInterceptor dummy = new MockInterceptor();
 346  1 dummy.setCache(cache);
 347   
 348  1 cache.setInterceptorChain(getAlteredInterceptorChain(dummy, cache, false));
 349  1 DummyTransactionManager mgr = DummyTransactionManager.getInstance();
 350   
 351    //start local transaction
 352  1 mgr.begin();
 353  1 Transaction tx = mgr.getTransaction();
 354   
 355   
 356  1 SamplePojo pojo = new SamplePojo(21, "test");
 357   
 358  1 cache.put("/one/two", "key1", pojo);
 359   
 360  1 GlobalTransaction gtx = cache.getCurrentTransaction(tx);
 361  1 TransactionTable table = cache.getTransactionTable();
 362  1 OptimisticTransactionEntry entry = (OptimisticTransactionEntry) table.get(gtx);
 363  1 assertEquals(tx, mgr.getTransaction());
 364   
 365    //now send the remote prepare
 366   
 367  1 GlobalTransaction remoteGtx = new GlobalTransaction();
 368   
 369  1 remoteGtx.setAddress(new DummyAddress());
 370    //hack the method call to make it have the remote gtx
 371  1 MethodCall meth = entry.getModifications().get(0);
 372   
 373  1 meth.getArgs()[0] = remoteGtx;
 374    //call our remote method
 375  1 MethodCall prepareMethod = MethodCallFactory.create(MethodDeclarations.optimisticPrepareMethod, remoteGtx, injectDataVersion(entry.getModifications()), null, remoteGtx.getAddress(), Boolean.FALSE);
 376  1 try
 377    {
 378  1 cache._replicate(prepareMethod);
 379    }
 380    catch (Throwable t)
 381    {
 382  0 t.printStackTrace();
 383  0 fail();
 384    }
 385   
 386    //we should have the same transaction back again
 387  1 assertEquals(tx, mgr.getTransaction());
 388   
 389    // there should be a registration for the remote gtx
 390  1 assertNotNull(table.get(remoteGtx));
 391  1 assertNotNull(table.getLocalTransaction(remoteGtx));
 392   
 393   
 394  1 List calls = dummy.getAllCalled();
 395  1 assertEquals(MethodDeclarations.optimisticPrepareMethod, calls.get(0));
 396   
 397    //assert we have two current transactions
 398  1 assertEquals(2, cache.getTransactionTable().getNumGlobalTransactions());
 399  1 assertEquals(2, cache.getTransactionTable().getNumLocalTransactions());
 400   
 401    //commit the local tx
 402  1 mgr.commit();
 403   
 404    //check local calls
 405  1 calls = dummy.getAllCalled();
 406  1 assertEquals(MethodDeclarations.optimisticPrepareMethod, calls.get(1));
 407  1 assertEquals(MethodDeclarations.commitMethod, calls.get(2));
 408   
 409    //assert we have only 1 transaction left
 410   
 411  1 assertEquals(1, cache.getTransactionTable().getNumGlobalTransactions());
 412  1 assertEquals(1, cache.getTransactionTable().getNumLocalTransactions());
 413   
 414  1 assertNotNull(table.get(remoteGtx));
 415  1 assertNotNull(table.getLocalTransaction(remoteGtx));
 416   
 417  1 assertNull(table.get(gtx));
 418  1 assertNull(table.getLocalTransaction(gtx));
 419    //assert we are no longer associated
 420  1 assertEquals(null, mgr.getTransaction());
 421  1 cache.stop();
 422    }
 423   
 424  1 public void testRemoteCommitSuspendTransaction() throws Exception
 425    {
 426   
 427  1 CacheImpl cache = createCache();
 428  1 MockInterceptor dummy = new MockInterceptor();
 429  1 dummy.setCache(cache);
 430   
 431  1 cache.setInterceptorChain(getAlteredInterceptorChain(dummy, cache, true));
 432   
 433  1 DummyTransactionManager mgr = DummyTransactionManager.getInstance();
 434   
 435    //start local transaction
 436  1 mgr.begin();
 437  1 Transaction tx = mgr.getTransaction();
 438   
 439    //this sets
 440  1 cache.getCurrentTransaction(tx);
 441   
 442  1 SamplePojo pojo = new SamplePojo(21, "test");
 443   
 444  1 cache.put("/one/two", "key1", pojo);
 445   
 446  1 GlobalTransaction gtx = cache.getCurrentTransaction(tx);
 447  1 TransactionTable table = cache.getTransactionTable();
 448  1 OptimisticTransactionEntry entry = (OptimisticTransactionEntry) table.get(gtx);
 449  1 assertEquals(tx, mgr.getTransaction());
 450   
 451    //now send the remote prepare
 452   
 453  1 GlobalTransaction remoteGtx = new GlobalTransaction();
 454   
 455  1 remoteGtx.setAddress(new DummyAddress());
 456    //hack the method call to make it have the remote gtx
 457  1 MethodCall meth = entry.getModifications().get(0);
 458   
 459  1 meth.getArgs()[0] = remoteGtx;
 460    //call our remote method
 461  1 MethodCall prepareMethod = MethodCallFactory.create(MethodDeclarations.optimisticPrepareMethod, remoteGtx, injectDataVersion(entry.getModifications()), null, remoteGtx.getAddress(), Boolean.FALSE);
 462  1 try
 463    {
 464  1 cache._replicate(prepareMethod);
 465    }
 466    catch (Throwable t)
 467    {
 468  0 fail();
 469    }
 470   
 471  1 assertEquals(2, cache.getTransactionTable().getNumGlobalTransactions());
 472  1 assertEquals(2, cache.getTransactionTable().getNumLocalTransactions());
 473   
 474    // call our remote method
 475  1 MethodCall commitMethod = MethodCallFactory.create(MethodDeclarations.commitMethod, remoteGtx);
 476  1 try
 477    {
 478  1 cache._replicate(commitMethod);
 479    }
 480    catch (Throwable t)
 481    {
 482  0 t.printStackTrace();
 483  0 fail();
 484    }
 485   
 486    //we should have the same transaction back again
 487  1 assertEquals(tx, mgr.getTransaction());
 488   
 489    // there should be a registration for the remote gtx
 490  1 assertNull(table.get(remoteGtx));
 491  1 assertNull(table.getLocalTransaction(remoteGtx));
 492   
 493  1 List calls = dummy.getAllCalled();
 494  1 assertEquals(MethodDeclarations.optimisticPrepareMethod, calls.get(0));
 495  1 assertEquals(MethodDeclarations.commitMethod, calls.get(1));
 496   
 497  1 assertEquals(1, cache.getTransactionTable().getNumGlobalTransactions());
 498  1 assertEquals(1, cache.getTransactionTable().getNumLocalTransactions());
 499   
 500    //commit the local tx
 501  1 mgr.commit();
 502   
 503  1 calls = dummy.getAllCalled();
 504  1 assertEquals(MethodDeclarations.optimisticPrepareMethod, calls.get(2));
 505  1 assertEquals(MethodDeclarations.commitMethod, calls.get(3));
 506   
 507   
 508  1 assertEquals(0, cache.getTransactionTable().getNumGlobalTransactions());
 509  1 assertEquals(0, cache.getTransactionTable().getNumLocalTransactions());
 510   
 511  1 assertEquals(null, mgr.getTransaction());
 512  1 cache.stop();
 513    }
 514   
 515  1 public void testRemoteRollbackSuspendTransaction() throws Exception
 516    {
 517   
 518  1 CacheImpl cache = createCache();
 519  1 MockInterceptor dummy = new MockInterceptor();
 520  1 dummy.setCache(cache);
 521   
 522  1 cache.setInterceptorChain(getAlteredInterceptorChain(dummy, cache, true));
 523  1 DummyTransactionManager mgr = DummyTransactionManager.getInstance();
 524   
 525    //start local transaction
 526  1 mgr.begin();
 527  1 Transaction tx = mgr.getTransaction();
 528   
 529    //this sets
 530  1 cache.getCurrentTransaction(tx);
 531   
 532  1 SamplePojo pojo = new SamplePojo(21, "test");
 533   
 534  1 cache.put("/one/two", "key1", pojo);
 535   
 536  1 GlobalTransaction gtx = cache.getCurrentTransaction(tx);
 537  1 TransactionTable table = cache.getTransactionTable();
 538  1 OptimisticTransactionEntry entry = (OptimisticTransactionEntry) table.get(gtx);
 539  1 assertEquals(tx, mgr.getTransaction());
 540   
 541    //now send the remote prepare
 542   
 543  1 GlobalTransaction remoteGtx = new GlobalTransaction();
 544   
 545  1 remoteGtx.setAddress(new DummyAddress());
 546    //hack the method call to make it have the remote gtx
 547  1 MethodCall meth = entry.getModifications().get(0);
 548   
 549  1 meth.getArgs()[0] = remoteGtx;
 550    //call our remote method
 551  1 MethodCall prepareMethod = MethodCallFactory.create(MethodDeclarations.optimisticPrepareMethod, remoteGtx, injectDataVersion(entry.getModifications()), null, remoteGtx.getAddress(), Boolean.FALSE);
 552  1 try
 553    {
 554  1 cache._replicate(prepareMethod);
 555    }
 556    catch (Throwable t)
 557    {
 558  0 fail();
 559    }
 560   
 561  1 assertEquals(2, cache.getTransactionTable().getNumGlobalTransactions());
 562  1 assertEquals(2, cache.getTransactionTable().getNumLocalTransactions());
 563   
 564    // call our remote method
 565  1 MethodCall rollbackMethod = MethodCallFactory.create(MethodDeclarations.rollbackMethod, remoteGtx);
 566  1 try
 567    {
 568  1 cache._replicate(rollbackMethod);
 569    }
 570    catch (Throwable t)
 571    {
 572  0 fail();
 573    }
 574   
 575    //we should have the same transaction back again
 576  1 assertEquals(tx, mgr.getTransaction());
 577   
 578    // there should be a registration for the remote gtx
 579  1 assertNull(table.get(remoteGtx));
 580  1 assertNull(table.getLocalTransaction(remoteGtx));
 581   
 582  1 List calls = dummy.getAllCalled();
 583  1 assertEquals(MethodDeclarations.optimisticPrepareMethod, calls.get(0));
 584  1 assertEquals(MethodDeclarations.rollbackMethod, calls.get(1));
 585   
 586  1 assertEquals(1, cache.getTransactionTable().getNumGlobalTransactions());
 587  1 assertEquals(1, cache.getTransactionTable().getNumLocalTransactions());
 588   
 589    //commit the local tx
 590  1 mgr.commit();
 591   
 592  1 calls = dummy.getAllCalled();
 593  1 assertEquals(MethodDeclarations.optimisticPrepareMethod, calls.get(2));
 594  1 assertEquals(MethodDeclarations.commitMethod, calls.get(3));
 595   
 596   
 597  1 assertEquals(0, cache.getTransactionTable().getNumGlobalTransactions());
 598  1 assertEquals(0, cache.getTransactionTable().getNumLocalTransactions());
 599   
 600  1 assertEquals(null, mgr.getTransaction());
 601  1 cache.stop();
 602    }
 603   
 604  1 public void testRemoteCommitTransaction() throws Exception
 605    {
 606   
 607  1 CacheImpl cache = createCache();
 608  1 MockInterceptor dummy = new MockInterceptor();
 609  1 dummy.setCache(cache);
 610   
 611  1 cache.setInterceptorChain(getAlteredInterceptorChain(dummy, cache, true));
 612  1 DummyTransactionManager mgr = DummyTransactionManager.getInstance();
 613   
 614    //start local transaction
 615  1 mgr.begin();
 616  1 Transaction tx = mgr.getTransaction();
 617   
 618    //this sets
 619  1 cache.getCurrentTransaction(tx);
 620   
 621  1 SamplePojo pojo = new SamplePojo(21, "test");
 622   
 623  1 cache.put("/one/two", "key1", pojo);
 624   
 625  1 GlobalTransaction gtx = cache.getCurrentTransaction(tx);
 626  1 TransactionTable table = cache.getTransactionTable();
 627  1 OptimisticTransactionEntry entry = (OptimisticTransactionEntry) table.get(gtx);
 628  1 assertNotNull(mgr.getTransaction());
 629  1 mgr.commit();
 630   
 631    //test local calls
 632  1 List calls = dummy.getAllCalled();
 633  1 assertEquals(MethodDeclarations.optimisticPrepareMethod, calls.get(0));
 634  1 assertEquals(MethodDeclarations.commitMethod, calls.get(1));
 635   
 636  1 assertNull(mgr.getTransaction());
 637   
 638  1 assertEquals(0, cache.getTransactionTable().getNumGlobalTransactions());
 639  1 assertEquals(0, cache.getTransactionTable().getNumLocalTransactions());
 640   
 641   
 642  1 GlobalTransaction remoteGtx = new GlobalTransaction();
 643   
 644  1 remoteGtx.setAddress(new DummyAddress());
 645   
 646    // hack the method call to make it have the remote gtx
 647  1 MethodCall meth = entry.getModifications().get(0);
 648   
 649  1 meth.getArgs()[0] = remoteGtx;
 650    //call our remote method
 651  1 MethodCall prepareMethod = MethodCallFactory.create(MethodDeclarations.optimisticPrepareMethod, remoteGtx, injectDataVersion(entry.getModifications()), null, remoteGtx.getAddress(), Boolean.FALSE);
 652  1 try
 653    {
 654  1 cache._replicate(prepareMethod);
 655    }
 656    catch (Throwable t)
 657    {
 658  0 fail();
 659    }
 660   
 661    //our thread should be null
 662  1 assertNull(mgr.getTransaction());
 663  1 assertEquals(1, cache.getTransactionTable().getNumGlobalTransactions());
 664  1 assertEquals(1, cache.getTransactionTable().getNumLocalTransactions());
 665   
 666    // there should be a registration for the remote gtx
 667  1 assertNotNull(table.get(remoteGtx));
 668  1 assertNotNull(table.getLocalTransaction(remoteGtx));
 669    //this is not populated until replication interceptor is used
 670  1 assertEquals(1, table.get(remoteGtx).getModifications().size());
 671   
 672  1 calls = dummy.getAllCalled();
 673  1 assertEquals(MethodDeclarations.optimisticPrepareMethod, calls.get(2));
 674   
 675  1 assertNull(mgr.getTransaction());
 676    // call our remote method
 677  1 MethodCall commitMethod = MethodCallFactory.create(MethodDeclarations.commitMethod, remoteGtx, Boolean.TRUE);
 678  1 try
 679    {
 680  1 cache._replicate(commitMethod);
 681    }
 682    catch (Throwable t)
 683    {
 684  0 fail();
 685    }
 686   
 687  1 assertNull(table.get(remoteGtx));
 688  1 assertNull(table.getLocalTransaction(remoteGtx));
 689   
 690  1 assertEquals(0, cache.getTransactionTable().getNumGlobalTransactions());
 691  1 assertEquals(0, cache.getTransactionTable().getNumLocalTransactions());
 692  1 assertNull(mgr.getTransaction());
 693  1 cache.stop();
 694   
 695    }
 696   
 697  1 public void testRemoteRollbackTransaction() throws Exception
 698    {
 699   
 700  1 CacheImpl cache = createCache();
 701  1 MockInterceptor dummy = new MockInterceptor();
 702  1 dummy.setCache(cache);
 703   
 704  1 cache.setInterceptorChain(getAlteredInterceptorChain(dummy, cache, true));
 705  1 DummyTransactionManager mgr = DummyTransactionManager.getInstance();
 706   
 707    //start local transaction
 708  1 mgr.begin();
 709  1 Transaction tx = mgr.getTransaction();
 710   
 711    //this sets
 712  1 cache.getCurrentTransaction(tx);
 713   
 714  1 SamplePojo pojo = new SamplePojo(21, "test");
 715   
 716  1 cache.put("/one/two", "key1", pojo);
 717   
 718  1 GlobalTransaction gtx = cache.getCurrentTransaction(tx);
 719  1 TransactionTable table = cache.getTransactionTable();
 720  1 OptimisticTransactionEntry entry = (OptimisticTransactionEntry) table.get(gtx);
 721  1 assertNotNull(mgr.getTransaction());
 722  1 mgr.commit();
 723   
 724    //test local calls
 725  1 List calls = dummy.getAllCalled();
 726  1 assertEquals(MethodDeclarations.optimisticPrepareMethod, calls.get(0));
 727  1 assertEquals(MethodDeclarations.commitMethod, calls.get(1));
 728   
 729  1 GlobalTransaction remoteGtx = new GlobalTransaction();
 730   
 731  1 remoteGtx.setAddress(new DummyAddress());
 732   
 733    // hack the method call to make it have the remote gtx
 734  1 MethodCall meth = entry.getModifications().get(0);
 735   
 736  1 meth.getArgs()[0] = remoteGtx;
 737    //call our remote method
 738  1 MethodCall prepareMethod = MethodCallFactory.create(MethodDeclarations.optimisticPrepareMethod, remoteGtx, injectDataVersion(entry.getModifications()), null, remoteGtx.getAddress(), Boolean.FALSE);
 739  1 try
 740    {
 741  1 cache._replicate(prepareMethod);
 742    }
 743    catch (Throwable t)
 744    {
 745  0 fail();
 746    }
 747   
 748    //our thread should be null
 749  1 assertNull(mgr.getTransaction());
 750   
 751    // there should be a registration for the remote gtx
 752  1 assertNotNull(table.get(remoteGtx));
 753  1 assertNotNull(table.getLocalTransaction(remoteGtx));
 754    //this is not populated until replication interceptor is used
 755  1 assertEquals(1, table.get(remoteGtx).getModifications().size());
 756   
 757  1 calls = dummy.getAllCalled();
 758  1 assertEquals(MethodDeclarations.optimisticPrepareMethod, calls.get(2));
 759   
 760    // call our remote method
 761  1 MethodCall rollbackMethod = MethodCallFactory.create(MethodDeclarations.rollbackMethod, remoteGtx);
 762  1 try
 763    {
 764  1 cache._replicate(rollbackMethod);
 765    }
 766    catch (Throwable t)
 767    {
 768  0 fail();
 769    }
 770   
 771  1 calls = dummy.getAllCalled();
 772  1 assertEquals(MethodDeclarations.optimisticPrepareMethod, calls.get(2));
 773  1 assertEquals(MethodDeclarations.rollbackMethod, calls.get(3));
 774   
 775  1 assertNull(table.get(remoteGtx));
 776  1 assertNull(table.getLocalTransaction(remoteGtx));
 777   
 778  1 assertEquals(0, cache.getTransactionTable().getNumGlobalTransactions());
 779  1 assertEquals(0, cache.getTransactionTable().getNumLocalTransactions());
 780  1 cache.stop();
 781   
 782    }
 783   
 784   
 785  1 public void testSequentialTransactionExists() throws Exception
 786    {
 787   
 788  1 CacheImpl cache = createCache();
 789  1 MockInterceptor dummy = new MockInterceptor();
 790  1 dummy.setCache(cache);
 791   
 792  1 cache.setInterceptorChain(getAlteredInterceptorChain(dummy, cache, false));
 793   
 794  1 DummyTransactionManager mgr = DummyTransactionManager.getInstance();
 795   
 796  1 mgr.begin();
 797  1 Transaction tx = mgr.getTransaction();
 798  1 SamplePojo pojo = new SamplePojo(21, "test");
 799   
 800  1 cache.put("/one/two", "key1", pojo);
 801   
 802  1 assertNotNull(mgr.getTransaction());
 803  1 mgr.commit();
 804   
 805   
 806  1 mgr.begin();
 807  1 Transaction tx1 = mgr.getTransaction();
 808  1 assertNotNull(tx1);
 809  1 assertNotSame(tx, tx1);
 810  1 cache.put("/one/two", "key1", pojo);
 811  1 mgr.commit();
 812   
 813   
 814  1 List calls = dummy.getAllCalled();
 815  1 assertEquals(MethodDeclarations.optimisticPrepareMethod, calls.get(0));
 816  1 assertEquals(MethodDeclarations.commitMethod, calls.get(1));
 817   
 818  1 assertEquals(MethodDeclarations.optimisticPrepareMethod, calls.get(2));
 819  1 assertEquals(MethodDeclarations.commitMethod, calls.get(3));
 820  1 cache.stop();
 821    }
 822   
 823    }