Merge pull request #8 from kemitix/insertinpath-uses-setdata

insertInPath() uses setData() to merge nodes
This commit is contained in:
Paul Campbell 2016-05-24 23:02:35 +01:00
commit aa4be5ebf1
2 changed files with 8 additions and 8 deletions

View file

@ -333,9 +333,7 @@ public class NodeItem<T> implements Node<T> {
throw new NodeException( throw new NodeException(
"A non-empty node with that name already exists here"); "A non-empty node with that name already exists here");
} else { } else {
existing.getChildren().forEach(nodeItem::addChild); existing.setData(nodeItem.getData());
existing.removeParent();
addChild(nodeItem);
} }
return; return;
} }

View file

@ -734,7 +734,9 @@ public class NodeItemTest {
node.insertInPath(grandchild, "child"); node.insertInPath(grandchild, "child");
node.insertInPath(child); node.insertInPath(child);
//then //then
assertThat(node.getChildByName("child")).as("child").isSameAs(child); assertThat(node.getChildByName("child").getData()).as("data in tree")
.isSameAs(
"child data");
assertThat( assertThat(
node.getChildByName("child").getChildByName("grandchild")).as( node.getChildByName("child").getChildByName("grandchild")).as(
"grandchild").isSameAs(grandchild); "grandchild").isSameAs(grandchild);
@ -798,14 +800,14 @@ public class NodeItemTest {
child.addChild(target); child.addChild(target);
final NodeItem<String> addMe = new NodeItem<>("I'm new", "target"); final NodeItem<String> addMe = new NodeItem<>("I'm new", "target");
assertThat(addMe.getParent()).isNull(); assertThat(addMe.getParent()).isNull();
assertThat(child.getChildByName("target").isEmpty()).as(
"target starts empty").isTrue();
//when //when
// addMe should replace target as the sole descendant of child // addMe should replace target as the sole descendant of child
node.insertInPath(addMe, "child"); node.insertInPath(addMe, "child");
//then //then
assertThat(child.getChildren()).as("child only contains new node") assertThat(child.getChildByName("target").getData()).as(
.containsOnly(addMe); "target now contains data").isEqualTo("I'm new");
assertThat(target.getParent()).as("old node is removed from tree")
.isNull();
} }
@Test @Test