commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ohe...@apache.org
Subject svn commit: r1406264 - in /commons/proper/configuration/trunk/src: main/java/org/apache/commons/configuration/ConfigurationUtils.java test/java/org/apache/commons/configuration/TestImmutableConfiguration.java
Date Tue, 06 Nov 2012 18:32:41 GMT
Author: oheger
Date: Tue Nov  6 18:32:41 2012
New Revision: 1406264

URL: http://svn.apache.org/viewvc?rev=1406264&view=rev
Log:
Added a utility method to ConfigurationUtils to create immutable hierarchical configurations.

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/TestImmutableConfiguration.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=1406264&r1=1406263&r2=1406264&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
Tue Nov  6 18:32:41 2012
@@ -71,6 +71,14 @@ public final class ConfigurationUtils
         ImmutableConfiguration.class
     };
 
+    /**
+     * An array with interfaces to be implemented by a proxy for an immutable
+     * hierarchical configuration.
+     */
+    private static final Class<?>[] IMMUTABLE_HIERARCHICAL_CONFIG_IFCS = {
+        ImmutableHierarchicalConfiguration.class
+    };
+
     /** The logger.*/
     private static final Log LOG = LogFactory.getLog(ConfigurationUtils.class);
 
@@ -785,9 +793,43 @@ public final class ConfigurationUtils
     public static ImmutableConfiguration unmodifiableConfiguration(
             Configuration c)
     {
+        return createUnmodifiableConfiguration(IMMUTABLE_CONFIG_IFCS, c);
+    }
+
+    /**
+     * Creates an {@code ImmutableHierarchicalConfiguration} from the given
+     * {@code HierarchicalConfiguration} object. This method works exactly like
+     * the method with the same name, but it operates on hierarchical
+     * configurations.
+     *
+     * @param c the {@code HierarchicalConfiguration} to be wrapped (must not be
+     *        <b>null</b>)
+     * @return an {@code ImmutableHierarchicalConfiguration} view on the
+     *         specified {@code HierarchicalConfiguration} object
+     * @throws NullPointerException if the passed in
+     *         {@code HierarchicalConfiguration} is <b>null</b>
+     * @since 2.0
+     */
+    public static ImmutableHierarchicalConfiguration unmodifiableConfiguration(
+            HierarchicalConfiguration c)
+    {
+        return (ImmutableHierarchicalConfiguration) createUnmodifiableConfiguration(
+                IMMUTABLE_HIERARCHICAL_CONFIG_IFCS, c);
+    }
+
+    /**
+     * Helper method for creating a proxy for an unmodifiable configuration.
+     * The interfaces the proxy should implement are passed as argument.
+     * @param ifcs an array with the interface classes the proxy must implement
+     * @param c the configuration object to be wrapped
+     * @return a proxy object for an immutable configuration
+     * @throws NullPointerException if the configuration is <b>null</b>
+     */
+    private static ImmutableConfiguration createUnmodifiableConfiguration(
+            Class<?>[] ifcs, Configuration c)
+    {
         return (ImmutableConfiguration) Proxy.newProxyInstance(
-                ConfigurationUtils.class.getClassLoader(),
-                IMMUTABLE_CONFIG_IFCS,
+                ConfigurationUtils.class.getClassLoader(), ifcs,
                 new ImmutableConfigurationInvocationHandler(c));
     }
 }

Modified: commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestImmutableConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestImmutableConfiguration.java?rev=1406264&r1=1406263&r2=1406264&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestImmutableConfiguration.java
(original)
+++ commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestImmutableConfiguration.java
Tue Nov  6 18:32:41 2012
@@ -190,4 +190,19 @@ public class TestImmutableConfiguration
         assertFalse("No content", subset.isEmpty());
         assertEquals("Wrong value", 1000000, subset.getLong("long"));
     }
+
+    /**
+     * Tests whether an unmodifiable hierarchical configuration can be created.
+     */
+    @Test
+    public void testUnmodifiableHierarchicalConfiguration()
+    {
+        HierarchicalConfiguration conf = new BaseHierarchicalConfiguration();
+        String key = "test";
+        conf.addProperty(key, Boolean.TRUE);
+        ImmutableHierarchicalConfiguration ihc =
+                ConfigurationUtils.unmodifiableConfiguration(conf);
+        assertTrue("Property not found", ihc.getBoolean(key));
+        assertEquals("Wrong max index", 0, ihc.getMaxIndex(key));
+    }
 }



Mime
View raw message