Author: oheger
Date: Wed Aug 30 12:22:42 2006
New Revision: 438598
URL: http://svn.apache.org/viewvc?rev=438598&view=rev
Log:
Updated AbstractFileConfiguration to check for auto save in addProperty() and
setProperty() instead of addPropertyDirect(); fixes CONFIGURATION-223; thanks
to Gabriele Garuglieri for the additional unit tests
Modified:
jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/AbstractFileConfiguration.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/AbstractFileConfiguration.java
URL:
http://svn.apache.org/viewvc/jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/AbstractFileConfiguration.java?rev=438598&r1=438597&r2=438598&view=diff
==============================================================================
---
jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/AbstractFileConfiguration.java
(original)
+++
jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/AbstractFileConfiguration.java
Wed Aug 30 12:22:42 2006
@@ -525,7 +525,7 @@
/**
* Set the name of the file. The passed in file name can contain a
- * relative path.
+ * relative path.
* It must be used when referring files with relative paths from classpath.
* Use <code>[EMAIL PROTECTED] AbstractFileConfiguration#setPath(String)
* setPath()}</code> to set a full qualified file name.
@@ -562,7 +562,7 @@
/**
* Return the file where the configuration is stored. If the base path is a
- * URL with a protocol different than "file", or the
configuration
+ * URL with a protocol different than "file", or the
configuration
* file is within a compressed archive, the return value
* will not point to a valid file object.
*
@@ -604,7 +604,7 @@
/**
* Returns the full path to the file this configuration is based on. The
- * return value is a valid File path only if this configuration is based
on
+ * return value is a valid File path only if this configuration is based on
* a file on the local disk.
* If the configuration was loaded from a packed archive the returned value
* is the string form of the URL from which the configuration was loaded.
@@ -620,7 +620,7 @@
{
path = file.getAbsolutePath();
}
-
+
// try to see if file was loaded from a jar
if (path == null)
{
@@ -643,10 +643,10 @@
}
/**
- * Sets the location of this configuration as a full or relative path
name.
+ * Sets the location of this configuration as a full or relative path name.
* The passed in path should represent a valid file name on the file
system.
- * It must not be used to specify relative paths for files that exist
- * in classpath, either plain file system or compressed archive,
+ * It must not be used to specify relative paths for files that exist
+ * in classpath, either plain file system or compressed archive,
* because this method expands any relative path to an absolute one which
* may end in an invalid absolute path for classpath references.
*
@@ -712,9 +712,30 @@
}
}
- protected void addPropertyDirect(String key, Object obj)
+ /**
+ * Adds a new property to this configuration. This implementation checks if
+ * the auto save mode is enabled and saves the configuration if necessary.
+ *
+ * @param key the key of the new property
+ * @param value the value
+ */
+ public void addProperty(String key, Object value)
+ {
+ super.addProperty(key, value);
+ possiblySave();
+ }
+
+ /**
+ * Sets a new value for the specified property. This implementation checks
+ * if the auto save mode is enabled and saves the configuration if
+ * necessary.
+ *
+ * @param key the key of the affected property
+ * @param value the value
+ */
+ public void setProperty(String key, Object value)
{
- super.addPropertyDirect(key, obj);
+ super.setProperty(key, value);
possiblySave();
}
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?rev=438598&r1=438597&r2=438598&view=diff
==============================================================================
---
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
Wed Aug 30 12:22:42 2006
@@ -18,6 +18,7 @@
import java.io.File;
import java.io.FileWriter;
+import java.io.IOException;
import java.io.PrintWriter;
import java.io.Reader;
import java.io.StringReader;
@@ -189,7 +190,7 @@
String key = (String) i.next();
assertTrue("The saved configuration doesn't contain the key '" +
key + "'",
checkConfig.containsKey(key));
- assertEquals("Value of the '" + key + "' property",
+ assertEquals("Value of the '" + key + "' property",
pc.getProperty(key), checkConfig.getProperty(key));
}
@@ -292,6 +293,62 @@
*/
public void testLoadWithAutoSave() throws Exception
{
+ setUpSavedProperties();
+ }
+
+ /**
+ * Tests the auto save functionality when an existing property is modified.
+ */
+ public void testLoadWithAutoSaveAndSetExisting() throws Exception
+ {
+ setUpSavedProperties();
+ conf.setProperty("a", "moreThanOne");
+ checkSavedConfig();
+ }
+
+ /**
+ * Tests the auto save functionality when a new property is added using the
+ * setProperty() method.
+ */
+ public void testLoadWithAutoSaveAndSetNew() throws Exception
+ {
+ setUpSavedProperties();
+ conf.setProperty("d", "four");
+ checkSavedConfig();
+ }
+
+ /**
+ * Tests the auto save functionality when a new property is added using the
+ * addProperty() method.
+ */
+ public void testLoadWithAutoSaveAndAdd() throws Exception
+ {
+ setUpSavedProperties();
+ conf.addProperty("d", "four");
+ checkSavedConfig();
+ }
+
+ /**
+ * Tests the auto save functionality when a property is removed.
+ */
+ public void testLoadWithAutoSaveAndClear() throws Exception
+ {
+ setUpSavedProperties();
+ conf.clearProperty("c");
+ PropertiesConfiguration checkConfig = checkSavedConfig();
+ assertFalse("The saved configuration contain the key '" + "c" + "'",
+ checkConfig.containsKey("c"));
+ }
+
+ /**
+ * Creates a properties file on disk. Used for testing load and save
+ * operations.
+ *
+ * @throws IOException if an I/O error occurs
+ */
+ private void setUpSavedProperties() throws IOException,
+ ConfigurationException
+ {
PrintWriter out = null;
try
@@ -313,11 +370,34 @@
}
finally
{
- if(out != null)
+ if (out != null)
{
out.close();
}
}
+ }
+
+ /**
+ * Helper method for testing a saved configuration. Reads in the file using
+ * a new instance and compares this instance with the original one.
+ *
+ * @return the newly created configuration instance
+ * @throws ConfigurationException if an error occurs
+ */
+ private PropertiesConfiguration checkSavedConfig()
+ throws ConfigurationException
+ {
+ PropertiesConfiguration checkConfig = new PropertiesConfiguration(
+ testSavePropertiesFile);
+ 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));
+ }
+ return checkConfig;
}
public void testGetStringWithEscapedChars()
Modified: jakarta/commons/proper/configuration/trunk/xdocs/changes.xml
URL:
http://svn.apache.org/viewvc/jakarta/commons/proper/configuration/trunk/xdocs/changes.xml?rev=438598&r1=438597&r2=438598&view=diff
==============================================================================
--- jakarta/commons/proper/configuration/trunk/xdocs/changes.xml (original)
+++ jakarta/commons/proper/configuration/trunk/xdocs/changes.xml Wed Aug 30
12:22:42 2006
@@ -23,6 +23,14 @@
<body>
<release version="1.3-rc2" date="in SVN">
+ <action dev="oheger" type="update" issue="CONFIGURATION-223"
due-to="Gabriele Garuglieri">
+ AbstractFileConfiguration now overrides addProperty() and setProperty()
+ instead of addPropertyDirect() to implement the auto save feature.
+ This was necessary to properly integrate PropertiesConfigurationLayout.
+ It has also the advantage that an auto save is triggered only once if
+ multi-valued properties are involved (before a save operation was
+ performed for each property value).
+ </action>
<action dev="oheger" type="update" issue="CONFIGURATION-222"
due-to="Gabriele Garuglieri">
The new PropertiesConfigurationLayout class broke the save() operation
of PropertiesConfiguration when an instance was newly created and
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]