commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ohe...@apache.org
Subject svn commit: r419330 - in /jakarta/commons/proper/configuration/trunk/src: java/org/apache/commons/configuration/ test/org/apache/commons/configuration/
Date Wed, 05 Jul 2006 20:29:51 GMT
Author: oheger
Date: Wed Jul  5 13:29:51 2006
New Revision: 419330

URL: http://svn.apache.org/viewvc?rev=419330&view=rev
Log:
Added an utility method to ConfigurationUtils for cloning a configuration

Added:
    jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/NonCloneableConfiguration.java
  (with props)
Modified:
    jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/ConfigurationUtils.java
    jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestConfigurationUtils.java

Modified: jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/ConfigurationUtils.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/ConfigurationUtils.java?rev=419330&r1=419329&r2=419330&view=diff
==============================================================================
--- jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/ConfigurationUtils.java
(original)
+++ jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/ConfigurationUtils.java
Wed Jul  5 13:29:51 2006
@@ -22,6 +22,8 @@
 import java.io.PrintStream;
 import java.io.PrintWriter;
 import java.io.StringWriter;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
 import java.net.MalformedURLException;
 import java.net.URL;
 import java.net.URLDecoder;
@@ -49,6 +51,9 @@
     /** Constant for the resource path separator.*/
     static final String RESOURCE_PATH_SEPARATOR = "/";
 
+    /** Constant for the name of the clone() method.*/
+    private static final String METHOD_CLONE = "clone";
+
     /** The logger.*/
     private static Log log = LogFactory.getLog(ConfigurationUtils.class);
 
@@ -181,6 +186,83 @@
             HierarchicalConfiguration hc = new HierarchicalConfiguration();
             ConfigurationUtils.copy(conf, hc);
             return hc;
+        }
+    }
+
+    /**
+     * Clones the given configuration object if this is possible. If the passed
+     * in configuration object implements the <code>Cloneable</code>
+     * interface, its <code>clone()</code> method will be invoked. Otherwise
+     * an exception will be thrown.
+     *
+     * @param config the configuration object to be cloned (can be <b>null</b>)
+     * @return the cloned configuration (<b>null</b> if the argument was
+     * <b>null</b>, too)
+     * @throws ConfigurationRuntimeException if cloning is not supported for
+     * this object
+     * @since 1.3
+     */
+    public static Configuration cloneConfiguration(Configuration config)
+            throws ConfigurationRuntimeException
+    {
+        if (config == null)
+        {
+            return null;
+        }
+        else
+        {
+            try
+            {
+                return (Configuration) clone(config);
+            }
+            catch (CloneNotSupportedException cnex)
+            {
+                throw new ConfigurationRuntimeException(cnex);
+            }
+        }
+    }
+
+    /**
+     * An internally used helper method for cloning objects. This implementation
+     * is not very sophisticated nor efficient. Maybe it can be replaced by an
+     * implementation from Commons Lang later. The method checks whether the
+     * passed in object implements the <code>Cloneable</code> interface. If
+     * this is the case, the <code>clone()</code> method is invoked by
+     * reflection. Errors that occur during the cloning process are re-thrown as
+     * runtime exceptions.
+     *
+     * @param obj the object to be cloned
+     * @return the cloned object
+     * @throws CloneNotSupportedException if the object cannot be cloned
+     */
+    static Object clone(Object obj) throws CloneNotSupportedException
+    {
+        if (obj instanceof Cloneable)
+        {
+            try
+            {
+                Method m = obj.getClass().getMethod(METHOD_CLONE, null);
+                return m.invoke(obj, null);
+            }
+            catch (NoSuchMethodException nmex)
+            {
+                throw new CloneNotSupportedException(
+                        "No clone() method found for class"
+                                + obj.getClass().getName());
+            }
+            catch (IllegalAccessException iaex)
+            {
+                throw new ConfigurationRuntimeException(iaex);
+            }
+            catch (InvocationTargetException itex)
+            {
+                throw new ConfigurationRuntimeException(itex);
+            }
+        }
+        else
+        {
+            throw new CloneNotSupportedException(obj.getClass().getName()
+                    + " does not implement Cloneable");
         }
     }
 

Added: jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/NonCloneableConfiguration.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/NonCloneableConfiguration.java?rev=419330&view=auto
==============================================================================
--- jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/NonCloneableConfiguration.java
(added)
+++ jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/NonCloneableConfiguration.java
Wed Jul  5 13:29:51 2006
@@ -0,0 +1,68 @@
+/*
+ * Copyright 2004 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.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.configuration;
+
+import java.util.Iterator;
+
+/**
+ * A specialized configuration implementation that does not support cloning.
+ * This class is only used in some test cases for testing implementations of
+ * clone() methods. It does not make much sense otherwise; all methods are just
+ * dummies.
+ *
+ * @version $Id$
+ */
+public class NonCloneableConfiguration extends AbstractConfiguration
+{
+    /**
+     * Dummy implementation of this method.
+     */
+    protected void addPropertyDirect(String key, Object value)
+    {
+    }
+
+    /**
+     * Dummy implementation of this method.
+     */
+    public boolean isEmpty()
+    {
+        return true;
+    }
+
+    /**
+     * Dummy implementation of this method.
+     */
+    public boolean containsKey(String key)
+    {
+        return false;
+    }
+
+    /**
+     * Dummy implementation of this method.
+     */
+    public Iterator getKeys()
+    {
+        return null;
+    }
+
+    /**
+     * Dummy implementation of this method.
+     */
+    public Object getProperty(String key)
+    {
+        return null;
+    }
+}

Propchange: jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/NonCloneableConfiguration.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/NonCloneableConfiguration.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/NonCloneableConfiguration.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestConfigurationUtils.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestConfigurationUtils.java?rev=419330&r1=419329&r2=419330&view=diff
==============================================================================
--- jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestConfigurationUtils.java
(original)
+++ jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestConfigurationUtils.java
Wed Jul  5 13:29:51 2006
@@ -243,4 +243,44 @@
         assertNull("Wrong conversion result for null config",
                 ConfigurationUtils.convertToHierarchical(null));
     }
+
+    /**
+     * Tests cloning a configuration that supports this operation.
+     */
+    public void testCloneConfiguration()
+    {
+        HierarchicalConfiguration conf = new HierarchicalConfiguration();
+        conf.addProperty("test", "yes");
+        HierarchicalConfiguration copy = (HierarchicalConfiguration) ConfigurationUtils
+                .cloneConfiguration(conf);
+        assertNotSame("Same object was returned", conf, copy);
+        assertEquals("Property was not cloned", "yes", copy.getString("test"));
+    }
+
+    /**
+     * Tests cloning a configuration that does not support this operation. This
+     * should cause an exception.
+     */
+    public void testCloneConfigurationNotSupported()
+    {
+        Configuration myNonCloneableConfig = new NonCloneableConfiguration();
+        try
+        {
+            ConfigurationUtils.cloneConfiguration(myNonCloneableConfig);
+            fail("Could clone non cloneable config!");
+        }
+        catch (ConfigurationRuntimeException crex)
+        {
+            // ok
+        }
+    }
+
+    /**
+     * Tests cloning a <b>null</b> configuration.
+     */
+    public void testCloneConfigurationNull()
+    {
+        assertNull("Wrong return value", ConfigurationUtils
+                .cloneConfiguration(null));
+    }
 }



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