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 2013/07/13 21:19:36 UTC

svn commit: r1502837 - in /commons/proper/configuration/trunk/src: main/java/org/apache/commons/configuration/INIConfiguration.java test/java/org/apache/commons/configuration/TestINIConfiguration.java

Author: oheger
Date: Sat Jul 13 19:19:35 2013
New Revision: 1502837

URL: http://svn.apache.org/r1502837
Log:
Integrated ListDelimiterHandler with INIConfiguration.

There was a bug that list delimiter characters in property values were not
correctly escaped when saving an INIConfiguration. For this problem test
cases were added to verify that it is fixed after this change.

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

Modified: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/INIConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/INIConfiguration.java?rev=1502837&r1=1502836&r2=1502837&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/INIConfiguration.java (original)
+++ commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/INIConfiguration.java Sat Jul 13 19:19:35 2013
@@ -301,7 +301,7 @@ public class INIConfiguration extends Ba
                         value = values.next();
                         out.print(key);
                         out.print(" = ");
-                        out.print(formatValue(value.toString()));
+                        out.print(escapeValue(value.toString()));
                         out.println();
                     }
                 }
@@ -309,7 +309,7 @@ public class INIConfiguration extends Ba
                 {
                     out.print(key);
                     out.print(" = ");
-                    out.print(formatValue(value.toString()));
+                    out.print(escapeValue(value.toString()));
                     out.println();
                 }
             }
@@ -396,7 +396,7 @@ public class INIConfiguration extends Ba
         }
         else
         {
-            values = PropertyConverter.split(value, getListDelimiter(), false);
+            values = getListDelimiterHandler().split(value, false);
         }
 
         for (String v : values)
@@ -654,9 +654,25 @@ public class INIConfiguration extends Ba
     }
 
     /**
-     * Add quotes around the specified value if it contains a comment character.
+     * Escapes the given property value before it is written. This method add
+     * quotes around the specified value if it contains a comment character and
+     * handles list delimiter characters.
+     *
+     * @param value the string to be escaped
+     */
+    private String escapeValue(String value)
+    {
+        return String.valueOf(getListDelimiterHandler().escape(
+                escapeComments(value), ListDelimiterHandler.NOOP_TRANSFORMER));
+    }
+
+    /**
+     * Escapes comment characters in the given value.
+     *
+     * @param value the value to be escaped
+     * @return the value with comment characters escaped
      */
-    private String formatValue(String value)
+    private static String escapeComments(String value)
     {
         boolean quoted = false;
 

Modified: commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestINIConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestINIConfiguration.java?rev=1502837&r1=1502836&r2=1502837&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestINIConfiguration.java (original)
+++ commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestINIConfiguration.java Sat Jul 13 19:19:35 2013
@@ -122,6 +122,7 @@ public class TestINIConfiguration
             throws ConfigurationException
     {
         INIConfiguration instance = new INIConfiguration();
+        instance.setListDelimiterHandler(new DefaultListDelimiterHandler(','));
         load(instance, data);
         return instance;
     }
@@ -149,6 +150,29 @@ public class TestINIConfiguration
     }
 
     /**
+     * Saves the specified configuration to a string. The string can be compared
+     * with an expected value or again loaded into a configuration.
+     *
+     * @param config the configuration to be saved
+     * @return the content of this configuration saved to a string
+     * @throws ConfigurationException if an error occurs
+     */
+    private static String saveToString(INIConfiguration config)
+            throws ConfigurationException
+    {
+        StringWriter writer = new StringWriter();
+        try
+        {
+            config.write(writer);
+        }
+        catch (IOException e)
+        {
+            throw new ConfigurationException(e);
+        }
+        return writer.toString();
+    }
+
+    /**
      * Writes a test ini file.
      *
      * @param content the content of the file
@@ -202,16 +226,8 @@ public class TestINIConfiguration
     private void checkSave(String content) throws ConfigurationException
     {
         INIConfiguration config = setUpConfig(content);
-        StringWriter writer = new StringWriter();
-        try
-        {
-            config.write(writer);
-        }
-        catch (IOException e)
-        {
-            throw new ConfigurationException(e);
-        }
-        assertEquals("Wrong content of ini file", content, writer.toString());
+        String sOutput = saveToString(config);
+        assertEquals("Wrong content of ini file", content, sOutput);
     }
 
     /**
@@ -234,6 +250,27 @@ public class TestINIConfiguration
     }
 
     /**
+     * Tests whether list delimiter parsing can be disabled.
+     */
+    @Test
+    public void testSaveWithDelimiterParsingDisabled()
+            throws ConfigurationException
+    {
+        INIConfiguration config = new INIConfiguration();
+        String data =
+                INI_DATA.substring(0, INI_DATA.length() - 2)
+                        + "nolist = 1,2, 3";
+        load(config, data);
+        assertEquals("Wrong property value", "1,2, 3",
+                config.getString("section3.nolist"));
+        String content = saveToString(config);
+        INIConfiguration config2 = new INIConfiguration();
+        load(config2, content);
+        assertEquals("Wrong property value after reload", "1,2, 3",
+                config2.getString("section3.nolist"));
+    }
+
+    /**
      * Test of load method, of class {@link INIConfiguration}.
      */
     @Test
@@ -1002,6 +1039,42 @@ public class TestINIConfiguration
     }
 
     /**
+     * Tests whether the configuration deals correctly with list delimiters.
+     */
+    @Test
+    public void testListDelimiterHandling() throws ConfigurationException
+    {
+        INIConfiguration config = new INIConfiguration();
+        config.setListDelimiterHandler(new DefaultListDelimiterHandler(','));
+        config.addProperty("list", "a,b,c");
+        config.addProperty("listesc", "3\\,1415");
+        String output = saveToString(config);
+        INIConfiguration config2 = setUpConfig(output);
+        assertEquals("Wrong list size", 3, config2.getList("list").size());
+        assertEquals("Wrong list element", "b", config2.getList("list").get(1));
+        assertEquals("Wrong escaped list element", "3,1415",
+                config2.getString("listesc"));
+    }
+
+    /**
+     * Tests whether property values are correctly escaped even if they are part
+     * of a property with multiple values.
+     */
+    @Test
+    public void testListDelimiterHandlingInList() throws ConfigurationException
+    {
+        String data =
+                INI_DATA + "[sectest]" + LINE_SEPARATOR
+                        + "list = 3\\,1415,pi,\\\\Test\\,5" + LINE_SEPARATOR;
+        INIConfiguration config = setUpConfig(data);
+        INIConfiguration config2 = setUpConfig(saveToString(config));
+        List<Object> list = config2.getList("sectest.list");
+        assertEquals("Wrong number of values", 3, list.size());
+        assertEquals("Wrong element 1", "3,1415", list.get(0));
+        assertEquals("Wrong element 3", "\\Test,5", list.get(2));
+    }
+
+    /**
      * A thread class for testing concurrent access to the global section.
      */
     private static class GlobalSectionTestThread extends Thread