commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ohe...@apache.org
Subject svn commit: r1437170 - in /commons/proper/configuration/trunk/src: main/java/org/apache/commons/configuration/interpol/ConfigurationInterpolator.java test/java/org/apache/commons/configuration/interpol/TestConfigurationInterpolator.java
Date Tue, 22 Jan 2013 21:06:27 GMT
Author: oheger
Date: Tue Jan 22 21:06:26 2013
New Revision: 1437170

URL: http://svn.apache.org/viewvc?rev=1437170&view=rev
Log:
Added a method for obtaining a ConfigurationInterpolator based on an InterpolatorSpecification
object.

Modified:
    commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/interpol/ConfigurationInterpolator.java
    commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/interpol/TestConfigurationInterpolator.java

Modified: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/interpol/ConfigurationInterpolator.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/interpol/ConfigurationInterpolator.java?rev=1437170&r1=1437169&r2=1437170&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/interpol/ConfigurationInterpolator.java
(original)
+++ commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/interpol/ConfigurationInterpolator.java
Tue Jan 22 21:06:26 2013
@@ -114,6 +114,31 @@ public class ConfigurationInterpolator
     }
 
     /**
+     * Creates a new {@code ConfigurationInterpolator} instance based on the
+     * passed in specification object. If the {@code InterpolatorSpecification}
+     * already contains a {@code ConfigurationInterpolator} object, it is used
+     * directly. Otherwise, a new instance is created and initialized with the
+     * properties stored in the specification.
+     *
+     * @param spec the {@code InterpolatorSpecification} (must not be
+     *        <b>null</b>)
+     * @return the {@code ConfigurationInterpolator} obtained or created based
+     *         on the given specification
+     * @throws IllegalArgumentException if the specification is <b>null</b>
+     */
+    public static ConfigurationInterpolator fromSpecification(
+            InterpolatorSpecification spec)
+    {
+        if (spec == null)
+        {
+            throw new IllegalArgumentException(
+                    "InterpolatorSpecification must not be null!");
+        }
+        return (spec.getInterpolator() != null) ? spec.getInterpolator()
+                : createInterpolator(spec);
+    }
+
+    /**
      * Returns a map with the currently registered {@code Lookup} objects and
      * their prefixes. This is a snapshot copy of the internally used map. So
      * modifications of this map do not effect this instance.
@@ -408,4 +433,21 @@ public class ConfigurationInterpolator
             }
         });
     }
+
+    /**
+     * Creates a new instance based on the properties in the given specification
+     * object.
+     *
+     * @param spec the {@code InterpolatorSpecification}
+     * @return the newly created instance
+     */
+    private static ConfigurationInterpolator createInterpolator(
+            InterpolatorSpecification spec)
+    {
+        ConfigurationInterpolator ci = new ConfigurationInterpolator();
+        ci.addDefaultLookups(spec.getDefaultLookups());
+        ci.registerLookups(spec.getPrefixLookups());
+        ci.setParentInterpolator(spec.getParentInterpolator());
+        return ci;
+    }
 }

Modified: commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/interpol/TestConfigurationInterpolator.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/interpol/TestConfigurationInterpolator.java?rev=1437170&r1=1437169&r2=1437170&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/interpol/TestConfigurationInterpolator.java
(original)
+++ commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/interpol/TestConfigurationInterpolator.java
Tue Jan 22 21:06:26 2013
@@ -484,4 +484,57 @@ public class TestConfigurationInterpolat
         assertEquals("Wrong result (2)", "C:\\java\\1.4",
                 interpolator.interpolate(var));
     }
+
+    /**
+     * Tries to obtain an instance from a null specification.
+     */
+    @Test(expected = IllegalArgumentException.class)
+    public void testFromSpecificationNull()
+    {
+        ConfigurationInterpolator.fromSpecification(null);
+    }
+
+    /**
+     * Tests fromSpecification() if the specification contains an instance.
+     */
+    @Test
+    public void testFromSpecificationInterpolator()
+    {
+        ConfigurationInterpolator ci =
+                EasyMock.createMock(ConfigurationInterpolator.class);
+        EasyMock.replay(ci);
+        InterpolatorSpecification spec =
+                new InterpolatorSpecification.Builder()
+                        .withDefaultLookup(EasyMock.createMock(Lookup.class))
+                        .withParentInterpolator(interpolator)
+                        .withInterpolator(ci).create();
+        assertSame("Wrong result", ci,
+                ConfigurationInterpolator.fromSpecification(spec));
+    }
+
+    /**
+     * Tests fromSpecification() if a new instance has to be created.
+     */
+    @Test
+    public void testFromSpecificationNewInstance()
+    {
+        Lookup defLookup = EasyMock.createMock(Lookup.class);
+        Lookup preLookup = EasyMock.createMock(Lookup.class);
+        EasyMock.replay(defLookup, preLookup);
+        InterpolatorSpecification spec =
+                new InterpolatorSpecification.Builder()
+                        .withDefaultLookup(defLookup)
+                        .withPrefixLookup("p", preLookup)
+                        .withParentInterpolator(interpolator).create();
+        ConfigurationInterpolator ci =
+                ConfigurationInterpolator.fromSpecification(spec);
+        assertEquals("Wrong number of default lookups", 1, ci
+                .getDefaultLookups().size());
+        assertTrue("Wrong default lookup",
+                ci.getDefaultLookups().contains(defLookup));
+        assertEquals("Wrong number of prefix lookups", 1, ci.getLookups()
+                .size());
+        assertSame("Wrong prefix lookup", preLookup, ci.getLookups().get("p"));
+        assertSame("Wrong parent", interpolator, ci.getParentInterpolator());
+    }
 }



Mime
View raw message