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.DefaultCacheFactory; |
8 |
| import org.jboss.cache.Fqn; |
9 |
| import org.jboss.cache.config.Configuration; |
10 |
| import org.jboss.cache.interceptors.CacheMgmtInterceptor; |
11 |
| |
12 |
| import java.util.HashMap; |
13 |
| import java.util.List; |
14 |
| |
15 |
| |
16 |
| |
17 |
| |
18 |
| |
19 |
| |
20 |
| |
21 |
| public class MgmtCoreTest extends TestCase |
22 |
| { |
23 |
| private static final String CAPITAL = "capital"; |
24 |
| private static final String CURRENCY = "currency"; |
25 |
| private static final String POPULATION = "population"; |
26 |
| private static final String AREA = "area"; |
27 |
| |
28 |
| CacheImpl cache = null; |
29 |
| |
30 |
1
| protected void setUp() throws Exception
|
31 |
| { |
32 |
1
| super.setUp();
|
33 |
1
| cache = (CacheImpl) DefaultCacheFactory.getInstance().createCache(false);
|
34 |
1
| cache.getConfiguration().setCacheMode(Configuration.CacheMode.LOCAL);
|
35 |
1
| cache.getConfiguration().setExposeManagementStatistics(true);
|
36 |
1
| cache.create();
|
37 |
1
| cache.start();
|
38 |
| } |
39 |
| |
40 |
1
| protected void tearDown() throws Exception
|
41 |
| { |
42 |
1
| super.tearDown();
|
43 |
1
| if (cache != null)
|
44 |
| { |
45 |
1
| cache.stop();
|
46 |
1
| cache.destroy();
|
47 |
1
| cache = null;
|
48 |
| } |
49 |
| } |
50 |
| |
51 |
1
| public void testCacheMgmt() throws Exception
|
52 |
| { |
53 |
1
| assertNotNull("Cache is null.", cache);
|
54 |
| |
55 |
| |
56 |
1
| loadCacheData();
|
57 |
| |
58 |
| |
59 |
| |
60 |
| |
61 |
1
| CacheMgmtInterceptor mgmt = getCacheMgmtInterceptor();
|
62 |
1
| assertNotNull("CacheMgmtInterceptor not found.", mgmt);
|
63 |
| |
64 |
| |
65 |
1
| Fqn key = Fqn.fromString("Europe/Austria");
|
66 |
1
| assertNotNull("Retrieval error: expected to retrieve " + CAPITAL + " for " + key, cache.get(key, CAPITAL));
|
67 |
1
| assertNotNull("Retrieval error: expected to retrieve " + CURRENCY + " for " + key, cache.get(key, CURRENCY));
|
68 |
1
| assertNotNull("Retrieval error: expected to retrieve " + POPULATION + " for " + key, cache.get(key, POPULATION));
|
69 |
1
| key = Fqn.fromString("Europe/England");
|
70 |
1
| assertNotNull("Retrieval error: expected to retrieve " + CAPITAL + " for " + key, cache.get(key, CAPITAL));
|
71 |
1
| assertNotNull("Retrieval error: expected to retrieve " + CURRENCY + " for " + key, cache.get(key, CURRENCY));
|
72 |
1
| assertNotNull("Retrieval error: expected to retrieve " + POPULATION + " for " + key, cache.get(key, POPULATION));
|
73 |
1
| key = Fqn.fromString("Europe/France");
|
74 |
1
| assertNotNull("Retrieval error: expected to retrieve " + CAPITAL + " for " + key, cache.get(key, CAPITAL));
|
75 |
1
| assertNotNull("Retrieval error: expected to retrieve " + CURRENCY + " for " + key, cache.get(key, CURRENCY));
|
76 |
1
| assertNotNull("Retrieval error: expected to retrieve " + POPULATION + " for " + key, cache.get(key, POPULATION));
|
77 |
1
| key = Fqn.fromString("Europe/Germany");
|
78 |
1
| assertNotNull("Retrieval error: expected to retrieve " + CAPITAL + " for " + key, cache.get(key, CAPITAL));
|
79 |
1
| assertNotNull("Retrieval error: expected to retrieve " + CURRENCY + " for " + key, cache.get(key, CURRENCY));
|
80 |
1
| assertNotNull("Retrieval error: expected to retrieve " + POPULATION + " for " + key, cache.get(key, POPULATION));
|
81 |
1
| key = Fqn.fromString("Europe/Italy");
|
82 |
1
| assertNotNull("Retrieval error: expected to retrieve " + CAPITAL + " for " + key, cache.get(key, CAPITAL));
|
83 |
1
| assertNotNull("Retrieval error: expected to retrieve " + CURRENCY + " for " + key, cache.get(key, CURRENCY));
|
84 |
1
| assertNotNull("Retrieval error: expected to retrieve " + POPULATION + " for " + key, cache.get(key, POPULATION));
|
85 |
1
| key = Fqn.fromString("Europe/Switzerland");
|
86 |
1
| assertNotNull("Retrieval error: expected to retrieve " + CAPITAL + " for " + key, cache.get(key, CAPITAL));
|
87 |
1
| assertNotNull("Retrieval error: expected to retrieve " + CURRENCY + " for " + key, cache.get(key, CURRENCY));
|
88 |
1
| assertNotNull("Retrieval error: expected to retrieve " + POPULATION + " for " + key, cache.get(key, POPULATION));
|
89 |
| |
90 |
| |
91 |
1
| key = Fqn.fromString("Europe/Austria");
|
92 |
1
| assertNull("Retrieval error: did not expect to retrieve " + AREA + " for " + key, cache.get(key, AREA));
|
93 |
1
| key = Fqn.fromString("Europe/England");
|
94 |
1
| assertNull("Retrieval error: did not expect to retrieve " + AREA + " for " + key, cache.get(key, AREA));
|
95 |
1
| key = Fqn.fromString("Europe/France");
|
96 |
1
| assertNull("Retrieval error: did not expect to retrieve " + AREA + " for " + key, cache.get(key, AREA));
|
97 |
1
| key = Fqn.fromString("Europe/Germany");
|
98 |
1
| assertNull("Retrieval error: did not expect to retrieve " + AREA + " for " + key, cache.get(key, AREA));
|
99 |
1
| key = Fqn.fromString("Europe/Italy");
|
100 |
1
| assertNull("Retrieval error: did not expect to retrieve " + AREA + " for " + key, cache.get(key, AREA));
|
101 |
1
| key = Fqn.fromString("Europe/Switzerland");
|
102 |
1
| assertNull("Retrieval error: did not expect to retrieve " + AREA + " for " + key, cache.get(key, AREA));
|
103 |
| |
104 |
| |
105 |
1
| assertEquals("NumberOfNodes count error: ", new Integer(13), new Integer(mgmt.getNumberOfNodes()));
|
106 |
1
| assertEquals("NumberOfAttributes count error: ", new Integer(40), new Integer(mgmt.getNumberOfAttributes()));
|
107 |
1
| assertEquals("Stores count error: ", new Long(40), new Long(mgmt.getStores()));
|
108 |
1
| assertEquals("Evictions count error: ", new Long(0), new Long(mgmt.getEvictions()));
|
109 |
1
| assertEquals("Hits count error: ", new Long(18), new Long(mgmt.getHits()));
|
110 |
1
| assertEquals("Misses count error: ", new Long(6), new Long(mgmt.getMisses()));
|
111 |
1
| assertEquals("HitMissRatio error: ", 0.75, mgmt.getHitMissRatio());
|
112 |
1
| assertEquals("ReadWriteRatio error: ", 0.60, mgmt.getReadWriteRatio());
|
113 |
| |
114 |
| |
115 |
1
| cache.evict(Fqn.fromString("Europe/Czech Republic"));
|
116 |
1
| cache.evict(Fqn.fromString("Europe/Poland"));
|
117 |
1
| assertEquals("NumberOfNodes count error after evictions: ", new Integer(11), new Integer(mgmt.getNumberOfNodes()));
|
118 |
1
| assertEquals("NumberOfAttributes count error after evictions: ", new Integer(34), new Integer(mgmt.getNumberOfAttributes()));
|
119 |
1
| assertEquals("Stores count error: ", new Long(40), new Long(mgmt.getStores()));
|
120 |
1
| assertEquals("Evictions count error: ", new Long(2), new Long(mgmt.getEvictions()));
|
121 |
| |
122 |
| |
123 |
1
| Thread.sleep(1000);
|
124 |
1
| long t1 = mgmt.getElapsedTime();
|
125 |
1
| if (t1 < 1)
|
126 |
| { |
127 |
0
| fail("ElapsedTime should be greater than 0 seconds.");
|
128 |
| } |
129 |
1
| t1 = mgmt.getTimeSinceReset();
|
130 |
1
| if (t1 < 1)
|
131 |
| { |
132 |
0
| fail("TimeSinceReset should be greater than 0 seconds.");
|
133 |
| } |
134 |
1
| Thread.sleep(1000);
|
135 |
| |
136 |
| |
137 |
1
| mgmt.resetStatistics();
|
138 |
| |
139 |
| |
140 |
1
| t1 = mgmt.getElapsedTime();
|
141 |
1
| if (t1 < 2)
|
142 |
| { |
143 |
0
| fail("ElapsedTime after reset should be greater than 1 second.");
|
144 |
| } |
145 |
1
| t1 = mgmt.getTimeSinceReset();
|
146 |
1
| if (t1 > 1)
|
147 |
| { |
148 |
0
| fail("TimeSinceReset after reset should be less than 2 seconds.");
|
149 |
| } |
150 |
| |
151 |
| |
152 |
1
| assertEquals("NumberOfNodes count error after reset: ", new Integer(11), new Integer(mgmt.getNumberOfNodes()));
|
153 |
1
| assertEquals("NumberOfAttributes count error after reset: ", new Integer(34), new Integer(mgmt.getNumberOfAttributes()));
|
154 |
1
| assertEquals("Stores count error after reset: ", new Long(0), new Long(mgmt.getStores()));
|
155 |
1
| assertEquals("Evictions count error after reset: ", new Long(0), new Long(mgmt.getEvictions()));
|
156 |
1
| assertEquals("Hits count error after reset: ", new Long(0), new Long(mgmt.getHits()));
|
157 |
1
| assertEquals("Misses count error after reset: ", new Long(0), new Long(mgmt.getMisses()));
|
158 |
| } |
159 |
| |
160 |
1
| private void loadCacheData() throws Exception
|
161 |
| { |
162 |
1
| cache.put("Europe", new HashMap());
|
163 |
1
| cache.put("Europe/Austria", new HashMap());
|
164 |
1
| cache.put("Europe/Czech Republic", new HashMap());
|
165 |
1
| cache.put("Europe/England", new HashMap());
|
166 |
1
| cache.put("Europe/France", new HashMap());
|
167 |
1
| cache.put("Europe/Germany", new HashMap());
|
168 |
1
| cache.put("Europe/Italy", new HashMap());
|
169 |
1
| cache.put("Europe/Poland", new HashMap());
|
170 |
1
| cache.put("Europe/Switzerland", new HashMap());
|
171 |
| |
172 |
1
| cache.put("Europe/Austria", CAPITAL, "Vienna");
|
173 |
1
| cache.put("Europe/Czech Republic", CAPITAL, "Prague");
|
174 |
1
| cache.put("Europe/England", CAPITAL, "London");
|
175 |
1
| cache.put("Europe/France", CAPITAL, "Paris");
|
176 |
1
| cache.put("Europe/Germany", CAPITAL, "Berlin");
|
177 |
1
| cache.put("Europe/Italy", CAPITAL, "Rome");
|
178 |
1
| cache.put("Europe/Poland", CAPITAL, "Warsaw");
|
179 |
1
| cache.put("Europe/Switzerland", CAPITAL, "Bern");
|
180 |
| |
181 |
1
| cache.put("Europe/Austria", CURRENCY, "Euro");
|
182 |
1
| cache.put("Europe/Czech Republic", CURRENCY, "Czech Koruna");
|
183 |
1
| cache.put("Europe/England", CURRENCY, "British Pound");
|
184 |
1
| cache.put("Europe/France", CURRENCY, "Euro");
|
185 |
1
| cache.put("Europe/Germany", CURRENCY, "Euro");
|
186 |
1
| cache.put("Europe/Italy", CURRENCY, "Euro");
|
187 |
1
| cache.put("Europe/Poland", CURRENCY, "Zloty");
|
188 |
1
| cache.put("Europe/Switzerland", CURRENCY, "Swiss Franc");
|
189 |
| |
190 |
1
| cache.put("Europe/Austria", POPULATION, 8184691);
|
191 |
1
| cache.put("Europe/Czech Republic", POPULATION, 10241138);
|
192 |
1
| cache.put("Europe/England", POPULATION, 60441457);
|
193 |
1
| cache.put("Europe/France", POPULATION, 60656178);
|
194 |
1
| cache.put("Europe/Germany", POPULATION, 82431390);
|
195 |
1
| cache.put("Europe/Italy", POPULATION, 58103033);
|
196 |
1
| cache.put("Europe/Poland", POPULATION, 38635144);
|
197 |
1
| cache.put("Europe/Switzerland", POPULATION, 7489370);
|
198 |
| |
199 |
1
| HashMap albania = new HashMap(4);
|
200 |
1
| albania.put(CAPITAL, "Tirana");
|
201 |
1
| albania.put(CURRENCY, "Lek");
|
202 |
1
| albania.put(POPULATION, 3563112);
|
203 |
1
| albania.put(AREA, 28748);
|
204 |
1
| cache.put("Europe/Albania", albania);
|
205 |
| |
206 |
1
| HashMap hungary = new HashMap(4);
|
207 |
1
| hungary.put(CAPITAL, "Budapest");
|
208 |
1
| hungary.put(CURRENCY, "Forint");
|
209 |
1
| hungary.put(POPULATION, 10006835);
|
210 |
1
| hungary.put(AREA, 93030);
|
211 |
1
| cache.put("Europe/Hungary", hungary);
|
212 |
| |
213 |
1
| HashMap romania = new HashMap(4);
|
214 |
1
| romania.put(CAPITAL, "Bucharest");
|
215 |
1
| romania.put(CURRENCY, "Leu");
|
216 |
1
| romania.put(POPULATION, 22329977);
|
217 |
1
| romania.put(AREA, 237500);
|
218 |
1
| cache.put("Europe/Romania", romania);
|
219 |
| |
220 |
1
| HashMap slovakia = new HashMap(4);
|
221 |
1
| slovakia.put(CAPITAL, "Bratislava");
|
222 |
1
| slovakia.put(CURRENCY, "Slovak Koruna");
|
223 |
1
| slovakia.put(POPULATION, 5431363);
|
224 |
1
| slovakia.put(AREA, 48845);
|
225 |
1
| cache.put("Europe/Slovakia", slovakia);
|
226 |
| |
227 |
| } |
228 |
| |
229 |
1
| private CacheMgmtInterceptor getCacheMgmtInterceptor()
|
230 |
| { |
231 |
1
| List interceptors = cache.getInterceptors();
|
232 |
1
| if (interceptors.isEmpty())
|
233 |
| { |
234 |
0
| return null;
|
235 |
| } |
236 |
| |
237 |
2
| for (int i = 0; i < interceptors.size(); i++)
|
238 |
| { |
239 |
2
| Object o = interceptors.get(i);
|
240 |
2
| if (o instanceof CacheMgmtInterceptor)
|
241 |
| { |
242 |
1
| return (CacheMgmtInterceptor) o;
|
243 |
| } |
244 |
| } |
245 |
0
| return null;
|
246 |
| } |
247 |
| |
248 |
1
| public static Test suite()
|
249 |
| { |
250 |
1
| return new TestSuite(MgmtCoreTest.class);
|
251 |
| } |
252 |
| |
253 |
| } |