Author: oheger
Date: Sat Feb 1 20:29:54 2014
New Revision: 1563462
URL: http://svn.apache.org/r1563462
Log:
Reworked NodeAddData class.
Made the class immutable. It now has a generic parameter for the type of nodes
supported.
Modified:
commons/proper/configuration/branches/immutableNodes/src/main/java/org/apache/commons/configuration/tree/NodeAddData.java
commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/tree/TestNodeAddData.java
Modified:
commons/proper/configuration/branches/immutableNodes/src/main/java/org/apache/commons/configuration/tree/NodeAddData.java
URL:
http://svn.apache.org/viewvc/commons/proper/configuration/branches/immutableNodes/src/main/java/org/apache/commons/configuration/tree/NodeAddData.java?rev=1563462&r1=1563461&r2=1563462&view=diff
==============================================================================
---
commons/proper/configuration/branches/immutableNodes/src/main/java/org/apache/commons/configuration/tree/NodeAddData.java
(original)
+++
commons/proper/configuration/branches/immutableNodes/src/main/java/org/apache/commons/configuration/tree/NodeAddData.java
Sat Feb 1 20:29:54 2014
@@ -16,8 +16,9 @@
*/
package org.apache.commons.configuration.tree;
+import java.util.ArrayList;
+import java.util.Collection;
import java.util.Collections;
-import java.util.LinkedList;
import java.util.List;
/**
@@ -46,47 +47,41 @@ import java.util.List;
* </p>
*
* @since 1.3
- * @author <a
- * href="http://commons.apache.org/configuration/team-list.html">Commons
- * Configuration team</a>
* @version $Id$
+ * @param <T> the type of nodes this class can handle
*/
-public class NodeAddData
+public class NodeAddData<T>
{
/** Stores the parent node of the add operation. */
- private ConfigurationNode parent;
+ private final T parent;
/**
- * Stores a list with nodes that are on the path between the parent node
and
- * the new node.
+ * Stores a list with the names of nodes that are on the path between the
+ * parent node and the new node.
*/
- private List<String> pathNodes;
+ private final List<String> pathNodes;
/** Stores the name of the new node. */
- private String newNodeName;
+ private final String newNodeName;
/** Stores the attribute flag. */
- private boolean attribute;
+ private final boolean attribute;
/**
- * Creates a new, uninitialized instance of {@code NodeAddData}.
- */
- public NodeAddData()
- {
- this(null, null);
- }
-
- /**
- * Creates a new instance of {@code NodeAddData} and sets the most
- * important data fields.
+ * Creates a new instance of {@code NodeAddData} and initializes it.
*
- * @param parent the parent node
- * @param nodeName the name of the new node
+ * @param parentNode the parent node of the add operation
+ * @param newName the name of the new node
+ * @param isAttr flag whether the new node is an attribute
+ * @param intermediateNodes an optional collection with path nodes
*/
- public NodeAddData(ConfigurationNode parent, String nodeName)
+ public NodeAddData(T parentNode, String newName, boolean isAttr,
+ Collection<String> intermediateNodes)
{
- setParent(parent);
- setNewNodeName(nodeName);
+ parent = parentNode;
+ newNodeName = newName;
+ attribute = isAttr;
+ pathNodes = createPathNodes(intermediateNodes);
}
/**
@@ -101,17 +96,6 @@ public class NodeAddData
}
/**
- * Sets the attribute flag. This flag determines whether an attribute or a
- * child node will be added.
- *
- * @param attribute the attribute flag
- */
- public void setAttribute(boolean attribute)
- {
- this.attribute = attribute;
- }
-
- /**
* Returns the name of the new node.
*
* @return the new node's name
@@ -122,39 +106,18 @@ public class NodeAddData
}
/**
- * Sets the name of the new node. A node with this name will be added to
the
- * configuration's node hierarchy.
- *
- * @param newNodeName the name of the new node
- */
- public void setNewNodeName(String newNodeName)
- {
- this.newNodeName = newNodeName;
- }
-
- /**
* Returns the parent node.
*
* @return the parent node
*/
- public ConfigurationNode getParent()
+ public T getParent()
{
return parent;
}
/**
- * Sets the parent node. New nodes will be added to this node.
- *
- * @param parent the parent node
- */
- public void setParent(ConfigurationNode parent)
- {
- this.parent = parent;
- }
-
- /**
* Returns a list with further nodes that must be added. This is needed if
a
- * complete branch is to be added at once. For instance imagine that there
+ * complete branch is to be added at once. For instance, imagine that there
* exists only a node {@code database}. Now the key
* {@code database.connection.settings.username} (assuming the syntax
* of the default expression engine) is to be added. Then
@@ -168,29 +131,26 @@ public class NodeAddData
*/
public List<String> getPathNodes()
{
- if (pathNodes != null)
- {
- return Collections.unmodifiableList(pathNodes);
- }
- else
- {
- return Collections.emptyList();
- }
+ return pathNodes;
}
/**
- * Adds the name of a path node. With this method an additional node to be
- * added can be defined.
+ * Creates the list with path nodes. Handles null input.
*
- * @param nodeName the name of the node
- * @see #getPathNodes()
+ * @param intermediateNodes the nodes passed to the constructor
+ * @return an unmodifiable list of path nodes
*/
- public void addPathNode(String nodeName)
+ private static List<String> createPathNodes(
+ Collection<String> intermediateNodes)
{
- if (pathNodes == null)
+ if (intermediateNodes == null)
+ {
+ return Collections.emptyList();
+ }
+ else
{
- pathNodes = new LinkedList<String>();
+ return Collections.unmodifiableList(new ArrayList<String>(
+ intermediateNodes));
}
- pathNodes.add(nodeName);
}
}
Modified:
commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/tree/TestNodeAddData.java
URL:
http://svn.apache.org/viewvc/commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/tree/TestNodeAddData.java?rev=1563462&r1=1563461&r2=1563462&view=diff
==============================================================================
---
commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/tree/TestNodeAddData.java
(original)
+++
commons/proper/configuration/branches/immutableNodes/src/test/java/org/apache/commons/configuration/tree/TestNodeAddData.java
Sat Feb 1 20:29:54 2014
@@ -17,91 +17,91 @@
package org.apache.commons.configuration.tree;
import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertNull;
-import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
+import java.util.ArrayList;
+import java.util.Collections;
import java.util.List;
-import org.junit.Before;
+import org.junit.BeforeClass;
import org.junit.Test;
/**
* Test class for NodeAddData.
*
- * @author <a
- * href="http://commons.apache.org/configuration/team-list.html">Commons
- * Configuration team</a>
* @version $Id$
*/
public class TestNodeAddData
{
- /** Constant for the default parent node used for testing. */
- private static final ConfigurationNode TEST_PARENT = new
DefaultConfigurationNode(
- "parent");
-
/** Constant for the name of the new node. */
private static final String TEST_NODENAME = "testNewNode";
/** Constant for the name of a path node. */
private static final String PATH_NODE_NAME = "PATHNODE";
- /** Constant for the number of path nodes to be added. */
- private static final int PATH_NODE_COUNT = 10;
-
- /** The object to be tested. */
- NodeAddData addData;
+ /** A default parent node. */
+ private static ImmutableNode parentNode;
- @Before
- public void setUp() throws Exception
+ @BeforeClass
+ public static void setUpBeforeClass() throws Exception
{
- addData = new NodeAddData(TEST_PARENT, TEST_NODENAME);
+ parentNode = new ImmutableNode.Builder().name("testParent").create();
}
/**
- * Tests the default values of an uninitialized instance.
+ * Tests whether the constructor can handle a null collection of path
nodes.
*/
@Test
- public void testUninitialized()
+ public void testPathNodesNull()
{
- addData = new NodeAddData();
- assertNull("A parent is set", addData.getParent());
- assertNull("Node has a name", addData.getNewNodeName());
- assertFalse("Attribute flag is set", addData.isAttribute());
- assertTrue("Path nodes are not empty",
addData.getPathNodes().isEmpty());
+ NodeAddData<ImmutableNode> data =
+ new NodeAddData<ImmutableNode>(parentNode, TEST_NODENAME,
+ false, null);
+ assertTrue("Got path nodes", data.getPathNodes().isEmpty());
}
/**
- * Tests the constructor that initializes the most important fields.
+ * Tests whether the collection with path nodes cannot be modified if no
+ * data is available.
*/
- @Test
- public void testInitialized()
+ @Test(expected = UnsupportedOperationException.class)
+ public void testPathNodesNullModify()
{
- assertSame("Wrong parent", TEST_PARENT, addData.getParent());
- assertEquals("Wrong node name", TEST_NODENAME,
addData.getNewNodeName());
- assertFalse("Attribute flag is set", addData.isAttribute());
- assertTrue("Path nodes are not empty",
addData.getPathNodes().isEmpty());
+ NodeAddData<ImmutableNode> data =
+ new NodeAddData<ImmutableNode>(parentNode, TEST_NODENAME,
+ false, null);
+ data.getPathNodes().add("test");
}
/**
- * Tests adding path nodes.
+ * Tests whether a defensive copy of the collection with path nodes is
+ * created.
*/
@Test
- public void testAddPathNode()
+ public void testInitPathNodesDefensiveCopy()
{
- for (int i = 0; i < PATH_NODE_COUNT; i++)
- {
- addData.addPathNode(PATH_NODE_NAME + i);
- }
-
- List<String> nodes = addData.getPathNodes();
- assertEquals("Incorrect number of path nodes", PATH_NODE_COUNT, nodes
+ List<String> pathNodes = new ArrayList<String>();
+ pathNodes.add(PATH_NODE_NAME);
+ NodeAddData<ImmutableNode> data =
+ new NodeAddData<ImmutableNode>(parentNode, TEST_NODENAME,
+ false, pathNodes);
+ pathNodes.add("anotherNode");
+ assertEquals("Wrong number of path nodes", 1, data.getPathNodes()
.size());
- for (int i = 0; i < PATH_NODE_COUNT; i++)
- {
- assertEquals("Wrong path node at position" + i, PATH_NODE_NAME + i,
- nodes.get(i));
- }
+ assertEquals("Wrong path node", PATH_NODE_NAME, data.getPathNodes()
+ .get(0));
+ }
+
+ /**
+ * Tests that the collection with path nodes cannot be modified if data is
+ * available.
+ */
+ @Test(expected = UnsupportedOperationException.class)
+ public void testPathNodesDefinedModify()
+ {
+ NodeAddData<ImmutableNode> data =
+ new NodeAddData<ImmutableNode>(parentNode, TEST_NODENAME,
+ false, Collections.singleton(PATH_NODE_NAME));
+ data.getPathNodes().add("anotherNode");
}
}