KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > 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.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.transaction.DummyTransactionManager;
17
18 import javax.naming.Context JavaDoc;
19 import javax.naming.InitialContext JavaDoc;
20 import javax.transaction.UserTransaction JavaDoc;
21 import java.util.Properties JavaDoc;
22
23 /**
24  * Tests upgrade locks from read -> write
25  *
26  * @author Bela Ban
27  * @version $Id: UpgradeLockTest.java,v 1.6 2006/12/30 17:49:58 msurtani Exp $
28  */

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