setProperty in XmlConfiguration escapes backslashes
---------------------------------------------------

                 Key: CONFIGURATION-440
                 URL: https://issues.apache.org/jira/browse/CONFIGURATION-440
             Project: Commons Configuration
          Issue Type: Bug
          Components: Build
    Affects Versions: 1.6
         Environment: Windows 7
            Reporter: B
             Fix For: 2.0


When setPropery sets a new value that has backslashes (like path), backslashes 
are escaped. When that value is loaded again, all backslashes are duplicated. 

public class XmlConfigurationTest {

    private static final String XML_CONFIGURATION_PATH = 
"resources/xml/config.xml";

    private static final String PATH1 = "c:\\temp\\path\\file.txt";

    private static final String PATH2 = "c:\\temp1\\path1\\file1.txt";

    private static final String PATH3 = "c:\\temp2\\path2\\file2.txt";

    @Test
    public void testXmlConfiguration() throws IOException {
        File resourceFile = null;
        try {

            assertNotNull(_bundleContext);
            resourceFile = new File(SystemUtils.getTargetDirectory(),
                    XML_CONFIGURATION_PATH);
            assertTrue(resourceFile.getParentFile().mkdirs());

            String value = null;

            XMLConfiguration configuration = new XMLConfiguration();
            configuration.setExpressionEngine(new XPathExpressionEngine());
            configuration.setDelimiterParsingDisabled(true);
            configuration.addProperty(" key1", PATH1);
            configuration.setProperty(" key2", PATH2);
            configuration.save(resourceFile);
            /* All values are saved with non escaped backslashes */

            value = configuration.getString("key1");
            assertEquals(value, PATH1);
            value = configuration.getString("key2");
            assertEquals(value, PATH2);

            /*
             * Set again same property with different value. Setting property
             * with this configuration will escape backslashes. Even though
             * assert will pass, path with escaped backslashes will be written
             * in a file (don't know if it is setProperty or save that is 
causing troubles).
             */
            configuration.setProperty(" key2", PATH3);
            configuration.save(resourceFile);
            value = configuration.getString("key2");
            assertEquals(value, PATH3);

            /*
             * Create new configuration and load values from previously saved
             * file.
             */
            XMLConfiguration newConfiguration = new XMLConfiguration();
            newConfiguration.setExpressionEngine(new XPathExpressionEngine());
            newConfiguration.setDelimiterParsingDisabled(true);
            newConfiguration.load(resourceFile);

            /*
             * At this point, configuration will load escaped backslashes, and
             * the test will fail.
             */
            value = newConfiguration.getString("key2");
            assertEquals(value, PATH3);

        } catch (Throwable e) {
            e.printStackTrace();
            fail(e.getLocalizedMessage());
        } finally {
            /*
             * Delete resource file
             */
            resourceFile.delete();
        }

    }
}

-- 
This message is automatically generated by JIRA.
-
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

Reply via email to