Clover coverage report -
Coverage timestamp: Wed Jan 31 2007 15:38:53 EST
file stats: LOC: 373   Methods: 16
NCLOC: 259   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
ExplicitVersionsReplTest.java 50% 94.4% 100% 93.4%
coverage coverage
 1    package org.jboss.cache.options;
 2   
 3    import junit.framework.Assert;
 4    import junit.framework.TestCase;
 5    import org.jboss.cache.CacheImpl;
 6    import org.jboss.cache.DefaultCacheFactory;
 7    import org.jboss.cache.Fqn;
 8    import org.jboss.cache.NodeSPI;
 9    import org.jboss.cache.config.Configuration;
 10    import org.jboss.cache.config.Option;
 11    import org.jboss.cache.misc.TestingUtil;
 12    import org.jboss.cache.optimistic.DataVersion;
 13    import org.jboss.cache.optimistic.DefaultDataVersion;
 14    import org.jboss.cache.optimistic.DataVersioningException;
 15   
 16    import javax.transaction.RollbackException;
 17    import javax.transaction.TransactionManager;
 18   
 19    /**
 20    * Tests the passing in of explicit {@see DataVersion} instances when using optimistic locking + replication.
 21    *
 22    * @author <a href="mailto:manik@jboss.org">Manik Surtani (manik@jboss.org)</a>
 23    */
 24    public class ExplicitVersionsReplTest extends TestCase
 25    {
 26    private CacheImpl cache[];
 27    private Fqn fqn = Fqn.fromString("/a");
 28    private String key = "key";
 29   
 30  12 protected void setUp() throws Exception
 31    {
 32  0 if (cache != null) tearDown();
 33  12 cache = new CacheImpl[2];
 34  12 cache[0] = createCache();
 35  12 cache[1] = createCache();
 36  12 TestingUtil.blockUntilViewsReceived(cache, 20000);
 37    }
 38   
 39  24 private CacheImpl createCache() throws Exception
 40    {
 41  24 CacheImpl cache = (CacheImpl) DefaultCacheFactory.getInstance().createCache(false);
 42  24 Configuration c = cache.getConfiguration();
 43  24 c.setCacheMode(Configuration.CacheMode.REPL_SYNC);
 44  24 c.setNodeLockingScheme("OPTIMISTIC");
 45    // give us lots of time to trace and debug shit
 46  24 c.setSyncCommitPhase(true);
 47  24 c.setSyncRollbackPhase(true);
 48  24 c.setSyncReplTimeout(1000);
 49  24 c.setLockAcquisitionTimeout(1000);
 50  24 c.setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup");
 51   
 52  24 cache.setConfiguration(c);
 53  24 cache.start();
 54  24 return cache;
 55    }
 56   
 57  12 protected void tearDown()
 58    {
 59  12 if (cache != null)
 60    {
 61  24 for (CacheImpl aCache : cache) destroyCache(aCache);
 62  12 cache = null;
 63    }
 64    }
 65   
 66  24 private void destroyCache(CacheImpl c)
 67    {
 68  24 TransactionManager tm = c.getTransactionManager();
 69  24 try
 70    {
 71  0 if (tm != null && tm.getTransaction() != null) tm.getTransaction().rollback();
 72    }
 73    catch (Exception e)
 74    {
 75    }
 76  24 c.stop();
 77    }
 78   
 79    /**
 80    * This test sets a custom data version first, expects it to replicate, and then does a put on the remote
 81    * cache using an implicit data version. Should fail with a CCE.
 82    *
 83    * @throws Exception
 84    */
 85  1 public void testIncompatibleVersionTypes1() throws Exception
 86    {
 87  1 DataVersion version = new TestVersion("99");
 88  1 cache[0].getInvocationContext().getOptionOverrides().setDataVersion(version);
 89  1 cache[0].put(fqn, key, "value");// TestVersion-99 should be on both caches now
 90   
 91  1 TransactionManager mgr = cache[0].getTransactionManager();
 92  1 mgr.begin();
 93    // don't explicitly set a data version.
 94   
 95  1 System.out.println("************ stage 2");
 96   
 97    // force an IC scrub
 98    //cache[1].getInvocationContext().setOptionOverrides(null);
 99  1 cache[1].put(fqn, key, "value2");
 100  1 try
 101    {
 102  1 mgr.commit();
 103   
 104  0 System.out.println(cache[0].printDetails());
 105  0 System.out.println(cache[1].printDetails());
 106   
 107  0 Assert.assertTrue("expected to fail", false);
 108    }
 109    catch (RollbackException e)
 110    {
 111    // should fail.
 112  1 Assert.assertTrue("expected to fail with a nested ClassCastException", true);
 113    }
 114    }
 115   
 116    /**
 117    * This test sets a custom data version first, expects it to replicate, and then does a put on the remote
 118    * cache using a higher custom data version. Should pass and not throw any exceptions.
 119    *
 120    * @throws Exception
 121    */
 122  1 public void testCompatibleVersionTypes1() throws Exception
 123    {
 124  1 DataVersion version = new TestVersion("99");
 125  1 cache[0].getInvocationContext().getOptionOverrides().setDataVersion(version);
 126  1 cache[0].put(fqn, key, "value");// TestVersion-99 should be on both caches now
 127   
 128  1 TransactionManager mgr = cache[0].getTransactionManager();
 129  1 mgr.begin();
 130   
 131  1 version = new TestVersion("999");
 132  1 cache[1].getInvocationContext().getOptionOverrides().setDataVersion(version);
 133  1 cache[1].put(fqn, key, "value2");
 134  1 mgr.commit();
 135    }
 136   
 137   
 138    /**
 139    * This test sets a custom data version first, expects it to replicate, and then does a put on the remote
 140    * cache using a lower custom data version. Should fail.
 141    *
 142    * @throws Exception
 143    */
 144  1 public void testCompatibleVersionTypesOutDatedVersion1() throws Exception
 145    {
 146  1 DataVersion version = new TestVersion("99");
 147  1 cache[0].getInvocationContext().getOptionOverrides().setDataVersion(version);
 148  1 cache[0].put(fqn, key, "value");// TestVersion-99 should be on both caches now
 149   
 150  1 TransactionManager mgr = cache[0].getTransactionManager();
 151  1 mgr.begin();
 152   
 153  1 version = new TestVersion("29");
 154  1 cache[1].getInvocationContext().getOptionOverrides().setDataVersion(version);
 155  1 cache[1].put(fqn, key, "value2");
 156  1 try
 157    {
 158  1 mgr.commit();
 159  0 Assert.assertTrue("expected to fail", false);
 160    }
 161    catch (RollbackException e)
 162    {
 163    // should fail.
 164  1 Assert.assertTrue("expected to fail with a CacheException to do with a versioning mismatch", true);
 165    }
 166    }
 167   
 168   
 169    /**
 170    * This test sets an implicit data version first, expects it to replicate, and then does a put on the remote
 171    * cache using a custom data version. Should fail with a CCE.
 172    *
 173    * @throws Exception
 174    */
 175  1 public void testIncompatibleVersionTypes2() throws Exception
 176    {
 177  1 cache[0].put(fqn, key, "value");// default data version should be on both caches now
 178   
 179  1 TransactionManager mgr = cache[0].getTransactionManager();
 180  1 mgr.begin();
 181   
 182    // explicitly set data version
 183  1 DataVersion version = new TestVersion("99");
 184  1 cache[1].getInvocationContext().getOptionOverrides().setDataVersion(version);
 185   
 186  1 try
 187    {
 188  1 cache[1].put(fqn, key, "value2");
 189  1 mgr.commit();
 190  0 Assert.assertTrue("expected to fail", false);
 191    }
 192    catch (Exception e)
 193    {
 194    // should fail.
 195  1 Assert.assertTrue("expected to fail", true);
 196    }
 197    }
 198   
 199    /**
 200    * This test sets an implicit data version first, expects it to replicate, and then does a put on the remote
 201    * cache using a higher implicit data version. Should pass and not throw any exceptions.
 202    *
 203    * @throws Exception
 204    */
 205  1 public void testCompatibleVersionTypes2() throws Exception
 206    {
 207  1 cache[0].put(fqn, key, "value");// TestVersion-99 should be on both caches now
 208   
 209  1 TransactionManager mgr = cache[0].getTransactionManager();
 210  1 mgr.begin();
 211   
 212  1 DataVersion version = new DefaultDataVersion(300);
 213  1 cache[1].getInvocationContext().getOptionOverrides().setDataVersion(version);
 214  1 cache[1].put(fqn, key, "value2");
 215  1 mgr.commit();
 216    }
 217   
 218   
 219    /**
 220    * This test sets an implicit data version first, expects it to replicate, and then does a put on the remote
 221    * cache using a lower implicit data version. Should fail.
 222    *
 223    * @throws Exception
 224    */
 225  1 public void testCompatibleVersionTypesOutDatedVersion2() throws Exception
 226    {
 227  1 DataVersion version = new DefaultDataVersion(200);
 228  1 cache[0].getInvocationContext().getOptionOverrides().setDataVersion(version);
 229  1 cache[0].put(fqn, key, "value");// TestVersion-99 should be on both caches now
 230   
 231  1 TransactionManager mgr = cache[0].getTransactionManager();
 232  1 mgr.begin();
 233   
 234  1 version = new DefaultDataVersion(100);
 235  1 cache[1].getInvocationContext().getOptionOverrides().setDataVersion(version);
 236  1 cache[1].put(fqn, key, "value2");
 237  1 try
 238    {
 239    // this call will use implicit versioning and will hence fail.
 240  1 mgr.commit();
 241  0 Assert.assertTrue("expected to fail", false);
 242    }
 243    catch (Exception e)
 244    {
 245    // should fail.
 246  1 Assert.assertTrue("expected to fail with a CacheException to do with a versioning mismatch", true);
 247    }
 248    }
 249   
 250  1 public void testPropagationOfDefaultVersions() throws Exception
 251    {
 252  1 DefaultDataVersion expected = new DefaultDataVersion();
 253  1 expected = (DefaultDataVersion) expected.increment();
 254   
 255  1 cache[0].put(fqn, key, "value");
 256   
 257  1 assertEquals("value", cache[0].get(fqn, key));
 258  1 assertEquals("value", cache[1].get(fqn, key));
 259  1 assertEquals(expected, ((NodeSPI) cache[0].get(fqn)).getVersion());
 260  1 assertEquals(expected, ((NodeSPI) cache[1].get(fqn)).getVersion());
 261   
 262  1 cache[1].put(fqn, key, "value2");
 263  1 expected = (DefaultDataVersion) expected.increment();
 264   
 265  1 assertEquals("value2", cache[0].get(fqn, key));
 266  1 assertEquals("value2", cache[1].get(fqn, key));
 267  1 assertEquals(expected, ((NodeSPI) cache[0].get(fqn)).getVersion());
 268  1 assertEquals(expected, ((NodeSPI) cache[1].get(fqn)).getVersion());
 269    }
 270   
 271  1 public void testPropagationOfCustomVersions() throws Exception
 272    {
 273  1 TestVersion expected = new TestVersion("100");
 274  1 cache[0].getInvocationContext().getOptionOverrides().setDataVersion(expected);
 275  1 cache[0].put(fqn, key, "value");
 276   
 277  1 assertEquals("value", cache[0].get(fqn, key));
 278  1 assertEquals("value", cache[1].get(fqn, key));
 279  1 assertEquals(expected, ((NodeSPI) cache[0].get(fqn)).getVersion());
 280  1 assertEquals(expected, ((NodeSPI) cache[1].get(fqn)).getVersion());
 281   
 282  1 expected = new TestVersion("200");
 283  1 cache[1].getInvocationContext().getOptionOverrides().setDataVersion(expected);
 284  1 cache[1].put(fqn, key, "value2");
 285   
 286  1 assertEquals("value2", cache[0].get(fqn, key));
 287  1 assertEquals("value2", cache[1].get(fqn, key));
 288  1 assertEquals(expected, ((NodeSPI) cache[0].get(fqn)).getVersion());
 289  1 assertEquals(expected, ((NodeSPI) cache[1].get(fqn)).getVersion());
 290    }
 291   
 292  1 public void testExplicitVersionOnRoot() throws Exception
 293    {
 294  1 TestVersion newVersion = new TestVersion("100");
 295   
 296  1 cache[0].getInvocationContext().getOptionOverrides().setDataVersion(newVersion);
 297  1 cache[0].getTransactionManager().begin();
 298  1 cache[0].put(Fqn.ROOT, "k", "v");
 299   
 300  1 try
 301    {
 302  1 cache[0].getTransactionManager().commit();
 303  0 fail("Should have barfed");
 304    }
 305    catch (RollbackException rbe)
 306    {
 307    // should barf since by default ROOT uses a default DV
 308    }
 309    }
 310   
 311  1 public void testExplicitVersionOnLeaf() throws Exception
 312    {
 313  1 cache[0].put("/org/domain/Entity", null);
 314  1 assertEquals(1, ((DefaultDataVersion) ((NodeSPI) cache[0].get("/org/domain/Entity")).getVersion()).getRawVersion());
 315  1 assertEquals(1, ((DefaultDataVersion) ((NodeSPI) cache[1].get("/org/domain/Entity")).getVersion()).getRawVersion());
 316   
 317  1 TestVersion v = new TestVersion("Arse");
 318  1 cache[0].getInvocationContext().getOptionOverrides().setDataVersion(v);
 319   
 320  1 cache[0].put(Fqn.fromString("/org/domain/Entity/EntityInstance#1"), "k", "v");
 321   
 322  1 assertEquals(2, ((DefaultDataVersion) ((NodeSPI) cache[0].get("/org/domain/Entity")).getVersion()).getRawVersion());
 323  1 assertEquals(v, ((NodeSPI) cache[0].get("/org/domain/Entity/EntityInstance#1")).getVersion());
 324  1 assertEquals(2, ((DefaultDataVersion) ((NodeSPI) cache[1].get("/org/domain/Entity")).getVersion()).getRawVersion());
 325  1 assertEquals(v, ((NodeSPI) cache[1].get("/org/domain/Entity/EntityInstance#1")).getVersion());
 326   
 327    }
 328   
 329  1 public void testExplicitVersionOnLeafImplicitParentCreation() throws Exception
 330    {
 331  1 TestVersion v = new TestVersion("Arse");
 332  1 cache[0].getInvocationContext().getOptionOverrides().setDataVersion(v);
 333   
 334  1 cache[0].put(Fqn.fromString("/org/domain/Entity/EntityInstance#1"), "k", "v");
 335   
 336  1 assertEquals(1, ((DefaultDataVersion) ((NodeSPI) cache[0].get("/org/domain/Entity")).getVersion()).getRawVersion());
 337  1 assertEquals(v, ((NodeSPI) cache[0].get("/org/domain/Entity/EntityInstance#1")).getVersion());
 338  1 assertEquals(1, ((DefaultDataVersion) ((NodeSPI) cache[1].get("/org/domain/Entity")).getVersion()).getRawVersion());
 339  1 assertEquals(v, ((NodeSPI) cache[1].get("/org/domain/Entity/EntityInstance#1")).getVersion());
 340   
 341    }
 342   
 343  1 public void testExplicitVersionOnParentAndChild() throws Exception
 344    {
 345  1 TestVersion vParent = new TestVersion("Parent-Version");
 346   
 347  1 cache[0].getTransactionManager().begin();
 348  1 cache[0].getInvocationContext().getOptionOverrides().setDataVersion(vParent);
 349  1 cache[0].put(Fqn.fromString("/parent"), "k", "v");
 350  1 cache[0].getTransactionManager().commit();
 351   
 352  1 assertEquals(1, ((DefaultDataVersion)((NodeSPI)cache[0].get("/")).getVersion()).getRawVersion());
 353  1 assertEquals(vParent, ((NodeSPI)cache[0].get("/parent")).getVersion());
 354  1 assertEquals(1, ((DefaultDataVersion)((NodeSPI)cache[1].get("/")).getVersion()).getRawVersion());
 355  1 assertEquals(vParent, ((NodeSPI)cache[1].get("/parent")).getVersion());
 356   
 357   
 358  1 TestVersion vChild = new TestVersion("Child-Version");
 359   
 360  1 cache[0].getTransactionManager().begin();
 361  1 cache[0].getInvocationContext().getOptionOverrides().setDataVersion(vChild);
 362  1 cache[0].put(Fqn.fromString("/parent/child"), "k", "v");
 363  1 cache[0].getTransactionManager().commit();
 364   
 365  1 assertEquals(1, ((DefaultDataVersion)((NodeSPI)cache[0].get("/")).getVersion()).getRawVersion());
 366  1 assertEquals(vParent, ((NodeSPI)cache[0].get("/parent")).getVersion());
 367  1 assertEquals(vChild, ((NodeSPI)cache[0].get("/parent/child")).getVersion());
 368  1 assertEquals(1, ((DefaultDataVersion)((NodeSPI)cache[1].get("/")).getVersion()).getRawVersion());
 369  1 assertEquals(vParent, ((NodeSPI)cache[1].get("/parent")).getVersion());
 370  1 assertEquals(vChild, ((NodeSPI)cache[1].get("/parent/child")).getVersion());
 371   
 372    }
 373    }