NodeItem.removeParent(): use same name supplier in the new root node
This commit is contained in:
parent
d82a7d6e9f
commit
6f81a62162
2 changed files with 68 additions and 3 deletions
|
@ -376,12 +376,14 @@ public class NodeItem<T> implements Node<T> {
|
|||
@Override
|
||||
public void removeParent() {
|
||||
if (parent != null) {
|
||||
parent.removeChild(this);
|
||||
Node<T> oldParent = parent;
|
||||
Function<Node<T>, 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<String> 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<String> child = node.createChild("child");
|
||||
assertThat(node.getChildren()).containsExactly(child);
|
||||
//then
|
||||
node.removeChild(child);
|
||||
//then
|
||||
assertThat(node.getChildren()).isEmpty();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue