Author: oheger
Date: Wed Oct 27 20:26:06 2010
New Revision: 1028086

URL: http://svn.apache.org/viewvc?rev=1028086&view=rev
Log:
[CONFIGURATION-424] Fixed a bug in the handling of the global section in 
HierarchicalINIConfiguration.

Modified:
    commons/proper/configuration/trunk/src/changes/changes.xml
    
commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/HierarchicalINIConfiguration.java
    
commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestHierarchicalINIConfiguration.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=1028086&r1=1028085&r2=1028086&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/changes/changes.xml (original)
+++ commons/proper/configuration/trunk/src/changes/changes.xml Wed Oct 27 
20:26:06 2010
@@ -23,6 +23,10 @@
 
   <body>
     <release version="1.7" date="in SVN" description="">
+      <action dev="oheger" type="fix" issue="CONFIGURATION-424">
+        HierarchicalINIConfiguration now works correctly with configurations
+        that contain only properties in the global section.
+      </action>
       <action dev="rgoers" type="fix" issue="CONFIGURATION-423" 
due-to="William Buckley">
         testFromClassPath() can fail when it should not because of 
inconsistent escaping of output from
         PropertiesConfiguration.getURL() and 
FileChangedReloadingStrategy.getFile().toURL().

Modified: 
commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/HierarchicalINIConfiguration.java
URL: 
http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/HierarchicalINIConfiguration.java?rev=1028086&r1=1028085&r2=1028086&view=diff
==============================================================================
--- 
commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/HierarchicalINIConfiguration.java
 (original)
+++ 
commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/HierarchicalINIConfiguration.java
 Wed Oct 27 20:26:06 2010
@@ -616,22 +616,23 @@ public class HierarchicalINIConfiguratio
     {
         Set sections = new ListOrderedSet();
         boolean globalSection = false;
+        boolean inSection = false;
 
         for (Iterator it = getRootNode().getChildren().iterator(); 
it.hasNext();)
         {
             ConfigurationNode node = (ConfigurationNode) it.next();
             if (isSectionNode(node))
             {
-                if (globalSection)
-                {
-                    sections.add(null);
-                    globalSection = false;
-                }
+                inSection = true;
                 sections.add(node.getName());
             }
             else
             {
-                globalSection = true;
+                if(!inSection && !globalSection)
+                {
+                    globalSection = true;
+                    sections.add(null);
+                }
             }
         }
 

Modified: 
commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestHierarchicalINIConfiguration.java
URL: 
http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestHierarchicalINIConfiguration.java?rev=1028086&r1=1028085&r2=1028086&view=diff
==============================================================================
--- 
commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestHierarchicalINIConfiguration.java
 (original)
+++ 
commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestHierarchicalINIConfiguration.java
 Wed Oct 27 20:26:06 2010
@@ -76,9 +76,13 @@ public class TestHierarchicalINIConfigur
             + "  line 2" + LINE_SEPARATOR
             + "continueNoLine = one \\" + LINE_SEPARATOR;
 
+    /** An ini file that contains only a property in the global section. */
+    private static final String INI_DATA_GLOBAL_ONLY = "globalVar = testGlobal"
+            + LINE_SEPARATOR + LINE_SEPARATOR;
+
     /** An ini file with a global section. */
-    private static final String INI_DATA_GLOBAL = "globalVar = testGlobal"
-            + LINE_SEPARATOR + LINE_SEPARATOR + INI_DATA;
+    private static final String INI_DATA_GLOBAL = INI_DATA_GLOBAL_ONLY
+            + INI_DATA;
 
     /** A test ini file. */
     private static final File TEST_FILE = new File("target/test.ini");
@@ -152,15 +156,36 @@ public class TestHierarchicalINIConfigur
     }
 
     /**
-     * Tests saving a configuration that contains a global section.
+     * Helper method for testing a save operation. This method constructs a
+     * configuration from the specified content string. Then it saves this
+     * configuration and checks whether the result matches the original 
content.
+     *
+     * @param content the content of the configuration
+     * @throws ConfigurationException if an error occurs
      */
-    public void testSaveWithGlobalSection() throws ConfigurationException
+    private void checkSave(String content) throws ConfigurationException
     {
-        HierarchicalINIConfiguration config = setUpConfig(INI_DATA_GLOBAL);
+        HierarchicalINIConfiguration config = setUpConfig(content);
         StringWriter writer = new StringWriter();
         config.save(writer);
-        assertEquals("Wrong content of ini file", INI_DATA_GLOBAL, writer
-                .toString());
+        assertEquals("Wrong content of ini file", content, writer.toString());
+    }
+
+    /**
+     * Tests saving a configuration that contains a global section.
+     */
+    public void testSaveWithGlobalSection() throws ConfigurationException
+    {
+        checkSave(INI_DATA_GLOBAL);
+    }
+
+    /**
+     * Tests whether a configuration that contains only a global section can be
+     * saved correctly.
+     */
+    public void testSaveWithOnlyGlobalSection() throws ConfigurationException
+    {
+        checkSave(INI_DATA_GLOBAL_ONLY);
     }
 
     /**
@@ -209,7 +234,7 @@ public class TestHierarchicalINIConfigur
     {
         writeTestFile(INI_DATA);
         HierarchicalINIConfiguration config = new HierarchicalINIConfiguration(
-                TEST_FILE.toURL());
+                TEST_FILE.toURI().toURL());
         checkContent(config);
     }
 
@@ -449,6 +474,17 @@ public class TestHierarchicalINIConfigur
     }
 
     /**
+     * Tests whether the sections of a configuration can be queried that
+     * contains only a global section.
+     */
+    public void testGetSectionsGlobalOnly() throws ConfigurationException
+    {
+        checkSectionNames(INI_DATA_GLOBAL_ONLY, new String[] {
+            null
+        });
+    }
+
+    /**
      * Tests whether variables containing a dot are not misinterpreted as
      * sections. This test is related to CONFIGURATION-327.
      */


Reply via email to