Author: oheger
Date: Sun Feb 23 18:42:14 2014
New Revision: 1571060
URL: http://svn.apache.org/r1571060
Log:
Enhanced implementation of InMemoryNodeModel.addNodes().
Now the case is handled that a new node has to be created to which the passed
in nodes have to be added.
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=1571060&r1=1571059&r2=1571060&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
Sun Feb 23 18:42:14 2014
@@ -225,8 +225,22 @@ public class InMemoryNodeModel implement
List<QueryResult<ImmutableNode>> results =
resolver.resolveKey(tx.getCurrentData().getRoot(), key,
InMemoryNodeModel.this);
- // TODO handle other cases, e.g. empty or ambiguous result set
- tx.addAddNodesOperation(results.get(0).getNode(), nodes);
+ if (results.size() == 1)
+ {
+ tx.addAddNodesOperation(results.get(0).getNode(), nodes);
+ }
+ else
+ {
+ NodeAddData<ImmutableNode> addData =
+ resolver.resolveAddKey(tx.getCurrentData()
+ .getRoot(), key, InMemoryNodeModel.this);
+ ImmutableNode newNode =
+ new ImmutableNode.Builder(nodes.size())
+ .name(addData.getNewNodeName())
+ .addChildren(nodes).create();
+ addNodesByAddData(tx, addData,
+ Collections.singleton(newNode));
+ }
return true;
}
});
@@ -452,6 +466,23 @@ public class InMemoryNodeModel implement
{
Collection<ImmutableNode> newNodes =
createNodesToAdd(addData.getNewNodeName(), values);
+ addNodesByAddData(tx, addData, newNodes);
+ }
+
+ /**
+ * Initializes a transaction to add a collection of nodes as described by a
+ * {@code NodeAddData} object. If necessary, new path nodes are created.
+ * Eventually, the new nodes are added as children to the specified target
+ * node.
+ *
+ * @param tx the transaction
+ * @param addData the {@code NodeAddData}
+ * @param newNodes the collection of new child nodes
+ */
+ private static void addNodesByAddData(ModelTransaction tx,
+ NodeAddData<ImmutableNode> addData,
+ Collection<ImmutableNode> newNodes)
+ {
if (addData.getPathNodes().isEmpty())
{
tx.addAddNodesOperation(addData.getParent(), newNodes);
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=1571060&r1=1571059&r2=1571060&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
Sun Feb 23 18:42:14 2014
@@ -1134,4 +1134,35 @@ public class TestInMemoryNodeModel
assertSame("New child 2 not added", newWork2,
node.getChildren().get(size - 1));
}
+
+ /**
+ * Tests whether nodes can be added to a node which has to be created.
+ */
+ @Test
+ public void testAddNodesToNewNode()
+ {
+ NodeKeyResolver resolver = EasyMock.createMock(NodeKeyResolver.class);
+ InMemoryNodeModel model =
+ new InMemoryNodeModel(NodeStructureHelper.ROOT_AUTHORS_TREE);
+ final String newAuthor = "Goethe";
+ final String newWork = "Faust";
+ final String newPersona = "Mephisto";
+ EasyMock.expect(
+ resolver.resolveKey(NodeStructureHelper.ROOT_AUTHORS_TREE, KEY,
+ model)).andReturn(
+ new ArrayList<QueryResult<ImmutableNode>>(0));
+ EasyMock.expect(
+ resolver.resolveAddKey(NodeStructureHelper.ROOT_AUTHORS_TREE,
+ KEY, model)).andReturn(
+ new NodeAddData<ImmutableNode>(
+ NodeStructureHelper.ROOT_AUTHORS_TREE, newWork, false,
+ Arrays.asList(newAuthor)));
+ EasyMock.replay(resolver);
+
+ ImmutableNode personaNode =
+ new ImmutableNode.Builder().name(newPersona).create();
+ model.addNodes(KEY, Collections.singleton(personaNode), resolver);
+ assertSame("Wrong added node", personaNode,
+ nodeForKey(model, newAuthor + "/" + newWork + "/" +
newPersona));
+ }
}