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 2016/02/14 20:02:43 UTC

svn commit: r1730379 - in /commons/proper/configuration/trunk/src: main/java/org/apache/commons/configuration2/builder/FileBasedBuilderParametersImpl.java test/java/org/apache/commons/configuration2/builder/TestFileBasedBuilderParameters.java

Author: oheger
Date: Sun Feb 14 19:02:43 2016
New Revision: 1730379

URL: http://svn.apache.org/viewvc?rev=1730379&view=rev
Log:
[CONFIGURATION-619] Implemented inheritFrom() in FileBasedBuilderParametersImpl.

Here some more properties are taken into account.

Modified:
    commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/builder/FileBasedBuilderParametersImpl.java
    commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/builder/TestFileBasedBuilderParameters.java

Modified: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/builder/FileBasedBuilderParametersImpl.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/builder/FileBasedBuilderParametersImpl.java?rev=1730379&r1=1730378&r2=1730379&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/builder/FileBasedBuilderParametersImpl.java (original)
+++ commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/builder/FileBasedBuilderParametersImpl.java Sun Feb 14 19:02:43 2016
@@ -164,6 +164,37 @@ public class FileBasedBuilderParametersI
     }
 
     /**
+     * {@inheritDoc} This implementation takes some properties defined in this
+     * class into account.
+     */
+    @Override
+    public void inheritFrom(Map<String, ?> source)
+    {
+        super.inheritFrom(source);
+
+        FileBasedBuilderParametersImpl srcParams = fromParameters(source);
+        if (srcParams != null)
+        {
+            setFileSystem(srcParams.getFileHandler().getFileSystem());
+            setLocationStrategy(
+                    srcParams.getFileHandler().getLocationStrategy());
+            if (srcParams.getFileHandler().getEncoding() != null)
+            {
+                setEncoding(srcParams.getFileHandler().getEncoding());
+            }
+            if (srcParams.getReloadingDetectorFactory() != null)
+            {
+                setReloadingDetectorFactory(
+                        srcParams.getReloadingDetectorFactory());
+            }
+            if (srcParams.getReloadingRefreshDelay() != null)
+            {
+                setReloadingRefreshDelay(srcParams.getReloadingRefreshDelay());
+            }
+        }
+    }
+
+    /**
      * Returns the {@code FileHandler} managed by this object. This object is
      * updated every time the file location is changed.
      *

Modified: commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/builder/TestFileBasedBuilderParameters.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/builder/TestFileBasedBuilderParameters.java?rev=1730379&r1=1730378&r2=1730379&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/builder/TestFileBasedBuilderParameters.java (original)
+++ commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/builder/TestFileBasedBuilderParameters.java Sun Feb 14 19:02:43 2016
@@ -18,6 +18,7 @@ package org.apache.commons.configuration
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNotSame;
 import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertSame;
@@ -354,4 +355,83 @@ public class TestFileBasedBuilderParamet
         assertNull("Got refresh delay", params.getReloadingRefreshDelay());
         assertNull("Got a file name", params.getFileHandler().getFileName());
     }
+
+    /**
+     * Tests whether properties can be inherited from another object.
+     */
+    @Test
+    public void testInheritFrom()
+    {
+        FileBasedBuilderParametersImpl params =
+                new FileBasedBuilderParametersImpl();
+        params.setEncoding("ISO-8856-1");
+        params.setPath("A path");
+        params.setReloadingDetectorFactory(
+                EasyMock.createMock(ReloadingDetectorFactory.class));
+        params.setFileSystem(EasyMock.createMock(FileSystem.class));
+        params.setLocationStrategy(EasyMock.createMock(FileLocationStrategy.class));
+        params.setReloadingRefreshDelay(20160213171737L);
+        params.setThrowExceptionOnMissing(true);
+        FileBasedBuilderParametersImpl params2 =
+                new FileBasedBuilderParametersImpl();
+
+        params2.inheritFrom(params.getParameters());
+        assertEquals("Encoding not set", params.getFileHandler().getEncoding(),
+                params2.getFileHandler().getEncoding());
+        assertEquals("File system not set",
+                params.getFileHandler().getFileSystem(),
+                params2.getFileHandler().getFileSystem());
+        assertEquals("Location strategy not set",
+                params.getFileHandler().getLocationStrategy(),
+                params2.getFileHandler().getLocationStrategy());
+        assertEquals("Detector factory not set",
+                params.getReloadingDetectorFactory(),
+                params2.getReloadingDetectorFactory());
+        assertEquals("Refresh delay not set", params.getReloadingRefreshDelay(),
+                params2.getReloadingRefreshDelay());
+        assertNull("Path was copied", params2.getFileHandler().getPath());
+        assertEquals("Base properties not set", Boolean.TRUE,
+                params2.getParameters().get("throwExceptionOnMissing"));
+    }
+
+    /**
+     * Tests that missing properties in the passed in map are skipped by
+     * inheritFrom().
+     */
+    @Test
+    public void testInheritFromSkipMissingProperties()
+    {
+        String encoding = "UTF-16";
+        ReloadingDetectorFactory factory =
+                EasyMock.createMock(ReloadingDetectorFactory.class);
+        Long refreshDelay = 20160213172611L;
+        FileBasedBuilderParametersImpl params =
+                new FileBasedBuilderParametersImpl().setEncoding(encoding)
+                        .setReloadingDetectorFactory(factory)
+                        .setReloadingRefreshDelay(refreshDelay);
+
+        params.inheritFrom(
+                new FileBasedBuilderParametersImpl().getParameters());
+        assertEquals("Encoding overwritten", encoding,
+                params.getFileHandler().getEncoding());
+        assertEquals("Detector factory overwritten", factory,
+                params.getReloadingDetectorFactory());
+        assertEquals("Refresh delay overwritten", refreshDelay,
+                params.getReloadingRefreshDelay());
+    }
+
+    /**
+     * Tests inheritFrom() if no parameters object can be found in the map.
+     */
+    @Test
+    public void testInheritFromNoParametersObject()
+    {
+        FileBasedBuilderParametersImpl params =
+                new FileBasedBuilderParametersImpl()
+                        .setReloadingRefreshDelay(20160213211429L);
+
+        params.inheritFrom(new HashMap<String, Object>());
+        assertNotNull("Properties were overwritten",
+                params.getReloadingRefreshDelay());
+    }
 }