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.Fqn; 7 import org.jboss.cache.NodeSPI; 8 import org.jboss.cache.config.Configuration; 9 import org.jboss.cache.misc.TestingUtil; 10 import org.jboss.cache.optimistic.DataVersion; 11 import org.jboss.cache.optimistic.DefaultDataVersion; 12 13 import javax.transaction.RollbackException ; 14 import javax.transaction.TransactionManager ; 15 16 21 public class ExplicitVersionsReplTest extends TestCase 22 { 23 private CacheImpl cache[]; 24 private Fqn fqn = Fqn.fromString("/a"); 25 private String key = "key"; 26 27 protected void setUp() throws Exception 28 { 29 if (cache != null) tearDown(); 30 cache = new CacheImpl[2]; 31 cache[0] = createCache(); 32 cache[1] = createCache(); 33 TestingUtil.blockUntilViewsReceived(cache, 20000); 34 } 35 36 private CacheImpl createCache() throws Exception 37 { 38 CacheImpl cache = new CacheImpl(); 39 Configuration c = cache.getConfiguration(); 40 c.setCacheMode(Configuration.CacheMode.REPL_SYNC); 41 c.setNodeLockingScheme("OPTIMISTIC"); 42 c.setSyncCommitPhase(true); 44 c.setSyncRollbackPhase(true); 45 c.setSyncReplTimeout(1000); 46 c.setLockAcquisitionTimeout(1000); 47 c.setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup"); 48 49 cache.setConfiguration(c); 50 cache.start(); 51 return cache; 52 } 53 54 protected void tearDown() 55 { 56 if (cache != null) 57 { 58 for (CacheImpl aCache : cache) destroyCache(aCache); 59 cache = null; 60 } 61 } 62 63 private void destroyCache(CacheImpl c) 64 { 65 TransactionManager tm = c.getTransactionManager(); 66 try 67 { 68 if (tm != null && tm.getTransaction() != null) tm.getTransaction().rollback(); 69 } 70 catch (Exception e) 71 { 72 } 73 c.stop(); 74 } 75 76 82 public void testIncompatibleVersionTypes1() throws Exception 83 { 84 DataVersion version = new TestVersion("99"); 85 cache[0].getInvocationContext().getOptionOverrides().setDataVersion(version); 86 cache[0].put(fqn, key, "value"); 88 TransactionManager mgr = cache[0].getTransactionManager(); 89 mgr.begin(); 90 92 System.out.println("************ stage 2"); 93 94 cache[1].put(fqn, key, "value2"); 97 try 98 { 99 mgr.commit(); 100 101 System.out.println(cache[0].printDetails()); 102 System.out.println(cache[1].printDetails()); 103 104 Assert.assertTrue("expected to fail", false); 105 } 106 catch (RollbackException e) 107 { 108 Assert.assertTrue("expected to fail with a nested ClassCastException", true); 110 } 111 } 112 113 119 public void testCompatibleVersionTypes1() throws Exception 120 { 121 DataVersion version = new TestVersion("99"); 122 cache[0].getInvocationContext().getOptionOverrides().setDataVersion(version); 123 cache[0].put(fqn, key, "value"); 125 TransactionManager mgr = cache[0].getTransactionManager(); 126 mgr.begin(); 127 128 version = new TestVersion("199"); 129 cache[1].getInvocationContext().getOptionOverrides().setDataVersion(version); 130 cache[1].put(fqn, key, "value2"); 131 mgr.commit(); 132 } 133 134 135 141 public void testCompatibleVersionTypesOutDatedVersion1() throws Exception 142 { 143 DataVersion version = new TestVersion("99"); 144 cache[0].getInvocationContext().getOptionOverrides().setDataVersion(version); 145 cache[0].put(fqn, key, "value"); 147 TransactionManager mgr = cache[0].getTransactionManager(); 148 mgr.begin(); 149 150 version = new TestVersion("29"); 151 cache[1].getInvocationContext().getOptionOverrides().setDataVersion(version); 152 cache[1].put(fqn, key, "value2"); 153 try 154 { 155 mgr.commit(); 156 Assert.assertTrue("expected to fail", false); 157 } 158 catch (RollbackException e) 159 { 160 Assert.assertTrue("expected to fail with a CacheException to do with a versioning mismatch", true); 162 } 163 } 164 165 166 172 public void testIncompatibleVersionTypes2() throws Exception 173 { 174 cache[0].put(fqn, key, "value"); 176 TransactionManager mgr = cache[0].getTransactionManager(); 177 mgr.begin(); 178 179 DataVersion version = new TestVersion("99"); 181 cache[1].getInvocationContext().getOptionOverrides().setDataVersion(version); 182 183 try 184 { 185 cache[1].put(fqn, key, "value2"); 186 mgr.commit(); 187 Assert.assertTrue("expected to fail", false); 188 } 189 catch (Exception e) 190 { 191 Assert.assertTrue("expected to fail", true); 193 } 194 } 195 196 202 public void testCompatibleVersionTypes2() throws Exception 203 { 204 cache[0].put(fqn, key, "value"); 206 TransactionManager mgr = cache[0].getTransactionManager(); 207 mgr.begin(); 208 209 DataVersion version = new DefaultDataVersion(300); 210 cache[1].getInvocationContext().getOptionOverrides().setDataVersion(version); 211 cache[1].put(fqn, key, "value2"); 212 mgr.commit(); 213 } 214 215 216 222 public void testCompatibleVersionTypesOutDatedVersion2() throws Exception 223 { 224 DataVersion version = new DefaultDataVersion(200); 225 cache[0].getInvocationContext().getOptionOverrides().setDataVersion(version); 226 cache[0].put(fqn, key, "value"); 228 TransactionManager mgr = cache[0].getTransactionManager(); 229 mgr.begin(); 230 231 version = new DefaultDataVersion(100); 232 cache[1].getInvocationContext().getOptionOverrides().setDataVersion(version); 233 cache[1].put(fqn, key, "value2"); 234 try 235 { 236 mgr.commit(); 238 Assert.assertTrue("expected to fail", false); 239 } 240 catch (Exception e) 241 { 242 Assert.assertTrue("expected to fail with a CacheException to do with a versioning mismatch", true); 244 } 245 } 246 247 public void testPropagationOfDefaultVersions() throws Exception 248 { 249 DefaultDataVersion expected = new DefaultDataVersion(); 250 expected = (DefaultDataVersion) expected.increment(); 251 252 cache[0].put(fqn, key, "value"); 253 254 assertEquals("value", cache[0].get(fqn, key)); 255 assertEquals("value", cache[1].get(fqn, key)); 256 assertEquals(expected, ((NodeSPI) cache[0].get(fqn)).getVersion()); 257 assertEquals(expected, ((NodeSPI) cache[1].get(fqn)).getVersion()); 258 259 cache[1].put(fqn, key, "value2"); 260 expected = (DefaultDataVersion) expected.increment(); 261 262 assertEquals("value2", cache[0].get(fqn, key)); 263 assertEquals("value2", cache[1].get(fqn, key)); 264 assertEquals(expected, ((NodeSPI) cache[0].get(fqn)).getVersion()); 265 assertEquals(expected, ((NodeSPI) cache[1].get(fqn)).getVersion()); 266 } 267 268 public void testPropagationOfCustomVersions() throws Exception 269 { 270 TestVersion expected = new TestVersion("100"); 271 cache[0].getInvocationContext().getOptionOverrides().setDataVersion(expected); 272 cache[0].put(fqn, key, "value"); 273 274 assertEquals("value", cache[0].get(fqn, key)); 275 assertEquals("value", cache[1].get(fqn, key)); 276 assertEquals(expected, ((NodeSPI) cache[0].get(fqn)).getVersion()); 277 assertEquals(expected, ((NodeSPI) cache[1].get(fqn)).getVersion()); 278 279 expected = new TestVersion("200"); 280 cache[1].getInvocationContext().getOptionOverrides().setDataVersion(expected); 281 cache[1].put(fqn, key, "value2"); 282 283 assertEquals("value2", cache[0].get(fqn, key)); 284 assertEquals("value2", cache[1].get(fqn, key)); 285 assertEquals(expected, ((NodeSPI) cache[0].get(fqn)).getVersion()); 286 assertEquals(expected, ((NodeSPI) cache[1].get(fqn)).getVersion()); 287 } 288 289 public void testExplicitVersionOnRoot() throws Exception 290 { 291 TestVersion newVersion = new TestVersion("100"); 292 293 cache[0].getInvocationContext().getOptionOverrides().setDataVersion(newVersion); 294 cache[0].getTransactionManager().begin(); 295 cache[0].put(Fqn.ROOT, "k", "v"); 296 297 cache[0].getTransactionManager().commit(); 299 } 300 301 public void testExplicitVersionOnLeaf() throws Exception 302 { 303 cache[0].put("/org/domain/Entity", null); 304 assertEquals(1, ((DefaultDataVersion) ((NodeSPI) cache[0].get("/org/domain/Entity")).getVersion()).getRawVersion()); 305 assertEquals(1, ((DefaultDataVersion) ((NodeSPI) cache[1].get("/org/domain/Entity")).getVersion()).getRawVersion()); 306 307 TestVersion v = new TestVersion("Arse"); 308 cache[0].getInvocationContext().getOptionOverrides().setDataVersion(v); 309 310 cache[0].put(Fqn.fromString("/org/domain/Entity/EntityInstance#1"), "k", "v"); 311 312 assertEquals(2, ((DefaultDataVersion) ((NodeSPI) cache[0].get("/org/domain/Entity")).getVersion()).getRawVersion()); 313 assertEquals(v, ((NodeSPI) cache[0].get("/org/domain/Entity/EntityInstance#1")).getVersion()); 314 assertEquals(2, ((DefaultDataVersion) ((NodeSPI) cache[1].get("/org/domain/Entity")).getVersion()).getRawVersion()); 315 assertEquals(v, ((NodeSPI) cache[1].get("/org/domain/Entity/EntityInstance#1")).getVersion()); 316 317 } 318 319 320 } 321 | Popular Tags |