You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by oh...@apache.org on 2014/03/21 21:34:45 UTC

svn commit: r1580039 - in /commons/proper/configuration/branches/immutableNodes/src: main/java/org/apache/commons/configuration/AbstractHierarchicalConfiguration.java test/java/org/apache/commons/configuration/TestAbstractHierarchicalConfiguration.java

Author: oheger
Date: Fri Mar 21 20:34:45 2014
New Revision: 1580039

URL: http://svn.apache.org/r1580039
Log:
Implemented nodeKey() in AbstractHierarchicalConfiguration.

Modified:
    commons/proper/configuration/branches/immutableNodes/src/main/java/org/apache/commons/configuration/AbstractHierarchicalConfiguration.java
    commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/TestAbstractHierarchicalConfiguration.java

Modified: commons/proper/configuration/branches/immutableNodes/src/main/java/org/apache/commons/configuration/AbstractHierarchicalConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/immutableNodes/src/main/java/org/apache/commons/configuration/AbstractHierarchicalConfiguration.java?rev=1580039&r1=1580038&r2=1580039&view=diff
==============================================================================
--- commons/proper/configuration/branches/immutableNodes/src/main/java/org/apache/commons/configuration/AbstractHierarchicalConfiguration.java (original)
+++ commons/proper/configuration/branches/immutableNodes/src/main/java/org/apache/commons/configuration/AbstractHierarchicalConfiguration.java Fri Mar 21 20:34:45 2014
@@ -541,9 +541,31 @@ public abstract class AbstractHierarchic
                 removedItems, key);
     }
 
+    /**
+     * {@inheritDoc} This implementation uses the expression engine to generate a
+     * canonical key for the passed in node. For this purpose, the path to the
+     * root node has to be traversed. The cache is used to store and access keys
+     * for nodes encountered on the path.
+     */
     public String nodeKey(T node, Map<T, String> cache, NodeHandler<T> handler) {
-        //TODO implementation
-        return null;
+        List<T> path = new LinkedList<T>();
+        T currentNode = node;
+        String key = cache.get(node);
+        while (key == null && currentNode != null)
+        {
+            path.add(0, currentNode);
+            currentNode = handler.getParent(currentNode);
+            key = cache.get(currentNode);
+        }
+
+        for (T n : path)
+        {
+            String currentKey = getExpressionEngine().canonicalKey(n, key, handler);
+            cache.put(n, currentKey);
+            key = currentKey;
+        }
+
+        return key;
     }
 
     /**

Modified: commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/TestAbstractHierarchicalConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/TestAbstractHierarchicalConfiguration.java?rev=1580039&r1=1580038&r2=1580039&view=diff
==============================================================================
--- commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/TestAbstractHierarchicalConfiguration.java (original)
+++ commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/TestAbstractHierarchicalConfiguration.java Fri Mar 21 20:34:45 2014
@@ -27,9 +27,11 @@ import static org.junit.Assert.fail;
 
 import java.util.ArrayList;
 import java.util.Collection;
+import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Iterator;
 import java.util.List;
+import java.util.Map;
 import java.util.Set;
 
 import org.apache.commons.collections.CollectionUtils;
@@ -42,6 +44,7 @@ import org.apache.commons.configuration.
 import org.apache.commons.configuration.tree.ExpressionEngine;
 import org.apache.commons.configuration.tree.ImmutableNode;
 import org.apache.commons.configuration.tree.InMemoryNodeModel;
+import org.apache.commons.configuration.tree.NodeHandler;
 import org.apache.commons.configuration.tree.NodeModel;
 import org.apache.commons.configuration.tree.NodeStructureHelper;
 import org.junit.Before;
@@ -832,6 +835,92 @@ public class TestAbstractHierarchicalCon
     }
 
     /**
+     * Tests whether a correct node key is generated if no data is contained in
+     * the cache.
+     */
+    @Test
+    public void testNodeKeyEmptyCache()
+    {
+        Map<ImmutableNode, String> cache = new HashMap<ImmutableNode, String>();
+        ImmutableNode nodeTabName =
+                NodeStructureHelper.nodeForKey(config.getRootNode(),
+                        "tables/table(0)/name");
+        ImmutableNode nodeFldName =
+                NodeStructureHelper.nodeForKey(config.getRootNode(),
+                        "tables/table(0)/fields/field(1)/name");
+        assertEquals("Wrong key (1)", "tables(0).table(0).name(0)",
+                config.nodeKey(nodeTabName, cache, config.getModel()
+                        .getNodeHandler()));
+        assertEquals("Wrong key (2)",
+                "tables(0).table(0).fields(0).field(1).name(0)",
+                config.nodeKey(nodeFldName, cache, config.getModel()
+                        .getNodeHandler()));
+    }
+
+    /**
+     * Tests whether the cache map is filled while generating node keys.
+     */
+    @Test
+    public void testNodeKeyCachePopulated()
+    {
+        Map<ImmutableNode, String> cache = new HashMap<ImmutableNode, String>();
+        ImmutableNode nodeTabName =
+                NodeStructureHelper.nodeForKey(config.getRootNode(),
+                        "tables/table(0)/name");
+        NodeHandler<ImmutableNode> handler = config.getModel().getNodeHandler();
+        config.nodeKey(nodeTabName, cache, handler);
+        assertEquals("Wrong number of elements", 4, cache.size());
+        assertEquals("Wrong entry (1)", "tables(0).table(0).name(0)",
+                cache.get(nodeTabName));
+        assertEquals("Wrong entry (2)", "tables(0).table(0)",
+                cache.get(handler.getParent(nodeTabName)));
+        assertEquals("Wrong entry (3)", "tables(0)",
+                cache.get(handler.getParent(handler.getParent(nodeTabName))));
+        assertEquals("Wrong root entry", "", cache.get(config.getRootNode()));
+    }
+
+    /**
+     * Tests whether the cache is used by nodeKey().
+     */
+    @Test
+    public void testNodeKeyCacheUsage()
+    {
+        Map<ImmutableNode, String> cache = new HashMap<ImmutableNode, String>();
+        ImmutableNode nodeTabName =
+                NodeStructureHelper.nodeForKey(config.getRootNode(),
+                        "tables/table(0)/name");
+        NodeHandler<ImmutableNode> handler = config.getModel().getNodeHandler();
+        cache.put(handler.getParent(nodeTabName), "somePrefix");
+        assertEquals("Wrong key", "somePrefix.name(0)",
+                config.nodeKey(nodeTabName, cache, handler));
+    }
+
+    /**
+     * Tests whether a node key for the root node can be generated.
+     */
+    @Test
+    public void testNodeKeyRootNode()
+    {
+        Map<ImmutableNode, String> cache = new HashMap<ImmutableNode, String>();
+        assertEquals("Wrong root node key", "",
+                config.nodeKey(config.getRootNode(), cache, config.getModel()
+                        .getNodeHandler()));
+    }
+
+    /**
+     * Tests nodeKey() if the key is directly found in the cache.
+     */
+    @Test
+    public void testNodeKeyCacheHit()
+    {
+        Map<ImmutableNode, String> cache = new HashMap<ImmutableNode, String>();
+        final String key = "someResultKey";
+        cache.put(config.getRootNode(), key);
+        assertEquals("Wrong result", key, config.nodeKey(config.getRootNode(),
+                cache, config.getModel().getNodeHandler()));
+    }
+
+    /**
      * Helper method for testing the getKeys(String) method.
      *
      * @param prefix the key to pass into getKeys()