KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > mgmt > MgmtCoreTest


1 package org.jboss.cache.mgmt;
2
3 import junit.framework.Test;
4 import junit.framework.TestCase;
5 import junit.framework.TestSuite;
6 import org.jboss.cache.CacheImpl;
7 import org.jboss.cache.Fqn;
8 import org.jboss.cache.config.Configuration;
9 import org.jboss.cache.interceptors.CacheMgmtInterceptor;
10
11 import java.util.HashMap JavaDoc;
12 import java.util.List JavaDoc;
13
14 /**
15  * Simple functional tests for CacheMgmtInterceptor
16  *
17  * @author Jerry Gauthier
18  * @version $Id: MgmtCoreTest.java,v 1.9 2006/12/30 19:48:46 msurtani Exp $
19  */

20 public class MgmtCoreTest extends TestCase
21 {
22    private static final String JavaDoc CAPITAL = "capital";
23    private static final String JavaDoc CURRENCY = "currency";
24    private static final String JavaDoc POPULATION = "population";
25    private static final String JavaDoc AREA = "area";
26
27    CacheImpl cache = null;
28
29    protected void setUp() throws Exception JavaDoc
30    {
31       super.setUp();
32       cache = new CacheImpl();
33       cache.getConfiguration().setCacheMode(Configuration.CacheMode.LOCAL);
34       cache.getConfiguration().setExposeManagementStatistics(true);
35       cache.create();
36       cache.start();
37    }
38
39    protected void tearDown() throws Exception JavaDoc
40    {
41       super.tearDown();
42       if (cache != null)
43       {
44          cache.stop();
45          cache.destroy();
46          cache = null;
47       }
48    }
49
50    public void testCacheMgmt() throws Exception JavaDoc
51    {
52       assertNotNull("Cache is null.", cache);
53
54       // populate the cache with test data
55
loadCacheData();
56
57       // Note: because these tests are normally executed without a server, the interceptor
58
// MBeans are usually not available for use in the tests. Consequently it's necessary
59
// to obtain a reference to the interceptor and work with it directly.
60
CacheMgmtInterceptor mgmt = getCacheMgmtInterceptor();
61       assertNotNull("CacheMgmtInterceptor not found.", mgmt);
62
63       // try some successful retrievals - fail if they miss since this shouldn't occur
64
Fqn key = Fqn.fromString("Europe/Austria");
65       assertNotNull("Retrieval error: expected to retrieve " + CAPITAL + " for " + key, cache.get(key, CAPITAL));
66       assertNotNull("Retrieval error: expected to retrieve " + CURRENCY + " for " + key, cache.get(key, CURRENCY));
67       assertNotNull("Retrieval error: expected to retrieve " + POPULATION + " for " + key, cache.get(key, POPULATION));
68       key = Fqn.fromString("Europe/England");
69       assertNotNull("Retrieval error: expected to retrieve " + CAPITAL + " for " + key, cache.get(key, CAPITAL));
70       assertNotNull("Retrieval error: expected to retrieve " + CURRENCY + " for " + key, cache.get(key, CURRENCY));
71       assertNotNull("Retrieval error: expected to retrieve " + POPULATION + " for " + key, cache.get(key, POPULATION));
72       key = Fqn.fromString("Europe/France");
73       assertNotNull("Retrieval error: expected to retrieve " + CAPITAL + " for " + key, cache.get(key, CAPITAL));
74       assertNotNull("Retrieval error: expected to retrieve " + CURRENCY + " for " + key, cache.get(key, CURRENCY));
75       assertNotNull("Retrieval error: expected to retrieve " + POPULATION + " for " + key, cache.get(key, POPULATION));
76       key = Fqn.fromString("Europe/Germany");
77       assertNotNull("Retrieval error: expected to retrieve " + CAPITAL + " for " + key, cache.get(key, CAPITAL));
78       assertNotNull("Retrieval error: expected to retrieve " + CURRENCY + " for " + key, cache.get(key, CURRENCY));
79       assertNotNull("Retrieval error: expected to retrieve " + POPULATION + " for " + key, cache.get(key, POPULATION));
80       key = Fqn.fromString("Europe/Italy");
81       assertNotNull("Retrieval error: expected to retrieve " + CAPITAL + " for " + key, cache.get(key, CAPITAL));
82       assertNotNull("Retrieval error: expected to retrieve " + CURRENCY + " for " + key, cache.get(key, CURRENCY));
83       assertNotNull("Retrieval error: expected to retrieve " + POPULATION + " for " + key, cache.get(key, POPULATION));
84       key = Fqn.fromString("Europe/Switzerland");
85       assertNotNull("Retrieval error: expected to retrieve " + CAPITAL + " for " + key, cache.get(key, CAPITAL));
86       assertNotNull("Retrieval error: expected to retrieve " + CURRENCY + " for " + key, cache.get(key, CURRENCY));
87       assertNotNull("Retrieval error: expected to retrieve " + POPULATION + " for " + key, cache.get(key, POPULATION));
88
89       // try some unsuccessful retrievals - fail if they hit since this shouldn't occur
90
key = Fqn.fromString("Europe/Austria");
91       assertNull("Retrieval error: did not expect to retrieve " + AREA + " for " + key, cache.get(key, AREA));
92       key = Fqn.fromString("Europe/England");
93       assertNull("Retrieval error: did not expect to retrieve " + AREA + " for " + key, cache.get(key, AREA));
94       key = Fqn.fromString("Europe/France");
95       assertNull("Retrieval error: did not expect to retrieve " + AREA + " for " + key, cache.get(key, AREA));
96       key = Fqn.fromString("Europe/Germany");
97       assertNull("Retrieval error: did not expect to retrieve " + AREA + " for " + key, cache.get(key, AREA));
98       key = Fqn.fromString("Europe/Italy");
99       assertNull("Retrieval error: did not expect to retrieve " + AREA + " for " + key, cache.get(key, AREA));
100       key = Fqn.fromString("Europe/Switzerland");
101       assertNull("Retrieval error: did not expect to retrieve " + AREA + " for " + key, cache.get(key, AREA));
102
103       // verify basic statistics for entries loaded into cache
104
assertEquals("NumberOfNodes count error: ", new Integer JavaDoc(13), new Integer JavaDoc(mgmt.getNumberOfNodes()));
105       assertEquals("NumberOfAttributes count error: ", new Integer JavaDoc(40), new Integer JavaDoc(mgmt.getNumberOfAttributes()));
106       assertEquals("Stores count error: ", new Long JavaDoc(40), new Long JavaDoc(mgmt.getStores()));
107       assertEquals("Evictions count error: ", new Long JavaDoc(0), new Long JavaDoc(mgmt.getEvictions()));
108       assertEquals("Hits count error: ", new Long JavaDoc(18), new Long JavaDoc(mgmt.getHits()));
109       assertEquals("Misses count error: ", new Long JavaDoc(6), new Long JavaDoc(mgmt.getMisses()));
110       assertEquals("HitMissRatio error: ", 0.75, mgmt.getHitMissRatio());
111       assertEquals("ReadWriteRatio error: ", 0.60, mgmt.getReadWriteRatio());
112
113       // now evict some nodes (each node has 3 attributes)
114
cache.evict(Fqn.fromString("Europe/Czech Republic"));
115       cache.evict(Fqn.fromString("Europe/Poland"));
116       assertEquals("NumberOfNodes count error after evictions: ", new Integer JavaDoc(11), new Integer JavaDoc(mgmt.getNumberOfNodes()));
117       assertEquals("NumberOfAttributes count error after evictions: ", new Integer JavaDoc(34), new Integer JavaDoc(mgmt.getNumberOfAttributes()));
118       assertEquals("Stores count error: ", new Long JavaDoc(40), new Long JavaDoc(mgmt.getStores()));
119       assertEquals("Evictions count error: ", new Long JavaDoc(2), new Long JavaDoc(mgmt.getEvictions()));
120
121       // time is measured in seconds so add a delay to ensure it's not rounded to zero
122
Thread.sleep(1000);
123       long t1 = mgmt.getElapsedTime();
124       if (t1 < 1)
125          fail("ElapsedTime should be greater than 0 seconds.");
126       t1 = mgmt.getTimeSinceReset();
127       if (t1 < 1)
128          fail("TimeSinceReset should be greater than 0 seconds.");
129       Thread.sleep(1000);
130
131       // now reset the statistics (node count and attribute count aren't affected)
132
mgmt.resetStatistics();
133
134       // check times again
135
t1 = mgmt.getElapsedTime();
136       if (t1 < 2)
137          fail("ElapsedTime after reset should be greater than 1 second.");
138       t1 = mgmt.getTimeSinceReset();
139       if (t1 > 1) // assumes that reset takes less than 2 seconds
140
fail("TimeSinceReset after reset should be less than 2 seconds.");
141
142       // check other statistics
143
assertEquals("NumberOfNodes count error after reset: ", new Integer JavaDoc(11), new Integer JavaDoc(mgmt.getNumberOfNodes()));
144       assertEquals("NumberOfAttributes count error after reset: ", new Integer JavaDoc(34), new Integer JavaDoc(mgmt.getNumberOfAttributes()));
145       assertEquals("Stores count error after reset: ", new Long JavaDoc(0), new Long JavaDoc(mgmt.getStores()));
146       assertEquals("Evictions count error after reset: ", new Long JavaDoc(0), new Long JavaDoc(mgmt.getEvictions()));
147       assertEquals("Hits count error after reset: ", new Long JavaDoc(0), new Long JavaDoc(mgmt.getHits()));
148       assertEquals("Misses count error after reset: ", new Long JavaDoc(0), new Long JavaDoc(mgmt.getMisses()));
149    }
150
151    private void loadCacheData() throws Exception JavaDoc
152    {
153       cache.put("Europe", new HashMap JavaDoc());
154       cache.put("Europe/Austria", new HashMap JavaDoc());
155       cache.put("Europe/Czech Republic", new HashMap JavaDoc());
156       cache.put("Europe/England", new HashMap JavaDoc());
157       cache.put("Europe/France", new HashMap JavaDoc());
158       cache.put("Europe/Germany", new HashMap JavaDoc());
159       cache.put("Europe/Italy", new HashMap JavaDoc());
160       cache.put("Europe/Poland", new HashMap JavaDoc());
161       cache.put("Europe/Switzerland", new HashMap JavaDoc());
162
163       cache.put("Europe/Austria", CAPITAL, "Vienna");
164       cache.put("Europe/Czech Republic", CAPITAL, "Prague");
165       cache.put("Europe/England", CAPITAL, "London");
166       cache.put("Europe/France", CAPITAL, "Paris");
167       cache.put("Europe/Germany", CAPITAL, "Berlin");
168       cache.put("Europe/Italy", CAPITAL, "Rome");
169       cache.put("Europe/Poland", CAPITAL, "Warsaw");
170       cache.put("Europe/Switzerland", CAPITAL, "Bern");
171
172       cache.put("Europe/Austria", CURRENCY, "Euro");
173       cache.put("Europe/Czech Republic", CURRENCY, "Czech Koruna");
174       cache.put("Europe/England", CURRENCY, "British Pound");
175       cache.put("Europe/France", CURRENCY, "Euro");
176       cache.put("Europe/Germany", CURRENCY, "Euro");
177       cache.put("Europe/Italy", CURRENCY, "Euro");
178       cache.put("Europe/Poland", CURRENCY, "Zloty");
179       cache.put("Europe/Switzerland", CURRENCY, "Swiss Franc");
180
181       cache.put("Europe/Austria", POPULATION, 8184691);
182       cache.put("Europe/Czech Republic", POPULATION, 10241138);
183       cache.put("Europe/England", POPULATION, 60441457);
184       cache.put("Europe/France", POPULATION, 60656178);
185       cache.put("Europe/Germany", POPULATION, 82431390);
186       cache.put("Europe/Italy", POPULATION, 58103033);
187       cache.put("Europe/Poland", POPULATION, 38635144);
188       cache.put("Europe/Switzerland", POPULATION, 7489370);
189
190       HashMap JavaDoc albania = new HashMap JavaDoc(4);
191       albania.put(CAPITAL, "Tirana");
192       albania.put(CURRENCY, "Lek");
193       albania.put(POPULATION, 3563112);
194       albania.put(AREA, 28748);
195       cache.put("Europe/Albania", albania);
196
197       HashMap JavaDoc hungary = new HashMap JavaDoc(4);
198       hungary.put(CAPITAL, "Budapest");
199       hungary.put(CURRENCY, "Forint");
200       hungary.put(POPULATION, 10006835);
201       hungary.put(AREA, 93030);
202       cache.put("Europe/Hungary", hungary);
203
204       HashMap JavaDoc romania = new HashMap JavaDoc(4);
205       romania.put(CAPITAL, "Bucharest");
206       romania.put(CURRENCY, "Leu");
207       romania.put(POPULATION, 22329977);
208       romania.put(AREA, 237500);
209       cache.put("Europe/Romania", romania);
210
211       HashMap JavaDoc slovakia = new HashMap JavaDoc(4);
212       slovakia.put(CAPITAL, "Bratislava");
213       slovakia.put(CURRENCY, "Slovak Koruna");
214       slovakia.put(POPULATION, 5431363);
215       slovakia.put(AREA, 48845);
216       cache.put("Europe/Slovakia", slovakia);
217
218    }
219
220    private CacheMgmtInterceptor getCacheMgmtInterceptor()
221    {
222       List JavaDoc interceptors = cache.getInterceptors();
223       if (interceptors.isEmpty())
224          return null;
225
226       for (int i = 0; i < interceptors.size(); i++)
227       {
228          Object JavaDoc o = interceptors.get(i);
229          if (o instanceof CacheMgmtInterceptor)
230             return (CacheMgmtInterceptor) o;
231       }
232       return null;
233    }
234
235    public static Test suite()
236    {
237       return new TestSuite(MgmtCoreTest.class);
238    }
239
240 }
241
Popular Tags