[jbosscache-commits] JBoss Cache SVN: r4563 - core/trunk/src/test/java/org/jboss/cache/loader.

jbosscache-commits at lists.jboss.org jbosscache-commits at lists.jboss.org
Mon Oct 8 10:47:53 EDT 2007


Author: manik.surtani at jboss.com
Date: 2007-10-08 10:47:53 -0400 (Mon, 08 Oct 2007)
New Revision: 4563

Modified:
   core/trunk/src/test/java/org/jboss/cache/loader/CacheLoaderPurgingTest.java
   core/trunk/src/test/java/org/jboss/cache/loader/DummyInMemoryCacheLoader.java
   core/trunk/src/test/java/org/jboss/cache/loader/DummySharedInMemoryCacheLoader.java
Log:
Patched DSIMCL to deal with multiple "bins", and patched CLPT to use the DSIMCL instead of FCLs - more accurate on Hudson servers and less chance of leaving stale files behind.

Modified: core/trunk/src/test/java/org/jboss/cache/loader/CacheLoaderPurgingTest.java
===================================================================
--- core/trunk/src/test/java/org/jboss/cache/loader/CacheLoaderPurgingTest.java	2007-10-08 14:16:26 UTC (rev 4562)
+++ core/trunk/src/test/java/org/jboss/cache/loader/CacheLoaderPurgingTest.java	2007-10-08 14:47:53 UTC (rev 4563)
@@ -6,11 +6,6 @@
  */
 package org.jboss.cache.loader;
 
-import static org.testng.AssertJUnit.assertEquals;
-import static org.testng.AssertJUnit.assertNotNull;
-import static org.testng.AssertJUnit.assertNull;
-import static org.testng.AssertJUnit.assertTrue;
-
 import org.jboss.cache.CacheException;
 import org.jboss.cache.CacheImpl;
 import org.jboss.cache.DefaultCacheFactory;
@@ -18,11 +13,14 @@
 import org.jboss.cache.config.Configuration;
 import org.jboss.cache.factories.XmlConfigurationParser;
 import org.jboss.cache.xml.XmlHelper;
+import static org.testng.AssertJUnit.*;
 import org.testng.annotations.AfterMethod;
+import org.testng.annotations.Test;
 import org.w3c.dom.Element;
 /**
  * @author <a href="mailto:manik at jboss.org">Manik Surtani (manik at jboss.org)</a>
  */
