KickJava   Java API By Example, From Geeks To Geeks.

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


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 NodeInterceptorKeyValTest extends AbstractOptimisticTestCase
20 {
21
22
23    /**
24     * @param name
25     */

26    public NodeInterceptorKeyValTest(String JavaDoc name)
27    {
28       super(name);
29    }
30
31    public void testTransactionPutKeyMethod() 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       mgr.commit();
72
73       //assert what should be the results of our call
74
assertEquals(3, workspace.getNodes().size());
75       assertNotNull(workspace.getNode(Fqn.fromString("/one/two")));
76       assertEquals(pojo, workspace.getNode(Fqn.fromString("/one/two")).get("key1"));
77       assertTrue(entry.getLocks().isEmpty());
78       assertEquals(1, entry.getModifications().size());
79       assertTrue(!cache.exists("/one/two"));
80       assertEquals(null, dummy.getCalled());
81       cache.stop();
82    }
83
84    public void testTransactionKeyValOverwriteMethod() throws Exception JavaDoc
85    {
86
87       TestListener listener = new TestListener();
88       final CacheImpl cache = createCacheWithListener(listener);
89
90       Interceptor interceptor = new OptimisticCreateIfNotExistsInterceptor();
91       interceptor.setCache(cache);
92       Interceptor nodeInterceptor = new OptimisticNodeInterceptor();
93       nodeInterceptor.setCache(cache);
94       MockInterceptor dummy = new MockInterceptor();
95       dummy.setCache(cache);
96
97       interceptor.setNext(nodeInterceptor);
98       nodeInterceptor.setNext(dummy);
99
100       cache.setInterceptorChain(interceptor);
101
102 // first set up a node with a pojo
103
DummyTransactionManager mgr = DummyTransactionManager.getInstance();
104       mgr.begin();
105       Transaction tx = mgr.getTransaction();
106
107       // inject InvocationContext
108
cache.getInvocationContext().setTransaction(tx);
109       cache.getInvocationContext().setGlobalTransaction(cache.getCurrentTransaction(tx));
110
111       SamplePojo pojo = new SamplePojo(21, "test");
112
113       cache.put("/one/two", "key1", pojo);
114
115       //overwrite the map we just put in
116
SamplePojo pojo2 = new SamplePojo(21, "test");
117
118       cache.put("/one/two", "key1", pojo2);
119
120       assertEquals(null, dummy.getCalled());
121       TransactionTable table = cache.getTransactionTable();
122
123       GlobalTransaction gtx = table.get(tx);
124
125       OptimisticTransactionEntry entry = (OptimisticTransactionEntry) table.get(gtx);
126
127       TransactionWorkspace workspace = entry.getTransactionWorkSpace();
128
129       mgr.commit();
130       //assert what should be the results of our call
131
assertEquals(3, workspace.getNodes().size());
132       assertNotNull(workspace.getNode(Fqn.fromString("/one/two")));
133       assertEquals(pojo2, workspace.getNode(Fqn.fromString("/one/two")).get("key1"));
134       assertTrue(entry.getLocks().isEmpty());
135       assertEquals(2, entry.getModifications().size());
136       assertTrue(!cache.exists("/one/two"));
137       assertEquals(null, dummy.getCalled());
138       cache.stop();
139    }
140
141    public void testTransactionKeyValOverwriteNullMethod() throws Exception JavaDoc
142    {
143
144       TestListener listener = new TestListener();
145       final CacheImpl cache = createCacheWithListener(listener);
146
147       Interceptor interceptor = new OptimisticCreateIfNotExistsInterceptor();
148       interceptor.setCache(cache);
149       Interceptor nodeInterceptor = new OptimisticNodeInterceptor();
150       nodeInterceptor.setCache(cache);
151       MockInterceptor dummy = new MockInterceptor();
152       dummy.setCache(cache);
153
154       interceptor.setNext(nodeInterceptor);
155       nodeInterceptor.setNext(dummy);
156
157       cache.setInterceptorChain(interceptor);
158
159 // first set up a node with a pojo
160
DummyTransactionManager mgr = DummyTransactionManager.getInstance();
161       mgr.begin();
162       Transaction tx = mgr.getTransaction();
163
164       // inject InvocationContext
165
cache.getInvocationContext().setTransaction(tx);
166       cache.getInvocationContext().setGlobalTransaction(cache.getCurrentTransaction(tx));
167
168       SamplePojo pojo = new SamplePojo(21, "test");
169
170       cache.put("/one/two", "key1", pojo);
171
172
173       cache.put("/one/two", "key1", null);
174
175       assertEquals(null, dummy.getCalled());
176       TransactionTable table = cache.getTransactionTable();
177
178       GlobalTransaction gtx = table.get(tx);
179
180       OptimisticTransactionEntry entry = (OptimisticTransactionEntry) table.get(gtx);
181
182       TransactionWorkspace workspace = entry.getTransactionWorkSpace();
183
184       mgr.commit();
185       //assert what should be the results of our call
186
assertEquals(3, workspace.getNodes().size());
187       assertNotNull(workspace.getNode(Fqn.fromString("/one/two")));
188       assertEquals(null, workspace.getNode(Fqn.fromString("/one/two")).get("key1"));
189       assertTrue(entry.getLocks().isEmpty());
190       assertEquals(2, entry.getModifications().size());
191       assertTrue(!cache.exists("/one/two"));
192       assertEquals(null, dummy.getCalled());
193       cache.stop();
194    }
195
196
197    public void testTransactionAdditionlaKeyValMethod() throws Exception JavaDoc
198    {
199
200       TestListener listener = new TestListener();
201       final CacheImpl cache = createCacheWithListener(listener);
202
203       Interceptor interceptor = new OptimisticCreateIfNotExistsInterceptor();
204       interceptor.setCache(cache);
205       Interceptor nodeInterceptor = new OptimisticNodeInterceptor();
206       nodeInterceptor.setCache(cache);
207       MockInterceptor dummy = new MockInterceptor();
208       dummy.setCache(cache);
209
210       interceptor.setNext(nodeInterceptor);
211       nodeInterceptor.setNext(dummy);
212
213       cache.setInterceptorChain(interceptor);
214
215 // first set up a node with a pojo
216
DummyTransactionManager mgr = DummyTransactionManager.getInstance();
217       mgr.begin();
218       Transaction tx = mgr.getTransaction();
219
220       // inject InvocationContext
221
cache.getInvocationContext().setTransaction(tx);
222       cache.getInvocationContext().setGlobalTransaction(cache.getCurrentTransaction(tx));
223
224       SamplePojo pojo = new SamplePojo(21, "test");
225
226       cache.put("/one/two", "key1", pojo);
227
228       SamplePojo pojo2 = new SamplePojo(21, "test");
229
230       cache.put("/one/two", "key2", pojo2);
231
232       assertEquals(null, dummy.getCalled());
233       TransactionTable table = cache.getTransactionTable();
234
235       GlobalTransaction gtx = table.get(tx);
236
237       OptimisticTransactionEntry entry = (OptimisticTransactionEntry) table.get(gtx);
238
239       TransactionWorkspace workspace = entry.getTransactionWorkSpace();
240
241       mgr.commit();
242       //assert what should be the results of our call
243
assertEquals(3, workspace.getNodes().size());
244       assertNotNull(workspace.getNode(Fqn.fromString("/one/two")));
245
246       assertEquals(pojo, workspace.getNode(Fqn.fromString("/one/two")).get("key1"));
247       assertEquals(pojo2, workspace.getNode(Fqn.fromString("/one/two")).get("key2"));
248       assertTrue(entry.getLocks().isEmpty());
249       assertEquals(2, entry.getModifications().size());
250       assertTrue(!cache.exists("/one/two"));
251       assertEquals(null, dummy.getCalled());
252       cache.stop();
253    }
254
255    public void testTwoTransactionAdditionKeyValMethod() throws Exception JavaDoc
256    {
257
258       TestListener listener = new TestListener();
259       final CacheImpl cache = createCacheWithListener(listener);
260
261       Interceptor interceptor = new OptimisticCreateIfNotExistsInterceptor();
262       interceptor.setCache(cache);
263       Interceptor nodeInterceptor = new OptimisticNodeInterceptor();
264       nodeInterceptor.setCache(cache);
265       MockInterceptor dummy = new MockInterceptor();
266       dummy.setCache(cache);
267
268       interceptor.setNext(nodeInterceptor);
269       nodeInterceptor.setNext(dummy);
270
271       cache.setInterceptorChain(interceptor);
272
273 // first set up a node with a pojo
274
DummyTransactionManager mgr = DummyTransactionManager.getInstance();
275       mgr.begin();
276       Transaction tx = mgr.getTransaction();
277
278       // inject InvocationContext
279
cache.getInvocationContext().setTransaction(tx);
280       cache.getInvocationContext().setGlobalTransaction(cache.getCurrentTransaction(tx));
281
282       SamplePojo pojo = new SamplePojo(21, "test");
283
284       cache.put("/one/two", "key1", pojo);
285
286       //suspend current transaction
287
mgr.suspend();
288
289       //start a new transaction
290
mgr.begin();
291       Transaction tx2 = mgr.getTransaction();
292       cache.getInvocationContext().setTransaction(tx2);
293       cache.getInvocationContext().setGlobalTransaction(cache.getCurrentTransaction(tx2));
294
295       SamplePojo pojo2 = new SamplePojo(21, "test");
296
297       cache.put("/one/two", "key2", pojo2);
298
299       assertEquals(null, dummy.getCalled());
300       TransactionTable table = cache.getTransactionTable();
301
302
303       GlobalTransaction gtx = table.get(tx);
304
305       OptimisticTransactionEntry entry = (OptimisticTransactionEntry) table.get(gtx);
306
307       TransactionWorkspace workspace = entry.getTransactionWorkSpace();
308
309       //resume the suspended transaction
310
GlobalTransaction gtx2 = table.get(tx2);
311
312       OptimisticTransactionEntry entry2 = (OptimisticTransactionEntry) table.get(gtx2);
313
314       TransactionWorkspace workspace2 = entry2.getTransactionWorkSpace();
315
316       //commit both tx
317
mgr.commit();
318       mgr.resume(tx);
319       mgr.commit();
320
321       //assert that our keys are in one space
322
assertEquals(3, workspace.getNodes().size());
323       assertNotNull(workspace.getNode(Fqn.fromString("/one/two")));
324       assertEquals(pojo, workspace.getNode(Fqn.fromString("/one/two")).get("key1"));
325       assertEquals(null, workspace.getNode(Fqn.fromString("/one/two")).get("key2"));
326       assertTrue(entry.getLocks().isEmpty());
327       assertEquals(1, entry.getModifications().size());
328
329       //assert that our keys are in one space
330
assertEquals(3, workspace2.getNodes().size());
331       assertNotNull(workspace2.getNode(Fqn.fromString("/one/two")));
332       assertEquals(null, workspace2.getNode(Fqn.fromString("/one/two")).get("key1"));
333       assertEquals(pojo2, workspace2.getNode(Fqn.fromString("/one/two")).get("key2"));
334       assertTrue(entry2.getLocks().isEmpty());
335       assertEquals(1, entry2.getModifications().size());
336
337       assertTrue(!cache.exists("/one/two"));
338       assertEquals(null, dummy.getCalled());
339       cache.stop();
340    }
341
342
343 }
344
Popular Tags