Author: ebourg Date: Thu May 24 04:31:25 2007 New Revision: 541270 URL: http://svn.apache.org/viewvc?view=rev&rev=541270 Log: Fixed PropertiesConfiguration.save() to avoid escaping the list delimiter if it has been disabled (CONFIGURATION-269)
Added: jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/ConfigurationAssert.java Modified: jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/PropertiesConfiguration.java jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/PropertiesConfigurationLayout.java jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestPropertiesConfiguration.java jakarta/commons/proper/configuration/trunk/xdocs/changes.xml Modified: jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/PropertiesConfiguration.java URL: http://svn.apache.org/viewvc/jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/PropertiesConfiguration.java?view=diff&rev=541270&r1=541269&r2=541270 ============================================================================== --- jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/PropertiesConfiguration.java (original) +++ jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/PropertiesConfiguration.java Thu May 24 04:31:25 2007 @@ -930,9 +930,12 @@ */ private String escapeValue(Object value) { - String v = StringEscapeUtils.escapeJava(String.valueOf(value)); - return StringUtils.replace(v, String.valueOf(delimiter), "\\" - + delimiter); + String escapedValue = StringEscapeUtils.escapeJava(String.valueOf(value)); + if (delimiter != 0) + { + escapedValue = StringUtils.replace(escapedValue, String.valueOf(delimiter), "\\" + delimiter); + } + return escapedValue; } /** Modified: jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/PropertiesConfigurationLayout.java URL: http://svn.apache.org/viewvc/jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/PropertiesConfigurationLayout.java?view=diff&rev=541270&r1=541269&r2=541270 ============================================================================== --- jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/PropertiesConfigurationLayout.java (original) +++ jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/PropertiesConfigurationLayout.java Thu May 24 04:31:25 2007 @@ -348,7 +348,8 @@ * Sets the "force single line" flag. If this flag is set, all * properties with multiple values are written on single lines. This mode * provides more compatibility with <code>java.lang.Properties</code>, - * which cannot deal with multiple definitions of a single property. + * which cannot deal with multiple definitions of a single property. This + * mode has no effect if the list delimiter parsing is disabled. * * @param f the force single line flag */ @@ -442,8 +443,8 @@ { try { - PropertiesConfiguration.PropertiesWriter writer = new PropertiesConfiguration.PropertiesWriter( - out, getConfiguration().getListDelimiter()); + char delimiter = getConfiguration().isDelimiterParsingDisabled() ? 0 : getConfiguration().getListDelimiter(); + PropertiesConfiguration.PropertiesWriter writer = new PropertiesConfiguration.PropertiesWriter(out, delimiter); if (headerComment != null) { writer.writeln(getCanonicalHeaderComment(true)); @@ -469,8 +470,8 @@ } // Output the property and its value - writer.writeProperty(key, getConfiguration().getProperty( - key), isForceSingleLine() || isSingleLine(key)); + boolean singleLine = (isForceSingleLine() || isSingleLine(key)) && !getConfiguration().isDelimiterParsingDisabled(); + writer.writeProperty(key, getConfiguration().getProperty(key), singleLine); } } writer.flush(); Added: jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/ConfigurationAssert.java URL: http://svn.apache.org/viewvc/jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/ConfigurationAssert.java?view=auto&rev=541270 ============================================================================== --- jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/ConfigurationAssert.java (added) +++ jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/ConfigurationAssert.java Thu May 24 04:31:25 2007 @@ -0,0 +1,49 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.commons.configuration; + +import java.util.Iterator; + +import junit.framework.Assert; + +/** + * Assertions on configurations for the unit tests. + * + * @author Emmanuel Bourg + * @version $Revision$, $Date$ + */ +public class ConfigurationAssert +{ + public static void assertEquals(Configuration expected, Configuration actual) + { + // check that the actual configuration contains all the properties of the expected configuration + for (Iterator it = expected.getKeys(); it.hasNext();) + { + String key = (String) it.next(); + Assert.assertTrue("The actual configuration doesn't contain the expected key '" + key + "'", actual.containsKey(key)); + Assert.assertEquals("Value of the '" + key + "' property", expected.getProperty(key), actual.getProperty(key)); + } + + // check that the actual configuration has no extra properties + for (Iterator it = actual.getKeys(); it.hasNext();) + { + String key = (String) it.next(); + Assert.assertTrue("The actual configuration contains an extra key '" + key + "'", expected.containsKey(key)); + } + } +} Modified: jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestPropertiesConfiguration.java URL: http://svn.apache.org/viewvc/jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestPropertiesConfiguration.java?view=diff&rev=541270&r1=541269&r2=541270 ============================================================================== --- jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestPropertiesConfiguration.java (original) +++ jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestPropertiesConfiguration.java Thu May 24 04:31:25 2007 @@ -160,12 +160,7 @@ // read the configuration and compare the properties PropertiesConfiguration checkConfig = new PropertiesConfiguration(filename); - for (Iterator i = conf.getKeys(); i.hasNext();) - { - String key = (String) i.next(); - assertTrue("The saved configuration doesn't contain the key '" + key + "'", checkConfig.containsKey(key)); - assertEquals("Value of the '" + key + "' property", conf.getProperty(key), checkConfig.getProperty(key)); - } + ConfigurationAssert.assertEquals(conf, checkConfig); // Save it again, verifing a save with a filename works. checkConfig.save(); @@ -268,15 +263,33 @@ // read the configuration and compare the properties PropertiesConfiguration checkConfig = new PropertiesConfiguration(filename); - for (Iterator i = pc.getKeys(); i.hasNext();) - { - String key = (String) i.next(); - assertTrue("The saved configuration doesn't contain the key '" + key + "'", checkConfig.containsKey(key)); - assertEquals("Value of the '" + key + "' property", pc.getProperty(key), checkConfig.getProperty(key)); - } + ConfigurationAssert.assertEquals(pc, checkConfig); // Save it again, verifing a save with a filename works. checkConfig.save(); + } + + /** + * Tests saving a configuration when delimiter parsing is disabled. + */ + public void testSaveWithDelimiterParsingDisabled() throws ConfigurationException + { + conf.clear(); + conf.setDelimiterParsingDisabled(true); + conf.addProperty("test.list", "a,b,c"); + conf.addProperty("test.dirs", "C:\\Temp\\,D:\\Data\\"); + // remove the file previously saved if necessary + if (testSavePropertiesFile.exists()) + { + assertTrue(testSavePropertiesFile.delete()); + } + conf.save(testSavePropertiesFile); + + PropertiesConfiguration checkConfig = new PropertiesConfiguration(); + checkConfig.setDelimiterParsingDisabled(true); + checkConfig.setFile(testSavePropertiesFile); + checkConfig.load(); + ConfigurationAssert.assertEquals(conf, checkConfig); } public void testSaveMissingFilename() Modified: jakarta/commons/proper/configuration/trunk/xdocs/changes.xml URL: http://svn.apache.org/viewvc/jakarta/commons/proper/configuration/trunk/xdocs/changes.xml?view=diff&rev=541270&r1=541269&r2=541270 ============================================================================== --- jakarta/commons/proper/configuration/trunk/xdocs/changes.xml (original) +++ jakarta/commons/proper/configuration/trunk/xdocs/changes.xml Thu May 24 04:31:25 2007 @@ -23,6 +23,10 @@ <body> <release version="1.5-SNAPSHOT" date="in SVN" description=""> + <action dev="ebourg" type="fix" issue="CONFIGURATION-269"> + PropertiesConfiguration no longer escape the list delimiter on saving + if the list delimiter has been disabled. + </action> <action dev="ebourg" type="fix" issue="CONFIGURATION-270"> List properties and properties containing interpolated variables are now properly saved by INIConfiguration. --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]