KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.jboss.cache.optimistic;
2
3 import org.jboss.cache.CacheImpl;
4 import org.jboss.cache.Fqn;
5 import org.jboss.cache.GlobalTransaction;
6 import org.jboss.cache.OptimisticTransactionEntry;
7 import org.jboss.cache.TransactionTable;
8 import org.jboss.cache.interceptors.Interceptor;
9 import org.jboss.cache.interceptors.OptimisticCreateIfNotExistsInterceptor;
10 import org.jboss.cache.interceptors.OptimisticNodeInterceptor;
11 import org.jboss.cache.loader.SamplePojo;
12 import org.jboss.cache.transaction.DummyTransactionManager;
13
14 import javax.transaction.Transaction JavaDoc;
15
16 /**
17  * @author xenephon
18  */

19 public class NodeInterceptorGetKeyValTest extends AbstractOptimisticTestCase
20 {
21
22
23    /**
24     * @param name
25     */

26    public NodeInterceptorGetKeyValTest(String JavaDoc name)
27    {
28       super(name);
29    }
30
31    public void testTransactionGetKeyMethod() throws Exception JavaDoc
32    {
33
34       TestListener listener = new TestListener();
35       final CacheImpl cache = createCacheWithListener(listener);
36
37       Interceptor interceptor = new OptimisticCreateIfNotExistsInterceptor();
38       interceptor.setCache(cache);
39       Interceptor nodeInterceptor = new OptimisticNodeInterceptor();
40       nodeInterceptor.setCache(cache);
41       MockInterceptor dummy = new MockInterceptor();
42       dummy.setCache(cache);
43
44       interceptor.setNext(nodeInterceptor);
45       nodeInterceptor.setNext(dummy);
46
47       cache.setInterceptorChain(interceptor);
48
49 // first set up a node with a pojo
50
DummyTransactionManager mgr = DummyTransactionManager.getInstance();
51       mgr.begin();
52       Transaction tx = mgr.getTransaction();
53
54       // inject InvocationContext
55
cache.getInvocationContext().setTransaction(tx);
56       cache.getInvocationContext().setGlobalTransaction(cache.getCurrentTransaction(tx));
57
58       SamplePojo pojo = new SamplePojo(21, "test");
59
60       cache.put("/one/two", "key1", pojo);
61
62       assertEquals(null, dummy.getCalled());
63       TransactionTable table = cache.getTransactionTable();
64
65       GlobalTransaction gtx = table.get(tx);
66
67       OptimisticTransactionEntry entry = (OptimisticTransactionEntry) table.get(gtx);
68
69       TransactionWorkspace workspace = entry.getTransactionWorkSpace();
70
71       //assert we can see this with a key value get in the transaction
72
assertEquals(pojo, cache.get("/one/two", "key1"));
73       mgr.commit();
74
75       //assert what should be the results of our call
76
assertEquals(3, workspace.getNodes().size());
77       assertNotNull(workspace.getNode(Fqn.fromString("/one/two")));
78       assertEquals(pojo, workspace.getNode(Fqn.fromString("/one/two")).get("key1"));
79       assertTrue(entry.getLocks().isEmpty());
80       assertEquals(1, entry.getModifications().size());
81       assertTrue(!cache.exists("/one/two"));
82       assertEquals(null, dummy.getCalled());
83
84       //assert that we cannot see the change if we have not put it into the cache
85
// we need to do this as we do not have the tx interceptor in this stack
86
mgr.begin();
87
88       Transaction tx2 = mgr.getTransaction();
89
90       // inject InvocationContext
91
cache.getInvocationContext().setTransaction(tx2);
92       cache.getInvocationContext().setGlobalTransaction(cache.getCurrentTransaction(tx2));
93
94       assertNull(cache.get("/one/two", "key1"));
95       mgr.commit();
96       cache.stop();
97    }
98
99    public void testTransactionGetKeyValOverwriteMethod() throws Exception JavaDoc
100    {
101
102       TestListener listener = new TestListener();
103       final CacheImpl cache = createCacheWithListener(listener);
104
105       Interceptor interceptor = new OptimisticCreateIfNotExistsInterceptor();
106       interceptor.setCache(cache);
107       Interceptor nodeInterceptor = new OptimisticNodeInterceptor();
108       nodeInterceptor.setCache(cache);
109       MockInterceptor dummy = new MockInterceptor();
110       dummy.setCache(cache);
111
112       interceptor.setNext(nodeInterceptor);
113       nodeInterceptor.setNext(dummy);
114
115       cache.setInterceptorChain(interceptor);
116
117 // first set up a node with a pojo
118
DummyTransactionManager mgr = DummyTransactionManager.getInstance();
119       mgr.begin();
120       Transaction tx = mgr.getTransaction();
121
122       // inject InvocationContext
123
cache.getInvocationContext().setTransaction(tx);
124       cache.getInvocationContext().setGlobalTransaction(cache.getCurrentTransaction(tx));
125
126       SamplePojo pojo = new SamplePojo(21, "test");
127
128       cache.put("/one/two", "key1", pojo);
129
130       //overwrite the map we just put in
131
SamplePojo pojo2 = new SamplePojo(22, "test2");
132
133       cache.put("/one/two", "key1", pojo2);
134
135       assertEquals(null, dummy.getCalled());
136       TransactionTable table = cache.getTransactionTable();
137
138       GlobalTransaction gtx = table.get(tx);
139
140       OptimisticTransactionEntry entry = (OptimisticTransactionEntry) table.get(gtx);
141
142       TransactionWorkspace workspace = entry.getTransactionWorkSpace();
143       assertEquals(pojo2, cache.get("/one/two", "key1"));
144       mgr.commit();
145
146       //assert what should be the results of our call
147
assertEquals(3, workspace.getNodes().size());
148       assertNotNull(workspace.getNode(Fqn.fromString("/one/two")));
149       assertEquals(pojo2, workspace.getNode(Fqn.fromString("/one/two")).get("key1"));
150       assertTrue(entry.getLocks().isEmpty());
151       assertEquals(2, entry.getModifications().size());
152       assertTrue(!cache.exists("/one/two"));
153       assertEquals(null, dummy.getCalled());
154
155       cache.stop();
156    }
157
158    public void testTransactionGetKeyValOverwriteNullMethod() throws Exception JavaDoc
159    {
160
161       TestListener listener = new TestListener();
162       final CacheImpl cache = createCacheWithListener(listener);
163
164       Interceptor interceptor = new OptimisticCreateIfNotExistsInterceptor();
165       interceptor.setCache(cache);
166       Interceptor nodeInterceptor = new OptimisticNodeInterceptor();
167       nodeInterceptor.setCache(cache);
168       MockInterceptor dummy = new MockInterceptor();
169       dummy.setCache(cache);
170
171       interceptor.setNext(nodeInterceptor);
172       nodeInterceptor.setNext(dummy);
173
174       cache.setInterceptorChain(interceptor);
175
176 // first set up a node with a pojo
177
DummyTransactionManager mgr = DummyTransactionManager.getInstance();
178       mgr.begin();
179       Transaction tx = mgr.getTransaction();
180
181       // inject InvocationContext
182
cache.getInvocationContext().setTransaction(tx);
183       cache.getInvocationContext().setGlobalTransaction(cache.getCurrentTransaction(tx));
184
185       SamplePojo pojo = new SamplePojo(21, "test");
186
187       cache.put("/one/two", "key1", pojo);
188
189
190       cache.put("/one/two", "key1", null);
191
192       assertEquals(null, dummy.getCalled());
193       TransactionTable table = cache.getTransactionTable();
194
195       GlobalTransaction gtx = table.get(tx);
196
197       OptimisticTransactionEntry entry = (OptimisticTransactionEntry) table.get(gtx);
198
199       TransactionWorkspace workspace = entry.getTransactionWorkSpace();
200
201       assertEquals(null, cache.get("/one/two", "key1"));
202
203       mgr.commit();
204       //assert what should be the results of our call
205
assertEquals(3, workspace.getNodes().size());
206       assertNotNull(workspace.getNode(Fqn.fromString("/one/two")));
207       assertEquals(null, workspace.getNode(Fqn.fromString("/one/two")).get("key1"));
208       assertTrue(entry.getLocks().isEmpty());
209       assertEquals(2, entry.getModifications().size());
210       assertTrue(!cache.exists("/one/two"));
211       assertEquals(null, dummy.getCalled());
212       cache.stop();
213    }
214
215
216    public void testTwoTransactionGetIsolationKeyValMethod() throws Exception JavaDoc
217    {
218
219       TestListener listener = new TestListener();
220       final CacheImpl cache = createCacheWithListener(listener);
221
222       Interceptor interceptor = new OptimisticCreateIfNotExistsInterceptor();
223       interceptor.setCache(cache);
224       Interceptor nodeInterceptor = new OptimisticNodeInterceptor();
225       nodeInterceptor.setCache(cache);
226       MockInterceptor dummy = new MockInterceptor();
227       dummy.setCache(cache);
228
229       interceptor.setNext(nodeInterceptor);
230       nodeInterceptor.setNext(dummy);
231
232       cache.setInterceptorChain(interceptor);
233
234 // first set up a node with a pojo
235
DummyTransactionManager mgr = DummyTransactionManager.getInstance();
236       mgr.begin();
237       Transaction tx = mgr.getTransaction();
238
239       // inject InvocationContext
240
cache.getInvocationContext().setTransaction(tx);
241       cache.getInvocationContext().setGlobalTransaction(cache.getCurrentTransaction(tx));
242
243       SamplePojo pojo = new SamplePojo(21, "test");
244
245       cache.put("/one/two", "key1", pojo);
246
247       assertEquals(pojo, cache.get("/one/two", "key1"));
248       //suspend current transaction
249
mgr.suspend();
250
251       //start a new transaction
252
mgr.begin();
253       Transaction tx2 = mgr.getTransaction();
254       // inject InvocationContext
255
cache.getInvocationContext().setTransaction(tx2);
256       cache.getInvocationContext().setGlobalTransaction(cache.getCurrentTransaction(tx2));
257
258       SamplePojo pojo2 = new SamplePojo(22, "test2");
259
260       cache.put("/one/two", "key2", pojo2);
261       assertEquals(null, cache.get("/one/two", "key1"));
262       assertEquals(pojo2, cache.get("/one/two", "key2"));
263
264
265       assertEquals(null, dummy.getCalled());
266       TransactionTable table = cache.getTransactionTable();
267
268
269       GlobalTransaction gtx = table.get(tx);
270
271       OptimisticTransactionEntry entry = (OptimisticTransactionEntry) table.get(gtx);
272
273       TransactionWorkspace workspace = entry.getTransactionWorkSpace();
274
275       //resume the suspended transaction
276
GlobalTransaction gtx2 = table.get(tx2);
277
278       OptimisticTransactionEntry entry2 = (OptimisticTransactionEntry) table.get(gtx2);
279
280       TransactionWorkspace workspace2 = entry2.getTransactionWorkSpace();
281
282       //commit both tx
283
mgr.commit();
284       mgr.resume(tx);
285       mgr.commit();
286
287       //assert that our keys are in one space
288
assertEquals(3, workspace.getNodes().size());
289       assertNotNull(workspace.getNode(Fqn.fromString("/one/two")));
290       assertEquals(pojo, workspace.getNode(Fqn.fromString("/one/two")).get("key1"));
291       assertEquals(null, workspace.getNode(Fqn.fromString("/one/two")).get("key2"));
292       assertTrue(entry.getLocks().isEmpty());
293       assertEquals(1, entry.getModifications().size());
294
295       //assert that our keys are in one space
296
assertEquals(3, workspace2.getNodes().size());
297       assertNotNull(workspace2.getNode(Fqn.fromString("/one/two")));
298       assertEquals(null, workspace2.getNode(Fqn.fromString("/one/two")).get("key1"));
299       assertEquals(pojo2, workspace2.getNode(Fqn.fromString("/one/two")).get("key2"));
300       assertTrue(entry2.getLocks().isEmpty());
301       assertEquals(1, entry2.getModifications().size());
302
303       assertTrue(!cache.exists("/one/two"));
304       assertEquals(null, dummy.getCalled());
305       cache.stop();
306    }
307
308
309 }
310
Popular Tags