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 21:23:55 UTC

svn commit: r806881 - in /commons/proper/configuration/branches/configuration2_experimental/src: main/java/org/apache/commons/configuration2/base/MapConfigurationSource.java test/java/org/apache/commons/configuration2/base/TestMapConfigurationSource.java

Author: oheger
Date: Sat Aug 22 19:23:55 2009
New Revision: 806881

URL: http://svn.apache.org/viewvc?rev=806881&view=rev
Log:
Made MapConfigurationSource serializable and added a copy constructor.

Modified:
    commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/base/MapConfigurationSource.java
    commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/base/TestMapConfigurationSource.java

Modified: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/base/MapConfigurationSource.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/base/MapConfigurationSource.java?rev=806881&r1=806880&r2=806881&view=diff
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/base/MapConfigurationSource.java (original)
+++ commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/base/MapConfigurationSource.java Sat Aug 22 19:23:55 2009
@@ -16,7 +16,9 @@
  */
 package org.apache.commons.configuration2.base;
 
+import java.io.Serializable;
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.Iterator;
 import java.util.LinkedHashMap;
 import java.util.List;
@@ -44,12 +46,24 @@
  * that a map-like structure and keep their whole data in memory. An example
  * could be a configuration source wrapping a properties file.
  * </p>
+ * <p>
+ * {@code MapConfigurationSource} implements the {@code Serializable} interface.
+ * The data that is serialized basically consists of the map used as data store.
+ * Serialization can only be successful if all property values can be
+ * serialized.
+ * </p>
  *
  * @author Commons Configuration team
  * @version $Id$
  */
-public class MapConfigurationSource implements ConfigurationSource
+public class MapConfigurationSource implements ConfigurationSource,
+        Serializable
 {
+    /**
+     * The serial version UID.
+     */
+    private static final long serialVersionUID = 3921765607397858876L;
+
     /** The map acting as data store. */
     private final Map<String, Object> store;
 
@@ -84,6 +98,42 @@
     }
 
     /**
+     * Creates a new instance of {@code MapConfigurationSource} and initializes
+     * it from the data of the specified {@code ConfigurationSource}. This
+     * constructor copies all properties stored in the passed in {@code
+     * ConfigurationSource} into this source.
+     *
+     * @param c the {@code ConfigurationSource} to be copied (must not be
+     *        <b>null</b>)
+     * @throws IllegalArgumentException if the source to be copied is
+     *         <b>null</b>
+     */
+    public MapConfigurationSource(ConfigurationSource c)
+    {
+        if (c == null)
+        {
+            throw new IllegalArgumentException(
+                    "Source to copy must not be null!");
+        }
+
+        store = new LinkedHashMap<String, Object>();
+        for (Iterator<String> it = c.getKeys(); it.hasNext();)
+        {
+            String key = it.next();
+            Object value = c.getProperty(key);
+
+            // special treatment of collection properties: the collections
+            // must be copied, too
+            if (value instanceof Collection<?>)
+            {
+                value = new ArrayList<Object>((Collection<?>) value);
+            }
+
+            store.put(key, value);
+        }
+    }
+
+    /**
      * Adds the specified {@code ConfigurationSourceListener} at this object.
      * This class does not support event listeners, so an exception is thrown.
      *

Modified: commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/base/TestMapConfigurationSource.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/base/TestMapConfigurationSource.java?rev=806881&r1=806880&r2=806881&view=diff
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/base/TestMapConfigurationSource.java (original)
+++ commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/base/TestMapConfigurationSource.java Sat Aug 22 19:23:55 2009
@@ -16,6 +16,11 @@
  */
 package org.apache.commons.configuration2.base;
 
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Iterator;
@@ -85,7 +90,7 @@
     {
         try
         {
-            new MapConfigurationSource(null);
+            new MapConfigurationSource((Map<String, Object>) null);
             fail("Could create instance without a map!");
         }
         catch (IllegalArgumentException iex)
@@ -361,4 +366,98 @@
             // ok
         }
     }
+
+    /**
+     * Tests whether a copy of a source can be created and the copy contains the
+     * same properties.
+     */
+    public void testInitCopy()
+    {
+        MapConfigurationSource src = new MapConfigurationSource(setUpMap());
+        MapConfigurationSource copy = new MapConfigurationSource(src);
+        assertEquals("Wrong number of properties", COUNT, copy.size());
+        for (int i = 0; i < COUNT; i++)
+        {
+            String key = KEY + i;
+            assertEquals("Wrong property for " + key, i, copy.getProperty(key));
+        }
+    }
+
+    /**
+     * Tests whether changing the original or the copy does not affect the other
+     * object.
+     */
+    public void testInitCopyModify()
+    {
+        MapConfigurationSource src = new MapConfigurationSource(setUpMap());
+        MapConfigurationSource copy = new MapConfigurationSource(src);
+        src.addProperty("original", Boolean.TRUE);
+        src.addProperty("clone", Boolean.FALSE);
+        copy.addProperty("original", Boolean.FALSE);
+        copy.addProperty("clone", Boolean.TRUE);
+        assertEquals("Wrong original property in original", Boolean.TRUE, src
+                .getProperty("original"));
+        assertEquals("Wrong clone property in original", Boolean.FALSE, src
+                .getProperty("clone"));
+        assertEquals("Wrong original property in clone", Boolean.FALSE, copy
+                .getProperty("original"));
+        assertEquals("Wrong clone property in clone", Boolean.TRUE, copy
+                .getProperty("clone"));
+    }
+
+    /**
+     * Tests the copy constructor if list properties are involved.
+     */
+    public void testInitCopyListProperty()
+    {
+        MapConfigurationSource src = new MapConfigurationSource(setUpMap());
+        src.addProperty(KEY, 1);
+        src.addProperty(KEY, 2);
+        MapConfigurationSource copy = new MapConfigurationSource(src);
+        copy.addProperty(KEY, 3);
+        checkList(src.getProperty(KEY), 1, 2);
+        checkList(copy.getProperty(KEY), 1, 2, 3);
+    }
+
+    /**
+     * Tries to invoke the copy constructor with a null source. This should
+     * cause an exception.
+     */
+    public void testInitCopyNullSource()
+    {
+        try
+        {
+            new MapConfigurationSource((ConfigurationSource) null);
+            fail("Could create copy of null source!");
+        }
+        catch (IllegalArgumentException iex)
+        {
+            // ok
+        }
+    }
+
+    /**
+     * Tests whether the source can be serialized.
+     */
+    public void testSerialization() throws IOException, ClassNotFoundException
+    {
+        ByteArrayOutputStream bos = new ByteArrayOutputStream();
+        ObjectOutputStream oos = new ObjectOutputStream(bos);
+        MapConfigurationSource src = new MapConfigurationSource(setUpMap());
+        src.addProperty(KEY, "value1");
+        src.addProperty(KEY, "value2");
+        oos.writeObject(src);
+        oos.close();
+        ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(
+                bos.toByteArray()));
+        MapConfigurationSource src2 = (MapConfigurationSource) ois.readObject();
+        ois.close();
+        assertEquals("Wrong number of properties", src.size(), src2.size());
+        for (Iterator<String> it = src.getKeys(); it.hasNext();)
+        {
+            String key = it.next();
+            assertEquals("Wrong value for property " + key, src
+                    .getProperty(key), src2.getProperty(key));
+        }
+    }
 }