Author: oheger Date: Thu Jul 26 13:10:36 2007 New Revision: 559964 URL: http://svn.apache.org/viewvc?view=rev&rev=559964 Log: CONFIGURATION-287: Reset the reference property of all nodes to be added in the HierarchicalConfiguration.addNodes() method
Modified: 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/src/test/org/apache/commons/configuration/TestXMLConfiguration.java jakarta/commons/proper/configuration/trunk/xdocs/changes.xml Modified: jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/HierarchicalConfiguration.java URL: http://svn.apache.org/viewvc/jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/HierarchicalConfiguration.java?view=diff&rev=559964&r1=559963&r2=559964 ============================================================================== --- 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 Thu Jul 26 13:10:36 2007 @@ -31,6 +31,7 @@ import org.apache.commons.configuration.event.ConfigurationEvent; import org.apache.commons.configuration.event.ConfigurationListener; import org.apache.commons.configuration.tree.ConfigurationNode; +import org.apache.commons.configuration.tree.ConfigurationNodeVisitor; import org.apache.commons.configuration.tree.ConfigurationNodeVisitorAdapter; import org.apache.commons.configuration.tree.DefaultConfigurationNode; import org.apache.commons.configuration.tree.DefaultExpressionEngine; @@ -373,9 +374,15 @@ * instead of a single property a whole collection of nodes can be added - * and thus complete configuration sub trees. E.g. with this method it is * possible to add parts of another <code>HierarchicalConfiguration</code> - * object to this object. If the passed in key refers to an existing and - * unique node, the new nodes are added to this node. Otherwise a new node - * will be created at the specified position in the hierarchy. + * object to this object. (However be aware that a + * <code>ConfigurationNode</code> object can only belong to a single + * configuration. So if nodes from one configuration are directly added to + * another one using this method, the structure of the source configuration + * will be broken. In this case you should clone the nodes to be added + * before calling <code>addNodes()</code>.) If the passed in key refers to + * an existing and unique node, the new nodes are added to this node. + * Otherwise a new node will be created at the specified position in the + * hierarchy. * * @param key the key where the nodes are to be added; can be <b>null </b>, * then they are added to the root node @@ -409,6 +416,17 @@ throw new IllegalArgumentException( "Cannot add nodes to an attribute node!"); } + + // a visitor to ensure that the nodes' references are cleared; this is + // necessary if the nodes are moved from another configuration + ConfigurationNodeVisitor clearRefVisitor = new ConfigurationNodeVisitorAdapter() + { + public void visitBeforeChildren(ConfigurationNode node) + { + node.setReference(null); + } + }; + for (Iterator it = nodes.iterator(); it.hasNext();) { ConfigurationNode child = (ConfigurationNode) it.next(); @@ -420,6 +438,7 @@ { parent.addChild(child); } + child.visit(clearRefVisitor); } fireEvent(EVENT_ADD_NODES, key, nodes, false); } Modified: jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestHierarchicalConfiguration.java URL: http://svn.apache.org/viewvc/jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestHierarchicalConfiguration.java?view=diff&rev=559964&r1=559963&r2=559964 ============================================================================== --- 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 Thu Jul 26 13:10:36 2007 @@ -608,6 +608,30 @@ } /** + * Tests copying nodes from one configuration to another one. + */ + public void testAddNodesCopy() + { + HierarchicalConfiguration configDest = new HierarchicalConfiguration(); + configDest.addProperty("test", "TEST"); + Collection nodes = config.getRootNode().getChildren(); + assertEquals("Wrong number of children", 1, nodes.size()); + configDest.addNodes("newNodes", nodes); + for (int i = 0; i < tables.length; i++) + { + String keyTab = "newNodes.tables.table(" + i + ")."; + assertEquals("Table " + i + " not found", tables[i], configDest + .getString(keyTab + "name")); + for (int j = 0; j < fields[i].length; j++) + { + assertEquals("Invalid field " + j + " in table " + i, + fields[i][j], configDest.getString(keyTab + + "fields.field(" + j + ").name")); + } + } + } + + /** * Tests removing children from a configuration node. */ public void testNodeRemove() Modified: jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestXMLConfiguration.java URL: http://svn.apache.org/viewvc/jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestXMLConfiguration.java?view=diff&rev=559964&r1=559963&r2=559964 ============================================================================== --- jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestXMLConfiguration.java (original) +++ jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestXMLConfiguration.java Thu Jul 26 13:10:36 2007 @@ -1145,6 +1145,19 @@ } /** + * Tests adding nodes from another configuration. + */ + public void testAddNodesCopy() throws ConfigurationException + { + XMLConfiguration c2 = new XMLConfiguration(testProperties2); + conf.addNodes("copiedProperties", c2.getRootNode().getChildren()); + conf.save(testSaveConf); + XMLConfiguration checkConf = new XMLConfiguration(); + checkConf.setFile(testSaveConf); + checkSavedConfig(checkConf); + } + + /** * Prepares a configuration object for testing a reload operation. * * @return the initialized configuration Modified: jakarta/commons/proper/configuration/trunk/xdocs/changes.xml URL: http://svn.apache.org/viewvc/jakarta/commons/proper/configuration/trunk/xdocs/changes.xml?view=diff&rev=559964&r1=559963&r2=559964 ============================================================================== --- jakarta/commons/proper/configuration/trunk/xdocs/changes.xml (original) +++ jakarta/commons/proper/configuration/trunk/xdocs/changes.xml Thu Jul 26 13:10:36 2007 @@ -23,6 +23,12 @@ <body> <release version="1.5-SNAPSHOT" date="in SVN" description=""> + <action dev="oheger" type="fix" issue="CONFIGURATION-287"> + HierarchicalConfiguration.addNodes() now resets the reference property + of all nodes to be added. This fixes a problem with XMLConfiguration, + which now detects the added nodes as new and treats them correctly when + the configuration is saved. + </action> <action dev="oheger" type="add" issue="CONFIGURATION-285"> DefaultConfigurationBuilder will now notify registered error listeners about optional configuration sources that could not be created. Before --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]