commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ohe...@apache.org
Subject svn commit: r705028 - in /commons/proper/configuration/trunk: src/java/org/apache/commons/configuration/CompositeConfiguration.java src/test/org/apache/commons/configuration/TestCompositeConfiguration.java xdocs/changes.xml
Date Wed, 15 Oct 2008 20:33:35 GMT
Author: oheger
Date: Wed Oct 15 13:33:35 2008
New Revision: 705028

URL: http://svn.apache.org/viewvc?rev=705028&view=rev
Log:
CONFIGURATION-339: CompositeConfiguration.getList() now takes the order of child configurations
into account when performing interpolation.

Modified:
    commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/CompositeConfiguration.java
    commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestCompositeConfiguration.java
    commons/proper/configuration/trunk/xdocs/changes.xml

Modified: commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/CompositeConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/CompositeConfiguration.java?rev=705028&r1=705027&r2=705028&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/CompositeConfiguration.java
(original)
+++ commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/CompositeConfiguration.java
Wed Oct 15 13:33:35 2008
@@ -294,12 +294,12 @@
             Configuration config = (Configuration) it.next();
             if (config != inMemoryConfiguration && config.containsKey(key))
             {
-                list.addAll(config.getList(key));
+                appendListProperty(list, config, key);
             }
         }
 
         // add all elements from the in memory configuration
-        list.addAll(inMemoryConfiguration.getList(key));
+        appendListProperty(list, inMemoryConfiguration, key);
 
         if (list.isEmpty())
         {
@@ -319,12 +319,12 @@
     {
         List list = getList(key);
 
-        // interpolate the strings
+        // transform property values into strings
         String[] tokens = new String[list.size()];
 
         for (int i = 0; i < tokens.length; i++)
         {
-            tokens[i] = interpolate(String.valueOf(list.get(i)));
+            tokens[i] = String.valueOf(list.get(i));
         }
 
         return tokens;
@@ -469,4 +469,30 @@
 
         return source;
     }
+
+    /**
+     * Adds the value of a property to the given list. This method is used by
+     * <code>getList()</code> for gathering property values from the child
+     * configurations.
+     *
+     * @param dest the list for collecting the data
+     * @param config the configuration to query
+     * @param key the key of the property
+     */
+    private static void appendListProperty(List dest, Configuration config,
+            String key)
+    {
+        Object value = config.getProperty(key);
+        if (value != null)
+        {
+            if (value instanceof Collection)
+            {
+                dest.addAll((Collection) value);
+            }
+            else
+            {
+                dest.add(value);
+            }
+        }
+    }
 }

Modified: commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestCompositeConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestCompositeConfiguration.java?rev=705028&r1=705027&r2=705028&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestCompositeConfiguration.java
(original)
+++ commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestCompositeConfiguration.java
Wed Oct 15 13:33:35 2008
@@ -22,17 +22,17 @@
 import java.io.IOException;
 import java.io.PrintWriter;
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.Iterator;
 import java.util.List;
 import java.util.NoSuchElementException;
-import java.util.Collection;
+
+import junit.framework.TestCase;
 
 import org.apache.commons.configuration.event.ConfigurationEvent;
 import org.apache.commons.configuration.event.ConfigurationListener;
 import org.apache.commons.configuration.reloading.FileAlwaysReloadingStrategy;
 
-import junit.framework.TestCase;
-
 /**
  * Test loading multiple configurations.
  *
@@ -766,6 +766,46 @@
     }
 
     /**
+     * Prepares a test for interpolation with multiple configurations and
+     * similar properties.
+     */
+    private void prepareInterpolationTest()
+    {
+        PropertiesConfiguration p = new PropertiesConfiguration();
+        p.addProperty("foo", "initial");
+        p.addProperty("bar", "${foo}");
+        p.addProperty("prefix.foo", "override");
+
+        cc.addConfiguration(p.subset("prefix"));
+        cc.addConfiguration(p);
+        assertEquals("Wrong value on direct access", "override", cc
+                .getString("bar"));
+    }
+
+    /**
+     * Tests querying a list when a tricky interpolation is involved. This is
+     * related to CONFIGURATION-339.
+     */
+    public void testGetListWithInterpolation()
+    {
+        prepareInterpolationTest();
+        List lst = cc.getList("bar");
+        assertEquals("Wrong number of values", 1, lst.size());
+        assertEquals("Wrong value in list", "override", lst.get(0));
+    }
+
+    /**
+     * Tests querying a string array when a tricky interpolation is involved.
+     */
+    public void testGetStringArrayWithInterpolation()
+    {
+        prepareInterpolationTest();
+        String[] values = cc.getStringArray("bar");
+        assertEquals("Wrong number of values", 1, values.length);
+        assertEquals("Wrong value in array", "override", values[0]);
+    }
+
+    /**
      * A test configuration event listener that counts the number of received
      * events. Used for testing the event facilities.
      */

Modified: commons/proper/configuration/trunk/xdocs/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/xdocs/changes.xml?rev=705028&r1=705027&r2=705028&view=diff
==============================================================================
--- commons/proper/configuration/trunk/xdocs/changes.xml (original)
+++ commons/proper/configuration/trunk/xdocs/changes.xml Wed Oct 15 13:33:35 2008
@@ -23,6 +23,13 @@
 
   <body>
     <release version="1.6" date="in SVN" description="">
+      <action dev="oheger" type="fix" issue="CONFIGURATION-339">
+        When performing interpolation the methods getList() and getStringArray()
+        of CompositeConfiguration did not take the order of child configurations
+        into account. This could lead to wrong interpolated values when the key
+        was contained in multiple child configuration. Interpolation is now
+        always done in the correct order.
+      </action>
       <action dev="oheger" type="add" issue="CONFIGURATION-338" due-to="David Donn">
         PropertiesConfiguration now also performs interpolation when searching
         for include files. This means that the name of a file to include can be



Mime
View raw message