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/04/13 17:56:02 UTC

svn commit: r1587007 - in /commons/proper/configuration/branches/immutableNodes/src: main/java/org/apache/commons/configuration/tree/ test/java/org/apache/commons/configuration/tree/

Author: oheger
Date: Sun Apr 13 15:56:02 2014
New Revision: 1587007

URL: http://svn.apache.org/r1587007
Log:
Reworked OverrideCombiner to operate on ImmutableNode objects.

The class and its test class were adapted to be compatible with the changes
on the NodeCombinder base class.

Modified:
    commons/proper/configuration/branches/immutableNodes/src/main/java/org/apache/commons/configuration/tree/OverrideCombiner.java
    commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/tree/AbstractCombinerTest.java
    commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/tree/TestOverrideCombiner.java

Modified: commons/proper/configuration/branches/immutableNodes/src/main/java/org/apache/commons/configuration/tree/OverrideCombiner.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/immutableNodes/src/main/java/org/apache/commons/configuration/tree/OverrideCombiner.java?rev=1587007&r1=1587006&r2=1587007&view=diff
==============================================================================
--- commons/proper/configuration/branches/immutableNodes/src/main/java/org/apache/commons/configuration/tree/OverrideCombiner.java (original)
+++ commons/proper/configuration/branches/immutableNodes/src/main/java/org/apache/commons/configuration/tree/OverrideCombiner.java Sun Apr 13 15:56:02 2014
@@ -47,9 +47,6 @@ package org.apache.commons.configuration
  * will never be combined.
  * </p>
  *
- * @author <a
- * href="http://commons.apache.org/configuration/team-list.html">Commons
- * Configuration team</a>
  * @version $Id$
  * @since 1.3
  */
@@ -63,16 +60,16 @@ public class OverrideCombiner extends No
      * @return the resulting combined node structure
      */
     @Override
