Clover coverage report -
Coverage timestamp: Wed Jan 31 2007 15:38:53 EST
file stats: LOC: 123   Methods: 5
NCLOC: 82   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
OptimisticVersioningTest.java - 98% 100% 98.1%
coverage coverage
 1    /*
 2    * JBoss, Home of Professional Open Source
 3    *
 4    * Distributable under LGPL license.
 5    * See terms of license at gnu.org.
 6    */
 7    package org.jboss.cache.optimistic;
 8   
 9    import junit.framework.Assert;
 10    import org.jboss.cache.CacheImpl;
 11    import org.jboss.cache.Fqn;
 12    import org.jboss.cache.NodeSPI;
 13    import org.jboss.cache.config.Configuration;
 14   
 15    import javax.transaction.RollbackException;
 16    import javax.transaction.Transaction;
 17    import javax.transaction.TransactionManager;
 18   
 19    /**
 20    * Unit test that covers versioning of data and workspace nodes when using optimistic locking.
 21    *
 22    * @author <a href="mailto:manik@jboss.org">Manik Surtani (manik@jboss.org)</a>
 23    */
 24    public class OptimisticVersioningTest extends AbstractOptimisticTestCase
 25    {
 26    CacheImpl cache1, cache2;
 27   
 28  2 public OptimisticVersioningTest(String name)
 29    {
 30  2 super(name);
 31    }
 32   
 33  2 protected void setUp() throws Exception
 34    {
 35  2 cache1 = createReplicatedCache(Configuration.CacheMode.REPL_SYNC);
 36  2 cache2 = createReplicatedCache(Configuration.CacheMode.REPL_SYNC);
 37    }
 38   
 39  2 protected void tearDown()
 40    {
 41  2 super.tearDown();
 42  2 destroyCache(cache1);
 43  2 destroyCache(cache2);
 44  2 cache1 = null;
 45  2 cache2 = null;
 46    }
 47   
 48  1 public void testVersionPropagation() throws Exception
 49    {
 50  1 Fqn fqn = Fqn.fromString("/a/b");
 51  1 String key = "key";
 52   
 53  1 cache1.put(fqn, key, "value");
 54   
 55  1 DataVersion v1 = ((NodeSPI) cache1.get(fqn)).getVersion();
 56  1 DataVersion v2 = ((NodeSPI) cache2.get(fqn)).getVersion();
 57   
 58  1 Assert.assertEquals("value", cache1.get(fqn, key));
 59  1 Assert.assertEquals("value", cache2.get(fqn, key));
 60  1 Assert.assertEquals("Version info should have propagated", v1, v2);
 61   
 62    // change stuff in the node again...
 63  1 cache1.put(fqn, key, "value2");
 64   
 65  1 v1 = ((NodeSPI) cache1.get(fqn)).getVersion();
 66  1 v2 = ((NodeSPI) cache2.get(fqn)).getVersion();
 67   
 68  1 Assert.assertEquals("value2", cache1.get(fqn, key));
 69  1 Assert.assertEquals("value2", cache2.get(fqn, key));
 70  1 Assert.assertEquals("Version info should have propagated", v1, v2);
 71    }
 72   
 73  1 public void testTwoCachesUpdatingSimultaneously() throws Exception
 74    {
 75  1 TransactionManager mgr1 = cache1.getTransactionManager();
 76  1 TransactionManager mgr2 = cache2.getTransactionManager();
 77  1 Transaction tx1, tx2;
 78   
 79  1 Fqn fqn = Fqn.fromString("/a/b");
 80  1 String key = "key";
 81   
 82  1 cache1.put(fqn, key, "value");
 83   
 84  1 DataVersion v1 = ((NodeSPI) cache1.get(fqn)).getVersion();
 85  1 DataVersion v2 = ((NodeSPI) cache2.get(fqn)).getVersion();
 86   
 87  1 Assert.assertEquals("value", cache1.get(fqn, key));
 88  1 Assert.assertEquals("value", cache2.get(fqn, key));
 89  1 Assert.assertEquals("Version info should have propagated", v1, v2);
 90   
 91    // Start a tx on cache 1
 92  1 mgr1.begin();
 93  1 cache1.put(fqn, key, "value2");
 94  1 tx1 = mgr1.suspend();
 95   
 96    // start a tx on cache 2
 97  1 mgr2.begin();
 98  1 cache2.put(fqn, key, "value3");
 99  1 tx2 = mgr2.suspend();
 100   
 101    // which tx completes, which fail?
 102  1 mgr1.resume(tx1);
 103    // should succeed...
 104  1 mgr1.commit();
 105   
 106  1 try
 107    {
 108  1 mgr2.resume(tx2);
 109  1 mgr2.commit();
 110  0 Assert.assertTrue("Should have failed", false);
 111    }
 112    catch (RollbackException rbe)
 113    {
 114  1 Assert.assertTrue("Should have failed", true);
 115    }
 116   
 117    // data versions should be in sync.
 118  1 v1 = ((NodeSPI) cache1.get(fqn)).getVersion();
 119  1 v2 = ((NodeSPI) cache2.get(fqn)).getVersion();
 120   
 121  1 Assert.assertEquals("Version info should have propagated", v1, v2);
 122    }
 123    }