1 |
| package org.jboss.cache.options; |
2 |
| |
3 |
| import junit.framework.Assert; |
4 |
| import junit.framework.TestCase; |
5 |
| import org.jboss.cache.CacheImpl; |
6 |
| import org.jboss.cache.DefaultCacheFactory; |
7 |
| import org.jboss.cache.Fqn; |
8 |
| import org.jboss.cache.NodeSPI; |
9 |
| import org.jboss.cache.config.Configuration; |
10 |
| import org.jboss.cache.misc.TestingUtil; |
11 |
| import org.jboss.cache.optimistic.DataVersion; |
12 |
| import org.jboss.cache.optimistic.DefaultDataVersion; |
13 |
| |
14 |
| import javax.transaction.RollbackException; |
15 |
| import javax.transaction.TransactionManager; |
16 |
| |
17 |
| |
18 |
| |
19 |
| |
20 |
| |
21 |
| |
22 |
| public class ExplicitVersionsReplTest extends TestCase |
23 |
| { |
24 |
| private CacheImpl cache[]; |
25 |
| private Fqn fqn = Fqn.fromString("/a"); |
26 |
| private String key = "key"; |
27 |
| |
28 |
12
| protected void setUp() throws Exception
|
29 |
| { |
30 |
0
| if (cache != null) tearDown();
|
31 |
12
| cache = new CacheImpl[2];
|
32 |
12
| cache[0] = createCache();
|
33 |
12
| cache[1] = createCache();
|
34 |
12
| TestingUtil.blockUntilViewsReceived(cache, 20000);
|
35 |
| } |
36 |
| |
37 |
24
| private CacheImpl createCache() throws Exception
|
38 |
| { |
39 |
24
| CacheImpl cache = (CacheImpl) DefaultCacheFactory.getInstance().createCache(false);
|
40 |
24
| Configuration c = cache.getConfiguration();
|
41 |
24
| c.setCacheMode(Configuration.CacheMode.REPL_SYNC);
|
42 |
24
| c.setNodeLockingScheme("OPTIMISTIC");
|
43 |
| |
44 |
24
| c.setSyncCommitPhase(true);
|
45 |
24
| c.setSyncRollbackPhase(true);
|
46 |
24
| c.setSyncReplTimeout(1000);
|
47 |
24
| c.setLockAcquisitionTimeout(1000);
|
48 |
24
| c.setTransactionManagerLookupClass("org.jboss.cache.transaction.DummyTransactionManagerLookup");
|
49 |
| |
50 |
24
| cache.setConfiguration(c);
|
51 |
24
| cache.start();
|
52 |
24
| return cache;
|
53 |
| } |
54 |
| |
55 |
12
| protected void tearDown()
|
56 |
| { |
57 |
12
| if (cache != null)
|
58 |
| { |
59 |
24
| for (CacheImpl aCache : cache) destroyCache(aCache);
|
60 |
12
| cache = null;
|
61 |
| } |
62 |
| } |
63 |
| |
64 |
24
| private void destroyCache(CacheImpl c)
|
65 |
| { |
66 |
24
| TransactionManager tm = c.getTransactionManager();
|
67 |
24
| try
|
68 |
| { |
69 |
0
| if (tm != null && tm.getTransaction() != null) tm.getTransaction().rollback();
|
70 |
| } |
71 |
| catch (Exception e) |
72 |
| { |
73 |
| } |
74 |
24
| c.stop();
|
75 |
| } |
76 |
| |
77 |
| |
78 |
| |
79 |
| |
80 |
| |
81 |
| |
82 |
| |
83 |
1
| public void testIncompatibleVersionTypes1() throws Exception
|
84 |
| { |
85 |
1
| DataVersion version = new TestVersion("99");
|
86 |
1
| cache[0].getInvocationContext().getOptionOverrides().setDataVersion(version);
|
87 |
1
| cache[0].put(fqn, key, "value");
|
88 |
| |
89 |
1
| TransactionManager mgr = cache[0].getTransactionManager();
|
90 |
1
| mgr.begin();
|
91 |
| |
92 |
| |
93 |
1
| System.out.println("************ stage 2");
|
94 |
| |
95 |
| |
96 |
| |
97 |
1
| cache[1].put(fqn, key, "value2");
|
98 |
1
| try
|
99 |
| { |
100 |
1
| mgr.commit();
|
101 |
| |
102 |
0
| System.out.println(cache[0].printDetails());
|
103 |
0
| System.out.println(cache[1].printDetails());
|
104 |
| |
105 |
0
| Assert.assertTrue("expected to fail", false);
|
106 |
| } |
107 |
| catch (RollbackException e) |
108 |
| { |
109 |
| |
110 |
1
| Assert.assertTrue("expected to fail with a nested ClassCastException", true);
|
111 |
| } |
112 |
| } |
113 |
| |
114 |
| |
115 |
| |
116 |
| |
117 |
| |
118 |
| |
119 |
| |
120 |
1
| public void testCompatibleVersionTypes1() throws Exception
|
121 |
| { |
122 |
1
| DataVersion version = new TestVersion("99");
|
123 |
1
| cache[0].getInvocationContext().getOptionOverrides().setDataVersion(version);
|
124 |
1
| cache[0].put(fqn, key, "value");
|
125 |
| |
126 |
1
| TransactionManager mgr = cache[0].getTransactionManager();
|
127 |
1
| mgr.begin();
|
128 |
| |
129 |
1
| version = new TestVersion("999");
|
130 |
1
| cache[1].getInvocationContext().getOptionOverrides().setDataVersion(version);
|
131 |
1
| cache[1].put(fqn, key, "value2");
|
132 |
1
| mgr.commit();
|
133 |
| } |
134 |
| |
135 |
| |
136 |
| |
137 |
| |
138 |
| |
139 |
| |
140 |
| |
141 |
| |
142 |
1
| public void testCompatibleVersionTypesOutDatedVersion1() throws Exception
|
143 |
| { |
144 |
1
| DataVersion version = new TestVersion("99");
|
145 |
1
| cache[0].getInvocationContext().getOptionOverrides().setDataVersion(version);
|
146 |
1
| cache[0].put(fqn, key, "value");
|
147 |
| |
148 |
1
| TransactionManager mgr = cache[0].getTransactionManager();
|
149 |
1
| mgr.begin();
|
150 |
| |
151 |
1
| version = new TestVersion("29");
|
152 |
1
| cache[1].getInvocationContext().getOptionOverrides().setDataVersion(version);
|
153 |
1
| cache[1].put(fqn, key, "value2");
|
154 |
1
| try
|
155 |
| { |
156 |
1
| mgr.commit();
|
157 |
0
| Assert.assertTrue("expected to fail", false);
|
158 |
| } |
159 |
| catch (RollbackException e) |
160 |
| { |
161 |
| |
162 |
1
| Assert.assertTrue("expected to fail with a CacheException to do with a versioning mismatch", true);
|
163 |
| } |
164 |
| } |
165 |
| |
166 |
| |
167 |
| |
168 |
| |
169 |
| |
170 |
| |
171 |
| |
172 |
| |
173 |
1
| public void testIncompatibleVersionTypes2() throws Exception
|
174 |
| { |
175 |
1
| cache[0].put(fqn, key, "value");
|
176 |
| |
177 |
1
| TransactionManager mgr = cache[0].getTransactionManager();
|
178 |
1
| mgr.begin();
|
179 |
| |
180 |
| |
181 |
1
| DataVersion version = new TestVersion("99");
|
182 |
1
| cache[1].getInvocationContext().getOptionOverrides().setDataVersion(version);
|
183 |
| |
184 |
1
| try
|
185 |
| { |
186 |
1
| cache[1].put(fqn, key, "value2");
|
187 |
1
| mgr.commit();
|
188 |
0
| Assert.assertTrue("expected to fail", false);
|
189 |
| } |
190 |
| catch (Exception e) |
191 |
| { |
192 |
| |
193 |
1
| Assert.assertTrue("expected to fail", true);
|
194 |
| } |
195 |
| } |
196 |
| |
197 |
| |
198 |
| |
199 |
| |
200 |
| |
201 |
| |
202 |
| |
203 |
1
| public void testCompatibleVersionTypes2() throws Exception
|
204 |
| { |
205 |
1
| cache[0].put(fqn, key, "value");
|
206 |
| |
207 |
1
| TransactionManager mgr = cache[0].getTransactionManager();
|
208 |
1
| mgr.begin();
|
209 |
| |
210 |
1
| DataVersion version = new DefaultDataVersion(300);
|
211 |
1
| cache[1].getInvocationContext().getOptionOverrides().setDataVersion(version);
|
212 |
1
| cache[1].put(fqn, key, "value2");
|
213 |
1
| mgr.commit();
|
214 |
| } |
215 |
| |
216 |
| |
217 |
| |
218 |
| |
219 |
| |
220 |
| |
221 |
| |
222 |
| |
223 |
1
| public void testCompatibleVersionTypesOutDatedVersion2() throws Exception
|
224 |
| { |
225 |
1
| DataVersion version = new DefaultDataVersion(200);
|
226 |
1
| cache[0].getInvocationContext().getOptionOverrides().setDataVersion(version);
|
227 |
1
| cache[0].put(fqn, key, "value");
|
228 |
| |
229 |
1
| TransactionManager mgr = cache[0].getTransactionManager();
|
230 |
1
| mgr.begin();
|
231 |
| |
232 |
1
| version = new DefaultDataVersion(100);
|
233 |
1
| cache[1].getInvocationContext().getOptionOverrides().setDataVersion(version);
|
234 |
1
| cache[1].put(fqn, key, "value2");
|
235 |
1
| try
|
236 |
| { |
237 |
| |
238 |
1
| mgr.commit();
|
239 |
0
| Assert.assertTrue("expected to fail", false);
|
240 |
| } |
241 |
| catch (Exception e) |
242 |
| { |
243 |
| |
244 |
1
| Assert.assertTrue("expected to fail with a CacheException to do with a versioning mismatch", true);
|
245 |
| } |
246 |
| } |
247 |
| |
248 |
1
| public void testPropagationOfDefaultVersions() throws Exception
|
249 |
| { |
250 |
1
| DefaultDataVersion expected = new DefaultDataVersion();
|
251 |
1
| expected = (DefaultDataVersion) expected.increment();
|
252 |
| |
253 |
1
| cache[0].put(fqn, key, "value");
|
254 |
| |
255 |
1
| assertEquals("value", cache[0].get(fqn, key));
|
256 |
1
| assertEquals("value", cache[1].get(fqn, key));
|
257 |
1
| assertEquals(expected, ((NodeSPI) cache[0].get(fqn)).getVersion());
|
258 |
1
| assertEquals(expected, ((NodeSPI) cache[1].get(fqn)).getVersion());
|
259 |
| |
260 |
1
| cache[1].put(fqn, key, "value2");
|
261 |
1
| expected = (DefaultDataVersion) expected.increment();
|
262 |
| |
263 |
1
| assertEquals("value2", cache[0].get(fqn, key));
|
264 |
1
| assertEquals("value2", cache[1].get(fqn, key));
|
265 |
1
| assertEquals(expected, ((NodeSPI) cache[0].get(fqn)).getVersion());
|
266 |
1
| assertEquals(expected, ((NodeSPI) cache[1].get(fqn)).getVersion());
|
267 |
| } |
268 |
| |
269 |
1
| public void testPropagationOfCustomVersions() throws Exception
|
270 |
| { |
271 |
1
| TestVersion expected = new TestVersion("100");
|
272 |
1
| cache[0].getInvocationContext().getOptionOverrides().setDataVersion(expected);
|
273 |
1
| cache[0].put(fqn, key, "value");
|
274 |
| |
275 |
1
| assertEquals("value", cache[0].get(fqn, key));
|
276 |
1
| assertEquals("value", cache[1].get(fqn, key));
|
277 |
1
| assertEquals(expected, ((NodeSPI) cache[0].get(fqn)).getVersion());
|
278 |
1
| assertEquals(expected, ((NodeSPI) cache[1].get(fqn)).getVersion());
|
279 |
| |
280 |
1
| expected = new TestVersion("200");
|
281 |
1
| cache[1].getInvocationContext().getOptionOverrides().setDataVersion(expected);
|
282 |
1
| cache[1].put(fqn, key, "value2");
|
283 |
| |
284 |
1
| assertEquals("value2", cache[0].get(fqn, key));
|
285 |
1
| assertEquals("value2", cache[1].get(fqn, key));
|
286 |
1
| assertEquals(expected, ((NodeSPI) cache[0].get(fqn)).getVersion());
|
287 |
1
| assertEquals(expected, ((NodeSPI) cache[1].get(fqn)).getVersion());
|
288 |
| } |
289 |
| |
290 |
1
| public void testExplicitVersionOnRoot() throws Exception
|
291 |
| { |
292 |
1
| TestVersion newVersion = new TestVersion("100");
|
293 |
| |
294 |
1
| cache[0].getInvocationContext().getOptionOverrides().setDataVersion(newVersion);
|
295 |
1
| cache[0].getTransactionManager().begin();
|
296 |
1
| cache[0].put(Fqn.ROOT, "k", "v");
|
297 |
| |
298 |
1
| try
|
299 |
| { |
300 |
1
| cache[0].getTransactionManager().commit();
|
301 |
0
| fail("Should have barfed");
|
302 |
| } |
303 |
| catch (RollbackException rbe) |
304 |
| { |
305 |
| |
306 |
| } |
307 |
| } |
308 |
| |
309 |
1
| public void testExplicitVersionOnLeaf() throws Exception
|
310 |
| { |
311 |
1
| cache[0].put("/org/domain/Entity", null);
|
312 |
1
| assertEquals(1, ((DefaultDataVersion) ((NodeSPI) cache[0].get("/org/domain/Entity")).getVersion()).getRawVersion());
|
313 |
1
| assertEquals(1, ((DefaultDataVersion) ((NodeSPI) cache[1].get("/org/domain/Entity")).getVersion()).getRawVersion());
|
314 |
| |
315 |
1
| TestVersion v = new TestVersion("Arse");
|
316 |
1
| cache[0].getInvocationContext().getOptionOverrides().setDataVersion(v);
|
317 |
| |
318 |
1
| cache[0].put(Fqn.fromString("/org/domain/Entity/EntityInstance#1"), "k", "v");
|
319 |
| |
320 |
1
| assertEquals(1, ((DefaultDataVersion) ((NodeSPI) cache[0].get("/org/domain/Entity")).getVersion()).getRawVersion());
|
321 |
1
| assertEquals(v, ((NodeSPI) cache[0].get("/org/domain/Entity/EntityInstance#1")).getVersion());
|
322 |
1
| assertEquals(1, ((DefaultDataVersion) ((NodeSPI) cache[1].get("/org/domain/Entity")).getVersion()).getRawVersion());
|
323 |
1
| assertEquals(v, ((NodeSPI) cache[1].get("/org/domain/Entity/EntityInstance#1")).getVersion());
|
324 |
| |
325 |
| } |
326 |
| |
327 |
1
| public void testExplicitVersionOnLeafImplicitParentCreation() throws Exception
|
328 |
| { |
329 |
1
| TestVersion v = new TestVersion("Arse");
|
330 |
1
| cache[0].getInvocationContext().getOptionOverrides().setDataVersion(v);
|
331 |
| |
332 |
1
| cache[0].put(Fqn.fromString("/org/domain/Entity/EntityInstance#1"), "k", "v");
|
333 |
| |
334 |
1
| assertEquals(0, ((DefaultDataVersion) ((NodeSPI) cache[0].get("/org/domain/Entity")).getVersion()).getRawVersion());
|
335 |
1
| assertEquals(v, ((NodeSPI) cache[0].get("/org/domain/Entity/EntityInstance#1")).getVersion());
|
336 |
1
| assertEquals(0, ((DefaultDataVersion) ((NodeSPI) cache[1].get("/org/domain/Entity")).getVersion()).getRawVersion());
|
337 |
1
| assertEquals(v, ((NodeSPI) cache[1].get("/org/domain/Entity/EntityInstance#1")).getVersion());
|
338 |
| |
339 |
| } |
340 |
| |
341 |
1
| public void testExplicitVersionOnParentAndChild() throws Exception
|
342 |
| { |
343 |
1
| TestVersion vParent = new TestVersion("Parent-Version");
|
344 |
| |
345 |
1
| cache[0].getTransactionManager().begin();
|
346 |
1
| cache[0].getInvocationContext().getOptionOverrides().setDataVersion(vParent);
|
347 |
1
| cache[0].put(Fqn.fromString("/parent"), "k", "v");
|
348 |
1
| cache[0].getTransactionManager().commit();
|
349 |
| |
350 |
1
| assertEquals(0, ((DefaultDataVersion) ((NodeSPI) cache[0].get("/")).getVersion()).getRawVersion());
|
351 |
1
| assertEquals(vParent, ((NodeSPI) cache[0].get("/parent")).getVersion());
|
352 |
1
| assertEquals(0, ((DefaultDataVersion) ((NodeSPI) cache[1].get("/")).getVersion()).getRawVersion());
|
353 |
1
| assertEquals(vParent, ((NodeSPI) cache[1].get("/parent")).getVersion());
|
354 |
| |
355 |
| |
356 |
1
| TestVersion vChild = new TestVersion("Child-Version");
|
357 |
| |
358 |
1
| cache[0].getTransactionManager().begin();
|
359 |
1
| cache[0].getInvocationContext().getOptionOverrides().setDataVersion(vChild);
|
360 |
1
| cache[0].put(Fqn.fromString("/parent/child"), "k", "v");
|
361 |
1
| cache[0].getTransactionManager().commit();
|
362 |
| |
363 |
1
| assertEquals(0, ((DefaultDataVersion) ((NodeSPI) cache[0].get("/")).getVersion()).getRawVersion());
|
364 |
1
| assertEquals(vParent, ((NodeSPI) cache[0].get("/parent")).getVersion());
|
365 |
1
| assertEquals(vChild, ((NodeSPI) cache[0].get("/parent/child")).getVersion());
|
366 |
1
| assertEquals(0, ((DefaultDataVersion) ((NodeSPI) cache[1].get("/")).getVersion()).getRawVersion());
|
367 |
1
| assertEquals(vParent, ((NodeSPI) cache[1].get("/parent")).getVersion());
|
368 |
1
| assertEquals(vChild, ((NodeSPI) cache[1].get("/parent/child")).getVersion());
|
369 |
| |
370 |
| } |
371 |
| } |