commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ohe...@apache.org
Subject svn commit: r685103 - in /commons/proper/configuration/trunk: src/java/org/apache/commons/configuration/HierarchicalConfiguration.java src/test/org/apache/commons/configuration/TestHierarchicalConfiguration.java xdocs/changes.xml
Date Tue, 12 Aug 2008 09:34:59 GMT
Author: oheger
Date: Tue Aug 12 02:34:57 2008
New Revision: 685103

URL: http://svn.apache.org/viewvc?rev=685103&view=rev
Log:
CONFIGURATION-334: Made handling of parent nodes more consistent.

Modified:
    commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/HierarchicalConfiguration.java
    commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestHierarchicalConfiguration.java
    commons/proper/configuration/trunk/xdocs/changes.xml

Modified: commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/HierarchicalConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/HierarchicalConfiguration.java?rev=685103&r1=685102&r2=685103&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/HierarchicalConfiguration.java
(original)
+++ commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/HierarchicalConfiguration.java
Tue Aug 12 02:34:57 2008
@@ -26,12 +26,11 @@
 import java.util.Set;
 import java.util.Stack;
 
-import org.apache.commons.collections.set.ListOrderedSet;
 import org.apache.commons.collections.iterators.SingletonIterator;
+import org.apache.commons.collections.set.ListOrderedSet;
 import org.apache.commons.configuration.event.ConfigurationEvent;
 import org.apache.commons.configuration.event.ConfigurationListener;
 import org.apache.commons.configuration.tree.ConfigurationNode;
-import org.apache.commons.configuration.tree.ConfigurationNodeVisitor;
 import org.apache.commons.configuration.tree.ConfigurationNodeVisitorAdapter;
 import org.apache.commons.configuration.tree.DefaultConfigurationNode;
 import org.apache.commons.configuration.tree.DefaultExpressionEngine;
@@ -203,6 +202,12 @@
      */
     public Node getRoot()
     {
+        if (root == null && rootNode != null)
+        {
+            // Dynamically create a snapshot of the root node
+            return new Node(rootNode);
+        }
+
         return root;
     }
 
@@ -250,7 +255,7 @@
         this.rootNode = rootNode;
 
         // For backward compatibility also set the old root field.
-        root = (rootNode instanceof Node) ? (Node) rootNode : new Node(rootNode);
+        root = (rootNode instanceof Node) ? (Node) rootNode : null;
     }
 
     /**
@@ -1203,11 +1208,20 @@
             setReference(src.getReference());
             for (Iterator it = src.getChildren().iterator(); it.hasNext();)
             {
-                addChild((ConfigurationNode) it.next());
+                ConfigurationNode nd = (ConfigurationNode) it.next();
+                // Don't change the parent node
+                ConfigurationNode parent = nd.getParentNode();
+                addChild(nd);
+                nd.setParentNode(parent);
             }
+
             for (Iterator it = src.getAttributes().iterator(); it.hasNext();)
             {
-                addAttribute((ConfigurationNode) it.next());
+                ConfigurationNode nd = (ConfigurationNode) it.next();
+                // Don't change the parent node
+                ConfigurationNode parent = nd.getParentNode();
+                addAttribute(nd);
+                nd.setParentNode(parent);
             }
         }
 

Modified: commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestHierarchicalConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestHierarchicalConfiguration.java?rev=685103&r1=685102&r2=685103&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestHierarchicalConfiguration.java
(original)
+++ commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestHierarchicalConfiguration.java
Tue Aug 12 02:34:57 2008
@@ -24,6 +24,8 @@
 import java.util.List;
 import java.util.Set;
 
+import junit.framework.TestCase;
+
 import org.apache.commons.collections.CollectionUtils;
 import org.apache.commons.configuration.HierarchicalConfiguration.Node;
 import org.apache.commons.configuration.event.ConfigurationEvent;
@@ -33,8 +35,6 @@
 import org.apache.commons.configuration.tree.DefaultExpressionEngine;
 import org.apache.commons.configuration.tree.ExpressionEngine;
 
-import junit.framework.TestCase;
-
 /**
  * Test class for HierarchicalConfiguration.
  *
@@ -921,6 +921,43 @@
 	}
 
 	/**
+     * Tests the parents of nodes when setRootNode() is involved. This is
+     * related to CONFIGURATION-334.
+     */
+    public void testNodeParentsAfterSetRootNode()
+    {
+        DefaultConfigurationNode root = new DefaultConfigurationNode();
+        DefaultConfigurationNode child1 = new DefaultConfigurationNode(
+                "child1", "test1");
+        root.addChild(child1);
+        config.setRootNode(root);
+        config.addProperty("child2", "test2");
+        List nodes = config.getExpressionEngine().query(config.getRootNode(),
+                "child2");
+        assertEquals("Wrong number of result nodes", 1, nodes.size());
+        ConfigurationNode child2 = (ConfigurationNode) nodes.get(0);
+        assertEquals("Different parent nodes", child1.getParentNode(), child2
+                .getParentNode());
+    }
+
+    /**
+     * Tests calling getRoot() after a root node was set using setRootNode() and
+     * further child nodes have been added. The newly add child nodes should be
+     * present in the root node returned.
+     */
+    public void testGetRootAfterSetRootNode()
+    {
+        DefaultConfigurationNode root = new DefaultConfigurationNode();
+        DefaultConfigurationNode child1 = new DefaultConfigurationNode(
+                "child1", "test1");
+        root.addChild(child1);
+        config.setRootNode(root);
+        config.addProperty("child2", "test2");
+        ConfigurationNode oldRoot = config.getRoot();
+        assertEquals("Wrong number of children", 2, oldRoot.getChildrenCount());
+    }
+
+	/**
      * Helper method for testing the getKeys(String) method.
      *
      * @param prefix the key to pass into getKeys()

Modified: commons/proper/configuration/trunk/xdocs/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/xdocs/changes.xml?rev=685103&r1=685102&r2=685103&view=diff
==============================================================================
--- commons/proper/configuration/trunk/xdocs/changes.xml (original)
+++ commons/proper/configuration/trunk/xdocs/changes.xml Tue Aug 12 02:34:57 2008
@@ -23,6 +23,10 @@
 
   <body>
     <release version="1.6" date="in SVN" description="">
+      <action dev="oheger" type="fix" issue="CONFIGURATION-334">
+        Made handling of parent nodes more consistent when setRoot() or
+        setRootNode() of HierarchicalConfiguration are involved.
+      </action>
       <action dev="oheger" type="fix" issue="CONFIGURATION-332">
         Properties written through a DataConfiguration to a wrapped
         PropertiesConfiguration got lost when the PropertiesConfiguration was



Mime
View raw message