-    public ConfigurationNode combine(ConfigurationNode node1,
-            ConfigurationNode node2)
+    public ImmutableNode combine(ImmutableNode node1,
+            ImmutableNode node2)
     {
-        ViewNode result = createViewNode();
-        result.setName(node1.getName());
+        ImmutableNode.Builder result = new ImmutableNode.Builder();
+        result.name(node1.getNodeName());
 
         // Process nodes from the first structure, which override the second
-        for (ConfigurationNode child : node1.getChildren())
+        for (ImmutableNode child : node1.getChildren())
         {
-            ConfigurationNode child2 = canCombine(node1, node2, child);
+            ImmutableNode child2 = canCombine(node1, node2, child);
             if (child2 != null)
             {
                 result.addChild(combine(child, child2));
@@ -85,9 +82,9 @@ public class OverrideCombiner extends No
 
         // Process nodes from the second structure, which are not contained
         // in the first structure
-        for (ConfigurationNode child : node2.getChildren())
+        for (ImmutableNode child : node2.getChildren())
         {
-            if (node1.getChildrenCount(child.getName()) < 1)
+            if (HANDLER.getChildrenCount(node1, child.getNodeName()) < 1)
             {
                 result.addChild(child);
             }
@@ -95,31 +92,31 @@ public class OverrideCombiner extends No
 
         // Handle attributes and value
         addAttributes(result, node1, node2);
-        result.setValue((node1.getValue() != null) ? node1.getValue() : node2
+        result.value((node1.getValue() != null) ? node1.getValue() : node2
                 .getValue());
 
-        return result;
+        return result.create();
     }
 
     /**
      * Handles the attributes during a combination process. First all attributes
-     * of the first node will be added to the result. Then all attributes of the
-     * second node, which are not contained in the first node, will also be
-     * added.
+     * of the first node are added to the result. Then all attributes of the
+     * second node, which are not contained in the first node, are also added.
      *
      * @param result the resulting node
      * @param node1 the first node
      * @param node2 the second node
      */
-    protected void addAttributes(ViewNode result, ConfigurationNode node1,
-            ConfigurationNode node2)
+    protected void addAttributes(ImmutableNode.Builder result,
+            ImmutableNode node1, ImmutableNode node2)
     {
-        result.appendAttributes(node1);
-        for (ConfigurationNode attr : node2.getAttributes())
+        result.addAttributes(node1.getAttributes());
+        for (String attr : node2.getAttributes().keySet())
         {
-            if (node1.getAttributeCount(attr.getName()) == 0)
+            if (!node1.getAttributes().containsKey(attr))
             {
-                result.addAttribute(attr);
+                result.addAttribute(attr,
+                        HANDLER.getAttributeValue(node2, attr));
             }
         }
     }
@@ -136,14 +133,14 @@ public class OverrideCombiner extends No
      * @param child the child node (of the first node)
      * @return a child of the second node, with which a combination is possible
      */
-    protected ConfigurationNode canCombine(ConfigurationNode node1,
-            ConfigurationNode node2, ConfigurationNode child)
+    protected ImmutableNode canCombine(ImmutableNode node1,
+            ImmutableNode node2, ImmutableNode child)
     {
-        if (node2.getChildrenCount(child.getName()) == 1
-                && node1.getChildrenCount(child.getName()) == 1
+        if (HANDLER.getChildrenCount(node2, child.getNodeName()) == 1
+                && HANDLER.getChildrenCount(node1, child.getNodeName()) == 1
                 && !isListNode(child))
         {
-            return node2.getChildren(child.getName()).get(0);
+            return HANDLER.getChildren(node2, child.getNodeName()).get(0);
         }
         else
         {

Modified: commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/tree/AbstractCombinerTest.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/tree/AbstractCombinerTest.java?rev=1587007&r1=1587006&r2=1587007&view=diff
==============================================================================
--- commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/tree/AbstractCombinerTest.java (original)
+++ commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/tree/AbstractCombinerTest.java Sun Apr 13 15:56:02 2014
@@ -40,10 +40,10 @@ import org.junit.Test;
 public abstract class AbstractCombinerTest
 {
     /** Constant for the first test configuration. */
-    static File CONF1 = ConfigurationAssert.getTestFile("testcombine1.xml");
+    private static final File CONF1 = ConfigurationAssert.getTestFile("testcombine1.xml");
 
     /** Constant for the second test configuration. */
-    static File CONF2 = ConfigurationAssert.getTestFile("testcombine2.xml");
+    private static final File CONF2 = ConfigurationAssert.getTestFile("testcombine2.xml");
 
     /** The combiner to be tested. */
     protected NodeCombiner combiner;
@@ -56,7 +56,7 @@ public abstract class AbstractCombinerTe
 
     /**
      * Creates the combiner to be tested. This method is called by
-     * <code>setUp()</code>. It must be implemented in concrete sub classes.
+     * {@code setUp()}. It must be implemented in concrete sub classes.
      *
      * @return the combiner to be tested
      */
@@ -75,7 +75,7 @@ public abstract class AbstractCombinerTe
         new FileHandler(conf1).load(CONF1);
         XMLConfiguration conf2 = new XMLConfiguration();
         new FileHandler(conf2).load(CONF2);
-        ConfigurationNode cn = combiner.combine(conf1.getRootNode(), conf2
+        ImmutableNode cn = combiner.combine(conf1.getRootNode(), conf2
                 .getRootNode());
 
         BaseHierarchicalConfiguration result = new BaseHierarchicalConfiguration();
@@ -92,7 +92,6 @@ public abstract class AbstractCombinerTe
     {
         assertTrue("Combiner has list nodes", combiner.getListNodes().isEmpty());
         assertFalse("Node is list node", combiner
-                .isListNode(new DefaultConfigurationNode("test")));
+                .isListNode(NodeStructureHelper.createNode("test", null)));
     }
-
 }

Modified: commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/tree/TestOverrideCombiner.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/tree/TestOverrideCombiner.java?rev=1587007&r1=1587006&r2=1587007&view=diff
==============================================================================
--- commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/tree/TestOverrideCombiner.java (original)
+++ commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/tree/TestOverrideCombiner.java Sun Apr 13 15:56:02 2014
@@ -18,10 +18,10 @@ package org.apache.commons.configuration
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertTrue;
 
 import java.util.List;
 
+import org.apache.commons.configuration.BaseHierarchicalConfiguration;
 import org.apache.commons.configuration.HierarchicalConfiguration;
 import org.apache.commons.configuration.ex.ConfigurationException;
 import org.junit.Test;
@@ -50,7 +50,7 @@ public class TestOverrideCombiner extend
     @Test
     public void testSimpleValues() throws ConfigurationException
     {
-        HierarchicalConfiguration config = createCombinedConfiguration();
+        BaseHierarchicalConfiguration config = createCombinedConfiguration();
         assertEquals("Wrong number of bgcolors", 0, config
                 .getMaxIndex("gui.bgcolor"));
         assertEquals("Wrong bgcolor", "green", config.getString("gui.bgcolor"));
@@ -66,7 +66,7 @@ public class TestOverrideCombiner extend
     @Test
     public void testAttributes() throws ConfigurationException
     {
-        HierarchicalConfiguration config = createCombinedConfiguration();
+        BaseHierarchicalConfiguration config = createCombinedConfiguration();
         assertEquals("Wrong value of min attribute", 1, config
                 .getInt("gui.level[@min]"));
         assertEquals("Wrong value of default attribute", 2, config
@@ -83,7 +83,7 @@ public class TestOverrideCombiner extend
     @Test
     public void testOverrideValues() throws ConfigurationException
     {
-        HierarchicalConfiguration config = createCombinedConfiguration();
+        BaseHierarchicalConfiguration config = createCombinedConfiguration();
         assertEquals("Wrong user", "Admin", config
                 .getString("base.services.security.login.user"));
         assertEquals("Wrong user type", "default", config
@@ -101,7 +101,7 @@ public class TestOverrideCombiner extend
     @Test
     public void testListFromFirstStructure() throws ConfigurationException
     {
-        HierarchicalConfiguration config = createCombinedConfiguration();
+        BaseHierarchicalConfiguration config = createCombinedConfiguration();
         assertEquals("Wrong number of services", 0, config
                 .getMaxIndex("net.service.url"));
         assertEquals("Wrong service", "http://service1.org", config
@@ -117,7 +117,7 @@ public class TestOverrideCombiner extend
     @Test
     public void testListFromSecondStructure() throws ConfigurationException
     {
-        HierarchicalConfiguration config = createCombinedConfiguration();
+        BaseHierarchicalConfiguration config = createCombinedConfiguration();
         assertEquals("Wrong number of servers", 3, config
                 .getMaxIndex("net.server.url"));
         assertEquals("Wrong server", "http://testsvr.com", config
@@ -128,18 +128,16 @@ public class TestOverrideCombiner extend
      * Tests the combination of the table structure. Because the table node is
      * not declared as a list node the structures will be combined. But this
      * won't make any difference because the values in the first table override
-     * the values in the second table. Only the node for the table element will
-     * be a ViewNode.
+     * the values in the second table.
      */
     @Test
     public void testCombinedTableNoList() throws ConfigurationException
     {
-        ConfigurationNode tabNode = checkTable(createCombinedConfiguration());
-        assertTrue("Node is not a view node", tabNode instanceof ViewNode);
+        checkTable(createCombinedConfiguration());
     }
 
     /**
-     * Tests the combination of the table structure when the table node is
+     * Tests the combination of the table structure if the table node is
      * declared as a list node. In this case the first table structure
      * completely overrides the second and will be directly added to the
      * resulting structure.
@@ -148,8 +146,7 @@ public class TestOverrideCombiner extend
     public void testCombinedTableList() throws ConfigurationException
     {
         combiner.addListNode("table");
-        ConfigurationNode tabNode = checkTable(createCombinedConfiguration());
-        assertFalse("Node is a view node", tabNode instanceof ViewNode);
+        checkTable(createCombinedConfiguration());
     }
 
     /**
@@ -158,11 +155,11 @@ public class TestOverrideCombiner extend
      * @param config the config
      * @return the node for the table element
      */
-    private ConfigurationNode checkTable(HierarchicalConfiguration config)
+    private ImmutableNode checkTable(BaseHierarchicalConfiguration config)
     {
         assertEquals("Wrong number of tables", 0, config
                 .getMaxIndex("database.tables.table"));
-        HierarchicalConfiguration c = config
+        HierarchicalConfiguration<ImmutableNode> c = config
                 .configurationAt("database.tables.table");
         assertEquals("Wrong table name", "documents", c.getString("name"));
         assertEquals("Wrong number of fields", 2, c
@@ -170,9 +167,12 @@ public class TestOverrideCombiner extend
         assertEquals("Wrong field", "docname", c
                 .getString("fields.field(1).name"));
 
-        List<ConfigurationNode> nds = config.getExpressionEngine().query(config.getRootNode(),
-                "database.tables.table");
+        List<QueryResult<ImmutableNode>> nds =
+                config.getExpressionEngine().query(config.getRootNode(),
+                        "database.tables.table",
+                        config.getModel().getNodeHandler());
         assertFalse("No node found", nds.isEmpty());
-        return nds.get(0);
+        assertFalse("An attribute result", nds.get(0).isAttributeResult());
+        return nds.get(0).getNode();
     }
 }