commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ohe...@apache.org
Subject svn commit: r420116 - in /jakarta/commons/proper/configuration/trunk/src: java/org/apache/commons/configuration/CompositeConfiguration.java test/org/apache/commons/configuration/TestCompositeConfiguration.java
Date Sat, 08 Jul 2006 15:09:47 GMT
Author: oheger
Date: Sat Jul  8 08:09:47 2006
New Revision: 420116

URL: http://svn.apache.org/viewvc?rev=420116&view=rev
Log:
Added a clone() method to CompositeConfiguration

Modified:
    jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/CompositeConfiguration.java
    jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestCompositeConfiguration.java

Modified: jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/CompositeConfiguration.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/CompositeConfiguration.java?rev=420116&r1=420115&r2=420116&view=diff
==============================================================================
--- jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/CompositeConfiguration.java
(original)
+++ jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/CompositeConfiguration.java
Sat Jul  8 08:09:47 2006
@@ -1,5 +1,5 @@
 /*
- * Copyright 2001-2005 The Apache Software Foundation.
+ * Copyright 2001-2006 The Apache Software Foundation.
  *
  * Licensed under the Apache License, Version 2.0 (the "License")
  * you may not use this file except in compliance with the License.
@@ -34,6 +34,7 @@
  * @version $Id$
  */
 public class CompositeConfiguration extends AbstractConfiguration
+implements Cloneable
 {
     /** List holding all the configuration */
     private List configList = new LinkedList();
@@ -360,5 +361,45 @@
     public Configuration getInMemoryConfiguration()
     {
         return inMemoryConfiguration;
+    }
+
+    /**
+     * Returns a copy of this object. This implementation will create a deep
+     * clone, i.e. all configurations contained in this composite will also be
+     * cloned. This only works if all contained configurations support cloning;
+     * otherwise a runtime exception will be thrown. Registered event handlers
+     * won't get cloned.
+     *
+     * @return the copy
+     */
+    public Object clone()
+    {
+        try
+        {
+            CompositeConfiguration copy = (CompositeConfiguration) super
+                    .clone();
+            copy.clearConfigurationListeners();
+            copy.configList = new LinkedList();
+            copy.inMemoryConfiguration = ConfigurationUtils
+                    .cloneConfiguration(getInMemoryConfiguration());
+            copy.configList.add(copy.inMemoryConfiguration);
+
+            for (int i = 0; i < getNumberOfConfigurations(); i++)
+            {
+                Configuration config = getConfiguration(i);
+                if (config != getInMemoryConfiguration())
+                {
+                    copy.addConfiguration(ConfigurationUtils
+                            .cloneConfiguration(config));
+                }
+            }
+
+            return copy;
+        }
+        catch (CloneNotSupportedException cnex)
+        {
+            // cannot happen
+            throw new ConfigurationRuntimeException(cnex);
+        }
     }
 }

Modified: jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestCompositeConfiguration.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestCompositeConfiguration.java?rev=420116&r1=420115&r2=420116&view=diff
==============================================================================
--- jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestCompositeConfiguration.java
(original)
+++ jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestCompositeConfiguration.java
Sat Jul  8 08:09:47 2006
@@ -1,5 +1,5 @@
 /*
- * Copyright 2001-2004 The Apache Software Foundation.
+ * Copyright 2001-2006 The Apache Software Foundation.
  *
  * Licensed under the Apache License, Version 2.0 (the "License")
  * you may not use this file except in compliance with the License.
@@ -23,6 +23,9 @@
 import java.util.NoSuchElementException;
 import java.util.Collection;
 
+import org.apache.commons.configuration.event.ConfigurationEvent;
+import org.apache.commons.configuration.event.ConfigurationListener;
+
 import junit.framework.TestCase;
 
 /**
@@ -78,7 +81,7 @@
         cc.addConfiguration(conf1);
         cc.addConfiguration(conf2);
         List l = cc.getList("packages");
-        assertTrue(l.contains("packagea"));       
+        assertTrue(l.contains("packagea"));
     }
 
     public void testGetProperty() throws Exception
@@ -437,7 +440,7 @@
         assertEquals("3rd element", "foo.bar3", array[2]);
     }
 
-    public void testInstanciateWithCollectiono()
+    public void testInstanciateWithCollection()
     {
         Collection configs = new ArrayList();
         configs.add(xmlConf);
@@ -447,5 +450,61 @@
         CompositeConfiguration config = new CompositeConfiguration(configs);
         assertEquals("Number of configurations", 4, config.getNumberOfConfigurations());
         assertTrue("The in memory configuration is not empty", config.getInMemoryConfiguration().isEmpty());
+    }
+
+    public void testClone()
+    {
+        CompositeConfiguration cc2 = (CompositeConfiguration) cc.clone();
+        assertEquals("Wrong number of contained configurations", cc
+                .getNumberOfConfigurations(), cc2.getNumberOfConfigurations());
+
+        StrictConfigurationComparator comp = new StrictConfigurationComparator();
+        for (int i = 0; i < cc.getNumberOfConfigurations(); i++)
+        {
+            assertEquals("Wrong configuration class at " + i, cc
+                    .getConfiguration(i).getClass(), cc2.getConfiguration(i)
+                    .getClass());
+            assertNotSame("Configuration was not cloned", cc
+                    .getConfiguration(i), cc2.getConfiguration(i));
+            assertTrue("Configurations at " + i + " not equal", comp.compare(cc
+                    .getConfiguration(i), cc2.getConfiguration(i)));
+        }
+
+        assertTrue("Configurations are not equal", comp.compare(cc, cc2));
+    }
+
+    /**
+     * Tests cloning if one of the contained configurations does not support
+     * this operation. This should cause an exception.
+     */
+    public void testCloneNotSupported()
+    {
+        cc.addConfiguration(new NonCloneableConfiguration());
+        try
+        {
+            cc.clone();
+            fail("Could clone non cloneable configuration!");
+        }
+        catch (ConfigurationRuntimeException crex)
+        {
+            // ok
+        }
+    }
+
+    /**
+     * Ensures that event listeners are not cloned.
+     */
+    public void testCloneEventListener()
+    {
+        cc.addConfigurationListener(new ConfigurationListener()
+        {
+            public void configurationChanged(ConfigurationEvent event)
+            {
+                // Just a dummy
+            }
+        });
+        CompositeConfiguration cc2 = (CompositeConfiguration) cc.clone();
+        assertTrue("Listeners have been cloned", cc2
+                .getConfigurationListeners().isEmpty());
     }
 }



---------------------------------------------------------------------
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