Clover coverage report -
Coverage timestamp: Thu Jul 5 2007 20:02:32 EDT
file stats: LOC: 248   Methods: 15
NCLOC: 183   Classes: 2
 
 Source file Conditionals Statements Methods TOTAL
ExplicitVersionsTest.java 58.3% 94.1% 93.3% 90.6%
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.options;
 8   
 9    import junit.framework.Assert;
 10    import junit.framework.TestCase;
 11    import org.jboss.cache.CacheImpl;
 12    import org.jboss.cache.DefaultCacheFactory;
 13    import org.jboss.cache.Fqn;
 14    import org.jboss.cache.Node;
 15    import org.jboss.cache.NodeSPI;
 16    import org.jboss.cache.config.Configuration;
 17    import org.jboss.cache.optimistic.DataVersion;
 18    import org.jboss.cache.optimistic.DefaultDataVersion;
 19   
 20    import javax.transaction.TransactionManager;
 21   
 22    /**
 23    * Tests the passing in of explicit {@see DataVersion} instances when using optimistic locking.
 24    *
 25    * @author <a href="mailto:manik@jboss.org">Manik Surtani (manik@jboss.org)</a>
 26    */
 27    public class ExplicitVersionsTest extends TestCase
 28    {
 29    private CacheImpl cache;
 30    private Fqn fqn = Fqn.fromString("/a");
 31    private String key = "key";
 32   
 33  7 protected void setUp() throws Exception
 34    {
 35  0 if (cache != null) tearDown();
 36  7 cache = (CacheImpl) DefaultCacheFactory.getInstance().createCache(false);
 37  7 cache.getConfiguration().setCacheMode(Configuration.CacheMode.LOCAL);
 38  7 cache.getConfiguration().setNodeLockingScheme("OPTIMISTIC");
 39  7 cache.getConfiguration().setTransactionManagerLookupClass("org.jboss.cache.transaction.DummyTransactionManagerLookup");
 40  7 cache.start();
 41    }
 42   
 43  7 protected void tearDown()
 44    {
 45  7 if (cache != null)
 46    {
 47  7 cache.stop();
 48  7 cache = null;
 49    }
 50    }
 51   
 52  1 public void testSimplePut() throws Exception
 53    {
 54  1 DataVersion version = new TestVersion("99");
 55  1 cache.getInvocationContext().getOptionOverrides().setDataVersion(version);
 56  1 cache.put(fqn, key, "value");
 57   
 58    //now retrieve the data from the cache.
 59  1 Assert.assertEquals("value", cache.get(fqn, key));
 60   
 61    // get a hold of the node
 62  1 NodeSPI node = (NodeSPI) cache.get(fqn);
 63  1 DataVersion versionFromCache = node.getVersion();
 64   
 65  1 Assert.assertEquals(TestVersion.class, versionFromCache.getClass());
 66  1 Assert.assertEquals("99", ((TestVersion) versionFromCache).getInternalVersion());
 67    }
 68   
 69  1 public void testFailingPut() throws Exception
 70    {
 71  1 DataVersion version = new TestVersion("99");
 72  1 cache.getInvocationContext().getOptionOverrides().setDataVersion(version);
 73  1 cache.put(fqn, key, "value");
 74   
 75  1 version = new TestVersion("25");
 76  1 cache.getInvocationContext().getOptionOverrides().setDataVersion(version);
 77  1 TransactionManager mgr = cache.getTransactionManager();
 78  1 mgr.begin();
 79  1 cache.put(fqn, key, "value2");
 80  1 try
 81    {
 82  1 mgr.commit();
 83  0 Assert.assertTrue("expected to fail", false);
 84    }
 85    catch (Exception e)
 86    {
 87    // should fail.
 88  1 Assert.assertTrue("expected to fail", true);
 89    }
 90    }
 91   
 92  1 public void testIncompatibleVersionTypes() throws Exception
 93    {
 94  1 DataVersion version = new TestVersion("99");
 95  1 cache.getInvocationContext().getOptionOverrides().setDataVersion(version);
 96  1 cache.put(fqn, key, "value");
 97  1 TransactionManager mgr = cache.getTransactionManager();
 98  1 mgr.begin();
 99  1 cache.getInvocationContext().getOptionOverrides().setDataVersion(new DefaultDataVersion(777));
 100  1 cache.put(fqn, key, "value2");
 101  1 try
 102    {
 103    // this call will use implicit versioning and will hence fail.
 104  1 mgr.commit();
 105  0 Assert.assertTrue("expected to fail", false);
 106    }
 107    catch (Exception e)
 108    {
 109    // should fail.
 110  1 Assert.assertTrue("expected to fail", true);
 111    }
 112    }
 113   
 114  1 public void testExplicitVersionOnLeaf() throws Exception
 115    {
 116  1 cache.put("/org/domain/Entity", null);
 117  1 assertEquals(1, ((DefaultDataVersion) ((NodeSPI) cache.get("/org/domain/Entity")).getVersion()).getRawVersion());
 118   
 119  1 TestVersion v = new TestVersion("Arse");
 120  1 cache.getInvocationContext().getOptionOverrides().setDataVersion(v);
 121  1 cache.put(Fqn.fromString("/org/domain/Entity/EntityInstance#1"), "k", "v");
 122   
 123  1 assertEquals(1, ((DefaultDataVersion) ((NodeSPI) cache.get("/org/domain/Entity")).getVersion()).getRawVersion());
 124  1 assertEquals(v, ((NodeSPI) cache.get("/org/domain/Entity/EntityInstance#1")).getVersion());
 125    }
 126   
 127  1 public void testExplicitVersionOnLeafImplicitParentCreation() throws Exception
 128    {
 129  1 TestVersion v = new TestVersion("Arse");
 130  1 cache.getInvocationContext().getOptionOverrides().setDataVersion(v);
 131  1 cache.put(Fqn.fromString("/org/domain/Entity/EntityInstance#1"), "k", "v");
 132   
 133  1 assertEquals(0, ((DefaultDataVersion) ((NodeSPI) cache.get("/org/domain/Entity")).getVersion()).getRawVersion());
 134  1 assertEquals(v, ((NodeSPI) cache.get("/org/domain/Entity/EntityInstance#1")).getVersion());
 135    }
 136   
 137  1 public void testExplicitVersionsOnParents()
 138    {
 139  1 Node root = cache.getRoot();
 140   
 141  1 TestVersion lev2V = new TestVersion("Lev2-v");
 142  1 cache.getInvocationContext().getOptionOverrides().setDataVersion(lev2V);
 143  1 root.addChild(Fqn.fromString("LEV2"));
 144   
 145  1 NodeSPI lev2 = (NodeSPI) root.getChild(Fqn.fromString("LEV2"));
 146   
 147  1 assertNotNull(lev2);
 148   
 149  1 assertEquals(lev2V, lev2.getVersion());
 150   
 151  1 TestVersion lev3V = new TestVersion("Lev3-v");
 152  1 cache.getInvocationContext().getOptionOverrides().setDataVersion(lev3V);
 153  1 lev2.addChild(Fqn.fromString("LEV3"));
 154   
 155  1 NodeSPI lev3 = (NodeSPI) lev2.getChild(Fqn.fromString("LEV3"));
 156   
 157  1 assertNotNull(lev3);
 158   
 159  1 assertEquals(lev3V, lev3.getVersion());
 160   
 161  1 TestVersion lev4V = new TestVersion("Lev4-v");
 162  1 cache.getInvocationContext().getOptionOverrides().setDataVersion(lev4V);
 163  1 lev3.addChild(Fqn.fromString("LEV4"));
 164   
 165  1 NodeSPI lev4 = (NodeSPI) lev3.getChild(Fqn.fromString("LEV4"));
 166   
 167  1 assertNotNull(lev4);
 168   
 169  1 assertEquals(lev4V, lev4.getVersion());
 170    }
 171   
 172  1 public void testExplicitVersionOnParentAndChild() throws Exception
 173    {
 174  1 TestVersion vParent = new TestVersion("Parent-Version");
 175   
 176  1 cache.getTransactionManager().begin();
 177  1 cache.getInvocationContext().getOptionOverrides().setDataVersion(vParent);
 178  1 cache.put(Fqn.fromString("/parent"), "k", "v");
 179  1 cache.getTransactionManager().commit();
 180   
 181  1 assertEquals(0, ((DefaultDataVersion) ((NodeSPI) cache.get("/")).getVersion()).getRawVersion());
 182  1 assertEquals(vParent, ((NodeSPI) cache.get("/parent")).getVersion());
 183   
 184  1 TestVersion vChild = new TestVersion("Child-Version");
 185   
 186  1 cache.getTransactionManager().begin();
 187  1 cache.getInvocationContext().getOptionOverrides().setDataVersion(vChild);
 188  1 cache.put(Fqn.fromString("/parent/child"), "k", "v");
 189  1 cache.getTransactionManager().commit();
 190   
 191  1 assertEquals(0, ((DefaultDataVersion) ((NodeSPI) cache.get("/")).getVersion()).getRawVersion());
 192  1 assertEquals(vParent, ((NodeSPI) cache.get("/parent")).getVersion());
 193  1 assertEquals(vChild, ((NodeSPI) cache.get("/parent/child")).getVersion());
 194    }
 195   
 196    }
 197   
 198    /**
 199    * Note that this uses STRING comparisons!!
 200    */
 201    class TestVersion implements DataVersion
 202    {
 203    private String myVersion;
 204   
 205  24 public TestVersion(String version)
 206    {
 207  24 myVersion = version;
 208    }
 209   
 210  8 public String getInternalVersion()
 211    {
 212  8 return myVersion;
 213    }
 214   
 215  0 public void setInternalVersion(String version)
 216    {
 217  0 myVersion = version;
 218    }
 219   
 220  8 public boolean newerThan(DataVersion other)
 221    {
 222  8 if (other instanceof TestVersion)
 223    {
 224  7 return myVersion.compareTo(((TestVersion) other).getInternalVersion()) > 0;
 225    }
 226    else
 227    {
 228  1 throw new IllegalArgumentException("version type mismatch");
 229    }
 230    }
 231   
 232   
 233  4 public String toString()
 234    {
 235  4 return "TestVersion-" + myVersion;
 236    }
 237   
 238  22 public boolean equals(Object other)
 239    {
 240  22 if (other instanceof TestVersion)
 241    {
 242  22 TestVersion oVersion = (TestVersion) other;
 243  0 if (oVersion.myVersion == null && myVersion == null) return true;
 244  22 if (myVersion != null) return myVersion.equals(oVersion.myVersion);
 245    }
 246  0 return false;
 247    }
 248    }