[jboss-cvs] JBossCache/tests/functional/org/jboss/cache/jmx ...

Brian Stansberry brian.stansberry at jboss.com
Wed May 9 14:34:12 EDT 2007


  User: bstansberry
  Date: 07/05/09 14:34:12

  Added:       tests/functional/org/jboss/cache/jmx    
                        CacheJmxWrapperTestBase.java
                        InterceptorRegistrationTest.java
                        CacheJmxWrapperTest.java
  Removed:     tests/functional/org/jboss/cache/jmx     CacheMBeanTest.java
  Log:
  [JBCACHE-1014] Improve testsuite coverage of JMX configuration
  
  Revision  Changes    Path
  1.1      date: 2007/05/09 18:34:12;  author: bstansberry;  state: Exp;JBossCache/tests/functional/org/jboss/cache/jmx/CacheJmxWrapperTestBase.java
  
  Index: CacheJmxWrapperTestBase.java
  ===================================================================
  package org.jboss.cache.jmx;
  
  import javax.management.MBeanServer;
  import javax.management.MBeanServerFactory;
  import javax.management.MBeanServerInvocationHandler;
  import javax.management.MalformedObjectNameException;
  import javax.management.ObjectName;
  
  import junit.framework.TestCase;
  
  import org.jboss.cache.Cache;
  import org.jboss.cache.DefaultCacheFactory;
  import org.jboss.cache.config.Configuration;
  
  /**
   * Tests the JMX wrapper class around the cache.
   *
   * @author <a href="mailto:manik at jboss.org">Manik Surtani</a>
   * @author Brian Stansberry
   */
  public class CacheJmxWrapperTestBase extends TestCase
  {
     public static final String CLUSTER_NAME = "CacheMBeanTest";
     
     protected Cache cache;
     protected CacheJmxWrapperMBean jmxWrapper;
     protected MBeanServer mBeanServer;
     protected ObjectName mBeanName;
     protected String mBeanNameStr;
  
     protected void setUp() throws Exception
     {
        mBeanServer = MBeanServerFactory.createMBeanServer("CacheMBeanTest");
        
        mBeanNameStr = JmxUtil.PREFIX + CLUSTER_NAME;
        mBeanName = new ObjectName(mBeanNameStr);
     }
  
     protected void tearDown() throws Exception
     {
        try
        {
           cleanup();
        }
        finally
        {
           if (mBeanServer != null)
           {
              MBeanServerFactory.releaseMBeanServer(mBeanServer);
              mBeanServer = null;
           }
        }
     }
     
     protected CacheJmxWrapperMBean registerWrapper() throws Exception
     {
        if (cache == null)
           cache = createCache(createConfiguration());
        return registerWrapper(cache);
     }
     
     protected CacheJmxWrapperMBean registerWrapper(Cache toWrap) throws Exception
     {
        CacheJmxWrapper wrapper = new CacheJmxWrapper(toWrap);
        return registerWrapper(wrapper);
     }
     
     protected CacheJmxWrapperMBean registerWrapper(Configuration config) throws Exception
     {
        CacheJmxWrapper wrapper = new CacheJmxWrapper();
        wrapper.setConfiguration(config);
        return registerWrapper(wrapper);
     }
     
     protected CacheJmxWrapperMBean registerWrapper(CacheJmxWrapperMBean wrapper) throws Exception
     {
        JmxUtil.registerCacheMBean(mBeanServer, wrapper, mBeanNameStr);
        jmxWrapper = (CacheJmxWrapperMBean) MBeanServerInvocationHandler.newProxyInstance(mBeanServer, mBeanName, CacheJmxWrapperMBean.class, false);
        return jmxWrapper;
     }
     
     protected void unregisterWrapper() throws Exception
     {
        mBeanServer.unregisterMBean(mBeanName);
     }
     
     protected CacheJmxWrapper createWrapper(Configuration config)
     {
        CacheJmxWrapper wrapper = new CacheJmxWrapper();
        wrapper.setConfiguration(config);
        return wrapper;
     }
     
     protected Cache createCache(Configuration config)
     {
        cache = DefaultCacheFactory.getInstance().createCache(config, false);
        return cache;
     }
     
     protected Configuration createConfiguration()
     {
        Configuration c = new Configuration();
        c.setClusterName(CLUSTER_NAME);
        c.setExposeManagementStatistics(true);
        c.setCacheMode(Configuration.CacheMode.LOCAL);
        return c;
     }
     
     private void cleanup() throws Exception
     {
        if (cache != null)
        {
           try
           {
              cache.stop();
           }
           catch (Exception ignored) {}
           
           cache = null;
        }
        if (jmxWrapper != null)
        {
           try
           {
              jmxWrapper.stop();
              jmxWrapper.destroy();
           }
           catch (Exception ignored) {}
           
           jmxWrapper = null;
        }
        
        if (mBeanServer != null && mBeanName != null && mBeanServer.isRegistered(mBeanName))
           mBeanServer.unregisterMBean(mBeanName);
     }
  
     protected void interceptorRegistrationTest(boolean expectMbeans) throws MalformedObjectNameException, NullPointerException
     {
        interceptorRegistrationTest(mBeanNameStr, expectMbeans);      
     }
  
     protected void interceptorRegistrationTest(String baseName, boolean expectMbeans) throws MalformedObjectNameException, NullPointerException
     {
        // should be 3 interceptor MBeans loaded:
        ObjectName[] interceptorMBeanNames = {
                new ObjectName(baseName + JmxUtil.MBEAN_KEY + "TxInterceptor"),
                new ObjectName(baseName + JmxUtil.MBEAN_KEY + "CacheMgmtInterceptor"),
                new ObjectName(baseName + JmxUtil.MBEAN_KEY + "InvocationContextInterceptor")
        };
  
        for (ObjectName n : interceptorMBeanNames)
        {
           if (expectMbeans)
              assertTrue(n + " should be registered", mBeanServer.isRegistered(n));
           else
              assertFalse(n + " should not be registered", mBeanServer.isRegistered(n));
        }
     }
  }
  
  
  
  1.1      date: 2007/05/09 18:34:12;  author: bstansberry;  state: Exp;JBossCache/tests/functional/org/jboss/cache/jmx/InterceptorRegistrationTest.java
  
  Index: InterceptorRegistrationTest.java
  ===================================================================
  /*
   * JBoss, Home of Professional Open Source.
   * Copyright 2006, Red Hat Middleware LLC, and individual contributors
   * as indicated by the @author tags. See the copyright.txt file in the
   * distribution for a full listing of individual contributors.
   *
   * This is free software; you can redistribute it and/or modify it
   * under the terms of the GNU Lesser General Public License as
   * published by the Free Software Foundation; either version 2.1 of
   * the License, or (at your option) any later version.
   *
   * This software is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
   * Lesser General Public License for more details.
   *
   * You should have received a copy of the GNU Lesser General Public
   * License along with this software; if not, write to the Free
   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
   */
  
  package org.jboss.cache.jmx;
  
  import org.jboss.cache.config.Configuration;
  
  /**
   * Tests the interceptor registration function of CacheJmxWrapper.
   * 
   * @author <a href="brian.stansberry at jboss.com">Brian Stansberry</a>
   * @version $Revision: 1.1 $
   */
  public class InterceptorRegistrationTest extends CacheJmxWrapperTestBase
  {
  
     /**
      * Confirms interceptor mbeans are registered if the following events
      * occur:
      * 
      * cache.start();
      * wrapper creation and registration.
      * 
      * @throws Exception
      */
     public void testInterceptorMBeans1() throws Exception
     {
        // have to start the cache to have any interceptors
        createCache(createConfiguration());
        cache.start();
        
        CacheJmxWrapperMBean wrapper = registerWrapper(cache);
        assertTrue("Should be registered", mBeanServer.isRegistered(mBeanName));
        
        interceptorRegistrationTest(true);
        
        wrapper.stop();
        wrapper.destroy();
        
        // Should still be registered
        interceptorRegistrationTest(true);
        
        unregisterWrapper();
        
        interceptorRegistrationTest(false);
     }
  
     /**
      * Confirms interceptor mbeans are registered if the following events
      * occur:
      * 
      * cache.start();
      * wrapper creation and and start
      * wrapper registration.
      * 
      * @throws Exception
      */
     public void testInterceptorMBeans2() throws Exception
     {
        // have to start the cache to have any interceptors
        createCache(createConfiguration());
        cache.start();
        
        CacheJmxWrapperMBean wrapper = new CacheJmxWrapper(cache);
        wrapper.start();
        wrapper = registerWrapper(wrapper);
        assertTrue("Should be registered", mBeanServer.isRegistered(mBeanName));
        
        interceptorRegistrationTest(true);
        
        wrapper.stop();
        wrapper.destroy();
        
        // Should still be registered
        interceptorRegistrationTest(true);
        
        unregisterWrapper();
        
        interceptorRegistrationTest(false);
     }
  
     /**
      * Confirms interceptor mbeans are registered if the following events
      * occur:
      * 
      * Cache not injected
      * wrapper registered;
      * wrapper created and started.
      * 
      * @throws Exception
      */
     public void testInterceptorMBeans3() throws Exception
     {
        CacheJmxWrapperMBean wrapper = registerWrapper(createConfiguration());
        assertTrue("Should be registered", mBeanServer.isRegistered(mBeanName));
        
        // have to start the cache to have any interceptors
        wrapper.create();
        wrapper.start();
        
        interceptorRegistrationTest(true);
        
        wrapper.stop();
        wrapper.destroy();
        
        // Destroy should unregister if we are managing
        interceptorRegistrationTest(false);
        
        unregisterWrapper();
        
        interceptorRegistrationTest(false);
     }
  
     /**
      * Confirms interceptor mbeans are registered if the following events
      * occur:
      * 
      * Cache not injected
      * wrapper created and started.
      * wrapper registered
      * 
      * @throws Exception
      */
     public void testInterceptorMBeans4() throws Exception
     {
        CacheJmxWrapper wrapper = createWrapper(createConfiguration());
        
        // have to start the cache to have any interceptors
        wrapper.create();
        wrapper.start();
        
        registerWrapper(wrapper);
        
        assertTrue("Should be registered", mBeanServer.isRegistered(mBeanName));
        
        interceptorRegistrationTest(true);
        
        wrapper.stop();
        wrapper.destroy();
        
        // Destroy should unregister if we are managing
        interceptorRegistrationTest(false);
        
        unregisterWrapper();
        
        interceptorRegistrationTest(false);
     }
  
     /**
      * Confirms interceptor mbeans are registered if the following events
      * occur:
      * 
      * cache constructed;
      * wrapper constructed and registered with manageCacheLifecycle=true
      * wrapper created and started
      * 
      * @throws Exception
      */
     public void testInterceptorMBeans5() throws Exception
     {
        CacheJmxWrapperMBean wrapper = registerWrapper();
        wrapper.setManageCacheLifecycle(true);
        assertTrue("Should be registered", mBeanServer.isRegistered(mBeanName));
        
        // have to start the cache to have any interceptors
        wrapper.create();
        wrapper.start();
        
        interceptorRegistrationTest(true);
        
        wrapper.stop();
        wrapper.destroy();
        
        // Destroy should unregister if we are managing
        interceptorRegistrationTest(false);
        
        unregisterWrapper();
        
        interceptorRegistrationTest(false);
     }
  
     /**
      * Confirms interceptor mbeans are NOT registered if the following events
      * occur:
      * 
      * cache constructed;
      * wrapper constructed and registered
      * wrapper created and started
      * 
      * @throws Exception
      */
     public void testInterceptorMBeans6() throws Exception
     {
        CacheJmxWrapperMBean wrapper = registerWrapper();
        assertTrue("Should be registered", mBeanServer.isRegistered(mBeanName));
        
        // have to start the cache to have any interceptors
        wrapper.create();
        wrapper.start();
        
        interceptorRegistrationTest(false);
        
        wrapper.stop();
        wrapper.destroy();
        
        interceptorRegistrationTest(false);
        
        unregisterWrapper();
        
        interceptorRegistrationTest(false);
     }
  
     /**
      * Confirms interceptor mbeans are NOT registered if the following events
      * occur:
      * 
      * cache constructed;
      * wrapper created and started
      * wrapper registered
      * 
      * @throws Exception
      */
     public void testInterceptorMBeans7() throws Exception
     {
        CacheJmxWrapperMBean wrapper = new CacheJmxWrapper(createCache(createConfiguration()));
        
        // have to start the cache to have any interceptors
        wrapper.create();
        wrapper.start();
        
        wrapper = registerWrapper(wrapper);
        assertTrue("Should be registered", mBeanServer.isRegistered(mBeanName));
        
        interceptorRegistrationTest(false);
        
        wrapper.stop();
        wrapper.destroy();
        
        interceptorRegistrationTest(false);
        
        unregisterWrapper();
        
        interceptorRegistrationTest(false);
     }
  
     /**
      * Confirms interceptor mbeans are NOT registered if the following events
      * occur:
      * 
      * wrapper created from config
      * wrapper.managerCacheLifecycle=false
      * wrapper created and started
      * wrapper registered
      * 
      * @throws Exception
      */
     public void testInterceptorMBeans8() throws Exception
     {
        CacheJmxWrapperMBean wrapper = new CacheJmxWrapper(createCache(createConfiguration()));
        wrapper.setManageCacheLifecycle(false);
        
        // have to start the cache to have any interceptors
        wrapper.create();
        wrapper.start();
        
        wrapper = registerWrapper(wrapper);
        assertTrue("Should be registered", mBeanServer.isRegistered(mBeanName));
        
        interceptorRegistrationTest(false);
        
        wrapper.stop();
        wrapper.destroy();
        
        interceptorRegistrationTest(false);
        
        unregisterWrapper();
        
        interceptorRegistrationTest(false);
     }
  
     /**
      * Confirms interceptor mbeans are NOT registered if the following events
      * occur:
      * 
      * wrapper created from config
      * wrapper.managerCacheLifecycle=false
      * wrapper registered
      * wrapper created and started
      * 
      * @throws Exception
      */
     public void testInterceptorMBeans9() throws Exception
     {
        CacheJmxWrapperMBean wrapper = new CacheJmxWrapper(createCache(createConfiguration()));
        wrapper.setManageCacheLifecycle(false);
        
        wrapper = registerWrapper(wrapper);
        
        assertTrue("Should be registered", mBeanServer.isRegistered(mBeanName));
        
        // have to start the cache to have any interceptors
        wrapper.create();
        wrapper.start();
        
        interceptorRegistrationTest(false);
        
        wrapper.stop();
        wrapper.destroy();
        
        interceptorRegistrationTest(false);
        
        unregisterWrapper();
        
        interceptorRegistrationTest(false);
     }
  
     /**
      * Tests that setting registerInterceptors=false disables interceptor 
      * registration when the wrapper is registered before create/start 
      * are called.
      * 
      * @throws Exception
      */
     public void testRegisterInterceptors1() throws Exception
     {
        CacheJmxWrapper wrapper = createWrapper(createConfiguration());
        wrapper.setRegisterInterceptors(false);
        
        registerWrapper(wrapper);
        
        assertTrue("Should be registered", mBeanServer.isRegistered(mBeanName));
        
        wrapper.create();
        wrapper.start();
        
        interceptorRegistrationTest(false);
        
        wrapper.stop();
        wrapper.destroy();
        
        interceptorRegistrationTest(false);
        
        unregisterWrapper();
        
        interceptorRegistrationTest(false);
     }
  
     /**
      * Tests that setting registerInterceptors=false disables interceptor 
      * registration when the wrapper is registered after create/start 
      * are called.
      * 
      * @throws Exception
      */
     public void testRegisterInterceptors2() throws Exception
     {
        CacheJmxWrapper wrapper = createWrapper(createConfiguration());
        wrapper.setRegisterInterceptors(false);
        
        wrapper.create();
        wrapper.start();
        
        registerWrapper(wrapper);
        
        assertTrue("Should be registered", mBeanServer.isRegistered(mBeanName));
        
        interceptorRegistrationTest(false);
        
        wrapper.stop();
        wrapper.destroy();
        
        interceptorRegistrationTest(false);
        
        unregisterWrapper();
        
        interceptorRegistrationTest(false);
     }
  
     public void testExposeManagementStatistics1() throws Exception
     {
        Configuration cfg = createConfiguration();
        cfg.setExposeManagementStatistics(false);
        
        CacheJmxWrapper wrapper = createWrapper(cfg);
        registerWrapper(cfg);
        
        assertTrue("Should be registered", mBeanServer.isRegistered(mBeanName));
        
        wrapper.create();
        wrapper.start();
        
        interceptorRegistrationTest(false);
        
        wrapper.stop();
        wrapper.destroy();
        
        interceptorRegistrationTest(false);
        
        unregisterWrapper();
        
        interceptorRegistrationTest(false);
     }
  
     public void testExposeManagementStatistics2() throws Exception
     {
        Configuration cfg = createConfiguration();
        cfg.setExposeManagementStatistics(false);
        
        CacheJmxWrapper wrapper = createWrapper(cfg);
        
        wrapper.create();
        wrapper.start();
        
        registerWrapper(wrapper);
        
        assertTrue("Should be registered", mBeanServer.isRegistered(mBeanName));
        
        interceptorRegistrationTest(false);
        
        wrapper.stop();
        wrapper.destroy();
        
        interceptorRegistrationTest(false);
        
        unregisterWrapper();
        
        interceptorRegistrationTest(false);
        
     }
  
     
  
  }
  
  
  
  1.1      date: 2007/05/09 18:34:12;  author: bstansberry;  state: Exp;JBossCache/tests/functional/org/jboss/cache/jmx/CacheJmxWrapperTest.java
  
  Index: CacheJmxWrapperTest.java
  ===================================================================
  package org.jboss.cache.jmx;
  
  import javax.management.ObjectName;
  import javax.transaction.Transaction;
  import javax.transaction.TransactionManager;
  
  import org.jboss.cache.Cache;
  import org.jboss.cache.Fqn;
  import org.jboss.cache.config.Configuration;
  import org.jboss.cache.transaction.DummyTransactionManagerLookup;
  
  /**
   * Tests the JMX wrapper class around the cache.
   *
   * @author <a href="mailto:manik at jboss.org">Manik Surtani</a>
   * @author Brian Stansberry
   */
  public class CacheJmxWrapperTest extends CacheJmxWrapperTestBase
  {
     public void testCacheMBeanBinding() throws Exception
     {
        registerWrapper();
        assertTrue("Should be registered", mBeanServer.isRegistered(mBeanName));
     }
  
     public void testSetCacheObjectName() throws Exception
     {
        ObjectName on = new ObjectName("jboss.cache:test=SetCacheObjectName");
        String str = on.getCanonicalName();
        boolean registered = false;
        try
        {
           CacheJmxWrapper wrapper = createWrapper(createConfiguration());
           wrapper.setCacheObjectName(str);
           
           // Register under the standard name
           registerWrapper(wrapper);
           // Should be registered under 'on'
           registered = mBeanServer.isRegistered(on);
           
           assertTrue("Registered with configured name", registered);
           assertEquals("Configured name retained", str, wrapper.getCacheObjectName());
           
           wrapper.create();
           wrapper.start();
           
           interceptorRegistrationTest(str, true);
           
           wrapper.stop();
           wrapper.destroy();
           
           interceptorRegistrationTest(false);
        }
        finally
        {
           if (registered)
              mBeanServer.unregisterMBean(on);
        }
     }
     
     public void testGetCacheObjectName() throws Exception
     {
        ObjectName on = new ObjectName("jboss.cache:test=SetCacheObjectName");
        String str = on.getCanonicalName();
        CacheJmxWrapper wrapper = createWrapper(createConfiguration());
        wrapper.setCacheObjectName(str);
        
        assertEquals("Setter and getter match", str, wrapper.getCacheObjectName());
        
        // Go back to the default
        wrapper.setCacheObjectName(null);
        assertEquals("Got default ObjectName", JmxUtil.PREFIX + CLUSTER_NAME, wrapper.getCacheObjectName());
        
        registerWrapper(wrapper);
        assertEquals("Returns standard name", mBeanName, new ObjectName(wrapper.getCacheObjectName()));
     }
     
     public void testSetObjectNameViaConfiguration() throws Exception
     {
        ObjectName on = new ObjectName("jboss.cache:test=SetCacheObjectName");
        Configuration cfg = createConfiguration();
        cfg.setServiceName(on.getCanonicalName());
        CacheJmxWrapper wrapper = createWrapper(cfg);
        mBeanServer.registerMBean(wrapper, null);
        
        assertEquals("Wrapper shows correct ObjectName", on, new ObjectName(wrapper.getCacheObjectName()));
        
        assertTrue("Wrapper registered under correct ObjectName", mBeanServer.isRegistered(on));
     }
  
     public void testGetConfiguration1() throws Exception
     {
        CacheJmxWrapperMBean wrapper = registerWrapper();
        Configuration cfgFromJmx = wrapper.getConfiguration();
        assertNotNull("Got a configuration", cfgFromJmx);
        assertSame(cache.getConfiguration(), cfgFromJmx);
     }
  
     public void testGetConfiguration2() throws Exception
     {
        Configuration cfg = createConfiguration();
        CacheJmxWrapperMBean wrapper  = registerWrapper(cfg);
        Configuration cfgFromJmx = wrapper.getConfiguration();
        assertNotNull("Got a configuration", cfgFromJmx);
        assertSame(cfg, cfgFromJmx);
     }
  
     /**
      * Note that this is a bit of a 'white box' test as it assumes that the 
      * returned String equals Configuration.toString(). That could change and 
      * break this test; if it does, and there's nothing wrong with the
      * change, just modify the test.
      * 
      * @throws Exception
      */
     public void testGetConfigurationAsString1() throws Exception
     {
        CacheJmxWrapperMBean wrapper = registerWrapper();
        String cfgFromJmx = wrapper.getConfigurationAsString();
        assertEquals(cache.getConfiguration().toString(), cfgFromJmx);
     }
  
     /**
      * Note that this is a bit of a 'white box' test as it assumes that the 
      * returned String equals Configuration.toString(). That could change and 
      * break this test; if it does, and there's nothing wrong with the
      * change, just modify the test.
      * 
      * @throws Exception
      */
     public void testGetConfigurationAsString2() throws Exception
     {
        Configuration cfg = createConfiguration();
        CacheJmxWrapperMBean wrapper  = registerWrapper(cfg);
        wrapper.create();
        wrapper.start();
        String cfgFromJmx = wrapper.getConfigurationAsString();
        assertEquals(wrapper.getCache().getConfiguration().toString(), cfgFromJmx);
     }
  
     /**
      * Note that this is a bit of a 'white box' test as it checks
      * the currently coded HTML format and assumes that the HTML content is
      * derived from Configuration.toString(). That could change and break
      * this test; if it does, and there's nothing wrong with the
      * change, just modify the test.
      * 
      * @throws Exception
      */
     public void testGetConfigurationAsHtml1() throws Exception
     {
        CacheJmxWrapperMBean wrapper = registerWrapper();
        String cfgFromJmx = wrapper.getConfigurationAsHtmlString();
        assertEquals(CacheJmxWrapper.formatHtml(cache.getConfiguration().toString()), cfgFromJmx);
        checkHtml(cfgFromJmx, false);
     }
  
     /**
      * Note that this is a bit of a 'white box' test as it checks
      * the currently coded HTML format and assumes that the HTML content is
      * derived from Configuration.toString(). That could change and break
      * this test; if it does, and there's nothing wrong with the
      * change, just modify the test.
      * 
      * @throws Exception
      */
     public void testGetConfigurationAsHtml2() throws Exception
     {
        Configuration cfg = createConfiguration();
        CacheJmxWrapperMBean wrapper  = registerWrapper(cfg);
        wrapper.create();
        wrapper.start();
        String cfgFromJmx = wrapper.getConfigurationAsHtmlString();
        assertEquals(CacheJmxWrapper.formatHtml(wrapper.getCache().getConfiguration().toString()), cfgFromJmx);
        checkHtml(cfgFromJmx, false);
     }
  
     public void testGetCache() throws Exception
     {
        registerWrapper();
        // have to start the cache before we'll have a root
        cache.start();
        
        Cache cacheJmx = (Cache) mBeanServer.getAttribute(mBeanName, "Cache");
        cacheJmx.getRoot().put("key", "value");
  
        assertEquals("value", cache.getRoot().get("key"));
  
        Fqn fqn = Fqn.fromString("/testing/jmx");
        cache.put(fqn, "key", "value");
  
        assertEquals("value", cacheJmx.get(fqn, "key"));
     }
  
     public void testGetCacheDetails() throws Exception
     {
        getCacheDetailsTest(false);
     }
  
     /**
      * Note that this is a bit of a 'white box' test as it checks
      * the currently coded HTML format. That could change and break
      * this test; if it does, and there's nothing wrong with the
      * change, just modify the test.
      * 
      * @throws Exception
      */
     public void testGetCacheDetailsAsHtml() throws Exception
     {
        String html = getCacheDetailsTest(true);
        checkHtml(html, true);
     }
     
     public void testGetLockInfo() throws Exception
     {
        getLockInfoTest(false);
     }
     
     /**
      * Note that this is a bit of a 'white box' test as it checks
      * the currently coded HTML format. That could change and break
      * this test; if it does, and there's nothing wrong with the
      * change, just modify the test.
      * 
      * @throws Exception
      */
     public void testGetLockInfoAsHtml() throws Exception
     {
        getLockInfoTest(true);
     }
  
     private String getCacheDetailsTest(boolean html) throws Exception
     {
        CacheJmxWrapperMBean wrapper = registerWrapper();
        
        // have to start the cache before we'll have a root
        cache.start();
        Fqn fqn = Fqn.fromString("/testing/jmx");
        cache.put(fqn, "foobar", "barfoo");
  
        assertEquals("barfoo", cache.get(fqn, "foobar"));
        
        String details = html ? wrapper.getCacheDetailsAsHtml() : wrapper.getCacheDetails();
        
        assertTrue("Details include testing", details.indexOf("testing") > -1);
        assertTrue("Details include jmx", details.indexOf("jmx") > -1);
        assertTrue("Details include foobar", details.indexOf("foobar") > -1);
        assertTrue("Details include barfoo", details.indexOf("barfoo") > -1);
        
        return details;
     }
     
     private String getLockInfoTest(boolean html) throws Exception
     {
        Configuration config = createConfiguration();
        config.setTransactionManagerLookupClass(DummyTransactionManagerLookup.class.getName());
        Cache c = createCache(config);
        CacheJmxWrapperMBean wrapper = registerWrapper(c);
        
        wrapper.setManageCacheLifecycle(true);
        wrapper.create();
        wrapper.start();
        
        TransactionManager tm = config.getRuntimeConfig().getTransactionManager();
        
        tm.begin();
        Transaction tx = tm.getTransaction();
        try
        {
           Fqn fqn = Fqn.fromString("/testing/jmx");
           cache.put(fqn, "foobar", "barfoo");
           
           String locks = html ? wrapper.getLockInfoAsHtml() : wrapper.getLockInfo();
           
           assertTrue("Details include testing", locks.indexOf("testing") > -1);
           assertTrue("Details include jmx", locks.indexOf("jmx") > -1);
           
           return locks;
        }
        catch (Exception e)
        {
           tx.setRollbackOnly();
           throw e;
        }
        finally
        {
           tx.commit();
        }
        
     }
     
     private void checkHtml(String html, boolean checkBR)
     {
        if (checkBR)
           assertTrue("Has <br", html.indexOf("<br") > -1);
        
        assertTrue("No tabs", html.indexOf('\t') == -1);
        
        assertTrue("No spaces", html.indexOf(' ') == -1);
        
     }
  }
  
  
  



More information about the jboss-cvs-commits mailing list