Return-Path: X-Original-To: apmail-commons-commits-archive@minotaur.apache.org Delivered-To: apmail-commons-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id AF3FCEEF5 for ; Thu, 17 Jan 2013 21:09:57 +0000 (UTC) Received: (qmail 99169 invoked by uid 500); 17 Jan 2013 21:09:57 -0000 Delivered-To: apmail-commons-commits-archive@commons.apache.org Received: (qmail 99101 invoked by uid 500); 17 Jan 2013 21:09:57 -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 99091 invoked by uid 99); 17 Jan 2013 21:09:57 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 17 Jan 2013 21:09:57 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.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; Thu, 17 Jan 2013 21:09:54 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id 4C87C2388847; Thu, 17 Jan 2013 21:09:34 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: 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 Date: Thu, 17 Jan 2013 21:09:34 -0000 To: commits@commons.apache.org From: oheger@apache.org X-Mailer: svnmailer-1.0.8-patched Message-Id: <20130117210934.4C87C2388847@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org 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 map = clone.getParameters(); + for (Map.Entry 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)