commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ohe...@apache.org
Subject svn commit: r1153643 - 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
Date Wed, 03 Aug 2011 20:23:18 GMT
Author: oheger
Date: Wed Aug  3 20:23:17 2011
New Revision: 1153643

URL: http://svn.apache.org/viewvc?rev=1153643&view=rev
Log:
[CONFIGURATION-455] Improvements of lINIConfiguration.getSection(). Ported changes to 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=1153643&r1=1153642&r2=1153643&view=diff
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/changes/changes.xml
(original)
+++ commons/proper/configuration/branches/configuration2_experimental/src/changes/changes.xml
Wed Aug  3 20:23:17 2011
@@ -84,6 +84,11 @@
         clear() method. This is more efficient and also solves some other
         problems related to clearing a SubnodeConfiguration.
       </action>
+      <action dev="oheger" type="update" issue="CONFIGURATION-455">
+        HierarchicalINIConfiguration.getSection() now creates a section if it
+        does not exist. The SubnodeConfiguration returned by this method is now
+        always connected to the parent ini configuration.
+      </action>
       <action dev="oheger" type="add" issue="CONFIGURATION-452">
         XPathExpressionEngine now provides better support for the setProperty()
         method.

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=1153643&r1=1153642&r2=1153643&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
Wed Aug  3 20:23:17 2011
@@ -87,8 +87,10 @@ import org.apache.commons.lang3.StringUt
  * '=' to separate keys and values in parameters, for example
  * <code>var1 : foo</code>.</li>
  * <li><b>Duplicate sections:</b> Typically duplicate sections are not
allowed,
- * this configuration does however support it. In the event of a duplicate
- * section, the two section's values are merged.</li>
+ * this configuration does however support this feature. In the event of a duplicate
+ * section, the two section's values are merged so that there is only a single
+ * section. <strong>Note</strong>: This also affects the internal data of the
+ * configuration. If it is saved, only a single section is written!</li>
  * <li><b>Duplicate parameters:</b> Typically duplicate parameters are
only
  * allowed if they are in two different sections, thus they are local to
  * sections; this configuration simply merges duplicates; if a section has a
@@ -177,7 +179,10 @@ import org.apache.commons.lang3.StringUt
  * also use other methods of {@link InMemoryConfiguration} for querying or
  * manipulating the hierarchy of configuration nodes, for instance the
  * {@code configurationAt()} method for obtaining the data of a specific
- * section.
+ * section. However, be careful that the storage scheme described above is not
+ * violated (e.g. by adding multiple levels of nodes or inserting duplicate
+ * section nodes). Otherwise, the special methods for ini configurations may not
+ * work correctly!
  * </p>
  * <p>
  * The set of sections in this configuration can be retrieved using the
@@ -721,13 +726,18 @@ public class INIConfiguration extends Ab
      * Returns a configuration with the content of the specified section. This
      * provides an easy way of working with a single section only. The way this
      * configuration is structured internally, this method is very similar to
-     * calling
-     * {@link AbstractHierarchicalConfiguration#configurationAt(String)}
+     * calling {@link AbstractHierarchicalConfiguration#configurationAt(String)}
      * with the name of the section in question. There are the following
      * differences however:
      * <ul>
      * <li>This method never throws an exception. If the section does not exist,
-     * an empty configuration is returned.</li>
+     * it is created now. The configuration returned in this case is empty.</li>
+     * <li>If section is contained multiple times in the configuration, the
+     * configuration returned by this method is initialized with the first
+     * occurrence of the section. (This can only happen if {@code addProperty()}
+     * has been used in a way that does not conform to the storage scheme used
+     * by {@code INIConfiguration}. If used correctly, there will not be
+     * duplicate sections.)</li>
      * <li>There is special support for the global section: Passing in
      * <b>null</b> as section name returns a configuration with the content of
      * the global section (which may also be empty).</li>
@@ -756,7 +766,7 @@ public class INIConfiguration extends Ab
                 // the passed in key does not map to exactly one node
                 // return an empty configuration
                 return new SubConfiguration<ConfigurationNode>(this,
-                        new DefaultConfigurationNode());
+                        getSectionNode(name));
             }
         }
     }

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=1153643&r1=1153642&r2=1153643&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
Wed Aug  3 20:23:17 2011
@@ -795,4 +795,80 @@ public class TestINIConfiguration extend
         assertEquals("Wrong value for var8", "value8",
                 config.getString("section.var:8"));
     }
+
+    /**
+     * Tests whether a section that has been cleared can be manipulated and
+     * saved later.
+     */
+    public void testSaveClearedSection() throws ConfigurationException
+    {
+        final String data = "[section]\ntest = failed\n";
+        INIConfiguration config = setUpConfig(data);
+        SubConfiguration<ConfigurationNode> sub = config.getSection("section");
+        assertFalse("No content", sub.isEmpty());
+        sub.clear();
+        sub.setProperty("test", "success");
+        StringWriter writer = new StringWriter();
+        config.save(writer);
+        INIConfiguration config2 = setUpConfig(writer.toString());
+        assertEquals("Wrong value", "success",
+                config2.getString("section.test"));
+    }
+
+    /**
+     * Tests whether a duplicate session is merged.
+     */
+    public void testMergeDuplicateSection() throws ConfigurationException
+    {
+        final String data =
+                "[section]\nvar1 = sec1\n\n" + "[section]\nvar2 = sec2\n";
+        INIConfiguration config = setUpConfig(data);
+        assertEquals("Wrong value 1", "sec1", config.getString("section.var1"));
+        assertEquals("Wrong value 2", "sec2", config.getString("section.var2"));
+        SubConfiguration<ConfigurationNode> sub = config.getSection("section");
+        assertEquals("Wrong sub value 1", "sec1", sub.getString("var1"));
+        assertEquals("Wrong sub value 2", "sec2", sub.getString("var2"));
+        StringWriter writer = new StringWriter();
+        config.save(writer);
+        String content = writer.toString();
+        int pos = content.indexOf("[section]");
+        assertTrue("Section not found: " + content, pos >= 0);
+        assertTrue("Section found multiple times: " + content,
+                content.indexOf("[section]", pos + 1) < 0);
+    }
+
+    /**
+     * Tests whether a section that was created by getSection() can be
+     * manipulated.
+     */
+    public void testGetSectionNonExistingManipulate()
+            throws ConfigurationException
+    {
+        INIConfiguration config = setUpConfig(INI_DATA);
+        SubConfiguration<ConfigurationNode> section =
+                config.getSection("newSection");
+        section.addProperty("test", "success");
+        assertEquals("Main config not updated", "success",
+                config.getString("newSection.test"));
+        StringWriter writer = new StringWriter();
+        config.save(writer);
+        INIConfiguration config2 = setUpConfig(writer.toString());
+        section = config2.getSection("newSection");
+        assertEquals("Wrong value", "success", section.getString("test"));
+    }
+
+    /**
+     * Tests whether getSection() can deal with duplicate sections.
+     */
+    public void testGetSectionDuplicate()
+    {
+        INIConfiguration config = new INIConfiguration();
+        config.addProperty("section.var1", "value1");
+        config.addProperty("section(-1).var2", "value2");
+        SubConfiguration<ConfigurationNode> section =
+                config.getSection("section");
+        Iterator<String> keys = section.getKeys();
+        assertEquals("Wrong key", "var1", keys.next());
+        assertFalse("Too many keys", keys.hasNext());
+    }
 }



Mime
View raw message