Author: oheger
Date: Thu Oct 16 13:19:59 2008
New Revision: 705348
URL: http://svn.apache.org/viewvc?rev=705348&view=rev
Log:
CONFIGURATION-339: CompositeConfiguration.getList() now takes the order of
child configurations into account when performing interpolation. Ported patch
to configuration2 branch.
Modified:
commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/CompositeConfiguration.java
commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestCompositeConfiguration.java
commons/proper/configuration/branches/configuration2_experimental/xdocs/changes.xml
Modified:
commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/CompositeConfiguration.java
URL:
http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/CompositeConfiguration.java?rev=705348&r1=705347&r2=705348&view=diff
==============================================================================
---
commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/CompositeConfiguration.java
(original)
+++
commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/CompositeConfiguration.java
Thu Oct 16 13:19:59 2008
@@ -278,9 +278,9 @@
}
@Override
- public <T> List<T> getList(String key, List<T> defaultValue)
+ public <E> List<E> getList(String key, List<E> defaultValue)
{
- List list = new ArrayList();
+ List<E> list = new ArrayList<E>();
// add all elements from the first configuration containing the
requested key
Iterator<Configuration> it = configList.iterator();
@@ -289,22 +289,22 @@
Configuration config = 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())
{
return defaultValue;
}
- ListIterator lit = list.listIterator();
+ ListIterator<E> lit = list.listIterator();
while (lit.hasNext())
{
- lit.set(interpolate(lit.next()));
+ lit.set((E) interpolate(lit.next()));
}
return list;
@@ -467,4 +467,31 @@
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
+ * @param <E> the type of the elements in the list
+ */
+ private static <E> void appendListProperty(List<E> dest, Configuration
config,
+ String key)
+ {
+ Object value = config.getProperty(key);
+ if (value != null)
+ {
+ if (value instanceof Collection)
+ {
+ dest.addAll((Collection<E>) value);
+ }
+ else
+ {
+ dest.add((E) value);
+ }
+ }
+ }
}
Modified:
commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestCompositeConfiguration.java
URL:
http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestCompositeConfiguration.java?rev=705348&r1=705347&r2=705348&view=diff
==============================================================================
---
commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestCompositeConfiguration.java
(original)
+++
commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestCompositeConfiguration.java
Thu Oct 16 13:19:59 2008
@@ -768,6 +768,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/branches/configuration2_experimental/xdocs/changes.xml
URL:
http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/xdocs/changes.xml?rev=705348&r1=705347&r2=705348&view=diff
==============================================================================
---
commons/proper/configuration/branches/configuration2_experimental/xdocs/changes.xml
(original)
+++
commons/proper/configuration/branches/configuration2_experimental/xdocs/changes.xml
Thu Oct 16 13:19:59 2008
@@ -85,6 +85,13 @@
</release>
<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