Author: oheger
Date: Mon Dec  1 13:28:31 2008
New Revision: 722238

URL: http://svn.apache.org/viewvc?rev=722238&view=rev
Log:
CONFIGURATION-321: The iterator returned by 
HierachicalConfiguration.getKeys(String prefix) now contains the prefix if this 
key is contained in the configuration.

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

Modified: 
commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/HierarchicalConfiguration.java
URL: 
http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/HierarchicalConfiguration.java?rev=722238&r1=722237&r2=722238&view=diff
==============================================================================
--- 
commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/HierarchicalConfiguration.java
 (original)
+++ 
commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/HierarchicalConfiguration.java
 Mon Dec  1 13:28:31 2008
@@ -824,6 +824,12 @@
     public Iterator getKeys(String prefix)
     {
         DefinedKeysVisitor visitor = new DefinedKeysVisitor(prefix);
+        if (containsKey(prefix))
+        {
+            // explicitly add the prefix
+            visitor.getKeyList().add(prefix);
+        }
+
         List nodes = fetchNodeList(prefix);
 
         for (Iterator itNodes = nodes.iterator(); itNodes.hasNext();)

Modified: 
commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestHierarchicalConfiguration.java
URL: 
http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestHierarchicalConfiguration.java?rev=722238&r1=722237&r2=722238&view=diff
==============================================================================
--- 
commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestHierarchicalConfiguration.java
 (original)
+++ 
commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestHierarchicalConfiguration.java
 Mon Dec  1 13:28:31 2008
@@ -357,6 +357,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 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 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/trunk/xdocs/changes.xml
URL: 
http://svn.apache.org/viewvc/commons/proper/configuration/trunk/xdocs/changes.xml?rev=722238&r1=722237&r2=722238&view=diff
==============================================================================
--- commons/proper/configuration/trunk/xdocs/changes.xml (original)
+++ commons/proper/configuration/trunk/xdocs/changes.xml Mon Dec  1 13:28:31 
2008
@@ -157,6 +157,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