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

Manik Surtani manik at jboss.org
Mon Jul 16 22:03:26 EDT 2007


  User: msurtani
  Date: 07/07/16 22:03:26

  Modified:    tests/functional/org/jboss/cache/mgmt    
                        CacheLoaderTest.java InvalidationTest.java
                        PassivationTest.java
  Added:       tests/functional/org/jboss/cache/mgmt     MgmtTestBase.java
  Log:
  refactored tests
  
  Revision  Changes    Path
  1.14      +25 -192   JBossCache/tests/functional/org/jboss/cache/mgmt/CacheLoaderTest.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: CacheLoaderTest.java
  ===================================================================
  RCS file: /cvsroot/jboss/JBossCache/tests/functional/org/jboss/cache/mgmt/CacheLoaderTest.java,v
  retrieving revision 1.13
  retrieving revision 1.14
  diff -u -b -r1.13 -r1.14
  --- CacheLoaderTest.java	17 Jul 2007 00:00:39 -0000	1.13
  +++ CacheLoaderTest.java	17 Jul 2007 02:03:25 -0000	1.14
  @@ -1,95 +1,43 @@
   package org.jboss.cache.mgmt;
   
  -import junit.framework.Test;
  -import junit.framework.TestCase;
  -import junit.framework.TestSuite;
  -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.factories.XmlConfigurationParser;
   import org.jboss.cache.interceptors.CacheLoaderInterceptor;
   import org.jboss.cache.interceptors.CacheStoreInterceptor;
  -import org.jboss.cache.loader.CacheLoader;
  -import org.jboss.cache.loader.DummyInMemoryCacheLoader;
  -import org.jboss.cache.xml.XmlHelper;
  -import org.w3c.dom.Element;
  +import org.jboss.cache.misc.TestingUtil;
   
   import java.util.HashMap;
  -import java.util.List;
   
   /**
    * Simple functional tests for CacheLoaderInterceptor and CacheStoreInterceptor statistics
    *
    * @author Jerry Gauthier
  - * @version $Id: CacheLoaderTest.java,v 1.13 2007/07/17 00:00:39 msurtani Exp $
  + * @version $Id: CacheLoaderTest.java,v 1.14 2007/07/17 02:03:25 msurtani Exp $
    */
  -public class CacheLoaderTest extends TestCase
  +public class CacheLoaderTest extends MgmtTestBase
   {
  -   private static final String CAPITAL = "capital";
  -   private static final String CURRENCY = "currency";
  -   private static final String POPULATION = "population";
  -   private static final String AREA = "area";
  -   private static final String EUROPE_NODE = "Europe";
  -
  -   CacheImpl cache = null;
  -
  -   protected void setUp() throws Exception
  -   {
  -      super.setUp();
  -      cache = createCache();
  -   }
  -
  -   protected void tearDown() throws Exception
  -   {
  -      super.tearDown();
  -      if (cache != null)
  -      {
  -         CacheLoader cl = cache.getCacheLoader();
  -         cl.remove(Fqn.fromString(EUROPE_NODE));
  -         cache.stop();
  -         cache.destroy();
  -         cache = null;
  -      }
  -   }
  -
      public void testCacheLoaderMgmt() throws Exception
      {
         assertNotNull("Cache is null.", cache);
   
  -//      loader.resetStatistics();
  -//      store.resetStatistics();
  -
  -      // note -it's essential to flush any existing test data from the cache loader
  -      // otherwise the loads and misses counts will be incorrect
  -      CacheLoader cl = cache.getCacheLoader();
  -      assertNotNull("CacheLoader is null.", cl);
  -      cl.remove(Fqn.fromString(EUROPE_NODE));
  -
  -      // populate cache with test data
  -      loadCache(cache);
  -
         // Note: because these tests are normally executed without a server, the interceptor
         // MBeans are usually not available for use in the tests.  Consequently it's necessary
         // to obtain references to the interceptors and work with them directly.
  -      CacheLoaderInterceptor loader = getCacheLoaderInterceptor(cache);
  +      CacheLoaderInterceptor loader = TestingUtil.findInterceptor(cache, CacheLoaderInterceptor.class);
         assertNotNull("CacheLoaderInterceptor not found.", loader);
  -      CacheStoreInterceptor store = getCacheStoreInterceptor(cache);
  +      CacheStoreInterceptor store = TestingUtil.findInterceptor(cache, CacheStoreInterceptor.class);
         assertNotNull("CacheStoreInterceptor not found.", store);
   
         // verify cache loader statistics for entries loaded into cache
         int miss = 0;
  -      int load = 2;
  -      int stores = 11;
  +      int load = 0;
  +      int stores = 5;
         assertEquals("CacheLoaderLoads count error: ", load, loader.getCacheLoaderLoads());
         assertEquals("CacheLoaderMisses count error: ", miss, loader.getCacheLoaderMisses());
         assertEquals("CacheLoaderStores count error: ", stores, store.getCacheLoaderStores());
   
         // now try retrieving a valid attribute and an invalid attribute
  -      Fqn key = Fqn.fromString("Europe/Austria");
  -      assertNotNull("Retrieval error: expected to retrieve " + CAPITAL + " for " + key, cache.get(key, CAPITAL));
  -      assertNull("Retrieval error: did not expect to retrieve " + AREA + " for " + key, cache.get(key, AREA));
  +      assertNotNull("Retrieval error: expected to retrieve " + CAPITAL + " for " + AUSTRIA, cache.get(AUSTRIA, CAPITAL));
  +      assertNull("Retrieval error: did not expect to retrieve " + AREA + " for " + AUSTRIA, cache.get(AUSTRIA, AREA));
  +      load++; // since we did a get on a non-existent key which triggered a load      
   
         // verify statistics after retrieving entries - misses should still be same since nodes were already loaded
         assertEquals("CacheLoaderLoads count error: ", load, loader.getCacheLoaderLoads());
  @@ -97,8 +45,7 @@
         assertEquals("CacheLoaderStores count error: ", stores, store.getCacheLoaderStores());
   
         // now try retrieving an attribute for an invalid node
  -      key = Fqn.fromString("Europe/Poland");
  -      assertNull("Retrieval error: did not expect to retrieve " + CAPITAL + " for " + key, cache.get(key, CAPITAL));
  +      assertNull("Retrieval error: did not expect to retrieve " + CAPITAL + " for " + POLAND, cache.get(POLAND, CAPITAL));
   
         // verify statistics for after retrieving entries - misses should now be +1 after attempt to load Poland
         miss++;
  @@ -107,13 +54,12 @@
         assertEquals("CacheLoaderStores count error: ", stores, store.getCacheLoaderStores());
   
         // now evict Austria and confirm that it's no longer in cache
  -      key = Fqn.fromString("Europe/Austria");
  -      cache.evict(key);
  -      assertFalse("Retrieval error: did not expect to find node " + key + " in cache", cache.exists(key));
  +      cache.evict(AUSTRIA, false);
  +      assertNull("Retrieval error: did not expect to find node " + AUSTRIA + " in cache", cache.peek(AUSTRIA, false));
   
         // now try retrieving its attributes again - first retrieval should trigger a cache load
  -      assertNotNull("Retrieval error: expected to retrieve " + CAPITAL + " for " + key, cache.get(key, CAPITAL));
  -      assertNotNull("Retrieval error: expected to retrieve " + CURRENCY + " for " + key, cache.get(key, CURRENCY));
  +      assertNotNull("Retrieval error: expected to retrieve " + CAPITAL + " for " + AUSTRIA, cache.get(AUSTRIA, CAPITAL));
  +      assertNotNull("Retrieval error: expected to retrieve " + CURRENCY + " for " + AUSTRIA, cache.get(AUSTRIA, CURRENCY));
   
         // verify statistics after retrieving evicted entry - loads should now be +1
         load++;
  @@ -122,9 +68,9 @@
         assertEquals("CacheLoaderStores count error: ", stores, store.getCacheLoaderStores());
   
         // now remove Austria and confirm that it's not in cache or loader
  -      cache.remove(key);
  -      assertFalse("Retrieval error: did not expect to find node " + key + " in cache", cache.exists(key));
  -      assertFalse("Retrieval error: did not expect to find node " + key + " in loader", cl.exists(key));
  +      cache.removeNode(AUSTRIA);
  +      assertNull("Retrieval error: did not expect to find node " + AUSTRIA + " in cache", cache.peek(AUSTRIA, false));
  +      assertFalse("Retrieval error: did not expect to find node " + AUSTRIA + " in loader", cl.exists(AUSTRIA));
   
         // verify statistics after removing entry - should be unchanged
         assertEquals("CacheLoaderLoads count error: ", load, loader.getCacheLoaderLoads());
  @@ -132,8 +78,8 @@
         assertEquals("CacheLoaderStores count error: ", stores, store.getCacheLoaderStores());
   
         // now try retrieving attributes again - each attempt should fail and cause a miss since node is now removed
  -      assertNull("Retrieval error: did not expect to retrieve " + CAPITAL + " for " + key, cache.get(key, CAPITAL));
  -      assertNull("Retrieval error: did not expect to retrieve " + CURRENCY + " for " + key, cache.get(key, CURRENCY));
  +      assertNull("Retrieval error: did not expect to retrieve " + CAPITAL + " for " + AUSTRIA, cache.get(AUSTRIA, CAPITAL));
  +      assertNull("Retrieval error: did not expect to retrieve " + CURRENCY + " for " + AUSTRIA, cache.get(AUSTRIA, CURRENCY));
   
         // verify statistics after trying to retrieve removed node's attributes - should be two more misses
         miss += 2;
  @@ -143,7 +89,7 @@
   
         // add a new node - this should cause a store
         stores++;
  -      cache.put("Europe/Poland", new HashMap());
  +      cache.put(POLAND, new HashMap());
         assertEquals("CacheLoaderLoads count error: ", load, loader.getCacheLoaderLoads());
         assertEquals("CacheLoaderMisses count error: ", miss, loader.getCacheLoaderMisses());
         assertEquals("CacheLoaderStores count error: ", stores, store.getCacheLoaderStores());
  @@ -152,17 +98,16 @@
         // plus one load as we're doing put(k, v)
         stores += 2;
         load++;
  -      cache.put("Europe/Poland", CAPITAL, "Warsaw");
  -      cache.put("Europe/Poland", CURRENCY, "Zloty");
  +      cache.put(POLAND, CAPITAL, "Warsaw");
  +      cache.put(POLAND, CURRENCY, "Zloty");
         assertEquals("CacheLoaderLoads count error: ", load, loader.getCacheLoaderLoads());
         assertEquals("CacheLoaderMisses count error: ", miss, loader.getCacheLoaderMisses());
         assertEquals("CacheLoaderStores count error: ", stores, store.getCacheLoaderStores());
   
         // evict Poland and then try to retrieve an invalid attribute - this will cause a load as the node is restored
         load++;
  -      key = Fqn.fromString("Europe/Poland");
  -      cache.evict(key);
  -      assertNull("Retrieval error: did not expect to retrieve " + AREA + " for " + key, cache.get(key, AREA));
  +      cache.evict(POLAND, false);
  +      assertNull("Retrieval error: did not expect to retrieve " + AREA + " for " + POLAND, cache.get(POLAND, AREA));
         assertEquals("CacheLoaderLoads count error: ", load, loader.getCacheLoaderLoads());
         assertEquals("CacheLoaderMisses count error: ", miss, loader.getCacheLoaderMisses());
         assertEquals("CacheLoaderStores count error: ", stores, store.getCacheLoaderStores());
  @@ -176,116 +121,4 @@
         assertEquals("CacheLoaderMisses count error after reset: ", 0, loader.getCacheLoaderMisses());
         assertEquals("CacheLoaderStores count error after reset: ", 0, store.getCacheLoaderStores());
      }
  -
  -   /**
  -    * Should trigger 2 cache load events
  -    */
  -   private void loadCache(CacheImpl cache) throws Exception
  -   {
  -      cache.put(EUROPE_NODE, new HashMap());
  -      cache.put("Europe/Austria", new HashMap());
  -      // should cause a cache load
  -      cache.put("Europe/Austria", CAPITAL, "Vienna");
  -      cache.put("Europe/Austria", CURRENCY, "Euro");
  -      cache.put("Europe/Austria", POPULATION, 8184691);
  -
  -      cache.put("Europe/England", new HashMap());
  -      // should cause a cache load
  -      cache.put("Europe/England", CAPITAL, "London");
  -      cache.put("Europe/England", CURRENCY, "British Pound");
  -      cache.put("Europe/England", POPULATION, 60441457);
  -
  -      HashMap albania = new HashMap(4);
  -      albania.put(CAPITAL, "Tirana");
  -      albania.put(CURRENCY, "Lek");
  -      albania.put(POPULATION, 3563112);
  -      albania.put(AREA, 28748);
  -      cache.put("Europe/Albania", albania);
  -
  -      HashMap hungary = new HashMap(4);
  -      hungary.put(CAPITAL, "Budapest");
  -      hungary.put(CURRENCY, "Forint");
  -      hungary.put(POPULATION, 10006835);
  -      hungary.put(AREA, 93030);
  -      cache.put("Europe/Hungary", hungary);
  -
  -   }
  -
  -   private CacheImpl createCache() throws Exception
  -   {
  -      CacheImpl cache = (CacheImpl) DefaultCacheFactory.getInstance().createCache(false);
  -      cache.getConfiguration().setCacheMode(Configuration.CacheMode.LOCAL);
  -      cache.getConfiguration().setCacheLoaderConfig(getCacheLoaderConfig());
  -      cache.getConfiguration().setExposeManagementStatistics(true);
  -      cache.create();
  -      cache.start();
  -      return cache;
  -   }
  -
  -   private CacheLoaderInterceptor getCacheLoaderInterceptor(CacheImpl cache)
  -   {
  -      List interceptors = cache.getInterceptors();
  -      if (interceptors.isEmpty())
  -      {
  -         return null;
  -      }
  -
  -      for (int i = 0; i < interceptors.size(); i++)
  -      {
  -         Object o = interceptors.get(i);
  -         if (o instanceof CacheLoaderInterceptor)
  -         {
  -            return (CacheLoaderInterceptor) o;
  -         }
  -      }
  -      return null;
  -   }
  -
  -   private CacheStoreInterceptor getCacheStoreInterceptor(CacheImpl cache)
  -   {
  -      List interceptors = cache.getInterceptors();
  -      if (interceptors.isEmpty())
  -      {
  -         return null;
  -      }
  -
  -      for (int i = 0; i < interceptors.size(); i++)
  -      {
  -         Object o = interceptors.get(i);
  -         if (o instanceof CacheStoreInterceptor)
  -         {
  -            return (CacheStoreInterceptor) o;
  -         }
  -      }
  -      return null;
  -   }
  -
  -   private CacheLoaderConfig getCacheLoaderConfig() throws Exception
  -   {
  -      String xml = "<config>\n" +
  -              "<passivation>false</passivation>\n" +
  -              "<preload></preload>\n" +
  -              "<shared>false</shared>\n" +
  -              "<cacheloader>\n" +
  -              "<class>" + DummyInMemoryCacheLoader.class.getName() + "</class>\n" +
  -              "<properties>debug=true</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 String getTempDir()
  -   {
  -      return System.getProperty("java.io.tempdir", "/tmp");
  -   }
  -
  -   public static Test suite()
  -   {
  -      return new TestSuite(CacheLoaderTest.class);
  -   }
  -
   }
  
  
  
  1.14      +4 -31     JBossCache/tests/functional/org/jboss/cache/mgmt/InvalidationTest.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: InvalidationTest.java
  ===================================================================
  RCS file: /cvsroot/jboss/JBossCache/tests/functional/org/jboss/cache/mgmt/InvalidationTest.java,v
  retrieving revision 1.13
  retrieving revision 1.14
  diff -u -b -r1.13 -r1.14
  --- InvalidationTest.java	16 Apr 2007 17:37:11 -0000	1.13
  +++ InvalidationTest.java	17 Jul 2007 02:03:26 -0000	1.14
  @@ -1,8 +1,6 @@
   package org.jboss.cache.mgmt;
   
  -import junit.framework.Test;
   import junit.framework.TestCase;
  -import junit.framework.TestSuite;
   import org.jboss.cache.CacheImpl;
   import org.jboss.cache.DefaultCacheFactory;
   import org.jboss.cache.Fqn;
  @@ -10,15 +8,15 @@
   import org.jboss.cache.config.Configuration.CacheMode;
   import org.jboss.cache.factories.UnitTestCacheConfigurationFactory;
   import org.jboss.cache.interceptors.InvalidationInterceptor;
  +import org.jboss.cache.misc.TestingUtil;
   
   import java.util.HashMap;
  -import java.util.List;
   
   /**
    * Simple functional tests for InvalidationInterceptor statistics
    *
    * @author Jerry Gauthier
  - * @version $Id: InvalidationTest.java,v 1.13 2007/04/16 17:37:11 vblagojevic Exp $
  + * @version $Id: InvalidationTest.java,v 1.14 2007/07/17 02:03:26 msurtani Exp $
    */
   public class InvalidationTest extends TestCase
   {
  @@ -86,10 +84,10 @@
         // Note: because these tests are normally executed without a server, the interceptor
         // MBeans are usually not available for use in the tests.  Consequently it's necessary
         // to obtain a reference to the interceptor and work with it directly.
  -      InvalidationInterceptor mgmt1 = getInvalidationInterceptor(cache1);
  +      InvalidationInterceptor mgmt1 = TestingUtil.findInterceptor(cache1, InvalidationInterceptor.class);
         assertNotNull("Cache1 InvalidationInterceptor not found.", mgmt1);
   
  -      InvalidationInterceptor mgmt2 = getInvalidationInterceptor(cache2);
  +      InvalidationInterceptor mgmt2 = TestingUtil.findInterceptor(cache2, InvalidationInterceptor.class);
         assertNotNull("Cache2 InvalidationInterceptor not found.", mgmt2);
   
         assertTrue("Cache1 not configured to use MBeans", cache1.getConfiguration().getExposeManagementStatistics());
  @@ -156,29 +154,4 @@
         cache.start();
         return cache;
      }
  -
  -   private InvalidationInterceptor getInvalidationInterceptor(CacheImpl cache)
  -   {
  -      List interceptors = cache.getInterceptors();
  -      if (interceptors.isEmpty())
  -      {
  -         return null;
  -      }
  -
  -      for (int i = 0; i < interceptors.size(); i++)
  -      {
  -         Object o = interceptors.get(i);
  -         if (o instanceof InvalidationInterceptor)
  -         {
  -            return (InvalidationInterceptor) o;
  -         }
  -      }
  -      return null;
  -   }
  -
  -   public static Test suite()
  -   {
  -      return new TestSuite(InvalidationTest.class);
  -   }
  -
   }
  
  
  
  1.11      +83 -230   JBossCache/tests/functional/org/jboss/cache/mgmt/PassivationTest.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: PassivationTest.java
  ===================================================================
  RCS file: /cvsroot/jboss/JBossCache/tests/functional/org/jboss/cache/mgmt/PassivationTest.java,v
  retrieving revision 1.10
  retrieving revision 1.11
  diff -u -b -r1.10 -r1.11
  --- PassivationTest.java	11 Jan 2007 13:49:05 -0000	1.10
  +++ PassivationTest.java	17 Jul 2007 02:03:26 -0000	1.11
  @@ -1,307 +1,160 @@
   package org.jboss.cache.mgmt;
   
  -import junit.framework.Test;
  -import junit.framework.TestCase;
  -import junit.framework.TestSuite;
  -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.factories.XmlConfigurationParser;
   import org.jboss.cache.interceptors.ActivationInterceptor;
   import org.jboss.cache.interceptors.PassivationInterceptor;
  -import org.jboss.cache.loader.CacheLoader;
  -import org.jboss.cache.xml.XmlHelper;
  -import org.w3c.dom.Element;
  +import org.jboss.cache.misc.TestingUtil;
   
   import java.util.HashMap;
  -import java.util.List;
   
   /**
    * Simple functional tests for ActivationInterceptor and PassivationInterceptor statistics
    *
    * @author Jerry Gauthier
  - * @version $Id: PassivationTest.java,v 1.10 2007/01/11 13:49:05 msurtani Exp $
  + * @version $Id: PassivationTest.java,v 1.11 2007/07/17 02:03:26 msurtani Exp $
    */
  -public class PassivationTest extends TestCase
  +public class PassivationTest extends MgmtTestBase
   {
  -   private static final String CAPITAL = "capital";
  -   private static final String CURRENCY = "currency";
  -   private static final String POPULATION = "population";
  -   private static final String AREA = "area";
  -   private static final String EUROPE_NODE = "Europe";
   
  -   CacheImpl cache = null;
   
  -   protected void setUp() throws Exception
  +   public PassivationTest()
      {
  -      super.setUp();
  -      cache = createCache();
  -   }
  -
  -   protected void tearDown() throws Exception
  -   {
  -      super.tearDown();
  -      if (cache != null)
  -      {
  -         CacheLoader cl = cache.getCacheLoader();
  -         cl.remove(Fqn.fromString(EUROPE_NODE));
  -         cache.stop();
  -         cache.destroy();
  -         cache = null;
  -      }
  +      passivation = true;
      }
   
      public void testPassivationMgmt() throws Exception
      {
         assertNotNull("Cache is null.", cache);
   
  -      // note -it's essential to flush any existing test data from the cache loader
  -      // otherwise the loads and misses counts will be incorrect
  -      CacheLoader cl = cache.getCacheLoader();
  -      assertNotNull("CacheLoader is null.", cl);
  -      cl.remove(Fqn.fromString(EUROPE_NODE));
  -
  -      // populate cache with test data
  -      loadCache(cache);
  -
         // Note: because these tests are normally executed without a server, the interceptor
         // MBeans are usually not available for use in the tests.  Consequently it's necessary
         // to obtain references to the interceptors and work with them directly.
  -      ActivationInterceptor act = getActivationInterceptor(cache);
  +      ActivationInterceptor act = TestingUtil.findInterceptor(cache, ActivationInterceptor.class);
         assertNotNull("ActivationInterceptor not found.", act);
  -      PassivationInterceptor pass = getPassivationInterceptor(cache);
  +      PassivationInterceptor pass = TestingUtil.findInterceptor(cache, PassivationInterceptor.class);
         assertNotNull("PassivationInterceptor not found.", pass);
   
         System.out.println("count of misses " + act.getCacheLoaderMisses());
  -      int miss = 2;
  +      int miss = 0;
  +      int activations = 0;
         // was 5 in 1.3 (one per attribute)
         // now just Europe/Albania and Europe/Hungary were loaded
   
         // verify statistics for entries loaded into cache
  -      assertEquals("CacheLoaderLoads count error: ", new Long(0), new Long(act.getCacheLoaderLoads()));
  -      assertEquals("CacheLoaderMisses count error: ", new Long(miss), new Long(act.getCacheLoaderMisses()));
  -      assertEquals("Activations count error: ", new Long(0), new Long(act.getActivations()));
  -      assertEquals("Passivations count error: ", new Long(0), new Long(pass.getPassivations()));
  +      assertEquals("CacheLoaderLoads count error: ", 0, act.getCacheLoaderLoads());
  +      assertEquals("CacheLoaderMisses count error: ", miss, act.getCacheLoaderMisses());
  +      assertEquals("Activations count error: ", activations, act.getActivations());
  +      assertEquals("Passivations count error: ", 0, pass.getPassivations());
   
         // now try retrieving a valid attribute and an invalid attribute
  -      Fqn key = Fqn.fromString("Europe/Austria");
  -      assertNotNull("Retrieval error: expected to retrieve " + CAPITAL + " for " + key, cache.get(key, CAPITAL));
  -      assertNull("Retrieval error: did not expect to retrieve " + AREA + " for " + key, cache.get(key, AREA));
  +      assertNotNull("Retrieval error: expected to retrieve " + CAPITAL + " for " + AUSTRIA, cache.get(AUSTRIA, CAPITAL));
  +      // will cause a load
  +      miss++;
  +      assertNull("Retrieval error: did not expect to retrieve " + AREA + " for " + AUSTRIA, cache.get(AUSTRIA, AREA));
   
         // verify statistics after retrieving entries - no change since nodes were already loaded
  -      assertEquals("CacheLoaderLoads count error: ", new Long(0), new Long(act.getCacheLoaderLoads()));
  -      assertEquals("CacheLoaderMisses count error: ", new Long(miss), new Long(act.getCacheLoaderMisses()));
  -      assertEquals("Activations count error: ", new Long(0), new Long(act.getActivations()));
  -      assertEquals("Passivations count error: ", new Long(0), new Long(pass.getPassivations()));
  +      assertEquals("CacheLoaderLoads count error: ", 0, act.getCacheLoaderLoads());
  +      assertEquals("CacheLoaderMisses count error: ", miss, act.getCacheLoaderMisses());
  +      assertEquals("Activations count error: ", activations, act.getActivations());
  +      assertEquals("Passivations count error: ", 0, pass.getPassivations());
   
         // now try retrieving an attribute for an invalid node
  -      key = Fqn.fromString("Europe/Poland");
  -      assertNull("Retrieval error: did not expect to retrieve " + CAPITAL + " for " + key, cache.get(key, CAPITAL));
  +      assertNull("Retrieval error: did not expect to retrieve " + CAPITAL + " for " + POLAND, cache.get(POLAND, CAPITAL));
   
         // verify statistics after retrieving invalid node - misses should now be incremented
         miss++;
  -      assertEquals("CacheLoaderLoads count error: ", new Long(0), new Long(act.getCacheLoaderLoads()));
  -      assertEquals("CacheLoaderMisses count error: ", new Long(miss), new Long(act.getCacheLoaderMisses()));
  -      assertEquals("Activations count error: ", new Long(0), new Long(act.getActivations()));
  -      assertEquals("Passivations count error: ", new Long(0), new Long(pass.getPassivations()));
  +      assertEquals("CacheLoaderLoads count error: ", 0, act.getCacheLoaderLoads());
  +      assertEquals("CacheLoaderMisses count error: ", miss, act.getCacheLoaderMisses());
  +      assertEquals("Activations count error: ", activations, act.getActivations());
  +      assertEquals("Passivations count error: ", 0, pass.getPassivations());
   
         // now evict Austria and confirm that it's no longer in cache
  -      key = Fqn.fromString("Europe/Austria");
  -      cache.evict(key);
  -      assertFalse("Retrieval error: did not expect to find node " + key + " in cache", cache.exists(key));
  +      cache.evict(AUSTRIA, false);
  +      assertNull("Retrieval error: did not expect to find node " + AUSTRIA + " in cache", cache.peek(AUSTRIA, false));
   
         // passivations should noe be 1
  -      assertEquals("Passivations count error: ", new Long(1), new Long(pass.getPassivations()));
  +      assertEquals("Passivations count error: ", 1, pass.getPassivations());
   
         // now try retrieving the attributes again - first retrieval should trigger a cache load
  -      assertNotNull("Retrieval error: expected to retrieve " + CAPITAL + " for " + key, cache.get(key, CAPITAL));
  -      assertNotNull("Retrieval error: expected to retrieve " + CURRENCY + " for " + key, cache.get(key, CURRENCY));
  +      assertNotNull("Retrieval error: expected to retrieve " + CAPITAL + " for " + AUSTRIA, cache.get(AUSTRIA, CAPITAL));
  +      assertNotNull("Retrieval error: expected to retrieve " + CURRENCY + " for " + AUSTRIA, cache.get(AUSTRIA, CURRENCY));
   
  -      // verify statistics after retrieving evicted entry - loads and activations should now be 1
  -      assertEquals("CacheLoaderLoads count error: ", new Long(1), new Long(act.getCacheLoaderLoads()));
  -      assertEquals("CacheLoaderMisses count error: ", new Long(miss), new Long(act.getCacheLoaderMisses()));
  -      assertEquals("Activations count error: ", new Long(1), new Long(act.getActivations()));
  -      assertEquals("Passivations count error: ", new Long(1), new Long(pass.getPassivations()));
  +      // verify statistics after retrieving evicted entry - loads and activations should now increment by 1
  +      activations++;
  +      assertEquals("CacheLoaderLoads count error: ", 1, act.getCacheLoaderLoads());
  +      assertEquals("CacheLoaderMisses count error: ", miss, act.getCacheLoaderMisses());
  +      assertEquals("Activations count error: ", activations, act.getActivations());
  +      assertEquals("Passivations count error: ", 1, pass.getPassivations());
   
         // now remove Austria and confirm that it's not in cache or loader
  -      cache.remove(key);
  -      assertFalse("Retrieval error: did not expect to find node " + key + " in cache", cache.exists(key));
  -      assertFalse("Retrieval error: did not expect to find node " + key + " in loader", cl.exists(key));
  +      cache.removeNode(AUSTRIA);
  +      assertNull("Retrieval error: did not expect to find node " + AUSTRIA + " in cache", cache.peek(AUSTRIA, false));
  +      assertFalse("Retrieval error: did not expect to find node " + AUSTRIA + " in loader", cl.exists(AUSTRIA));
   
         // verify statistics after removing entry - should be unchanged
  -      assertEquals("CacheLoaderLoads count error: ", new Long(1), new Long(act.getCacheLoaderLoads()));
  -      assertEquals("CacheLoaderMisses count error: ", new Long(miss), new Long(act.getCacheLoaderMisses()));
  -      assertEquals("Activations count error: ", new Long(1), new Long(act.getActivations()));
  -      assertEquals("Passivations count error: ", new Long(1), new Long(pass.getPassivations()));
  +      assertEquals("CacheLoaderLoads count error: ", 1, act.getCacheLoaderLoads());
  +      assertEquals("CacheLoaderMisses count error: ", miss, act.getCacheLoaderMisses());
  +      assertEquals("Activations count error: ", activations, act.getActivations());
  +      assertEquals("Passivations count error: ", 1, pass.getPassivations());
   
         // now try retrieving attributes again - each attempt should fail and cause a miss since node is now removed
  -      assertNull("Retrieval error: did not expect to retrieve " + CAPITAL + " for " + key, cache.get(key, CAPITAL));
  -      assertNull("Retrieval error: did not expect to retrieve " + CURRENCY + " for " + key, cache.get(key, CURRENCY));
  +      assertNull("Retrieval error: did not expect to retrieve " + CAPITAL + " for " + AUSTRIA, cache.get(AUSTRIA, CAPITAL));
  +      assertNull("Retrieval error: did not expect to retrieve " + CURRENCY + " for " + AUSTRIA, cache.get(AUSTRIA, CURRENCY));
   
         // verify statistics after trying to retrieve removed node's attributes - should be two more misses
         miss += 2;
  -      assertEquals("CacheLoaderLoads count error: ", new Long(1), new Long(act.getCacheLoaderLoads()));
  -      assertEquals("CacheLoaderMisses count error: ", new Long(miss), new Long(act.getCacheLoaderMisses()));
  -      assertEquals("Activations count error: ", new Long(1), new Long(act.getActivations()));
  -      assertEquals("Passivations count error: ", new Long(1), new Long(pass.getPassivations()));
  +      assertEquals("CacheLoaderLoads count error: ", 1, act.getCacheLoaderLoads());
  +      assertEquals("CacheLoaderMisses count error: ", miss, act.getCacheLoaderMisses());
  +      assertEquals("Activations count error: ", activations, act.getActivations());
  +      assertEquals("Passivations count error: ", 1, pass.getPassivations());
   
         // add a new node and two attributes - this should cause a miss only
         miss++;
  -      cache.put("Europe/Poland", new HashMap());
  -      cache.put("Europe/Poland", CAPITAL, "Warsaw");
  -      cache.put("Europe/Poland", CURRENCY, "Zloty");
  -      assertEquals("CacheLoaderLoads count error: ", new Long(1), new Long(act.getCacheLoaderLoads()));
  -      assertEquals("CacheLoaderMisses count error: ", new Long(miss), new Long(act.getCacheLoaderMisses()));
  -      assertEquals("Activations count error: ", new Long(1), new Long(act.getActivations()));
  -      assertEquals("Passivations count error: ", new Long(1), new Long(pass.getPassivations()));
  +      cache.put(POLAND, new HashMap());
  +      cache.put(POLAND, CAPITAL, "Warsaw");
  +      cache.put(POLAND, CURRENCY, "Zloty");
  +      assertEquals("CacheLoaderLoads count error: ", 1, act.getCacheLoaderLoads());
  +      assertEquals("CacheLoaderMisses count error: ", miss, act.getCacheLoaderMisses());
  +      assertEquals("Activations count error: ", activations, act.getActivations());
  +      assertEquals("Passivations count error: ", 1, pass.getPassivations());
   
         // evict Poland - this will cause a passivation
  -      key = Fqn.fromString("Europe/Poland");
  -      cache.evict(key);
  -      assertEquals("CacheLoaderLoads count error: ", new Long(1), new Long(act.getCacheLoaderLoads()));
  -      assertEquals("CacheLoaderMisses count error: ", new Long(miss), new Long(act.getCacheLoaderMisses()));
  -      assertEquals("Activations count error: ", new Long(1), new Long(act.getActivations()));
  -      assertEquals("Passivations count error: ", new Long(2), new Long(pass.getPassivations()));
  +      cache.evict(POLAND, false);
  +      assertEquals("CacheLoaderLoads count error: ", 1, act.getCacheLoaderLoads());
  +      assertEquals("CacheLoaderMisses count error: ", miss, act.getCacheLoaderMisses());
  +      assertEquals("Activations count error: ", activations, act.getActivations());
  +      assertEquals("Passivations count error: ", 2, pass.getPassivations());
   
         // retrieve a valid attribute - this will cause an activation and a load
  -      assertNotNull("Retrieval error: expected to retrieve " + CURRENCY + " for " + key, cache.get(key, CURRENCY));
  -      assertEquals("CacheLoaderLoads count error: ", new Long(2), new Long(act.getCacheLoaderLoads()));
  -      assertEquals("CacheLoaderMisses count error: ", new Long(miss), new Long(act.getCacheLoaderMisses()));
  -      assertEquals("Activations count error: ", new Long(2), new Long(act.getActivations()));
  -      assertEquals("Passivations count error: ", new Long(2), new Long(pass.getPassivations()));
  +      activations++;
  +      assertNotNull("Retrieval error: expected to retrieve " + CURRENCY + " for " + POLAND, cache.get(POLAND, CURRENCY));
  +      assertEquals("CacheLoaderLoads count error: ", 2, act.getCacheLoaderLoads());
  +      assertEquals("CacheLoaderMisses count error: ", miss, act.getCacheLoaderMisses());
  +      assertEquals("Activations count error: ", activations, act.getActivations());
  +      assertEquals("Passivations count error: ", 2, pass.getPassivations());
   
         // evict again - causing another passivation
  -      cache.evict(key);
  -      assertEquals("CacheLoaderLoads count error: ", new Long(2), new Long(act.getCacheLoaderLoads()));
  -      assertEquals("CacheLoaderMisses count error: ", new Long(miss), new Long(act.getCacheLoaderMisses()));
  -      assertEquals("Activations count error: ", new Long(2), new Long(act.getActivations()));
  -      assertEquals("Passivations count error: ", new Long(3), new Long(pass.getPassivations()));
  +      cache.evict(POLAND, false);
  +      assertEquals("CacheLoaderLoads count error: ", 2, act.getCacheLoaderLoads());
  +      assertEquals("CacheLoaderMisses count error: ", miss, act.getCacheLoaderMisses());
  +      assertEquals("Activations count error: ", activations, act.getActivations());
  +      assertEquals("Passivations count error: ", 3, pass.getPassivations());
   
         // retrieve an invalid attribute - this will cause an activation and a load
  -      assertNull("Retrieval error: did not expect to retrieve " + AREA + " for " + key, cache.get(key, AREA));
  -      assertEquals("CacheLoaderLoads count error: ", new Long(3), new Long(act.getCacheLoaderLoads()));
  -      assertEquals("CacheLoaderMisses count error: ", new Long(miss), new Long(act.getCacheLoaderMisses()));
  -      assertEquals("Activations count error: ", new Long(3), new Long(act.getActivations()));
  -      assertEquals("Passivations count error: ", new Long(3), new Long(pass.getPassivations()));
  +      activations++;
  +      assertNull("Retrieval error: did not expect to retrieve " + AREA + " for " + POLAND, cache.get(POLAND, AREA));
  +      assertEquals("CacheLoaderLoads count error: ", 3, act.getCacheLoaderLoads());
  +      assertEquals("CacheLoaderMisses count error: ", miss, act.getCacheLoaderMisses());
  +      assertEquals("Activations count error: ", activations, act.getActivations());
  +      assertEquals("Passivations count error: ", 3, pass.getPassivations());
   
         // reset statistics
         act.resetStatistics();
         pass.resetStatistics();
   
         // check the statistics again
  -      assertEquals("CacheLoaderLoads count error after reset: ", new Long(0), new Long(act.getCacheLoaderLoads()));
  -      assertEquals("CacheLoaderMisses count error after reset: ", new Long(0), new Long(act.getCacheLoaderMisses()));
  -      assertEquals("Activations count error: ", new Long(0), new Long(act.getActivations()));
  -      assertEquals("Passivations count error: ", new Long(0), new Long(pass.getPassivations()));
  -   }
  -
  -   private void loadCache(CacheImpl cache) throws Exception
  -   {
  -      cache.put(EUROPE_NODE, new HashMap());
  -      cache.put("Europe/Austria", new HashMap());
  -      cache.put("Europe/Austria", CAPITAL, "Vienna");
  -      cache.put("Europe/Austria", CURRENCY, "Euro");
  -      cache.put("Europe/Austria", POPULATION, 8184691);
  -
  -      cache.put("Europe/England", new HashMap());
  -      cache.put("Europe/England", CAPITAL, "London");
  -      cache.put("Europe/England", CURRENCY, "British Pound");
  -      cache.put("Europe/England", POPULATION, 60441457);
  -
  -      HashMap albania = new HashMap(4);
  -      albania.put(CAPITAL, "Tirana");
  -      albania.put(CURRENCY, "Lek");
  -      albania.put(POPULATION, 3563112);
  -      albania.put(AREA, 28748);
  -      cache.put("Europe/Albania", albania);
  -
  -      HashMap hungary = new HashMap(4);
  -      hungary.put(CAPITAL, "Budapest");
  -      hungary.put(CURRENCY, "Forint");
  -      hungary.put(POPULATION, 10006835);
  -      hungary.put(AREA, 93030);
  -      cache.put("Europe/Hungary", hungary);
  -   }
  -
  -   private CacheImpl createCache() throws Exception
  -   {
  -      CacheImpl cache = (CacheImpl) DefaultCacheFactory.getInstance().createCache(false);
  -      Configuration c = new Configuration();
  -      c.setCacheMode("LOCAL");
  -      c.setCacheLoaderConfig(getCacheLoaderConfig("location=" + getTempDir()));
  -      c.setExposeManagementStatistics(true);
  -      cache.setConfiguration(c);
  -      cache.create();
  -      cache.start();
  -      return cache;
  -   }
  -
  -   private ActivationInterceptor getActivationInterceptor(CacheImpl cache)
  -   {
  -      List interceptors = cache.getInterceptors();
  -      if (interceptors.isEmpty())
  -      {
  -         return null;
  -      }
  -
  -      for (int i = 0; i < interceptors.size(); i++)
  -      {
  -         Object o = interceptors.get(i);
  -         if (o instanceof ActivationInterceptor)
  -         {
  -            return (ActivationInterceptor) o;
  +      assertEquals("CacheLoaderLoads count error after reset: ", 0, act.getCacheLoaderLoads());
  +      assertEquals("CacheLoaderMisses count error after reset: ", 0, act.getCacheLoaderMisses());
  +      assertEquals("Activations count error: ", 0, act.getActivations());
  +      assertEquals("Passivations count error: ", 0, pass.getPassivations());
            }
  -      }
  -      return null;
  -   }
  -
  -   private PassivationInterceptor getPassivationInterceptor(CacheImpl cache)
  -   {
  -      List interceptors = cache.getInterceptors();
  -      if (interceptors.isEmpty())
  -      {
  -         return null;
  -      }
  -
  -      for (int i = 0; i < interceptors.size(); i++)
  -      {
  -         Object o = interceptors.get(i);
  -         if (o instanceof PassivationInterceptor)
  -         {
  -            return (PassivationInterceptor) o;
  -         }
  -      }
  -      return null;
  -   }
  -
  -   private CacheLoaderConfig getCacheLoaderConfig(String properties) throws Exception
  -   {
  -      String xml = "<config>\n" +
  -              "<passivation>true</passivation>\n" +
  -              "<preload></preload>\n" +
  -              "<shared>false</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 String getTempDir()
  -   {
  -      return System.getProperty("java.io.tempdir", "/tmp");
  -   }
  -
  -   public static Test suite()
  -   {
  -      return new TestSuite(PassivationTest.class);
  -   }
  -
   }
  
  
  
  1.1      date: 2007/07/17 02:03:26;  author: msurtani;  state: Exp;JBossCache/tests/functional/org/jboss/cache/mgmt/MgmtTestBase.java
  
  Index: MgmtTestBase.java
  ===================================================================
  package org.jboss.cache.mgmt;
  
  import junit.framework.TestCase;
  import org.jboss.cache.CacheSPI;
  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.factories.XmlConfigurationParser;
  import org.jboss.cache.loader.CacheLoader;
  import org.jboss.cache.loader.DummyInMemoryCacheLoader;
  import org.jboss.cache.xml.XmlHelper;
  import org.w3c.dom.Element;
  
  import java.util.HashMap;
  import java.util.Map;
  
  public abstract class MgmtTestBase extends TestCase
  {
     protected static final String CAPITAL = "capital";
     protected static final String CURRENCY = "currency";
     protected static final String POPULATION = "population";
     protected static final String AREA = "area";
     protected static final Fqn EUROPE = Fqn.fromString("/Europe");
     protected static final Fqn AUSTRIA = Fqn.fromString("/Europe/Austria");
     protected static final Fqn ENGLAND = Fqn.fromString("/Europe/England");
     protected static final Fqn ALBANIA = Fqn.fromString("/Europe/Albania");
     protected static final Fqn HUNGARY = Fqn.fromString("/Europe/Hungary");
     protected static final Fqn POLAND = Fqn.fromString("/Europe/Poland");
  
     protected boolean passivation = false;
  
     protected CacheSPI<String, Object> cache = null;
     protected CacheLoader cl;
  
     protected void setUp() throws Exception
     {
        super.setUp();
        cache = createCache();
        // populate cache with test data
        loadCache();
        cl = cache.getCacheLoaderManager().getCacheLoader();
     }
  
     protected void tearDown() throws Exception
     {
        super.tearDown();
        if (cache != null)
        {
           cache.stop();
           cache.destroy();
           cache = null;
        }
     }
  
     /**
      * Should trigger 2 cache load events
      */
     protected void loadCache() throws Exception
     {
        cache.put(EUROPE, null);
  
        Map austria = new HashMap();
        austria.put(CAPITAL, "VIENNA");
        austria.put(CURRENCY, "Euro");
        austria.put(POPULATION, 8184691);
        cache.put(AUSTRIA, austria);
  
        Map england = new HashMap();
        england.put(CAPITAL, "London");
        england.put(CURRENCY, "British Pound");
        england.put(POPULATION, 60441457);
        cache.put(ENGLAND, england);
  
        Map albania = new HashMap(4);
        albania.put(CAPITAL, "Tirana");
        albania.put(CURRENCY, "Lek");
        albania.put(POPULATION, 3563112);
        albania.put(AREA, 28748);
        cache.put(ALBANIA, albania);
  
        Map hungary = new HashMap(4);
        hungary.put(CAPITAL, "Budapest");
        hungary.put(CURRENCY, "Forint");
        hungary.put(POPULATION, 10006835);
        hungary.put(AREA, 93030);
        cache.put(HUNGARY, hungary);
     }
  
     private CacheSPI<String, Object> createCache() throws Exception
     {
        CacheSPI cache = (CacheSPI) DefaultCacheFactory.getInstance().createCache(false);
        cache.getConfiguration().setCacheMode(Configuration.CacheMode.LOCAL);
        cache.getConfiguration().setCacheLoaderConfig(getCacheLoaderConfig());
        cache.getConfiguration().setExposeManagementStatistics(true);
        cache.create();
        cache.start();
        return cache;
     }
  
     private CacheLoaderConfig getCacheLoaderConfig() throws Exception
     {
        String xml = "<config>\n" +
                "<passivation>" + passivation + "</passivation>\n" +
                "<preload></preload>\n" +
                "<shared>false</shared>\n" +
                "<cacheloader>\n" +
                "<class>" + DummyInMemoryCacheLoader.class.getName() + "</class>\n" +
                "<properties>debug=true</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);
     }
  }
  
  
  



More information about the jboss-cvs-commits mailing list