Author: oheger
Date: Mon Dec  1 13:30:16 2008
New Revision: 722241

URL: http://svn.apache.org/viewvc?rev=722241&view=rev
Log:
CONFIGURATION-321: The iterator returned by 
AbstractHierachicalConfiguration.getKeys(String prefix) now contains the prefix 
if this key is contained in the configuration. Ported the fix from trunk to 
configuration2 branch.

Modified:
    
commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/AbstractHierarchicalConfiguration.java
    
commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestInMemoryConfiguration.java
    
commons/proper/configuration/branches/configuration2_experimental/xdocs/changes.xml

Modified: 
commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/AbstractHierarchicalConfiguration.java
URL: 
http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/AbstractHierarchicalConfiguration.java?rev=722241&r1=722240&r2=722241&view=diff
==============================================================================
--- 
commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/AbstractHierarchicalConfiguration.java
 (original)
+++ 
commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/AbstractHierarchicalConfiguration.java
 Mon Dec  1 13:30:16 2008
@@ -619,6 +619,12 @@
     public Iterator<String> getKeys(String prefix)
     {
         DefinedKeysVisitor visitor = new DefinedKeysVisitor(prefix);
+        if (containsKey(prefix))
+        {
+            // explicitly add the prefix
+            visitor.getKeyList().add(prefix);
+        }
+
         NodeList<T> nodes = fetchNodeList(prefix);
 
         for (int i = 0; i < nodes.size(); i++)

Modified: 
commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestInMemoryConfiguration.java
URL: 
http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestInMemoryConfiguration.java?rev=722241&r1=722240&r2=722241&view=diff
==============================================================================
--- 
commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestInMemoryConfiguration.java
 (original)
+++ 
commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/TestInMemoryConfiguration.java
 Mon Dec  1 13:30:16 2008
@@ -349,6 +349,35 @@
                 new String[] {"url", "user" });
     }
 
+    /**
+     * Tests getKeys() with a prefix when the prefix matches exactly a key.
+     */
+    public void testGetKeysWithKeyAsPrefix()
+    {
+        config.addProperty("order.key1", "value1");
+        config.addProperty("order.key2", "value2");
+        Iterator<String> it = config.getKeys("order.key1");
+        assertTrue("no key found", it.hasNext());
+        assertEquals("1st key", "order.key1", it.next());
+        assertFalse("more keys than expected", it.hasNext());
+    }
+
+    /**
+     * Tests getKeys() with a prefix when the prefix matches exactly a key, and
+     * there are multiple keys starting with this prefix.
+     */
+    public void testGetKeysWithKeyAsPrefixMultiple()
+    {
+        config.addProperty("order.key1", "value1");
+        config.addProperty("order.key1.test", "value2");
+        config.addProperty("order.key1.test.complex", "value2");
+        Iterator<String> it = config.getKeys("order.key1");
+        assertEquals("Wrong key 1", "order.key1", it.next());
+        assertEquals("Wrong key 2", "order.key1.test", it.next());
+        assertEquals("Wrong key 3", "order.key1.test.complex", it.next());
+        assertFalse("More keys than expected", it.hasNext());
+    }
+
     public void testAddProperty()
     {
         config.addProperty("tables.table(0).fields.field(-1).name", "phone");

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=722241&r1=722240&r2=722241&view=diff
==============================================================================
--- 
commons/proper/configuration/branches/configuration2_experimental/xdocs/changes.xml
 (original)
+++ 
commons/proper/configuration/branches/configuration2_experimental/xdocs/changes.xml
 Mon Dec  1 13:30:16 2008
@@ -169,6 +169,11 @@
         ConfigurationDynaBean now works properly with indexed properties
         stored internally in the underlying configuration as arrays.
       </action>
+      <action dev="oheger" type="fix" issue="CONFIGURATION-321">
+        The iterator returned by HierarchicalConfiguration.getKeys(String 
prefix)
+        now also contains the prefix if this key is contained in the
+        configuration.
+      </action>
       <action dev="ebourg" type="fix" issue="CONFIGURATION-320">
         XMLPropertyListConfiguration is no longer limited to 32 bits integers.
       </action>


Reply via email to