You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by oh...@apache.org on 2007/04/17 21:49:52 UTC

svn commit: r529734 - in /jakarta/commons/proper/configuration/trunk: conf/test.xml src/java/org/apache/commons/configuration/XMLConfiguration.java src/test/org/apache/commons/configuration/TestXMLConfiguration.java xdocs/changes.xml

Author: oheger
Date: Tue Apr 17 12:49:51 2007
New Revision: 529734

URL: http://svn.apache.org/viewvc?view=rev&rev=529734
Log:
CONFIGURATION-263: Fix for problem with XMLConfiguration and attribute nodes when list values are involved

Modified:
    jakarta/commons/proper/configuration/trunk/conf/test.xml
    jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/XMLConfiguration.java
    jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestXMLConfiguration.java
    jakarta/commons/proper/configuration/trunk/xdocs/changes.xml

Modified: jakarta/commons/proper/configuration/trunk/conf/test.xml
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/configuration/trunk/conf/test.xml?view=diff&rev=529734&r1=529733&r2=529734
==============================================================================
--- jakarta/commons/proper/configuration/trunk/conf/test.xml (original)
+++ jakarta/commons/proper/configuration/trunk/conf/test.xml Tue Apr 17 12:49:51 2007
@@ -78,4 +78,11 @@
          empty string as value.
     -->
     <empty/>
+    
+    <!-- List nodes with attributes -->
+    <attrList>
+      <a name="x">ABC</a>
+      <a name="y">1,2,3</a>
+      <a name="u,v,w" test="yes">value1,value2</a>
+    </attrList>
 </testconfig>

Modified: jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/XMLConfiguration.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/XMLConfiguration.java?view=diff&rev=529734&r1=529733&r2=529734
==============================================================================
--- jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/XMLConfiguration.java (original)
+++ jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/XMLConfiguration.java Tue Apr 17 12:49:51 2007
@@ -477,12 +477,25 @@
 
             if (values.size() > 1)
             {
-                // remove the original child
+                Iterator it = values.iterator();
+                // Create new node for the original child's first value
+                Node c = createNode(child.getName());
+                c.setValue(it.next());
+                // Copy original attributes to the new node
+                for (Iterator itAttrs = child.getAttributes().iterator(); itAttrs
+                        .hasNext();)
+                {
+                    Node ndAttr = (Node) itAttrs.next();
+                    ndAttr.setReference(null);
+                    c.addAttribute(ndAttr);
+                }
                 parent.remove(child);
+                parent.addChild(c);
+
                 // add multiple new children
-                for (Iterator it = values.iterator(); it.hasNext();)
+                while (it.hasNext())
                 {
-                    Node c = new XMLNode(child.getName(), null);
+                    c = new XMLNode(child.getName(), null);
                     c.setValue(it.next());
                     parent.addChild(c);
                 }

Modified: jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestXMLConfiguration.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestXMLConfiguration.java?view=diff&rev=529734&r1=529733&r2=529734
==============================================================================
--- jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestXMLConfiguration.java (original)
+++ jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestXMLConfiguration.java Tue Apr 17 12:49:51 2007
@@ -963,6 +963,69 @@
     }
 
     /**
+     * Tests list nodes with multiple values and attributes.
+     */
+    public void testListWithAttributes()
+    {
+        assertEquals("Wrong number of <a> elements", 6, conf.getList(
+                "attrList.a").size());
+        assertEquals("Wrong value of first element", "ABC", conf
+                .getString("attrList.a(0)"));
+        assertEquals("Wrong value of first name attribute", "x", conf
+                .getString("attrList.a(0)[@name]"));
+        assertEquals("Wrong number of name attributes", 5, conf.getList(
+                "attrList.a[@name]").size());
+    }
+
+    /**
+     * Tests a list node with attributes that has multiple values separated by
+     * the list delimiter. In this scenario the attribute should be added to the
+     * node with the first value.
+     */
+    public void testListWithAttributesMultiValue()
+    {
+        assertEquals("Wrong value of 2nd element", "1", conf
+                .getString("attrList.a(1)"));
+        assertEquals("Wrong value of 2nd name attribute", "y", conf
+                .getString("attrList.a(1)[@name]"));
+        for (int i = 2; i <= 3; i++)
+        {
+            assertEquals("Wrong value of element " + (i + 1), i, conf
+                    .getInt("attrList.a(" + i + ")"));
+            assertFalse("element " + (i + 1) + " has attribute", conf
+                    .containsKey("attrList.a(2)[@name]"));
+        }
+    }
+
+    /**
+     * Tests a list node with a multi-value attribute and multiple values. All
+     * attribute values should be assigned to the node with the first value.
+     */
+    public void testListWithMultiAttributesMultiValue()
+    {
+        for (int i = 1; i <= 2; i++)
+        {
+            assertEquals("Wrong value of multi-valued node", "value" + i, conf
+                    .getString("attrList.a(" + (i + 3) + ")"));
+        }
+        List attrs = conf.getList("attrList.a(4)[@name]");
+        final String attrVal = "uvw";
+        assertEquals("Wrong number of name attributes", attrVal.length(), attrs
+                .size());
+        for (int i = 0; i < attrVal.length(); i++)
+        {
+            assertEquals("Wrong value for attribute " + i, String
+                    .valueOf(attrVal.charAt(i)), attrs.get(i));
+        }
+        assertEquals("Wrong value of test attribute", "yes", conf
+                .getString("attrList.a(4)[@test]"));
+        assertFalse("Name attribute for 2nd value", conf
+                .containsKey("attrList.a(5)[@name]"));
+        assertFalse("Test attribute for 2nd value", conf
+                .containsKey("attrList.a(5)[@test]"));
+    }
+
+    /**
      * Prepares a configuration object for testing a reload operation.
      *
      * @return the initialized configuration

Modified: jakarta/commons/proper/configuration/trunk/xdocs/changes.xml
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/configuration/trunk/xdocs/changes.xml?view=diff&rev=529734&r1=529733&r2=529734
==============================================================================
--- jakarta/commons/proper/configuration/trunk/xdocs/changes.xml (original)
+++ jakarta/commons/proper/configuration/trunk/xdocs/changes.xml Tue Apr 17 12:49:51 2007
@@ -23,6 +23,10 @@
 
   <body>
     <release version="1.5-SNAPSHOT" date="in SVN" description="">
+      <action dev="oheger" type="fix" issue="CONFIGURATION-263">
+        XMLConfiguration used to drop attributes when an element's value was a
+        list. This has been fixed.
+      </action>
       <action dev="ebourg" type="add" issue="CONFIGURATION-249">
         File configurations can now be saved to FTP URLs, or any other URL
         protocol supporting data output.



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org