Return-Path: Delivered-To: apmail-commons-commits-archive@minotaur.apache.org Received: (qmail 51842 invoked from network); 22 Aug 2009 19:23:57 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 22 Aug 2009 19:23:57 -0000 Received: (qmail 29262 invoked by uid 500); 22 Aug 2009 19:24:17 -0000 Delivered-To: apmail-commons-commits-archive@commons.apache.org Received: (qmail 29167 invoked by uid 500); 22 Aug 2009 19:24:17 -0000 Mailing-List: contact commits-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@commons.apache.org Delivered-To: mailing list commits@commons.apache.org Received: (qmail 29158 invoked by uid 99); 22 Aug 2009 19:24:17 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 22 Aug 2009 19:24:17 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 22 Aug 2009 19:24:15 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id C737A23888E5; Sat, 22 Aug 2009 19:23:55 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: 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 Date: Sat, 22 Aug 2009 19:23:55 -0000 To: commits@commons.apache.org From: oheger@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20090822192355.C737A23888E5@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org 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. *

+ *

+ * {@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. + *

* * @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 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 + * null) + * @throws IllegalArgumentException if the source to be copied is + * null + */ + public MapConfigurationSource(ConfigurationSource c) + { + if (c == null) + { + throw new IllegalArgumentException( + "Source to copy must not be null!"); + } + + store = new LinkedHashMap(); + for (Iterator 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((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) 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 it = src.getKeys(); it.hasNext();) + { + String key = it.next(); + assertEquals("Wrong value for property " + key, src + .getProperty(key), src2.getProperty(key)); + } + } }