commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ohe...@apache.org
Subject svn commit: r1545072 - in /commons/proper/configuration/trunk/src: main/java/org/apache/commons/configuration/builder/fluent/Parameters.java test/java/org/apache/commons/configuration/builder/fluent/TestParameters.java
Date Sun, 24 Nov 2013 20:37:06 GMT
Author: oheger
Date: Sun Nov 24 20:37:05 2013
New Revision: 1545072

URL: http://svn.apache.org/r1545072
Log:
Parameters objects now implement multiple interfaces.

The proxy objects created by Parameters now not only implement one single
parameters interface but also all logic base interfaces. This can be used to
apply certain logic based on their inheritance structure.

Modified:
    commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/fluent/Parameters.java
    commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/builder/fluent/TestParameters.java

Modified: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/fluent/Parameters.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/fluent/Parameters.java?rev=1545072&r1=1545071&r2=1545072&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/fluent/Parameters.java
(original)
+++ commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/fluent/Parameters.java
Sun Nov 24 20:37:05 2013
@@ -87,8 +87,8 @@ public final class Parameters
      */
     public FileBasedBuilderParameters fileBased()
     {
-        return createParametersProxy(FileBasedBuilderParameters.class,
-                new FileBasedBuilderParametersImpl());
+        return createParametersProxy(new FileBasedBuilderParametersImpl(),
+                FileBasedBuilderParameters.class);
     }
 
     /**
@@ -99,8 +99,8 @@ public final class Parameters
      */
     public CombinedBuilderParameters combined()
     {
-        return createParametersProxy(CombinedBuilderParameters.class,
-                new CombinedBuilderParametersImpl());
+        return createParametersProxy(new CombinedBuilderParametersImpl(),
+                CombinedBuilderParameters.class);
     }
 
     /**
@@ -110,8 +110,8 @@ public final class Parameters
      */
     public JndiBuilderParameters jndi()
     {
-        return createParametersProxy(JndiBuilderParameters.class,
-                new JndiBuilderParametersImpl());
+        return createParametersProxy(new JndiBuilderParametersImpl(),
+                JndiBuilderParameters.class);
     }
 
     /**
@@ -122,8 +122,9 @@ public final class Parameters
      */
     public HierarchicalBuilderParameters hierarchical()
     {
-        return createParametersProxy(HierarchicalBuilderParameters.class,
-                new HierarchicalBuilderParametersImpl());
+        return createParametersProxy(new HierarchicalBuilderParametersImpl(),
+                HierarchicalBuilderParameters.class,
+                FileBasedBuilderParameters.class);
     }
 
     /**
@@ -133,8 +134,9 @@ public final class Parameters
      */
     public XMLBuilderParameters xml()
     {
-        return createParametersProxy(XMLBuilderParameters.class,
-                new XMLBuilderParametersImpl());
+        return createParametersProxy(new XMLBuilderParametersImpl(),
+                XMLBuilderParameters.class, FileBasedBuilderParameters.class,
+                HierarchicalBuilderParameters.class);
     }
 
     /**
@@ -145,8 +147,9 @@ public final class Parameters
      */
     public PropertiesBuilderParameters properties()
     {
-        return createParametersProxy(PropertiesBuilderParameters.class,
-                new PropertiesBuilderParametersImpl());
+        return createParametersProxy(new PropertiesBuilderParametersImpl(),
+                PropertiesBuilderParameters.class,
+                FileBasedBuilderParameters.class);
     }
 
     /**
@@ -157,8 +160,8 @@ public final class Parameters
      */
     public MultiFileBuilderParameters multiFile()
     {
-        return createParametersProxy(MultiFileBuilderParameters.class,
-                new MultiFileBuilderParametersImpl());
+        return createParametersProxy(new MultiFileBuilderParametersImpl(),
+                MultiFileBuilderParameters.class);
     }
 
     /**
@@ -169,8 +172,8 @@ public final class Parameters
      */
     public DatabaseBuilderParameters database()
     {
-        return createParametersProxy(DatabaseBuilderParameters.class,
-                new DatabaseBuilderParametersImpl());
+        return createParametersProxy(new DatabaseBuilderParametersImpl(),
+                DatabaseBuilderParameters.class);
     }
 
     /**
@@ -178,16 +181,21 @@ public final class Parameters
      * given implementation object.
      *
      * @param <T> the type of the parameters interface
-     * @param ifcClass the interface class
      * @param target the implementing target object
+     * @param ifcClass the interface class
+     * @param superIfcs an array with additional interface classes to be
+     *        implemented
      * @return the proxy object
      */
