KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > lock > LockReleaseTest


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.cache.lock;
10
11 import junit.framework.Test;
12 import junit.framework.TestCase;
13 import junit.framework.TestSuite;
14 import org.apache.commons.logging.Log;
15 import org.jboss.cache.CacheImpl;
16 import org.jboss.cache.Fqn;
17 import org.jboss.cache.transaction.DummyTransactionManager;
18
19 import javax.naming.Context JavaDoc;
20 import javax.naming.InitialContext JavaDoc;
21 import javax.transaction.UserTransaction JavaDoc;
22 import java.util.Properties JavaDoc;
23 import java.util.Set JavaDoc;
24
25 /**
26  * Verifies that there are no read locks held when a transaction ends.
27  *
28  * @author Bela Ban
29  * @version $Id: LockReleaseTest.java,v 1.6 2006/12/30 17:49:58 msurtani Exp $
30  */

31 public class LockReleaseTest extends TestCase
32 {
33    CacheImpl cache = null;
34    UserTransaction JavaDoc tx = null;
35    Log log;
36    Properties JavaDoc p = null;
37    String JavaDoc old_factory = null;
38    final String JavaDoc FACTORY = "org.jboss.cache.transaction.DummyContextFactory";
39    final Fqn NODE1 = Fqn.fromString("/test");
40    final Fqn NODE2 = Fqn.fromString("/my/test");
41    final String JavaDoc KEY = "key";
42    final String JavaDoc VAL1 = "val1";
43    final String JavaDoc VAL2 = "val2";
44
45
46    public LockReleaseTest(String JavaDoc name)
47    {
48       super(name);
49    }
50
51    public void setUp() throws Exception JavaDoc
52    {
53       super.setUp();
54       old_factory = System.getProperty(Context.INITIAL_CONTEXT_FACTORY);
55       System.setProperty(Context.INITIAL_CONTEXT_FACTORY, FACTORY);
56       DummyTransactionManager.getInstance();
57       if (p == null)
58       {
59          p = new Properties JavaDoc();
60          p.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.cache.transaction.DummyContextFactory");
61       }
62       tx = (UserTransaction JavaDoc) new InitialContext JavaDoc(p).lookup("UserTransaction");
63    }
64
65    public void tearDown() throws Exception JavaDoc
66    {
67       super.tearDown();
68       if (cache != null)
69          cache.stop();
70
71       // BW. kind of a hack to destroy jndi binding and thread local tx before next run.
72
DummyTransactionManager.destroy();
73       if (old_factory != null)
74       {
75          System.setProperty(Context.INITIAL_CONTEXT_FACTORY, old_factory);
76          old_factory = null;
77       }
78
79       if (tx != null)
80       {
81          try
82          {
83             tx.rollback();
84          }
85          catch (Throwable JavaDoc t)
86          {
87          }
88          tx = null;
89       }
90    }
91
92    CacheImpl createCache(IsolationLevel level) throws Exception JavaDoc
93    {
94       CacheImpl c = new CacheImpl();
95       c.getConfiguration().setClusterName("test");
96       c.getConfiguration().setInitialStateRetrievalTimeout(10000);
97       c.getConfiguration().setTransactionManagerLookupClass("org.jboss.cache.JBossTransactionManagerLookup");
98       c.getConfiguration().setLockAcquisitionTimeout(500);
99       c.getConfiguration().setIsolationLevel(level);
100       c.create();
101       c.start();
102       return c;
103    }
104
105
106    public void testReadWithReadUncommitted() throws Exception JavaDoc
107    {
108       testReadLockRelease(IsolationLevel.READ_UNCOMMITTED);
109    }
110
111    public void testWriteWithReadUncommitted() throws Exception JavaDoc
112    {
113       testWriteLockRelease(IsolationLevel.READ_UNCOMMITTED);
114    }
115
116
117    public void testReadWithReadCommitted() throws Exception JavaDoc
118    {
119       testReadLockRelease(IsolationLevel.READ_COMMITTED);
120    }
121
122    public void testWriteWithReadCommitted() throws Exception JavaDoc
123    {
124       testWriteLockRelease(IsolationLevel.READ_COMMITTED);
125    }
126
127
128    public void testReadWithRepeatableRead() throws Exception JavaDoc
129    {
130       testReadLockRelease(IsolationLevel.REPEATABLE_READ);
131    }
132
133    public void testWriteWithRepeatableRead() throws Exception JavaDoc
134    {
135       testWriteLockRelease(IsolationLevel.REPEATABLE_READ);
136    }
137
138
139    public void testReadWithSerialzable() throws Exception JavaDoc
140    {
141       testReadLockRelease(IsolationLevel.SERIALIZABLE);
142    }
143
144    public void testWriteWithSerializable() throws Exception JavaDoc
145    {
146       testWriteLockRelease(IsolationLevel.SERIALIZABLE);
147    }
148
149
150    public void testGetKeys() throws Exception JavaDoc
151    {
152       cache = createCache(IsolationLevel.REPEATABLE_READ);
153       // add initial values outside of TX
154
cache.put(NODE1, KEY, VAL1);
155       cache.put(NODE2, KEY, VAL1);
156       assertEquals("we ran outside of a TX, locks should have been released: ", 0, cache.getNumberOfLocksHeld());
157
158       Set JavaDoc keys = cache.getKeys(NODE1);
159       System.out.println("keys of " + NODE1 + " are " + keys);
160       assertEquals("getKeys() called outside the TX should have released all locks", 0, cache.getNumberOfLocksHeld());
161
162       tx.begin();
163       keys = cache.getKeys(NODE1);
164       assertEquals("we should hold 1 read locks now: ", 2, cache.getNumberOfLocksHeld());
165       keys = cache.getKeys(NODE2);
166       assertEquals("we should hold 3 read locks now: ", 4, cache.getNumberOfLocksHeld());
167       tx.commit();
168       assertEquals("we should have released all 3 read locks: ", 0, cache.getNumberOfLocksHeld());
169    }
170
171
172    public void testGetChildrenNames() throws Exception JavaDoc
173    {
174       cache = createCache(IsolationLevel.REPEATABLE_READ);
175       // add initial values outside of TX
176
cache.put(NODE1, KEY, VAL1);
177       cache.put(NODE2, KEY, VAL1);
178       assertEquals("we ran outside of a TX, locks should have been released: ", 0, cache.getNumberOfLocksHeld());
179
180       Set JavaDoc keys = cache.getChildrenNames(NODE2);
181       System.out.println("keys of " + NODE2 + " are " + keys);
182       assertEquals("getChildrenNames() called outside the TX should have released all locks", 0,
183               cache.getNumberOfLocksHeld());
184
185       tx.begin();
186       keys = cache.getChildrenNames(NODE1);
187       assertEquals("we should hold 1 read locks now: ", 2, cache.getNumberOfLocksHeld());
188       keys = cache.getChildrenNames(NODE2);
189       assertEquals("we should hold 3 read locks now: ", 4, cache.getNumberOfLocksHeld());
190       tx.commit();
191       assertEquals("we should have released all 3 read locks: ", 0, cache.getNumberOfLocksHeld());
192    }
193
194    public void testPrint() throws Exception JavaDoc
195    {
196       cache = createCache(IsolationLevel.REPEATABLE_READ);
197       // add initial values outside of TX
198
cache.put(NODE1, KEY, VAL1);
199       cache.put(NODE2, KEY, VAL1);
200       assertEquals("we ran outside of a TX, locks should have been released: ", 0, cache.getNumberOfLocksHeld());
201
202       cache.print(NODE1);
203       assertEquals("print() called outside the TX should have released all locks", 0, cache.getNumberOfLocksHeld());
204
205       tx.begin();
206       cache.print(NODE1);
207       assertEquals("we should hold 1 read locks now (for print()): ", 2, cache.getNumberOfLocksHeld());
208       cache.print(NODE2);
209       assertEquals("we should hold 3 read locks now (for print()): ", 4, cache.getNumberOfLocksHeld());
210       tx.commit();
211       assertEquals("we should have released all 3 read locks: ", 0, cache.getNumberOfLocksHeld());
212    }
213
214
215    void testReadLockRelease(IsolationLevel level) throws Exception JavaDoc
216    {
217       cache = createCache(level);
218       // add initial values outside of TX
219
cache.put(NODE1, KEY, VAL1);
220       cache.put(NODE2, KEY, VAL1);
221
222       assertEquals("we ran outside of a TX, locks should have been released: ", 0, cache.getNumberOfLocksHeld());
223
224       tx.begin();
225       assertEquals(VAL1, cache.get(NODE1, KEY));
226       assertEquals(VAL1, cache.get(NODE2, KEY));
227       assertEquals("we should hold 3 read locks now: ", 4, cache.getNumberOfLocksHeld());
228       tx.commit();
229       assertEquals("we should have released all 3 read locks: ", 0, cache.getNumberOfLocksHeld());
230    }
231
232    void testWriteLockRelease(IsolationLevel level) throws Exception JavaDoc
233    {
234       cache = createCache(level);
235       // add initial values outside of TX
236
cache.put(NODE1, KEY, VAL1);
237       cache.put(NODE2, KEY, VAL1);
238
239       assertEquals("we ran outside of a TX, locks should have been released: ", 0, cache.getNumberOfLocksHeld());
240
241       tx.begin();
242       cache.put(NODE1, KEY, VAL1);
243       cache.put(NODE2, KEY, VAL1);
244       assertEquals("we should hold 3 write locks now: ", 4, cache.getNumberOfLocksHeld());
245       tx.commit();
246       assertEquals("we should have released all 3 write locks: ", 0, cache.getNumberOfLocksHeld());
247    }
248
249    public static Test suite() throws Exception JavaDoc
250    {
251       return new TestSuite(LockReleaseTest.class);
252    }
253
254    public static void main(String JavaDoc[] args) throws Exception JavaDoc
255    {
256       junit.textui.TestRunner.run(suite());
257    }
258
259
260 }
261
Popular Tags