[prev in list] [next in list] [prev in thread] [next in thread] 

List:       jakarta-commons-dev
Subject:    svn commit: r1546642 - in /commons/proper/configuration/trunk/src: main/java/org/apache/commons/conf
From:       oheger () apache ! org
Date:       2013-11-29 20:53:05
Message-ID: 20131129205305.998102388999 () eris ! apache ! org
[Download RAW message or body]

Author: oheger
Date: Fri Nov 29 20:53:05 2013
New Revision: 1546642

URL: http://svn.apache.org/r1546642
Log:
Added CopyObjectDefaultHandler class.

This is a DefaultParametersHandler implementation that copies the properties of
a given object onto the object to be initialized.

Added:
    commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/CopyObjectDefaultHandler.java
  commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/builder/TestCopyObjectDefaultHandler.java


Added: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/CopyObjectDefaultHandler.java
                
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org \
/apache/commons/configuration/builder/CopyObjectDefaultHandler.java?rev=1546642&view=auto
 ==============================================================================
--- commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/CopyObjectDefaultHandler.java \
                (added)
+++ commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/CopyObjectDefaultHandler.java \
Fri Nov 29 20:53:05 2013 @@ -0,0 +1,109 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.builder;
+
+import org.apache.commons.beanutils.PropertyUtils;
+import org.apache.commons.configuration.ConfigurationRuntimeException;
+
+/**
+ * <p>
+ * A specialized implementation of {@code DefaultParametersHandler} that copies
+ * the properties of a {@link BuilderParameters} object (passed at construction
+ * time) onto the object to be initialized.
+ * </p>
+ * <p>
+ * Using this handler implementation makes specifying default values pretty
+ * straight-forward: Just create a corresponding parameters object, initialize
+ * it as desired, and pass it to this class. When invoked the handler uses
+ * functionality from <em>Commons BeanUtils</em> to copy all properties defined
+ * in the associated parameters object onto the target object. This is based on
+ * reflection. Properties not available for the target object are silently
+ * ignored. If an exception occurs during the copy operation, it is re-thrown as
+ * a runtime exception.
+ * </p>
+ * <p>
+ * Note that there is no default way to create a defensive copy of the passed in
+ * parameters object; therefore, the reference is stored. This makes it possible
+ * to change the parameters object later on, and the changes will be effective
+ * when initializing objects afterwards. Client code should not rely on this
+ * feature.
+ * </p>
+ *
+ * @version $Id: $
+ * @since 2.0
+ */
+public class CopyObjectDefaultHandler implements
+        DefaultParametersHandler<Object>
+{
+    /** The source object with the properties to be initialized. */
+    private final BuilderParameters source;
+
+    /**
+     * Creates a new instance of {@code CopyObjectDefaultHandler} and
+     * initializes it with the specified source object. The properties defined
+     * by the source object are copied onto the objects to be initialized.
+     *
+     * @param src the source object (must not be <b>null</b>)
+     * @throws IllegalArgumentException if the source object is <b>null</b>
+     */
+    public CopyObjectDefaultHandler(BuilderParameters src)
+    {
+        if (src == null)
+        {
+            throw new IllegalArgumentException(
+                    "Source object must not be null!");
+        }
+        source = src;
+    }
+
+    /**
+     * Returns the source object of this handler. This is the object whose
+     * properties are copied on the objects to be initialized.
+     *
+     * @return the source object of this {@code CopyObjectDefaultHandler}
+     */
+    public BuilderParameters getSource()
+    {
+        return source;
+    }
+
+    /**
+     * {@inheritDoc} This implementation uses
+     * {@code PropertyUtils.copyProperties()} to copy all defined properties
+     * from the source object onto the passed in parameters object. Both the map
+     * with properties (obtained via the {@code getParameters()} method of the
+     * source parameters object) and other properties of the source object are
+     * copied.
+     *
+     * @throws ConfigurationRuntimeException if an exception occurs
+     * @see BuilderParameters#getParameters()
+     */
+    public void initializeDefaults(Object parameters)
+    {
+        try
+        {
+            PropertyUtils.copyProperties(parameters, getSource()
+                    .getParameters());
+            PropertyUtils.copyProperties(parameters, getSource());
+        }
+        catch (Exception e)
+        {
+            // Handle all reflection-related exceptions the same way
+            throw new ConfigurationRuntimeException(e);
+        }
+    }
+}

