1 8 9 package org.jboss.test.cache.test.local; 10 11 import junit.framework.Test; 12 import junit.framework.TestCase; 13 import junit.framework.TestSuite; 14 import org.jboss.cache.GlobalTransaction; 15 import org.jboss.cache.Node; 16 import org.jboss.cache.TreeCache; 17 import org.jboss.cache.lock.IdentityLock; 18 import org.jboss.cache.lock.IsolationLevel; 19 import org.jboss.cache.transaction.DummyTransactionManager; 20 21 import javax.naming.Context ; 22 import javax.naming.InitialContext ; 23 import javax.transaction.UserTransaction ; 24 import java.util.HashMap ; 25 import java.util.Map ; 26 import java.util.Properties ; 27 import java.util.Set ; 28 29 35 public class TxUnitTestCase extends TestCase { 36 TreeCache cache=null; 37 UserTransaction tx=null; 38 Properties p=null; 39 String old_factory=null; 40 final String FACTORY="org.jboss.cache.transaction.DummyContextFactory"; 41 42 43 public TxUnitTestCase(String name) { 44 super(name); 45 } 46 47 public void setUp() throws Exception { 48 super.setUp(); 49 old_factory=System.getProperty(Context.INITIAL_CONTEXT_FACTORY); 50 System.setProperty(Context.INITIAL_CONTEXT_FACTORY, FACTORY); 51 DummyTransactionManager.getInstance(); 52 if(p == null) { 53 p=new Properties (); 54 p.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.cache.transaction.DummyContextFactory"); 55 } 56 57 tx=(UserTransaction )new InitialContext (p).lookup("UserTransaction"); 58 cache=new TreeCache("test", null, 10000); 59 cache.setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup"); 60 cache.setIsolationLevel(IsolationLevel.SERIALIZABLE); 61 cache.createService(); 62 cache.startService(); 63 } 64 65 public void tearDown() throws Exception { 66 super.tearDown(); 67 if(cache != null) 68 cache.stopService(); 69 70 DummyTransactionManager.destroy(); 72 if(old_factory != null) { 73 System.setProperty(Context.INITIAL_CONTEXT_FACTORY, old_factory); 74 old_factory=null; 75 } 76 77 if(tx != null) { 78 try { 79 tx.rollback(); 80 } 81 catch(Throwable t) { 82 } 83 tx=null; 84 } 85 } 86 87 88 public void testPutTx() { 89 try { 90 tx.begin(); 91 cache.put("/a/b/c", "age", new Integer (38)); 92 assertEquals(new Integer (38), cache.get("/a/b/c", "age")); 93 94 cache.put("/a/b/c", "age", new Integer (39)); 95 tx.commit(); 96 97 assertEquals(new Integer (39), cache.get("/a/b/c", "age")); 100 } 101 catch(Throwable t) { 102 t.printStackTrace(); 103 fail(t.toString()); 104 } 105 } 106 107 108 public void testRollbackTx1() { 109 try { 110 tx.begin(); 111 cache.put("/a/b/c", "age", new Integer (38)); 112 cache.put("/a/b/c", "age", new Integer (39)); 113 tx.rollback(); 114 115 assertNull(cache.get("/a/b/c", "age")); 118 } 119 catch(Throwable t) { 120 t.printStackTrace(); 121 fail(t.toString()); 122 } 123 } 124 125 126 public void testRollbackTx2() { 127 try { 128 tx.begin(); 129 cache.put("/a/b/c", "age", new Integer (38)); 130 cache.remove("/a/b/c", "age"); 131 tx.rollback(); 132 133 assertNull(cache.get("/a/b/c", "age")); 136 } 137 catch(Throwable t) { 138 t.printStackTrace(); 139 fail(t.toString()); 140 } 141 } 142 143 public void testRollbackTx2a() { 144 try { 145 cache.put("/a/b/c", "age", new Integer (38)); 146 tx.begin(); 147 cache.remove("/a/b/c", "age"); 148 tx.rollback(); 149 150 assertEquals(new Integer (38), cache.get("/a/b/c", "age")); 153 } 154 catch(Throwable t) { 155 t.printStackTrace(); 156 fail(t.toString()); 157 } 158 } 159 160 public void testRollbackTx3() { 161 try { 162 java.util.Map map1=new java.util.HashMap (); 163 map1.put("age", new Integer (38)); 164 java.util.Map map2=new java.util.HashMap (); 165 map2.put("age", new Integer (39)); 166 tx.begin(); 167 cache.put("/a/b/c", map1); 168 cache.put("/a/b/c", map2); 169 tx.rollback(); 170 171 assertNull(cache.get("/a/b/c", "age")); 174 } 175 catch(Throwable t) { 176 t.printStackTrace(); 177 fail(t.toString()); 178 } 179 } 180 181 182 public void testRollbackTx4() { 183 try { 184 Map map=new HashMap (); 185 map.put("age", new Integer (38)); 186 tx.begin(); 187 cache.put("/a/b/c", map); 188 cache.remove("/a/b/c"); 189 tx.rollback(); 190 191 assertNull(cache.get("/a/b/c", "age")); 194 } 195 catch(Throwable t) { 196 t.printStackTrace(); 197 fail(t.toString()); 198 } 199 } 200 201 public void testNodeCreationRollback() { 202 try { 203 tx.begin(); 204 System.out.println("initial state:\n" + cache); 205 cache.put("/bela/ban", null); 206 System.out.println("after put():\n" + cache); 207 tx.rollback(); 208 System.out.println("after rollback():\n" + cache); 209 210 assertNull("node should be not existent", cache.get("/bela/ban")); 211 } 212 catch(Throwable t) { 213 t.printStackTrace(); 214 fail(t.toString()); 215 } 216 } 217 218 public void testNodeCreationRollback2() { 219 try { 220 cache.put("/bela/ban", null); 221 tx.begin(); 222 cache.put("/bela/ban/michelle", null); 223 tx.rollback(); 224 assertNotNull("node should be not null", cache.get("/bela/ban")); 225 assertNull("node should be not existent", cache.get("/bela/ban/michelle")); 226 } 227 catch(Throwable t) { 228 t.printStackTrace(); 229 fail(t.toString()); 230 } 231 } 232 233 public void testNodeDeletionRollback() { 234 try { 235 cache.put("/a/b/c", null); 236 tx.begin(); 237 cache.remove("/a/b/c"); 238 assertNull(cache.get("/a/b/c")); 239 cache.remove("/a/b"); 240 assertNull(cache.get("/a/b")); 241 cache.remove("/a"); 242 assertNull(cache.get("/a")); 243 tx.rollback(); 244 assertNotNull(cache.get("/a/b/c")); 245 assertNotNull(cache.get("/a/b")); 246 assertNotNull(cache.get("/a")); 247 } 248 catch(Throwable t) { 249 t.printStackTrace(); 250 fail(t.toString()); 251 } 252 } 253 254 public void testNodeDeletionRollback2() { 255 try { 256 cache.put("/a/b/c", null); 257 cache.put("/a/b/c1", null); 258 cache.put("/a/b/c2", null); 259 tx.begin(); 260 cache.remove("/a"); 261 assertNull(cache.get("/a/b/c")); 262 assertNull(cache.get("/a/b/c1")); 263 assertNull(cache.get("/a/b/c2")); 264 assertNull(cache.get("/a/b")); 265 assertNull(cache.get("/a")); 266 Set children=cache.getChildrenNames("/a/b"); 267 assertNull(children); 268 children=cache.getChildrenNames("/a"); 269 assertNull(children); 270 tx.rollback(); 271 assertNotNull(cache.get("/a")); 272 assertNotNull(cache.get("/a/b")); 273 assertNotNull(cache.get("/a/b/c")); 274 assertNotNull(cache.get("/a/b/c1")); 275 assertNotNull(cache.get("/a/b/c2")); 276 children=cache.getChildrenNames("/a/b"); 277 assertTrue(children.size() == 3); 278 } 279 catch(Throwable t) { 280 t.printStackTrace(); 281 fail(t.toString()); 282 } 283 } 284 285 286 287 public void testNodeDeletionRollback3() { 288 GlobalTransaction gtx; 289 try { 290 tx.begin(); 291 gtx=cache.getCurrentTransaction(); 292 cache.put("/a/b/c1", null); 293 checkLock(gtx, "/a", false); 294 checkLock(gtx, "/a/b", false); 295 checkLock(gtx, "/a/b/c1", true); 296 297 cache.put("/a/b/c2", null); 298 checkLock(gtx, "/a/b/c2", true); 299 300 cache.put("/a/b/c3", null); 301 cache.put("/a/b/c1/one", null); 302 checkLock(gtx, "/a/b/c1", true); 303 checkLock(gtx, "/a/b/c1/one", true); 304 305 cache.put("/a/b/c1/two", null); 306 cache.put("/a/b/c1/one/1", null); 307 checkLock(gtx, "/a/b/c1", true); 308 checkLock(gtx, "/a/b/c1/one", true); 309 checkLock(gtx, "/a/b/c1/one/1", true); 310 311 cache.put("/a/b/c1/two/2/3/4", null); 312 checkLock(gtx, "/a/b/c1", true); 313 checkLock(gtx, "/a/b/c1/two", true); 314 checkLock(gtx, "/a/b/c1/two/2", false); 315 checkLock(gtx, "/a/b/c1/two/2/3", false); 316 checkLock(gtx, "/a/b/c1/two/2/3/4", true); 317 318 System.out.println("locks: " + cache.printLockInfo()); 319 320 cache.remove("/a/b"); 321 tx.rollback(); 322 assertNull(cache.getChildrenNames("/a/b")); 323 } 324 catch(Throwable t) { 325 t.printStackTrace(); 326 fail(t.toString()); 327 } 328 } 329 330 public void testDoubleLocks() { 331 try { 332 tx.begin(); 333 cache.put("/a/b/c", null); 334 cache.put("/a/b/c", null); 335 336 Node n=cache.get("/a"); 337 IdentityLock lock=n.getImmutableLock(); 338 int num=lock.getReaderOwners().size(); 339 assertTrue(num == 1); 340 341 n=cache.get("/a/b"); 342 lock=n.getImmutableLock(); 343 num=lock.getReaderOwners().size(); 344 assertTrue(num == 1); 345 } 346 catch(Throwable t) { 347 t.printStackTrace(); 348 fail(t.toString()); 349 } 350 } 351 352 private void checkLock(Object owner, String fqn, boolean write_locked) throws Exception { 353 Node n=cache.get(fqn); 354 IdentityLock lock=n.getImmutableLock(); 355 if(owner == null) 356 owner=Thread.currentThread(); 357 if(lock.isLocked() == false) 358 throw new Exception ("node " + fqn + " is not locked"); 359 if(write_locked) { 360 if(lock.isWriteLocked() == false) 361 throw new Exception ("node " + fqn + " is not write-locked"); 362 } 363 else { 364 if(lock.isReadLocked() == false) 365 throw new Exception ("node " + fqn + " is not read-locked"); 366 } 367 if(lock.isOwner(owner) == false) 368 throw new Exception ("owner " + owner + "is not owner"); 369 } 370 371 public void testRemoveKeyRollback() { 372 try { 373 cache.put("/bela/ban", "name", "Bela"); 374 tx.begin(); 375 cache.remove("/bela/ban", "name"); 376 assertNull(cache.get("/bela/ban", "name")); 377 tx.rollback(); 378 assertEquals("Bela", cache.get("/bela/ban", "name")); 379 } 380 catch(Throwable t) { 381 t.printStackTrace(); 382 fail(t.toString()); 383 } 384 } 385 386 387 public void testRemoveKeyRollback2() { 388 try { 389 Map m=new HashMap (); 390 m.put("name", "Bela"); 391 m.put("id", new Integer (322649)); 392 cache.put("/bela/ban", m); 393 tx.begin(); 394 cache.remove("/bela/ban", "name"); 395 assertNull(cache.get("/bela/ban", "name")); 396 tx.rollback(); 397 assertEquals("Bela", cache.get("/bela/ban", "name")); 398 } 399 catch(Throwable t) { 400 t.printStackTrace(); 401 fail(t.toString()); 402 } 403 } 404 405 public void testRemoveKeyRollback3() { 406 try { 407 cache.put("/bela/ban", "name", "Bela"); 408 tx.begin(); 409 cache.put("/bela/ban", "name", "Michelle"); 410 cache.remove("/bela/ban", "name"); 411 assertNull(cache.get("/bela/ban", "name")); 412 tx.rollback(); 413 assertEquals("Bela", cache.get("/bela/ban", "name")); 414 } 415 catch(Throwable t) { 416 t.printStackTrace(); 417 fail(t.toString()); 418 } 419 } 420 421 422 423 424 public void testDoubleRemovalOfSameData() { 425 try { 426 tx.begin(); 427 cache.put("/foo/1", "item", new Integer (1)); 428 assertEquals(cache.get("/foo/1", "item"), new Integer (1)); 429 cache.remove("/foo/1"); 430 assertNull(cache.get("/foo/1", "item")); 431 cache.remove("/foo/1"); 432 assertNull(cache.get("/foo/1", "item")); 433 tx.rollback(); 434 assertFalse(cache.exists("/foo/1")); 435 assertNull(cache.get("/foo/1", "item")); 436 } 437 catch(Throwable t) { 438 t.printStackTrace(); 439 fail(t.toString()); 440 } 441 } 442 443 446 public void testPutDataRollback1() { 447 try { 448 cache.put("/bela/ban", null); tx.begin(); 450 Map m=new HashMap (); 451 m.put("name", "Bela"); 452 m.put("id", new Integer (322649)); 453 cache.put("/bela/ban", m); 454 tx.rollback(); 455 456 Node n=cache.get("/bela/ban"); 457 if(n.getData() == null) return; 458 assertEquals("map should be empty", 0, n.getData().size()); 459 } 460 catch(Throwable t) { 461 t.printStackTrace(); 462 fail(t.toString()); 463 } 464 } 465 466 469 public void testputDataRollback2() { 470 Map m1, m2; 471 m1=new HashMap (); 472 m1.put("name", "Bela"); 473 m1.put("id", new Integer (322649)); 474 m2=new HashMap (); 475 m2.put("other", "bla"); 476 m2.put("name", "Michelle"); 477 478 try { 479 cache.put("/bela/ban", m1); 480 tx.begin(); 481 482 cache.put("/bela/ban", m2); 483 Map tmp=cache.get("/bela/ban").getData(); 484 assertTrue(tmp.size() == 3); 485 assertTrue(tmp.get("name").equals("Michelle")); 486 assertTrue(tmp.get("id").equals(new Integer (322649))); 487 assertTrue(tmp.get("other").equals("bla")); 488 tx.rollback(); 489 490 tmp=cache.get("/bela/ban").getData(); 491 assertTrue(tmp.size() == 2); 492 assertTrue(tmp.get("name").equals("Bela")); 493 assertTrue(tmp.get("id").equals(new Integer (322649))); 494 } 495 catch(Throwable t) { 496 t.printStackTrace(); 497 fail(t.toString()); 498 } 499 } 500 501 502 public void testPutRollback() { 503 try { 504 cache.put("/bela/ban", null); tx.begin(); 506 cache.put("/bela/ban", "name", "Bela"); 507 assertEquals("Bela", cache.get("/bela/ban", "name")); 508 tx.rollback(); 509 assertNull(cache.get("/bela/ban", "name")); 510 } 511 catch(Throwable t) { 512 t.printStackTrace(); 513 fail(t.toString()); 514 } 515 } 516 517 518 public void testPutRollback2() { 519 try { 520 cache.put("/bela/ban", "name", "Bela"); tx.begin(); 522 cache.put("/bela/ban", "name", "Michelle"); 523 assertEquals("Michelle", cache.get("/bela/ban", "name")); 524 tx.rollback(); 525 assertEquals("Bela", cache.get("/bela/ban", "name")); 526 } 527 catch(Throwable t) { 528 t.printStackTrace(); 529 fail(t.toString()); 530 } 531 } 532 533 534 535 594 595 641 657 658 public static Test suite() throws Exception { 659 return new TestSuite(TxUnitTestCase.class); 661 } 662 663 public static void main(String [] args) throws Exception { 664 junit.textui.TestRunner.run(suite()); 665 } 666 667 668 } 669 | Popular Tags |