Author: oheger Date: Thu Sep 20 20:15:04 2012 New Revision: 1388193 URL: http://svn.apache.org/viewvc?rev=1388193&view=rev Log: [CONFIGURATION-504] Added a new clearAndDetachFromParent() method to SubnodeConfiguration.
Modified: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/SubnodeConfiguration.java commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestHierarchicalConfiguration.java Modified: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/SubnodeConfiguration.java URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/SubnodeConfiguration.java?rev=1388193&r1=1388192&r2=1388193&view=diff ============================================================================== --- commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/SubnodeConfiguration.java (original) +++ commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/SubnodeConfiguration.java Thu Sep 20 20:15:04 2012 @@ -241,6 +241,24 @@ public class SubnodeConfiguration extend } /** + * Clears this configuration and removes its root node from the parent + * configuration. While a default {@link #clear()} operation just removes + * all content from the root node, this method is more radical. It also + * removes this configuration's root node from the node structure of its + * parent. This means that even if later properties are added to this + * {@code SubnodeConfiguration}, they will not be visible in the parent + * configuration. + * + * @since 2.0 + */ + public void clearAndDetachFromParent() + { + clear(); + setSubnodeKey(null); // always detach + getParent().removeNode(getRootNode()); + } + + /** * Returns a hierarchical configuration object for the given sub node. * This implementation will ensure that the returned * {@code SubnodeConfiguration} object will have the same parent than Modified: commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestHierarchicalConfiguration.java URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestHierarchicalConfiguration.java?rev=1388193&r1=1388192&r2=1388193&view=diff ============================================================================== --- commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestHierarchicalConfiguration.java (original) +++ commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestHierarchicalConfiguration.java Thu Sep 20 20:15:04 2012 @@ -597,7 +597,7 @@ public class TestHierarchicalConfigurati SubnodeConfiguration sub = config.configurationAt("test.sub"); assertEquals("Wrong value", "fail", sub.getString("test")); sub.clear(); - assertNull("Key still found", config.getString("test.sub.key")); + assertNull("Key still found", config.getString("test.sub.test")); sub.setProperty("test", "success"); assertEquals("Property not set", "success", config.getString("test.sub.test")); @@ -605,6 +605,23 @@ public class TestHierarchicalConfigurati } /** + * Tests whether a {@code SubnodeConfiguration} can be cleared and its root + * node can be removed from its parent configuration. + */ + @Test + public void testConfigurationAtClearAndDetach() + { + config.addProperty("test.sub.test", "success"); + config.addProperty("test.other", "check"); + SubnodeConfiguration sub = config.configurationAt("test.sub"); + sub.clearAndDetachFromParent(); + assertTrue("Sub not empty", sub.isEmpty()); + assertNull("Key still found", config.getString("test.sub.test")); + sub.setProperty("test", "failure!"); + assertNull("Node not detached", config.getString("test.sub.test")); + } + + /** * Tests the configurationsAt() method. */ @Test