Merge branch release/0.6.0 into master

Add streamAll() support
This commit is contained in:
Paul Campbell 2017-02-16 21:19:07 +00:00
commit 41b5ffca44
6 changed files with 193 additions and 139 deletions

View file

@ -1,6 +1,11 @@
CHANGELOG CHANGELOG
========= =========
0.6.0
------
* Add streamAll() support
0.5.0 0.5.0
------ ------

19
pom.xml
View file

@ -2,7 +2,7 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<artifactId>node</artifactId> <artifactId>node</artifactId>
<version>0.5.0</version> <version>0.6.0</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<name>Node</name> <name>Node</name>
@ -11,14 +11,16 @@
<parent> <parent>
<groupId>net.kemitix</groupId> <groupId>net.kemitix</groupId>
<artifactId>kemitix-parent</artifactId> <artifactId>kemitix-parent</artifactId>
<version>2.1.0</version> <version>2.4.0</version>
</parent> </parent>
<properties> <properties>
<lombok.version>1.16.10</lombok.version> <lombok.version>1.16.10</lombok.version>
<assertj.version>3.5.2</assertj.version> <assertj.version>3.6.2</assertj.version>
<coveralls-maven-plugin.version>4.2.0</coveralls-maven-plugin.version> <coveralls-maven-plugin.version>4.3.0</coveralls-maven-plugin.version>
<trajano-commons-testing.version>2.1.0</trajano-commons-testing.version> <trajano-commons-testing.version>2.1.0</trajano-commons-testing.version>
<junit.version>4.12</junit.version>
<hamcrest.version>1.3</hamcrest.version>
</properties> </properties>
<issueManagement> <issueManagement>
@ -47,21 +49,16 @@
</build> </build>
<dependencies> <dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</dependency>
<dependency> <dependency>
<groupId>junit</groupId> <groupId>junit</groupId>
<artifactId>junit</artifactId> <artifactId>junit</artifactId>
<version>4.12</version> <version>${junit.version}</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.hamcrest</groupId> <groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId> <artifactId>hamcrest-core</artifactId>
<version>1.3</version> <version>${hamcrest.version}</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency> <dependency>

View file

@ -27,6 +27,7 @@ package net.kemitix.node;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import java.util.Set; import java.util.Set;
import java.util.stream.Stream;
/** /**
* An interface for tree node items. * An interface for tree node items.
@ -221,4 +222,11 @@ public interface Node<T> {
* Removes the parent from the node. Makes the node into a new root node. * Removes the parent from the node. Makes the node into a new root node.
*/ */
void removeParent(); void removeParent();
/**
* Returns a stream containing the node and all its children and their children.
*
* @return a stream of all the nodes in the tree below this node
*/
Stream<Node<T>> streamAll();
} }

View file

