commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ebo...@apache.org
Subject svn commit: r787637 - in /commons/proper/configuration/branches/configuration2_experimental/src: main/java/org/apache/commons/configuration2/ConfigurationUtils.java test/java/org/apache/commons/configuration2/TestConfigurationUtils.java
Date Tue, 23 Jun 2009 11:33:56 GMT
Author: ebourg
Date: Tue Jun 23 11:33:56 2009
New Revision: 787637

URL: http://svn.apache.org/viewvc?rev=787637&view=rev
Log:
Added the new convertToHierarchical() method from the trunk to ConfigurationUtils

Modified:
    commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/ConfigurationUtils.java
    commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestConfigurationUtils.java

Modified: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/ConfigurationUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/ConfigurationUtils.java?rev=787637&r1=787636&r2=787637&view=diff
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/ConfigurationUtils.java
(original)
+++ commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/ConfigurationUtils.java
Tue Jun 23 11:33:56 2009
@@ -33,6 +33,7 @@
 import org.apache.commons.configuration2.event.ConfigurationErrorEvent;
 import org.apache.commons.configuration2.event.ConfigurationErrorListener;
 import org.apache.commons.configuration2.event.EventSource;
+import org.apache.commons.configuration2.expr.ExpressionEngine;
 import org.apache.commons.lang.StringUtils;
 
 /**
@@ -179,9 +180,10 @@
      * and only if the passed in configuration is <b>null</b>)
      * @since 1.3
      */
