Author: oheger
Date: Sat Aug 12 08:23:52 2006
New Revision: 431042
URL: http://svn.apache.org/viewvc?rev=431042&view=rev
Log:
Added a clone() method to PropertiesConfiguration to correctly handle the
layout object
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/src/test/org/apache/commons/configuration/TestPropertiesConfigurationLayout.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?rev=431042&r1=431041&r2=431042&view=diff
==============================================================================
---
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 Aug 12 08:23:52 2006
@@ -413,6 +413,21 @@
}
/**
+ * Creates a copy of this object.
+ *
+ * @return the copy
+ */
+ public Object clone()
+ {
+ PropertiesConfiguration copy = (PropertiesConfiguration) super.clone();
+ if (layout != null)
+ {
+ copy.setLayout(new PropertiesConfigurationLayout(copy, layout));
+ }
+ return copy;
+ }
+
+ /**
* This method is invoked by the associated
* <code>[EMAIL PROTECTED] PropertiesConfigurationLayout}</code> object
for each
* property definition detected in the parsed properties file. Its task is
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?rev=431042&r1=431041&r2=431042&view=diff
==============================================================================
---
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
Sat Aug 12 08:23:52 2006
@@ -145,6 +145,20 @@
*/
public PropertiesConfigurationLayout(PropertiesConfiguration config)
{
+ this(config, null);
+ }
+
+ /**
+ * Creates a new instance of <code>PropertiesConfigurationLayout</code>
+ * and initializes it with the given configuration object. The data of the
+ * specified layout object is copied.
+ *
+ * @param config the configuration (must not be <b>null</b>)
+ * @param c the layout object to be copied
+ */
+ public PropertiesConfigurationLayout(PropertiesConfiguration config,
+ PropertiesConfigurationLayout c)
+ {
if (config == null)
{
throw new IllegalArgumentException(
@@ -153,6 +167,11 @@
configuration = config;
layoutData = new LinkedMap();
config.addConfigurationListener(this);
+
+ if (c != null)
+ {
+ copyFrom(c);
+ }
}
/**
@@ -688,10 +707,26 @@
}
/**
+ * Copies the data from the given layout object.
+ *
+ * @param c the layout object to copy
+ */
+ private void copyFrom(PropertiesConfigurationLayout c)
+ {
+ for (Iterator it = c.getKeys().iterator(); it.hasNext();)
+ {
+ String key = (String) it.next();
+ PropertyLayoutData data = (PropertyLayoutData) c.layoutData
+ .get(key);
+ layoutData.put(key, data.clone());
+ }
+ }
+
+ /**
* A helper class for storing all layout related information for a
* configuration property.
*/
- static class PropertyLayoutData
+ static class PropertyLayoutData implements Cloneable
{
/** Stores the comment for the property. */
private StringBuffer comment;
@@ -797,6 +832,30 @@
public String getComment()
{
return (comment == null) ? null : comment.toString();
+ }
+
+ /**
+ * Creates a copy of this object.
+ *
+ * @return the copy
+ */
+ public Object clone()
+ {
+ try
+ {
+ PropertyLayoutData copy = (PropertyLayoutData) super.clone();
+ if (comment != null)
+ {
+ // must copy string buffer, too
+ copy.comment = new StringBuffer(getComment());
+ }
+ return copy;
+ }
+ catch (CloneNotSupportedException cnex)
+ {
+ // This cannot happen!
+ throw new ConfigurationRuntimeException(cnex);
+ }
}
}
}
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=431042&r1=431041&r2=431042&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
Sat Aug 12 08:23:52 2006
@@ -541,6 +541,42 @@
}
/**
+ * Tests whether a properties configuration can be successfully cloned. It
+ * is especially checked whether the layout object is taken into account.
+ */
+ public void testClone() throws ConfigurationException
+ {
+ PropertiesConfiguration copy = (PropertiesConfiguration) conf.clone();
+ assertNotSame("Copy has same layout object", conf.getLayout(), copy
+ .getLayout());
+ assertEquals("Wrong number of event listeners for original", 1, conf
+ .getConfigurationListeners().size());
+ assertEquals("Wrong number of event listeners for clone", 1, copy
+ .getConfigurationListeners().size());
+ assertSame("Wrong event listener for original", conf.getLayout(), conf
+ .getConfigurationListeners().iterator().next());
+ assertSame("Wrong event listener for clone", copy.getLayout(), copy
+ .getConfigurationListeners().iterator().next());
+ StringWriter outConf = new StringWriter();
+ conf.save(outConf);
+ StringWriter outCopy = new StringWriter();
+ copy.save(outCopy);
+ assertEquals("Output from copy is different", outConf.toString(),
+ outCopy.toString());
+ }
+
+ /**
+ * Tests the clone() method when no layout object exists yet.
+ */
+ public void testCloneNullLayout()
+ {
+ conf = new PropertiesConfiguration();
+ PropertiesConfiguration copy = (PropertiesConfiguration) conf.clone();
+ assertNotSame("Layout objects are the same", conf.getLayout(), copy
+ .getLayout());
+ }
+
+ /**
* A dummy layout implementation for checking whether certain methods are
* correctly called by the configuration.
*/
Modified:
jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestPropertiesConfigurationLayout.java
URL:
http://svn.apache.org/viewvc/jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestPropertiesConfigurationLayout.java?rev=431042&r1=431041&r2=431042&view=diff
==============================================================================
---
jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestPropertiesConfigurationLayout.java
(original)
+++
jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestPropertiesConfigurationLayout.java
Sat Aug 12 08:23:52 2006
@@ -560,6 +560,42 @@
}
/**
+ * Tests the copy constructor.
+ */
+ public void testInitCopy()
+ {
+ fillLayout();
+ PropertiesConfigurationLayout l2 = new PropertiesConfigurationLayout(
+ config, layout);
+ assertEquals("Wrong number of keys", layout.getKeys().size(), l2
+ .getKeys().size());
+ for (Iterator it = layout.getKeys().iterator(); it.hasNext();)
+ {
+ Object key = it.next();
+ assertTrue("Key was not found: " + key,
l2.getKeys().contains(key));
+ }
+ }
+
+ /**
+ * Tests if the copy and the original are independend from each other.
+ */
+ public void testInitCopyModify()
+ {
+ fillLayout();
+ PropertiesConfigurationLayout l2 = new PropertiesConfigurationLayout(
+ config, layout);
+ assertEquals("Comments are not equal", layout.getComment(TEST_KEY), l2
+ .getComment(TEST_KEY));
+ layout.setComment(TEST_KEY, "A new comment");
+ assertEquals("Comment was changed", TEST_COMMENT, l2
+ .getCanonicalComment(TEST_KEY, false));
+ l2.setBlancLinesBefore(TEST_KEY, l2.getBlancLinesBefore(TEST_KEY) + 1);
+ assertFalse("Blanc lines do not differ", layout
+ .getBlancLinesBefore(TEST_KEY) == l2
+ .getBlancLinesBefore(TEST_KEY));
+ }
+
+ /**
* Helper method for filling the layout object with some properties.
*/
private void fillLayout()
Modified: jakarta/commons/proper/configuration/trunk/xdocs/changes.xml
URL:
http://svn.apache.org/viewvc/jakarta/commons/proper/configuration/trunk/xdocs/changes.xml?rev=431042&r1=431041&r2=431042&view=diff
==============================================================================
--- jakarta/commons/proper/configuration/trunk/xdocs/changes.xml (original)
+++ jakarta/commons/proper/configuration/trunk/xdocs/changes.xml Sat Aug 12
08:23:52 2006
@@ -22,7 +22,13 @@
<body>
- <release version="1.3-SNAPSHOT" date="in SVN">
+ <release version="1.3-rc2" date="in SVN">
+ <action dev="oheger" type="update" issue="CONFIGURATION-219">
+ PropertiesConfiguration now defines its own clone() method to handle
+ the associated PropertiesConfigurationLayout object correctly.
+ </action>
+ </release>
+ <release version="1.3-rc1" date="2006-07-30">
<action dev="oheger" type="update" issue="CONFIGURATION-217">
The dependency to servletapi was updated from version 2.3 to version
2.4, but version 2.3 will still work.
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]