commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ebo...@apache.org
Subject cvs commit: jakarta-commons/configuration/src/test/org/apache/commons/configuration TestHierarchicalConfiguration.java
Date Mon, 18 Oct 2004 10:19:27 GMT
ebourg      2004/10/18 03:19:27

  Modified:    configuration/xdocs changes.xml
               configuration/src/java/org/apache/commons/configuration
                        HierarchicalConfiguration.java
               configuration/src/test/org/apache/commons/configuration
                        TestHierarchicalConfiguration.java
  Log:
  Fixed HierarchicalConfiguration.getKeys(String) (bug 31745 by Oliver Heger)
  
  Revision  Changes    Path
  1.57      +4 -0      jakarta-commons/configuration/xdocs/changes.xml
  
  Index: changes.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/configuration/xdocs/changes.xml,v
  retrieving revision 1.56
  retrieving revision 1.57
  diff -u -r1.56 -r1.57
  --- changes.xml	18 Oct 2004 09:54:37 -0000	1.56
  +++ changes.xml	18 Oct 2004 10:19:26 -0000	1.57
  @@ -8,6 +8,10 @@
     <body>
   
       <release version="1.1-dev" date="in CVS">
  +      <action dev="oheger" type="fix" issue="31745">
  +        Fixed HierarchicalConfiguration.getKeys(String), it returned an empty
  +        iterator if the prefix string contained indices.
  +      </action>
         <action dev="ebourg" type="add">
           Added a DataConfiguration decorator providing getters for all useful
           types found in a configuration (URL, Locale, Date, Calendar, Color,
  
  
  
  1.12      +26 -1     jakarta-commons/configuration/src/java/org/apache/commons/configuration/HierarchicalConfiguration.java
  
  Index: HierarchicalConfiguration.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/configuration/src/java/org/apache/commons/configuration/HierarchicalConfiguration.java,v
  retrieving revision 1.11
  retrieving revision 1.12
  diff -u -r1.11 -r1.12
  --- HierarchicalConfiguration.java	11 Oct 2004 09:27:20 -0000	1.11
  +++ HierarchicalConfiguration.java	18 Oct 2004 10:19:26 -0000	1.12
  @@ -377,6 +377,31 @@
           getRoot().visit(visitor, new ConfigurationKey());
           return visitor.getKeyList().iterator();
       }
  +    
  +    /**
  +     * Returns an iterator with all keys defined in this configuration that
  +     * start with the given prefix. The returned keys will not contain any
  +     * indices.
  +     * @param prefix the prefix of the keys to start with
  +     * @return an iterator with the found keys
  +     */
  +    public Iterator getKeys(String prefix)
  +    {
  +        DefinedKeysVisitor visitor = new DefinedKeysVisitor();
  +        List nodes = fetchNodeList(prefix);
  +        ConfigurationKey key = new ConfigurationKey();
  +        
  +        for(Iterator itNodes = nodes.iterator(); itNodes.hasNext();)
  +        {
  +            Node node = (Node) itNodes.next();
  +            for(Iterator it = node.getChildren().iterator(); it.hasNext();)
  +            {
  +                ((Node) it.next()).visit(visitor, key);
  +            }
  +        }
  +        
  +        return visitor.getKeyList().iterator();
  +    }
   
       /**
        * Returns the maximum defined index for the given key. This is
  
  
  
  1.6       +55 -1     jakarta-commons/configuration/src/test/org/apache/commons/configuration/TestHierarchicalConfiguration.java
  
  Index: TestHierarchicalConfiguration.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/configuration/src/test/org/apache/commons/configuration/TestHierarchicalConfiguration.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- TestHierarchicalConfiguration.java	13 Mar 2004 17:04:04 -0000	1.5
  +++ TestHierarchicalConfiguration.java	18 Oct 2004 10:19:27 -0000	1.6
  @@ -18,8 +18,10 @@
   
   import java.util.ArrayList;
   import java.util.Collection;
  +import java.util.HashSet;
   import java.util.Iterator;
   import java.util.List;
  +import java.util.Set;
   
   import junit.framework.TestCase;
   
  @@ -158,6 +160,28 @@
           assertTrue(keys.contains("tables.table.fields.field.name"));
       }
       
  +    public void testGetKeysString()
  +    {
  +        // add some more properties to make it more interesting
  +        config.addProperty("tables.table(0).fields.field(1).type", "VARCHAR");
  +        config.addProperty("tables.table(0)[@type]", "system");
  +        config.addProperty("tables.table(0).size", "42");
  +        config.addProperty("tables.table(0).fields.field(0).size", "128");
  +        config.addProperty("connections.connection.param.url", "url1");
  +        config.addProperty("connections.connection.param.user", "me");
  +        config.addProperty("connections.connection.param.pwd", "secret");
  +        config.addProperty("connections.connection(-1).param.url", "url2");
  +        config.addProperty("connections.connection(1).param.user", "guest");
  +        
  +        checkKeys("tables.table(1)", new String[] { "name", "fields.field.name" });
  +        checkKeys("tables.table(0)",
  +                new String[] { "name", "fields.field.name", "[@type]", "size", "fields.field.type",
"fields.field.size" });
  +        checkKeys("connections.connection(0).param",
  +                new String[] {"url", "user", "pwd" });
  +        checkKeys("connections.connection(1).param",
  +                new String[] {"url", "user" });
  +    }
  +    
       public void testAddProperty()
       {
           config.addProperty("tables.table(0).fields.field(-1).name", "phone");
  @@ -245,5 +269,35 @@
           
           conf = config.subset("tables.table.fields.field.name");
           assertTrue("subset is not empty", conf.isEmpty());
  +    }
  +    
  +    /**
  +     * Helper method for testing the getKeys(String) method.
  +     * @param prefix the key to pass into getKeys()
  +     * @param expected the expected result
  +     */
  +    private void checkKeys(String prefix, String[] expected)
  +    {
  +        Set values = new HashSet();
  +        for(int i = 0; i < expected.length; i++)
  +        {
  +            values.add(expected[i]);
  +        }
  +        
  +        Iterator itKeys = config.getKeys(prefix);
  +        while(itKeys.hasNext())
  +        {
  +            String key = (String) itKeys.next();
  +            if(!values.contains(key))
  +            {
  +                fail("Found unexpected key: " + key);
  +            }
  +            else
  +            {
  +                values.remove(key);
  +            }
  +        }
  +        
  +        assertTrue("Remaining keys " + values, values.isEmpty());
       }
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message