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/04/05 21:48:56 UTC

svn commit: r1585191 - in /commons/proper/configuration/branches/immutableNodes/src: main/java/org/apache/commons/configuration/tree/ImmutableNode.java test/java/org/apache/commons/configuration/tree/TestImmutableNode.java

Author: oheger
Date: Sat Apr  5 19:48:55 2014
New Revision: 1585191

URL: http://svn.apache.org/r1585191
Log:
ImmutableNode now supports changing the name.

A setName() method was added which creates a new node instance with the
passed in name.

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

Modified: commons/proper/configuration/branches/immutableNodes/src/main/java/org/apache/commons/configuration/tree/ImmutableNode.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/immutableNodes/src/main/java/org/apache/commons/configuration/tree/ImmutableNode.java?rev=1585191&r1=1585190&r2=1585191&view=diff
==============================================================================
--- commons/proper/configuration/branches/immutableNodes/src/main/java/org/apache/commons/configuration/tree/ImmutableNode.java (original)
+++ commons/proper/configuration/branches/immutableNodes/src/main/java/org/apache/commons/configuration/tree/ImmutableNode.java Sat Apr  5 19:48:55 2014
@@ -40,6 +40,7 @@ import java.util.Map;
  * </p>
  *
  * @version $Id$
+ * @since 2.0
  */
 public class ImmutableNode
 {
@@ -113,6 +114,19 @@ public class ImmutableNode
 
     /**
      * Creates a new {@code ImmutableNode} instance which is a copy of this
+     * object with the name changed to the passed in value.
+     *
+     * @param name the name of the newly created node
+     * @return the new node with the changed name
+     */
+    public ImmutableNode setName(String name)
+    {
+        return new Builder(children, attributes).name(name).value(value)
+                .create();
+    }
+
+    /**
+     * Creates a new {@code ImmutableNode} instance which is a copy of this
      * object with the value changed to the passed in value.
      *
      * @param newValue the value of the newly created node

Modified: commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/tree/TestImmutableNode.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/tree/TestImmutableNode.java?rev=1585191&r1=1585190&r2=1585191&view=diff
==============================================================================
--- commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/tree/TestImmutableNode.java (original)
+++ commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/tree/TestImmutableNode.java Sat Apr  5 19:48:55 2014
@@ -315,8 +315,21 @@ public class TestImmutableNode
      */
     private ImmutableNode createDefaultNode(Object value)
     {
+        return createDefaultNode(NAME, value);
+    }
+
+    /**
+     * Creates a default node instance with a variable name and value that can
+     * be used by tests for updating properties.
+     *
+     * @param name the name of the node
+     * @param value the value of the node
+     * @return the default node instance
+     */
+    private ImmutableNode createDefaultNode(String name, Object value)
+    {
         ImmutableNode.Builder builder = new ImmutableNode.Builder(1);
-        return builder.name(NAME).addChild(createChild())
+        return builder.name(name).addChild(createChild())
                 .addAttribute("testAttr", "anotherTest").value(value).create();
     }
 
@@ -362,6 +375,21 @@ public class TestImmutableNode
     }
 
     /**
+     * Tests whether the name of a node can be changed for a new instance.
+     */
+    @Test
+    public void testSetName()
+    {
+        ImmutableNode node = createDefaultNode("anotherName", VALUE);
+        ImmutableNode node2 = node.setName(NAME);
+        checkUpdatedNode(node, node2);
+        assertSame("Different children", node.getChildren(),
+                node2.getChildren());
+        assertSame("Different attributes", node.getAttributes(),
+                node2.getAttributes());
+    }
+
+    /**
      * Tests whether a child node can be added.
      */
     @Test