-    private static <T> T createParametersProxy(Class<T> ifcClass, Object target)
+    private static <T> T createParametersProxy(Object target,
+            Class<T> ifcClass, Class<?>... superIfcs)
     {
+        Class<?>[] ifcClasses = new Class<?>[1 + superIfcs.length];
+        ifcClasses[0] = ifcClass;
+        System.arraycopy(superIfcs, 0, ifcClasses, 1, superIfcs.length);
         return ifcClass.cast(Proxy.newProxyInstance(
-                Parameters.class.getClassLoader(), new Class<?>[] {
-                    ifcClass
-                }, new ParametersIfcInvocationHandler(target)));
+                Parameters.class.getClassLoader(), ifcClasses,
+                new ParametersIfcInvocationHandler(target)));
     }
 
     /**

Modified: commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/builder/fluent/TestParameters.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/builder/fluent/TestParameters.java?rev=1545072&r1=1545071&r2=1545072&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/builder/fluent/TestParameters.java
(original)
+++ commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/builder/fluent/TestParameters.java
Sun Nov 24 20:37:05 2013
@@ -25,6 +25,7 @@ import java.util.Map;
 
 import org.apache.commons.configuration.PropertiesConfiguration;
 import org.apache.commons.configuration.builder.BasicBuilderParameters;
+import org.apache.commons.configuration.builder.BasicBuilderProperties;
 import org.apache.commons.configuration.builder.BuilderParameters;
 import org.apache.commons.configuration.builder.FileBasedBuilderParametersImpl;
 import org.apache.commons.configuration.builder.combined.CombinedBuilderParametersImpl;
@@ -105,6 +106,43 @@ public class TestParameters
     }
 
     /**
+     * Helper method for testing whether the given object is an instance of the
+     * provided class.
+     *
+     * @param obj the object to be checked
+     * @param cls the class
+     */
+    private static void checkInstanceOf(Object obj, Class<?> cls)
+    {
+        assertTrue(obj + " is not an instance of " + cls, cls.isInstance(obj));
+    }
+
+    /**
+     * Checks whether a given parameters object implements all the specified
+     * interfaces.
+     *
+     * @param params the parameters object to check
+     * @param ifcClasses the interface classes to be implemented
+     */
+    private static void checkInheritance(Object params, Class<?>... ifcClasses)
+    {
+        checkInstanceOf(params, BasicBuilderProperties.class);
+        for (Class<?> c : ifcClasses)
+        {
+            checkInstanceOf(params, c);
+        }
+    }
+
+    /**
+     * Tests the inheritance structure of a fileBased parameters object.
+     */
+    @Test
+    public void testFileBasedInheritance()
+    {
+        checkInheritance(parameters.fileBased());
+    }
+
+    /**
      * Tests whether the proxy parameters object can deal with methods inherited
      * from Object.
      */
@@ -171,6 +209,16 @@ public class TestParameters
     }
 
     /**
+     * Tests the inheritance structure of a hierarchical parameters object.
+     */
+    @Test
+    public void testHierarchicalInheritance()
+    {
+        checkInheritance(parameters.hierarchical(),
+                FileBasedBuilderParameters.class);
+    }
+
+    /**
      * Tests whether a parameters object for an XML configuration can be
      * created.
      */
@@ -197,6 +245,16 @@ public class TestParameters
     }
 
     /**
+     * Tests the inheritance structure of an XML parameters object.
+     */
+    @Test
+    public void testXmlInheritance()
+    {
+        checkInheritance(parameters.xml(), HierarchicalBuilderParameters.class,
+                FileBasedBuilderParameters.class);
+    }
+
+    /**
      * Tests whether a parameters object for a properties configuration can be
      * created.
      */
@@ -221,6 +279,16 @@ public class TestParameters
     }
 
     /**
+     * Tests the inheritance structure of a properties parameters object.
+     */
+    @Test
+    public void testPropertiesInheritance()
+    {
+        checkInheritance(parameters.properties(),
+                FileBasedBuilderParameters.class);
+    }
+
+    /**
      * Tests whether a {@code MultiFileBuilderParameters} object can be created.
      */
     @Test
@@ -258,4 +326,28 @@ public class TestParameters
         assertEquals("Wrong auto commit flag", Boolean.TRUE,
                 map.get("autoCommit"));
     }
+
+    /**
+     * Tests whether the parameters objects created by the Parameters instance
+     * have a logic inheritance hierarchy. This means that they also implement
+     * all base interfaces that make sense.
+     */
+    @Test
+    public void testInheritance()
+    {
+        Object params = parameters.xml();
+        assertTrue("No instance of base interface",
+                params instanceof FileBasedBuilderParameters);
+        assertTrue("No instance of base interface (dynamic)",
+                FileBasedBuilderParameters.class.isInstance(params));
+        FileBasedBuilderParameters fbParams =
+                (FileBasedBuilderParameters) params;
+        fbParams.setListDelimiterHandler(listHandler).setFileName("test.xml")
+                .setThrowExceptionOnMissing(true);
+        ExpressionEngine engine = EasyMock.createMock(ExpressionEngine.class);
+        ((HierarchicalBuilderParameters) params).setExpressionEngine(engine);
+        Map<String, Object> map = fbParams.getParameters();
+        checkBasicProperties(map);
+        assertSame("Wrong expression engine", engine, map.get("expressionEngine"));
+    }
 }



Mime
View raw message