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 2010/10/28 21:35:46 UTC

svn commit: r1028445 - in /commons/proper/configuration/branches/configuration2_experimental/src: changes/changes.xml main/java/org/apache/commons/configuration2/INIConfiguration.java test/java/org/apache/commons/configuration2/TestINIConfiguration.java

Author: oheger
Date: Thu Oct 28 19:35:45 2010
New Revision: 1028445

URL: http://svn.apache.org/viewvc?rev=1028445&view=rev
Log:
[CONFIGURATION-424] Fixed a bug in the handling of the global section in INIConfiguration. Ported fix from trunk to configuration2 branch.

Modified:
    commons/proper/configuration/branches/configuration2_experimental/src/changes/changes.xml
    commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/INIConfiguration.java
    commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestINIConfiguration.java

Modified: commons/proper/configuration/branches/configuration2_experimental/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/changes/changes.xml?rev=1028445&r1=1028444&r2=1028445&view=diff
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/changes/changes.xml (original)
+++ commons/proper/configuration/branches/configuration2_experimental/src/changes/changes.xml Thu Oct 28 19:35:45 2010
@@ -79,6 +79,10 @@
     </release>
 
     <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@intuit.com">
         testFromClassPath() can fail when it should not because of inconsistent escaping of output from
         PropertiesConfiguration.getURL() and FileChangedReloadingStrategy.getFile().toURL().

Modified: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/INIConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/INIConfiguration.java?rev=1028445&r1=1028444&r2=1028445&view=diff
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/INIConfiguration.java (original)
+++ commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/INIConfiguration.java Thu Oct 28 19:35:45 2010
@@ -600,21 +600,22 @@ public class INIConfiguration extends Ab
     {
         Set<String> sections = new LinkedHashSet<String>();
         boolean globalSection = false;
+        boolean inSection = false;
 
         for (ConfigurationNode node : getRootNode().getChildren())
         {
             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/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestINIConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestINIConfiguration.java?rev=1028445&r1=1028444&r2=1028445&view=diff
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestINIConfiguration.java (original)
+++ commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestINIConfiguration.java Thu Oct 28 19:35:45 2010
@@ -80,9 +80,13 @@ public class TestINIConfiguration extend
             + "  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 = ConfigurationAssert.getOutFile("test.ini");
@@ -157,15 +161,36 @@ public class TestINIConfiguration extend
     }
 
     /**
-     * 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
     {
-        INIConfiguration config = setUpConfig(INI_DATA_GLOBAL);
+        INIConfiguration 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);
     }
 
     /**
@@ -407,7 +432,7 @@ public class TestINIConfiguration extend
      * @param expected an array with the expected sections
      */
     private void checkSectionNames(INIConfiguration config,
-            String[] expected)
+            String... expected)
     {
         Set<String> sectionNames = config.getSections();
         Iterator<String> it = sectionNames.iterator();
@@ -426,7 +451,7 @@ public class TestINIConfiguration extend
      * @return the configuration instance
      */
     private INIConfiguration checkSectionNames(String data,
-            String[] expected) throws ConfigurationException
+            String... expected) throws ConfigurationException
     {
         INIConfiguration config = setUpConfig(data);
         checkSectionNames(config, expected);
@@ -438,9 +463,8 @@ public class TestINIConfiguration extend
      */
     public void testGetSectionsWithGlobal() throws ConfigurationException
     {
-        checkSectionNames(INI_DATA_GLOBAL, new String[] {
-                null, "section1", "section2", "section3"
-        });
+        checkSectionNames(INI_DATA_GLOBAL, null, "section1", "section2",
+                "section3");
     }
 
     /**
@@ -448,9 +472,16 @@ public class TestINIConfiguration extend
      */
     public void testGetSectionsNoGlobal() throws ConfigurationException
     {
-        checkSectionNames(INI_DATA, new String[] {
-                "section1", "section2", "section3"
-        });
+        checkSectionNames(INI_DATA, "section1", "section2", "section3");
+    }
+
+    /**
+     * 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, (String) null);
     }
 
     /**
@@ -460,12 +491,11 @@ public class TestINIConfiguration extend
     public void testGetSectionsDottedVar() throws ConfigurationException
     {
         final String data = "dotted.var = 1" + LINE_SEPARATOR + INI_DATA_GLOBAL;
-        INIConfiguration config = checkSectionNames(data,
-                new String[] {
-                        null, "section1", "section2", "section3"
-                });
-        assertEquals("Wrong value of dotted variable", 1, config
-                .getInt("dotted..var"));
+        INIConfiguration config =
+                checkSectionNames(data, null, "section1", "section2",
+                        "section3");
+        assertEquals("Wrong value of dotted variable", 1,
+                config.getInt("dotted..var"));
     }
 
     /**
@@ -475,9 +505,7 @@ public class TestINIConfiguration extend
     {
         INIConfiguration config = setUpConfig(INI_DATA2);
         config.addProperty("section5.test", Boolean.TRUE);
-        checkSectionNames(config, new String[] {
-                "section4", "section5"
-        });
+        checkSectionNames(config, "section4", "section5");
     }
 
     /**