KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > tests > TxCacheLoaderTest


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

7 package org.jboss.cache.tests;
8
9 import junit.framework.Test;
10 import junit.framework.TestCase;
11 import junit.framework.TestSuite;
12 import org.jboss.cache.TreeCache;
13 import org.jboss.cache.transaction.DummyTransactionManager;
14 import org.jgroups.util.Util;
15
16 import javax.transaction.NotSupportedException JavaDoc;
17 import javax.transaction.Transaction JavaDoc;
18 import javax.transaction.SystemException JavaDoc;
19 import javax.transaction.RollbackException JavaDoc;
20 import java.util.Set JavaDoc;
21
22 /**
23  * Created by IntelliJ IDEA.
24  * User: bela
25  * Date: Jun 9, 2004
26  * Time: 9:05:19 AM
27  */

28 public class TxCacheLoaderTest extends TestCase {
29    TreeCache cache1, cache2;
30
31    protected void setUp() throws Exception JavaDoc {
32       super.setUp();
33       cache1=new TreeCache();
34       cache1.setCacheMode("repl_sync");
35       cache1.setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup");
36       // cache1.setReplQueueInterval(3000);
37
cache1.createService();
38       cache1.startService();
39
40       cache2=new TreeCache();
41       cache2.setCacheMode("repl_sync");
42       cache2.setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup");
43       cache2.setLockAcquisitionTimeout(2000);
44       // cache2.setReplQueueInterval(3000);
45
cache2.createService();
46       cache2.startService();
47    }
48
49    protected void tearDown() throws Exception JavaDoc {
50       super.tearDown();
51       cache1.stopService();
52       cache1.destroyService();
53       cache2.stopService();
54       cache2.destroyService();
55    }
56
57
58
59
60
61    public void testTxPutCommit() throws Exception JavaDoc, NotSupportedException JavaDoc {
62       DummyTransactionManager mgr=DummyTransactionManager.getInstance();
63       mgr.begin();
64       Transaction JavaDoc tx=mgr.getTransaction();
65
66
67       cache1.put("/one/two/three", "key1", "val1");
68       cache1.put("/one/two/three/four", "key2", "val2");
69       assertNull(cache2.get("/one/two/three", "key1"));
70       assertNull(cache2.get("/one/two/three/four", "key2"));
71       tx.commit();
72       assertNotNull(cache1.getKeys("/one/two/three"));
73       Set JavaDoc children=cache1.getChildrenNames("/one");
74       assertEquals(1, children.size());
75       Util.sleep(100);
76       assertEquals("val1", cache2.get("/one/two/three", "key1"));
77       assertEquals("val2", cache2.get("/one/two/three/four", "key2"));
78    }
79
80
81
82    public void testTxPrepareAndRollback() throws Exception JavaDoc, NotSupportedException JavaDoc {
83       final DummyTransactionManager mgr=DummyTransactionManager.getInstance();
84       mgr.begin();
85       Transaction JavaDoc tx1=mgr.getTransaction();
86
87       cache1.setLockAcquisitionTimeout(1500);
88       cache2.setLockAcquisitionTimeout(1500);
89
90
91       Thread JavaDoc locker=new Thread JavaDoc() {
92          Transaction JavaDoc tx2=null;
93          public void run() {
94             try {
95                mgr.begin();
96                tx2=mgr.getTransaction();
97                cache2.put("/one/two/three", "block-key1", "block-val1"); // acquires a lock on cache2./one/two/three
98
Util.sleep(5000);
99             }
100             catch(Exception JavaDoc e) {
101                e.printStackTrace();
102             }
103             finally {
104                if(tx2 != null) {
105                   try {
106                      tx2.rollback();
107                   }
108                   catch(SystemException JavaDoc e) {
109                      e.printStackTrace();
110                   }
111                }
112             }
113          }
114       };
115
116       locker.start();
117       Util.sleep(1000);
118
119       cache1.put("/one/two/three", "key1", "val1");
120       cache1.put("/one/two/three/four", "key2", "val2");
121
122       try {
123          tx1.commit(); // prepare() on cache2 will fail due to lock held by locker thread
124
fail("commit() should fail because we cannot acquire the lock on cache2");
125       }
126       catch(RollbackException JavaDoc rollback) {
127          System.out.println("--- TX was rolled back (as expected)");
128          assertTrue(true);
129       }
130
131       assertNull(cache1.get("/one/two/three", "key1"));
132       assertNull(cache1.get("/one/two/three/four", "key1"));
133
134    }
135
136
137    public void testPutAfterTxCommit() throws Exception JavaDoc, NotSupportedException JavaDoc {
138       DummyTransactionManager mgr=DummyTransactionManager.getInstance();
139       mgr.begin();
140       Transaction JavaDoc tx=mgr.getTransaction();
141
142       cache1.put("/one/two/three", "key1", "val1");
143       assertTrue(cache1.exists("/one/two/three"));
144       tx.commit();
145       assertTrue(cache1.exists("/one/two/three"));
146       cache1.put("/a/b/c", null); // should be run outside a TX !
147
assertTrue(cache1.exists("/a/b/c"));
148    }
149
150    public void testPutAfterTxRollback() throws Exception JavaDoc, NotSupportedException JavaDoc {
151       DummyTransactionManager mgr=DummyTransactionManager.getInstance();
152       mgr.begin();
153       Transaction JavaDoc tx=mgr.getTransaction();
154
155       cache1.put("/one/two/three", "key1", "val1");
156       assertTrue(cache1.exists("/one/two/three"));
157       tx.rollback();
158       assertFalse(cache1.exists("/one/two/three"));
159       cache1.put("/a/b/c", null); // should be run outside a TX !
160
assertTrue(cache1.exists("/a/b/c"));
161    }
162
163
164
165
166    public static Test suite() {
167       return new TestSuite(TxCacheLoaderTest.class);
168    }
169
170    public static void main(String JavaDoc[] args) {
171       junit.textui.TestRunner.run(suite());
172    }
173
174 }
175
Popular Tags