Clover coverage report -
Coverage timestamp: Thu Jul 5 2007 20:02:32 EDT
file stats: LOC: 168   Methods: 13
NCLOC: 140   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
PojoCacheJmxWrapperTestBase.java 75% 100% 100% 95.9%
coverage coverage
 1    package org.jboss.cache.pojo.jmx;
 2   
 3    import junit.framework.TestCase;
 4    import org.jboss.cache.config.Configuration;
 5    import org.jboss.cache.pojo.PojoCache;
 6    import org.jboss.cache.pojo.PojoCacheFactory;
 7   
 8    import javax.management.MBeanServer;
 9    import javax.management.MBeanServerFactory;
 10    import javax.management.MBeanServerInvocationHandler;
 11    import javax.management.MalformedObjectNameException;
 12    import javax.management.ObjectName;
 13   
 14    /**
 15    * Tests the JMX wrapper class around a PojoCache.
 16    *
 17    * @author Brian Stansberry
 18    */
 19    public class PojoCacheJmxWrapperTestBase extends TestCase
 20    {
 21    public static final String PC_PREFIX = JmxUtil.POJO_CACHE_DOMAIN + ":" +
 22    JmxUtil.SERVICE_KEY_NAME + "=PojoCacheJmxWrapperTest," +
 23    JmxUtil.CLUSTER_KEY + "=";
 24    public static final String CLUSTER_NAME = "PojoCacheMBeanTest";
 25   
 26    protected PojoCache cache;
 27    protected PojoCacheJmxWrapperMBean jmxWrapper;
 28    protected MBeanServer mBeanServer;
 29    protected ObjectName mBeanName;
 30    protected String mBeanNameStr;
 31    protected ObjectName plainCacheMBeanName;
 32    protected String plainCacheMBeanNameStr;
 33   
 34  24 protected void setUp() throws Exception
 35    {
 36  24 mBeanServer = MBeanServerFactory.createMBeanServer("PojoCacheMBeanTest");
 37   
 38  24 mBeanName = new ObjectName(PC_PREFIX + CLUSTER_NAME);
 39  24 mBeanNameStr = mBeanName.getCanonicalName();
 40  24 plainCacheMBeanName = JmxUtil.getPlainCacheObjectName(mBeanName);
 41  24 plainCacheMBeanNameStr = plainCacheMBeanName.getCanonicalName();
 42    }
 43   
 44  24 protected void tearDown() throws Exception
 45    {
 46  24 try
 47    {
 48  24 cleanup();
 49    }
 50    finally
 51    {
 52  24 if (mBeanServer != null)
 53    {
 54  24 MBeanServerFactory.releaseMBeanServer(mBeanServer);
 55  24 mBeanServer = null;
 56    }
 57    }
 58    }
 59   
 60  5 protected PojoCacheJmxWrapperMBean registerWrapper() throws Exception
 61    {
 62  5 if (cache == null)
 63  5 cache = createCache(createConfiguration());
 64  5 return registerWrapper(cache);
 65    }
 66   
 67  6 protected PojoCacheJmxWrapperMBean registerWrapper(PojoCache toWrap) throws Exception
 68    {
 69  6 PojoCacheJmxWrapper wrapper = new PojoCacheJmxWrapper(toWrap);
 70  6 return registerWrapper(wrapper);
 71    }
 72   
 73  2 protected PojoCacheJmxWrapperMBean registerWrapper(Configuration config) throws Exception
 74    {
 75  2 PojoCacheJmxWrapper wrapper = new PojoCacheJmxWrapper();
 76  2 wrapper.setConfiguration(config);
 77  2 return registerWrapper(wrapper);
 78    }
 79   
 80  24 protected PojoCacheJmxWrapperMBean registerWrapper(PojoCacheJmxWrapperMBean wrapper) throws Exception
 81    {
 82  24 JmxUtil.registerPojoCache(mBeanServer, wrapper, mBeanNameStr);
 83  24 jmxWrapper = (PojoCacheJmxWrapperMBean) MBeanServerInvocationHandler.newProxyInstance(mBeanServer, mBeanName, PojoCacheJmxWrapperMBean.class, false);
 84  24 return jmxWrapper;
 85    }
 86   
 87  16 protected void unregisterWrapper() throws Exception
 88    {
 89  16 mBeanServer.unregisterMBean(mBeanName);
 90    }
 91   
 92  11 protected PojoCacheJmxWrapper createWrapper(Configuration config)
 93    {
 94  11 PojoCacheJmxWrapper wrapper = new PojoCacheJmxWrapper();
 95  11 wrapper.setConfiguration(config);
 96  11 return wrapper;
 97    }
 98   
 99  10 protected PojoCache createCache(Configuration config)
 100    {
 101  10 cache = PojoCacheFactory.createCache(config, false);
 102  10 return cache;
 103    }
 104   
 105  22 protected Configuration createConfiguration()
 106    {
 107  22 Configuration c = new Configuration();
 108  22 c.setClusterName(CLUSTER_NAME);
 109  22 c.setExposeManagementStatistics(true);
 110  22 c.setCacheMode(Configuration.CacheMode.LOCAL);
 111  22 return c;
 112    }
 113   
 114  24 private void cleanup() throws Exception
 115    {
 116  24 if (cache != null)
 117    {
 118  10 try
 119    {
 120  10 cache.stop();
 121    }
 122    catch (Exception ignored)
 123    {
 124    }
 125   
 126  10 cache = null;
 127    }
 128  24 if (jmxWrapper != null)
 129    {
 130  24 try
 131    {
 132  24 jmxWrapper.stop();
 133  8 jmxWrapper.destroy();
 134    }
 135    catch (Exception ignored)
 136    {
 137    }
 138   
 139  24 jmxWrapper = null;
 140    }
 141   
 142  24 if (mBeanServer != null && mBeanName != null && mBeanServer.isRegistered(mBeanName))
 143  8 mBeanServer.unregisterMBean(mBeanName);
 144    }
 145   
 146  39 protected void interceptorRegistrationTest(boolean expectMbeans) throws MalformedObjectNameException, NullPointerException
 147    {
 148  39 interceptorRegistrationTest(plainCacheMBeanNameStr, expectMbeans);
 149    }
 150   
 151  39 protected void interceptorRegistrationTest(String baseName, boolean expectMbeans) throws MalformedObjectNameException, NullPointerException
 152    {
 153    // should be 3 interceptor MBeans loaded:
 154  39 ObjectName[] interceptorMBeanNames = {
 155    new ObjectName(baseName + JmxUtil.INTERCEPTOR_KEY + "TxInterceptor"),
 156    new ObjectName(baseName + JmxUtil.INTERCEPTOR_KEY + "CacheMgmtInterceptor"),
 157    new ObjectName(baseName + JmxUtil.INTERCEPTOR_KEY + "InvocationContextInterceptor")
 158    };
 159   
 160  39 for (ObjectName n : interceptorMBeanNames)
 161    {
 162  117 if (expectMbeans)
 163  24 assertTrue(n + " should be registered", mBeanServer.isRegistered(n));
 164    else
 165  93 assertFalse(n + " should not be registered", mBeanServer.isRegistered(n));
 166    }
 167    }
 168    }