Author: oheger
Date: Sat Jan 23 16:28:14 2010
New Revision: 902437

URL: http://svn.apache.org/viewvc?rev=902437&view=rev
Log:
[CONFIGURATION-403] Fixed XMLConfiguration.load() so that an empty 
configuration that was saved and reloaded is still considered empty. Ported 
patch from trunk to configuration2 branch.

Modified:
    
commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/XMLConfiguration.java
    
commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestXMLConfiguration.java
    
commons/proper/configuration/branches/configuration2_experimental/xdocs/changes.xml

Modified: 
commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/XMLConfiguration.java
URL: 
http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/XMLConfiguration.java?rev=902437&r1=902436&r2=902437&view=diff
==============================================================================
--- 
commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/XMLConfiguration.java
 (original)
+++ 
commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/XMLConfiguration.java
 Sat Jan 23 16:28:14 2010
@@ -32,6 +32,7 @@
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
+
 import javax.xml.parsers.DocumentBuilder;
 import javax.xml.parsers.DocumentBuilderFactory;
 import javax.xml.parsers.ParserConfigurationException;
@@ -164,7 +165,7 @@
  *
  * @author Jörg Schaible
  * @author Oliver Heger
- * @version $Revision$, $Date$
+ * @version $Id$
  */
 public class XMLConfiguration extends AbstractHierarchicalFileConfiguration
     implements EntityResolver, EntityRegistry
@@ -569,6 +570,7 @@
      * Removes all properties from this configuration. If this configuration
      * was loaded from a file, the associated DOM document is also cleared.
      */
+    @Override
     public void clear()
     {
         super.clear();
@@ -637,7 +639,7 @@
         {
             text = text.trim();
         }
-        if (text.length() > 0 || !hasChildren(node))
+        if (text.length() > 0 || (!hasChildren(node) && node != getRootNode()))
         {
             node.setValue(text);
         }
@@ -814,6 +816,7 @@
                 // register an error handler which detects validation errors
                 result.setErrorHandler(new DefaultHandler()
                 {
+                    @Override
                     public void error(SAXParseException ex) throws SAXException
                     {
                         throw ex;
@@ -913,6 +916,7 @@
      * @param in the input stream
      * @throws ConfigurationException if an error occurs
      */
+    @Override
     public void load(InputStream in) throws ConfigurationException
     {
         load(new InputSource(in));
@@ -962,7 +966,7 @@
         }
         catch (Exception e)
         {
-            this.getLogger().debug("Unable to load the configuraton", e);      
     
+            this.getLogger().debug("Unable to load the configuraton", e);
             throw new ConfigurationException("Unable to load the 
configuration", e);
         }
     }
@@ -1070,6 +1074,7 @@
      *
      * @return the copy
      */
+    @Override
     public Object clone()
     {
         XMLConfiguration copy = (XMLConfiguration) super.clone();
@@ -1199,6 +1204,7 @@
      * @since 1.5
      * @deprecated Use getEntityResolver().resolveEntity()
      */
+    @Deprecated
     public InputSource resolveEntity(String publicId, String systemId)
             throws SAXException
     {

Modified: 
commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestXMLConfiguration.java
URL: 
http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestXMLConfiguration.java?rev=902437&r1=902436&r2=902437&view=diff
==============================================================================
--- 
commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestXMLConfiguration.java
 (original)
+++ 
commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestXMLConfiguration.java
 Sat Jan 23 16:28:14 2010
@@ -936,6 +936,18 @@
     }
 
     /**
+     * Tests the isEmpty() method for an empty configuration that was reloaded.
+     */
+    public void testEmptyReload() throws ConfigurationException
+    {
+        XMLConfiguration config = new XMLConfiguration();
+        assertTrue("Newly created configuration not empty", config.isEmpty());
+        config.save(testSaveConf);
+        config.load(testSaveConf);
+        assertTrue("Reloaded configuration not empty", config.isEmpty());
+    }
+
+    /**
      * Tests whether the encoding is correctly detected by the XML parser. This
      * is done by loading an XML file with the encoding "UTF-16". If this
      * encoding is not detected correctly, an exception will be thrown that

Modified: 
commons/proper/configuration/branches/configuration2_experimental/xdocs/changes.xml
URL: 
http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/xdocs/changes.xml?rev=902437&r1=902436&r2=902437&view=diff
==============================================================================
--- 
commons/proper/configuration/branches/configuration2_experimental/xdocs/changes.xml
 (original)
+++ 
commons/proper/configuration/branches/configuration2_experimental/xdocs/changes.xml
 Sat Jan 23 16:28:14 2010
@@ -79,6 +79,11 @@
     </release>
 
     <release version="1.7" date="in SVN" description="">
+      <action dev="oheger" type="fix" issue="CONFIGURATION-403">
+        When an empty XMLConfiguration was saved and reloaded the root element
+        was assigned an empty text value. Because of this isEmpty() returned
+        false for this configuration. This has been fixed.
+      </action>
       <action dev="oheger" type="add" issue="CONFIGURATION-399">
         Default variable interpolation now supports the env: prefix for
         referencing environment variables.


Reply via email to