NoteItem: replace empty target node properly
This commit is contained in:
parent
aaab7bbe67
commit
d82a7d6e9f
2 changed files with 68 additions and 23 deletions
|
@ -294,24 +294,26 @@ public class NodeItem<T> implements Node<T> {
|
||||||
@Override
|
@Override
|
||||||
public void placeNodeIn(final Node<T> nodeItem, final String... path) {
|
public void placeNodeIn(final Node<T> nodeItem, final String... path) {
|
||||||
if (path.length == 0) {
|
if (path.length == 0) {
|
||||||
if (nodeItem.isNamed()) {
|
if (!nodeItem.isNamed()) { // nothing to conflict with
|
||||||
final Optional<Node<T>> childNamed = findChildNamed(
|
addChild(nodeItem);
|
||||||
nodeItem.getName());
|
return;
|
||||||
if (childNamed.isPresent()) {
|
}
|
||||||
final Node<T> existing = childNamed.get();
|
final Optional<Node<T>> childNamed = findChildNamed(
|
||||||
if (!existing.isEmpty()) {
|
nodeItem.getName());
|
||||||
throw new NodeException(
|
if (!childNamed.isPresent()) { // nothing with the same name exists
|
||||||
"A non-empty node with that name already "
|
addChild(nodeItem);
|
||||||
+ "exists here");
|
return;
|
||||||
} else {
|
}
|
||||||
existing.getChildren().forEach(nodeItem::addChild);
|
// we have an existing node with the same name
|
||||||
existing.removeParent();
|
final Node<T> existing = childNamed.get();
|
||||||
nodeItem.setParent(this);
|
if (!existing.isEmpty()) {
|
||||||
return;
|
throw new NodeException(
|
||||||
}
|
"A non-empty node with that name already exists here");
|
||||||
}
|
} else {
|
||||||
|
existing.getChildren().forEach(nodeItem::addChild);
|
||||||
|
existing.removeParent();
|
||||||
|
addChild(nodeItem);
|
||||||
}
|
}
|
||||||
addChild(nodeItem);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
String item = path[0];
|
String item = path[0];
|
||||||
|
@ -321,12 +323,6 @@ public class NodeItem<T> implements Node<T> {
|
||||||
child = new NodeItem<>(null, item, this);
|
child = new NodeItem<>(null, item, this);
|
||||||
} else {
|
} else {
|
||||||
child = childNamed.get();
|
child = childNamed.get();
|
||||||
if (child.isEmpty()) {
|
|
||||||
if (path.length == 1) {
|
|
||||||
getChildren().forEach(nodeItem::addChild);
|
|
||||||
nodeItem.setParent(this);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
child.placeNodeIn(nodeItem, Arrays.copyOfRange(path, 1, path.length));
|
child.placeNodeIn(nodeItem, Arrays.copyOfRange(path, 1, path.length));
|
||||||
}
|
}
|
||||||
|
|
|
@ -744,4 +744,53 @@ public class NodeItemTest {
|
||||||
//when
|
//when
|
||||||
node.removeParent();
|
node.removeParent();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void placeNodeInTreeWhereNonEmptyNodeWithSameNameExists() {
|
||||||
|
//given
|
||||||
|
exception.expect(NodeException.class);
|
||||||
|
exception.expectMessage(
|
||||||
|
"A non-empty node with that name already exists here");
|
||||||
|
node = new NodeItem<>(null);
|
||||||
|
val child = new NodeItem<String>(null, "child", node);
|
||||||
|
new NodeItem<>("data", "grandchild", child);
|
||||||
|
// root -> child -> grandchild
|
||||||
|
// only grandchild has data
|
||||||
|
//when
|
||||||
|
// attempt to add another node called 'grandchild' to 'child'
|
||||||
|
node.placeNodeIn(new NodeItem<>("cuckoo", "grandchild"), "child");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public void placeNodeInTreeWhenAddedNodeIsUnnamed() {
|
||||||
|
//given
|
||||||
|
node = new NodeItem<>(null);
|
||||||
|
final Node<String> newNode = new NodeItem<>(null);
|
||||||
|
//when
|
||||||
|
node.placeNodeIn(newNode);
|
||||||
|
//then
|
||||||
|
assertThat(node.getChildren()).containsOnly(newNode);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public void placeNodeInTreeWhenEmptyChildWithTargetNameExists() {
|
||||||
|
//given
|
||||||
|
node = new NodeItem<>(null);
|
||||||
|
final NodeItem<String> child = new NodeItem<>(null, "child");
|
||||||
|
final NodeItem<String> target = new NodeItem<>(null, "target");
|
||||||
|
node.addChild(child);
|
||||||
|
child.addChild(target);
|
||||||
|
final NodeItem<String> addMe = new NodeItem<>("I'm new", "target");
|
||||||
|
assertThat(addMe.getParent()).isNull();
|
||||||
|
//when
|
||||||
|
// addMe should replace target as the sole descendant of child
|
||||||
|
node.placeNodeIn(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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue