1 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.Fqn; 13 import org.jboss.cache.NodeSPI; 14 import org.jboss.cache.config.Configuration; 15 import org.jboss.cache.config.Option; 16 import org.jboss.cache.optimistic.DataVersion; 17 import org.jboss.cache.optimistic.DefaultDataVersion; 18 19 import javax.transaction.TransactionManager ; 20 21 26 public class ExplicitVersionsTest extends TestCase 27 { 28 29 private CacheImpl cache; 30 private Fqn fqn = Fqn.fromString("/a"); 31 private String key = "key"; 32 private Option option = new Option(); 33 34 protected void setUp() throws Exception  35 { 36 if (cache != null) tearDown(); 37 cache = new CacheImpl(); 38 cache.getConfiguration().setCacheMode(Configuration.CacheMode.LOCAL); 39 cache.getConfiguration().setNodeLockingScheme("OPTIMISTIC"); 40 cache.getConfiguration().setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup"); 41 cache.start(); 42 } 43 44 protected void tearDown() 45 { 46 if (cache != null) 47 { 48 cache.stop(); 49 cache = null; 50 } 51 } 52 53 public void testSimplePut() throws Exception  54 { 55 DataVersion version = new TestVersion("99"); 56 cache.getInvocationContext().getOptionOverrides().setDataVersion(version); 57 cache.put(fqn, key, "value"); 58 59 Assert.assertEquals("value", cache.get(fqn, key)); 61 62 NodeSPI node = (NodeSPI) cache.get(fqn); 64 DataVersion versionFromCache = node.getVersion(); 65 66 Assert.assertEquals(TestVersion.class, versionFromCache.getClass()); 67 Assert.assertEquals("99", ((TestVersion) versionFromCache).getInternalVersion()); 68 } 69 70 public void testFailingPut() throws Exception  71 { 72 DataVersion version = new TestVersion("99"); 73 cache.getInvocationContext().getOptionOverrides().setDataVersion(version); 74 cache.put(fqn, key, "value"); 75 76 version = new TestVersion("25"); 77 cache.getInvocationContext().getOptionOverrides().setDataVersion(version); 78 TransactionManager mgr = cache.getTransactionManager(); 79 mgr.begin(); 80 cache.put(fqn, key, "value2"); 81 try 82 { 83 mgr.commit(); 84 Assert.assertTrue("expected to fail", false); 85 } 86 catch (Exception e) 87 { 88 Assert.assertTrue("expected to fail", true); 90 } 91 } 92 93 public void testIncompatibleVersionTypes() throws Exception  94 { 95 DataVersion version = new TestVersion("99"); 96 cache.getInvocationContext().getOptionOverrides().setDataVersion(version); 97 cache.put(fqn, key, "value"); 98 TransactionManager mgr = cache.getTransactionManager(); 99 mgr.begin(); 100 cache.getInvocationContext().getOptionOverrides().setDataVersion(new DefaultDataVersion(777)); 101 cache.put(fqn, key, "value2"); 102 try 103 { 104 mgr.commit(); 106 Assert.assertTrue("expected to fail", false); 107 } 108 catch (Exception e) 109 { 110 Assert.assertTrue("expected to fail", true); 112 } 113 } 114 115 public void testExplicitVersionOnLeaf() throws Exception  116 { 117 cache.put("/org/domain/Entity", null); 118 assertEquals(1, ((DefaultDataVersion) ((NodeSPI) cache.get("/org/domain/Entity")).getVersion()).getRawVersion()); 119 120 TestVersion v = new TestVersion("Arse"); 121 cache.getInvocationContext().getOptionOverrides().setDataVersion(v); 122 cache.put(Fqn.fromString("/org/domain/Entity/EntityInstance#1"), "k", "v"); 123 124 assertEquals(2, ((DefaultDataVersion) ((NodeSPI) cache.get("/org/domain/Entity")).getVersion()).getRawVersion()); 125 assertEquals(v, ((NodeSPI) cache.get("/org/domain/Entity/EntityInstance#1")).getVersion()); 126 } 127 128 } 129 130 class TestVersion implements DataVersion 131 { 132 private String myVersion; 133 134 public TestVersion(String version) 135 { 136 myVersion = version; 137 } 138 139 public String getInternalVersion() 140 { 141 return myVersion; 142 } 143 144 public void setInternalVersion(String version) 145 { 146 myVersion = version; 147 } 148 149 public boolean newerThan(DataVersion other) 150 { 151 if (other instanceof TestVersion) 152 { 153 int myInt = Integer.parseInt(myVersion); 154 int otherInt = Integer.parseInt(((TestVersion) other).getInternalVersion()); 155 return myInt > otherInt; 156 } 157 else throw new IllegalArgumentException ("version type mismatch"); 158 } 159 160 161 public String toString() 162 { 163 return "TestVersion-" + myVersion; 164 } 165 166 public boolean equals(Object other) 167 { 168 if (other instanceof TestVersion) 169 { 170 TestVersion oVersion = (TestVersion) other; 171 if (oVersion.myVersion == null && myVersion == null) return true; 172 if (myVersion != null) return myVersion.equals(oVersion.myVersion); 173 } 174 return false; 175 } 176 } | Popular Tags |