From 51e8194db76037a10619f3c493b1064ea1bd75f7 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Sun, 21 Aug 2016 11:37:15 +0100 Subject: [PATCH] Nodes: add utility class to create Node instances --- src/main/java/net/kemitix/node/Nodes.java | 66 +++++++++++++++++++ src/test/java/net/kemitix/node/NodesTest.java | 62 +++++++++++++++++ 2 files changed, 128 insertions(+) create mode 100644 src/main/java/net/kemitix/node/Nodes.java create mode 100644 src/test/java/net/kemitix/node/NodesTest.java diff --git a/src/main/java/net/kemitix/node/Nodes.java b/src/main/java/net/kemitix/node/Nodes.java new file mode 100644 index 0000000..24dc50e --- /dev/null +++ b/src/main/java/net/kemitix/node/Nodes.java @@ -0,0 +1,66 @@ +package net.kemitix.node; + +/** + * Utility class for {@link Node} items. + * + * @author pcampbell + */ +public final class Nodes { + + private Nodes() { + } + + /** + * Creates a new unnamed root node. + * + * @param data the data the node will contain + * @param the type of the data + * + * @return the new node + */ + public static Node unnamedRoot(final T data) { + return new NodeItem<>(data); + } + + /** + * Creates a new named root node. + * + * @param data the data the node will contain + * @param name the name of the node + * @param the type of the data + * + * @return the new node + */ + public static Node namedRoot(final T data, final String name) { + return new NodeItem<>(data, name); + } + + /** + * Creates a new unnamed child node. + * + * @param data the data the node will contain + * @param parent the parent of the node + * @param the type of the data + * + * @return the new node + */ + public static Node unnamedChild(final T data, final Node parent) { + return new NodeItem<>(data, parent); + } + + /** + * Creates a new named child node. + * + * @param data the data the node will contain + * @param name the name of the node + * @param parent the parent of the node + * @param the type of the data + * + * @return the new node + */ + public static Node namedChild( + final T data, final String name, final Node parent) { + return new NodeItem<>(data, name, parent); + } + +} diff --git a/src/test/java/net/kemitix/node/NodesTest.java b/src/test/java/net/kemitix/node/NodesTest.java new file mode 100644 index 0000000..ace4f79 --- /dev/null +++ b/src/test/java/net/kemitix/node/NodesTest.java @@ -0,0 +1,62 @@ +package net.kemitix.node; + +import lombok.val; +import org.assertj.core.api.SoftAssertions; +import org.junit.Test; + +import static net.trajano.commons.testing.UtilityClassTestUtil + .assertUtilityClassWellDefined; + +/** + * Tests for {@link Nodes}. + * + * @author pcampbell + */ +public class NodesTest { + + @Test + public void shouldBeValidUtilityClass() throws Exception { + assertUtilityClassWellDefined(Nodes.class); + } + + @Test + public void shouldCreateUnnamedRoot() throws Exception { + val node = Nodes.unnamedRoot("data"); + SoftAssertions softly = new SoftAssertions(); + softly.assertThat(node.getData()).contains("data"); + softly.assertThat(node.getName()).isNull(); + softly.assertAll(); + } + + @Test + public void shouldCreateNamedRoot() throws Exception { + val node = Nodes.namedRoot("data", "name"); + SoftAssertions softly = new SoftAssertions(); + softly.assertThat(node.getData()).contains("data"); + softly.assertThat(node.getName()).isEqualTo("name"); + softly.assertAll(); + } + + @Test + public void shouldCreateUnnamedChild() throws Exception { + val parent = Nodes.unnamedRoot("root"); + val node = Nodes.unnamedChild("data", parent); + SoftAssertions softly = new SoftAssertions(); + softly.assertThat(node.getData()).contains("data"); + softly.assertThat(node.getName()).isNull(); + softly.assertThat(node.getParent()).contains(parent); + softly.assertAll(); + } + + @Test + public void shouldCreateNamedChild() throws Exception { + val parent = Nodes.unnamedRoot("root"); + val node = Nodes.namedChild("data", "child", parent); + SoftAssertions softly = new SoftAssertions(); + softly.assertThat(node.getData()).contains("data"); + softly.assertThat(node.getName()).isEqualTo("child"); + softly.assertThat(node.getParent()).contains(parent); + softly.assertAll(); + } + +}