You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by oh...@apache.org on 2014/03/01 21:04:19 UTC

svn commit: r1573213 - in /commons/proper/configuration/branches/immutableNodes/src: main/java/org/apache/commons/configuration/tree/InMemoryNodeModel.java test/java/org/apache/commons/configuration/tree/TestInMemoryNodeModel.java

Author: oheger
Date: Sat Mar  1 20:04:19 2014
New Revision: 1573213

URL: http://svn.apache.org/r1573213
Log:
Added a setRoot() method to InMemoryNodeModel.

Modified:
    commons/proper/configuration/branches/immutableNodes/src/main/java/org/apache/commons/configuration/tree/InMemoryNodeModel.java
    commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/tree/TestInMemoryNodeModel.java

Modified: commons/proper/configuration/branches/immutableNodes/src/main/java/org/apache/commons/configuration/tree/InMemoryNodeModel.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/immutableNodes/src/main/java/org/apache/commons/configuration/tree/InMemoryNodeModel.java?rev=1573213&r1=1573212&r2=1573213&view=diff
==============================================================================
--- commons/proper/configuration/branches/immutableNodes/src/main/java/org/apache/commons/configuration/tree/InMemoryNodeModel.java (original)
+++ commons/proper/configuration/branches/immutableNodes/src/main/java/org/apache/commons/configuration/tree/InMemoryNodeModel.java Sat Mar  1 20:04:19 2014
@@ -381,6 +381,21 @@ public class InMemoryNodeModel implement
     }
 
     /**
+     * Sets a new root node for this model. The whole structure is replaced by
+     * the new node and its children. Care has to be taken when this method is
+     * used and the model is accessed by multiple threads. It is not
+     * deterministic which concurrent operations see the old root and which see
+     * the new root node.
+     *
+     * @param newRoot the new root node to be set (can be <b>null</b>, then an
+     *        empty root node is set)
+     */
+    public void setRoot(ImmutableNode newRoot)
+    {
+        structure.set(createTreeData(initialRootNode(newRoot)));
+    }
+
+    /**
      * Returns the current {@code TreeData} object. This object contains all
      * information about the current node structure.
      *

Modified: commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/tree/TestInMemoryNodeModel.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/tree/TestInMemoryNodeModel.java?rev=1573213&r1=1573212&r2=1573213&view=diff
==============================================================================
--- commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/tree/TestInMemoryNodeModel.java (original)
+++ commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/tree/TestInMemoryNodeModel.java Sat Mar  1 20:04:19 2014
@@ -1224,4 +1224,33 @@ public class TestInMemoryNodeModel
     {
         checkAddNodesNoNodes(Collections.<ImmutableNode> emptySet());
     }
+
+    /**
+     * Tests whether a new root node can be set.
+     */
+    @Test
+    public void testSetRoot()
+    {
+        InMemoryNodeModel model =
+                new InMemoryNodeModel(NodeStructureHelper.ROOT_PERSONAE_TREE);
+        model.setRoot(NodeStructureHelper.ROOT_AUTHORS_TREE);
+        assertSame("Root node not changed",
+                NodeStructureHelper.ROOT_AUTHORS_TREE, model.getRootNode());
+        ImmutableNode node = nodeForKey(model, "Homer/Ilias");
+        assertEquals("Wrong parent mapping", nodeForKey(model, "Homer"),
+                model.getParent(node));
+    }
+
+    /**
+     * Tests whether the root node can be set to null.
+     */
+    @Test
+    public void testSetRootNull()
+    {
+        InMemoryNodeModel model =
+                new InMemoryNodeModel(NodeStructureHelper.ROOT_PERSONAE_TREE);
+        model.setRoot(null);
+        ImmutableNode rootNode = model.getRootNode();
+        assertTrue("Got children", rootNode.getChildren().isEmpty());
+    }
 }