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.
This commit is contained in:
parent
96ac0f720f
commit
eebd293633
2 changed files with 8 additions and 8 deletions
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
|
@ -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
|
||||||
|
|
Loading…
Reference in a new issue