diff --git a/src/main/java/net/kemitix/node/NodeItem.java b/src/main/java/net/kemitix/node/NodeItem.java index b77da2a..8ef24a6 100644 --- a/src/main/java/net/kemitix/node/NodeItem.java +++ b/src/main/java/net/kemitix/node/NodeItem.java @@ -376,12 +376,14 @@ public class NodeItem implements Node { @Override public void removeParent() { if (parent != null) { - parent.removeChild(this); + Node oldParent = parent; + Function, String> supplier = getNameSupplier(); parent = null; - if (nameSupplier == null) { + oldParent.removeChild(this); + if (this.nameSupplier == null) { // this is now a root node, so must provide a default name // supplier - nameSupplier = n -> null; + this.nameSupplier = supplier; } } } diff --git a/src/test/java/net/kemitix/node/NodeItemTest.java b/src/test/java/net/kemitix/node/NodeItemTest.java index 5f5a5b0..ec9953f 100644 --- a/src/test/java/net/kemitix/node/NodeItemTest.java +++ b/src/test/java/net/kemitix/node/NodeItemTest.java @@ -793,4 +793,67 @@ public class NodeItemTest { assertThat(target.getParent()).as("old node is removed from tree") .isNull(); } + + @Test + public void findChildNamedShouldThrowNPEWhenNameIsNull() { + //given + exception.expect(NullPointerException.class); + exception.expectMessage("name"); + node = new NodeItem<>(null); + //when + node.findChildNamed(null); + } + + @Test + public void isNamedNull() { + //given + node = new NodeItem<>(null); + //then + assertThat(node.isNamed()).isFalse(); + } + + @Test + public void isNamedEmpty() { + //given + node = new NodeItem<>(null, ""); + //then + assertThat(node.isNamed()).isFalse(); + } + + @Test + public void isNamedNamed() { + //given + node = new NodeItem<>(null, "named"); + //then + assertThat(node.isNamed()).isTrue(); + } + + @Test + public void removeParentNodeProvidesSameNameSupplier() { + // once a node has it's parent removed it should provide a default name + // provider + //given + node = new NodeItem<>("data", Node::getData); // name provider: getData + final NodeItem child = new NodeItem<>("other", node); + assertThat(node.getName()).as("initial root name").isEqualTo("data"); + assertThat(child.getName()).as("initial child name").isEqualTo("other"); + //when + child.removeParent(); + //then + assertThat(node.getName()).as("final root name").isEqualTo("data"); + assertThat(child.getName()).as("final child name").isEqualTo("other"); + } + + @Test + @SuppressWarnings("unchecked") + public void removeChildRemovesTheChild() { + //given + node = new NodeItem<>(null); + Node child = node.createChild("child"); + assertThat(node.getChildren()).containsExactly(child); + //then + node.removeChild(child); + //then + assertThat(node.getChildren()).isEmpty(); + } }