1 7 package org.jboss.cache.optimistic; 8 9 import org.jboss.cache.CacheImpl; 10 import org.jboss.cache.Fqn; 11 import org.jboss.cache.GlobalTransaction; 12 import org.jboss.cache.NodeSPI; 13 import org.jboss.cache.OptimisticTransactionEntry; 14 import org.jboss.cache.TransactionTable; 15 import org.jboss.cache.interceptors.Interceptor; 16 import org.jboss.cache.interceptors.OptimisticCreateIfNotExistsInterceptor; 17 import org.jboss.cache.interceptors.OptimisticNodeInterceptor; 18 import org.jboss.cache.interceptors.OptimisticValidatorInterceptor; 19 import org.jboss.cache.loader.SamplePojo; 20 import org.jboss.cache.marshall.MethodCall; 21 import org.jboss.cache.marshall.MethodCallFactory; 22 import org.jboss.cache.marshall.MethodDeclarations; 23 import org.jboss.cache.transaction.DummyTransactionManager; 24 25 import javax.transaction.Transaction ; 26 import java.util.HashMap ; 27 import java.util.Map ; 28 29 32 public class ValidatorInterceptorTest extends AbstractOptimisticTestCase 33 { 34 35 36 39 public ValidatorInterceptorTest(String name) 40 { 41 super(name); 42 } 43 44 public void testTransactionvalidateMethod() throws Exception 45 { 46 CacheImpl cache = createCacheWithListener(); 47 48 Interceptor validateInterceptor = new OptimisticValidatorInterceptor(); 49 validateInterceptor.setCache(cache); 50 Interceptor interceptor = new OptimisticCreateIfNotExistsInterceptor(); 51 interceptor.setCache(cache); 52 Interceptor nodeInterceptor = new OptimisticNodeInterceptor(); 53 nodeInterceptor.setCache(cache); 54 MockInterceptor dummy = new MockInterceptor(); 55 dummy.setCache(cache); 56 validateInterceptor.setNext(interceptor); 57 interceptor.setNext(nodeInterceptor); 58 nodeInterceptor.setNext(dummy); 59 60 cache.setInterceptorChain(validateInterceptor); 61 62 DummyTransactionManager mgr = DummyTransactionManager.getInstance(); 64 mgr.begin(); 65 Transaction tx = mgr.getTransaction(); 66 67 cache.getInvocationContext().setTransaction(tx); 69 cache.getInvocationContext().setGlobalTransaction(cache.getCurrentTransaction(tx)); 70 71 SamplePojo pojo = new SamplePojo(21, "test"); 72 Map temp = new HashMap (); 73 temp.put("key1", pojo); 74 cache.put("/one/two", temp); 75 76 assertEquals(null, dummy.getCalled()); 77 TransactionTable table = cache.getTransactionTable(); 78 79 GlobalTransaction gtx = table.get(tx); 80 81 OptimisticTransactionEntry entry = (OptimisticTransactionEntry) table.get(gtx); 82 83 TransactionWorkspace workspace = entry.getTransactionWorkSpace(); 84 85 89 90 assertEquals(3, workspace.getNodes().size()); 91 assertNotNull(workspace.getNode(Fqn.fromString("/one/two"))); 92 assertEquals(pojo, workspace.getNode(Fqn.fromString("/one/two")).get("key1")); 93 assertEquals(1, workspace.getNode(Fqn.fromString("/one/two")).getMergedData().size()); 94 assertTrue(entry.getLocks().isEmpty()); 95 assertEquals(1, entry.getModifications().size()); 96 assertTrue(!cache.exists("/one/two")); 97 assertEquals(null, dummy.getCalled()); 98 99 MethodCall prepareMethod = MethodCallFactory.create(MethodDeclarations.optimisticPrepareMethod, new Object []{gtx, entry.getModifications(), gtx.getAddress(), Boolean.FALSE}); 101 try 102 { 103 cache._replicate(prepareMethod); 104 } 105 catch (Throwable t) 106 { 107 108 } 109 110 111 assertEquals(3, workspace.getNodes().size()); 112 assertNotNull(workspace.getNode(Fqn.fromString("/one/two"))); 113 assertEquals(pojo, workspace.getNode(Fqn.fromString("/one/two")).get("key1")); 114 assertEquals(1, workspace.getNode(Fqn.fromString("/one/two")).getMergedData().size()); 115 assertTrue(entry.getLocks().isEmpty()); 116 assertEquals(1, entry.getModifications().size()); 117 assertTrue(!cache.exists("/one/two")); 118 assertEquals(null, dummy.getCalled()); 119 120 121 mgr.commit(); 122 123 destroyCache(cache); 124 } 125 126 public void testTransactionValidateFailureMethod() throws Exception 127 { 128 129 CacheImpl cache = createCacheWithListener(); 130 131 Interceptor validateInterceptor = new OptimisticValidatorInterceptor(); 132 validateInterceptor.setCache(cache); 133 Interceptor interceptor = new OptimisticCreateIfNotExistsInterceptor(); 134 interceptor.setCache(cache); 135 Interceptor nodeInterceptor = new OptimisticNodeInterceptor(); 136 nodeInterceptor.setCache(cache); 137 MockInterceptor dummy = new MockInterceptor(); 138 dummy.setCache(cache); 139 validateInterceptor.setNext(interceptor); 140 interceptor.setNext(nodeInterceptor); 141 nodeInterceptor.setNext(dummy); 142 143 cache.setInterceptorChain(validateInterceptor); 144 145 DummyTransactionManager mgr = DummyTransactionManager.getInstance(); 147 mgr.begin(); 148 Transaction tx = mgr.getTransaction(); 149 150 cache.getInvocationContext().setTransaction(tx); 152 cache.getInvocationContext().setGlobalTransaction(cache.getCurrentTransaction(tx)); 153 154 SamplePojo pojo = new SamplePojo(21, "test"); 155 Map temp = new HashMap (); 156 temp.put("key1", pojo); 157 cache.put("/one/two", temp); 158 159 assertEquals(null, dummy.getCalled()); 160 TransactionTable table = cache.getTransactionTable(); 161 162 GlobalTransaction gtx = table.get(tx); 163 164 OptimisticTransactionEntry entry = (OptimisticTransactionEntry) table.get(gtx); 165 166 TransactionWorkspace workspace = entry.getTransactionWorkSpace(); 167 168 172 173 assertEquals(3, workspace.getNodes().size()); 174 assertNotNull(workspace.getNode(Fqn.fromString("/one/two"))); 175 assertEquals(pojo, workspace.getNode(Fqn.fromString("/one/two")).get("key1")); 176 assertEquals(1, workspace.getNode(Fqn.fromString("/one/two")).getMergedData().size()); 177 assertTrue(entry.getLocks().isEmpty()); 178 assertEquals(1, entry.getModifications().size()); 179 assertTrue(!cache.exists("/one/two")); 180 assertEquals(null, dummy.getCalled()); 181 182 workspace.getNode(Fqn.fromString("/one/two")).getNode().setVersion(new DefaultDataVersion(2)); 184 MethodCall prepareMethod = MethodCallFactory.create(MethodDeclarations.optimisticPrepareMethod, gtx, entry.getModifications(), gtx.getAddress(), Boolean.FALSE); 186 try 187 { 188 cache._replicate(prepareMethod); 189 fail(); 190 } 191 catch (Throwable t) 192 { 193 assertTrue(true); 194 } 195 196 197 mgr.commit(); 198 199 destroyCache(cache); 200 } 201 202 public void testTransactionValidateCommitMethod() throws Exception 203 { 204 205 CacheImpl cache = createCacheWithListener(); 206 207 Interceptor validateInterceptor = new OptimisticValidatorInterceptor(); 208 validateInterceptor.setCache(cache); 209 Interceptor interceptor = new OptimisticCreateIfNotExistsInterceptor(); 210 interceptor.setCache(cache); 211 Interceptor nodeInterceptor = new OptimisticNodeInterceptor(); 212 nodeInterceptor.setCache(cache); 213 MockInterceptor dummy = new MockInterceptor(); 214 dummy.setCache(cache); 215 validateInterceptor.setNext(interceptor); 216 interceptor.setNext(nodeInterceptor); 217 nodeInterceptor.setNext(dummy); 218 219 cache.setInterceptorChain(validateInterceptor); 220 221 DummyTransactionManager mgr = DummyTransactionManager.getInstance(); 223 mgr.begin(); 224 Transaction tx = mgr.getTransaction(); 225 226 cache.getInvocationContext().setTransaction(tx); 228 cache.getInvocationContext().setGlobalTransaction(cache.getCurrentTransaction(tx)); 229 230 SamplePojo pojo = new SamplePojo(21, "test"); 231 Map temp = new HashMap (); 232 temp.put("key1", pojo); 233 cache.put("/one/two", temp); 234 235 assertEquals(null, dummy.getCalled()); 236 TransactionTable table = cache.getTransactionTable(); 237 238 GlobalTransaction gtx = table.get(tx); 239 240 OptimisticTransactionEntry entry = (OptimisticTransactionEntry) table.get(gtx); 241 242 TransactionWorkspace workspace = entry.getTransactionWorkSpace(); 243 244 248 249 assertEquals(3, workspace.getNodes().size()); 250 assertNotNull(workspace.getNode(Fqn.fromString("/one/two"))); 251 assertEquals(pojo, workspace.getNode(Fqn.fromString("/one/two")).get("key1")); 252 assertEquals(1, workspace.getNode(Fqn.fromString("/one/two")).getMergedData().size()); 253 assertTrue(entry.getLocks().isEmpty()); 254 assertEquals(1, entry.getModifications().size()); 255 assertTrue(!cache.exists("/one/two")); 256 assertEquals(null, dummy.getCalled()); 257 258 MethodCall prepareMethod = MethodCallFactory.create(MethodDeclarations.optimisticPrepareMethod, new Object []{gtx, entry.getModifications(), gtx.getAddress(), Boolean.FALSE}); 261 try 262 { 263 cache._replicate(prepareMethod); 264 fail(); 265 } 266 catch (Throwable t) 267 { 268 assertTrue(true); 269 } 270 271 MethodCall commitMethod = MethodCallFactory.create(MethodDeclarations.commitMethod, new Object []{gtx}); 272 try 273 { 274 cache._replicate(commitMethod); 275 } 276 catch (Throwable t) 277 { 278 fail(); 279 } 280 281 282 assertEquals(3, workspace.getNodes().size()); 283 assertNotNull(workspace.getNode(Fqn.fromString("/one/two"))); 284 assertEquals(pojo, workspace.getNode(Fqn.fromString("/one/two")).get("key1")); 285 assertEquals(1, workspace.getNode(Fqn.fromString("/one/two")).getMergedData().size()); 286 assertTrue(entry.getLocks().isEmpty()); 287 assertEquals(1, entry.getModifications().size()); 288 289 290 assertEquals(null, dummy.getCalled()); 291 NodeSPI node = workspace.getNode(Fqn.fromString("/")).getNode(); 292 294 assertNotNull(node); 295 node = (NodeSPI) node.getChild("one"); 296 assertEquals(new DefaultDataVersion(1), node.getVersion()); 297 assertNotNull(node); 298 assertTrue(cache.exists(node.getFqn())); 299 assertEquals(new DefaultDataVersion(1), node.getVersion()); 300 node = (NodeSPI) node.getChild("two"); 301 assertNotNull(node); 302 assertTrue(cache.exists(node.getFqn())); 303 assertEquals(new DefaultDataVersion(1), node.getVersion()); 304 305 assertEquals(pojo, node.get("key1")); 306 307 mgr.commit(); 308 309 destroyCache(cache); 310 } 311 312 313 public void testTransactionValidateFailRemoteCommitMethod() throws Exception 314 { 315 316 CacheImpl cache = createCacheWithListener(); 317 318 Interceptor validateInterceptor = new OptimisticValidatorInterceptor(); 319 validateInterceptor.setCache(cache); 320 Interceptor interceptor = new OptimisticCreateIfNotExistsInterceptor(); 321 interceptor.setCache(cache); 322 Interceptor nodeInterceptor = new OptimisticNodeInterceptor(); 323 nodeInterceptor.setCache(cache); 324 MockInterceptor dummy = new MockInterceptor(); 325 dummy.setCache(cache); 326 validateInterceptor.setNext(interceptor); 327 interceptor.setNext(nodeInterceptor); 328 nodeInterceptor.setNext(dummy); 329 330 cache.setInterceptorChain(validateInterceptor); 331 332 DummyTransactionManager mgr = DummyTransactionManager.getInstance(); 334 mgr.begin(); 335 Transaction tx = mgr.getTransaction(); 336 337 cache.getInvocationContext().setTransaction(tx); 339 cache.getInvocationContext().setGlobalTransaction(cache.getCurrentTransaction(tx)); 340 341 SamplePojo pojo = new SamplePojo(21, "test"); 342 Map temp = new HashMap (); 343 temp.put("key1", pojo); 344 cache.put("/one/two", temp); 345 346 assertEquals(null, dummy.getCalled()); 347 TransactionTable table = cache.getTransactionTable(); 348 349 GlobalTransaction gtx = table.get(tx); 350 351 OptimisticTransactionEntry entry = (OptimisticTransactionEntry) table.get(gtx); 352 353 TransactionWorkspace workspace = entry.getTransactionWorkSpace(); 354 355 assertEquals(3, workspace.getNodes().size()); 356 assertNotNull(workspace.getNode(Fqn.fromString("/one/two"))); 357 assertEquals(pojo, workspace.getNode(Fqn.fromString("/one/two")).get("key1")); 358 assertEquals(1, workspace.getNode(Fqn.fromString("/one/two")).getMergedData().size()); 359 assertTrue(entry.getLocks().isEmpty()); 360 assertEquals(1, entry.getModifications().size()); 361 assertTrue(!cache.exists("/one/two")); 362 assertEquals(null, dummy.getCalled()); 363 364 MethodCall prepareMethod = MethodCallFactory.create(MethodDeclarations.optimisticPrepareMethod, new Object []{gtx, entry.getModifications(), gtx.getAddress(), Boolean.FALSE}); 367 try 368 { 369 cache._replicate(prepareMethod); 370 fail(); 371 } 372 catch (Throwable t) 373 { 374 assertTrue(true); 375 } 376 377 378 MethodCall commitMethod = MethodCallFactory.create(MethodDeclarations.commitMethod, new Object []{gtx}); 379 try 380 { 381 cache._replicate(commitMethod); 382 } 383 catch (Throwable t) 384 { 385 fail(); 386 } 387 388 389 assertEquals(3, workspace.getNodes().size()); 390 assertNotNull(workspace.getNode(Fqn.fromString("/one/two"))); 391 assertEquals(pojo, workspace.getNode(Fqn.fromString("/one/two")).get("key1")); 392 assertEquals(1, workspace.getNode(Fqn.fromString("/one/two")).getMergedData().size()); 393 assertTrue(entry.getLocks().isEmpty()); 394 assertEquals(1, entry.getModifications().size()); 395 396 397 assertEquals(null, dummy.getCalled()); 398 NodeSPI node = workspace.getNode(Fqn.fromString("/")).getNode(); 399 401 assertNotNull(node); 402 node = (NodeSPI) node.getChild("one"); 403 assertEquals(new DefaultDataVersion(1), node.getVersion()); 404 assertNotNull(node); 405 assertTrue(cache.exists(node.getFqn())); 406 assertEquals(new DefaultDataVersion(1), node.getVersion()); 407 node = (NodeSPI) node.getChild("two"); 408 assertNotNull(node); 409 assertTrue(cache.exists(node.getFqn())); 410 assertEquals(new DefaultDataVersion(1), node.getVersion()); 411 412 assertEquals(pojo, node.get("key1")); 413 414 mgr.commit(); 415 416 destroyCache(cache); 417 } 418 419 public void testTransactionValidateRollbackMethod() throws Exception 420 { 421 422 CacheImpl cache = createCacheWithListener(); 423 Interceptor validateInterceptor = new OptimisticValidatorInterceptor(); 424 validateInterceptor.setCache(cache); 425 Interceptor interceptor = new OptimisticCreateIfNotExistsInterceptor(); 426 interceptor.setCache(cache); 427 Interceptor nodeInterceptor = new OptimisticNodeInterceptor(); 428 nodeInterceptor.setCache(cache); 429 MockInterceptor dummy = new MockInterceptor(); 430 dummy.setCache(cache); 431 validateInterceptor.setNext(interceptor); 432 interceptor.setNext(nodeInterceptor); 433 nodeInterceptor.setNext(dummy); 434 435 cache.setInterceptorChain(validateInterceptor); 436 437 DummyTransactionManager mgr = DummyTransactionManager.getInstance(); 439 mgr.begin(); 440 Transaction tx = mgr.getTransaction(); 441 442 cache.getInvocationContext().setTransaction(tx); 444 cache.getInvocationContext().setGlobalTransaction(cache.getCurrentTransaction(tx)); 445 446 SamplePojo pojo = new SamplePojo(21, "test"); 447 Map temp = new HashMap (); 448 temp.put("key1", pojo); 449 cache.put("/one/two", temp); 450 451 assertEquals(null, dummy.getCalled()); 452 TransactionTable table = cache.getTransactionTable(); 453 454 GlobalTransaction gtx = table.get(tx); 455 456 OptimisticTransactionEntry entry = (OptimisticTransactionEntry) table.get(gtx); 457 458 TransactionWorkspace workspace = entry.getTransactionWorkSpace(); 459 460 464 465 assertEquals(3, workspace.getNodes().size()); 466 assertNotNull(workspace.getNode(Fqn.fromString("/one/two"))); 467 assertEquals(pojo, workspace.getNode(Fqn.fromString("/one/two")).get("key1")); 468 assertEquals(1, workspace.getNode(Fqn.fromString("/one/two")).getMergedData().size()); 469 assertTrue(entry.getLocks().isEmpty()); 470 assertEquals(1, entry.getModifications().size()); 471 assertTrue(!cache.exists("/one/two")); 472 assertEquals(null, dummy.getCalled()); 473 474 MethodCall prepareMethod = MethodCallFactory.create(MethodDeclarations.optimisticPrepareMethod, new Object []{gtx, entry.getModifications(), gtx.getAddress(), Boolean.FALSE}); 477 try 478 { 479 cache._replicate(prepareMethod); 480 fail(); 481 } 482 catch (Throwable t) 483 { 484 assertTrue(true); 485 } 486 487 MethodCall rollbackMethod = MethodCallFactory.create(MethodDeclarations.rollbackMethod, new Object []{gtx}); 488 try 489 { 490 cache._replicate(rollbackMethod); 491 } 492 catch (Throwable t) 493 { 494 fail(); 495 } 496 497 498 assertEquals(0, workspace.getNodes().size()); 499 assertNull(workspace.getNode(Fqn.fromString("/one/two"))); 500 assertNull(workspace.getNode(Fqn.fromString("/one"))); 501 assertTrue(entry.getLocks().isEmpty()); 502 assertEquals(1, entry.getModifications().size()); 503 504 mgr.commit(); 505 506 destroyCache(cache); 507 } 508 509 } 510 | Popular Tags |