Author: ebourg Date: Tue Aug 2 08:45:55 2005 New Revision: 227028 URL: http://svn.apache.org/viewcvs?rev=227028&view=rev Log: getKeys() in HierarchicalConfiguration now returns an iterator over an ordered set, this implies an upgrade of the dependency on Commons Collections (3.0 -> 3.1) (Bug 35903) New Node constructor (name/value) Removed the default constuctor from HierarchicalConfiguration
Modified: jakarta/commons/proper/configuration/trunk/project.xml jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/HierarchicalConfiguration.java jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestHierarchicalConfiguration.java jakarta/commons/proper/configuration/trunk/xdocs/changes.xml Modified: jakarta/commons/proper/configuration/trunk/project.xml URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/configuration/trunk/project.xml?rev=227028&r1=227027&r2=227028&view=diff ============================================================================== --- jakarta/commons/proper/configuration/trunk/project.xml (original) +++ jakarta/commons/proper/configuration/trunk/project.xml Tue Aug 2 08:45:55 2005 @@ -198,7 +198,7 @@ <dependency> <groupId>commons-collections</groupId> <artifactId>commons-collections</artifactId> - <version>3.0</version> + <version>3.1</version> <properties> <war.bundle>true</war.bundle> </properties> Modified: jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/HierarchicalConfiguration.java URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/HierarchicalConfiguration.java?rev=227028&r1=227027&r2=227028&view=diff ============================================================================== --- jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/HierarchicalConfiguration.java (original) +++ jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/HierarchicalConfiguration.java Tue Aug 2 08:45:55 2005 @@ -19,7 +19,6 @@ import java.io.Serializable; import java.util.ArrayList; import java.util.Collection; -import java.util.HashSet; import java.util.Iterator; import java.util.LinkedList; import java.util.List; @@ -27,6 +26,7 @@ import java.util.Stack; import org.apache.commons.collections.map.LinkedMap; +import org.apache.commons.collections.set.ListOrderedSet; import org.apache.commons.lang.StringUtils; /** @@ -92,8 +92,7 @@ * @version $Id: HierarchicalConfiguration.java,v 1.14 2004/12/02 22:05:52 * ebourg Exp $ */ -public class HierarchicalConfiguration extends AbstractConfiguration implements - Cloneable +public class HierarchicalConfiguration extends AbstractConfiguration implements Serializable, Cloneable { /** Constant for a new dummy key. */ private static final String NEW_KEY = "newKey"; @@ -102,14 +101,6 @@ private Node root = new Node(); /** - * Creates a new instance of <code>HierarchicalConfiguration</code>. - */ - public HierarchicalConfiguration() - { - super(); - } - - /** * Returns the root node of this hierarchical configuration. * * @return the root node @@ -421,8 +412,8 @@ } /** - * <p>Returns an iterator with all keys defined in this configuration.</p> - * <p>Note that the keys returned by this method will not contain any + * Returns an iterator with all keys defined in this configuration. + * Note that the keys returned by this method will not contain any * indices. This means that some structure will be lost.</p> * * @return an iterator with the defined keys in this configuration @@ -431,6 +422,7 @@ { DefinedKeysVisitor visitor = new DefinedKeysVisitor(); getRoot().visit(visitor, new ConfigurationKey()); + return visitor.getKeyList().iterator(); } @@ -523,13 +515,13 @@ * * @param keyPart the configuration key iterator * @param node the actual node - * @param data here the found nodes are stored + * @param nodes here the found nodes are stored */ - protected void findPropertyNodes(ConfigurationKey.KeyIterator keyPart, Node node, Collection data) + protected void findPropertyNodes(ConfigurationKey.KeyIterator keyPart, Node node, Collection nodes) { if (!keyPart.hasNext()) { - data.add(node); + nodes.add(node); } else { @@ -540,14 +532,14 @@ if (keyPart.getIndex() < children.size() && keyPart.getIndex() >= 0) { findPropertyNodes((ConfigurationKey.KeyIterator) keyPart.clone(), (Node) children.get(keyPart - .getIndex()), data); + .getIndex()), nodes); } } else { for (Iterator it = children.iterator(); it.hasNext();) { - findPropertyNodes((ConfigurationKey.KeyIterator) keyPart.clone(), (Node) it.next(), data); + findPropertyNodes((ConfigurationKey.KeyIterator) keyPart.clone(), (Node) it.next(), nodes); } } } @@ -736,6 +728,18 @@ } /** + * Creates a new instance of [EMAIL PROTECTED] Node} and sets the name and the value. + * + * @param name the node's name + * @param value the value + */ + public Node(String name, Object value) + { + setName(name); + setValue(value); + } + + /** * Returns the name of this node. * * @return the node name @@ -1172,7 +1176,7 @@ */ public DefinedKeysVisitor() { - keyList = new HashSet(); + keyList = new ListOrderedSet(); } /** Modified: jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestHierarchicalConfiguration.java URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestHierarchicalConfiguration.java?rev=227028&r1=227027&r2=227028&view=diff ============================================================================== --- jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestHierarchicalConfiguration.java (original) +++ jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestHierarchicalConfiguration.java Tue Aug 2 08:45:55 2005 @@ -1,7 +1,5 @@ -package org.apache.commons.configuration; - /* - * Copyright 2001-2004 The Apache Software Foundation. + * Copyright 2001-2005 The Apache Software Foundation. * * Licensed under the Apache License, Version 2.0 (the "License") * you may not use this file except in compliance with the License. @@ -16,6 +14,8 @@ * limitations under the License. */ +package org.apache.commons.configuration; + import java.util.ArrayList; import java.util.Collection; import java.util.HashSet; @@ -44,6 +44,18 @@ protected void setUp() throws Exception { + /** + * Initialize the configuration with the following structure: + * + * tables + * table + * name + * fields + * field + * name + * field + * name + */ config = new HierarchicalConfiguration(); HierarchicalConfiguration.Node nodeTables = createNode("tables", null); for(int i = 0; i < tables.length; i++) @@ -54,11 +66,13 @@ nodeTable.addChild(nodeName); HierarchicalConfiguration.Node nodeFields = createNode("fields", null); nodeTable.addChild(nodeFields); - for(int j = 0; j < fields[i].length; j++) + + for (int j = 0; j < fields[i].length; j++) { nodeFields.addChild(createFieldNode(fields[i][j])); - } /* for */ - } /* for */ + } + } + config.getRoot().addChild(nodeTables); } @@ -67,10 +81,8 @@ assertFalse(config.isEmpty()); HierarchicalConfiguration conf2 = new HierarchicalConfiguration(); assertTrue(conf2.isEmpty()); - HierarchicalConfiguration.Node child1 = - new HierarchicalConfiguration.Node("child1"); - HierarchicalConfiguration.Node child2 = - new HierarchicalConfiguration.Node("child2"); + HierarchicalConfiguration.Node child1 = new HierarchicalConfiguration.Node("child1"); + HierarchicalConfiguration.Node child2 = new HierarchicalConfiguration.Node("child2"); child1.addChild(child2); conf2.getRoot().addChild(child1); assertTrue(conf2.isEmpty()); @@ -180,14 +192,24 @@ public void testGetKeys() { List keys = new ArrayList(); - for(Iterator it = config.getKeys(); it.hasNext();) + for (Iterator it = config.getKeys(); it.hasNext();) { keys.add(it.next()); - } /* for */ + } assertEquals(2, keys.size()); assertTrue(keys.contains("tables.table.name")); assertTrue(keys.contains("tables.table.fields.field.name")); + + // test the order of the keys returned + config.addProperty("order.key1", "value1"); + config.addProperty("order.key2", "value2"); + config.addProperty("order.key3", "value3"); + + Iterator it = config.getKeys("order"); + assertEquals("1st key", "order.key1", it.next()); + assertEquals("2nd key", "order.key2", it.next()); + assertEquals("3rd key", "order.key3", it.next()); } public void testGetKeysString() @@ -270,35 +292,42 @@ ConfigurationKey key = new ConfigurationKey("tables.table(0).fields"); key.append("field").appendIndex(i).append("name"); assertNotNull(config.getProperty(key.toString())); - } /* for */ + } } public void testSubset() { - Configuration conf = config.subset("tables.table(0)"); - assertEquals("users", conf.getProperty("name")); - Object prop = conf.getProperty("fields.field.name"); + // test the subset on the first table + Configuration subset = config.subset("tables.table(0)"); + assertEquals(tables[0], subset.getProperty("name")); + + Object prop = subset.getProperty("fields.field.name"); assertNotNull(prop); assertTrue(prop instanceof Collection); assertEquals(5, ((Collection) prop).size()); - for(int i = 0; i < fields[0].length; i++) + for (int i = 0; i < fields[0].length; i++) { ConfigurationKey key = new ConfigurationKey(); key.append("fields").append("field").appendIndex(i); key.append("name"); - assertEquals(fields[0][i], conf.getProperty(key.toString())); - } /* for */ + assertEquals(fields[0][i], subset.getProperty(key.toString())); + } + // test the subset on the second table assertTrue("subset is not empty", config.subset("tables.table(2)").isEmpty()); - conf = config.subset("tables.table.fields.field"); - prop = conf.getProperty("name"); + // test the subset on the fields + subset = config.subset("tables.table.fields.field"); + prop = subset.getProperty("name"); assertTrue("prop is not a collection", prop instanceof Collection); assertEquals(10, ((Collection) prop).size()); - - conf = config.subset("tables.table.fields.field.name"); - assertTrue("subset is not empty", conf.isEmpty()); + + assertEquals(fields[0][0], subset.getProperty("name(0)")); + + // tset the subset on the field names + subset = config.subset("tables.table.fields.field.name"); + assertTrue("subset is not empty", subset.isEmpty()); } public void testClone() @@ -307,12 +336,10 @@ assertTrue(copy instanceof HierarchicalConfiguration); for (int i = 0; i < tables.length; i++) { - assertEquals(tables[i], copy.getString("tables.table(" + i - + ").name")); + assertEquals(tables[i], copy.getString("tables.table(" + i + ").name")); for (int j = 0; j < fields[i].length; j++) { - assertEquals(fields[i][j], copy.getString("tables.table(" + i - + ").fields.field(" + j + ").name")); + assertEquals(fields[i][j], copy.getString("tables.table(" + i + ").fields.field(" + j + ").name")); } } } Modified: jakarta/commons/proper/configuration/trunk/xdocs/changes.xml URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/configuration/trunk/xdocs/changes.xml?rev=227028&r1=227027&r2=227028&view=diff ============================================================================== --- jakarta/commons/proper/configuration/trunk/xdocs/changes.xml (original) +++ jakarta/commons/proper/configuration/trunk/xdocs/changes.xml Tue Aug 2 08:45:55 2005 @@ -23,6 +23,12 @@ <body> <release version="1.2-dev" date="in SVN"> + <action dev="ebourg" type="update" issue="35903"> + getKeys() in HierarchicalConfiguration now returns the keys in the same order the properties were inserted. + </action> + <action dev="ebourg" type="update"> + Commons Configuration now depends on Commons Collections 3.1 instead of 3.0 + </action> <action dev="ebourg" type="add" issue="32633"> New configurations implementing the "property list" format used in NeXT/OpenStep and its XML variant used in Mac OS X. --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]