KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > jmx > CacheMBeanTest


1 package org.jboss.cache.jmx;
2
3 import junit.framework.TestCase;
4 import org.jboss.cache.Cache;
5 import org.jboss.cache.CacheSPI;
6 import org.jboss.cache.Fqn;
7 import org.jboss.cache.config.Configuration;
8 import org.jboss.cache.factories.DefaultCacheFactory;
9
10 import javax.management.MBeanServer JavaDoc;
11 import javax.management.MBeanServerFactory JavaDoc;
12 import javax.management.ObjectName JavaDoc;
13
14 /**
15  * Tests the cache as an MBean
16  *
17  * @author <a HREF="mailto:manik@jboss.org">Manik Surtani</a>
18  */

19 public class CacheMBeanTest extends TestCase
20 {
21    private Cache cache;
22    private MBeanServer JavaDoc mBeanServer;
23    private ObjectName JavaDoc mBeanName;
24    private String JavaDoc mBeanNameStr;
25
26    protected void setUp() throws Exception JavaDoc
27    {
28       mBeanServer = MBeanServerFactory.createMBeanServer("CacheMBeanTest");
29
30       Configuration c = new Configuration();
31       c.setClusterName("CacheMBeanTest");
32       c.setExposeManagementStatistics(true);
33       c.setCacheMode(Configuration.CacheMode.LOCAL);
34       cache = DefaultCacheFactory.getInstance().createCache(c);
35       mBeanNameStr = JmxUtil.PREFIX + cache.getConfiguration().getClusterName();
36       mBeanName = new ObjectName JavaDoc(mBeanNameStr);
37
38       JmxUtil.registerCacheMBean(mBeanServer, new CacheJmxWrapper(cache), mBeanNameStr);
39       JmxUtil.registerInterceptors(mBeanServer, ((CacheSPI) cache).getInterceptorChain(), mBeanNameStr);
40    }
41
42    protected void tearDown()
43    {
44       if (cache != null)
45       {
46          cache.stop();
47          cache = null;
48       }
49
50       if (mBeanServer != null)
51       {
52          MBeanServerFactory.releaseMBeanServer(mBeanServer);
53          mBeanServer = null;
54       }
55    }
56
57    public void testCacheMBeanBinding() throws Exception JavaDoc
58    {
59       assertTrue("Should be registered", mBeanServer.isRegistered(mBeanName));
60    }
61
62    public void testInterceptorMBeans() throws Exception JavaDoc
63    {
64       assertTrue("Should be registered", mBeanServer.isRegistered(mBeanName));
65
66       // should be 3 interceptor MBeans loaded:
67
ObjectName JavaDoc[] interceptorMBeanNames = {
68               new ObjectName JavaDoc(mBeanNameStr + JmxUtil.MBEAN_KEY + "TxInterceptor"),
69               new ObjectName JavaDoc(mBeanNameStr + JmxUtil.MBEAN_KEY + "CacheMgmtInterceptor"),
70               new ObjectName JavaDoc(mBeanNameStr + JmxUtil.MBEAN_KEY + "InvocationContextInterceptor")
71       };
72
73       // TestingUtil.sleepThread(600000);
74

75       for (ObjectName JavaDoc n : interceptorMBeanNames)
76       {
77          assertTrue(n + " should be registered", mBeanServer.isRegistered(n));
78       }
79    }
80
81    public void testConfiguration() throws Exception JavaDoc
82    {
83       Configuration cfgFromJmx = (Configuration) mBeanServer.getAttribute(mBeanName, "Configuration");
84       assertEquals(cache.getConfiguration(), cfgFromJmx);
85    }
86
87    public void testCacheOperations() throws Exception JavaDoc
88    {
89       Cache cacheJmx = (Cache) mBeanServer.getAttribute(mBeanName, "Cache");
90       cacheJmx.getRoot().put("key", "value");
91
92       assertEquals("value", cache.getRoot().get("key"));
93
94       Fqn fqn = Fqn.fromString("//testing/jmx");
95       cache.put(fqn, "key", "value");
96
97       assertEquals("value", cacheJmx.get(fqn, "key"));
98    }
99 }
100
Popular Tags