KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.jboss.cache.optimistic;
2
3 import org.jboss.cache.CacheImpl;
4 import org.jboss.cache.Fqn;
5
6 import javax.transaction.Transaction JavaDoc;
7 import javax.transaction.TransactionManager JavaDoc;
8
9 /**
10  * @author <a HREF="mailto:manik@jboss.org">Manik Surtani</a>
11  */

12 public class VersioningOnReadTest extends AbstractOptimisticTestCase
13 {
14    CacheImpl cache;
15    Fqn fqn = Fqn.fromString("/a");
16    TransactionManager JavaDoc tm;
17
18    public VersioningOnReadTest(String JavaDoc name)
19    {
20       super(name);
21    }
22
23    protected void setUp() throws Exception JavaDoc
24    {
25       super.setUp();
26       cache = createCache();
27       tm = cache.getTransactionManager();
28    }
29
30    protected void tearDown()
31    {
32       super.tearDown();
33       destroyCache(cache);
34    }
35
36    public void testUpdateOnWrite() throws Exception JavaDoc
37    {
38       cache.put(fqn, "k", "v");
39
40       assertEquals("v", cache.get(fqn, "k"));
41
42       // now start a tx to mod the node
43
tm.begin();
44       cache.put(fqn, "k", "v2");
45
46       // suspend the tx
47
Transaction JavaDoc tx = tm.suspend();
48
49       // now modify the node
50
cache.put(fqn, "k", "v3");
51
52       // resume the tx
53
tm.resume(tx);
54
55       try
56       {
57          tm.commit();
58          fail("Should have failed with a data version mismatch");
59       }
60       catch (Exception JavaDoc e)
61       {
62          // do nothing
63
}
64    }
65
66    public void testUpdateOnRemove() throws Exception JavaDoc
67    {
68       cache.put(fqn, "k", "v");
69
70       assertEquals("v", cache.get(fqn, "k"));
71
72       // now start a tx to mod the node
73
tm.begin();
74       cache.remove(fqn, "k");
75
76       // suspend the tx
77
Transaction JavaDoc tx = tm.suspend();
78
79       // now modify the node
80
cache.put(fqn, "k", "v3");
81
82       // resume the tx
83
tm.resume(tx);
84
85       try
86       {
87          tm.commit();
88          fail("Should have failed with a data version mismatch");
89       }
90       catch (Exception JavaDoc e)
91       {
92          // do nothing
93
}
94    }
95
96    public void testUpdateOnRemoveNode() throws Exception JavaDoc
97    {
98       cache.put(fqn, "k", "v");
99
100       assertEquals("v", cache.get(fqn, "k"));
101
102       // now start a tx to mod the node
103
tm.begin();
104       cache.remove(fqn);
105
106       // suspend the tx
107
Transaction JavaDoc tx = tm.suspend();
108
109       // now modify the node
110
cache.put(fqn, "k", "v3");
111
112       // resume the tx
113
tm.resume(tx);
114
115       try
116       {
117          tm.commit();
118          fail("Should have failed with a data version mismatch");
119       }
120       catch (Exception JavaDoc e)
121       {
122          // do nothing
123
}
124    }
125
126
127    public void testUpdateOnRead() throws Exception JavaDoc
128    {
129       cache.put(fqn, "k", "v");
130
131       assertEquals("v", cache.get(fqn, "k"));
132
133       // now start a tx to mod the node
134
tm.begin();
135       cache.get(fqn, "k");
136
137       // suspend the tx
138
Transaction JavaDoc tx = tm.suspend();
139
140       // now modify the node
141
cache.put(fqn, "k", "v3");
142
143       // resume the tx
144
tm.resume(tx);
145
146       // now put some other stuff elsewhere
147
cache.put(Fqn.fromString("/b"), "k", "v");
148
149       // this should succeed since there is no contention on writing to /a
150
tm.commit();
151    }
152
153 }
154
Popular Tags