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 ; 11 import javax.management.MBeanServerFactory ; 12 import javax.management.ObjectName ; 13 14 19 public class CacheMBeanTest extends TestCase 20 { 21 private Cache cache; 22 private MBeanServer mBeanServer; 23 private ObjectName mBeanName; 24 private String mBeanNameStr; 25 26 protected void setUp() throws Exception 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 (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 58 { 59 assertTrue("Should be registered", mBeanServer.isRegistered(mBeanName)); 60 } 61 62 public void testInterceptorMBeans() throws Exception 63 { 64 assertTrue("Should be registered", mBeanServer.isRegistered(mBeanName)); 65 66 ObjectName [] interceptorMBeanNames = { 68 new ObjectName (mBeanNameStr + JmxUtil.MBEAN_KEY + "TxInterceptor"), 69 new ObjectName (mBeanNameStr + JmxUtil.MBEAN_KEY + "CacheMgmtInterceptor"), 70 new ObjectName (mBeanNameStr + JmxUtil.MBEAN_KEY + "InvocationContextInterceptor") 71 }; 72 73 75 for (ObjectName n : interceptorMBeanNames) 76 { 77 assertTrue(n + " should be registered", mBeanServer.isRegistered(n)); 78 } 79 } 80 81 public void testConfiguration() throws Exception 82 { 83 Configuration cfgFromJmx = (Configuration) mBeanServer.getAttribute(mBeanName, "Configuration"); 84 assertEquals(cache.getConfiguration(), cfgFromJmx); 85 } 86 87 public void testCacheOperations() throws Exception 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 |