@ -25,6 +25,7 @@ SOFTWARE.
package net.kemitix.node; package net.kemitix.node;
import lombok.NonNull; import lombok.NonNull;
import lombok.ToString;
import lombok.val; import lombok.val;
import java.util.Arrays; import java.util.Arrays;
@ -32,6 +33,7 @@ import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import java.util.Set; import java.util.Set;
import java.util.stream.Stream;
/** /**
* Represents a tree of nodes. * Represents a tree of nodes.
@ -40,6 +42,7 @@ import java.util.Set;
* *
* @author Paul Campbell (pcampbell@kemitix.net) * @author Paul Campbell (pcampbell@kemitix.net)
*/ */
@ToString(exclude = "children")
class NodeItem<T> implements Node<T> { class NodeItem<T> implements Node<T> {
private final Set<Node<T>> children = new HashSet<>(); private final Set<Node<T>> children = new HashSet<>();
@ -143,7 +146,13 @@ class NodeItem<T> implements Node<T> {
@Override @Override
public void addChild(@NonNull final Node<T> child) { public void addChild(@NonNull final Node<T> child) {
verifyChildIsNotAnAncestor(child); verifyChildIsNotAnAncestor(child);
verifyChildWithSameNameDoesNotAlreadyExist(child); //verifyChildWithSameNameDoesNotAlreadyExist
if (child.isNamed()) {
findChildByName(child.getName()).filter(existingChild -> existingChild != child)
.ifPresent(existingChild -> {
throw new NodeException("Node with that name already exists here");
});
}
children.add(child); children.add(child);
// update the child's parent if they don't have one or it is not this // update the child's parent if they don't have one or it is not this
val childParent = child.getParent(); val childParent = child.getParent();
@ -153,17 +162,6 @@ class NodeItem<T> implements Node<T> {
} }
} }
private void verifyChildWithSameNameDoesNotAlreadyExist(
final @NonNull Node<T> child
) {
if (child.isNamed()) {
findChildByName(child.getName()).filter(existingChild -> existingChild != child)
.ifPresent(existingChild -> {
throw new NodeException("Node with that name already exists here");
});
}
}
private void verifyChildIsNotAnAncestor(final @NonNull Node<T> child) { private void verifyChildIsNotAnAncestor(final @NonNull Node<T> child) {
if (this.equals(child) || isDescendantOf(child)) { if (this.equals(child) || isDescendantOf(child)) {
throw new NodeException("Child is an ancestor"); throw new NodeException("Child is an ancestor");
@ -352,4 +350,9 @@ class NodeItem<T> implements Node<T> {
} }
} }
@Override
public Stream<Node<T>> streamAll() {
return Stream.concat(Stream.of(this), getChildren().stream()
.flatMap(Node::streamAll));
}
} }

View file

@ -10,6 +10,7 @@ import org.junit.rules.ExpectedException;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.Optional; import java.util.Optional;
import java.util.stream.Collectors;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
@ -39,8 +40,8 @@ public class ImmutableNodeItemTest {
//when //when
immutableNode = Nodes.asImmutable(Nodes.unnamedRoot(data)); immutableNode = Nodes.asImmutable(Nodes.unnamedRoot(data));
//then //then
assertThat(immutableNode.getData()).as( assertThat(immutableNode.getData()).as("can get the data from a immutableNode")
"can get the data from a immutableNode"). .
contains(data); contains(data);
} }
@ -73,8 +74,7 @@ public class ImmutableNodeItemTest {
//given //given
immutableNode = Nodes.asImmutable(Nodes.unnamedRoot("data")); immutableNode = Nodes.asImmutable(Nodes.unnamedRoot("data"));
//then //then
assertThat(immutableNode.getParent()).as( assertThat(immutableNode.getParent()).as("immutableNode created without a parent has no parent")
"immutableNode created without a parent has no parent")
.isEmpty(); .isEmpty();
} }
@ -101,13 +101,11 @@ public class ImmutableNodeItemTest {
//then //then
// get the immutable node's child's parent // get the immutable node's child's parent
val immutableChild = immutableNode.getChildByName("child"); val immutableChild = immutableNode.getChildByName("child");
final Optional<Node<String>> optionalParent final Optional<Node<String>> optionalParent = immutableChild.getParent();
= immutableChild.getParent();
if (optionalParent.isPresent()) { if (optionalParent.isPresent()) {
val p = optionalParent.get(); val p = optionalParent.get();
assertThat(p).hasFieldOrPropertyWithValue("name", "root") assertThat(p).hasFieldOrPropertyWithValue("name", "root")
.hasFieldOrPropertyWithValue("data", .hasFieldOrPropertyWithValue("data", Optional.of("parent"));
Optional.of("parent"));
} }
} }
@ -153,7 +151,8 @@ public class ImmutableNodeItemTest {
//then //then
assertThat(result.isPresent()).isTrue(); assertThat(result.isPresent()).isTrue();
if (result.isPresent()) { if (result.isPresent()) {
assertThat(result.get().getName()).isEqualTo("child"); assertThat(result.get()
.getName()).isEqualTo("child");
} }
} }
@ -169,8 +168,7 @@ public class ImmutableNodeItemTest {
Nodes.unnamedChild("child", Nodes.unnamedChild("parent", root)); Nodes.unnamedChild("child", Nodes.unnamedChild("parent", root));
immutableNode = Nodes.asImmutable(root); immutableNode = Nodes.asImmutable(root);
//when //when
val result = immutableNode.findInPath( val result = immutableNode.findInPath(Arrays.asList("parent", "no child"));
Arrays.asList("parent", "no child"));
//then //then
assertThat(result.isPresent()).isFalse(); assertThat(result.isPresent()).isFalse();
} }
@ -218,7 +216,8 @@ public class ImmutableNodeItemTest {
//then //then
assertThat(result.isPresent()).isTrue(); assertThat(result.isPresent()).isTrue();
if (result.isPresent()) { if (result.isPresent()) {
assertThat(result.get().getData()).contains("child"); assertThat(result.get()
.getData()).contains("child");
} }
} }
@ -324,8 +323,7 @@ public class ImmutableNodeItemTest {
val bob = Nodes.namedChild("bob data", "bob", root); val bob = Nodes.namedChild("bob data", "bob", root);
val alice = Nodes.namedChild("alice data", "alice", root); val alice = Nodes.namedChild("alice data", "alice", root);
Nodes.namedChild("dave data", "dave", alice); Nodes.namedChild("dave data", "dave", alice);
Nodes.unnamedChild("bob's child's data", Nodes.unnamedChild("bob's child's data", bob); // has no name and no children so no included
bob); // has no name and no children so no included
val kim = Nodes.unnamedChild("kim data", root); // nameless mother val kim = Nodes.unnamedChild("kim data", root); // nameless mother
Nodes.namedChild("lucy data", "lucy", kim); Nodes.namedChild("lucy data", "lucy", kim);
immutableNode = Nodes.asImmutable(root); immutableNode = Nodes.asImmutable(root);
@ -333,11 +331,9 @@ public class ImmutableNodeItemTest {
val tree = immutableNode.drawTree(0); val tree = immutableNode.drawTree(0);
//then //then
String[] lines = tree.split("\n"); String[] lines = tree.split("\n");
assertThat(lines).contains("[root]", "[ alice]", "[ dave]", assertThat(lines).contains("[root]", "[ alice]", "[ dave]", "[ (unnamed)]", "[ lucy]", "[ bob]");
"[ (unnamed)]", "[ lucy]", "[ bob]");
assertThat(lines).containsSubsequence("[root]", "[ alice]", "[ dave]"); assertThat(lines).containsSubsequence("[root]", "[ alice]", "[ dave]");
assertThat(lines).containsSubsequence("[root]", "[ (unnamed)]", assertThat(lines).containsSubsequence("[root]", "[ (unnamed)]", "[ lucy]");
"[ lucy]");
assertThat(lines).containsSubsequence("[root]", "[ bob]"); assertThat(lines).containsSubsequence("[root]", "[ bob]");
} }
@ -406,8 +402,7 @@ public class ImmutableNodeItemTest {
immutableNode = Nodes.asImmutable(Nodes.unnamedRoot("")); immutableNode = Nodes.asImmutable(Nodes.unnamedRoot(""));
expectImmutableException(); expectImmutableException();
//when //when
immutableNode.createDescendantLine( immutableNode.createDescendantLine(Arrays.asList("child", "grandchild"));
Arrays.asList("child", "grandchild"));
} }
@Test @Test
@ -427,4 +422,30 @@ public class ImmutableNodeItemTest {
//when //when
Nodes.asImmutable(Nodes.unnamedChild("child", Nodes.unnamedRoot("root"))); Nodes.asImmutable(Nodes.unnamedChild("child", Nodes.unnamedRoot("root")));
} }
@Test
public void canStreamAll() throws Exception {
//given
val node = Nodes.namedRoot("root", "root");
val n1 = Nodes.namedChild("one", "one", node);
val n2 = Nodes.namedChild("two", "two", node);
Nodes.namedChild("three", "three", n1);
Nodes.namedChild("four", "four", n2);
val n5 = Nodes.namedChild("five", "five", n1);
val n6 = Nodes.namedChild("six", "six", n2);
Nodes.namedChild("seven", "seven", n5);
Nodes.namedChild("eight", "eight", n6);
val immutableRoot = Nodes.asImmutable(node);
//when
val result = immutableRoot.streamAll()
.collect(Collectors.toList());
//then
assertThat(result).as("full tree")
.hasSize(9);
// and
assertThat(immutableRoot.getChild("one")
.streamAll()
.collect(Collectors.toList())).as("sub-tree")
.hasSize(4);
}
} }

