commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ohe...@apache.org
Subject svn commit: r1739471 - in /commons/proper/configuration/trunk/src: main/java/org/apache/commons/configuration2/INIConfiguration.java test/java/org/apache/commons/configuration2/TestINIConfiguration.java
Date Sat, 16 Apr 2016 15:02:02 GMT
Author: oheger
Date: Sat Apr 16 15:02:02 2016
New Revision: 1739471

URL: http://svn.apache.org/viewvc?rev=1739471&view=rev
Log:
[CONFIGURATION-622] Fixed handling of escaped keys in INIConfiguration.write.

Separator characters contained in keys were duplicated when the
configuration was saved.

Modified:
    commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/INIConfiguration.java
    commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/TestINIConfiguration.java

Modified: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/INIConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/INIConfiguration.java?rev=1739471&r1=1739470&r2=1739471&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/INIConfiguration.java
(original)
+++ commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/INIConfiguration.java
Sat Apr 16 15:02:02 2016
@@ -23,7 +23,6 @@ import java.io.Reader;
 import java.io.Writer;
 import java.util.ArrayList;
 import java.util.Collection;
-import java.util.Iterator;
 import java.util.LinkedHashMap;
 import java.util.LinkedHashSet;
 import java.util.List;
@@ -280,47 +279,44 @@ public class INIConfiguration extends Ba
     public void write(Writer writer) throws ConfigurationException, IOException
     {
         PrintWriter out = new PrintWriter(writer);
-        for (String section : getSections())
-        {
-            if (section != null)
-            {
-                out.print("[");
-                out.print(section);
-                out.print("]");
-                out.println();
-            }
-            Configuration subset = getSection(section);
+        boolean first = true;
 
-            Iterator<String> keys = subset.getKeys();
-            while (keys.hasNext())
+        beginRead(false);
+        try
+        {
+            for (ImmutableNode node : getModel().getNodeHandler().getRootNode()
+                    .getChildren())
             {
-                String key = keys.next();
-                Object value = subset.getProperty(key);
-                if (value instanceof Collection)
+                if (isSectionNode(node))
                 {
-                    Iterator<?> values = ((Collection<?>) value).iterator();
-                    while (values.hasNext())
+                    if (!first)
                     {
-                        value = values.next();
-                        out.print(key);
-                        out.print(" = ");
-                        out.print(escapeValue(value.toString()));
                         out.println();
                     }
+                    out.print("[");
+                    out.print(node.getNodeName());
+                    out.print("]");
+                    out.println();
+
+                    for (ImmutableNode child : node.getChildren())
+                    {
+                        writeProperty(out, child.getNodeName(),
+                                child.getValue());
+                    }
                 }
                 else
                 {
-                    out.print(key);
-                    out.print(" = ");
-                    out.print(escapeValue(value.toString()));
-                    out.println();
+                    writeProperty(out, node.getNodeName(), node.getValue());
                 }
+                first = false;
             }
-
             out.println();
+            out.flush();
+        }
+        finally
+        {
+            endRead();
         }
-
-        out.flush();
     }
 
     /**
@@ -449,6 +445,21 @@ public class INIConfiguration extends Ba
     }
 
     /**
+     * Writes data about a property into the given stream.
+     *
+     * @param out the output stream
+     * @param key the key
+     * @param value the value
+     */
+    private void writeProperty(PrintWriter out, String key, Object value)
+    {
+        out.print(key);
+        out.print(" = ");
+        out.print(escapeValue(value.toString()));
+        out.println();
+    }
+
+    /**
      * Parse the value to remove the quotes and ignoring the comment. Example:
      *
      * <pre>

Modified: commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/TestINIConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/TestINIConfiguration.java?rev=1739471&r1=1739470&r2=1739471&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/TestINIConfiguration.java
(original)
+++ commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/TestINIConfiguration.java
Sat Apr 16 15:02:02 2016
@@ -17,8 +17,10 @@
 
 package org.apache.commons.configuration2;
 
+import static org.hamcrest.CoreMatchers.containsString;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertThat;
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 
@@ -831,6 +833,23 @@ public class TestINIConfiguration
     }
 
     /**
+     * Tests that loading and saving a configuration that contains keys with
+     * delimiter characters works correctly. This test is related to
+     * CONFIGURATION-622.
+     */
+    @Test
+    public void testPropertyWithDelimiter() throws ConfigurationException
+    {
+        String data = INI_DATA + "key.dot = dotValue";
+        INIConfiguration conf = new INIConfiguration();
+        load(conf, data);
+        assertEquals("Wrong property value", "dotValue",
+                conf.getString("section3.key..dot"));
+        String output = saveToString(conf);
+        assertThat(output, containsString("key.dot = dotValue"));
+    }
+
+    /**
      * Tests whether a value which contains a semicolon can be loaded
      * successfully. This test is related to CONFIGURATION-434.
      */



Mime
View raw message