You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by oh...@apache.org on 2009/08/22 17:37:24 UTC

svn commit: r806863 - in /commons/proper/configuration/branches/configuration2_experimental: src/main/java/org/apache/commons/configuration2/flat/ src/test/java/org/apache/commons/configuration2/flat/ xdocs/

Author: oheger
Date: Sat Aug 22 15:37:23 2009
New Revision: 806863

URL: http://svn.apache.org/viewvc?rev=806863&view=rev
Log:
[CONFIGURATION-393] Special treatment of collection properties when cloning a BaseConfiguration. Copied the fix from trunk.

Modified:
    commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/flat/BaseConfiguration.java
    commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/flat/TestBaseConfiguration.java
    commons/proper/configuration/branches/configuration2_experimental/xdocs/changes.xml

Modified: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/flat/BaseConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/flat/BaseConfiguration.java?rev=806863&r1=806862&r2=806863&view=diff
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/flat/BaseConfiguration.java (original)
+++ commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/flat/BaseConfiguration.java Sat Aug 22 15:37:23 2009
@@ -18,6 +18,7 @@
 package org.apache.commons.configuration2.flat;
 
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.Iterator;
 import java.util.LinkedHashMap;
 import java.util.List;
@@ -206,13 +207,26 @@
      * @since 1.3
      */
     @Override
-    @SuppressWarnings("unchecked")
     public Object clone()
     {
         try
         {
             BaseConfiguration copy = (BaseConfiguration) super.clone();
-            copy.store = (Map<String, Object>) ConfigurationUtils.clone(store);
+            @SuppressWarnings("unchecked")
+            Map<String, Object> clonedStore = (Map<String, Object>) ConfigurationUtils
+                    .clone(store);
+
+            // Handle collections in the map; they have to be cloned, too
+            for (Map.Entry<String, Object> e : store.entrySet())
+            {
+                if (e.getValue() instanceof Collection<?>)
+                {
+                    clonedStore.put(e.getKey(), new ArrayList<Object>(
+                            (Collection<?>) e.getValue()));
+                }
+            }
+
+            copy.store = clonedStore;
             return copy;
         }
         catch (CloneNotSupportedException cex)

Modified: commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/flat/TestBaseConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/flat/TestBaseConfiguration.java?rev=806863&r1=806862&r2=806863&view=diff
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/flat/TestBaseConfiguration.java (original)
+++ commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/flat/TestBaseConfiguration.java Sat Aug 22 15:37:23 2009
@@ -764,6 +764,20 @@
     }
 
     /**
+     * Tests the clone() method if a list property is involved.
+     */
+    public void testCloneListProperty()
+    {
+        final String key = "list";
+        config.addProperty(key, "value1");
+        config.addProperty(key, "value2");
+        BaseConfiguration config2 = (BaseConfiguration) config.clone();
+        config2.addProperty(key, "value3");
+        assertEquals("Wrong number of original properties", 2, config.getList(
+                key).size());
+    }
+
+    /**
      * Tests the getMaxIndex() method for a non existing property.
      */
     public void testGetMaxIndexNonExisting()

Modified: commons/proper/configuration/branches/configuration2_experimental/xdocs/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/xdocs/changes.xml?rev=806863&r1=806862&r2=806863&view=diff
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/xdocs/changes.xml (original)
+++ commons/proper/configuration/branches/configuration2_experimental/xdocs/changes.xml Sat Aug 22 15:37:23 2009
@@ -85,6 +85,10 @@
     </release>
 
     <release version="1.7" date="in SVN" description="">
+      <action dev="oheger" type="fix" issue="CONFIGURATION-393">
+        BaseConfiguration.clone() now also clones collections stored in the
+        internal map. This causes list properties to be handled correctly.
+      </action>
       <action dev="rgoers" type="fix" issue="CONFIGURATION-388">
         Attribute or element values will not be escaped when attribute or element splitting are disabled.
       </action>