Clover coverage report -
Coverage timestamp: Wed Jan 31 2007 15:38:53 EST
file stats: LOC: 449   Methods: 15
NCLOC: 296   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
CacheTest.java 90% 98.5% 100% 98.2%
coverage coverage
 1    /*
 2    * Created on 17-Feb-2005
 3    *
 4    */
 5    package org.jboss.cache.optimistic;
 6   
 7    import junit.framework.Assert;
 8    import org.apache.commons.logging.Log;
 9    import org.apache.commons.logging.LogFactory;
 10    import org.jboss.cache.CacheImpl;
 11    import org.jboss.cache.Fqn;
 12    import org.jboss.cache.GlobalTransaction;
 13    import org.jboss.cache.OptimisticTransactionEntry;
 14    import org.jboss.cache.TransactionTable;
 15    import org.jboss.cache.config.Configuration;
 16    import org.jboss.cache.loader.SamplePojo;
 17    import org.jboss.cache.marshall.MethodCall;
 18    import org.jboss.cache.marshall.MethodCallFactory;
 19    import org.jboss.cache.marshall.MethodDeclarations;
 20    import org.jboss.cache.transaction.DummyTransactionManager;
 21   
 22    import javax.transaction.RollbackException;
 23    import javax.transaction.Transaction;
 24    import javax.transaction.TransactionManager;
 25    import java.util.List;
 26   
 27    public class CacheTest extends AbstractOptimisticTestCase
 28    {
 29    Log log = LogFactory.getLog(CacheTest.class);
 30   
 31  11 public CacheTest(String s)
 32    {
 33  11 super(s);
 34    }
 35   
 36    private CacheImpl c;
 37   
 38  11 protected void setUp() throws Exception
 39    {
 40  11 c = createCache();
 41    }
 42   
 43  11 protected void tearDown()
 44    {
 45  11 super.tearDown();
 46  11 if (c != null)
 47  11 destroyCache(c);
 48  11 c = null;
 49    }
 50   
 51  1 public void testExplicitTxFailure() throws Exception
 52    {
 53    // explicit.
 54  1 TransactionManager mgr = c.getTransactionManager();
 55  1 try
 56    {
 57  1 mgr.begin();
 58  1 c.put("/a", "k", "v");
 59  1 Transaction t = mgr.suspend();
 60  1 c.put("/a", "k2", "v2");
 61  1 mgr.resume(t);
 62  1 mgr.commit();
 63  0 Assert.assertTrue("Expecting a rollback exception!", false);
 64    }
 65    catch (RollbackException re)
 66    {
 67  1 Assert.assertTrue("Expecting a rollback exception!", true);
 68    }
 69    }
 70   
 71  1 public void testImplicitTxFailure() throws Exception
 72    {
 73    // implicit (much harder to orchestrate...
 74  1 int numThreads = 50;
 75  1 ExceptionThread thread[] = new ExceptionThread[numThreads];
 76   
 77  1 for (int i = 0; i < numThreads; i++)
 78    {
 79  50 thread[i] = new ExceptionThread()
 80    {
 81  50 public void run()
 82    {
 83  50 try
 84    {
 85  50 c.put("/a", "k", "v");
 86    }
 87    catch (Exception e)
 88    {
 89  0 log.fatal("*** Thew an exception!!", e);
 90  0 setException(e);
 91    }
 92    }
 93    };
 94    }
 95   
 96  50 for (int i = 0; i < numThreads; i++) thread[i].start();
 97  50 for (int i = 0; i < numThreads; i++) thread[i].join();
 98    // test exceptions.
 99  1 for (int i = 0; i < numThreads; i++)
 100    {
 101  50 Assert.assertNull("Thread " + thread[i].getName() + " threw exception!", thread[i].getException());
 102    }
 103    }
 104   
 105  1 public void testLocalTransaction() throws Exception
 106    {
 107  1 MockInterceptor dummy = new MockInterceptor();
 108  1 dummy.setCache(c);
 109   
 110  1 c.setInterceptorChain(getAlteredInterceptorChain(dummy, c, true));
 111   
 112  1 DummyTransactionManager mgr = DummyTransactionManager.getInstance();
 113  1 assertNull(mgr.getTransaction());
 114   
 115  1 mgr.begin();
 116   
 117  1 assertEquals(0, c.getTransactionTable().getNumGlobalTransactions());
 118  1 assertEquals(0, c.getTransactionTable().getNumLocalTransactions());
 119   
 120  1 SamplePojo pojo = new SamplePojo(21, "test");
 121   
 122  1 c.put("/one/two", "key1", pojo);
 123   
 124  1 mgr.commit();
 125   
 126  1 assertNull(mgr.getTransaction());
 127  1 assertEquals(0, c.getTransactionTable().getNumGlobalTransactions());
 128  1 assertEquals(0, c.getTransactionTable().getNumLocalTransactions());
 129   
 130    //make sure all calls were done in right order
 131   
 132  1 List calls = dummy.getAllCalled();
 133   
 134  1 assertEquals(MethodDeclarations.optimisticPrepareMethod, calls.get(0));
 135  1 assertEquals(MethodDeclarations.commitMethod, calls.get(1));
 136    }
 137   
 138  1 public void testRollbackTransaction() throws Exception
 139    {
 140   
 141  1 destroyCache(c);
 142  1 c = createCacheWithListener();
 143   
 144  1 MockInterceptor dummy = new MockInterceptor();
 145  1 dummy.setCache(c);
 146   
 147  1 c.setInterceptorChain(getAlteredInterceptorChain(dummy, c, true));
 148   
 149  1 DummyTransactionManager mgr = DummyTransactionManager.getInstance();
 150  1 assertNull(mgr.getTransaction());
 151  1 assertEquals(0, c.getTransactionTable().getNumGlobalTransactions());
 152  1 assertEquals(0, c.getTransactionTable().getNumLocalTransactions());
 153   
 154  1 SamplePojo pojo = new SamplePojo(21, "test");
 155  1 mgr.begin();
 156  1 c.put("/one/two", "key1", pojo);
 157  1 mgr.rollback();
 158  1 assertNull(mgr.getTransaction());
 159  1 assertEquals(0, c.getTransactionTable().getNumGlobalTransactions());
 160  1 assertEquals(0, c.getTransactionTable().getNumLocalTransactions());
 161   
 162    //make sure all calls were done in right order
 163   
 164  1 List calls = dummy.getAllCalled();
 165   
 166  1 assertEquals(1, calls.size());
 167  1 assertEquals(MethodDeclarations.rollbackMethod, calls.get(0));
 168    }
 169   
 170  1 public void testRemotePrepareTransaction() throws Throwable
 171    {
 172  1 destroyCache(c);
 173  1 c = createCacheWithListener();
 174   
 175  1 MockInterceptor dummy = new MockInterceptor();
 176  1 dummy.setCache(c);
 177   
 178  1 c.setInterceptorChain(getAlteredInterceptorChain(dummy, c, true));
 179  1 DummyTransactionManager mgr = DummyTransactionManager.getInstance();
 180   
 181    //start local transaction
 182  1 mgr.begin();
 183  1 Transaction tx = mgr.getTransaction();
 184   
 185    //this sets
 186  1 c.getCurrentTransaction(tx);
 187   
 188  1 SamplePojo pojo = new SamplePojo(21, "test");
 189   
 190  1 c.put("/one/two", "key1", pojo);
 191   
 192  1 GlobalTransaction gtx = c.getCurrentTransaction(tx);
 193  1 TransactionTable table = c.getTransactionTable();
 194  1 OptimisticTransactionEntry entry = (OptimisticTransactionEntry) table.get(gtx);
 195  1 assertNotNull(mgr.getTransaction());
 196  1 mgr.commit();
 197   
 198   
 199  1 GlobalTransaction remoteGtx = new GlobalTransaction();
 200   
 201  1 remoteGtx.setAddress(new DummyAddress());
 202    //hack the method call to make it have the remote gtx
 203  1 MethodCall meth = entry.getModifications().get(0);
 204   
 205  1 meth.getArgs()[0] = remoteGtx;
 206    //call our remote method
 207  1 MethodCall prepareMethod = MethodCallFactory.create(MethodDeclarations.optimisticPrepareMethod, remoteGtx, injectDataVersion(entry.getModifications()), null, remoteGtx.getAddress(), Boolean.FALSE);
 208  1 c._replicate(prepareMethod);
 209   
 210    //our thread should be null
 211  1 assertNull(mgr.getTransaction());
 212   
 213    // there should be a registration for the remote gtx
 214  1 assertNotNull(table.get(remoteGtx));
 215  1 assertNotNull(table.getLocalTransaction(remoteGtx));
 216    //assert that this is populated
 217  1 assertEquals(1, table.get(remoteGtx).getModifications().size());
 218   
 219    //assert that the remote prepare has populated the local workspace
 220  1 OptimisticTransactionEntry opEntry = (OptimisticTransactionEntry) table.get(gtx);
 221   
 222  1 assertEquals(3, entry.getTransactionWorkSpace().getNodes().size());
 223  1 assertEquals(1, entry.getModifications().size());
 224  1 List calls = dummy.getAllCalled();
 225  1 assertEquals(MethodDeclarations.optimisticPrepareMethod, calls.get(2));
 226   
 227  1 assertEquals(1, c.getTransactionTable().getNumGlobalTransactions());
 228  1 assertEquals(1, c.getTransactionTable().getNumLocalTransactions());
 229    }
 230   
 231  1 public void testRemoteCacheBroadcast() throws Exception
 232    {
 233  1 destroyCache(c);
 234   
 235  1 CacheImpl cache = createReplicatedCache(Configuration.CacheMode.REPL_SYNC);
 236  1 CacheImpl cache2 = createReplicatedCache(Configuration.CacheMode.REPL_SYNC);
 237  1 assertEquals(2, cache.getMembers().size());
 238  1 assertEquals(2, cache2.getMembers().size());
 239   
 240   
 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 GlobalTransaction gtx = cache.getCurrentTransaction(tx);
 249   
 250  1 SamplePojo pojo = new SamplePojo(21, "test");
 251   
 252  1 cache.put("/one/two", "key1", pojo);
 253   
 254    //GlobalTransaction gtx = cache.getCurrentTransaction(tx);
 255  1 TransactionTable table = cache.getTransactionTable();
 256  1 assertNotNull(mgr.getTransaction());
 257  1 mgr.commit();
 258   
 259   
 260  1 assertNull(mgr.getTransaction());
 261   
 262    //assert that the local cache is in the right state
 263  1 assertEquals(0, cache.getTransactionTable().getNumGlobalTransactions());
 264  1 assertEquals(0, cache.getTransactionTable().getNumLocalTransactions());
 265   
 266  1 assertTrue(cache.exists(Fqn.fromString("/one/two")));
 267  1 assertTrue(cache.exists(Fqn.fromString("/one")));
 268  1 assertEquals(pojo, cache.get(Fqn.fromString("/one/two"), "key1"));
 269   
 270  1 assertEquals(0, cache2.getTransactionTable().getNumGlobalTransactions());
 271  1 assertEquals(0, cache2.getTransactionTable().getNumLocalTransactions());
 272   
 273  1 assertTrue(cache2.exists(Fqn.fromString("/one/two")));
 274  1 assertTrue(cache2.exists(Fqn.fromString("/one")));
 275  1 assertEquals(pojo, cache2.get(Fqn.fromString("/one/two"), "key1"));
 276   
 277   
 278  1 destroyCache(cache);
 279  1 destroyCache(cache2);
 280    }
 281   
 282   
 283  1 public void testTwoWayRemoteCacheBroadcast() throws Exception
 284    {
 285   
 286  1 destroyCache(c);
 287  1 CacheImpl cache = createReplicatedCache(Configuration.CacheMode.REPL_SYNC);
 288  1 CacheImpl cache2 = createReplicatedCache(Configuration.CacheMode.REPL_SYNC);
 289  1 assertEquals(2, cache.getMembers().size());
 290  1 assertEquals(2, cache2.getMembers().size());
 291   
 292   
 293  1 DummyTransactionManager mgr = DummyTransactionManager.getInstance();
 294   
 295    //start local transaction
 296  1 mgr.begin();
 297  1 Transaction tx = mgr.getTransaction();
 298   
 299    //this sets
 300  1 cache.getCurrentTransaction(tx);
 301   
 302  1 SamplePojo pojo = new SamplePojo(21, "test");
 303   
 304  1 cache.put("/one/two", "key1", pojo);
 305   
 306  1 GlobalTransaction gtx = cache.getCurrentTransaction(tx);
 307  1 TransactionTable table = cache.getTransactionTable();
 308  1 assertNotNull(mgr.getTransaction());
 309  1 mgr.commit();
 310   
 311   
 312  1 assertNull(mgr.getTransaction());
 313   
 314    //assert that the local cache is in the right state
 315  1 assertEquals(0, cache.getTransactionTable().getNumGlobalTransactions());
 316  1 assertEquals(0, cache.getTransactionTable().getNumLocalTransactions());
 317   
 318  1 assertTrue(cache.exists(Fqn.fromString("/one/two")));
 319  1 assertTrue(cache.exists(Fqn.fromString("/one")));
 320  1 assertEquals(pojo, cache.get(Fqn.fromString("/one/two"), "key1"));
 321   
 322   
 323  1 assertEquals(0, cache2.getTransactionTable().getNumGlobalTransactions());
 324  1 assertEquals(0, cache2.getTransactionTable().getNumLocalTransactions());
 325   
 326  1 assertTrue(cache2.exists(Fqn.fromString("/one/two")));
 327  1 assertTrue(cache2.exists(Fqn.fromString("/one")));
 328   
 329  1 assertEquals(pojo, cache2.get(Fqn.fromString("/one/two"), "key1"));
 330   
 331   
 332  1 destroyCache(cache);
 333  1 destroyCache(cache2);
 334   
 335   
 336    }
 337   
 338   
 339  1 public void testRemotePessCacheBroadcast() throws Exception
 340    {
 341  1 destroyCache(c);
 342   
 343  1 CacheImpl cache = createPessimisticCache();
 344  1 CacheImpl cache2 = createPessimisticCache();
 345   
 346  1 DummyTransactionManager mgr = DummyTransactionManager.getInstance();
 347   
 348    //start local transaction
 349  1 mgr.begin();
 350  1 Transaction tx = mgr.getTransaction();
 351   
 352    //this sets
 353  1 cache.getCurrentTransaction(tx);
 354   
 355  1 SamplePojo pojo = new SamplePojo(21, "test");
 356   
 357  1 cache.put("/one/two", "key1", pojo);
 358   
 359   
 360  1 mgr.commit();
 361   
 362  1 destroyCache(cache);
 363  1 destroyCache(cache2);
 364   
 365    }
 366   
 367  1 public void testConcurrentNodeRemoval() throws Exception
 368    {
 369  1 c.put(fqn, "key", "value");
 370   
 371    // now start a tx to change the value in fqn
 372  1 TransactionManager mgr = c.getTransactionManager();
 373  1 mgr.begin();
 374   
 375  1 c.put(fqn, "key2", "value2");
 376   
 377  1 Transaction tx = mgr.suspend();
 378   
 379    // now remove the original node...
 380  1 c.remove(fqn);
 381   
 382  1 mgr.resume(tx);
 383    // now try and commit this - this should fail.
 384  1 boolean ok = false;
 385  1 try
 386    {
 387  1 mgr.commit();
 388    }
 389    catch (RollbackException rbe)
 390    {
 391  1 ok = true;
 392    }
 393   
 394  1 Assert.assertTrue("Concurrent mod should result in a rollback", ok);
 395    // now assert that the node has in fact been removed.
 396  1 Assert.assertTrue("The node should have been removed!", !c.exists(fqn));
 397   
 398    }
 399   
 400  1 public void testConcurrentNodeModification() throws Exception
 401    {
 402  1 c.put(fqn, "key", "value");
 403   
 404    // now start a tx to change the value in fqn
 405  1 TransactionManager mgr = c.getTransactionManager();
 406  1 mgr.begin();
 407   
 408  1 c.put(fqn, "key2", "value2");
 409   
 410  1 Transaction tx = mgr.suspend();
 411   
 412    // now change the original node...
 413  1 c.put(fqn, "key3", "value3");
 414   
 415  1 mgr.resume(tx);
 416    // now try and commit this - this should fail.
 417  1 boolean ok = false;
 418  1 try
 419    {
 420  1 mgr.commit();
 421    }
 422    catch (RollbackException rbe)
 423    {
 424  1 ok = true;
 425    }
 426   
 427  1 Assert.assertTrue("Concurrent mod should result in a rollback", ok);
 428    }
 429   
 430  1 public void testRemoveAndCreate() throws Exception
 431    {
 432  1 c = createCache();
 433  1 c.put(fqn, "key", "value");
 434  1 TransactionManager tm = c.getTransactionManager();
 435  1 tm.begin();
 436  1 c.put(fqn, "test", "test");
 437  1 tm.commit();
 438   
 439  1 assertEquals(1, c.getRoot().getChildrenNames().size());
 440   
 441  1 tm.begin();
 442  1 c.removeNode(fqn);
 443  1 c.put(fqn, "test", "test");
 444  1 tm.commit();
 445   
 446  1 assertEquals(1, c.getRoot().getChildrenNames().size());
 447   
 448    }
 449    }