KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > loader > CacheLoaderMethodCallCounterTest


1 /*
2  * JBoss, Home of Professional Open Source
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.cache.loader;
8
9 import junit.textui.TestRunner;
10 import org.jboss.cache.CacheImpl;
11
12 /**
13  * A simple non-failing unit test to measure how many times each method on a cache loader is called.
14  *
15  * @author <a HREF="mailto:manik@jboss.org">Manik Surtani (manik@jboss.org)</a>
16  */

17 public class CacheLoaderMethodCallCounterTest extends AbstractCacheLoaderTestBase
18 {
19    private CacheImpl cache;
20    private DummyCacheLoader dummyLoader;
21
22    public static void main(String JavaDoc[] args)
23    {
24       TestRunner.run(CacheLoaderMethodCallCounterTest.class);
25    }
26
27    protected void setUp() throws Exception JavaDoc
28    {
29       if (cache != null) tearDown();
30       cache = new CacheImpl();
31       cache.getConfiguration().setCacheLoaderConfig(getSingleCacheLoaderConfig("", DummyCacheLoader.class.getName(), "", false, false, false));
32       cache.start();
33       dummyLoader = (DummyCacheLoader) cache.getCacheLoaderManager().getCacheLoader();
34    }
35
36    protected void tearDown()
37    {
38       if (cache != null)
39          cache.stop();
40       cache.destroy();
41       cache = null;
42    }
43
44
45    public void testPut() throws Exception JavaDoc
46    {
47       cache.put("/node", "key", "value");
48       printReport("putKeyValue", dummyLoader);
49    }
50
51    public void testGet() throws Exception JavaDoc
52    {
53       cache.get("/node", "key");
54       printReport("getKey", dummyLoader);
55    }
56
57    public void testRemove() throws Exception JavaDoc
58    {
59       cache.remove("/node", "key");
60       printReport("removeKey", dummyLoader);
61    }
62
63    private void printReport(String JavaDoc test, DummyCacheLoader d)
64    {
65       System.out.println("------------------------------");
66       System.out.println(" Test name: " + test);
67       System.out.println(" cache loader stats:");
68       System.out.println(" put count: " + d.getPutCount());
69       System.out.println(" get count: " + d.getGetCount());
70       System.out.println(" exists count: " + d.getExistsCount());
71       System.out.println(" remove count: " + d.getRemoveCount());
72       System.out.println(" getChildrenNames count: " + d.getGetChildrenNamesCount());
73       System.out.println("------------------------------");
74    }
75
76    public void testLoopedGets() throws Exception JavaDoc
77    {
78       // put an object in cache
79
cache.put("/test", "key", "value");
80
81       // we should see this put in the cl
82
assertEquals(1, dummyLoader.getPutCount());
83       // the cloader interceptor does a get as well when doing the put ... ?
84
assertEquals(0, dummyLoader.getGetCount());
85
86       for (int i = 0; i < 2000; i++)
87       {
88          cache.get("/test");
89       }
90
91       assertEquals(1, dummyLoader.getPutCount());
92       assertEquals(1, dummyLoader.getGetCount());
93       assertEquals(0, dummyLoader.getRemoveCount());
94       assertEquals(0, dummyLoader.getExistsCount());
95    }
96 }
97
Popular Tags