Author: oheger Date: Sat Jun 2 13:00:02 2007 New Revision: 543778 URL: http://svn.apache.org/viewvc?view=rev&rev=543778 Log: CONFIGURATION-279: Fix PropertiesConfiguration to work correctly when created from a non-existing file; the layout object is now created manually in this case
Modified: jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/PropertiesConfiguration.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=543778&r1=543777&r2=543778 ============================================================================== --- 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 Sat Jun 2 13:00:02 2007 @@ -232,7 +232,9 @@ /** * Creates and loads the extended properties from the specified file. * The specified file can contain "include = " properties which then - * are loaded and merged into the properties. + * are loaded and merged into the properties. If the file does not exist, + * an empty configuration will be created. Later the <code>save()</code> + * method can be called to save the properties to the specified file. * * @param file The properties file to load. * @throws ConfigurationException Error while loading the properties file @@ -240,6 +242,13 @@ public PropertiesConfiguration(File file) throws ConfigurationException { super(file); + + // If the file does not exist, no layout object was created. We have to + // do this manually in this case. + if (layout == null) + { + layout = createLayout(); + } } /** 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=543778&r1=543777&r2=543778 ============================================================================== --- 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 Sat Jun 2 13:00:02 2007 @@ -57,6 +57,13 @@ protected void setUp() throws Exception { conf = new PropertiesConfiguration(testProperties); + + // remove the test save file if it exists + if (testSavePropertiesFile.exists()) + { + assertTrue("Test output file could not be deleted", + testSavePropertiesFile.delete()); + } } public void testLoad() throws Exception @@ -132,12 +139,6 @@ public void testSave() throws Exception { - // remove the file previously saved if necessary - if (testSavePropertiesFile.exists()) - { - assertTrue(testSavePropertiesFile.delete()); - } - // add an array of strings to the configuration conf.addProperty("string", "value1"); List list = new ArrayList(); @@ -174,12 +175,6 @@ public void testInMemoryCreatedSave() throws Exception { - // remove the file previously saved if necessary - if (testSavePropertiesFile.exists()) - { - assertTrue(testSavePropertiesFile.delete()); - } - PropertiesConfiguration pc = new PropertiesConfiguration(); // add an array of strings to the configuration pc.addProperty("string", "value1"); @@ -213,11 +208,6 @@ 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(); @@ -247,12 +237,6 @@ */ public void testSaveWithBasePath() throws Exception { - // remove the file previously saved if necessary - if (testSavePropertiesFile.exists()) - { - assertTrue(testSavePropertiesFile.delete()); - } - conf.setProperty("test", "true"); conf.setBasePath(testSavePropertiesFile.getParentFile().toURL().toString()); conf.setFileName(testSavePropertiesFile.getName()); @@ -270,10 +254,6 @@ conf.addProperty("test.dirs", "C:\\Temp\\\\,D:\\Data\\\\,E:\\Test\\"); List dirs = conf.getList("test.dirs"); assertEquals("Wrong number of list elements", 3, dirs.size()); - if (testSavePropertiesFile.exists()) - { - assertTrue(testSavePropertiesFile.delete()); - } conf.save(testSavePropertiesFile); PropertiesConfiguration checkConfig = new PropertiesConfiguration( @@ -725,12 +705,6 @@ */ public void testSaveToHTTPServerSuccess() throws Exception { - if (testSavePropertiesFile.exists()) - { - assertTrue("Could not delete test file", testSavePropertiesFile - .delete()); - } - MockHttpURLStreamHandler handler = new MockHttpURLStreamHandler( HttpURLConnection.HTTP_OK, testSavePropertiesFile); URL url = new URL(null, "http://jakarta.apache.org", handler); @@ -763,6 +737,23 @@ assertTrue("Wrong root cause: " + cex, cex.getCause() instanceof IOException); } + } + + /** + * Tests initializing a properties configuration from a non existing file. + * There was a bug, which caused properties getting lost when later save() + * is called. + */ + public void testInitFromNonExistingFile() throws ConfigurationException + { + final String testProperty = "test.successfull"; + conf = new PropertiesConfiguration(testSavePropertiesFile); + conf.addProperty(testProperty, Boolean.TRUE); + conf.save(); + PropertiesConfiguration checkConfig = new PropertiesConfiguration( + testSavePropertiesFile); + assertTrue("Test property not found", checkConfig + .getBoolean(testProperty)); } /** 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=543778&r1=543777&r2=543778 ============================================================================== --- jakarta/commons/proper/configuration/trunk/xdocs/changes.xml (original) +++ jakarta/commons/proper/configuration/trunk/xdocs/changes.xml Sat Jun 2 13:00:02 2007 @@ -23,6 +23,10 @@ <body> <release version="1.5-SNAPSHOT" date="in SVN" description=""> + <action dev="oheger" type="fix" issue="CONFIGURATION-279"> + A PropertiesConfiguration that was created from a non existing file + lost its content when it was saved. This problem has been solved. + </action> <action dev="oheger" type="add" issue="CONFIGURATION-215"> A new getSource() method was added to CompositeConfiguration and CombinedConfiguration, which returns the child configuration, in which --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]