View file

@ -11,6 +11,7 @@ import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.Optional; import java.util.Optional;
import java.util.Set; import java.util.Set;
import java.util.stream.Collectors;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
@ -33,8 +34,9 @@ public class NodeItemTest {
//when //when
node = Nodes.unnamedRoot(data); node = Nodes.unnamedRoot(data);
//then //then
assertThat(node.getData()).as("can get the data from a node"). assertThat(node.getData()).as("can get the data from a node")
contains(data); .
contains(data);
} }
@Test @Test
@ -43,8 +45,12 @@ public class NodeItemTest {
node = Nodes.unnamedRoot(null); node = Nodes.unnamedRoot(null);
//then //then
SoftAssertions softly = new SoftAssertions(); SoftAssertions softly = new SoftAssertions();
softly.assertThat(node.isEmpty()).as("node is empty").isTrue(); softly.assertThat(node.isEmpty())
softly.assertThat(node.isNamed()).as("node is unnamed").isFalse(); .as("node is empty")
.isTrue();
softly.assertThat(node.isNamed())
.as("node is unnamed")
.isFalse();
softly.assertAll(); softly.assertAll();
} }
@ -66,8 +72,8 @@ public class NodeItemTest {
//given //given
node = Nodes.unnamedRoot("data"); node = Nodes.unnamedRoot("data");
//then //then
assertThat(node.getParent()).as( assertThat(node.getParent()).as("node created without a parent has no parent")
"node created without a parent has no parent").isEmpty(); .isEmpty();
} }
/** /**
@ -80,8 +86,7 @@ public class NodeItemTest {
//when //when
node = Nodes.unnamedChild("subject", parent); node = Nodes.unnamedChild("subject", parent);
//then //then
assertThat(node.getParent()).as( assertThat(node.getParent()).as("node created with a parent can return the parent")
"node created with a parent can return the parent")
.contains(parent); .contains(parent);
} }
@ -124,8 +129,8 @@ public class NodeItemTest {
node = Nodes.unnamedChild("subject", parent); node = Nodes.unnamedChild("subject", parent);
//then //then
assertThat(parent.getChildren()).as( assertThat(parent.getChildren()).as(
"when a node is created with a parent, the parent has the new" "when a node is created with a parent, the parent has the new" + " node among it's children")
+ " node among it's children").contains(node); .contains(node);
} }
/** /**
@ -139,9 +144,8 @@ public class NodeItemTest {
//when //when
node.setParent(parent); node.setParent(parent);
//then //then
assertThat(node.getParent()).as( assertThat(node.getParent()).as("when a node is assigned a new parent that parent can be " + "returned")
"when a node is assigned a new parent that parent can be " .contains(parent);
+ "returned").contains(parent);
} }
/** /**
@ -184,12 +188,12 @@ public class NodeItemTest {
//when //when
child.setParent(newParent); child.setParent(newParent);
//then //then
assertThat(child.getParent()).as( assertThat(child.getParent()).as("when a node is assigned a new parent, the old parent is " + "replaced")
"when a node is assigned a new parent, the old parent is " .contains(newParent);
+ "replaced").contains(newParent); assertThat(node.findChild("child")
assertThat(node.findChild("child").isPresent()).as( .isPresent()).as(
"when a node is assigned a new parent, the old parent no " "when a node is assigned a new parent, the old parent no " + "longer has the node among it's children")
+ "longer has the node among it's children").isFalse(); .isFalse();
} }
/** /**
@ -205,14 +209,14 @@ public class NodeItemTest {
//when //when
newParent.addChild(child); newParent.addChild(child);
//then //then
assertThat(child.getParent()).as( assertThat(child.getParent()).as("when a node with an existing parent is added as a child " +
"when a node with an existing parent is added as a child " "to another node, then the old parent is replaced")
+ "to another node, then the old parent is replaced")
.contains(newParent); .contains(newParent);
assertThat(node.findChild("child").isPresent()).as( assertThat(node.findChild("child")
"when a node with an existing parent is added as a child to " .isPresent()).as("when a node with an existing parent is added as a child to " +
+ "another node, then the old parent no longer has " "another node, then the old parent no longer has " +
+ "the node among it's children").isFalse(); "the node among it's children")
.isFalse();
} }
/** /**
@ -240,9 +244,8 @@ public class NodeItemTest {
//when //when
node.addChild(child); node.addChild(child);
//then //then
assertThat(node.getChildren()).as( assertThat(node.getChildren()).as("when a node is added as a child, the node is among the " + "children")
"when a node is added as a child, the node is among the " .contains(child);
+ "children").contains(child);
} }
/** /**
@ -313,9 +316,8 @@ public class NodeItemTest {
//when //when
node.addChild(child); node.addChild(child);
//then //then
assertThat(child.getParent()).as( assertThat(child.getParent()).as("when a node is added as a child, the child has the node as " + "its parent")
"when a node is added as a child, the child has the node as " .contains(node);
+ "its parent").contains(node);
} }
/** /**
@ -334,11 +336,10 @@ public class NodeItemTest {
//when //when
val result = grandParentNode.findInPath(Arrays.asList(parent, subject)); val result = grandParentNode.findInPath(Arrays.asList(parent, subject));
//then //then
assertThat(result.isPresent()).as( assertThat(result.isPresent()).as("when we walk the tree to a node it is found")
"when we walk the tree to a node it is found").isTrue(); .isTrue();
if (result.isPresent()) { if (result.isPresent()) {
assertThat(result.get()).as( assertThat(result.get()).as("when we walk the tree to a node the correct node is found")
"when we walk the tree to a node the correct node is found")
.isSameAs(node); .isSameAs(node);
} }
} }
@ -358,9 +359,8 @@ public class NodeItemTest {
//when //when
val result = parentNode.findInPath(Arrays.asList(subject, "no child")); val result = parentNode.findInPath(Arrays.asList(subject, "no child"));
//then //then
assertThat(result.isPresent()).as( assertThat(result.isPresent()).as("when we walk the tree to a node that doesn't exists, nothing" + " is found")
"when we walk the tree to a node that doesn't exists, nothing" .isFalse();
+ " is found").isFalse();
} }
/** /**
@ -403,37 +403,32 @@ public class NodeItemTest {
val betaData = "beta"; val betaData = "beta";
val gammaData = "gamma"; val gammaData = "gamma";
//when //when
node.createDescendantLine( node.createDescendantLine(Arrays.asList(alphaData, betaData, gammaData));
Arrays.asList(alphaData, betaData, gammaData));
//then //then
val alphaOptional = node.findChild(alphaData); val alphaOptional = node.findChild(alphaData);
assertThat(alphaOptional.isPresent()).as( assertThat(alphaOptional.isPresent()).as("when creating a descendant line, the first element is found")
"when creating a descendant line, the first element is found")
.isTrue(); .isTrue();
if (alphaOptional.isPresent()) { if (alphaOptional.isPresent()) {
val alpha = alphaOptional.get(); val alpha = alphaOptional.get();
assertThat(alpha.getParent()).as( assertThat(alpha.getParent()).as(
"when creating a descendant line, the first element has " "when creating a descendant line, the first element has " + "the current node as its parent")
+ "the current node as its parent").contains(node); .contains(node);
val betaOptional = alpha.findChild(betaData); val betaOptional = alpha.findChild(betaData);
assertThat(betaOptional.isPresent()).as( assertThat(betaOptional.isPresent()).as("when creating a descendant line, the second element is " + "found")
"when creating a descendant line, the second element is " .isTrue();
+ "found").isTrue();
if (betaOptional.isPresent()) { if (betaOptional.isPresent()) {
val beta = betaOptional.get(); val beta = betaOptional.get();
assertThat(beta.getParent()).as( assertThat(beta.getParent()).as(
"when creating a descendant line, the second element " "when creating a descendant line, the second element " + "has the first as its parent")
+ "has the first as its parent")
.contains(alpha); .contains(alpha);
val gammaOptional = beta.findChild(gammaData); val gammaOptional = beta.findChild(gammaData);
assertThat(gammaOptional.isPresent()).as( assertThat(gammaOptional.isPresent()).as(
"when creating a descendant line, the third element " "when creating a descendant line, the third element " + "is found")
+ "is found").isTrue(); .isTrue();
if (gammaOptional.isPresent()) { if (gammaOptional.isPresent()) {
val gamma = gammaOptional.get(); val gamma = gammaOptional.get();
assertThat(gamma.getParent()).as( assertThat(gamma.getParent()).as(
"when creating a descendant line, the third " "when creating a descendant line, the third " + "element has the second as its parent")
+ "element has the second as its parent")
.contains(beta); .contains(beta);
} }
} }
@ -464,9 +459,8 @@ public class NodeItemTest {
//when //when
node.createDescendantLine(Collections.emptyList()); node.createDescendantLine(Collections.emptyList());
//then //then
assertThat(node.getChildren()).as( assertThat(node.getChildren()).as("when creating a descendant line from an empty list, nothing " + "is created")
"when creating a descendant line from an empty list, nothing " .isEmpty();
+ "is created").isEmpty();
} }
/** /**
@ -482,12 +476,11 @@ public class NodeItemTest {
//when //when
val found = node.findChild(childData); val found = node.findChild(childData);
//then //then
assertThat(found.isPresent()).as( assertThat(found.isPresent()).as("when retrieving a child by its data, it is found")
"when retrieving a child by its data, it is found").isTrue(); .isTrue();
if (found.isPresent()) { if (found.isPresent()) {
assertThat(found.get()).as( assertThat(found.get()).as("when retrieving a child by its data, it is the expected " + "node")
"when retrieving a child by its data, it is the expected " .isSameAs(child);
+ "node").isSameAs(child);
} }
} }
@ -517,16 +510,15 @@ public class NodeItemTest {
val child = node.createChild(childData); val child = node.createChild(childData);
//then //then
assertThat(child.getParent()).as( assertThat(child.getParent()).as(
"when creating a child node, the child has the current node " "when creating a child node, the child has the current node " + "as its parent")
+ "as its parent").contains(node); .contains(node);
val foundChild = node.findChild(childData); val foundChild = node.findChild(childData);
assertThat(foundChild.isPresent()).as( assertThat(foundChild.isPresent()).as("when creating a child node, the child can be found by its " + "data")
"when creating a child node, the child can be found by its " .isTrue();
+ "data").isTrue();
if (foundChild.isPresent()) { if (foundChild.isPresent()) {
assertThat(foundChild.get()).as( assertThat(foundChild.get()).as(
"when creating a child node, the correct child can be " "when creating a child node, the correct child can be " + "found by its data")
+ "found by its data").isSameAs(child); .isSameAs(child);
} }
} }
@ -598,14 +590,19 @@ public class NodeItemTest {
//when //when
node.insertInPath(four, "one", "two", "three"); node.insertInPath(four, "one", "two", "three");
//then //then
val three = four.getParent().get(); val three = four.getParent()
assertThat(four.getParent()).as("add node to a tree").isNotNull(); .get();
assertThat(four.getParent()).as("add node to a tree")
.isNotNull();
assertThat(three.getName()).isEqualTo("three"); assertThat(three.getName()).isEqualTo("three");
val two = three.getParent().get(); val two = three.getParent()
.get();
assertThat(two.getName()).isEqualTo("two"); assertThat(two.getName()).isEqualTo("two");
val one = two.getParent().get(); val one = two.getParent()
.get();
assertThat(one.getName()).isEqualTo("one"); assertThat(one.getName()).isEqualTo("one");
assertThat(one.getParent().get()).isSameAs(node); assertThat(one.getParent()
.get()).isSameAs(node);
assertThat(node.getChildByName("one") assertThat(node.getChildByName("one")
.getChildByName("two") .getChildByName("two")
.getChildByName("three") .getChildByName("three")
@ -623,10 +620,11 @@ public class NodeItemTest {
node.insertInPath(child); // as root/child node.insertInPath(child); // as root/child
node.insertInPath(grandchild, "child"); // as root/child/grandchild node.insertInPath(grandchild, "child"); // as root/child/grandchild
//then //then
assertThat(node.getChildByName("child")).as("child").isSameAs(child); assertThat(node.getChildByName("child")).as("child")
assertThat( .isSameAs(child);
node.getChildByName("child").getChildByName("grandchild")).as( assertThat(node.getChildByName("child")
"grandchild").isSameAs(grandchild); .getChildByName("grandchild")).as("grandchild")
.isSameAs(grandchild);
} }
@Test @Test
@ -640,12 +638,12 @@ public class NodeItemTest {
node.insertInPath(grandchild, "child"); node.insertInPath(grandchild, "child");
node.insertInPath(child); node.insertInPath(child);
//then //then
assertThat(node.getChildByName("child").getData()).as("data in tree") assertThat(node.getChildByName("child")
.contains( .getData()).as("data in tree")
"child data"); .contains("child data");
assertThat( assertThat(node.getChildByName("child")
node.getChildByName("child").getChildByName("grandchild")).as( .getChildByName("grandchild")).as("grandchild")
"grandchild").isSameAs(grandchild); .isSameAs(grandchild);
} }
@Test @Test
@ -672,8 +670,7 @@ public class NodeItemTest {
public void placeNodeInTreeWhereNonEmptyNodeWithSameNameExists() { public void placeNodeInTreeWhereNonEmptyNodeWithSameNameExists() {
//given //given
exception.expect(NodeException.class); exception.expect(NodeException.class);
exception.expectMessage( exception.expectMessage("A non-empty node named 'grandchild' already exists here");
"A non-empty node named 'grandchild' already exists here");
node = Nodes.unnamedRoot(null); node = Nodes.unnamedRoot(null);
val child = Nodes.namedChild("child data", "child", node); val child = Nodes.namedChild("child data", "child", node);
Nodes.namedChild("data", "grandchild", child); Nodes.namedChild("data", "grandchild", child);
@ -707,14 +704,16 @@ public class NodeItemTest {
child.addChild(target); child.addChild(target);
val addMe = Nodes.namedRoot("I'm new", "target"); val addMe = Nodes.namedRoot("I'm new", "target");
assertThat(addMe.getParent()).isEmpty(); assertThat(addMe.getParent()).isEmpty();
assertThat(child.getChildByName("target").isEmpty()).as( assertThat(child.getChildByName("target")
"target starts empty").isTrue(); .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.getChildByName("target").getData()).as( assertThat(child.getChildByName("target")
"target now contains data").contains("I'm new"); .getData()).as("target now contains data")
.contains("I'm new");
} }
@Test @Test
@ -772,19 +771,16 @@ public class NodeItemTest {
val bob = Nodes.namedChild("bob data", "bob", node); val bob = Nodes.namedChild("bob data", "bob", node);
val alice = Nodes.namedChild("alice data", "alice", node); val alice = Nodes.namedChild("alice data", "alice", node);
Nodes.namedChild("dave data", "dave", alice); Nodes.namedChild("dave data", "dave", alice);
Nodes.unnamedChild("bob's child's data", Nodes.unnamedChild("bob's child's data", bob); // has no name and no children so no included
bob); // has no name and no children so no included
val kim = Nodes.unnamedChild("kim data", node); // nameless mother val kim = Nodes.unnamedChild("kim data", node); // nameless mother
Nodes.namedChild("lucy data", "lucy", kim); Nodes.namedChild("lucy data", "lucy", kim);
//when //when
val tree = node.drawTree(0); val tree = node.drawTree(0);
//then //then
String[] lines = tree.split("\n"); String[] lines = tree.split("\n");
assertThat(lines).contains("[root]", "[ alice]", "[ dave]", assertThat(lines).contains("[root]", "[ alice]", "[ dave]", "[ (unnamed)]", "[ lucy]", "[ bob]");
"[ (unnamed)]", "[ lucy]", "[ bob]");
assertThat(lines).containsSubsequence("[root]", "[ alice]", "[ dave]"); assertThat(lines).containsSubsequence("[root]", "[ alice]", "[ dave]");
assertThat(lines).containsSubsequence("[root]", "[ (unnamed)]", assertThat(lines).containsSubsequence("[root]", "[ (unnamed)]", "[ lucy]");
"[ lucy]");
assertThat(lines).containsSubsequence("[root]", "[ bob]"); assertThat(lines).containsSubsequence("[root]", "[ bob]");
} }
@ -852,4 +848,28 @@ public class NodeItemTest {
//when //when
node.findChild("data"); node.findChild("data");
} }
@Test
public void canStreamAll() throws Exception {
//given
node = Nodes.namedRoot("root", "root");
val n1 = Nodes.namedChild("one", "one", node);
val n2 = Nodes.namedChild("two", "two", node);
val n3 = Nodes.namedChild("three", "three", n1);
val n4 = Nodes.namedChild("four", "four", n2);
val n5 = Nodes.namedChild("five", "five", n1);
val n6 = Nodes.namedChild("six", "six", n2);
val n7 = Nodes.namedChild("seven", "seven", n5);
val n8 = Nodes.namedChild("eight", "eight", n6);
//when
val result = node.streamAll()
.collect(Collectors.toList());
//then
assertThat(result).as("full tree")
.contains(node, n1, n2, n3, n4, n5, n6, n7, n8);
// and
assertThat(n1.streamAll()
.collect(Collectors.toList())).as("sub-tree")
.containsExactlyInAnyOrder(n1, n3, n5, n7);
}
} }