commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ohe...@apache.org
Subject svn commit: r575786 - 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 Fri, 14 Sep 2007 20:20:36 GMT
Author: oheger
Date: Fri Sep 14 13:20:35 2007
New Revision: 575786

URL: http://svn.apache.org/viewvc?rev=575786&view=rev
Log:
CONFIGURATION-295: HierarchicalConfiguration.subset() now sets the value of the returned configuration's
root note if it can be determined

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=575786&r1=575785&r2=575786&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
Fri Sep 14 13:20:35 2007
@@ -458,7 +458,16 @@
      * Creates a new <code>Configuration</code> object containing all keys
      * that start with the specified prefix. This implementation will return a
      * <code>HierarchicalConfiguration</code> object so that the structure of
-     * the keys will be saved.
+     * the keys will be saved. The nodes selected by the prefix (it is possible
+     * that multiple nodes are selected) are mapped to the root node of the
+     * returned configuration, i.e. their children and attributes will become
+     * children and attributes of the new root node. However a value of the root
+     * node is only set if exactly one of the selected nodes contain a value (if
+     * multiple nodes have a value, there is simply no way to decide how these
+     * values are merged together). Note that the returned
+     * <code>Configuration</code> object is not connected to its source
+     * configuration: updates on the source configuration are not reflected in
+     * the subset and vice versa.
      *
      * @param prefix the prefix of the keys for the subset
      * @return a new configuration object representing the selected subset
@@ -482,21 +491,37 @@
         };
         CloneVisitor visitor = new CloneVisitor();
 
+        // Initialize the new root node
+        Object value = null;
+        int valueCount = 0;
         for (Iterator it = nodes.iterator(); it.hasNext();)
         {
             ConfigurationNode nd = (ConfigurationNode) it.next();
+            if (nd.getValue() != null)
+            {
+                value = nd.getValue();
+                valueCount++;
+            }
             nd.visit(visitor);
 
-            for (Iterator it2 = visitor.getClone().getChildren().iterator(); it2.hasNext();)
+            for (Iterator it2 = visitor.getClone().getChildren().iterator(); it2
+                    .hasNext();)
             {
                 result.getRootNode().addChild((ConfigurationNode) it2.next());
             }
-            for (Iterator it2 = visitor.getClone().getAttributes().iterator(); it2.hasNext();)
+            for (Iterator it2 = visitor.getClone().getAttributes().iterator(); it2
+                    .hasNext();)
             {
-                result.getRootNode().addAttribute((ConfigurationNode) it2.next());
+                result.getRootNode().addAttribute(
+                        (ConfigurationNode) it2.next());
             }
         }
 
+        // Determine the value of the new root
+        if (valueCount == 1)
+        {
+            result.getRootNode().setValue(value);
+        }
         return (result.isEmpty()) ? new HierarchicalConfiguration() : result;
     }
 

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=575786&r1=575785&r2=575786&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
Fri Sep 14 13:20:35 2007
@@ -457,9 +457,38 @@
 
         assertEquals(fields[0][0], subset.getProperty("name(0)"));
 
-        // tset the subset on the field names
+        // test the subset on the field names
         subset = config.subset("tables.table.fields.field.name");
         assertTrue("subset is not empty", subset.isEmpty());
+    }
+
+    /**
+     * Tests the subset() method when the specified node has a value. This value
+     * must be available in the subset, too. Related to CONFIGURATION-295.
+     */
+    public void testSubsetNodeWithValue()
+    {
+        config.setProperty("tables.table(0).fields", "My fields");
+        Configuration subset = config.subset("tables.table(0).fields");
+        assertEquals("Wrong field name", fields[0][0], subset
+                .getString("field(0).name"));
+        assertEquals("Wrong value of root", "My fields", subset.getString(""));
+    }
+
+    /**
+     * Tests the subset() method when the specified key selects multiple keys.
+     * The resulting root node should have a value only if exactly one of the
+     * selected nodes has a value. Related to CONFIGURATION-295.
+     */
+    public void testSubsetMultipleNodesWithValues()
+    {
+        config.setProperty("tables.table(0).fields", "My fields");
+        Configuration subset = config.subset("tables.table.fields");
+        assertEquals("Wrong value of root", "My fields", subset.getString(""));
+        config.setProperty("tables.table(1).fields", "My other fields");
+        subset = config.subset("tables.table.fields");
+        assertNull("Root value is not null though there are multiple values",
+                subset.getString(""));
     }
 
     /**

Modified: commons/proper/configuration/trunk/xdocs/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/xdocs/changes.xml?rev=575786&r1=575785&r2=575786&view=diff
==============================================================================
--- commons/proper/configuration/trunk/xdocs/changes.xml (original)
+++ commons/proper/configuration/trunk/xdocs/changes.xml Fri Sep 14 13:20:35 2007
@@ -23,6 +23,10 @@
 
   <body>
     <release version="1.5-SNAPSHOT" date="in SVN" description="">
+      <action dev="oheger" type="fix" issue="CONFIGURATION-295">
+        The subset() method of HierarchicalConfiguration now takes the value of
+        the subset's root node into account if it is not ambigous.
+      </action>
       <action dev="oheger" type="fix" issue="CONFIGURATION-294">
         Nodes added to a XMLConfiguration using the addNodes() method could
         lose their value when the configuration was saved. This is now fixed.



Mime
View raw message