-    public static HierarchicalConfiguration convertToHierarchical(
-            Configuration conf)
+    public static HierarchicalConfiguration convertToHierarchical(Configuration conf)
     {
+        // todo to be changed into convertToHierarchical(conf, null) when HierarchicalConfiguration
is removed
+        
         if (conf == null)
         {
             return null;
@@ -204,6 +206,63 @@
     }
 
     /**
+     * Converts the passed in <code>Configuration</code> object to a
+     * hierarchical one using the specified <code>ExpressionEngine</code>. This
+     * conversion works by adding the keys found in the configuration to a newly
+     * created hierarchical configuration. When adding new keys to a
+     * hierarchical configuration the keys are interpreted by its
+     * <code>ExpressionEngine</code>. If they contain special characters (e.g.
+     * brackets) that are treated in a special way by the default expression
+     * engine, it may be necessary using a specific engine that can deal with
+     * such characters. Otherwise <b>null</b> can be passed in for the
+     * <code>ExpressionEngine</code>; then the default expression engine is
+     * used. If the passed in configuration is already hierarchical, it is
+     * directly returned. (However, the <code>ExpressionEngine</code> is set
if
+     * it is not <b>null</b>.) Otherwise all properties are copied into a new
+     * hierarchical configuration.
+     *
+     * @param conf the configuration to convert
+     * @param engine the <code>ExpressionEngine</code> for the hierarchical
+     *        configuration or <b>null</b> for the default
+     * @return the new hierarchical configuration (the result is <b>null</b>
if
+     *         and only if the passed in configuration is <b>null</b>)
+     * @since 1.6
+     */
+    public static AbstractHierarchicalConfiguration convertToHierarchical(Configuration conf,
ExpressionEngine engine)
+    {
+        if (conf == null)
+        {
+            return null;
+        }
+
+        if (conf instanceof AbstractHierarchicalConfiguration)
+        {
+            AbstractHierarchicalConfiguration hc = (AbstractHierarchicalConfiguration) conf;
+            if (engine != null)
+            {
+                hc.setExpressionEngine(engine);
+            }
+
+            return hc;
+        }
+        else
+        {
+            AbstractHierarchicalConfiguration hc = new InMemoryConfiguration();
+            if (engine != null)
+            {
+                hc.setExpressionEngine(engine);
+            }
+
+            // Workaround for problem with copy()
+            boolean delimiterParsingStatus = hc.isDelimiterParsingDisabled();
+            hc.setDelimiterParsingDisabled(true);
+            hc.append(conf);
+            hc.setDelimiterParsingDisabled(delimiterParsingStatus);
+            return hc;
+        }
+    }
+
+    /**
      * Clones the given configuration object if this is possible. If the passed
      * in configuration object implements the <code>Cloneable</code>
      * interface, its <code>clone()</code> method will be invoked. Otherwise

Modified: commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestConfigurationUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestConfigurationUtils.java?rev=787637&r1=787636&r2=787637&view=diff
==============================================================================
--- commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestConfigurationUtils.java
(original)
+++ commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestConfigurationUtils.java
Tue Jun 23 11:33:56 2009
@@ -24,6 +24,8 @@
 import java.util.List;
 
 import org.apache.commons.configuration2.flat.BaseConfiguration;
+import org.apache.commons.configuration2.expr.def.DefaultExpressionEngine;
+import org.apache.commons.configuration2.expr.ExpressionEngine;
 
 import com.mockobjects.dynamic.Mock;
 import junit.framework.TestCase;
@@ -269,6 +271,63 @@
     }
 
     /**
+     * Tests converting a configuration to a hierarchical one using a specific
+     * expression engine.
+     */
+    public void testConvertToHierarchicalEngine()
+    {
+        Configuration conf = new BaseConfiguration();
+        conf.addProperty("test(a)", Boolean.TRUE);
+        conf.addProperty("test(b)", Boolean.FALSE);
+        DefaultExpressionEngine engine = new DefaultExpressionEngine();
+        engine.setIndexStart("[");
+        engine.setIndexEnd("]");
+        AbstractHierarchicalConfiguration hc = ConfigurationUtils.convertToHierarchical(conf,
engine);
+        assertTrue("Wrong value for test(a)", hc.getBoolean("test(a)"));
+        assertFalse("Wrong value for test(b)", hc.getBoolean("test(b)"));
+    }
+
+    /**
+     * Tests converting an already hierarchical configuration using an
+     * expression engine. The new engine should be set.
+     */
+    public void testConvertHierarchicalToHierarchicalEngine()
+    {
+        InMemoryConfiguration hc = new InMemoryConfiguration();
+        ExpressionEngine engine = new DefaultExpressionEngine();
+        assertSame("Created new configuration", hc, ConfigurationUtils.convertToHierarchical(hc,
engine));
+        assertSame("Engine was not set", engine, hc.getExpressionEngine());
+    }
+
+    /**
+     * Tests converting an already hierarchical configuration using a null
+     * expression engine. In this case the expression engine of the
+     * configuration should not be touched.
+     */
+    public void testConvertHierarchicalToHierarchicalNullEngine()
+    {
+        InMemoryConfiguration hc = new InMemoryConfiguration();
+        ExpressionEngine engine = new DefaultExpressionEngine();
+        hc.setExpressionEngine(engine);
+        assertSame("Created new configuration", hc, ConfigurationUtils.convertToHierarchical(hc,
null));
+        assertSame("Expression engine was changed", engine, hc.getExpressionEngine());
+    }
+
+    /**
+     * Tests converting a configuration to a hierarchical one that contains a
+     * property with multiple values. This test is related to CONFIGURATION-346.
+     */
+    public void testConvertToHierarchicalMultiValues()
+    {
+        BaseConfiguration config = new BaseConfiguration();
+        config.addProperty("test", "1,2,3");
+        AbstractHierarchicalConfiguration hc = ConfigurationUtils.convertToHierarchical(config,
null);
+        assertEquals("Wrong value 1", 1, hc.getInt("test(0)"));
+        assertEquals("Wrong value 2", 2, hc.getInt("test(1)"));
+        assertEquals("Wrong value 3", 3, hc.getInt("test(2)"));
+    }
+    
+    /**
      * Tests cloning a configuration that supports this operation.
      */
     public void testCloneConfiguration()



Mime
View raw message