KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > optimistic > OptimisticVersioningTest


1 /*
2  * JBoss, Home of Professional Open Source
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.cache.optimistic;
8
9 import junit.framework.Assert;
10 import org.jboss.cache.CacheImpl;
11 import org.jboss.cache.Fqn;
12 import org.jboss.cache.NodeSPI;
13 import org.jboss.cache.config.Configuration;
14
15 import javax.transaction.RollbackException JavaDoc;
16 import javax.transaction.Transaction JavaDoc;
17 import javax.transaction.TransactionManager JavaDoc;
18
19 /**
20  * Unit test that covers versioning of data and workspace nodes when using optimistic locking.
21  *
22  * @author <a HREF="mailto:manik@jboss.org">Manik Surtani (manik@jboss.org)</a>
23  */

24 public class OptimisticVersioningTest extends AbstractOptimisticTestCase
25 {
26    CacheImpl cache1, cache2;
27
28    public OptimisticVersioningTest(String JavaDoc name)
29    {
30       super(name);
31    }
32
33    protected void setUp() throws Exception JavaDoc
34    {
35       cache1 = createReplicatedCache(Configuration.CacheMode.REPL_SYNC);
36       cache2 = createReplicatedCache(Configuration.CacheMode.REPL_SYNC);
37    }
38
39    protected void tearDown()
40    {
41       super.tearDown();
42       destroyCache(cache1);
43       destroyCache(cache2);
44       cache1 = null;
45       cache2 = null;
46    }
47
48    public void testVersionPropagation() throws Exception JavaDoc
49    {
50       Fqn fqn = Fqn.fromString("/a/b");
51       String JavaDoc key = "key";
52
53       cache1.put(fqn, key, "value");
54
55       DataVersion v1 = ((NodeSPI) cache1.get(fqn)).getVersion();
56       DataVersion v2 = ((NodeSPI) cache2.get(fqn)).getVersion();
57
58       Assert.assertEquals("value", cache1.get(fqn, key));
59       Assert.assertEquals("value", cache2.get(fqn, key));
60       Assert.assertEquals("Version info should have propagated", v1, v2);
61
62       // change stuff in the node again...
63
cache1.put(fqn, key, "value2");
64
65       v1 = ((NodeSPI) cache1.get(fqn)).getVersion();
66       v2 = ((NodeSPI) cache2.get(fqn)).getVersion();
67
68       Assert.assertEquals("value2", cache1.get(fqn, key));
69       Assert.assertEquals("value2", cache2.get(fqn, key));
70       Assert.assertEquals("Version info should have propagated", v1, v2);
71    }
72
73    public void testTwoCachesUpdatingSimultaneously() throws Exception JavaDoc
74    {
75       TransactionManager JavaDoc mgr1 = cache1.getTransactionManager();
76       TransactionManager JavaDoc mgr2 = cache2.getTransactionManager();
77       Transaction JavaDoc tx1, tx2;
78
79       Fqn fqn = Fqn.fromString("/a/b");
80       String JavaDoc key = "key";
81
82       cache1.put(fqn, key, "value");
83
84       DataVersion v1 = ((NodeSPI) cache1.get(fqn)).getVersion();
85       DataVersion v2 = ((NodeSPI) cache2.get(fqn)).getVersion();
86
87       Assert.assertEquals("value", cache1.get(fqn, key));
88       Assert.assertEquals("value", cache2.get(fqn, key));
89       Assert.assertEquals("Version info should have propagated", v1, v2);
90
91       // Start a tx on cache 1
92
mgr1.begin();
93       cache1.put(fqn, key, "value2");
94       tx1 = mgr1.suspend();
95
96       // start a tx on cache 2
97
mgr2.begin();
98       cache2.put(fqn, key, "value3");
99       tx2 = mgr2.suspend();
100
101       // which tx completes, which fail?
102
mgr1.resume(tx1);
103       // should succeed...
104
mgr1.commit();
105
106       try
107       {
108          mgr2.resume(tx2);
109          mgr2.commit();
110          Assert.assertTrue("Should have failed", false);
111       }
112       catch (RollbackException JavaDoc rbe)
113       {
114          Assert.assertTrue("Should have failed", true);
115       }
116
117       // data versions should be in sync.
118
v1 = ((NodeSPI) cache1.get(fqn)).getVersion();
119       v2 = ((NodeSPI) cache2.get(fqn)).getVersion();
120
121       Assert.assertEquals("Version info should have propagated", v1, v2);
122    }
123 }
124
Popular Tags