KickJava   Java API By Example, From Geeks To Geeks.

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


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.lock.IsolationLevel;
16 import org.jboss.cache.transaction.DummyTransactionManager;
17 import org.jboss.logging.Logger;
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: UpgradeLockUnitTestCase.java,v 1.3 2004/08/17 14:25:35 belaban Exp $
29  */

30 public class UpgradeLockUnitTestCase extends TestCase {
31    TreeCache cache=null;
32    UserTransaction JavaDoc tx=null;
33    Logger 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 UpgradeLockUnitTestCase(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       if(cache != null)
62          cache.stopService();
63
64       // BW. kind of a hack to destroy jndi binding and thread local tx before next run.
65
DummyTransactionManager.destroy();
66       if(old_factory != null) {
67          System.setProperty(Context.INITIAL_CONTEXT_FACTORY, old_factory);
68          old_factory=null;
69       }
70
71       if(tx != null) {
72          try {
73             tx.rollback();
74          }
75          catch(Throwable JavaDoc t) {
76          }
77          tx=null;
78       }
79    }
80
81    TreeCache createCache(IsolationLevel level) throws Exception JavaDoc {
82       TreeCache cache=new TreeCache("test", null, 10000);
83       cache.setTransactionManagerLookupClass("org.jboss.cache.JBossTransactionManagerLookup");
84       cache.setLockAcquisitionTimeout(500);
85       cache.setIsolationLevel(level);
86       cache.createService();
87       cache.startService();
88       return cache;
89    }
90
91
92    public void testUpgradeWithNone() throws Exception JavaDoc {
93       runTestWithIsolationLevel(IsolationLevel.NONE);
94    }
95
96
97    public void testUpgradeWithReadUncommitted() throws Exception JavaDoc {
98       runTestWithIsolationLevel(IsolationLevel.READ_UNCOMMITTED);
99    }
100
101    public void testUpgradeWithReadCommitted() throws Exception JavaDoc {
102       runTestWithIsolationLevel(IsolationLevel.READ_COMMITTED);
103    }
104
105    public void testUpgradeWithRepeatableRead() throws Exception JavaDoc {
106       runTestWithIsolationLevel(IsolationLevel.REPEATABLE_READ);
107    }
108
109    public void testUpgradeWithSerializable() throws Exception JavaDoc {
110       runTestWithIsolationLevel(IsolationLevel.SERIALIZABLE);
111    }
112
113
114    void runTestWithIsolationLevel(IsolationLevel level) throws Exception JavaDoc {
115       cache=createCache(level);
116       // add initial values outside of TX
117
cache.put(NODE1, KEY, VAL1);
118       cache.put(NODE2, KEY, VAL1);
119
120       tx.begin();
121       try {
122          assertEquals(VAL1, cache.get(NODE1, KEY));
123          assertEquals(VAL1, cache.get(NODE2, KEY));
124
125          cache.put(NODE1, KEY, VAL2); // causes read lock to upgrade to r/w lock
126
cache.put(NODE2, KEY, VAL2); // causes read lock to upgrade to r/w lock
127
assertEquals(VAL2, cache.get(NODE1, KEY));
128          assertEquals(VAL2, cache.get(NODE2, KEY));
129          tx.commit();
130       }
131       catch(Throwable JavaDoc t) {
132          if(tx != null)
133             tx.rollback();
134       }
135       assertEquals(VAL2, cache.get(NODE1, KEY));
136       assertEquals(VAL2, cache.get(NODE2, KEY));
137    }
138
139
140    void sleep(long timeout) {
141       try {
142          Thread.sleep(timeout);
143       }
144       catch(InterruptedException JavaDoc e) {
145       }
146    }
147
148    void log(String JavaDoc msg) {
149       log.info("-- [" + Thread.currentThread() + "]: " + msg);
150    }
151
152
153    public static Test suite() throws Exception JavaDoc {
154       // return getDeploySetup(TxUnitTestCase.class, "cachetest.jar");
155
return new TestSuite(UpgradeLockUnitTestCase.class);
156    }
157
158    public static void main(String JavaDoc[] args) throws Exception JavaDoc {
159       junit.textui.TestRunner.run(suite());
160    }
161
162
163 }
164
Popular Tags