1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
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.DefaultCacheFactory; |
17 |
| import org.jboss.cache.transaction.DummyTransactionManager; |
18 |
| |
19 |
| import javax.naming.Context; |
20 |
| import javax.naming.InitialContext; |
21 |
| import javax.transaction.UserTransaction; |
22 |
| import java.util.Properties; |
23 |
| |
24 |
| |
25 |
| |
26 |
| |
27 |
| |
28 |
| |
29 |
| |
30 |
| public class UpgradeLockTest extends TestCase |
31 |
| { |
32 |
| CacheImpl cache = null; |
33 |
| UserTransaction tx = null; |
34 |
| Log log; |
35 |
| Properties p = null; |
36 |
| String old_factory = null; |
37 |
| final String FACTORY = "org.jboss.cache.transaction.DummyContextFactory"; |
38 |
| final String NODE1 = "/test"; |
39 |
| final String NODE2 = "/my/test"; |
40 |
| final String KEY = "key"; |
41 |
| final String VAL1 = "val1"; |
42 |
| final String VAL2 = "val2"; |
43 |
| |
44 |
| |
45 |
7
| public UpgradeLockTest(String name)
|
46 |
| { |
47 |
7
| super(name);
|
48 |
| } |
49 |
| |
50 |
7
| public void setUp() throws Exception
|
51 |
| { |
52 |
7
| super.setUp();
|
53 |
7
| old_factory = System.getProperty(Context.INITIAL_CONTEXT_FACTORY);
|
54 |
7
| System.setProperty(Context.INITIAL_CONTEXT_FACTORY, FACTORY);
|
55 |
7
| DummyTransactionManager.getInstance();
|
56 |
7
| if (p == null)
|
57 |
| { |
58 |
7
| p = new Properties();
|
59 |
7
| p.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.cache.transaction.DummyContextFactory");
|
60 |
| } |
61 |
7
| tx = (UserTransaction) new InitialContext(p).lookup("UserTransaction");
|
62 |
| } |
63 |
| |
64 |
7
| public void tearDown() throws Exception
|
65 |
| { |
66 |
7
| super.tearDown();
|
67 |
7
| if (cache != null)
|
68 |
| { |
69 |
7
| cache.stop();
|
70 |
| } |
71 |
| |
72 |
| |
73 |
7
| DummyTransactionManager.destroy();
|
74 |
7
| if (old_factory != null)
|
75 |
| { |
76 |
6
| System.setProperty(Context.INITIAL_CONTEXT_FACTORY, old_factory);
|
77 |
6
| old_factory = null;
|
78 |
| } |
79 |
| |
80 |
7
| if (tx != null)
|
81 |
| { |
82 |
7
| try
|
83 |
| { |
84 |
7
| tx.rollback();
|
85 |
| } |
86 |
| catch (Throwable t) |
87 |
| { |
88 |
| } |
89 |
7
| tx = null;
|
90 |
| } |
91 |
| } |
92 |
| |
93 |
7
| CacheImpl createCache(IsolationLevel level) throws Exception
|
94 |
| { |
95 |
7
| CacheImpl c = (CacheImpl) DefaultCacheFactory.getInstance().createCache(false);
|
96 |
7
| c.getConfiguration().setClusterName("test");
|
97 |
7
| c.getConfiguration().setInitialStateRetrievalTimeout(10000);
|
98 |
7
| c.getConfiguration().setTransactionManagerLookupClass("org.jboss.cache.JBossTransactionManagerLookup");
|
99 |
7
| c.getConfiguration().setLockAcquisitionTimeout(500);
|
100 |
7
| c.getConfiguration().setIsolationLevel(level);
|
101 |
7
| c.create();
|
102 |
7
| c.start();
|
103 |
7
| return c;
|
104 |
| } |
105 |
| |
106 |
| |
107 |
1
| public void testUpgradeWithNone() throws Exception
|
108 |
| { |
109 |
1
| runTestWithIsolationLevel(IsolationLevel.NONE);
|
110 |
| } |
111 |
| |
112 |
| |
113 |
1
| public void testUpgradeWithReadUncommitted() throws Exception
|
114 |
| { |
115 |
1
| runTestWithIsolationLevel(IsolationLevel.READ_UNCOMMITTED);
|
116 |
| } |
117 |
| |
118 |
1
| public void testUpgradeWithReadCommitted() throws Exception
|
119 |
| { |
120 |
1
| runTestWithIsolationLevel(IsolationLevel.READ_COMMITTED);
|
121 |
| } |
122 |
| |
123 |
1
| public void testUpgradeWithRepeatableRead() throws Exception
|
124 |
| { |
125 |
1
| runTestWithIsolationLevel(IsolationLevel.REPEATABLE_READ);
|
126 |
| } |
127 |
| |
128 |
1
| public void testUpgradeWithSerializable() throws Exception
|
129 |
| { |
130 |
1
| runTestWithIsolationLevel(IsolationLevel.SERIALIZABLE);
|
131 |
| } |
132 |
| |
133 |
1
| public void testIsolationLevelSerializable() throws Exception
|
134 |
| { |
135 |
1
| _testIsolationLevel(IsolationLevel.SERIALIZABLE);
|
136 |
| } |
137 |
| |
138 |
1
| public void testIsolationLevelNone() throws Exception
|
139 |
| { |
140 |
1
| _testIsolationLevel(IsolationLevel.NONE);
|
141 |
| } |
142 |
| |
143 |
| |
144 |
2
| void _testIsolationLevel(IsolationLevel l) throws Exception
|
145 |
| { |
146 |
2
| cache = createCache(l);
|
147 |
2
| tx.begin();
|
148 |
| |
149 |
2
| int expected_num_locks = l == IsolationLevel.NONE ? 0 : 2;
|
150 |
| |
151 |
2
| cache.put(NODE1, null);
|
152 |
2
| assertEquals(expected_num_locks, cache.getNumberOfLocksHeld());
|
153 |
| |
154 |
2
| cache.put(NODE1, null);
|
155 |
2
| assertEquals(expected_num_locks, cache.getNumberOfLocksHeld());
|
156 |
| |
157 |
2
| tx.rollback();
|
158 |
2
| assertEquals(0, cache.getNumberOfLocksHeld());
|
159 |
| } |
160 |
| |
161 |
| |
162 |
5
| void runTestWithIsolationLevel(IsolationLevel level) throws Exception
|
163 |
| { |
164 |
5
| cache = createCache(level);
|
165 |
| |
166 |
5
| cache.put(NODE1, KEY, VAL1);
|
167 |
5
| cache.put(NODE2, KEY, VAL1);
|
168 |
| |
169 |
5
| tx.begin();
|
170 |
5
| try
|
171 |
| { |
172 |
5
| assertEquals(VAL1, cache.get(NODE1, KEY));
|
173 |
5
| assertEquals(VAL1, cache.get(NODE2, KEY));
|
174 |
| |
175 |
5
| cache.put(NODE1, KEY, VAL2);
|
176 |
5
| cache.put(NODE2, KEY, VAL2);
|
177 |
5
| assertEquals(VAL2, cache.get(NODE1, KEY));
|
178 |
5
| assertEquals(VAL2, cache.get(NODE2, KEY));
|
179 |
5
| tx.commit();
|
180 |
| } |
181 |
| catch (Throwable t) |
182 |
| { |
183 |
0
| if (tx != null)
|
184 |
| { |
185 |
0
| tx.rollback();
|
186 |
| } |
187 |
| } |
188 |
5
| assertEquals(VAL2, cache.get(NODE1, KEY));
|
189 |
5
| assertEquals(VAL2, cache.get(NODE2, KEY));
|
190 |
| } |
191 |
| |
192 |
| |
193 |
0
| void log(String msg)
|
194 |
| { |
195 |
0
| log.info("-- [" + Thread.currentThread() + "]: " + msg);
|
196 |
| } |
197 |
| |
198 |
| |
199 |
1
| public static Test suite() throws Exception
|
200 |
| { |
201 |
| |
202 |
1
| return new TestSuite(UpgradeLockTest.class);
|
203 |
| } |
204 |
| |
205 |
0
| public static void main(String[] args) throws Exception
|
206 |
| { |
207 |
0
| junit.textui.TestRunner.run(suite());
|
208 |
| } |
209 |
| |
210 |
| |
211 |
| } |