[jboss-cvs] JBossCache/tests/functional/org/jboss/cache/loader ...
Manik Surtani
manik at jboss.org
Tue May 29 06:26:28 EDT 2007
User: msurtani
Date: 07/05/29 06:26:28
Modified: tests/functional/org/jboss/cache/loader
DummyInMemoryCacheLoader.java
Log:
Fixed breaking UTs
Revision Changes Path
1.7 +50 -18 JBossCache/tests/functional/org/jboss/cache/loader/DummyInMemoryCacheLoader.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: DummyInMemoryCacheLoader.java
===================================================================
RCS file: /cvsroot/jboss/JBossCache/tests/functional/org/jboss/cache/loader/DummyInMemoryCacheLoader.java,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -b -r1.6 -r1.7
--- DummyInMemoryCacheLoader.java 4 Jan 2007 14:36:00 -0000 1.6
+++ DummyInMemoryCacheLoader.java 29 May 2007 10:26:28 -0000 1.7
@@ -17,6 +17,7 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
/**
* Dummy cache loader that stores data in memory
@@ -25,9 +26,9 @@
*/
public class DummyInMemoryCacheLoader extends AbstractCacheLoader
{
- private Map<Fqn, Node> nodes = new HashMap<Fqn, Node>();
+ private Map<Fqn, DummyNode> nodes = new ConcurrentHashMap<Fqn, DummyNode>();
private Log log = LogFactory.getLog(DummyInMemoryCacheLoader.class);
- private Map<Object, List<Modification>> transactions = new HashMap<Object, List<Modification>>();
+ private Map<Object, List<Modification>> transactions = new ConcurrentHashMap<Object, List<Modification>>();
public void setConfig(IndividualCacheLoaderConfig config)
{
@@ -40,7 +41,7 @@
public Set getChildrenNames(Fqn fqn) throws Exception
{
- log.debug("Calling getChildrenNames on Fqn " + fqn);
+ log.debug("Calling getChildrenNames on Fqn " + fqn + ". Data map = " + nodes);
if (!nodes.containsKey(fqn))
{
log.debug("node not in loader");
@@ -49,7 +50,8 @@
Set children = findChildren(fqn);
log.debug("Fqn " + fqn + " has children " + children);
- return children;
+ // to keep in line with the CacheLoader interface contract for this method.
+ return children.size() == 0 ? null : children;
}
private Set findChildren(Fqn p)
@@ -57,7 +59,7 @@
Set c = new HashSet();
for (Fqn f : nodes.keySet())
{
- if (f.getParent().equals(p))
+ if (!f.isRoot() && f.getParent().equals(p))
{
c.add(f.getLastElement());
}
@@ -79,37 +81,48 @@
public Object put(Fqn name, Object key, Object value) throws Exception
{
- Node n = nodes.get(name);
+ DummyNode n = nodes.get(name);
if (n == null)
{
- n = new Node();
- n.fqn = name;
+ n = new DummyNode(name);
}
Object old = n.data.put(key, value);
nodes.put(name, n);
+ // we need to make sure parents get put in as well.
+ recursivelyPutParentsIfNeeded(name);
log.debug("Did a put on " + name + ", data is " + n.data);
return old;
}
public void put(Fqn name, Map attributes) throws Exception
{
- Node n = nodes.get(name);
+ DummyNode n = nodes.get(name);
if (n == null)
{
- n = new Node();
- n.fqn = name;
+ n = new DummyNode(name);
}
if (attributes != null) n.data.putAll(attributes);
nodes.put(name, n);
+ // we need to make sure parents get put in as well.
+ recursivelyPutParentsIfNeeded(name);
log.debug("Did a put on " + name + ", data is " + n.data);
}
+ private void recursivelyPutParentsIfNeeded(Fqn node)
+ {
+ Fqn parent = node.getParent();
+ if (nodes.containsKey(parent)) return; // nothing to do.
+
+ // else put the parent in.
+ nodes.put(parent, new DummyNode(parent));
+ recursivelyPutParentsIfNeeded(parent);
+ }
+
public Object remove(Fqn fqn, Object key) throws Exception
{
log.debug("Removing data from " + fqn);
- Node n = nodes.get(fqn);
- if (n == null) n = new Node();
- n.fqn = fqn;
+ DummyNode n = nodes.get(fqn);
+ if (n == null) n = new DummyNode(fqn);
Object old = n.data.remove(key);
nodes.put(fqn, n);
return old;
@@ -119,14 +132,29 @@
{
log.debug("Removing fqn " + fqn);
nodes.remove(fqn);
+ // remove children.
+ recursivelyRemoveChildren(fqn);
+ }
+
+ private void recursivelyRemoveChildren(Fqn removedParent)
+ {
+ for (Fqn f : nodes.keySet())
+ {
+ if (f.getParent().equals(removedParent))
+ {
+ // remove the child node too
+ nodes.remove(f);
+ // and it's children. Depth first.
+ recursivelyRemoveChildren(f);
+ }
+ }
}
public void removeData(Fqn fqn) throws Exception
{
log.debug("Removing data from " + fqn);
- Node n = nodes.get(fqn);
- if (n == null) n = new Node();
- n.fqn = fqn;
+ DummyNode n = nodes.get(fqn);
+ if (n == null) n = new DummyNode(fqn);
n.data.clear();
nodes.put(fqn, n);
}
@@ -176,11 +204,15 @@
}
- public class Node
+ public class DummyNode
{
Map data = new HashMap();
Fqn fqn;
+ public DummyNode(Fqn fqn)
+ {
+ this.fqn = fqn;
+ }
public String toString()
{
More information about the jboss-cvs-commits
mailing list