Added: commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/builder/TestCopyObjectDefaultHandler.java
                
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/java/org \
/apache/commons/configuration/builder/TestCopyObjectDefaultHandler.java?rev=1546642&view=auto
 ==============================================================================
--- commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/builder/TestCopyObjectDefaultHandler.java \
                (added)
+++ commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/builder/TestCopyObjectDefaultHandler.java \
Fri Nov 29 20:53:05 2013 @@ -0,0 +1,112 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.builder;
+
+import static org.junit.Assert.assertEquals;
+
+import java.util.Map;
+
+import org.apache.commons.configuration.ConfigurationRuntimeException;
+import org.apache.commons.configuration.tree.ExpressionEngine;
+import org.easymock.EasyMock;
+import org.junit.Test;
+
+/**
+ * Test class for {@code CopyObjectDefaultHandler}.
+ *
+ * @version $Id: $
+ */
+public class TestCopyObjectDefaultHandler
+{
+    /**
+     * Tries to create an instance without a source object.
+     */
+    @Test(expected = IllegalArgumentException.class)
+    public void testInitNull()
+    {
+        new CopyObjectDefaultHandler(null);
+    }
+
+    /**
+     * Tests whether default values can be copied onto an object of the same
+     * type.
+     */
+    @Test
+    public void testInitializeDefaultsSameType()
+    {
+        final Long refresh = 50000L;
+        FileBasedBuilderParametersImpl source =
+                new FileBasedBuilderParametersImpl();
+        source.setReloadingRefreshDelay(refresh).setThrowExceptionOnMissing(
+                true);
+        CopyObjectDefaultHandler handler = new CopyObjectDefaultHandler(source);
+        FileBasedBuilderParametersImpl copy =
+                new FileBasedBuilderParametersImpl();
+        handler.initializeDefaults(copy);
+        Map<String, Object> map = copy.getParameters();
+        assertEquals("Wrong exception flag", Boolean.TRUE,
+                map.get("throwExceptionOnMissing"));
+        assertEquals("Wrong refresh", refresh, copy.getReloadingRefreshDelay());
+    }
+
+    /**
+     * Tests whether a base type can be initialized with default values. Unknown
+     * properties should silently be ignored.
+     */
+    @Test
+    public void testInitializeDefaultsBaseType()
+    {
+        final Long refresh = 50000L;
+        XMLBuilderParametersImpl paramsXml = new XMLBuilderParametersImpl();
+        paramsXml
+                .setValidating(true)
+                .setExpressionEngine(
+                        EasyMock.createMock(ExpressionEngine.class))
+                .setReloadingRefreshDelay(refresh);
+        CopyObjectDefaultHandler handler =
+                new CopyObjectDefaultHandler(paramsXml);
+        FileBasedBuilderParametersImpl paramsFb =
+                new FileBasedBuilderParametersImpl();
+        handler.initializeDefaults(paramsFb);
+        assertEquals("Wrong refresh", refresh,
+                paramsFb.getReloadingRefreshDelay());
+    }
+
+    /**
+     * Tests whether exceptions during copying are re-thrown as runtime
+     * exceptions.
+     */
+    @Test(expected = ConfigurationRuntimeException.class)
+    public void testInitializeDefaultsException()
+    {
+        ExpressionEngine engine = EasyMock.createMock(ExpressionEngine.class);
+        XMLBuilderParametersImpl source = new XMLBuilderParametersImpl();
+        source.setExpressionEngine(engine);
+        XMLBuilderParametersImpl dest = new XMLBuilderParametersImpl()
+        {
+            @Override
+            public HierarchicalBuilderParametersImpl setExpressionEngine(
+                    ExpressionEngine engine)
+            {
+                throw new ConfigurationRuntimeException("Test exception");
+            }
+        };
+
+        CopyObjectDefaultHandler handler = new CopyObjectDefaultHandler(source);
+        handler.initializeDefaults(dest);
+    }
+}


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic