1 |
| package org.jboss.cache.jmx; |
2 |
| |
3 |
| import org.jboss.cache.Cache; |
4 |
| import org.jboss.cache.CacheException; |
5 |
| import org.jboss.cache.CacheStatus; |
6 |
| import org.jboss.cache.Fqn; |
7 |
| import org.jboss.cache.config.Configuration; |
8 |
| import org.jboss.cache.config.Configuration.CacheMode; |
9 |
| import org.jboss.cache.misc.TestingUtil; |
10 |
| import org.jboss.cache.notifications.annotation.CacheListener; |
11 |
| import org.jboss.cache.notifications.annotation.CacheStarted; |
12 |
| import org.jboss.cache.notifications.annotation.CacheStopped; |
13 |
| import org.jboss.cache.notifications.event.Event; |
14 |
| import org.jboss.cache.transaction.DummyTransactionManagerLookup; |
15 |
| import org.jgroups.Address; |
16 |
| import org.jgroups.stack.IpAddress; |
17 |
| |
18 |
| import javax.management.ObjectName; |
19 |
| import javax.transaction.Transaction; |
20 |
| import javax.transaction.TransactionManager; |
21 |
| import java.util.List; |
22 |
| |
23 |
| |
24 |
| |
25 |
| |
26 |
| |
27 |
| |
28 |
| |
29 |
| public class CacheJmxWrapperTest extends CacheJmxWrapperTestBase |
30 |
| { |
31 |
1
| public void testCacheMBeanBinding() throws Exception
|
32 |
| { |
33 |
1
| registerWrapper();
|
34 |
1
| assertTrue("Should be registered", mBeanServer.isRegistered(mBeanName));
|
35 |
| } |
36 |
| |
37 |
1
| public void testSetCacheObjectName() throws Exception
|
38 |
| { |
39 |
1
| ObjectName on = new ObjectName("jboss.cache:test=SetCacheObjectName");
|
40 |
1
| String str = on.getCanonicalName();
|
41 |
1
| boolean registered = false;
|
42 |
1
| try
|
43 |
| { |
44 |
1
| CacheJmxWrapper wrapper = createWrapper(createConfiguration());
|
45 |
1
| wrapper.setCacheObjectName(str);
|
46 |
| |
47 |
| |
48 |
1
| registerWrapper(wrapper);
|
49 |
| |
50 |
1
| registered = mBeanServer.isRegistered(on);
|
51 |
| |
52 |
1
| assertTrue("Registered with configured name", registered);
|
53 |
1
| assertEquals("Configured name retained", str, wrapper.getCacheObjectName());
|
54 |
| |
55 |
1
| wrapper.create();
|
56 |
1
| wrapper.start();
|
57 |
| |
58 |
1
| interceptorRegistrationTest(str, true);
|
59 |
| |
60 |
1
| wrapper.stop();
|
61 |
1
| wrapper.destroy();
|
62 |
| |
63 |
1
| interceptorRegistrationTest(false);
|
64 |
| } |
65 |
| finally |
66 |
| { |
67 |
1
| if (registered)
|
68 |
1
| mBeanServer.unregisterMBean(on);
|
69 |
| } |
70 |
| } |
71 |
| |
72 |
1
| public void testGetCacheObjectName() throws Exception
|
73 |
| { |
74 |
1
| ObjectName on = new ObjectName("jboss.cache:test=SetCacheObjectName");
|
75 |
1
| String str = on.getCanonicalName();
|
76 |
1
| CacheJmxWrapper wrapper = createWrapper(createConfiguration());
|
77 |
1
| wrapper.setCacheObjectName(str);
|
78 |
| |
79 |
1
| assertEquals("Setter and getter match", str, wrapper.getCacheObjectName());
|
80 |
| |
81 |
| |
82 |
1
| wrapper.setCacheObjectName(null);
|
83 |
1
| assertEquals("Got default ObjectName", JmxUtil.PREFIX + CLUSTER_NAME, wrapper.getCacheObjectName());
|
84 |
| |
85 |
1
| registerWrapper(wrapper);
|
86 |
1
| assertEquals("Returns standard name", mBeanName, new ObjectName(wrapper.getCacheObjectName()));
|
87 |
| } |
88 |
| |
89 |
1
| public void testGetConfiguration1() throws Exception
|
90 |
| { |
91 |
1
| CacheJmxWrapperMBean wrapper = registerWrapper();
|
92 |
1
| Configuration cfgFromJmx = wrapper.getConfiguration();
|
93 |
1
| assertNotNull("Got a configuration", cfgFromJmx);
|
94 |
1
| assertSame(cache.getConfiguration(), cfgFromJmx);
|
95 |
| } |
96 |
| |
97 |
1
| public void testGetConfiguration2() throws Exception
|
98 |
| { |
99 |
1
| Configuration cfg = createConfiguration();
|
100 |
1
| CacheJmxWrapperMBean wrapper = registerWrapper(cfg);
|
101 |
1
| Configuration cfgFromJmx = wrapper.getConfiguration();
|
102 |
1
| assertNotNull("Got a configuration", cfgFromJmx);
|
103 |
1
| assertSame(cfg, cfgFromJmx);
|
104 |
| } |
105 |
| |
106 |
| |
107 |
| |
108 |
| |
109 |
| |
110 |
| |
111 |
| |
112 |
| |
113 |
| |
114 |
1
| public void testGetConfigurationAsString1() throws Exception
|
115 |
| { |
116 |
1
| CacheJmxWrapperMBean wrapper = registerWrapper();
|
117 |
1
| String cfgFromJmx = wrapper.getConfigurationAsString();
|
118 |
1
| assertEquals(cache.getConfiguration().toString(), cfgFromJmx);
|
119 |
| } |
120 |
| |
121 |
| |
122 |
| |
123 |
| |
124 |
| |
125 |
| |
126 |
| |
127 |
| |
128 |
| |
129 |
1
| public void testGetConfigurationAsString2() throws Exception
|
130 |
| { |
131 |
1
| Configuration cfg = createConfiguration();
|
132 |
1
| CacheJmxWrapperMBean wrapper = registerWrapper(cfg);
|
133 |
1
| wrapper.create();
|
134 |
1
| wrapper.start();
|
135 |
1
| String cfgFromJmx = wrapper.getConfigurationAsString();
|
136 |
1
| assertEquals(wrapper.getCache().getConfiguration().toString(), cfgFromJmx);
|
137 |
| } |
138 |
| |
139 |
| |
140 |
| |
141 |
| |
142 |
| |
143 |
| |
144 |
| |
145 |
| |
146 |
| |
147 |
| |
148 |
1
| public void testGetConfigurationAsHtml1() throws Exception
|
149 |
| { |
150 |
1
| CacheJmxWrapperMBean wrapper = registerWrapper();
|
151 |
1
| String cfgFromJmx = wrapper.getConfigurationAsHtmlString();
|
152 |
1
| assertEquals(CacheJmxWrapper.formatHtml(cache.getConfiguration().toString()), cfgFromJmx);
|
153 |
1
| checkHtml(cfgFromJmx, false);
|
154 |
| } |
155 |
| |
156 |
| |
157 |
| |
158 |
| |
159 |
| |
160 |
| |
161 |
| |
162 |
| |
163 |
| |
164 |
| |
165 |
1
| public void testGetConfigurationAsHtml2() throws Exception
|
166 |
| { |
167 |
1
| Configuration cfg = createConfiguration();
|
168 |
1
| CacheJmxWrapperMBean wrapper = registerWrapper(cfg);
|
169 |
1
| wrapper.create();
|
170 |
1
| wrapper.start();
|
171 |
1
| String cfgFromJmx = wrapper.getConfigurationAsHtmlString();
|
172 |
1
| assertEquals(CacheJmxWrapper.formatHtml(wrapper.getCache().getConfiguration().toString()), cfgFromJmx);
|
173 |
1
| checkHtml(cfgFromJmx, false);
|
174 |
| } |
175 |
| |
176 |
1
| public void testGetCache() throws Exception
|
177 |
| { |
178 |
1
| registerWrapper();
|
179 |
| |
180 |
1
| cache.start();
|
181 |
| |
182 |
1
| Cache cacheJmx = (Cache) mBeanServer.getAttribute(mBeanName, "Cache");
|
183 |
1
| cacheJmx.getRoot().put("key", "value");
|
184 |
| |
185 |
1
| assertEquals("value", cache.getRoot().get("key"));
|
186 |
| |
187 |
1
| Fqn fqn = Fqn.fromString("/testing/jmx");
|
188 |
1
| cache.put(fqn, "key", "value");
|
189 |
| |
190 |
1
| assertEquals("value", cacheJmx.get(fqn, "key"));
|
191 |
| } |
192 |
| |
193 |
1
| public void testGetCacheDetails() throws Exception
|
194 |
| { |
195 |
1
| getCacheDetailsTest(false);
|
196 |
| } |
197 |
| |
198 |
| |
199 |
| |
200 |
| |
201 |
| |
202 |
| |
203 |
| |
204 |
| |
205 |
| |
206 |
1
| public void testGetCacheDetailsAsHtml() throws Exception
|
207 |
| { |
208 |
1
| String html = getCacheDetailsTest(true);
|
209 |
1
| checkHtml(html, true);
|
210 |
| } |
211 |
| |
212 |
1
| public void testGetLockInfo() throws Exception
|
213 |
| { |
214 |
1
| getLockInfoTest(false);
|
215 |
| } |
216 |
| |
217 |
| |
218 |
| |
219 |
| |
220 |
| |
221 |
| |
222 |
| |
223 |
| |
224 |
| |
225 |
1
| public void testGetLockInfoAsHtml() throws Exception
|
226 |
| { |
227 |
1
| getLockInfoTest(true);
|
228 |
| } |
229 |
| |
230 |
1
| public void testGetLocalAddress() throws Exception
|
231 |
| { |
232 |
1
| Configuration c = createConfiguration();
|
233 |
1
| c.setCacheMode(CacheMode.REPL_ASYNC);
|
234 |
1
| CacheJmxWrapperMBean wrapper = registerWrapper(c);
|
235 |
1
| wrapper.start();
|
236 |
1
| assertTrue("Got an IpAddress", wrapper.getLocalAddress() instanceof IpAddress);
|
237 |
| } |
238 |
| |
239 |
1
| public void testGetMembers() throws Exception
|
240 |
| { |
241 |
1
| Configuration c = createConfiguration();
|
242 |
1
| c.setCacheMode(CacheMode.REPL_ASYNC);
|
243 |
1
| CacheJmxWrapperMBean wrapper = registerWrapper(c);
|
244 |
1
| wrapper.start();
|
245 |
| |
246 |
1
| c = createConfiguration();
|
247 |
1
| c.setCacheMode(CacheMode.REPL_ASYNC);
|
248 |
1
| Cache cache2 = null;
|
249 |
1
| try
|
250 |
| { |
251 |
1
| cache2 = createCache(c);
|
252 |
1
| cache2.start();
|
253 |
1
| Cache[] caches = new Cache[]{wrapper.getCache(), cache2};
|
254 |
1
| TestingUtil.blockUntilViewsReceived(caches, 5000);
|
255 |
| |
256 |
1
| Address addr = wrapper.getLocalAddress();
|
257 |
1
| assertNotNull("Got an Address", addr);
|
258 |
1
| List members = wrapper.getMembers();
|
259 |
1
| assertNotNull("Got members", addr);
|
260 |
1
| assertEquals("Got correct number of members", 2, members.size());
|
261 |
1
| assertTrue("I am a member", members.contains(addr));
|
262 |
| } |
263 |
| finally |
264 |
| { |
265 |
1
| if (cache2 != null)
|
266 |
| { |
267 |
1
| cache2.destroy();
|
268 |
| } |
269 |
| } |
270 |
| } |
271 |
| |
272 |
1
| public void testDuplicateInvocation() throws Exception
|
273 |
| { |
274 |
1
| CacheJmxWrapperMBean cache = registerWrapper();
|
275 |
1
| cache.create();
|
276 |
1
| cache.start();
|
277 |
1
| cache.create();
|
278 |
1
| cache.start();
|
279 |
| |
280 |
1
| cache.getCache().put(Fqn.fromString("/a/b/c"), null);
|
281 |
1
| assertTrue(cache.getNumberOfNodes() > 0);
|
282 |
1
| assertEquals(0, cache.getNumberOfAttributes());
|
283 |
| |
284 |
1
| System.out.println("cache locks before restart:\n" + cache.getLockInfo());
|
285 |
1
| cache.destroy();
|
286 |
1
| cache.start();
|
287 |
1
| System.out.println("cache locks after restart:\n" + cache.getLockInfo());
|
288 |
| |
289 |
1
| assertEquals(0, cache.getNumberOfNodes());
|
290 |
1
| assertEquals(0, cache.getNumberOfAttributes());
|
291 |
| |
292 |
1
| cache.stop();
|
293 |
1
| cache.destroy();
|
294 |
1
| cache.stop();
|
295 |
1
| cache.destroy();
|
296 |
| } |
297 |
| |
298 |
1
| public void testFailedStart() throws Exception
|
299 |
| { |
300 |
1
| CacheJmxWrapper wrapper = new CacheJmxWrapper(createCache(createConfiguration()));
|
301 |
1
| registerWrapper(wrapper);
|
302 |
1
| assertEquals("Correct state", CacheStatus.INSTANTIATED, wrapper.getCacheStatus());
|
303 |
1
| wrapper.create();
|
304 |
| |
305 |
1
| DisruptLifecycleListener listener = new DisruptLifecycleListener();
|
306 |
1
| listener.setDisrupt(true);
|
307 |
1
| wrapper.getCache().addCacheListener(listener);
|
308 |
| |
309 |
1
| assertEquals("Correct state", CacheStatus.CREATED, wrapper.getCacheStatus());
|
310 |
1
| try
|
311 |
| { |
312 |
1
| wrapper.start();
|
313 |
0
| fail("Listener did not prevent start");
|
314 |
| } |
315 |
| catch (CacheException good) |
316 |
| { |
317 |
| } |
318 |
| |
319 |
1
| assertEquals("Correct state", CacheStatus.FAILED, wrapper.getCacheStatus());
|
320 |
| |
321 |
1
| listener.setDisrupt(false);
|
322 |
| |
323 |
1
| wrapper.start();
|
324 |
| |
325 |
1
| assertEquals("Correct state", CacheStatus.STARTED, wrapper.getCacheStatus());
|
326 |
| |
327 |
1
| wrapper.getCache().put(Fqn.fromString("/a/b/c"), null);
|
328 |
1
| assertTrue(wrapper.getNumberOfNodes() > 0);
|
329 |
1
| assertEquals(0, wrapper.getNumberOfAttributes());
|
330 |
| |
331 |
1
| listener.setDisrupt(true);
|
332 |
| |
333 |
1
| cache.addCacheListener(listener);
|
334 |
| |
335 |
1
| try
|
336 |
| { |
337 |
1
| wrapper.stop();
|
338 |
0
| fail("Listener did not prevent stop");
|
339 |
| } |
340 |
| catch (CacheException good) |
341 |
| { |
342 |
| } |
343 |
| |
344 |
1
| assertEquals("Correct state", CacheStatus.FAILED, wrapper.getCacheStatus());
|
345 |
| |
346 |
1
| listener.setDisrupt(false);
|
347 |
| |
348 |
1
| wrapper.stop();
|
349 |
1
| assertEquals("Correct state", CacheStatus.STOPPED, wrapper.getCacheStatus());
|
350 |
1
| wrapper.destroy();
|
351 |
1
| assertEquals("Correct state", CacheStatus.DESTROYED, wrapper.getCacheStatus());
|
352 |
| } |
353 |
| |
354 |
2
| private String getCacheDetailsTest(boolean html) throws Exception
|
355 |
| { |
356 |
2
| CacheJmxWrapperMBean wrapper = registerWrapper();
|
357 |
| |
358 |
| |
359 |
2
| cache.start();
|
360 |
2
| Fqn fqn = Fqn.fromString("/testing/jmx");
|
361 |
2
| cache.put(fqn, "foobar", "barfoo");
|
362 |
| |
363 |
2
| assertEquals("barfoo", cache.get(fqn, "foobar"));
|
364 |
| |
365 |
2
| String details = html ? wrapper.getCacheDetailsAsHtml() : wrapper.getCacheDetails();
|
366 |
| |
367 |
2
| assertTrue("Details include testing", details.contains("testing"));
|
368 |
2
| assertTrue("Details include jmx", details.contains("jmx"));
|
369 |
2
| assertTrue("Details include foobar", details.contains("foobar"));
|
370 |
2
| assertTrue("Details include barfoo", details.contains("barfoo"));
|
371 |
| |
372 |
2
| return details;
|
373 |
| } |
374 |
| |
375 |
2
| private String getLockInfoTest(boolean html) throws Exception
|
376 |
| { |
377 |
2
| Configuration config = createConfiguration();
|
378 |
2
| config.setTransactionManagerLookupClass(DummyTransactionManagerLookup.class.getName());
|
379 |
2
| Cache c = createCache(config);
|
380 |
2
| CacheJmxWrapperMBean wrapper = registerWrapper(c);
|
381 |
| |
382 |
| |
383 |
2
| wrapper.create();
|
384 |
2
| wrapper.start();
|
385 |
| |
386 |
2
| TransactionManager tm = config.getRuntimeConfig().getTransactionManager();
|
387 |
| |
388 |
2
| tm.begin();
|
389 |
2
| Transaction tx = tm.getTransaction();
|
390 |
2
| try
|
391 |
| { |
392 |
2
| Fqn fqn = Fqn.fromString("/testing/jmx");
|
393 |
2
| cache.put(fqn, "foobar", "barfoo");
|
394 |
| |
395 |
2
| String locks = html ? wrapper.getLockInfoAsHtml() : wrapper.getLockInfo();
|
396 |
| |
397 |
2
| assertTrue("Details include testing", locks.contains("testing"));
|
398 |
2
| assertTrue("Details include jmx", locks.contains("jmx"));
|
399 |
| |
400 |
2
| return locks;
|
401 |
| } |
402 |
| catch (Exception e) |
403 |
| { |
404 |
0
| tx.setRollbackOnly();
|
405 |
0
| throw e;
|
406 |
| } |
407 |
| finally |
408 |
| { |
409 |
2
| tx.commit();
|
410 |
| } |
411 |
| |
412 |
| } |
413 |
| |
414 |
3
| private void checkHtml(String html, boolean checkBR)
|
415 |
| { |
416 |
3
| if (checkBR)
|
417 |
1
| assertTrue("Has <br", html.contains("<br"));
|
418 |
| |
419 |
3
| assertTrue("No tabs", html.indexOf('\t') == -1);
|
420 |
| |
421 |
3
| assertTrue("No spaces", html.indexOf(' ') == -1);
|
422 |
| |
423 |
| } |
424 |
| |
425 |
| @CacheListener |
426 |
| public class DisruptLifecycleListener |
427 |
| { |
428 |
| private boolean disrupt; |
429 |
| |
430 |
1
| @CacheStarted
|
431 |
| public void cacheStarted(Event e) |
432 |
| { |
433 |
1
| if (disrupt) throw new IllegalStateException("I don't want to start");
|
434 |
| } |
435 |
| |
436 |
2
| @CacheStopped
|
437 |
| public void cacheStopped(Event e) |
438 |
| { |
439 |
1
| if (disrupt) throw new IllegalStateException("I don't want to stop");
|
440 |
| } |
441 |
| |
442 |
4
| public void setDisrupt(boolean disrupt)
|
443 |
| { |
444 |
4
| this.disrupt = disrupt;
|
445 |
| } |
446 |
| } |
447 |
| } |