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 2011/04/20 22:07:32 UTC

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

Author: oheger
Date: Wed Apr 20 20:07:31 2011
New Revision: 1095498

URL: http://svn.apache.org/viewvc?rev=1095498&view=rev
Log:
[CONFIGURATION-446] Attributes whose value is an empty string are no more removed on saving the configuration.

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

Modified: commons/proper/configuration/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/changes/changes.xml?rev=1095498&r1=1095497&r2=1095498&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/changes/changes.xml (original)
+++ commons/proper/configuration/trunk/src/changes/changes.xml Wed Apr 20 20:07:31 2011
@@ -23,6 +23,10 @@
 
   <body>
     <release version="1.7" date="in SVN" description="">
+      <action dev="oheger" type="update" issue="CONFIGURATION-446">
+        XMLConfiguration now handles attributes correctly whose value is an
+        empty string.
+      </action>
       <action dev="oheger" type="fix" issue="CONFIGURATION-445">
         Transforming a CombinedConfiguration with ViewNodes to an
         XMLConfiguration could cause problems with attributes. This has been

Modified: commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/XMLConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/XMLConfiguration.java?rev=1095498&r1=1095497&r2=1095498&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/XMLConfiguration.java (original)
+++ commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/XMLConfiguration.java Wed Apr 20 20:07:31 2011
@@ -1498,6 +1498,7 @@ public class XMLConfiguration extends Ab
         {
             if (node != null && elem != null)
             {
+                boolean hasAttribute = false;
                 List attrs = node.getAttributes(name);
                 StringBuffer buf = new StringBuffer();
                 char delimiter = (listDelimiter != 0) ? listDelimiter : ATTR_VALUE_DELIMITER;
@@ -1506,6 +1507,7 @@ public class XMLConfiguration extends Ab
                     Node attr = (Node) it.next();
                     if (attr.getValue() != null)
                     {
+                        hasAttribute = true;
                         if (buf.length() > 0)
                         {
                             buf.append(delimiter);
@@ -1518,7 +1520,7 @@ public class XMLConfiguration extends Ab
                     attr.setReference(elem);
                 }
 
-                if (buf.length() < 1)
+                if (!hasAttribute)
                 {
                     elem.removeAttribute(name);
                 }

Modified: commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestXMLConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestXMLConfiguration.java?rev=1095498&r1=1095497&r2=1095498&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestXMLConfiguration.java (original)
+++ commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestXMLConfiguration.java Wed Apr 20 20:07:31 2011
@@ -1741,6 +1741,23 @@ public class TestXMLConfiguration extend
     }
 
     /**
+     * Tests whether an attribute can be set to an empty string. This test is
+     * related to CONFIGURATION-446.
+     */
+    public void testEmptyAttribute() throws ConfigurationException
+    {
+        String key = "element3[@value]";
+        conf.setProperty(key, "");
+        assertTrue("Key not found", conf.containsKey(key));
+        assertEquals("Wrong value", "", conf.getString(key));
+        conf.save(testSaveConf);
+        conf = new XMLConfiguration();
+        conf.load(testSaveConf);
+        assertTrue("Key not found after save", conf.containsKey(key));
+        assertEquals("Wrong value after save", "", conf.getString(key));
+    }
+
+    /**
      * Prepares a configuration object for testing a reload operation.
      *
      * @return the initialized configuration