KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Created on 17-Feb-2005
3  *
4  *
5  *
6  */

7 package org.jboss.cache.optimistic;
8
9 import org.jboss.cache.CacheImpl;
10 import org.jboss.cache.Fqn;
11 import org.jboss.cache.GlobalTransaction;
12 import org.jboss.cache.Node;
13 import org.jboss.cache.OptimisticTransactionEntry;
14 import org.jboss.cache.TransactionTable;
15 import org.jboss.cache.factories.InterceptorChainFactory;
16 import org.jboss.cache.interceptors.CallInterceptor;
17 import org.jboss.cache.interceptors.Interceptor;
18 import org.jboss.cache.loader.SamplePojo;
19
20 import javax.transaction.Transaction JavaDoc;
21 import javax.transaction.TransactionManager JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.Map JavaDoc;
24
25 /**
26  * @author xenephon
27  */

28 public class NodeInterceptorRemoveNodeTest extends AbstractOptimisticTestCase
29 {
30    private CacheImpl cache;
31    private TestListener listener;
32    private MockInterceptor dummy;
33    private TransactionManager JavaDoc mgr;
34
35    /**
36     * @param name
37     */

38    public NodeInterceptorRemoveNodeTest(String JavaDoc name)
39    {
40       super(name);
41    }
42
43    protected void setUp() throws Exception JavaDoc
44    {
45       super.setUp();
46       listener = new TestListener();
47       cache = createCacheWithListener(listener);
48
49       dummy = new MockInterceptor();
50       dummy.setCache(cache);
51
52       Interceptor interceptor = cache.getInterceptorChain().get(0); // get the first interceptor
53
// replace the last (call) interceptor with the mock interceptor.
54
Interceptor next = interceptor, prev = interceptor;
55       while (!(next instanceof CallInterceptor))
56       {
57          prev = next;
58          next = next.getNext();
59       }
60
61       prev.setNext(dummy);
62
63       InterceptorChainFactory.setLastInterceptorPointer(interceptor, dummy);
64
65       cache.setInterceptorChain(interceptor);
66       mgr = cache.getTransactionManager();
67    }
68
69    protected void tearDown()
70    {
71       super.tearDown();
72       cache.stop();
73    }
74
75    public void testTransactionRemoveNotExistsNodeMethod() throws Exception JavaDoc
76    {
77       mgr.begin();
78       Transaction tx = mgr.getTransaction();
79
80       // inject InvocationContext
81
cache.getInvocationContext().setTransaction(tx);
82       cache.getInvocationContext().setGlobalTransaction(cache.getCurrentTransaction(tx));
83
84
85       cache.remove("/one/two");
86
87       TransactionTable table = cache.getTransactionTable();
88       GlobalTransaction gtx = table.get(tx);
89       OptimisticTransactionEntry entry = (OptimisticTransactionEntry) table.get(gtx);
90       TransactionWorkspace workspace = entry.getTransactionWorkSpace();
91
92       mgr.commit();
93       assertEquals(null, dummy.getCalled());
94
95       //assert what should be the results of our call
96
assertEquals(0, workspace.getNodes().size());
97
98       assertTrue(entry.getLocks().isEmpty());
99       assertEquals(1, entry.getModifications().size());
100       assertTrue(!cache.exists("/one/two"));
101       assertEquals(null, dummy.getCalled());
102
103       assertEquals(0, listener.getNodesAdded());
104    }
105
106    public void testTransactionRemoveNodeMethod() throws Exception JavaDoc
107    {
108       mgr.begin();
109       Transaction tx = mgr.getTransaction();
110
111       // inject InvocationContext
112
cache.getInvocationContext().setTransaction(tx);
113       cache.getInvocationContext().setGlobalTransaction(cache.getCurrentTransaction(tx));
114
115       SamplePojo pojo = new SamplePojo(21, "test");
116       Map JavaDoc temp = new HashMap JavaDoc();
117       temp.put("key1", pojo);
118       cache.put("/one/two", temp);
119
120       cache.remove("/one/two");
121       assertEquals(null, dummy.getCalled());
122       TransactionTable table = cache.getTransactionTable();
123
124       GlobalTransaction gtx = table.get(tx);
125
126       OptimisticTransactionEntry entry = (OptimisticTransactionEntry) table.get(gtx);
127
128       TransactionWorkspace workspace = entry.getTransactionWorkSpace();
129
130       mgr.commit();
131
132       //assert what should be the results of our call
133
assertEquals(3, workspace.getNodes().size());
134
135       assertNotNull(workspace.getNode(Fqn.fromString("/one/two")));
136       assertEquals(true, workspace.getNode(Fqn.fromString("/one/two")).isDeleted());
137       assertNotNull(workspace.getNode(Fqn.fromString("/one")));
138       assertEquals(false, workspace.getNode(Fqn.fromString("/one")).isDeleted());
139       assertEquals(0, workspace.getNode(Fqn.fromString("/one")).getMergedChildren().size());
140       assertEquals(null, workspace.getNode(Fqn.fromString("/one")).getChild(Fqn.fromString("/two")));
141       assertEquals(2, entry.getModifications().size());
142       assertTrue(!cache.exists("/one/two"));
143       assertEquals(null, dummy.getCalled());
144    }
145
146    public void testTransactionRemoveIntermediateNodeMethod() throws Exception JavaDoc
147    {
148       mgr.begin();
149       Transaction tx = mgr.getTransaction();
150
151       // inject InvocationContext
152
cache.getInvocationContext().setTransaction(tx);
153       cache.getInvocationContext().setGlobalTransaction(cache.getCurrentTransaction(tx));
154
155       SamplePojo pojo = new SamplePojo(21, "test");
156       Map JavaDoc temp = new HashMap JavaDoc();
157       temp.put("key1", pojo);
158       cache.put("/one/two", temp);
159
160       cache.remove("/one");
161       assertEquals(null, dummy.getCalled());
162       TransactionTable table = cache.getTransactionTable();
163
164       GlobalTransaction gtx = table.get(tx);
165
166       OptimisticTransactionEntry entry = (OptimisticTransactionEntry) table.get(gtx);
167
168       TransactionWorkspace workspace = entry.getTransactionWorkSpace();
169
170       mgr.commit();
171
172       //assert what should be the results of our call
173
assertEquals(3, workspace.getNodes().size());
174
175       assertNotNull(workspace.getNode(Fqn.fromString("/one/two")));
176       assertEquals(true, workspace.getNode(Fqn.fromString("/one/two")).isDeleted());
177       assertNotNull(workspace.getNode(Fqn.fromString("/one")));
178       assertEquals(true, workspace.getNode(Fqn.fromString("/one")).isDeleted());
179       assertEquals(1, workspace.getNode(Fqn.fromString("/one")).getMergedChildren().size());
180       assertNotNull(workspace.getNode(Fqn.fromString("/one")).getChild(Fqn.fromString("two")));
181       assertEquals(null, workspace.getNode(Fqn.fromString("/")).getChild(Fqn.fromString("/one")));
182       assertEquals(2, entry.getModifications().size());
183       assertTrue(!cache.exists("/one/two"));
184       assertEquals(null, dummy.getCalled());
185    }
186
187    public void testTransactionRemoveTwiceMethod() throws Exception JavaDoc
188    {
189       mgr.begin();
190       Transaction tx = mgr.getTransaction();
191
192       // inject InvocationContext
193
cache.getInvocationContext().setTransaction(tx);
194       cache.getInvocationContext().setGlobalTransaction(cache.getCurrentTransaction(tx));
195
196       SamplePojo pojo = new SamplePojo(21, "test");
197       Map JavaDoc temp = new HashMap JavaDoc();
198       temp.put("key1", pojo);
199       cache.put("/one/two", temp);
200
201       // get the transaction stuff
202
TransactionTable table = cache.getTransactionTable();
203       GlobalTransaction gtx = table.get(tx);
204
205       OptimisticTransactionEntry entry = (OptimisticTransactionEntry) table.get(gtx);
206
207       TransactionWorkspace workspace = entry.getTransactionWorkSpace();
208
209       workspace.getNode(Fqn.fromString("/one"));
210       workspace.getNode(Fqn.fromString("/one/two"));
211
212
213       cache.remove("/one");
214
215       assertNotNull(workspace.getNode(Fqn.fromString("/one/two")));
216       assertEquals(true, workspace.getNode(Fqn.fromString("/one/two")).isDeleted());
217       assertNotNull(workspace.getNode(Fqn.fromString("/one")));
218       assertEquals(true, workspace.getNode(Fqn.fromString("/one")).isDeleted());
219
220       //will still have alink from one to two
221
assertNotNull(workspace.getNode(Fqn.fromString("/one")).getChild(Fqn.fromString("two")));
222       assertEquals(null, workspace.getNode(Fqn.fromString("/")).getChild(Fqn.fromString("one")));
223
224       //now put /one/two back in
225
cache.remove("/one");
226
227
228       assertNotNull(workspace.getNode(Fqn.fromString("/one/two")));
229       assertEquals(true, workspace.getNode(Fqn.fromString("/one/two")).isDeleted());
230       assertNotNull(workspace.getNode(Fqn.fromString("/one")));
231       assertEquals(true, workspace.getNode(Fqn.fromString("/one")).isDeleted());
232
233       //will still have alink from one to two
234
assertNotNull(workspace.getNode(Fqn.fromString("/one")).getChild(Fqn.fromString("two")));
235       assertEquals(null, workspace.getNode(Fqn.fromString("/")).getChild(Fqn.fromString("one")));
236
237       assertEquals(null, dummy.getCalled());
238
239
240       mgr.commit();
241
242       //assert what should be the results of our call
243
assertEquals(3, workspace.getNodes().size());
244
245
246       assertEquals(3, entry.getModifications().size());
247       assertTrue(!cache.exists("/one/two"));
248       assertEquals(null, dummy.getCalled());
249    }
250
251
252    public void testTransactionRemovePutNodeMethod() throws Exception JavaDoc
253    {
254       mgr.begin();
255       Transaction tx = mgr.getTransaction();
256
257       // inject InvocationContext
258
cache.getInvocationContext().setTransaction(tx);
259       cache.getInvocationContext().setGlobalTransaction(cache.getCurrentTransaction(tx));
260
261       SamplePojo pojo = new SamplePojo(21, "test");
262       Map JavaDoc temp = new HashMap JavaDoc();
263       temp.put("key1", pojo);
264       cache.put("/one/two", temp);
265
266       // get the transaction stuff
267
TransactionTable table = cache.getTransactionTable();
268       GlobalTransaction gtx = table.get(tx);
269
270       OptimisticTransactionEntry entry = (OptimisticTransactionEntry) table.get(gtx);
271
272       TransactionWorkspace workspace = entry.getTransactionWorkSpace();
273
274       Node one = workspace.getNode(Fqn.fromString("/one"));
275       Node two = workspace.getNode(Fqn.fromString("/one/two"));
276
277
278       cache.remove("/one");
279
280       assertNotNull(workspace.getNode(Fqn.fromString("/one/two")));
281       assertEquals(true, workspace.getNode(Fqn.fromString("/one/two")).isDeleted());
282       assertNotNull(workspace.getNode(Fqn.fromString("/one")));
283       assertEquals(true, workspace.getNode(Fqn.fromString("/one")).isDeleted());
284
285       //will still have alink from one to two
286
assertNotNull(workspace.getNode(Fqn.fromString("/one")).getChild(Fqn.fromString("two")));
287       assertEquals(null, workspace.getNode(Fqn.fromString("/")).getChild(Fqn.fromString("one")));
288
289       //now put /one/two back in
290
cache.put("/one/two", temp);
291
292       WorkspaceNode oneAfter = workspace.getNode(Fqn.fromString("/one"));
293       WorkspaceNode twoAfter = workspace.getNode(Fqn.fromString("/one/two"));
294
295       assertNotSame(one, oneAfter);
296       assertEquals(false, oneAfter.isDeleted());
297       assertNotSame(two, twoAfter);
298       assertEquals(false, twoAfter.isDeleted());
299
300       assertEquals(twoAfter.getNode(), workspace.getNode(Fqn.fromString("/one")).getChild(Fqn.fromString("two")));
301       assertEquals(oneAfter.getNode(), workspace.getNode(Fqn.fromString("/")).getChild(Fqn.fromString("one")));
302
303       assertEquals(null, dummy.getCalled());
304
305
306       mgr.commit();
307
308       //assert what should be the results of our call
309
assertEquals(3, workspace.getNodes().size());
310
311
312       assertEquals(3, entry.getModifications().size());
313       assertEquals(null, dummy.getCalled());
314       assertEquals(4, listener.getNodesAdded());
315    }
316
317
318    public void testTransactionRemovePutkeyValMethod() throws Exception JavaDoc
319    {
320       mgr.begin();
321       Transaction tx = mgr.getTransaction();
322
323       // inject InvocationContext
324
cache.getInvocationContext().setTransaction(tx);
325       cache.getInvocationContext().setGlobalTransaction(cache.getCurrentTransaction(tx));
326
327       SamplePojo pojo = new SamplePojo(21, "test");
328       Map JavaDoc temp = new HashMap JavaDoc();
329       temp.put("key1", pojo);
330       cache.put("/one/two", temp);
331
332       // get the transaction stuff
333
TransactionTable table = cache.getTransactionTable();
334       GlobalTransaction gtx = table.get(tx);
335
336       OptimisticTransactionEntry entry = (OptimisticTransactionEntry) table.get(gtx);
337
338       TransactionWorkspace workspace = entry.getTransactionWorkSpace();
339
340       Node one = workspace.getNode(Fqn.fromString("/one"));
341       Node two = workspace.getNode(Fqn.fromString("/one/two"));
342
343
344       cache.remove("/one");
345
346       assertNotNull(workspace.getNode(Fqn.fromString("/one/two")));
347       assertEquals(true, workspace.getNode(Fqn.fromString("/one/two")).isDeleted());
348       assertNotNull(workspace.getNode(Fqn.fromString("/one")));
349       assertEquals(true, workspace.getNode(Fqn.fromString("/one")).isDeleted());
350
351       //will still have alink from one to two
352
assertNotNull(workspace.getNode(Fqn.fromString("/one")).getChild(Fqn.fromString("two")));
353       assertEquals(null, workspace.getNode(Fqn.fromString("/")).getChild(Fqn.fromString("one")));
354
355       //now put /one back in
356
SamplePojo pojo2 = new SamplePojo(21, "test");
357       cache.put("/one", "key1", pojo2);
358
359       WorkspaceNode oneAfter = workspace.getNode(Fqn.fromString("/one"));
360       WorkspaceNode twoAfter = workspace.getNode(Fqn.fromString("/one/two"));
361
362       assertNotSame(one, oneAfter);
363       assertEquals(false, oneAfter.isDeleted());
364       assertEquals(two, twoAfter);
365       assertEquals(true, twoAfter.isDeleted());
366
367       assertNull(workspace.getNode(Fqn.fromString("/one")).getChild(Fqn.fromString("two")));
368       assertEquals(oneAfter.getNode(), workspace.getNode(Fqn.fromString("/")).getChild(Fqn.fromString("one")));
369
370       assertEquals(null, dummy.getCalled());
371
372
373       mgr.commit();
374
375       assertEquals(pojo2, workspace.getNode(Fqn.fromString("/one")).get("key1"));
376       //assert what should be the results of our call
377
assertEquals(3, workspace.getNodes().size());
378
379
380       assertEquals(3, entry.getModifications().size());
381       assertTrue(!cache.exists("/one/two"));
382       assertEquals(null, dummy.getCalled());
383       assertEquals(3, listener.getNodesAdded());
384    }
385
386    public void testTransactionRemoveSubNodeMethod() throws Exception JavaDoc
387    {
388       mgr.begin();
389       Transaction tx = mgr.getTransaction();
390
391       // inject InvocationContext
392
cache.getInvocationContext().setTransaction(tx);
393       cache.getInvocationContext().setGlobalTransaction(cache.getCurrentTransaction(tx));
394
395       SamplePojo pojo = new SamplePojo(21, "test");
396       Map JavaDoc temp = new HashMap JavaDoc();
397       temp.put("key1", pojo);
398       cache.put("/one/two", temp);
399
400       // get the transaction stuff
401
TransactionTable table = cache.getTransactionTable();
402       GlobalTransaction gtx = table.get(tx);
403
404       OptimisticTransactionEntry entry = (OptimisticTransactionEntry) table.get(gtx);
405
406       TransactionWorkspace workspace = entry.getTransactionWorkSpace();
407
408       WorkspaceNode one = workspace.getNode(Fqn.fromString("/one"));
409
410       assertEquals(1, one.getMergedChildren().size());
411
412       cache.remove("/one/two");
413
414       assertNotNull(workspace.getNode(Fqn.fromString("/one/two")));
415       assertEquals(true, workspace.getNode(Fqn.fromString("/one/two")).isDeleted());
416       assertNotNull(workspace.getNode(Fqn.fromString("/one")));
417       assertEquals(false, workspace.getNode(Fqn.fromString("/one")).isDeleted());
418
419       //will still have alink from one to two
420
assertEquals(null, workspace.getNode(Fqn.fromString("/one")).getChild(Fqn.fromString("two")));
421
422
423       assertEquals(null, dummy.getCalled());
424
425
426       mgr.commit();
427
428       assertEquals(0, workspace.getNode(Fqn.fromString("/one")).getMergedChildren().size());
429       //assert what should be the results of our call
430
assertEquals(3, workspace.getNodes().size());
431
432
433       assertEquals(2, entry.getModifications().size());
434       assertTrue(!cache.exists("/one/two"));
435       assertEquals(null, dummy.getCalled());
436       assertEquals(2, listener.getNodesAdded());
437    }
438 }
439
Popular Tags