KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.jboss.cache.loader;
2
3 import junit.framework.Test;
4 import junit.framework.TestSuite;
5 import org.jboss.cache.CacheImpl;
6 import org.jboss.cache.Fqn;
7 import org.jboss.cache.misc.TestingUtil;
8 import org.jboss.cache.transaction.DummyTransactionManager;
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.io.File JavaDoc;
15 import java.util.Set JavaDoc;
16
17 /**
18  * @author Bela Ban
19  * @version $Id: TxCacheLoaderTest.java,v 1.8 2006/12/30 17:49:57 msurtani Exp $
20  */

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