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 2013/01/17 22:09:34 UTC
svn commit: r1434915 - in /commons/proper/configuration/trunk/src:
main/java/org/apache/commons/configuration/ConfigurationUtils.java
test/java/org/apache/commons/configuration/TestConfigurationUtils.java
Author: oheger
Date: Thu Jan 17 21:09:33 2013
New Revision: 1434915
URL: http://svn.apache.org/viewvc?rev=1434915&view=rev
Log:
Added a utility method for optionally cloning an object to ConfigurationUtils.
Modified:
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/ConfigurationUtils.java
commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestConfigurationUtils.java
Modified: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/ConfigurationUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/ConfigurationUtils.java?rev=1434915&r1=1434914&r2=1434915&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/ConfigurationUtils.java (original)
+++ commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/ConfigurationUtils.java Thu Jan 17 21:09:33 2013
@@ -306,6 +306,30 @@ public final class ConfigurationUtils
}
/**
+ * Returns a clone of the passed in object if cloning is supported or the
+ * object itself if not. This method checks whether the passed in object
+ * implements the {@code Cloneable} interface. If this is the case, the
+ * {@code clone()} method is invoked. Otherwise, the object is directly
+ * returned. Errors that might occur during reflection calls are caught and
+ * also cause this method to return the original object.
+ *
+ * @param obj the object to be cloned
+ * @return the result of the cloning attempt
+ * @since 2.0
+ */
+ public static Object cloneIfPossible(Object obj)
+ {
+ try
+ {
+ return clone(obj);
+ }
+ catch (Exception ex)
+ {
+ return obj;
+ }
+ }
+
+ /**
* 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
Modified: commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestConfigurationUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestConfigurationUtils.java?rev=1434915&r1=1434914&r2=1434915&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestConfigurationUtils.java (original)
+++ commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestConfigurationUtils.java Thu Jan 17 21:09:33 2013
@@ -30,9 +30,11 @@ import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
+import java.util.Map;
import junitx.framework.ListAssert;
+import org.apache.commons.configuration.builder.XMLBuilderParametersImpl;
import org.apache.commons.configuration.tree.DefaultExpressionEngine;
import org.apache.commons.configuration.tree.ExpressionEngine;
import org.junit.After;
@@ -437,6 +439,68 @@ public class TestConfigurationUtils
}
/**
+ * Tests whether an object can be cloned which supports cloning.
+ */
+ @Test
+ public void testCloneIfPossibleSupported()
+ {
+ XMLBuilderParametersImpl params = new XMLBuilderParametersImpl();
+ params.setPublicID("testID");
+ params.setSchemaValidation(true);
+ XMLBuilderParametersImpl clone =
+ (XMLBuilderParametersImpl) ConfigurationUtils
+ .cloneIfPossible(params);
+ assertNotSame("No clone was created", params, clone);
+ Map<String, Object> map = clone.getParameters();
+ for (Map.Entry<String, Object> e : params.getParameters().entrySet())
+ {
+ if (!e.getKey().startsWith("config-"))
+ {
+ assertEquals("Wrong value for field " + e.getKey(),
+ e.getValue(), map.get(e.getKey()));
+ }
+ }
+ }
+
+ /**
+ * Tests cloneIfPossible() if the passed in object does not support cloning.
+ */
+ @Test
+ public void testCloneIfPossibleNotSupported()
+ {
+ Long value = 20130116221714L;
+ assertSame("Wrong result", value,
+ ConfigurationUtils.cloneIfPossible(value));
+ }
+
+ /**
+ * Tests whether errors are handled correctly by cloneIfPossible().
+ */
+ @Test
+ public void testCloneIfPossibleError()
+ {
+ XMLBuilderParametersImpl params = new XMLBuilderParametersImpl()
+ {
+ @Override
+ public XMLBuilderParametersImpl clone()
+ {
+ throw new ConfigurationRuntimeException();
+ }
+ };
+ assertSame("Wrong result", params,
+ ConfigurationUtils.cloneIfPossible(params));
+ }
+
+ /**
+ * Tests whether cloneIfPossible() can handle null parameters.
+ */
+ @Test
+ public void testCloneIfPossibleNull()
+ {
+ assertNull("Wrong result", ConfigurationUtils.cloneIfPossible(null));
+ }
+
+ /**
* Tests whether runtime exceptions can be enabled.
*/
@Test(expected = ConfigurationRuntimeException.class)