KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > interceptors > EvictionInterceptorTest


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.cache.interceptors;
8
9 import junit.framework.TestCase;
10 import org.jboss.cache.CacheImpl;
11 import org.jboss.cache.Fqn;
12 import org.jboss.cache.Node;
13 import org.jboss.cache.Region;
14 import org.jboss.cache.RegionManager;
15 import org.jboss.cache.eviction.DummyEvictionConfiguration;
16 import org.jboss.cache.eviction.EvictedEventNode;
17 import org.jboss.cache.eviction.EvictionPolicyConfig;
18 import org.jboss.cache.eviction.NodeEventType;
19 import org.jboss.cache.lock.IsolationLevel;
20 import org.jboss.cache.marshall.MethodCall;
21 import org.jboss.cache.marshall.MethodCallFactory;
22 import org.jboss.cache.marshall.MethodDeclarations;
23
24 import java.util.HashMap JavaDoc;
25 import java.util.Map JavaDoc;
26
27 /**
28  * @author Daniel Huang (dhuang@jboss.org)
29  * @version $Revision: $
30  */

31 public class EvictionInterceptorTest extends TestCase
32 {
33    private static final String JavaDoc fqn1 = "/a/b/c";
34    private static final String JavaDoc fqn2 = "/a/b";
35    private static final String JavaDoc fqn3 = "/a/b/d";
36
37    private static final String JavaDoc fqn4 = "/d/e/f";
38
39    private CacheImpl cache;
40    private Interceptor interceptor;
41    private RegionManager regionManager;
42
43    public void setUp() throws Exception JavaDoc
44    {
45       super.setUp();
46       regionManager = new RegionManager();
47       EvictionPolicyConfig config = new DummyEvictionConfiguration();
48       regionManager.setUsingEvictions(true);
49
50       regionManager.getRegion(RegionManager.DEFAULT_REGION, true).setEvictionPolicy(config);
51
52       regionManager.getRegion("/a/b/c", true).setEvictionPolicy(config);
53       regionManager.getRegion("/a/b/c/d", true).setEvictionPolicy(config);
54       regionManager.getRegion("/a/b", true).setEvictionPolicy(config);
55
56       regionManager.getRegion("/d/e/f", true).setEvictionPolicy(config);
57       regionManager.getRegion("/d/e/g", true).setEvictionPolicy(config);
58       regionManager.getRegion("/d/e", true).setEvictionPolicy(config);
59
60       cache = new CacheImpl();
61       cache.getConfiguration().setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup");
62       cache.getConfiguration().setIsolationLevel(IsolationLevel.SERIALIZABLE);
63
64       // make the interceptor chain (separate from the CacheImpl object.
65

66       interceptor = new CacheMgmtInterceptor();
67       interceptor.setCache(cache);
68
69       TxInterceptor ti = new TxInterceptor();
70       ti.setCache(cache);
71       interceptor.setNext(ti);
72
73       UnlockInterceptor ui = new UnlockInterceptor();
74       ui.setCache(cache);
75       ti.setNext(ui);
76
77       PessimisticLockInterceptor pli = new PessimisticLockInterceptor();
78       pli.setCache(cache);
79       ui.setNext(pli);
80
81       EvictionInterceptor ei = new EvictionInterceptor();
82       ei.setCache(cache);
83       ei.setRegionManager(regionManager);
84       pli.setNext(ei);
85
86       CallInterceptor ci = new CallInterceptor();
87       ci.setCache(cache);
88       ci.setTreeCacheInstance(cache);
89       ei.setNext(ci);
90
91       cache.getConfiguration().setCacheMode("LOCAL");
92       cache.start();
93    }
94
95    public void tearDown() throws Exception JavaDoc
96    {
97       super.tearDown();
98       cache.remove("/");
99       cache.stop();
100    }
101
102    public void testVisitNode() throws Throwable JavaDoc
103    {
104       // make sure node that doesn't exist does not result in a node visit event.
105

106       MethodCall mc = MethodCallFactory.create(MethodDeclarations.getNodeMethodLocal,
107               Fqn.fromString(fqn1));
108       interceptor.invoke(mc);
109       Region regionABC = regionManager.getRegion(fqn1, false);
110       assertNull(regionABC.takeLastEventNode());
111
112       cache.put(fqn1, "key", "value");
113       assertEquals("value", cache.get(fqn1, "key"));
114       Node node = cache.get(fqn1);
115       assertNotNull(node);
116       assertEquals("value", node.get("key"));
117
118       cache.put(fqn3, "key", "value");
119       assertEquals("value", cache.get(fqn3, "key"));
120       node = cache.get(fqn3);
121       assertNotNull(node);
122       assertEquals("value", node.get("key"));
123
124
125       mc = MethodCallFactory.create(MethodDeclarations.getNodeMethodLocal,
126               Fqn.fromString(fqn1));
127       interceptor.invoke(mc);
128
129       regionABC = regionManager.getRegion(fqn1, false);
130       EvictedEventNode event = regionABC.takeLastEventNode();
131       assertEquals(NodeEventType.VISIT_NODE_EVENT, event.getEventType());
132       assertEquals(fqn1, event.getFqn().toString());
133       assertNull(regionABC.takeLastEventNode());
134
135       mc = MethodCallFactory.create(MethodDeclarations.getNodeMethodLocal,
136               Fqn.fromString(fqn2));
137       interceptor.invoke(mc);
138
139       Region regionAB = regionManager.getRegion(fqn2, false);
140       event = regionAB.takeLastEventNode();
141       assertEquals(NodeEventType.VISIT_NODE_EVENT, event.getEventType());
142       assertEquals(fqn2, event.getFqn().toString());
143       assertNull(regionAB.takeLastEventNode());
144
145       mc = MethodCallFactory.create(MethodDeclarations.getDataMapMethodLocal, Fqn.fromString(fqn3));
146       interceptor.invoke(mc);
147       Region regionABD = regionManager.getRegion(fqn3, false);
148       event = regionABD.takeLastEventNode();
149       assertEquals(NodeEventType.VISIT_NODE_EVENT, event.getEventType());
150       assertEquals(fqn3, event.getFqn().toString());
151       assertNull(regionABD.takeLastEventNode());
152
153       for (int i = 0; i < 10; i++)
154       {
155          Fqn fqn = Fqn.fromString(fqn3);
156          mc = MethodCallFactory.create(MethodDeclarations.getNodeMethodLocal, fqn);
157          interceptor.invoke(mc);
158       }
159
160       for (int i = 0; i < 10; i++)
161       {
162          Region region = regionManager.getRegion(fqn3, false);
163          event = region.takeLastEventNode();
164          assertEquals(NodeEventType.VISIT_NODE_EVENT, event.getEventType());
165          assertEquals(fqn3, event.getFqn().toString());
166       }
167
168       assertNull(regionManager.getRegion(fqn3, false).takeLastEventNode());
169
170       // check null handling.
171
mc = MethodCallFactory.create(MethodDeclarations.getDataMapMethodLocal, new Object JavaDoc[]{null});
172       interceptor.invoke(mc);
173
174    }
175
176    public void testVisitElement() throws Throwable JavaDoc
177    {
178       // make sure a get/visit on an empty node and empty element results in no cache events being added to event queue
179
// aka MarshRegion.
180
Fqn fqn = Fqn.fromString(fqn4);
181       Object JavaDoc key = "key";
182       MethodCall mc = MethodCallFactory.create(MethodDeclarations.getKeyValueMethodLocal, fqn, key, false);
183       interceptor.invoke(mc);
184       Region region = regionManager.getRegion(fqn.toString(), false);
185       assertNull(region.takeLastEventNode());
186
187       // add the node but try to get on a null element should result in no cache events being added to MarshRegion.
188
cache.put(fqn, "wrongkey", "");
189       mc = MethodCallFactory.create(MethodDeclarations.getKeyValueMethodLocal, fqn, key, false);
190       interceptor.invoke(mc);
191       assertNull(region.takeLastEventNode());
192
193       // now make sure if we try to get on the node/key we just created in cache, that this DOES add a EvictedEventNode to
194
// the MarshRegion.
195
mc = MethodCallFactory.create(MethodDeclarations.getKeyValueMethodLocal, fqn, "wrongkey", false);
196       interceptor.invoke(mc);
197       EvictedEventNode event = region.takeLastEventNode();
198       assertEquals(fqn, event.getFqn());
199       assertEquals(NodeEventType.VISIT_NODE_EVENT, event.getEventType());
200
201       assertNull(region.takeLastEventNode());
202
203       cache.put(fqn4, key, "value");
204       // test on element granularity configured node.
205
fqn = Fqn.fromString(fqn4);
206       mc = MethodCallFactory.create(MethodDeclarations.getKeyValueMethodLocal, fqn, key, false);
207       interceptor.invoke(mc);
208
209       region = regionManager.getRegion(fqn.toString(), false);
210       event = region.takeLastEventNode();
211
212       assertEquals(NodeEventType.VISIT_NODE_EVENT, event.getEventType());
213       assertEquals(fqn, event.getFqn());
214
215       assertNull(region.takeLastEventNode());
216
217       fqn = Fqn.fromString("/d/e/g");
218       for (int i = 0; i < 1000; i++)
219       {
220          key = i;
221
222          cache.put("/d/e/g", key, "");
223
224          mc = MethodCallFactory.create(MethodDeclarations.getKeyValueMethodLocal, fqn, key, true);
225          interceptor.invoke(mc);
226       }
227
228       region = regionManager.getRegion(fqn.toString(), false);
229       for (int i = 0; i < 1000; i++)
230       {
231          event = region.takeLastEventNode();
232          assertEquals(fqn, event.getFqn());
233          assertEquals(NodeEventType.VISIT_NODE_EVENT, event.getEventType());
234       }
235
236       cache.put("/a/b/c", key, "");
237       fqn = Fqn.fromString("/a/b/c");
238       mc = MethodCallFactory.create(MethodDeclarations.getKeyValueMethodLocal, fqn, key, false);
239       interceptor.invoke(mc);
240
241       region = regionManager.getRegion(fqn.toString(), false);
242       event = region.takeLastEventNode();
243
244       assertNull(region.takeLastEventNode());
245       assertEquals(NodeEventType.VISIT_NODE_EVENT, event.getEventType());
246       assertEquals(fqn, event.getFqn());
247
248       for (int i = 0; i < 1000; i++)
249       {
250          key = i;
251
252          cache.put(fqn.toString(), key, "");
253
254          mc = MethodCallFactory.create(MethodDeclarations.getKeyValueMethodLocal, fqn, key, true);
255          interceptor.invoke(mc);
256       }
257
258       for (int i = 0; i < 1000; i++)
259       {
260          event = region.takeLastEventNode();
261          assertEquals(NodeEventType.VISIT_NODE_EVENT, event.getEventType());
262          assertEquals(fqn, event.getFqn());
263       }
264
265       assertNull(region.takeLastEventNode());
266
267       // check null handling
268
mc = MethodCallFactory.create(MethodDeclarations.getKeyValueMethodLocal, null, key, true);
269       interceptor.invoke(mc);
270       assertNull(region.takeLastEventNode());
271
272       mc = MethodCallFactory.create(MethodDeclarations.getKeyValueMethodLocal, null, null, true);
273       interceptor.invoke(mc);
274       assertNull(region.takeLastEventNode());
275
276       mc = MethodCallFactory.create(MethodDeclarations.getKeyValueMethodLocal, fqn, null, true);
277       interceptor.invoke(mc);
278       assertNull(region.takeLastEventNode());
279    }
280
281    public void testCreateNode() throws Throwable JavaDoc
282    {
283       Map JavaDoc data = new HashMap JavaDoc();
284       for (int i = 0; i < 100; i++)
285       {
286          data.put(i, i);
287       }
288
289       // this region is node granularity
290
Fqn fqn = Fqn.fromString("/a/b/c");
291
292       MethodCall mc = MethodCallFactory.create(MethodDeclarations.putDataMethodLocal, null, fqn, data, false);
293       interceptor.invoke(mc);
294
295       Region region = regionManager.getRegion(fqn.toString(), false);
296       EvictedEventNode event = region.takeLastEventNode();
297
298       assertEquals(NodeEventType.ADD_NODE_EVENT, event.getEventType());
299       assertEquals(fqn, event.getFqn());
300       assertEquals(100, event.getElementDifference());
301
302       Node node = cache.get(fqn.toString());
303       assertNotNull(node);
304
305       for (int i = 0; i < 100; i++)
306       {
307          assertTrue(node.getData().containsKey(i));
308          assertEquals(i, node.get(i));
309       }
310
311       for (int i = 0; i < 100; i++)
312       {
313          mc = MethodCallFactory.create(MethodDeclarations.putKeyValMethodLocal, null, fqn, i,
314                  "value", false);
315          interceptor.invoke(mc);
316
317          assertEquals("value", cache.get(fqn.toString(), i));
318       }
319
320       for (int i = 0; i < 100; i++)
321       {
322          event = region.takeLastEventNode();
323          assertNotNull(event);
324          assertEquals(fqn, event.getFqn());
325          assertEquals(NodeEventType.ADD_ELEMENT_EVENT, event.getEventType());
326       }
327
328       assertNull(region.takeLastEventNode());
329
330       fqn = Fqn.fromString("/a/b");
331       mc = MethodCallFactory.create(MethodDeclarations.putDataEraseMethodLocal, null, fqn, data, false, false);
332       interceptor.invoke(mc);
333       event = regionManager.getRegion(fqn.toString(), false).takeLastEventNode();
334       assertFalse(event.isResetElementCount());
335       assertEquals(NodeEventType.ADD_NODE_EVENT, event.getEventType());
336       assertEquals(fqn, event.getFqn());
337       assertEquals(100, event.getElementDifference());
338       assertNull(regionManager.getRegion(fqn.toString(), false).takeLastEventNode());
339       node = cache.get(fqn.toString());
340       assertEquals(100, node.getData().size());
341
342       assertNotNull(node);
343
344       for (int i = 0; i < 100; i++)
345       {
346          assertTrue(node.getData().containsKey(i));
347          assertEquals(i, node.get(i));
348       }
349
350       mc = MethodCallFactory.create(MethodDeclarations.putDataEraseMethodLocal, null, fqn, data, false, true);
351       interceptor.invoke(mc);
352       event = regionManager.getRegion(fqn.toString(), false).takeLastEventNode();
353       assertEquals(NodeEventType.ADD_NODE_EVENT, event.getEventType());
354       assertEquals(fqn, event.getFqn());
355       assertEquals(100, event.getElementDifference());
356       assertTrue(event.isResetElementCount());
357       assertNull(regionManager.getRegion(fqn.toString(), false).takeLastEventNode());
358
359
360       node = cache.get(fqn.toString());
361       assertEquals(100, node.getData().size());
362       assertNotNull(node);
363
364       for (int i = 0; i < 100; i++)
365       {
366          assertTrue(node.getData().containsKey(i));
367          assertEquals(i, node.get(i));
368       }
369
370    }
371
372    public void testCreateElement() throws Throwable JavaDoc
373    {
374       Fqn fqn = Fqn.fromString("/d/e/f");
375       Region region = regionManager.getRegion(fqn.toString(), false);
376       Object JavaDoc key = "key";
377       Object JavaDoc value = "value";
378
379       MethodCall mc = MethodCallFactory.create(MethodDeclarations.putKeyValMethodLocal,
380               null, fqn, key, value, false);
381       interceptor.invoke(mc);
382       assertEquals("value", cache.get(fqn, key));
383       EvictedEventNode event = region.takeLastEventNode();
384       assertEquals(NodeEventType.ADD_ELEMENT_EVENT, event.getEventType());
385       assertEquals(1, event.getElementDifference());
386       assertEquals(fqn, event.getFqn());
387       assertEquals("value", cache.get(fqn, key));
388       assertNull(region.takeLastEventNode());
389
390       mc = MethodCallFactory.create(MethodDeclarations.putKeyValMethodLocal,
391               null, fqn, key, value, false);
392       interceptor.invoke(mc);
393       assertEquals("value", cache.get(fqn, key));
394       event = region.takeLastEventNode();
395       assertEquals(NodeEventType.ADD_ELEMENT_EVENT, event.getEventType());
396       assertEquals(1, event.getElementDifference());
397       assertEquals(fqn, event.getFqn());
398       assertEquals("value", cache.get(fqn, key));
399       assertNull(region.takeLastEventNode());
400
401    }
402
403    public void testRemoveNode() throws Throwable JavaDoc
404    {
405       Fqn fqn = Fqn.fromString("/a/b/c");
406       cache.put(fqn, "a", "b");
407       cache.put(fqn, "b", "c");
408
409       MethodCall mc = MethodCallFactory.create(MethodDeclarations.removeDataMethodLocal, null, fqn, false);
410       interceptor.invoke(mc);
411
412       assertEquals(0, cache.get(fqn).getData().size());
413       Region region = regionManager.getRegion(fqn.toString(), false);
414       EvictedEventNode event = region.takeLastEventNode();
415       assertEquals(NodeEventType.REMOVE_NODE_EVENT, event.getEventType());
416       assertEquals(fqn, event.getFqn());
417       assertNull(region.takeLastEventNode());
418
419       mc = MethodCallFactory.create(MethodDeclarations.removeNodeMethodLocal, null, fqn, false);
420       interceptor.invoke(mc);
421
422       assertNull(cache.get(fqn));
423       event = region.takeLastEventNode();
424       assertEquals(NodeEventType.REMOVE_NODE_EVENT, event.getEventType());
425       assertEquals(fqn, event.getFqn());
426       assertNull(region.takeLastEventNode());
427    }
428
429    public void testRemoveElement() throws Throwable JavaDoc
430    {
431       Fqn fqn = Fqn.fromString("/a/b/c");
432       cache.put(fqn, "a", "b");
433       cache.put(fqn, "b", "c");
434
435       MethodCall mc = MethodCallFactory.create(MethodDeclarations.removeKeyMethodLocal,
436               null, fqn, "a", false);
437       interceptor.invoke(mc);
438
439       assertNull(cache.get(fqn, "a"));
440       Region region = regionManager.getRegion(fqn.toString(), false);
441       EvictedEventNode event = region.takeLastEventNode();
442       assertEquals(NodeEventType.REMOVE_ELEMENT_EVENT, event.getEventType());
443       assertEquals(fqn, event.getFqn());
444       assertEquals(1, event.getElementDifference());
445       assertNull(region.takeLastEventNode());
446
447       mc = MethodCallFactory.create(MethodDeclarations.removeKeyMethodLocal,
448               null, fqn, "b", false);
449       interceptor.invoke(mc);
450
451       assertNull(cache.get(fqn, "b"));
452       event = region.takeLastEventNode();
453       assertEquals(NodeEventType.REMOVE_ELEMENT_EVENT, event.getEventType());
454       assertEquals(fqn, event.getFqn());
455       assertEquals(1, event.getElementDifference());
456       assertNull(region.takeLastEventNode());
457
458       mc = MethodCallFactory.create(MethodDeclarations.removeKeyMethodLocal,
459               null, fqn, "b", false);
460       interceptor.invoke(mc);
461
462       event = region.takeLastEventNode();
463       assertNull(event);
464    }
465
466    public void testMixedEvent() throws Throwable JavaDoc
467    {
468       Map JavaDoc data = new HashMap JavaDoc();
469       for (int i = 0; i < 100; i++)
470       {
471          data.put(i, i);
472       }
473
474       // this region is node granularity
475
Fqn fqn = Fqn.fromString("/a/b/c");
476
477       MethodCall mc = MethodCallFactory.create(MethodDeclarations.putDataMethodLocal, null, fqn, data, false);
478       interceptor.invoke(mc);
479
480       Region region = regionManager.getRegion(fqn.toString(), false);
481       EvictedEventNode event = region.takeLastEventNode();
482
483       assertEquals(NodeEventType.ADD_NODE_EVENT, event.getEventType());
484       assertEquals(fqn, event.getFqn());
485       assertEquals(100, event.getElementDifference());
486       assertNull(region.takeLastEventNode());
487
488       mc = MethodCallFactory.create(MethodDeclarations.getNodeMethodLocal,
489               fqn);
490       interceptor.invoke(mc);
491       event = region.takeLastEventNode();
492       assertEquals(NodeEventType.VISIT_NODE_EVENT, event.getEventType());
493       assertEquals(fqn, event.getFqn());
494       assertNull(region.takeLastEventNode());
495
496       mc = MethodCallFactory.create(MethodDeclarations.removeNodeMethodLocal, null, fqn, false);
497       interceptor.invoke(mc);
498       assertNull(cache.get(fqn));
499       event = region.takeLastEventNode();
500       assertEquals(NodeEventType.REMOVE_NODE_EVENT, event.getEventType());
501       assertEquals(fqn, event.getFqn());
502       assertNull(region.takeLastEventNode());
503
504       Object JavaDoc key = "key";
505       Object JavaDoc value = "value";
506       mc = MethodCallFactory.create(MethodDeclarations.putKeyValMethodLocal,
507               null, fqn, key, value, false);
508       interceptor.invoke(mc);
509       assertEquals("value", cache.get(fqn, key));
510       event = region.takeLastEventNode();
511       assertEquals(NodeEventType.ADD_ELEMENT_EVENT, event.getEventType());
512       assertEquals(1, event.getElementDifference());
513       assertEquals(fqn, event.getFqn());
514       assertEquals("value", cache.get(fqn, key));
515       assertNull(region.takeLastEventNode());
516
517       mc = MethodCallFactory.create(MethodDeclarations.getKeyValueMethodLocal, fqn, key, false);
518       interceptor.invoke(mc);
519       region = regionManager.getRegion(fqn.toString(), false);
520       event = region.takeLastEventNode();
521       assertEquals(NodeEventType.VISIT_NODE_EVENT, event.getEventType());
522       assertEquals(fqn, event.getFqn());
523       assertNull(region.takeLastEventNode());
524
525       mc = MethodCallFactory.create(MethodDeclarations.removeKeyMethodLocal,
526               null, fqn, key, false);
527       interceptor.invoke(mc);
528
529       assertNull(cache.get(fqn, key));
530       event = region.takeLastEventNode();
531       assertEquals(NodeEventType.REMOVE_ELEMENT_EVENT, event.getEventType());
532       assertEquals(fqn, event.getFqn());
533       assertEquals(1, event.getElementDifference());
534       assertNull(region.takeLastEventNode());
535
536    }
537
538 }
Popular Tags