+ at Test(groups = {"functional"})
 public class CacheLoaderPurgingTest extends AbstractCacheLoaderTestBase
 {
    private CacheImpl<Object, Object> cache;
@@ -45,7 +43,7 @@
       cache = (CacheImpl<Object, Object>) DefaultCacheFactory.getInstance().createCache(false);
       Configuration c = new Configuration();
       cache.setConfiguration(c);
-      c.setCacheLoaderConfig(getSingleCacheLoaderConfig("", "org.jboss.cache.loader.FileCacheLoader", "location=" + System.getProperty("java.io.tmpdir", "/tmp") + "/" + "CacheLoaderPurgingTest", false, false, false));
+      c.setCacheLoaderConfig(getSingleCacheLoaderConfig("", DummySharedInMemoryCacheLoader.class.getName(), "", false, false, false));
       cache.start();
 
       cache.put(fqn, key, value);
@@ -69,7 +67,7 @@
       cache = (CacheImpl<Object, Object>) DefaultCacheFactory.getInstance().createCache(false);
       Configuration c = new Configuration();
       cache.setConfiguration(c);
-      c.setCacheLoaderConfig(getSingleCacheLoaderConfig("", "org.jboss.cache.loader.FileCacheLoader", "location=" + System.getProperty("java.io.tmpdir", "/tmp") + "/" + "CacheLoaderPurgingTest", false, false, false, true));
+      c.setCacheLoaderConfig(getSingleCacheLoaderConfig("", DummySharedInMemoryCacheLoader.class.getName(), "", false, false, false, true));
       cache.start();
 
       cache.put(fqn, key, value);
@@ -99,18 +97,18 @@
               "<passivation>false</passivation>\n" +
               "<preload></preload>\n" +
               "<cacheloader>\n" +
-              "<class>org.jboss.cache.loader.FileCacheLoader</class>\n" +
+              "<class>" + DummySharedInMemoryCacheLoader.class.getName() + "</class>\n" +
               "<properties>" +
-              " location=" + System.getProperty("java.io.tmpdir", "/tmp") + "/" + "CacheLoaderPurgingTest_1" + "\n" +
+              " bin=bin1\n" +
               "</properties>\n" +
               "<async>false</async>\n" +
               "<fetchPersistentState>true</fetchPersistentState>\n" +
               "<purgeOnStartup>" + true + "</purgeOnStartup>\n" +
               "</cacheloader>\n" +
               "<cacheloader>\n" +
-              "<class>org.jboss.cache.loader.FileCacheLoader</class>\n" +
+              "<class>" + DummySharedInMemoryCacheLoader.class.getName() + "</class>\n" +
               "<properties>" +
-              " location=" + System.getProperty("java.io.tmpdir", "/tmp") + "/" + "CacheLoaderPurgingTest_2" + "\n" +
+              " bin=bin2\n" +
               "</properties>\n" +
               "<async>false</async>\n" +
               "<fetchPersistentState>false</fetchPersistentState>\n" +

Modified: core/trunk/src/test/java/org/jboss/cache/loader/DummyInMemoryCacheLoader.java
===================================================================
--- core/trunk/src/test/java/org/jboss/cache/loader/DummyInMemoryCacheLoader.java	2007-10-08 14:16:26 UTC (rev 4562)
+++ core/trunk/src/test/java/org/jboss/cache/loader/DummyInMemoryCacheLoader.java	2007-10-08 14:47:53 UTC (rev 4563)
@@ -29,6 +29,7 @@
 @ThreadSafe
 public class DummyInMemoryCacheLoader extends AbstractCacheLoader
 {
+   // Do NOT access this map directly.  always use getNodesMap() since it may be overridden.
    protected Map<Fqn, DummyNode> nodes = new ConcurrentHashMap<Fqn, DummyNode>();
    protected Log log = LogFactory.getLog(DummyInMemoryCacheLoader.class);
    protected Map<Object, List<Modification>> transactions = new ConcurrentHashMap<Object, List<Modification>>();
@@ -57,9 +58,9 @@
 
    public Set<?> getChildrenNames(Fqn fqn) throws Exception
    {
-      if (log.isDebugEnabled()) log.debug("Calling getChildrenNames on Fqn " + fqn + ".  Data map = " + nodes);
-      debugMessage("Calling getChildrenNames on Fqn " + fqn + ".  Data map = " + nodes);
-      if (!nodes.containsKey(fqn))
+      if (log.isDebugEnabled()) log.debug("Calling getChildrenNames on Fqn " + fqn + ".  Data map = " + getNodesMap());
+      debugMessage("Calling getChildrenNames on Fqn " + fqn + ".  Data map = " + getNodesMap());
+      if (!getNodesMap().containsKey(fqn))
       {
          log.debug("node not in loader");
          debugMessage("node not in loader");
@@ -77,7 +78,7 @@
    private Set findChildren(Fqn p)
    {
       Set c = new HashSet();
-      for (Fqn f : nodes.keySet())
+      for (Fqn f : getNodesMap().keySet())
       {
          if (!f.isRoot() && f.getParent().equals(p))
          {
@@ -89,7 +90,7 @@
 
    public Map<Object, Object> get(Fqn name) throws Exception
    {
-      DummyNode dn = nodes.get(name);
+      DummyNode dn = getNodesMap().get(name);
       Map<Object, Object> d = dn != null ? dn.data : null;
 
       if (log.isDebugEnabled()) log.debug("Getting data for fqn " + name + " = " + d);
@@ -151,12 +152,12 @@
    public boolean exists(Fqn name) throws Exception
    {
       debugMessage("Performing exists() on " + name);
-      return nodes.containsKey(name);
+      return getNodesMap().containsKey(name);
    }
 
    public Object put(Fqn name, Object key, Object value) throws Exception
    {
-      DummyNode n = nodes.get(name);
+      DummyNode n = getNodesMap().get(name);
       if (n == null)
       {
          n = new DummyNode(name);
@@ -165,7 +166,7 @@
       Object v = value == null ? NULL : value;
       Object old = n.data.put(k, v);
 
-      nodes.put(name, n);
+      getNodesMap().put(name, n);
       // we need to make sure parents get put in as well.
       recursivelyPutParentsIfNeeded(name);
       if (log.isDebugEnabled()) log.debug("Did a put on " + name + ", data is " + n.data);
@@ -175,13 +176,13 @@
 
    public void put(Fqn name, Map<Object, Object> attributes) throws Exception
    {
-      DummyNode n = nodes.get(name);
+      DummyNode n = getNodesMap().get(name);
       if (n == null)
       {
          n = new DummyNode(name);
       }
       if (attributes != null) n.data.putAll(injectNULLs(attributes));
-      nodes.put(name, n);
+      getNodesMap().put(name, n);
       // we need to make sure parents get put in as well.
       recursivelyPutParentsIfNeeded(name);
       if (log.isDebugEnabled()) log.debug("Did a put on " + name + ", data is " + n.data);
@@ -191,10 +192,10 @@
    private void recursivelyPutParentsIfNeeded(Fqn node)
    {
       Fqn parent = node.getParent();
-      if (nodes.containsKey(parent)) return; // nothing to do.
+      if (getNodesMap().containsKey(parent)) return; // nothing to do.
 
       // else put the parent in.
-      nodes.put(parent, new DummyNode(parent));
+      getNodesMap().put(parent, new DummyNode(parent));
       recursivelyPutParentsIfNeeded(parent);
    }
 
@@ -202,10 +203,10 @@
    {
       log.debug("Removing data from " + fqn);
       debugMessage("Removing data from " + fqn);
-      DummyNode n = nodes.get(fqn);
+      DummyNode n = getNodesMap().get(fqn);
       if (n == null) n = new DummyNode(fqn);
       Object old = n.data.remove(key == null ? NULL : key);
-      nodes.put(fqn, n);
+      getNodesMap().put(fqn, n);
       return old == NULL ? null : old;
    }
 
@@ -213,19 +214,19 @@
    {
       log.debug("Removing fqn " + fqn);
       debugMessage("Removing fqn " + fqn);
-      nodes.remove(fqn);
+      getNodesMap().remove(fqn);
       // remove children.
       recursivelyRemoveChildren(fqn);
    }
 
    private void recursivelyRemoveChildren(Fqn removedParent)
    {
-      for (Fqn f : nodes.keySet())
+      for (Fqn f : getNodesMap().keySet())
       {
          if (f.getParent().equals(removedParent))
          {
             // remove the child node too
-            nodes.remove(f);
+            getNodesMap().remove(f);
             // and it's children.  Depth first.
             recursivelyRemoveChildren(f);
          }
@@ -236,10 +237,10 @@
    {
       log.debug("Removing data from " + fqn);
       debugMessage("Removing data from " + fqn);
-      DummyNode n = nodes.get(fqn);
+      DummyNode n = getNodesMap().get(fqn);
       if (n == null) n = new DummyNode(fqn);
       n.data.clear();
-      nodes.put(fqn, n);
+      getNodesMap().put(fqn, n);
    }
 
    public class DummyNode
@@ -265,7 +266,7 @@
    public String toString()
    {
       return "DummyInMemoryCacheLoader{" +
-              "nodes=" + nodes +
+              "getNodesMap()=" + getNodesMap() +
               '}';
    }
 
@@ -273,4 +274,12 @@
    {
       if (debug) System.out.println(" DummyInMemoryCacheLoader debug: " + msg);
    }
+
+   /**
+    * ALWAYS use this method instead of accessing the node map directly as it may be overridden.
+    */
+   protected Map<Fqn, DummyNode> getNodesMap()
+   {
+      return nodes;
+   }
 }

Modified: core/trunk/src/test/java/org/jboss/cache/loader/DummySharedInMemoryCacheLoader.java
===================================================================
--- core/trunk/src/test/java/org/jboss/cache/loader/DummySharedInMemoryCacheLoader.java	2007-10-08 14:16:26 UTC (rev 4562)
+++ core/trunk/src/test/java/org/jboss/cache/loader/DummySharedInMemoryCacheLoader.java	2007-10-08 14:47:53 UTC (rev 4563)
@@ -1,23 +1,44 @@
 package org.jboss.cache.loader;
 
+import org.jboss.cache.Fqn;
+import org.jboss.cache.config.CacheLoaderConfig;
+
 import java.util.Map;
+import java.util.Properties;
 import java.util.concurrent.ConcurrentHashMap;
 
-import org.jboss.cache.Fqn;
-
 /**
  * An extension of the {@link org.jboss.cache.loader.DummyInMemoryCacheLoader} that uses static maps for data, children,
  * etc. so it can be shared across instances, emulating a shared database or filesystem cache loader.
  *
+ * Since 2.1.0, this dummy cache loader will take an optional parameter, "bin", which contains the name of the "bin" to use
+ * in the static field to store the content.  This allows for tests to mimic multiple shared cache loaders in the same cache.
+ *
  * @author <a href="mailto:manik at jboss.org">Manik Surtani</a>
  * @since 2.0.0
  */
 public class DummySharedInMemoryCacheLoader extends DummyInMemoryCacheLoader
 {
-   protected static final Map<Fqn, DummyNode> SHARED_NODES_MAP = new ConcurrentHashMap<Fqn, DummyNode>();
+   protected static final Map<String, Map<Fqn, DummyNode>> BINS = new ConcurrentHashMap<String, Map<Fqn, DummyNode>>();
+   private String bin = "_default_bin_";
 
-   public DummySharedInMemoryCacheLoader()
+   @Override
+   public void setConfig(CacheLoaderConfig.IndividualCacheLoaderConfig cfg)
    {
-      nodes = SHARED_NODES_MAP;
+      super.setConfig(cfg);
+
+      if (config != null && config.getProperties() != null)
+      {
+         bin = config.getProperties().getProperty("bin", "_default_bin_");
+      }
+
+      if (!BINS.containsKey(bin)) BINS.put(bin, new ConcurrentHashMap<Fqn, DummyNode>());
+      nodes = null; // set this to null so any method in superclass that uses this directly will barf with an NPE 
    }
+
+   @Override
+   protected Map<Fqn, DummyNode> getNodesMap()
+   {
+      return BINS.get(bin);
+   }
 }




More information about the jbosscache-commits mailing list