Clover coverage report -
Coverage timestamp: Thu Jul 5 2007 20:02:32 EDT
file stats: LOC: 226   Methods: 11
NCLOC: 139   Classes: 2
 
 Source file Conditionals Statements Methods TOTAL
PojoCacheJmxWrapperTest.java 75% 97.4% 100% 96.8%
coverage coverage
 1    /*
 2    * JBoss, Home of Professional Open Source
 3    *
 4    * Distributable under LGPL license.
 5    * See terms of license at gnu.org.
 6    */
 7   
 8    package org.jboss.cache.pojo.jmx;
 9   
 10    import org.jboss.cache.CacheException;
 11    import org.jboss.cache.CacheStatus;
 12    import org.jboss.cache.config.Configuration;
 13    import org.jboss.cache.notifications.annotation.CacheListener;
 14    import org.jboss.cache.notifications.annotation.CacheStarted;
 15    import org.jboss.cache.notifications.annotation.CacheStopped;
 16    import org.jboss.cache.notifications.event.Event;
 17    import org.jboss.cache.pojo.PojoCacheException;
 18   
 19    import javax.management.ObjectName;
 20   
 21    /**
 22    * Tests the PojoCacheJmxWrapper class
 23    *
 24    * @author <a href="mailto:ben.wang@jboss.org">Ben Wang</a>
 25    * @author Brian Stansberry
 26    */
 27    public class PojoCacheJmxWrapperTest extends PojoCacheJmxWrapperTestBase
 28    {
 29   
 30  1 public void testCacheMBeanBinding1() throws Exception
 31    {
 32  1 registerWrapper();
 33  1 assertTrue("Cache Mbean should be registered ", mBeanServer.isRegistered(plainCacheMBeanName));
 34  1 assertTrue("PojoCache Mbean should be registered ", mBeanServer.isRegistered(mBeanName));
 35   
 36  1 unregisterWrapper();
 37  1 assertFalse("Cache Mbean should not be registered ", mBeanServer.isRegistered(plainCacheMBeanName));
 38  1 assertFalse("PojoCache Mbean should not be registered ", mBeanServer.isRegistered(mBeanName));
 39    }
 40   
 41  1 public void testCacheMBeanBinding2() throws Exception
 42    {
 43  1 PojoCacheJmxWrapperMBean wrapper = createWrapper(createConfiguration());
 44  1 wrapper = registerWrapper(wrapper);
 45  1 assertFalse("Cache Mbean should not be registered ", mBeanServer.isRegistered(plainCacheMBeanName));
 46  1 assertTrue("PojoCache Mbean should be registered ", mBeanServer.isRegistered(mBeanName));
 47   
 48  1 wrapper.create();
 49  1 wrapper.start();
 50   
 51  1 assertTrue("Cache Mbean should be registered ", mBeanServer.isRegistered(plainCacheMBeanName));
 52   
 53  1 unregisterWrapper();
 54  1 assertFalse("Cache Mbean should not be registered ", mBeanServer.isRegistered(plainCacheMBeanName));
 55  1 assertFalse("PojoCache Mbean should not be registered ", mBeanServer.isRegistered(mBeanName));
 56    }
 57   
 58  1 public void testConfiguration() throws Exception
 59    {
 60  1 registerWrapper();
 61  1 Configuration cfgFromJmx = (Configuration) mBeanServer.getAttribute(plainCacheMBeanName, "Configuration");
 62  1 assertEquals(cache.getCache().getConfiguration(), cfgFromJmx);
 63    }
 64   
 65  1 public void testGetUnderlyingCacheObjectName() throws Exception
 66    {
 67  1 registerWrapper();
 68  1 String cacheName = (String) mBeanServer.getAttribute(mBeanName, "UnderlyingCacheObjectName");
 69  1 ObjectName tmpName = JmxUtil.getPlainCacheObjectName(mBeanName);
 70  1 assertEquals("Cache object name ", tmpName.getCanonicalName(), cacheName);
 71    }
 72   
 73    /**
 74    * Tests that setting registerPlainCache=false disables interceptor
 75    * registration when the wrapper is registered before create/start
 76    * are called.
 77    *
 78    * @throws Exception
 79    */
 80  1 public void testRegisterPlainCache1() throws Exception
 81    {
 82  1 PojoCacheJmxWrapper wrapper = createWrapper(createConfiguration());
 83  1 wrapper.setRegisterPlainCache(false);
 84   
 85  1 registerWrapper(wrapper);
 86   
 87  1 assertTrue("Should be registered", mBeanServer.isRegistered(mBeanName));
 88  1 assertFalse("Plain cache should not be registered", mBeanServer.isRegistered(plainCacheMBeanName));
 89    }
 90   
 91    /**
 92    * Tests that setting registerPlainCache=false disables interceptor
 93    * registration when the wrapper is registered after create/start
 94    * are called.
 95    *
 96    * @throws Exception
 97    */
 98  1 public void testRegisterPlainCache2() throws Exception
 99    {
 100  1 PojoCacheJmxWrapper wrapper = createWrapper(createConfiguration());
 101  1 wrapper.setRegisterPlainCache(false);
 102  1 wrapper.setRegisterInterceptors(true);
 103   
 104  1 wrapper.create();
 105  1 wrapper.start();
 106   
 107  1 registerWrapper(wrapper);
 108   
 109  1 assertTrue("Should be registered", mBeanServer.isRegistered(mBeanName));
 110  1 assertFalse("Plain cache should not be registered", mBeanServer.isRegistered(plainCacheMBeanName));
 111    }
 112   
 113    // public void testGetInternalLocation() throws Exception
 114    // {
 115    // PojoCacheJmxWrapperMBean wrapper = createWrapper(createConfiguration());
 116    // wrapper = registerWrapper(wrapper);
 117    // wrapper.create();
 118    // wrapper.start();
 119    //
 120    // registerWrapper(wrapper);
 121    // PojoCache pc = wrapper.getPojoCache();
 122    //
 123    // Person joe = new Person();
 124    // joe.setName("Joe");
 125    // joe.setAge(25);
 126    //
 127    // pc.attach("/person/joe", joe);
 128    //
 129    // assertEquals("Correct location", "/person/joe", wrapper.getInternalLocation(joe));
 130    // }
 131   
 132    //
 133   
 134  1 public void testDuplicateInvocation() throws Exception
 135    {
 136  1 PojoCacheJmxWrapperMBean cache = registerWrapper();
 137  1 cache.create();
 138  1 cache.start();
 139  1 cache.create();
 140  1 cache.start();
 141   
 142  1 cache.getPojoCache().attach("/a/b/c", null);
 143   
 144  1 cache.stop();
 145  1 cache.destroy();
 146  1 cache.stop();
 147  1 cache.destroy();
 148    }
 149   
 150  1 public void testFailedStart() throws Exception
 151    {
 152  1 PojoCacheJmxWrapper wrapper = new PojoCacheJmxWrapper(createCache(createConfiguration()));
 153  1 registerWrapper(wrapper);
 154  1 assertEquals("Correct state", CacheStatus.INSTANTIATED, wrapper.getCacheStatus());
 155  1 DisruptLifecycleListener listener = new DisruptLifecycleListener();
 156  1 wrapper.getPojoCache().getCache().addCacheListener(listener);
 157  1 wrapper.create();
 158  1 assertEquals("Correct state", CacheStatus.CREATED, wrapper.getCacheStatus());
 159  1 listener.setDisrupt(true);
 160  1 try
 161    {
 162  1 wrapper.start();
 163  0 fail("Listener did not prevent start");
 164    }
 165    catch (PojoCacheException good)
 166    {
 167    }
 168   
 169  1 assertEquals("Correct state", CacheStatus.FAILED, wrapper.getCacheStatus());
 170   
 171  1 listener.setDisrupt(false);
 172   
 173  1 wrapper.start();
 174   
 175  1 assertEquals("Correct state", CacheStatus.STARTED, wrapper.getCacheStatus());
 176   
 177  1 wrapper.getPojoCache().attach("/a/b/c", null);
 178   
 179  1 listener.setDisrupt(true);
 180    // need to re-add the listener since the failed start would have nullified the notifier.
 181  1 wrapper.getPojoCache().getCache().addCacheListener(listener);
 182   
 183   
 184  1 try
 185    {
 186  1 wrapper.stop();
 187  0 fail("Listener did not prevent stop");
 188    }
 189    catch (CacheException good)
 190    {
 191    }
 192   
 193  1 assertEquals("Correct state", CacheStatus.FAILED, wrapper.getCacheStatus());
 194   
 195  1 listener.setDisrupt(false);
 196   
 197  1 wrapper.stop();
 198  1 assertEquals("Correct state", CacheStatus.STOPPED, wrapper.getCacheStatus());
 199  1 wrapper.destroy();
 200  1 assertEquals("Correct state", CacheStatus.DESTROYED, wrapper.getCacheStatus());
 201    }
 202   
 203   
 204    @CacheListener
 205    public class DisruptLifecycleListener
 206    {
 207    private boolean disrupt;
 208   
 209  1 @CacheStarted
 210    public void cacheStarted(Event e)
 211    {
 212  1 if (disrupt) throw new IllegalStateException("I don't want to start");
 213    }
 214   
 215  2 @CacheStopped
 216    public void cacheStopped(Event e)
 217    {
 218  1 if (disrupt) throw new IllegalStateException("I don't want to stop");
 219    }
 220   
 221  4 public void setDisrupt(boolean disrupt)
 222    {
 223  4 this.disrupt = disrupt;
 224    }
 225    }
 226    }