KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > tests > 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.tests.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.Fqn;
16 import org.jboss.cache.TreeCache;
17 import org.jboss.cache.lock.IsolationLevel;
18 import org.jboss.cache.transaction.DummyTransactionManager;
19
20 import javax.naming.Context JavaDoc;
21 import javax.naming.InitialContext JavaDoc;
22 import javax.transaction.UserTransaction JavaDoc;
23 import java.util.Properties JavaDoc;
24 import java.util.Set JavaDoc;
25
26 /**
27  * Verifies that there are no read locks held when a transaction ends.
28  *
29  * @author Bela Ban
30  * @version $Id: LockReleaseTest.java,v 1.2 2005/04/05 17:19:43 belaban Exp $
31  */

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