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

Brian Stansberry brian.stansberry at jboss.com
Thu May 10 00:00:24 EDT 2007


  User: bstansberry
  Date: 07/05/10 00:00:24

  Added:       tests/functional/org/jboss/cache/jmx   NotificationTest.java
                        OptimisticNotificationTest.java
  Log:
  [JBCACHE-855] CacheMgmtInterceptor no longer handles JMX notifications; CacheJmxWrapper does
  
  Revision  Changes    Path
  1.1      date: 2007/05/10 04:00:24;  author: bstansberry;  state: Exp;JBossCache/tests/functional/org/jboss/cache/jmx/NotificationTest.java
  
  Index: NotificationTest.java
  ===================================================================
  package org.jboss.cache.jmx;
  
  import java.util.EnumSet;
  import java.util.HashMap;
  
  import javax.management.MBeanServer;
  import javax.management.MBeanServerFactory;
  import javax.management.Notification;
  import javax.management.NotificationListener;
  import javax.management.ObjectName;
  
  import junit.framework.TestCase;
  
  import org.jboss.cache.CacheImpl;
  import org.jboss.cache.DefaultCacheFactory;
  import org.jboss.cache.Fqn;
  import org.jboss.cache.config.CacheLoaderConfig;
  import org.jboss.cache.config.Configuration;
  import org.jboss.cache.config.Configuration.CacheMode;
  import org.jboss.cache.factories.UnitTestCacheConfigurationFactory;
  import org.jboss.cache.factories.XmlConfigurationParser;
  import org.jboss.cache.loader.CacheLoader;
  import org.jboss.cache.xml.XmlHelper;
  import org.w3c.dom.Element;
  
  /**
   * Functional tests for CacheJmxWrapper broadcast of cache event notifications
   *
   * @author Jerry Gauthier
   * @version $Id: NotificationTest.java,v 1.1 2007/05/10 04:00:24 bstansberry Exp $
   */
  public class NotificationTest extends TestCase
  {
     private static final String CLUSTER_NAME = "NotificationTestCluster";
  
     private static final String CAPITAL = "capital";
     private static final String CURRENCY = "currency";
     private static final String POPULATION = "population";
     private static final String EUROPE_NODE = "Europe";
  
     public enum Type
     {
        STARTED, STOPPED, PRECREATE, POSTCREATE, PREEVICT, POSTEVICT,
        PRELOAD, POSTLOAD, PREREMOVE, POSTREMOVE, PREVISIT, POSTVISIT,
        PREMODIFY, POSTMODIFY, PREACTIVATE, POSTACTIVATE, PREPASSIVATE,
        POSTPASSIVATE, VIEWCHANGE      
     }
  
     private MBeanServer m_server;
     private EnumSet<Type> events = EnumSet.noneOf(Type.class);
  
     CacheImpl cache = null;
     boolean optimistic = false;
  
     protected void setUp() throws Exception
     {
        super.setUp();
        m_server = MBeanServerFactory.createMBeanServer();
        cache = createCache(CLUSTER_NAME);
        // bind manually for now.
        ObjectName mgmt = getWrapperObjectName();
        CacheJmxWrapper cacheMBean = new CacheJmxWrapper(cache);
  
        m_server.registerMBean(cacheMBean, mgmt);
     }
  
     protected void tearDown() throws Exception
     {
        try
        {
           super.tearDown();
           
           cleanup();         
        }
        finally
        {
           // make sure we stop the mbean server
           if (m_server != null)
              MBeanServerFactory.releaseMBeanServer(m_server);
        }
     }
  
     private void cleanup() throws Exception
     {
        events.clear();
        
        if (cache != null)
        {
           // stop the cache before the listener is unregistered
           //cache1.stop();
           cache.destroy();
           cache = null;
        }
        
        if (m_server != null)
        {
           ObjectName mgmt = getWrapperObjectName();
           if (m_server.isRegistered(mgmt))
              m_server.unregisterMBean(mgmt);
        }
     }
     
     private ObjectName getWrapperObjectName() throws Exception
     {
        return new ObjectName(JmxUtil.PREFIX + CLUSTER_NAME);
     }
  
     public void testNotifications() throws Exception
     {
        assertNotNull("MBeanServer is null.", m_server);
        assertNotNull("Cache is null.", cache);
  
        MyListener listener = new MyListener();
  
        ObjectName mgmt = getWrapperObjectName();
        m_server.addNotificationListener(mgmt, listener, null, null);
  
        // start the cache after registering listener - this will trigger CacheStarted
        // since cache is defined with cluster, thiswill also trigger ViewChange
        cache.start();
  
        // add a node - this will trigger NodeCreated, NodeModify(pre/post) and NodeModified
        HashMap albania = new HashMap(4);
        albania.put(CAPITAL, "Tirana");
        albania.put(CURRENCY, "Lek");
        cache.put("Europe/Albania", albania);
  
        // modify a node - this will trigger NodeModified and NodeModify(pre/post)
        cache.put("Europe/Albania", POPULATION, 3563112);
  
        // retrieve an attribute - this will trigger NodeVisited
        Fqn key = Fqn.fromString("Europe/Albania");
        assertNotNull("Retrieval error: expected to retrieve " + CURRENCY + " for " + key, cache.get(key, CURRENCY));
  
        // evict the node - this will trigger NodePassivate, NodeEvicted and NodeEvict(pre/post)
        cache.evict(key);
  
        // retrieve the attribute again - this will trigger NodeVisited and NodeActivate
        assertNotNull("Retrieval error: expected to retrieve " + CURRENCY + " for " + key, cache.get(key, CURRENCY));
  
        // remove the node - this will trigger NodeRemoved and NodeRemove(pre/post)
        cache.remove(key);
  
        // clean up before stopping  the cache
        CacheLoader cl = cache.getCacheLoader();
        cl.remove(Fqn.fromString(EUROPE_NODE));
  
        // stop the cache
        cache.stop();
        m_server.removeNotificationListener(mgmt, listener);
  
        // run the tests
        assertTrue("Expected CacheStarted notification", events.contains(Type.STARTED));
        assertTrue("Expected CacheStopped notification", events.contains(Type.STOPPED));
        assertTrue("Expected NodeCreated notification", events.contains(Type.PRECREATE));
        assertTrue("Expected NodeCreated notification", events.contains(Type.POSTCREATE));
        assertTrue("Expected NodeEvicted notification", events.contains(Type.PREEVICT));
        assertTrue("Expected NodeEvicted notification", events.contains(Type.POSTEVICT));
        assertTrue("Expected NodeLoaded notification", events.contains(Type.PRELOAD));
        assertTrue("Expected NodeLoaded notification", events.contains(Type.POSTLOAD));
        assertTrue("Expected NodeVisited notification", events.contains(Type.PREVISIT));
        assertTrue("Expected NodeVisited notification", events.contains(Type.POSTVISIT));
        assertTrue("Expected NodeActivated notification", events.contains(Type.PREACTIVATE));
        assertTrue("Expected NodeActivated notification", events.contains(Type.POSTACTIVATE));
        assertTrue("Expected NodeModified notification", events.contains(Type.PREMODIFY));
        assertTrue("Expected NodeModified notification", events.contains(Type.POSTMODIFY));
        assertTrue("Expected NodePassivated notification", events.contains(Type.PREPASSIVATE));
        assertTrue("Expected NodePassivated notification", events.contains(Type.POSTPASSIVATE));
        assertTrue("Expected NodeRemoved notification", events.contains(Type.PREREMOVE));
        assertTrue("Expected NodeRemoved notification", events.contains(Type.POSTREMOVE));
        assertTrue("Expected ViewChange notification", events.contains(Type.VIEWCHANGE));
     }
     
     public void testEarlyRegistration() throws Exception
     {
        // undo setup
        cleanup();
        
        CacheJmxWrapper wrapper = new CacheJmxWrapper();
        ObjectName mgmt = getWrapperObjectName();
        m_server.registerMBean(wrapper, mgmt);
        MyListener listener = new MyListener();
        m_server.addNotificationListener(mgmt, listener, null, null);
        
        cache = createCache(CLUSTER_NAME);
        wrapper.setCache(cache);
        cache.start();
        try
        {
           assertTrue("Expected CacheStarted notification", events.contains(Type.STARTED));
        }
        finally
        {
           cache.stop();
        }
     }
  
     public void testLateRegistration() throws Exception
     {
        assertNotNull("MBeanServer is null.", m_server);
        assertNotNull("Cache is null.", cache);
  
        // start the cache before registering listener
        cache.start();
  
        try
        {
           MyListener listener = new MyListener();
     
           ObjectName mgmt = getWrapperObjectName();
           m_server.addNotificationListener(mgmt, listener, null, null);
     
           // add a node - this will trigger NodeCreated, NodeModify(pre/post) and NodeModified
           HashMap albania = new HashMap(4);
           albania.put(CAPITAL, "Tirana");
           albania.put(CURRENCY, "Lek");
           cache.put("Europe/Albania", albania);
        
           // run the tests
           assertTrue("Expected NodeModified notification", events.contains(Type.PREMODIFY));
           assertTrue("Expected NodeModified notification", events.contains(Type.POSTMODIFY));
        }
        finally
        {
           cache.stop();
        }
     }
     
     public void testListenerRemoval() throws Exception
     {
        assertNotNull("MBeanServer is null.", m_server);
        assertNotNull("Cache is null.", cache);
  
        MyListener listener = new MyListener();
  
        ObjectName mgmt = getWrapperObjectName();
        m_server.addNotificationListener(mgmt, listener, null, null);
  
        // start the cache after registering listener - this will trigger CacheStarted
        // since cache is defined with cluster, thiswill also trigger ViewChange
        cache.start();
        boolean ok = false;
        try
        {
           assertTrue("Expected CacheStarted notification", events.contains(Type.STARTED));
           
           m_server.removeNotificationListener(mgmt, listener);
           ok = true;
        }
        finally
        {
           cache.stop();
           if (ok)
              assertFalse("Expected no CacheStopped notification", events.contains(Type.STOPPED));
        }
     }
  
     private CacheImpl createCache(String clusterName) throws Exception
     {
        CacheImpl cache = (CacheImpl) DefaultCacheFactory.getInstance().createCache(false);
        cache.setConfiguration(UnitTestCacheConfigurationFactory.createConfiguration(CacheMode.REPL_SYNC));
        cache.getConfiguration().setCacheMode(Configuration.CacheMode.REPL_SYNC);
        cache.getConfiguration().setCacheLoaderConfig(getCacheLoaderConfig("location=" + getTempDir()));
        cache.getConfiguration().setExposeManagementStatistics(true);
        cache.getConfiguration().setClusterName(clusterName);
        if (optimistic)
        {
           cache.getConfiguration().setTransactionManagerLookupClass("org.jboss.cache.transaction.DummyTransactionManagerLookup");
           cache.getConfiguration().setNodeLockingScheme("OPTIMISTIC");
        }
        cache.create();
        // start the cache after the listener has been registered
        //cache.start();
        return cache;
     }
  
     private String getTempDir()
     {
        return System.getProperty("java.io.tempdir", "/tmp");
     }
  
     private boolean getPre(Object data)
     {
        assertNotNull("User data is null, should be Object[]", data);
        assertTrue("User data is " + data.getClass().getName() + ", should be Object[]", data instanceof Object[]);
  
        Object[] parms = (Object[]) data;
        assertTrue("Parameter is " + parms[1].getClass().getName() + ", should be Boolean", parms[1] instanceof Boolean);
        return (Boolean) parms[1];
     }
  
     private CacheLoaderConfig getCacheLoaderConfig(String properties) throws Exception
     {
        String xml = "<config>\n" +
                "<passivation>true</passivation>\n" +
                "<preload></preload>\n" +
                "<shared>true</shared>\n" +
                "<cacheloader>\n" +
                "<class>org.jboss.cache.loader.FileCacheLoader</class>\n" +
                "<properties>" + properties + "</properties>\n" +
                "<async>false</async>\n" +
                "<fetchPersistentState>false</fetchPersistentState>\n" +
                "<ignoreModifications>false</ignoreModifications>\n" +
                "</cacheloader>\n" +
                "</config>";
        Element element = XmlHelper.stringToElement(xml);
        return XmlConfigurationParser.parseCacheLoaderConfig(element);
     }
  
     private class MyListener implements NotificationListener
     {
        public void handleNotification(Notification notification, Object handback)
        {
           String type = notification.getType();
           Object userData = notification.getUserData();
  
           if (type.equals(CacheNotificationBroadcaster.NOTIF_CACHE_STARTED))
           {
              events.add(Type.STARTED);
           }
           else if (type.equals(CacheNotificationBroadcaster.NOTIF_CACHE_STOPPED))
           {
              events.add(Type.STOPPED);
           }
           else if (type.equals(CacheNotificationBroadcaster.NOTIF_NODE_CREATED))
           {
              if (getPre(userData))
              {
                 events.add(Type.PRECREATE);
              }
              else
              {
                 events.add(Type.POSTCREATE);
              }
           }
           else if (type.equals(CacheNotificationBroadcaster.NOTIF_NODE_EVICTED))
           {
              if (getPre(userData))
              {
                 events.add(Type.PREEVICT);
              }
              else
              {
                 events.add(Type.POSTEVICT);
              }
           }
           else if (type.equals(CacheNotificationBroadcaster.NOTIF_NODE_LOADED))
           {
              if (getPre(userData))
              {
                 events.add(Type.PRELOAD);
              }
              else
              {
                 events.add(Type.POSTLOAD);
              }
           }
           else if (type.equals(CacheNotificationBroadcaster.NOTIF_NODE_REMOVED))
           {
              if (getPre(userData))
              {
                 events.add(Type.PREREMOVE);
              }
              else
              {
                 events.add(Type.POSTREMOVE);
              }
           }
           else if (type.equals(CacheNotificationBroadcaster.NOTIF_NODE_VISITED))
           {
              if (getPre(userData))
              {
                 events.add(Type.PREVISIT);
              }
              else
              {
                 events.add(Type.POSTVISIT);
              }
           }
           else if (type.equals(CacheNotificationBroadcaster.NOTIF_VIEW_CHANGED))
           {
              events.add(Type.VIEWCHANGE);
           }
           else if (type.equals(CacheNotificationBroadcaster.NOTIF_NODE_ACTIVATED))
           {
              if (getPre(userData))
              {
                 events.add(Type.PREACTIVATE);
              }
              else
              {
                 events.add(Type.POSTACTIVATE);
              }
           }
           else if (type.equals(CacheNotificationBroadcaster.NOTIF_NODE_MODIFIED))
           {
              if (getPre(userData))
              {
                 events.add(Type.PREMODIFY);
              }
              else
              {
                 events.add(Type.POSTMODIFY);
              }
           }
           else if (type.equals(CacheNotificationBroadcaster.NOTIF_NODE_PASSIVATED))
           {
              if (getPre(userData))
              {
                 events.add(Type.PREPASSIVATE);
              }
              else
              {
                 events.add(Type.POSTPASSIVATE);
              }
           }
        }
     }
  
  }
  
  
  
  1.1      date: 2007/05/10 04:00:24;  author: bstansberry;  state: Exp;JBossCache/tests/functional/org/jboss/cache/jmx/OptimisticNotificationTest.java
  
  Index: OptimisticNotificationTest.java
  ===================================================================
  /*
   * JBoss, Home of Professional Open Source
   *
   * Distributable under LGPL license.
   * See terms of license at gnu.org.
   */
  package org.jboss.cache.jmx;
  
  
  /**
   * @author <a href="mailto:manik at jboss.org">Manik Surtani (manik at jboss.org)</a>
   */
  public class OptimisticNotificationTest extends NotificationTest
  {
     public OptimisticNotificationTest()
     {
        optimistic = true;
     }
  }
  
  
  



More information about the jboss-cvs-commits mailing list