Merge branch release/0.7.0 into master
Rename streamAll() as stream() Add parentStream() Add isRoot() Rename Optional<Node<T>> getParent() as findParent() Rename Optional<Node<T>> getData() as findData() Add Node<T> getParent() Add<Node<T> getData
This commit is contained in:
commit
5fbbc604bd
12 changed files with 400 additions and 101 deletions
11
CHANGELOG
11
CHANGELOG
|
@ -1,6 +1,17 @@
|
||||||
CHANGELOG
|
CHANGELOG
|
||||||
=========
|
=========
|
||||||
|
|
||||||
|
0.7.0
|
||||||
|
------
|
||||||
|
|
||||||
|
* Rename streamAll() as stream()
|
||||||
|
* Add parentStream()
|
||||||
|
* Add isRoot()
|
||||||
|
* Rename Optional<Node<T>> getParent() as findParent()
|
||||||
|
* Rename Optional<Node<T>> getData() as findData()
|
||||||
|
* Add Node<T> getParent()
|
||||||
|
* Add<Node<T> getData
|
||||||
|
|
||||||
0.6.0
|
0.6.0
|
||||||
------
|
------
|
||||||
|
|
||||||
|
|
4
pom.xml
4
pom.xml
|
@ -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.6.0</version>
|
<version>0.7.0</version>
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
<name>Node</name>
|
<name>Node</name>
|
||||||
|
@ -15,7 +15,7 @@
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<lombok.version>1.16.10</lombok.version>
|
<lombok.version>1.16.12</lombok.version>
|
||||||
<assertj.version>3.6.2</assertj.version>
|
<assertj.version>3.6.2</assertj.version>
|
||||||
<coveralls-maven-plugin.version>4.3.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>
|
||||||
|
|
42
src/main/java/net/kemitix/node/EmptyNodeException.java
Normal file
42
src/main/java/net/kemitix/node/EmptyNodeException.java
Normal file
|
@ -0,0 +1,42 @@
|
||||||
|
/*
|
||||||
|
The MIT License (MIT)
|
||||||
|
|
||||||
|
Copyright (c) 2016 Paul Campbell
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package net.kemitix.node;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Raised when an attempt is made to get the data from an empty node.
|
||||||
|
*
|
||||||
|
* @author Paul Campbell (pcampbell@kemitix.net)
|
||||||
|
*/
|
||||||
|
public class EmptyNodeException extends NodeException {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor with message.
|
||||||
|
*
|
||||||
|
* @param message the message
|
||||||
|
*/
|
||||||
|
public EmptyNodeException(final String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
}
|
|
@ -59,7 +59,14 @@ public interface Node<T> {
|
||||||
*
|
*
|
||||||
* @return an Optional containing the node's data, or empty if the node has none
|
* @return an Optional containing the node's data, or empty if the node has none
|
||||||
*/
|
*/
|
||||||
Optional<T> getData();
|
Optional<T> findData();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fetch the data held within the node.
|
||||||
|
*
|
||||||
|
* @return the node's data, or throws an {@link EmptyNodeException}
|
||||||
|
*/
|
||||||
|
T getData();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set the data held within the node.
|
* Set the data held within the node.
|
||||||
|
@ -75,12 +82,26 @@ public interface Node<T> {
|
||||||
*/
|
*/
|
||||||
boolean isEmpty();
|
boolean isEmpty();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns true is the node is a root node (has no parent).
|
||||||
|
*
|
||||||
|
* @return true is parent is null
|
||||||
|
*/
|
||||||
|
boolean isRoot();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Fetch the parent node.
|
* Fetch the parent node.
|
||||||
*
|
*
|
||||||
* @return an Optional contain the parent node, or empty if a root node
|
* @return an Optional contain the parent node, or empty if a root node
|
||||||
*/
|
*/
|
||||||
Optional<Node<T>> getParent();
|
Optional<Node<T>> findParent();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fetch the parent node.
|
||||||
|
*
|
||||||
|
* @return the parent node, or throws an {@link OrphanedNodeException}
|
||||||
|
*/
|
||||||
|
Node<T> getParent();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Make the current node a direct child of the parent.
|
* Make the current node a direct child of the parent.
|
||||||
|
@ -228,5 +249,12 @@ public interface Node<T> {
|
||||||
*
|
*
|
||||||
* @return a stream of all the nodes in the tree below this node
|
* @return a stream of all the nodes in the tree below this node
|
||||||
*/
|
*/
|
||||||
Stream<Node<T>> streamAll();
|
Stream<Node<T>> stream();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a stream of all the node's ancestor nodes.
|
||||||
|
*
|
||||||
|
* @return a stream of the node's parents recursively until the root node
|
||||||
|
*/
|
||||||
|
Stream<Node<T>> parentStream();
|
||||||
}
|
}
|
||||||
|
|
|
@ -42,7 +42,8 @@ import java.util.stream.Stream;
|
||||||
*
|
*
|
||||||
* @author Paul Campbell (pcampbell@kemitix.net)
|
* @author Paul Campbell (pcampbell@kemitix.net)
|
||||||
*/
|
*/
|
||||||
@ToString(exclude = "children")
|
@ToString(exclude = {"children", "data", "parent"})
|
||||||
|
@SuppressWarnings("methodcount")
|
||||||
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<>();
|
||||||
|
@ -61,9 +62,7 @@ class NodeItem<T> implements Node<T> {
|
||||||
* @param parent the parent of the node, or null for a root node
|
* @param parent the parent of the node, or null for a root node
|
||||||
* @param children the children of the node - must not be null
|
* @param children the children of the node - must not be null
|
||||||
*/
|
*/
|
||||||
NodeItem(
|
NodeItem(final T data, final String name, final Node<T> parent, @NonNull final Set<Node<T>> children) {
|
||||||
final T data, final String name, final Node<T> parent, @NonNull final Set<Node<T>> children
|
|
||||||
) {
|
|
||||||
this.data = data;
|
this.data = data;
|
||||||
this.name = name;
|
this.name = name;
|
||||||
if (parent != null) {
|
if (parent != null) {
|
||||||
|
@ -92,10 +91,18 @@ class NodeItem<T> implements Node<T> {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Optional<T> getData() {
|
public Optional<T> findData() {
|
||||||
return Optional.ofNullable(data);
|
return Optional.ofNullable(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public T getData() {
|
||||||
|
if (isEmpty()) {
|
||||||
|
throw new EmptyNodeException(getName());
|
||||||
|
}
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setData(final T data) {
|
public void setData(final T data) {
|
||||||
this.data = data;
|
this.data = data;
|
||||||
|
@ -107,10 +114,23 @@ class NodeItem<T> implements Node<T> {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Optional<Node<T>> getParent() {
|
public boolean isRoot() {
|
||||||
|
return parent == null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Optional<Node<T>> findParent() {
|
||||||
return Optional.ofNullable(parent);
|
return Optional.ofNullable(parent);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Node<T> getParent() {
|
||||||
|
if (parent == null) {
|
||||||
|
throw new OrphanedNodeException(getName());
|
||||||
|
}
|
||||||
|
return parent;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Make the current node a direct child of the parent.
|
* Make the current node a direct child of the parent.
|
||||||
*
|
*
|
||||||
|
@ -155,9 +175,9 @@ class NodeItem<T> implements Node<T> {
|
||||||
}
|
}
|
||||||
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();
|
if (!child.findParent()
|
||||||
if (!childParent.isPresent() || !childParent.get()
|
.filter(this::equals)
|
||||||
.equals(this)) {
|
.isPresent()) {
|
||||||
child.setParent(this);
|
child.setParent(this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -214,8 +234,8 @@ class NodeItem<T> implements Node<T> {
|
||||||
@Override
|
@Override
|
||||||
public Optional<Node<T>> findChild(@NonNull final T child) {
|
public Optional<Node<T>> findChild(@NonNull final T child) {
|
||||||
return children.stream()
|
return children.stream()
|
||||||
.filter(node -> child.equals(node.getData()
|
.filter(node -> child.equals(node.findData()
|
||||||
.orElseGet(() -> null)))
|
.orElse(null)))
|
||||||
.findFirst();
|
.findFirst();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -283,7 +303,7 @@ class NodeItem<T> implements Node<T> {
|
||||||
val existing = childByName.get();
|
val existing = childByName.get();
|
||||||
if (existing.isEmpty()) {
|
if (existing.isEmpty()) {
|
||||||
// place any data in the new node into the existing empty node
|
// place any data in the new node into the existing empty node
|
||||||
nodeItem.getData()
|
nodeItem.findData()
|
||||||
.ifPresent(existing::setData);
|
.ifPresent(existing::setData);
|
||||||
} else {
|
} else {
|
||||||
throw new NodeException("A non-empty node named '" + nodeItem.getName() + "' already exists here");
|
throw new NodeException("A non-empty node named '" + nodeItem.getName() + "' already exists here");
|
||||||
|
@ -351,8 +371,14 @@ class NodeItem<T> implements Node<T> {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Stream<Node<T>> streamAll() {
|
public Stream<Node<T>> stream() {
|
||||||
return Stream.concat(Stream.of(this), getChildren().stream()
|
return Stream.concat(Stream.of(this), getChildren().stream()
|
||||||
.flatMap(Node::streamAll));
|
.flatMap(Node::stream));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Stream<Node<T>> parentStream() {
|
||||||
|
return findParent().map(node -> Stream.concat(Stream.of(node), node.parentStream()))
|
||||||
|
.orElseGet(Stream::empty);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -101,12 +101,12 @@ public final class Nodes {
|
||||||
* @return the immutable copy of the tree
|
* @return the immutable copy of the tree
|
||||||
*/
|
*/
|
||||||
public static <T> Node<T> asImmutable(final Node<T> root) {
|
public static <T> Node<T> asImmutable(final Node<T> root) {
|
||||||
if (root.getParent()
|
if (root.findParent()
|
||||||
.isPresent()) {
|
.isPresent()) {
|
||||||
throw new IllegalArgumentException("source must be the root node");
|
throw new IllegalArgumentException("source must be the root node");
|
||||||
}
|
}
|
||||||
final Set<Node<T>> children = getImmutableChildren(root);
|
final Set<Node<T>> children = getImmutableChildren(root);
|
||||||
return ImmutableNodeItem.newRoot(root.getData()
|
return ImmutableNodeItem.newRoot(root.findData()
|
||||||
.orElse(null), root.getName(), children);
|
.orElse(null), root.getName(), children);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -120,11 +120,10 @@ public final class Nodes {
|
||||||
private static <T> Node<T> asImmutableChild(
|
private static <T> Node<T> asImmutableChild(
|
||||||
final Node<T> source
|
final Node<T> source
|
||||||
) {
|
) {
|
||||||
return ImmutableNodeItem.newChild(source.getData()
|
return ImmutableNodeItem.newChild(source.findData()
|
||||||
.orElse(null), source.getName(), source.getParent()
|
.orElse(null), source.getName(), source.findParent()
|
||||||
.orElse(null),
|
.orElse(null),
|
||||||
getImmutableChildren(source)
|
getImmutableChildren(source)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
42
src/main/java/net/kemitix/node/OrphanedNodeException.java
Normal file
42
src/main/java/net/kemitix/node/OrphanedNodeException.java
Normal file
|
@ -0,0 +1,42 @@
|
||||||
|
/*
|
||||||
|
The MIT License (MIT)
|
||||||
|
|
||||||
|
Copyright (c) 2016 Paul Campbell
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package net.kemitix.node;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Raised when an attempt is made to access the parent of a root node.
|
||||||
|
*
|
||||||
|
* @author Paul Campbell (pcampbell@kemitix.net)
|
||||||
|
*/
|
||||||
|
public class OrphanedNodeException extends NodeException {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor with message.
|
||||||
|
*
|
||||||
|
* @param message the message
|
||||||
|
*/
|
||||||
|
public OrphanedNodeException(final String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
}
|
29
src/test/java/net/kemitix/node/EmptyNodeExceptionTest.java
Normal file
29
src/test/java/net/kemitix/node/EmptyNodeExceptionTest.java
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
package net.kemitix.node;
|
||||||
|
|
||||||
|
import lombok.val;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import static org.hamcrest.CoreMatchers.is;
|
||||||
|
import static org.junit.Assert.assertThat;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests for {@link EmptyNodeException}.
|
||||||
|
*
|
||||||
|
* @author pcampbell
|
||||||
|
*/
|
||||||
|
public class EmptyNodeExceptionTest {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test that message provided to constructor is returned.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void shouldReturnConstructorMessage() {
|
||||||
|
//given
|
||||||
|
val message = "this is the message";
|
||||||
|
//when
|
||||||
|
val nodeException = new EmptyNodeException(message);
|
||||||
|
//then
|
||||||
|
assertThat(nodeException.getMessage(), is(message));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -74,8 +74,8 @@ public class ImmutableNodeItemTest {
|
||||||
//given
|
//given
|
||||||
immutableNode = Nodes.asImmutable(Nodes.unnamedRoot("data"));
|
immutableNode = Nodes.asImmutable(Nodes.unnamedRoot("data"));
|
||||||
//then
|
//then
|
||||||
assertThat(immutableNode.getParent()).as("immutableNode created without a parent has no parent")
|
assertThat(immutableNode.findParent()).as("immutableNode created without a parent has no parent")
|
||||||
.isEmpty();
|
.isEmpty();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -101,11 +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 = immutableChild.getParent();
|
final Optional<Node<String>> optionalParent = immutableChild.findParent();
|
||||||
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", Optional.of("parent"));
|
.hasFieldOrPropertyWithValue("data", "parent");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -437,14 +437,14 @@ public class ImmutableNodeItemTest {
|
||||||
Nodes.namedChild("eight", "eight", n6);
|
Nodes.namedChild("eight", "eight", n6);
|
||||||
val immutableRoot = Nodes.asImmutable(node);
|
val immutableRoot = Nodes.asImmutable(node);
|
||||||
//when
|
//when
|
||||||
val result = immutableRoot.streamAll()
|
val result = immutableRoot.stream()
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
//then
|
//then
|
||||||
assertThat(result).as("full tree")
|
assertThat(result).as("full tree")
|
||||||
.hasSize(9);
|
.hasSize(9);
|
||||||
// and
|
// and
|
||||||
assertThat(immutableRoot.getChild("one")
|
assertThat(immutableRoot.getChild("one")
|
||||||
.streamAll()
|
.stream()
|
||||||
.collect(Collectors.toList())).as("sub-tree")
|
.collect(Collectors.toList())).as("sub-tree")
|
||||||
.hasSize(4);
|
.hasSize(4);
|
||||||
}
|
}
|
||||||
|
|
|
@ -35,8 +35,79 @@ public class NodeItemTest {
|
||||||
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
|
||||||
|
public void getDataWhenEmptyThrowsException() throws Exception {
|
||||||
|
//given
|
||||||
|
node = Nodes.unnamedRoot(null);
|
||||||
|
assertThat(node.isEmpty()).isTrue();
|
||||||
|
exception.expect(EmptyNodeException.class);
|
||||||
|
//when
|
||||||
|
node.getData();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void findDataWhenFullReturnsData() {
|
||||||
|
//given
|
||||||
|
val data = "data";
|
||||||
|
node = Nodes.unnamedRoot(data);
|
||||||
|
//when
|
||||||
|
val result = node.findData();
|
||||||
|
//then
|
||||||
|
assertThat(result).contains(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void findDataWhenEmptyReturnsEmptyOptional() {
|
||||||
|
//given
|
||||||
|
node = Nodes.unnamedRoot(null);
|
||||||
|
//when
|
||||||
|
val result = node.findData();
|
||||||
|
//then
|
||||||
|
assertThat(result).isEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void getParentWhenRootThrowsException() {
|
||||||
|
//given
|
||||||
|
node = Nodes.unnamedRoot(null);
|
||||||
|
exception.expect(OrphanedNodeException.class);
|
||||||
|
//when
|
||||||
|
node.getParent();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void getParentWhenChildReturnsRoot() {
|
||||||
|
//given
|
||||||
|
val root = Nodes.unnamedRoot("root");
|
||||||
|
node = Nodes.unnamedChild("child", root);
|
||||||
|
//when
|
||||||
|
val result = node.getParent();
|
||||||
|
//then
|
||||||
|
assertThat(result).isSameAs(root);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void findParentWhenRootReturnsEmptyOptional() {
|
||||||
|
//given
|
||||||
|
node = Nodes.unnamedRoot(null);
|
||||||
|
//when
|
||||||
|
val result = node.findParent();
|
||||||
|
//then
|
||||||
|
assertThat(result).isEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void findParentWhenChildReturnsRoot() {
|
||||||
|
//given
|
||||||
|
val root = Nodes.unnamedRoot("root");
|
||||||
|
node = Nodes.unnamedChild("child", root);
|
||||||
|
//when
|
||||||
|
val result = node.findParent();
|
||||||
|
//then
|
||||||
|
assertThat(result).contains(root);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -72,8 +143,8 @@ public class NodeItemTest {
|
||||||
//given
|
//given
|
||||||
node = Nodes.unnamedRoot("data");
|
node = Nodes.unnamedRoot("data");
|
||||||
//then
|
//then
|
||||||
assertThat(node.getParent()).as("node created without a parent has no parent")
|
assertThat(node.findParent()).as("node created without a parent has no parent")
|
||||||
.isEmpty();
|
.isEmpty();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -86,8 +157,8 @@ public class NodeItemTest {
|
||||||
//when
|
//when
|
||||||
node = Nodes.unnamedChild("subject", parent);
|
node = Nodes.unnamedChild("subject", parent);
|
||||||
//then
|
//then
|
||||||
assertThat(node.getParent()).as("node created with a parent can return the parent")
|
assertThat(node.findParent()).as("node created with a parent can return the parent")
|
||||||
.contains(parent);
|
.contains(parent);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -144,8 +215,8 @@ public class NodeItemTest {
|
||||||
//when
|
//when
|
||||||
node.setParent(parent);
|
node.setParent(parent);
|
||||||
//then
|
//then
|
||||||
assertThat(node.getParent()).as("when a node is assigned a new parent that parent can be " + "returned")
|
assertThat(node.findParent()).as("when a node is assigned a new parent that parent can be " + "returned")
|
||||||
.contains(parent);
|
.contains(parent);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -188,8 +259,8 @@ public class NodeItemTest {
|
||||||
//when
|
//when
|
||||||
child.setParent(newParent);
|
child.setParent(newParent);
|
||||||
//then
|
//then
|
||||||
assertThat(child.getParent()).as("when a node is assigned a new parent, the old parent is " + "replaced")
|
assertThat(child.findParent()).as("when a node is assigned a new parent, the old parent is " + "replaced")
|
||||||
.contains(newParent);
|
.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 " + "longer has the node among it's children")
|
"when a node is assigned a new parent, the old parent no " + "longer has the node among it's children")
|
||||||
|
@ -209,9 +280,9 @@ public class NodeItemTest {
|
||||||
//when
|
//when
|
||||||
newParent.addChild(child);
|
newParent.addChild(child);
|
||||||
//then
|
//then
|
||||||
assertThat(child.getParent()).as("when a node with an existing parent is added as a child " +
|
assertThat(child.findParent()).as("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")
|
assertThat(node.findChild("child")
|
||||||
.isPresent()).as("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 " +
|
||||||
|
@ -316,8 +387,8 @@ public class NodeItemTest {
|
||||||
//when
|
//when
|
||||||
node.addChild(child);
|
node.addChild(child);
|
||||||
//then
|
//then
|
||||||
assertThat(child.getParent()).as("when a node is added as a child, the child has the node as " + "its parent")
|
assertThat(child.findParent()).as("when a node is added as a child, the child has the node as " + "its parent")
|
||||||
.contains(node);
|
.contains(node);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -338,10 +409,9 @@ public class NodeItemTest {
|
||||||
//then
|
//then
|
||||||
assertThat(result.isPresent()).as("when we walk the tree to a node it is found")
|
assertThat(result.isPresent()).as("when we walk the tree to a node it is found")
|
||||||
.isTrue();
|
.isTrue();
|
||||||
if (result.isPresent()) {
|
result.ifPresent(
|
||||||
assertThat(result.get()).as("when we walk the tree to a node the correct node is found")
|
stringNode -> assertThat(stringNode).as("when we walk the tree to a node the correct node is found")
|
||||||
.isSameAs(node);
|
.isSameAs(node));
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -408,31 +478,27 @@ public class NodeItemTest {
|
||||||
val alphaOptional = node.findChild(alphaData);
|
val alphaOptional = node.findChild(alphaData);
|
||||||
assertThat(alphaOptional.isPresent()).as("when creating a descendant line, the first element is found")
|
assertThat(alphaOptional.isPresent()).as("when creating a descendant line, the first element is found")
|
||||||
.isTrue();
|
.isTrue();
|
||||||
if (alphaOptional.isPresent()) {
|
alphaOptional.ifPresent(alpha -> {
|
||||||
val alpha = alphaOptional.get();
|
assertThat(alpha.findParent()).as(
|
||||||
assertThat(alpha.getParent()).as(
|
"when creating a descendant line, the first element has the current node as its parent")
|
||||||
"when creating a descendant line, the first element has " + "the current node as its parent")
|
.contains(node);
|
||||||
.contains(node);
|
|
||||||
val betaOptional = alpha.findChild(betaData);
|
val betaOptional = alpha.findChild(betaData);
|
||||||
assertThat(betaOptional.isPresent()).as("when creating a descendant line, the second element is " + "found")
|
assertThat(betaOptional.isPresent()).as("when creating a descendant line, the second element is found")
|
||||||
.isTrue();
|
.isTrue();
|
||||||
if (betaOptional.isPresent()) {
|
betaOptional.ifPresent(beta -> {
|
||||||
val beta = betaOptional.get();
|
assertThat(beta.findParent()).as(
|
||||||
assertThat(beta.getParent()).as(
|
"when creating a descendant line, the second element has the first as its parent")
|
||||||
"when creating a descendant line, the second element " + "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 is found")
|
||||||
"when creating a descendant line, the third element " + "is found")
|
|
||||||
.isTrue();
|
.isTrue();
|
||||||
if (gammaOptional.isPresent()) {
|
gammaOptional.ifPresent(gamma -> {
|
||||||
val gamma = gammaOptional.get();
|
assertThat(gamma.findParent()).as(
|
||||||
assertThat(gamma.getParent()).as(
|
"when creating a descendant line, the third element has the second as its parent")
|
||||||
"when creating a descendant line, the third " + "element has the second as its parent")
|
.contains(beta);
|
||||||
.contains(beta);
|
});
|
||||||
}
|
});
|
||||||
}
|
});
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -478,10 +544,9 @@ public class NodeItemTest {
|
||||||
//then
|
//then
|
||||||
assertThat(found.isPresent()).as("when retrieving a child by its data, it is found")
|
assertThat(found.isPresent()).as("when retrieving a child by its data, it is found")
|
||||||
.isTrue();
|
.isTrue();
|
||||||
if (found.isPresent()) {
|
found.ifPresent(
|
||||||
assertThat(found.get()).as("when retrieving a child by its data, it is the expected " + "node")
|
stringNode -> assertThat(stringNode).as("when retrieving a child by its data, it is the expected node")
|
||||||
.isSameAs(child);
|
.isSameAs(child));
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -509,17 +574,14 @@ public class NodeItemTest {
|
||||||
//when
|
//when
|
||||||
val child = node.createChild(childData);
|
val child = node.createChild(childData);
|
||||||
//then
|
//then
|
||||||
assertThat(child.getParent()).as(
|
assertThat(child.findParent()).as("when creating a child node, the child has the current node as its parent")
|
||||||
"when creating a child node, the child has the current node " + "as its parent")
|
.contains(node);
|
||||||
.contains(node);
|
|
||||||
val foundChild = node.findChild(childData);
|
val foundChild = node.findChild(childData);
|
||||||
assertThat(foundChild.isPresent()).as("when creating a child node, the child can be found by its " + "data")
|
assertThat(foundChild.isPresent()).as("when creating a child node, the child can be found by its data")
|
||||||
.isTrue();
|
.isTrue();
|
||||||
if (foundChild.isPresent()) {
|
foundChild.ifPresent(stringNode -> assertThat(stringNode).as(
|
||||||
assertThat(foundChild.get()).as(
|
"when creating a child node, the correct child can be found by its data")
|
||||||
"when creating a child node, the correct child can be " + "found by its data")
|
.isSameAs(child));
|
||||||
.isSameAs(child);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -538,7 +600,7 @@ public class NodeItemTest {
|
||||||
@Test
|
@Test
|
||||||
public void canCreateRootNodeWithoutData() {
|
public void canCreateRootNodeWithoutData() {
|
||||||
node = Nodes.namedRoot(null, "empty");
|
node = Nodes.namedRoot(null, "empty");
|
||||||
assertThat(node.getData()).isEmpty();
|
assertThat(node.findData()).isEmpty();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -590,19 +652,15 @@ public class NodeItemTest {
|
||||||
//when
|
//when
|
||||||
node.insertInPath(four, "one", "two", "three");
|
node.insertInPath(four, "one", "two", "three");
|
||||||
//then
|
//then
|
||||||
val three = four.getParent()
|
val three = four.getParent();
|
||||||
.get();
|
|
||||||
assertThat(four.getParent()).as("add node to a tree")
|
assertThat(four.getParent()).as("add node to a tree")
|
||||||
.isNotNull();
|
.isNotNull();
|
||||||
assertThat(three.getName()).isEqualTo("three");
|
assertThat(three.getName()).isEqualTo("three");
|
||||||
val two = three.getParent()
|
val two = three.getParent();
|
||||||
.get();
|
|
||||||
assertThat(two.getName()).isEqualTo("two");
|
assertThat(two.getName()).isEqualTo("two");
|
||||||
val one = two.getParent()
|
val one = two.getParent();
|
||||||
.get();
|
|
||||||
assertThat(one.getName()).isEqualTo("one");
|
assertThat(one.getName()).isEqualTo("one");
|
||||||
assertThat(one.getParent()
|
assertThat(one.getParent()).isSameAs(node);
|
||||||
.get()).isSameAs(node);
|
|
||||||
assertThat(node.getChildByName("one")
|
assertThat(node.getChildByName("one")
|
||||||
.getChildByName("two")
|
.getChildByName("two")
|
||||||
.getChildByName("three")
|
.getChildByName("three")
|
||||||
|
@ -662,7 +720,7 @@ public class NodeItemTest {
|
||||||
//when
|
//when
|
||||||
child.removeParent();
|
child.removeParent();
|
||||||
//then
|
//then
|
||||||
assertThat(child.getParent()).isEmpty();
|
assertThat(child.findParent()).isEmpty();
|
||||||
assertThat(node.getChildren()).isEmpty();
|
assertThat(node.getChildren()).isEmpty();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -703,7 +761,7 @@ public class NodeItemTest {
|
||||||
node.addChild(child);
|
node.addChild(child);
|
||||||
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.findParent()).isEmpty();
|
||||||
assertThat(child.getChildByName("target")
|
assertThat(child.getChildByName("target")
|
||||||
.isEmpty()).as("target starts empty")
|
.isEmpty()).as("target starts empty")
|
||||||
.isTrue();
|
.isTrue();
|
||||||
|
@ -761,7 +819,7 @@ public class NodeItemTest {
|
||||||
node.removeChild(child);
|
node.removeChild(child);
|
||||||
//then
|
//then
|
||||||
assertThat(node.getChildren()).isEmpty();
|
assertThat(node.getChildren()).isEmpty();
|
||||||
assertThat(child.getParent()).isEmpty();
|
assertThat(child.findParent()).isEmpty();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -803,7 +861,7 @@ public class NodeItemTest {
|
||||||
Node<String> child = node.createChild("child data", "child name");
|
Node<String> child = node.createChild("child data", "child name");
|
||||||
//then
|
//then
|
||||||
assertThat(child.getName()).isEqualTo("child name");
|
assertThat(child.getName()).isEqualTo("child name");
|
||||||
assertThat(child.getParent()).contains(node);
|
assertThat(child.findParent()).contains(node);
|
||||||
assertThat(node.getChildren()).containsExactly(child);
|
assertThat(node.getChildren()).containsExactly(child);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -836,7 +894,7 @@ public class NodeItemTest {
|
||||||
//when
|
//when
|
||||||
val child = Nodes.unnamedChild("child data", node);
|
val child = Nodes.unnamedChild("child data", node);
|
||||||
//then
|
//then
|
||||||
assertThat(child.getParent()).contains(node);
|
assertThat(child.findParent()).contains(node);
|
||||||
assertThat(node.getChildren()).containsExactly(child);
|
assertThat(node.getChildren()).containsExactly(child);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -862,14 +920,49 @@ public class NodeItemTest {
|
||||||
val n7 = Nodes.namedChild("seven", "seven", n5);
|
val n7 = Nodes.namedChild("seven", "seven", n5);
|
||||||
val n8 = Nodes.namedChild("eight", "eight", n6);
|
val n8 = Nodes.namedChild("eight", "eight", n6);
|
||||||
//when
|
//when
|
||||||
val result = node.streamAll()
|
val result = node.stream()
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
//then
|
//then
|
||||||
assertThat(result).as("full tree")
|
assertThat(result).as("full tree")
|
||||||
.contains(node, n1, n2, n3, n4, n5, n6, n7, n8);
|
.contains(node, n1, n2, n3, n4, n5, n6, n7, n8);
|
||||||
// and
|
// and
|
||||||
assertThat(n1.streamAll()
|
assertThat(n1.stream()
|
||||||
.collect(Collectors.toList())).as("sub-tree")
|
.collect(Collectors.toList())).as("sub-tree")
|
||||||
.containsExactlyInAnyOrder(n1, n3, n5, n7);
|
.containsExactlyInAnyOrder(n1, n3, n5, n7);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void isRootWhenRootIsTrue() {
|
||||||
|
assertThat(Nodes.unnamedRoot(null)
|
||||||
|
.isRoot()).isTrue();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void isRootWhenNotRootIsFalse() {
|
||||||
|
//given
|
||||||
|
val root = Nodes.unnamedRoot(null);
|
||||||
|
//then
|
||||||
|
assertThat(Nodes.unnamedChild(null, root)
|
||||||
|
.isRoot()).isFalse();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void parentStream() {
|
||||||
|
//given
|
||||||
|
val root = Nodes.namedRoot("root data", "root");
|
||||||
|
val child1 = Nodes.namedChild("child 1 data", "child 1", root);
|
||||||
|
val child2 = Nodes.namedChild("child 2 data", "child 2", root);
|
||||||
|
val child3 = Nodes.namedChild("child 3 data", "child 3", child2);
|
||||||
|
//when
|
||||||
|
val resultRoot = root.parentStream()
|
||||||
|
.collect(Collectors.toSet());
|
||||||
|
val resultChild1 = child1.parentStream()
|
||||||
|
.collect(Collectors.toSet());
|
||||||
|
val resultChild3 = child3.parentStream()
|
||||||
|
.collect(Collectors.toSet());
|
||||||
|
//then
|
||||||
|
assertThat(resultRoot).isEmpty();
|
||||||
|
assertThat(resultChild1).containsExactlyInAnyOrder(root);
|
||||||
|
assertThat(resultChild3).containsExactlyInAnyOrder(child2, root);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -44,7 +44,7 @@ public class NodesTest {
|
||||||
SoftAssertions softly = new SoftAssertions();
|
SoftAssertions softly = new SoftAssertions();
|
||||||
softly.assertThat(node.getData()).contains("data");
|
softly.assertThat(node.getData()).contains("data");
|
||||||
softly.assertThat(node.getName()).isEmpty();
|
softly.assertThat(node.getName()).isEmpty();
|
||||||
softly.assertThat(node.getParent()).contains(parent);
|
softly.assertThat(node.findParent()).contains(parent);
|
||||||
softly.assertAll();
|
softly.assertAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -55,7 +55,7 @@ public class NodesTest {
|
||||||
SoftAssertions softly = new SoftAssertions();
|
SoftAssertions softly = new SoftAssertions();
|
||||||
softly.assertThat(node.getData()).contains("data");
|
softly.assertThat(node.getData()).contains("data");
|
||||||
softly.assertThat(node.getName()).isEqualTo("child");
|
softly.assertThat(node.getName()).isEqualTo("child");
|
||||||
softly.assertThat(node.getParent()).contains(parent);
|
softly.assertThat(node.findParent()).contains(parent);
|
||||||
softly.assertAll();
|
softly.assertAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,29 @@
|
||||||
|
package net.kemitix.node;
|
||||||
|
|
||||||
|
import lombok.val;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import static org.hamcrest.CoreMatchers.is;
|
||||||
|
import static org.junit.Assert.assertThat;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests for {@link OrphanedNodeException}.
|
||||||
|
*
|
||||||
|
* @author pcampbell
|
||||||
|
*/
|
||||||
|
public class OrphanedNodeExceptionTest {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test that message provided to constructor is returned.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void shouldReturnConstructorMessage() {
|
||||||
|
//given
|
||||||
|
val message = "this is the message";
|
||||||
|
//when
|
||||||
|
val nodeException = new OrphanedNodeException(message);
|
||||||
|
//then
|
||||||
|
assertThat(nodeException.getMessage(), is(message));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in a new issue