commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ohe...@apache.org
Subject svn commit: r418608 - in /jakarta/commons/proper/configuration/trunk/src: java/org/apache/commons/configuration/ test/org/apache/commons/configuration/
Date Sun, 02 Jul 2006 15:28:14 GMT
Author: oheger
Date: Sun Jul  2 08:28:13 2006
New Revision: 418608

URL: http://svn.apache.org/viewvc?rev=418608&view=rev
Log:
Added clone() methods to BaseConfiguration and AbstractFileConfiguration

Modified:
    jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/AbstractFileConfiguration.java
    jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/BaseConfiguration.java
    jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestBaseConfiguration.java
    jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestFileConfiguration.java

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=418608&r1=418607&r2=418608&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
Sun Jul  2 08:28:13 2006
@@ -110,7 +110,7 @@
      */
     public AbstractFileConfiguration()
     {
-        setReloadingStrategy(new InvariantReloadingStrategy());
+        initReloadingStrategy();
     }
 
     /**
@@ -854,5 +854,34 @@
     public void setEncoding(String encoding)
     {
         this.encoding = encoding;
+    }
+
+    /**
+     * Creates a copy of this configuration. The new configuration object will
+     * contain the same properties as the original, but it will lose any
+     * connection to a source file (if one exists); this includes setting the
+     * source URL, base path, and file name to <b>null</b>. This is done to
+     * avoid race conditions if both the original and the copy are modified and
+     * then saved.
+     *
+     * @return the copy
+     * @since 1.3
+     */
+    public Object clone()
+    {
+        AbstractFileConfiguration copy = (AbstractFileConfiguration) super
+                .clone();
+        copy.setBasePath(null);
+        copy.setFileName(null);
+        copy.initReloadingStrategy();
+        return copy;
+    }
+
+    /**
+     * Helper method for initializing the reloading strategy.
+     */
+    private void initReloadingStrategy()
+    {
+        setReloadingStrategy(new InvariantReloadingStrategy());
     }
 }

Modified: jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/BaseConfiguration.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/BaseConfiguration.java?rev=418608&r1=418607&r2=418608&view=diff
==============================================================================
--- jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/BaseConfiguration.java
(original)
+++ jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/BaseConfiguration.java
Sun Jul  2 08:28:13 2006
@@ -47,7 +47,7 @@
  * @author <a href="mailto:oliver.heger@t-online.de">Oliver Heger</a>
  * @version $Id$
  */
-public class BaseConfiguration extends AbstractConfiguration
+public class BaseConfiguration extends AbstractConfiguration implements Cloneable
 {
     /** stores the configuration key-value pairs */
     private Map store = new LinkedMap();
@@ -151,5 +151,28 @@
     public Iterator getKeys()
     {
         return store.keySet().iterator();
+    }
+
+    /**
+     * Creates a copy of this object. This implementation will create a deep
+     * clone, i.e. the map that stores the properties is cloned, too. So changes
+     * performed at the copy won't affect the original and vice versa.
+     *
+     * @return the copy
+     * @since 1.3
+     */
+    public Object clone()
+    {
+        try
+        {
+            BaseConfiguration copy = (BaseConfiguration) super.clone();
+            copy.store = (Map) ((LinkedMap) store).clone();
+            return copy;
+        }
+        catch (CloneNotSupportedException cex)
+        {
+            // should not happen
+            throw new ConfigurationRuntimeException(cex);
+        }
     }
 }

Modified: jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestBaseConfiguration.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestBaseConfiguration.java?rev=418608&r1=418607&r2=418608&view=diff
==============================================================================
--- jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestBaseConfiguration.java
(original)
+++ jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestBaseConfiguration.java
Sun Jul  2 08:28:13 2006
@@ -736,4 +736,39 @@
         assertEquals("Wrong DigDecimal returned", new BigDecimal("42"), config
                 .getBigDecimal(KEY_NUMBER));
     }
+
+    /**
+     * Tests cloning a BaseConfiguration.
+     */
+    public void testClone()
+    {
+        for (int i = 0; i < 10; i++)
+        {
+            config.addProperty("key" + i, new Integer(i));
+        }
+        BaseConfiguration config2 = (BaseConfiguration) config.clone();
+
+        for (Iterator it = config.getKeys(); it.hasNext();)
+        {
+            String key = (String) it.next();
+            assertTrue("Key not found: " + key, config2.containsKey(key));
+            assertEquals("Wrong value for key " + key, config.getProperty(key),
+                    config2.getProperty(key));
+        }
+    }
+
+    /**
+     * Tests whether a cloned configuration is decoupled from its original.
+     */
+    public void testCloneModify()
+    {
+        config.addProperty("original", Boolean.TRUE);
+        BaseConfiguration config2 = (BaseConfiguration) config.clone();
+
+        config2.addProperty("clone", Boolean.TRUE);
+        assertFalse("New key appears in original", config.containsKey("clone"));
+        config2.setProperty("original", Boolean.FALSE);
+        assertTrue("Wrong value of original property", config
+                .getBoolean("original"));
+    }
 }

Modified: jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestFileConfiguration.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestFileConfiguration.java?rev=418608&r1=418607&r2=418608&view=diff
==============================================================================
--- jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestFileConfiguration.java
(original)
+++ jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestFileConfiguration.java
Sun Jul  2 08:28:13 2006
@@ -17,7 +17,6 @@
 package org.apache.commons.configuration;
 
 import java.net.URL;
-import java.util.Iterator;
 import java.util.Properties;
 import java.io.File;
 import java.io.FileInputStream;
@@ -489,12 +488,34 @@
         config1.load();
         PropertiesConfiguration config2 = new PropertiesConfiguration(
                 RESOURCE_NAME);
+        compare(config1, config2);
+    }
 
-        for (Iterator it = config1.getKeys(); it.hasNext();)
-        {
-            String key = (String) it.next();
-            assertEquals("Wrong value for key " + key,
-                    config1.getProperty(key), config2.getProperty(key));
-        }
+    /**
+     * Tests cloning a file based configuration.
+     */
+    public void testClone() throws ConfigurationException
+    {
+        PropertiesConfiguration config = new PropertiesConfiguration(
+                RESOURCE_NAME);
+        PropertiesConfiguration copy = (PropertiesConfiguration) config.clone();
+        compare(config, copy);
+        assertNull("URL was not reset", copy.getURL());
+        assertNull("Base path was not reset", copy.getBasePath());
+        assertNull("File name was not reset", copy.getFileName());
+        assertNotSame("Reloading strategy was not reset", config
+                .getReloadingStrategy(), copy.getReloadingStrategy());
+    }
+
+    /**
+     * Helper method for comparing the content of two configuration objects.
+     *
+     * @param config1 the first configuration
+     * @param config2 the second configuration
+     */
+    private void compare(Configuration config1, Configuration config2)
+    {
+        StrictConfigurationComparator cc = new StrictConfigurationComparator();
+        assertTrue("Configurations are different", cc.compare(config1, config2));
     }
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message