KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > cache > test > local > TxUnitTestCase


1 /*
2  *
3  * JBoss, the OpenSource J2EE webOS
4  *
5  * Distributable under LGPL license.
6  * See terms of license at gnu.org.
7  */

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 JavaDoc;
22 import javax.naming.InitialContext JavaDoc;
23 import javax.transaction.UserTransaction JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.Map JavaDoc;
26 import java.util.Properties JavaDoc;
27 import java.util.Set JavaDoc;
28
29 /**
30  * Tests transactional access to a local TreeCache.
31  * Note: we use DummpyTranasctionManager to replace jta
32  *
33  * @version $Id: TxUnitTestCase.java,v 1.8.2.1 2004/12/16 03:31:45 bwang00 Exp $
34  */

35 public class TxUnitTestCase extends TestCase {
36    TreeCache cache=null;
37    UserTransaction JavaDoc tx=null;
38    Properties JavaDoc p=null;
39    String JavaDoc old_factory=null;
40    final String JavaDoc FACTORY="org.jboss.cache.transaction.DummyContextFactory";
41
42
43    public TxUnitTestCase(String JavaDoc name) {
44       super(name);
45    }
46
47    public void setUp() throws Exception JavaDoc {
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 JavaDoc();
54          p.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.cache.transaction.DummyContextFactory");
55       }
56
57       tx=(UserTransaction JavaDoc)new InitialContext JavaDoc(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 JavaDoc {
66       super.tearDown();
67       if(cache != null)
68          cache.stopService();
69
70       // BW. kind of a hack to destroy jndi binding and thread local tx before next run.
71
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 JavaDoc 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 JavaDoc(38));
92          assertEquals(new Integer JavaDoc(38), cache.get("/a/b/c", "age"));
93
94          cache.put("/a/b/c", "age", new Integer JavaDoc(39));
95          tx.commit();
96
97          // This test is done outside the TX, it wouldn't work if someone else
98
// modified "age". This works because we're the only TX running.
99
assertEquals(new Integer JavaDoc(39), cache.get("/a/b/c", "age"));
100       }
101       catch(Throwable JavaDoc 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 JavaDoc(38));
112          cache.put("/a/b/c", "age", new Integer JavaDoc(39));
113          tx.rollback();
114
115          // This test is done outside the TX, it wouldn't work if someone else
116
// modified "age". This works because we're the only TX running.
117
assertNull(cache.get("/a/b/c", "age"));
118       }
119       catch(Throwable JavaDoc 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 JavaDoc(38));
130          cache.remove("/a/b/c", "age");
131          tx.rollback();
132
133          // This test is done outside the TX, it wouldn't work if someone else
134
// modified "age". This works because we're the only TX running.
135
assertNull(cache.get("/a/b/c", "age"));
136       }
137       catch(Throwable JavaDoc 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 JavaDoc(38));
146          tx.begin();
147          cache.remove("/a/b/c", "age");
148          tx.rollback();
149
150          // This test is done outside the TX, it wouldn't work if someone else
151
// modified "age". This works because we're the only TX running.
152
assertEquals(new Integer JavaDoc(38), cache.get("/a/b/c", "age"));
153       }
154       catch(Throwable JavaDoc t) {
155          t.printStackTrace();
156          fail(t.toString());
157       }
158    }
159
160    public void testRollbackTx3() {
161       try {
162          java.util.Map JavaDoc map1=new java.util.HashMap JavaDoc();
163          map1.put("age", new Integer JavaDoc(38));
164          java.util.Map JavaDoc map2=new java.util.HashMap JavaDoc();
165          map2.put("age", new Integer JavaDoc(39));
166          tx.begin();
167          cache.put("/a/b/c", map1);
168          cache.put("/a/b/c", map2);
169          tx.rollback();
170
171          // This test is done outside the TX, it wouldn't work if someone else
172
// modified "age". This works because we're the only TX running.
173
assertNull(cache.get("/a/b/c", "age"));
174       }
175       catch(Throwable JavaDoc t) {
176          t.printStackTrace();
177          fail(t.toString());
178       }
179    }
180
181
182    public void testRollbackTx4() {
183       try {
184          Map JavaDoc map=new HashMap JavaDoc();
185          map.put("age", new Integer JavaDoc(38));
186          tx.begin();
187          cache.put("/a/b/c", map);
188          cache.remove("/a/b/c");
189          tx.rollback();
190
191          // This test is done outside the TX, it wouldn't work if someone else
192
// modified "age". This works because we're the only TX running.
193
assertNull(cache.get("/a/b/c", "age"));
194       }
195       catch(Throwable JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc t) {
347          t.printStackTrace();
348          fail(t.toString());
349       }
350    }
351
352    private void checkLock(Object JavaDoc owner, String JavaDoc fqn, boolean write_locked) throws Exception JavaDoc {
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 JavaDoc("node " + fqn + " is not locked");
359       if(write_locked) {
360          if(lock.isWriteLocked() == false)
361             throw new Exception JavaDoc("node " + fqn + " is not write-locked");
362       }
363       else {
364          if(lock.isReadLocked() == false)
365             throw new Exception JavaDoc("node " + fqn + " is not read-locked");
366       }
367       if(lock.isOwner(owner) == false)
368          throw new Exception JavaDoc("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 JavaDoc t) {
381          t.printStackTrace();
382          fail(t.toString());
383       }
384    }
385
386
387    public void testRemoveKeyRollback2() {
388       try {
389          Map JavaDoc m=new HashMap JavaDoc();
390          m.put("name", "Bela");
391          m.put("id", new Integer JavaDoc(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 JavaDoc 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 JavaDoc 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 JavaDoc(1));
428          assertEquals(cache.get("/foo/1", "item"), new Integer JavaDoc(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 JavaDoc t) {
438          t.printStackTrace();
439          fail(t.toString());
440       }
441    }
442
443    /**
444     * put(Fqn, Map) with a previous null map
445     */

446    public void testPutDataRollback1() {
447       try {
448          cache.put("/bela/ban", null); // create a node /bela/ban with a null map
449
tx.begin();
450          Map JavaDoc m=new HashMap JavaDoc();
451          m.put("name", "Bela");
452          m.put("id", new Integer JavaDoc(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 JavaDoc t) {
461          t.printStackTrace();
462          fail(t.toString());
463       }
464    }
465
466    /**
467     * put(Fqn, Map) with a previous non-null map
468     */

469    public void testputDataRollback2() {
470       Map JavaDoc m1, m2;
471       m1=new HashMap JavaDoc();
472       m1.put("name", "Bela");
473       m1.put("id", new Integer JavaDoc(322649));
474       m2=new HashMap JavaDoc();
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 JavaDoc 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 JavaDoc(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 JavaDoc(322649)));
494       }
495       catch(Throwable JavaDoc t) {
496          t.printStackTrace();
497          fail(t.toString());
498       }
499    }
500
501
502    public void testPutRollback() {
503       try {
504          cache.put("/bela/ban", null); // /bela/ban needs to exist
505
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 JavaDoc 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"); // /bela/ban needs to exist
521
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 JavaDoc t) {
528          t.printStackTrace();
529          fail(t.toString());
530       }
531    }
532
533
534
535 // public void testRaceConditionOnNotInCacheCondition() throws Exception {
536
// cache.setIsolationLevel(IsolationLevel.SERIALIZABLE);
537
//
538
// tx.begin();
539
// // we now read the null entry, and decide that we need to go do something.
540
//
541
// Object cachedObject=cache.get("/SecurityInfo/", Integer.toString(23));
542
// assertNull(cachedObject); // we expect this in this test
543
//
544
// /**
545
// * now start another Thread to go do the same action, looking for the value, but it SHOULD
546
// * see the result of the main thread put once it commits.
547
// */
548
// Thread thread=new Thread(new Runnable() {
549
// UserTransaction tx2=(UserTransaction)new InitialContext(p).lookup("UserTransaction");
550
// public void run() {
551
// try {
552
// tx2.begin();
553
// log("OtherThread: inspecting the cache");
554
// Object cachedObject=cache.get("/SecurityInfo", Integer.toString(23));
555
//
556
// log("OtherThread: read from cache: " + cachedObject);
557
// Thread.sleep(3000);
558
//
559
// cachedObject=cache.get("/SecurityInfo", Integer.toString(23));
560
// log("OtherThread: read(second time) from cache:" + cachedObject);
561
//
562
// /**
563
// * This should really fail because the other thread should actually have put something else there.
564
// */
565
// cache.put("/SecurityInfo", Integer.toString(23), "HelloWorldDIRTY!");
566
//
567
// log("OtherThread: Has put something in the cache tha shouldn't be there");
568
// }
569
// catch(Exception e) {
570
// e.printStackTrace();
571
// }
572
// finally {
573
// if(tx2 != null)
574
// try {tx2.commit();} catch(Exception e) {e.printStackTrace();}
575
// }
576
// log("OthreThread: exiting");
577
//
578
// }
579
// });
580
//
581
// thread.start();
582
// log("MainThread is now waiting a little bit");
583
// Thread.sleep(2000); // wait long enough for the other thread to block a bit. Simulate a DB read
584
// log("MainThread is now putting something in the cache");
585
// cache.put("/SecurityInfo", Integer.toString(23), "HelloWorld");
586
// Thread.sleep(2000); // wait long enough for the other thread to block a bit. Simulate a DB read
587
// tx.commit();
588
// log("MainThread: committed");
589
// thread.join(30000);
590
//
591
// log(cache.get("/SecurityInfo", Integer.toString(23)).toString());
592
// }
593

594
595 // public void testConcurrentReadsWithSerializableIsolationLevel() throws CacheException, InterruptedException {
596
// final long TIMEOUT=5000;
597
// cache.setIsolationLevel(IsolationLevel.SERIALIZABLE);
598
// cache.put("/testfqn", "testkey", "testvalue"); // add initial value, to be read by threads
599
// Reader r1, r2;
600
// r1=new Reader("reader1", 3000);
601
// r2=new Reader("reader2", 0);
602
// r1.start();
603
// pause(100); // make sure thread1 starts and acquires the lock before thread2
604
// r2.start();
605
// r1.join(TIMEOUT);
606
// r2.join(TIMEOUT);
607
// }
608
//
609
// class Reader extends Thread {
610
// long timeout;
611
//
612
// public Reader(String name, long timeout) {
613
// super(name);
614
// this.timeout=timeout;
615
// }
616
//
617
// public void run() {
618
// UserTransaction trans=null;
619
// try {
620
// trans=(UserTransaction)new InitialContext(p).lookup("UserTransaction");
621
// trans.begin();
622
// Object retval=null;
623
// log2("accessing tree");
624
// retval=cache.get("testfqn", "testkey");
625
// log2("retval: " + retval);
626
// if(timeout > 0) {
627
// log2("sleeping for " + timeout + " ms");
628
// pause(timeout);
629
// }
630
// }
631
// catch(Exception e) {
632
// e.printStackTrace();
633
// }
634
// finally {
635
// try {trans.commit();} catch(Throwable t) {}
636
// log2("done");
637
// }
638
// }
639
// }
640

641 // private void pause(long timeout) {
642
// try {
643
// Thread.sleep(timeout);
644
// }
645
// catch(InterruptedException e) {
646
// }
647
// }
648
//
649
// private void log(String msg) {
650
// System.out.println("-- [" + Thread.currentThread() + "]: " + msg);
651
// }
652
//
653
// private void log2(String msg) {
654
// System.out.println("-- [" + System.currentTimeMillis() + " " + Thread.currentThread() + "]: " + msg);
655
// }
656

657
658    public static Test suite() throws Exception JavaDoc {
659       // return getDeploySetup(TxUnitTestCase.class, "cachetest.jar");
660
return new TestSuite(TxUnitTestCase.class);
661    }
662
663    public static void main(String JavaDoc[] args) throws Exception JavaDoc {
664       junit.textui.TestRunner.run(suite());
665    }
666
667
668 }
669
Popular Tags