From eebd2936338d509ea30f320b3bf8e4b602e14a9f Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Tue, 24 May 2016 22:16:35 +0100 Subject: [PATCH] NodeItem.insertInPath(): use setData() to when target is already in tree Where an empty node is already found in the tree where we want to insert a node, use the setData() method to place the data. This replaces the previous process of attempting to swapping in the new node. --- src/main/java/net/kemitix/node/NodeItem.java | 4 +--- src/test/java/net/kemitix/node/NodeItemTest.java | 12 +++++++----- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/main/java/net/kemitix/node/NodeItem.java b/src/main/java/net/kemitix/node/NodeItem.java index a6c7dea..b724048 100644 --- a/src/main/java/net/kemitix/node/NodeItem.java +++ b/src/main/java/net/kemitix/node/NodeItem.java @@ -333,9 +333,7 @@ public class NodeItem implements Node { throw new NodeException( "A non-empty node with that name already exists here"); } else { - existing.getChildren().forEach(nodeItem::addChild); - existing.removeParent(); - addChild(nodeItem); + existing.setData(nodeItem.getData()); } return; } diff --git a/src/test/java/net/kemitix/node/NodeItemTest.java b/src/test/java/net/kemitix/node/NodeItemTest.java index dde300a..664ec82 100644 --- a/src/test/java/net/kemitix/node/NodeItemTest.java +++ b/src/test/java/net/kemitix/node/NodeItemTest.java @@ -734,7 +734,9 @@ public class NodeItemTest { node.insertInPath(grandchild, "child"); node.insertInPath(child); //then - assertThat(node.getChildByName("child")).as("child").isSameAs(child); + assertThat(node.getChildByName("child").getData()).as("data in tree") + .isSameAs( + "child data"); assertThat( node.getChildByName("child").getChildByName("grandchild")).as( "grandchild").isSameAs(grandchild); @@ -798,14 +800,14 @@ public class NodeItemTest { child.addChild(target); final NodeItem addMe = new NodeItem<>("I'm new", "target"); assertThat(addMe.getParent()).isNull(); + assertThat(child.getChildByName("target").isEmpty()).as( + "target starts empty").isTrue(); //when // addMe should replace target as the sole descendant of child node.insertInPath(addMe, "child"); //then - assertThat(child.getChildren()).as("child only contains new node") - .containsOnly(addMe); - assertThat(target.getParent()).as("old node is removed from tree") - .isNull(); + assertThat(child.getChildByName("target").getData()).as( + "target now contains data").isEqualTo("I'm new"); } @Test