KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > cache > test > local > LockReleaseUnitTestCase


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.test.cache.test.local;
10
11 import junit.framework.Test;
12 import junit.framework.TestCase;
13 import junit.framework.TestSuite;
14 import org.jboss.cache.TreeCache;
15 import org.jboss.cache.Fqn;
16 import org.jboss.cache.lock.IsolationLevel;
17 import org.jboss.cache.transaction.DummyTransactionManager;
18 import org.jboss.logging.Logger;
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: LockReleaseUnitTestCase.java,v 1.3.4.2 2004/12/16 03:31:44 bwang00 Exp $
31  */

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