KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > tests > lock > UpgradeLockTest


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.TreeCache;
16 import org.jboss.cache.lock.IsolationLevel;
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
24 /**
25  * Tests upgrade locks from read -> write
26  *
27  * @author Bela Ban
28  * @version $Id: UpgradeLockTest.java,v 1.2 2005/04/05 17:19:43 belaban Exp $
29  */

30 public class UpgradeLockTest extends TestCase {
31    TreeCache cache=null;
32    UserTransaction JavaDoc tx=null;
33    Log log;
34    Properties JavaDoc p=null;
35    String JavaDoc old_factory=null;
36    final String JavaDoc FACTORY="org.jboss.cache.transaction.DummyContextFactory";
37    final String JavaDoc NODE1="/test";
38    final String JavaDoc NODE2="/my/test";
39    final String JavaDoc KEY="key";
40    final String JavaDoc VAL1="val1";
41    final String JavaDoc VAL2="val2";
42
43
44    public UpgradeLockTest(String JavaDoc name) {
45       super(name);
46    }
47
48    public void setUp() throws Exception JavaDoc {
49       super.setUp();
50       old_factory=System.getProperty(Context.INITIAL_CONTEXT_FACTORY);
51       System.setProperty(Context.INITIAL_CONTEXT_FACTORY, FACTORY);
52       DummyTransactionManager.getInstance();
53       if(p == null) {
54          p=new Properties JavaDoc();
55          p.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.cache.transaction.DummyContextFactory");
56       }
57       tx=(UserTransaction JavaDoc)new InitialContext JavaDoc(p).lookup("UserTransaction");
58    }
59
60    public void tearDown() throws Exception JavaDoc {
61       super.tearDown();
62       if(cache != null)
63          cache.stopService();
64
65       // BW. kind of a hack to destroy jndi binding and thread local tx before next run.
66
DummyTransactionManager.destroy();
67       if(old_factory != null) {
68          System.setProperty(Context.INITIAL_CONTEXT_FACTORY, old_factory);
69          old_factory=null;
70       }
71
72       if(tx != null) {
73          try {
74             tx.rollback();
75          }
76          catch(Throwable JavaDoc t) {
77          }
78          tx=null;
79       }
80    }
81
82    TreeCache createCache(IsolationLevel level) throws Exception JavaDoc {
83       TreeCache c=new TreeCache("test", null, 10000);
84       c.setTransactionManagerLookupClass("org.jboss.cache.JBossTransactionManagerLookup");
85       c.setLockAcquisitionTimeout(500);
86       c.setIsolationLevel(level);
87       c.createService();
88       c.startService();
89       return c;
90    }
91
92
93    public void testUpgradeWithNone() throws Exception JavaDoc {
94       runTestWithIsolationLevel(IsolationLevel.NONE);
95    }
96
97
98    public void testUpgradeWithReadUncommitted() throws Exception JavaDoc {
99       runTestWithIsolationLevel(IsolationLevel.READ_UNCOMMITTED);
100    }
101
102    public void testUpgradeWithReadCommitted() throws Exception JavaDoc {
103       runTestWithIsolationLevel(IsolationLevel.READ_COMMITTED);
104    }
105
106    public void testUpgradeWithRepeatableRead() throws Exception JavaDoc {
107       runTestWithIsolationLevel(IsolationLevel.REPEATABLE_READ);
108    }
109
110    public void testUpgradeWithSerializable() throws Exception JavaDoc {
111       runTestWithIsolationLevel(IsolationLevel.SERIALIZABLE);
112    }
113
114    public void testIsolationLevelSerializable() throws Exception JavaDoc {
115       _testIsolationLevel(IsolationLevel.SERIALIZABLE);
116    }
117
118    public void testIsolationLevelNone() throws Exception JavaDoc {
119       _testIsolationLevel(IsolationLevel.NONE);
120    }
121
122
123    void _testIsolationLevel(IsolationLevel l) throws Exception JavaDoc {
124       cache=createCache(l);
125       tx.begin();
126
127       int expected_num_locks=l == IsolationLevel.NONE? 0 : 1;
128
129       cache.put(NODE1, null);
130       assertEquals(expected_num_locks, cache.getNumberOfLocksHeld());
131
132       cache.put(NODE1, null);
133       assertEquals(expected_num_locks, cache.getNumberOfLocksHeld());
134
135       tx.rollback();
136       assertEquals(0, cache.getNumberOfLocksHeld());
137    }
138
139
140
141    void runTestWithIsolationLevel(IsolationLevel level) throws Exception JavaDoc {
142       cache=createCache(level);
143       // add initial values outside of TX
144
cache.put(NODE1, KEY, VAL1);
145       cache.put(NODE2, KEY, VAL1);
146
147       tx.begin();
148       try {
149          assertEquals(VAL1, cache.get(NODE1, KEY));
150          assertEquals(VAL1, cache.get(NODE2, KEY));
151
152          cache.put(NODE1, KEY, VAL2); // causes read lock to upgrade to r/w lock
153
cache.put(NODE2, KEY, VAL2); // causes read lock to upgrade to r/w lock
154
assertEquals(VAL2, cache.get(NODE1, KEY));
155          assertEquals(VAL2, cache.get(NODE2, KEY));
156          tx.commit();
157       }
158       catch(Throwable JavaDoc t) {
159          if(tx != null)
160             tx.rollback();
161       }
162       assertEquals(VAL2, cache.get(NODE1, KEY));
163       assertEquals(VAL2, cache.get(NODE2, KEY));
164    }
165
166
167    void sleep(long timeout) {
168       try {
169          Thread.sleep(timeout);
170       }
171       catch(InterruptedException JavaDoc e) {
172       }
173    }
174
175    void log(String JavaDoc msg) {
176       log.info("-- [" + Thread.currentThread() + "]: " + msg);
177    }
178
179
180    public static Test suite() throws Exception JavaDoc {
181       // return getDeploySetup(TxUnitTestCase.class, "cachetest.jar");
182
return new TestSuite(UpgradeLockTest.class);
183    }
184
185    public static void main(String JavaDoc[] args) throws Exception JavaDoc {
186       junit.textui.TestRunner.run(suite());
187    }
188
189
190 }
191
Popular Tags