KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > options > ExplicitVersionsReplTest


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.Fqn;
7 import org.jboss.cache.NodeSPI;
8 import org.jboss.cache.config.Configuration;
9 import org.jboss.cache.misc.TestingUtil;
10 import org.jboss.cache.optimistic.DataVersion;
11 import org.jboss.cache.optimistic.DefaultDataVersion;
12
13 import javax.transaction.RollbackException JavaDoc;
14 import javax.transaction.TransactionManager JavaDoc;
15
16 /**
17  * Tests the passing in of explicit {@see DataVersion} instances when using optimistic locking + replication.
18  *
19  * @author <a HREF="mailto:manik@jboss.org">Manik Surtani (manik@jboss.org)</a>
20  */

21 public class ExplicitVersionsReplTest extends TestCase
22 {
23    private CacheImpl cache[];
24    private Fqn fqn = Fqn.fromString("/a");
25    private String JavaDoc key = "key";
26
27    protected void setUp() throws Exception JavaDoc
28    {
29       if (cache != null) tearDown();
30       cache = new CacheImpl[2];
31       cache[0] = createCache();
32       cache[1] = createCache();
33       TestingUtil.blockUntilViewsReceived(cache, 20000);
34    }
35
36    private CacheImpl createCache() throws Exception JavaDoc
37    {
38       CacheImpl cache = new CacheImpl();
39       Configuration c = cache.getConfiguration();
40       c.setCacheMode(Configuration.CacheMode.REPL_SYNC);
41       c.setNodeLockingScheme("OPTIMISTIC");
42       // give us lots of time to trace and debug shit
43
c.setSyncCommitPhase(true);
44       c.setSyncRollbackPhase(true);
45       c.setSyncReplTimeout(1000);
46       c.setLockAcquisitionTimeout(1000);
47       c.setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup");
48
49       cache.setConfiguration(c);
50       cache.start();
51       return cache;
52    }
53
54    protected void tearDown()
55    {
56       if (cache != null)
57       {
58          for (CacheImpl aCache : cache) destroyCache(aCache);
59          cache = null;
60       }
61    }
62
63    private void destroyCache(CacheImpl c)
64    {
65       TransactionManager JavaDoc tm = c.getTransactionManager();
66       try
67       {
68          if (tm != null && tm.getTransaction() != null) tm.getTransaction().rollback();
69       }
70       catch (Exception JavaDoc e)
71       {
72       }
73       c.stop();
74    }
75
76    /**
77     * This test sets a custom data version first, expects it to replicate, and then does a put on the remote
78     * cache using an implicit data version. Should fail with a CCE.
79     *
80     * @throws Exception
81     */

82    public void testIncompatibleVersionTypes1() throws Exception JavaDoc
83    {
84       DataVersion version = new TestVersion("99");
85       cache[0].getInvocationContext().getOptionOverrides().setDataVersion(version);
86       cache[0].put(fqn, key, "value");// TestVersion-99 should be on both caches now
87

88       TransactionManager JavaDoc mgr = cache[0].getTransactionManager();
89       mgr.begin();
90       // don't explicitly set a data version.
91

92       System.out.println("************ stage 2");
93
94       // force an IC scrub
95
//cache[1].getInvocationContext().setOptionOverrides(null);
96
cache[1].put(fqn, key, "value2");
97       try
98       {
99          mgr.commit();
100
101          System.out.println(cache[0].printDetails());
102          System.out.println(cache[1].printDetails());
103
104          Assert.assertTrue("expected to fail", false);
105       }
106       catch (RollbackException JavaDoc e)
107       {
108          // should fail.
109
Assert.assertTrue("expected to fail with a nested ClassCastException", true);
110       }
111    }
112
113    /**
114     * This test sets a custom data version first, expects it to replicate, and then does a put on the remote
115     * cache using a higher custom data version. Should pass and not throw any exceptions.
116     *
117     * @throws Exception
118     */

119    public void testCompatibleVersionTypes1() throws Exception JavaDoc
120    {
121       DataVersion version = new TestVersion("99");
122       cache[0].getInvocationContext().getOptionOverrides().setDataVersion(version);
123       cache[0].put(fqn, key, "value");// TestVersion-99 should be on both caches now
124

125       TransactionManager JavaDoc mgr = cache[0].getTransactionManager();
126       mgr.begin();
127
128       version = new TestVersion("199");
129       cache[1].getInvocationContext().getOptionOverrides().setDataVersion(version);
130       cache[1].put(fqn, key, "value2");
131       mgr.commit();
132    }
133
134
135    /**
136     * This test sets a custom data version first, expects it to replicate, and then does a put on the remote
137     * cache using a lower custom data version. Should fail.
138     *
139     * @throws Exception
140     */

141    public void testCompatibleVersionTypesOutDatedVersion1() throws Exception JavaDoc
142    {
143       DataVersion version = new TestVersion("99");
144       cache[0].getInvocationContext().getOptionOverrides().setDataVersion(version);
145       cache[0].put(fqn, key, "value");// TestVersion-99 should be on both caches now
146

147       TransactionManager JavaDoc mgr = cache[0].getTransactionManager();
148       mgr.begin();
149
150       version = new TestVersion("29");
151       cache[1].getInvocationContext().getOptionOverrides().setDataVersion(version);
152       cache[1].put(fqn, key, "value2");
153       try
154       {
155          mgr.commit();
156          Assert.assertTrue("expected to fail", false);
157       }
158       catch (RollbackException JavaDoc e)
159       {
160          // should fail.
161
Assert.assertTrue("expected to fail with a CacheException to do with a versioning mismatch", true);
162       }
163    }
164
165
166    /**
167     * This test sets an implicit data version first, expects it to replicate, and then does a put on the remote
168     * cache using a custom data version. Should fail with a CCE.
169     *
170     * @throws Exception
171     */

172    public void testIncompatibleVersionTypes2() throws Exception JavaDoc
173    {
174       cache[0].put(fqn, key, "value");// default data version should be on both caches now
175

176       TransactionManager JavaDoc mgr = cache[0].getTransactionManager();
177       mgr.begin();
178
179       // explicitly set data version
180
DataVersion version = new TestVersion("99");
181       cache[1].getInvocationContext().getOptionOverrides().setDataVersion(version);
182
183       try
184       {
185          cache[1].put(fqn, key, "value2");
186          mgr.commit();
187          Assert.assertTrue("expected to fail", false);
188       }
189       catch (Exception JavaDoc e)
190       {
191          // should fail.
192
Assert.assertTrue("expected to fail", true);
193       }
194    }
195
196    /**
197     * This test sets an implicit data version first, expects it to replicate, and then does a put on the remote
198     * cache using a higher implicit data version. Should pass and not throw any exceptions.
199     *
200     * @throws Exception
201     */

202    public void testCompatibleVersionTypes2() throws Exception JavaDoc
203    {
204       cache[0].put(fqn, key, "value");// TestVersion-99 should be on both caches now
205

206       TransactionManager JavaDoc mgr = cache[0].getTransactionManager();
207       mgr.begin();
208
209       DataVersion version = new DefaultDataVersion(300);
210       cache[1].getInvocationContext().getOptionOverrides().setDataVersion(version);
211       cache[1].put(fqn, key, "value2");
212       mgr.commit();
213    }
214
215
216    /**
217     * This test sets an implicit data version first, expects it to replicate, and then does a put on the remote
218     * cache using a lower implicit data version. Should fail.
219     *
220     * @throws Exception
221     */

222    public void testCompatibleVersionTypesOutDatedVersion2() throws Exception JavaDoc
223    {
224       DataVersion version = new DefaultDataVersion(200);
225       cache[0].getInvocationContext().getOptionOverrides().setDataVersion(version);
226       cache[0].put(fqn, key, "value");// TestVersion-99 should be on both caches now
227

228       TransactionManager JavaDoc mgr = cache[0].getTransactionManager();
229       mgr.begin();
230
231       version = new DefaultDataVersion(100);
232       cache[1].getInvocationContext().getOptionOverrides().setDataVersion(version);
233       cache[1].put(fqn, key, "value2");
234       try
235       {
236          // this call will use implicit versioning and will hence fail.
237
mgr.commit();
238          Assert.assertTrue("expected to fail", false);
239       }
240       catch (Exception JavaDoc e)
241       {
242          // should fail.
243
Assert.assertTrue("expected to fail with a CacheException to do with a versioning mismatch", true);
244       }
245    }
246
247    public void testPropagationOfDefaultVersions() throws Exception JavaDoc
248    {
249       DefaultDataVersion expected = new DefaultDataVersion();
250       expected = (DefaultDataVersion) expected.increment();
251
252       cache[0].put(fqn, key, "value");
253
254       assertEquals("value", cache[0].get(fqn, key));
255       assertEquals("value", cache[1].get(fqn, key));
256       assertEquals(expected, ((NodeSPI) cache[0].get(fqn)).getVersion());
257       assertEquals(expected, ((NodeSPI) cache[1].get(fqn)).getVersion());
258
259       cache[1].put(fqn, key, "value2");
260       expected = (DefaultDataVersion) expected.increment();
261
262       assertEquals("value2", cache[0].get(fqn, key));
263       assertEquals("value2", cache[1].get(fqn, key));
264       assertEquals(expected, ((NodeSPI) cache[0].get(fqn)).getVersion());
265       assertEquals(expected, ((NodeSPI) cache[1].get(fqn)).getVersion());
266    }
267
268    public void testPropagationOfCustomVersions() throws Exception JavaDoc
269    {
270       TestVersion expected = new TestVersion("100");
271       cache[0].getInvocationContext().getOptionOverrides().setDataVersion(expected);
272       cache[0].put(fqn, key, "value");
273
274       assertEquals("value", cache[0].get(fqn, key));
275       assertEquals("value", cache[1].get(fqn, key));
276       assertEquals(expected, ((NodeSPI) cache[0].get(fqn)).getVersion());
277       assertEquals(expected, ((NodeSPI) cache[1].get(fqn)).getVersion());
278
279       expected = new TestVersion("200");
280       cache[1].getInvocationContext().getOptionOverrides().setDataVersion(expected);
281       cache[1].put(fqn, key, "value2");
282
283       assertEquals("value2", cache[0].get(fqn, key));
284       assertEquals("value2", cache[1].get(fqn, key));
285       assertEquals(expected, ((NodeSPI) cache[0].get(fqn)).getVersion());
286       assertEquals(expected, ((NodeSPI) cache[1].get(fqn)).getVersion());
287    }
288
289    public void testExplicitVersionOnRoot() throws Exception JavaDoc
290    {
291       TestVersion newVersion = new TestVersion("100");
292
293       cache[0].getInvocationContext().getOptionOverrides().setDataVersion(newVersion);
294       cache[0].getTransactionManager().begin();
295       cache[0].put(Fqn.ROOT, "k", "v");
296
297       // should not barf
298
cache[0].getTransactionManager().commit();
299    }
300
301    public void testExplicitVersionOnLeaf() throws Exception JavaDoc
302    {
303       cache[0].put("/org/domain/Entity", null);
304       assertEquals(1, ((DefaultDataVersion) ((NodeSPI) cache[0].get("/org/domain/Entity")).getVersion()).getRawVersion());
305       assertEquals(1, ((DefaultDataVersion) ((NodeSPI) cache[1].get("/org/domain/Entity")).getVersion()).getRawVersion());
306
307       TestVersion v = new TestVersion("Arse");
308       cache[0].getInvocationContext().getOptionOverrides().setDataVersion(v);
309
310       cache[0].put(Fqn.fromString("/org/domain/Entity/EntityInstance#1"), "k", "v");
311
312       assertEquals(2, ((DefaultDataVersion) ((NodeSPI) cache[0].get("/org/domain/Entity")).getVersion()).getRawVersion());
313       assertEquals(v, ((NodeSPI) cache[0].get("/org/domain/Entity/EntityInstance#1")).getVersion());
314       assertEquals(2, ((DefaultDataVersion) ((NodeSPI) cache[1].get("/org/domain/Entity")).getVersion()).getRawVersion());
315       assertEquals(v, ((NodeSPI) cache[1].get("/org/domain/Entity/EntityInstance#1")).getVersion());
316
317    }
318
319
320 }
321
Popular Tags