KickJava   Java API By Example, From Geeks To Geeks.

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


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

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