From fee38dc9299985b21c58aa05d4cb0da19a4e9862 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Mon, 14 Mar 2016 14:35:57 +0000 Subject: [PATCH 01/10] .idea: add IntelliJ configuration Signed-off-by: Paul Campbell --- .gitignore | 3 ++ .idea/codeStyleSettings.xml | 104 ++++++++++++++++++++++++++++++++++++ .idea/compiler.xml | 33 ++++++++++++ .idea/encodings.xml | 7 +++ .idea/misc.xml | 39 ++++++++++++++ .idea/modules.xml | 8 +++ .idea/vcs.xml | 6 +++ node.iml | 17 ++++++ 8 files changed, 217 insertions(+) create mode 100644 .idea/codeStyleSettings.xml create mode 100644 .idea/compiler.xml create mode 100644 .idea/encodings.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 node.iml diff --git a/.gitignore b/.gitignore index 5ec989b..25a8515 100644 --- a/.gitignore +++ b/.gitignore @@ -12,3 +12,6 @@ # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml hs_err_pid* + +/.idea/libraries/ +/.idea/workspace.xml diff --git a/.idea/codeStyleSettings.xml b/.idea/codeStyleSettings.xml new file mode 100644 index 0000000..bcea045 --- /dev/null +++ b/.idea/codeStyleSettings.xml @@ -0,0 +1,104 @@ + + + + + + \ No newline at end of file diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000..6dda909 --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,33 @@ + + + + + \ No newline at end of file diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..c0bce70 --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..5c910ae --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,39 @@ + + + + + + + + + + + + + + + + + + + + + + + 1.8 + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..935c7b9 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/node.iml b/node.iml new file mode 100644 index 0000000..6ad1e66 --- /dev/null +++ b/node.iml @@ -0,0 +1,17 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file From d0c6b07b66dfbcab49eb855775e8f0951e17f22c Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Mon, 14 Mar 2016 15:39:16 +0000 Subject: [PATCH 02/10] .idea - fixup --- .idea/inspectionProfiles/Project_Default.xml | 59 +++++++++++++++++++ .../inspectionProfiles/profiles_settings.xml | 7 +++ .idea/misc.xml | 3 + 3 files changed, 69 insertions(+) create mode 100644 .idea/inspectionProfiles/Project_Default.xml create mode 100644 .idea/inspectionProfiles/profiles_settings.xml diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..7df71eb --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,59 @@ + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000..3b31283 --- /dev/null +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,7 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml index 5c910ae..0abd9e0 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,5 +1,8 @@ + + + diff --git a/pom.xml b/pom.xml index 01cc354..32577de 100644 --- a/pom.xml +++ b/pom.xml @@ -1,7 +1,6 @@ 4.0.0 - net.kemitix node 0.2.0-SNAPSHOT jar @@ -31,6 +30,12 @@ 2016 + + org.projectlombok + lombok + 1.16.8 + test + junit junit From 79170b898a48bd6230752714f50c78ccbcf2f5e1 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Mon, 14 Mar 2016 15:40:51 +0000 Subject: [PATCH 04/10] NodeItem: initialise children set in field and make final Signed-off-by: Paul Campbell --- src/main/java/net/kemitix/node/NodeItem.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/main/java/net/kemitix/node/NodeItem.java b/src/main/java/net/kemitix/node/NodeItem.java index 0751a3e..13efcb5 100644 --- a/src/main/java/net/kemitix/node/NodeItem.java +++ b/src/main/java/net/kemitix/node/NodeItem.java @@ -18,7 +18,7 @@ public class NodeItem implements Node { private Node parent; - private Set> children; + private final Set> children = new HashSet<>(); /** * Creates a root node. @@ -43,7 +43,6 @@ public class NodeItem implements Node { if (parent != null) { setParent(parent); } - this.children = new HashSet<>(); } @Override From d60f6ae0bbca06e12848eed7225d89684b515fc3 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Mon, 14 Mar 2016 15:41:23 +0000 Subject: [PATCH 05/10] NodeItem.isChildOf(): simplify if statements Signed-off-by: Paul Campbell --- src/main/java/net/kemitix/node/NodeItem.java | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/src/main/java/net/kemitix/node/NodeItem.java b/src/main/java/net/kemitix/node/NodeItem.java index 13efcb5..5321674 100644 --- a/src/main/java/net/kemitix/node/NodeItem.java +++ b/src/main/java/net/kemitix/node/NodeItem.java @@ -108,13 +108,8 @@ public class NodeItem implements Node { */ @Override public boolean isChildOf(final Node node) { - if (node.equals(parent)) { - return true; - } - if (parent != null) { - return parent.isChildOf(node); - } - return false; + return parent != null && (node.equals(parent) || parent.isChildOf( + node)); } /** From d9d19a849e448632cd2cae6d04f499263e05e89d Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Mon, 14 Mar 2016 15:42:07 +0000 Subject: [PATCH 06/10] NodeItem.findOrCreateChild(): use Optional.orElseGet() to simplify Signed-off-by: Paul Campbell --- src/main/java/net/kemitix/node/NodeItem.java | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/main/java/net/kemitix/node/NodeItem.java b/src/main/java/net/kemitix/node/NodeItem.java index 5321674..3e4186e 100644 --- a/src/main/java/net/kemitix/node/NodeItem.java +++ b/src/main/java/net/kemitix/node/NodeItem.java @@ -166,12 +166,7 @@ public class NodeItem implements Node { if (child == null) { throw new NullPointerException("child"); } - Optional> found = getChild(child); - if (found.isPresent()) { - return found.get(); - } else { - return createChild(child); - } + return getChild(child).orElseGet(() -> createChild(child)); } /** From 9daebcea464c0348aa03e9302f63cd7a02f3d9da Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Mon, 14 Mar 2016 15:42:53 +0000 Subject: [PATCH 07/10] {Node,NodeItem}: javadoc tidy Signed-off-by: Paul Campbell --- src/main/java/net/kemitix/node/Node.java | 4 ++-- src/main/java/net/kemitix/node/NodeItem.java | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main/java/net/kemitix/node/Node.java b/src/main/java/net/kemitix/node/Node.java index 2dae709..002bead 100644 --- a/src/main/java/net/kemitix/node/Node.java +++ b/src/main/java/net/kemitix/node/Node.java @@ -7,8 +7,9 @@ import java.util.Set; /** * An interface for tree node items. * - * @author pcampbell * @param the type of data held in each node + * + * @author pcampbell */ public interface Node { @@ -21,7 +22,6 @@ public interface Node { /** * Fetch the parent node. - * *

* If the node is a root node, i.e. has no parent, then this will return * null. diff --git a/src/main/java/net/kemitix/node/NodeItem.java b/src/main/java/net/kemitix/node/NodeItem.java index 3e4186e..27ee22a 100644 --- a/src/main/java/net/kemitix/node/NodeItem.java +++ b/src/main/java/net/kemitix/node/NodeItem.java @@ -116,6 +116,7 @@ public class NodeItem implements Node { * Walks the node tree using the path to select each child. * * @param path the path to the desired child + * * @return the child or null */ @Override From 58363e474d5574f2f0a4585cfb6754e95f9a621a Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Mon, 14 Mar 2016 15:43:21 +0000 Subject: [PATCH 08/10] NodeItem: apply code style Signed-off-by: Paul Campbell --- src/main/java/net/kemitix/node/NodeItem.java | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/main/java/net/kemitix/node/NodeItem.java b/src/main/java/net/kemitix/node/NodeItem.java index 27ee22a..d11ff23 100644 --- a/src/main/java/net/kemitix/node/NodeItem.java +++ b/src/main/java/net/kemitix/node/NodeItem.java @@ -148,9 +148,8 @@ public class NodeItem implements Node { throw new NullPointerException("descendants"); } if (!descendants.isEmpty()) { - findOrCreateChild(descendants.get(0)) - .createDescendantLine( - descendants.subList(1, descendants.size())); + findOrCreateChild(descendants.get(0)).createDescendantLine( + descendants.subList(1, descendants.size())); } } @@ -183,8 +182,8 @@ public class NodeItem implements Node { throw new NullPointerException("child"); } return children.stream() - .filter((Node t) -> t.getData().equals(child)) - .findAny(); + .filter((Node t) -> t.getData().equals(child)) + .findAny(); } /** From 24ad03ccabca73b671a775491b444a26b357388b Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Mon, 14 Mar 2016 15:43:56 +0000 Subject: [PATCH 09/10] NodeExceptionTest: use lombok's val Signed-off-by: Paul Campbell --- .../java/net/kemitix/node/NodeExceptionTest.java | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/src/test/java/net/kemitix/node/NodeExceptionTest.java b/src/test/java/net/kemitix/node/NodeExceptionTest.java index 5e9f7ac..2d1422b 100644 --- a/src/test/java/net/kemitix/node/NodeExceptionTest.java +++ b/src/test/java/net/kemitix/node/NodeExceptionTest.java @@ -1,7 +1,6 @@ package net.kemitix.node; -import net.kemitix.node.NodeException; - +import lombok.val; import org.junit.Test; import static org.hamcrest.CoreMatchers.is; @@ -14,20 +13,15 @@ import static org.junit.Assert.assertThat; */ public class NodeExceptionTest { - /** - * Class under test. - */ - private NodeException nodeException; - /** * Test that message provided to constructor is returned. */ @Test public void shouldReturnConstructorMessage() { //given - final String message = "this is the message"; + val message = "this is the message"; //when - nodeException = new NodeException(message); + val nodeException = new NodeException(message); //then assertThat(nodeException.getMessage(), is(message)); } From 8187a28795c6ada0643c6d63121dd2d1ee5bf675 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Mon, 14 Mar 2016 15:51:44 +0000 Subject: [PATCH 10/10] NodeItemTest: use val and Assert.assertThat() * Lombok's val simplfies the local variable declarations * Assert.assertThat() gives better error messages * javadoc tidy Signed-off-by: Paul Campbell --- .../java/net/kemitix/node/NodeItemTest.java | 346 ++++++++++-------- 1 file changed, 195 insertions(+), 151 deletions(-) diff --git a/src/test/java/net/kemitix/node/NodeItemTest.java b/src/test/java/net/kemitix/node/NodeItemTest.java index c271ac3..e22605f 100644 --- a/src/test/java/net/kemitix/node/NodeItemTest.java +++ b/src/test/java/net/kemitix/node/NodeItemTest.java @@ -1,18 +1,17 @@ package net.kemitix.node; +import lombok.val; +import org.junit.Assert; import org.junit.Test; +import static org.hamcrest.CoreMatchers.hasItem; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.nullValue; + import java.util.Arrays; import java.util.Collections; import java.util.Optional; -import static org.hamcrest.CoreMatchers.hasItem; -import static org.hamcrest.CoreMatchers.is; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.assertTrue; - /** * Test for {@link NodeItem}. * @@ -26,22 +25,21 @@ public class NodeItemTest { private Node node; /** - * Test {@link NodeItem#Node(java.lang.Object) } that node data is - * recoverable. + * Test that node data is recoverable. */ @Test public void shouldReturnNodeData() { //given - final String data = "this node data"; + val data = "this node data"; //when node = new NodeItem<>(data); //then - assertThat(node.getData(), is(data)); + Assert.assertThat("can get the data from a node", node.getData(), + is(data)); } /** - * Test {@link NodeItem#Node(java.lang.Object) } that passing null as node - * data throws exception. + * Test that passing null as node data throws exception. */ @Test(expected = NullPointerException.class) public void shouldThrowNPEWhenDataIsNull() { @@ -50,78 +48,79 @@ public class NodeItemTest { } /** - * Test {@link NodeItem#Node(java.lang.Object) } that default node parent is - * null. + * Test that default node parent is null. */ @Test - public void shouldHaveNullForDefaulParent() { + public void shouldHaveNullForDefaultParent() { //given node = new NodeItem<>("data"); //then - assertNull(node.getParent()); + Assert.assertThat("node created without a parent has null as parent", + node.getParent(), nullValue()); } /** - * Test {@link NodeItem#Node(java.lang.Object, net.kemitix.node.Node) } that - * provided node parent is returned. + * Test that provided node parent is returned. */ @Test public void shouldReturnNodeParent() { //given - Node parent = new NodeItem<>("parent"); + val parent = new NodeItem("parent"); //when node = new NodeItem<>("subject", parent); //then - assertThat(node.getParent(), is(parent)); + Assert.assertThat("node created with a parent can return the parent", + node.getParent(), is(parent)); } /** - * Test {@link NodeItem#Node(java.lang.Object, net.kemitix.node.Node) } that - * setting the parent on a node where the proposed parent is a child of the - * node throws an exception. + * Test that setting the parent on a node where the proposed parent is a + * child of the node throws an exception. */ @Test(expected = NodeException.class) public void shouldThrowNEWhenSettingParentToAChild() { //given node = new NodeItem<>("subject"); - Node child = new NodeItem<>("child", node); + val child = new NodeItem("child", node); //when node.setParent(child); } /** - * Test {@link NodeItem#Node(java.lang.Object, net.kemitix.node.Node) } that - * when parent is added to created node, the created node is now a child of - * the parent. + * Test that when parent is added to created node, the created node is now a + * child of the parent. */ @Test public void shouldAddNewNodeAsChildToParent() { //given - Node parent = new NodeItem<>("parent"); + val parent = new NodeItem("parent"); //when node = new NodeItem<>("subject", parent); //then - assertThat(parent.getChildren(), hasItem(node)); + Assert.assertThat( + "when a node is created with a parent, the parent has the new" + + " node among it's children", parent.getChildren(), + hasItem(node)); } /** - * Test {@link NodeItem#setParent(net.kemitix.node.Node) } that we return - * the same parent when set. + * Test that we return the same parent when set. */ @Test public void shouldReturnSetParent() { //given node = new NodeItem<>("subject"); - Node parent = new NodeItem<>("parent"); + val parent = new NodeItem("parent"); //when node.setParent(parent); //then - assertThat(node.getParent(), is(parent)); + Assert.assertThat( + "when a node is assigned a new parent that parent can be " + + "returned", node.getParent(), is(parent)); } /** - * Test {@link NodeItem#setParent(net.kemitix.node.Node) } that we throw an - * exception when passed null. + * Test that we throw an exception when passed null. */ @Test(expected = NullPointerException.class) public void shouldThrowNPEWhenSetParentNull() { @@ -132,8 +131,8 @@ public class NodeItemTest { } /** - * Test {@link NodeItem#setParent(net.kemitix.node.Node) } that we throw an - * exceptions when attempting to node as its own parent. + * Test that we throw an exceptions when attempting to node as its own + * parent. */ @Test(expected = NodeException.class) public void shouldThrowNEWhenSetParentSelf() { @@ -144,44 +143,53 @@ public class NodeItemTest { } /** - * Test {@link NodeItem#setParent(net.kemitix.node.Node) } that when a node - * with an existing parent is assigned a new parent, that the old parent no - * longer sees it as one of its children. + * Test that when a node with an existing parent is assigned a new parent, + * that the old parent no longer sees it as one of its children. */ @Test public void shouldUpdateOldParentWhenNodeSetToNewParent() { //given node = new NodeItem<>("subject"); - Node child = node.createChild("child"); - Node newParent = new NodeItem<>("newParent"); + val child = node.createChild("child"); + val newParent = new NodeItem("newParent"); //when child.setParent(newParent); //then - assertThat(child.getParent(), is(newParent)); - assertFalse(node.getChild("child").isPresent()); + Assert.assertThat( + "when a node is assigned a new parent, the old parent is " + + "replaced", child.getParent(), is(newParent)); + Assert.assertThat( + "when a node is assigned a new parent, the old parent no " + + "longer has the node among it's children", + node.getChild("child").isPresent(), is(false)); } /** - * Test {@link NodeItem#addChild(net.kemitix.node.Node) } that when a node - * is added as a child to another node, that it's previous parent no longer - * has it as a child. + * Test that when a node is added as a child to another node, that it's + * previous parent no longer has it as a child. */ @Test public void shouldRemoveNodeFromOldParentWhenAddedAsChildToNewParent() { //given node = new NodeItem<>("subject"); - Node child = node.createChild("child"); - Node newParent = new NodeItem<>("newParent"); + val child = node.createChild("child"); + val newParent = new NodeItem("newParent"); //when newParent.addChild(child); //then - assertThat(child.getParent(), is(newParent)); - assertFalse(node.getChild("child").isPresent()); + Assert.assertThat( + "when a node with an existing parent is added as a child " + + "to another node, then the old parent is replaced", + child.getParent(), is(newParent)); + Assert.assertThat( + "when a node with an existing parent is added as a child to " + + "another node, then the old parent no longer has " + + "the node among it's children", + node.getChild("child").isPresent(), is(false)); } /** - * Test {@link NodeItem#addChild(net.kemitix.node.Node) } that adding null - * as a child throws an exception. + * Test that adding null as a child throws an exception. */ @Test(expected = NullPointerException.class) public void shouldThrowNPEWhenAddingNullAsChild() { @@ -192,23 +200,23 @@ public class NodeItemTest { } /** - * Test {@link NodeItem#addChild(net.kemitix.node.Node) } that adding a - * child is returned. + * Test that adding a child is returned. */ @Test public void shouldReturnAddedChild() { //given - Node child = new NodeItem<>("child"); node = new NodeItem<>("subject"); + val child = new NodeItem("child"); //when node.addChild(child); //then - assertThat(node.getChildren(), hasItem(child)); + Assert.assertThat( + "when a node is added as a child, the node is among the " + + "children", node.getChildren(), hasItem(child)); } /** - * Test {@link NodeItem#addChild(net.kemitix.node.Node) } that adding a node - * as it's own child throws an exception. + * Test that adding a node as it's own child throws an exception. */ @Test(expected = NodeException.class) public void shouldThrowNEWhenAddingANodeAsOwnChild() { @@ -219,8 +227,7 @@ public class NodeItemTest { } /** - * Test {@link NodeItem#addChild(net.kemitix.node.Node) } that adding a node - * to itself as a child causes an exception. + * Test that adding a node to itself as a child causes an exception. */ @Test(expected = NodeException.class) public void shouldThrowWhenAddingSelfAsChild() { @@ -231,89 +238,93 @@ public class NodeItemTest { } /** - * Test {@link NodeItem#addChild(net.kemitix.node.Node) } that adding the - * parent to node causes an exception. + * Test that adding the parent of a node to the node as a child causes an + * exception. */ @Test(expected = NodeException.class) public void shouldThrowWhenAddingParentAsChild() { //given - Node parent = new NodeItem<>("parent"); + val parent = new NodeItem("parent"); node = new NodeItem<>("subject", parent); //when node.addChild(parent); } /** - * Test {@link NodeItem#addChild(net.kemitix.node.Node) } that adding the - * grandparent to node causes an exception. + * Test that adding the grandparent to a node as a child causes an + * exception. */ @Test(expected = NodeException.class) public void shouldThrowWhenAddingGrandParentAsChild() { //given - Node grandParent = new NodeItem<>("grandparent"); - Node parent = new NodeItem<>("parent", grandParent); + val grandParent = new NodeItem("grandparent"); + val parent = new NodeItem("parent", grandParent); node = new NodeItem<>("subject", parent); //when node.addChild(grandParent); } /** - * Test {@link NodeItem#addChild(net.kemitix.node.Node) } that adding a - * child to a node, sets the child's parent node. + * Test that adding a child to a node, sets the child's parent node. */ @Test public void shouldSetParentOnChildWhenAddedAsChild() { //given - Node child = new NodeItem<>("child"); + val child = new NodeItem("child"); node = new NodeItem<>("subject"); //when node.addChild(child); //then - assertThat(child.getParent(), is(node)); + Assert.assertThat( + "when a node is added as a child, the child has the node as " + + "its parent", child.getParent(), is(node)); } /** - * Test {@link NodeItem#walkTree(java.util.List) } that we can walk a tree - * to the target node. + * Test that we can walk a tree to the target node. */ @Test public void shouldWalkTreeToNode() { //given - final String grandparent = "grandparent"; - Node grandParentNode = new NodeItem<>(grandparent); - final String parent = "parent"; - Node parentNode = new NodeItem<>(parent, grandParentNode); - final String subject = "subject"; + val grandparent = "grandparent"; + val grandParentNode = new NodeItem(grandparent); + val parent = "parent"; + val parentNode = new NodeItem(parent, grandParentNode); + val subject = "subject"; node = new NodeItem<>(subject, parentNode); //when - Optional> result = grandParentNode.walkTree(Arrays.asList( - parent, subject)); + val result = grandParentNode.walkTree(Arrays.asList(parent, subject)); //then - assertTrue(result.isPresent()); - assertThat(result.get(), is(node)); + Assert.assertThat("when we walk the tree to a node it is found", + result.isPresent(), is(true)); + if (result.isPresent()) { + Assert.assertThat( + "when we walk the tree to a node the correct node is found", + result.get(), is(node)); + } } /** - * Test {@link NodeItem#walkTree(java.util.List) } that we get an empty - * {@link Optional} when walking a path that doesn't exist. + * Test that we get an empty {@link Optional} when walking a path that + * doesn't exist. */ @Test - public void shouldNotFindNonExistantChildNode() { + public void shouldNotFindNonExistentChildNode() { //given - final String parent = "parent"; - Node parentNode = new NodeItem<>(parent); - final String subject = "subject"; + val parent = "parent"; + val parentNode = new NodeItem(parent); + val subject = "subject"; node = new NodeItem<>(subject, parentNode); //when - Optional> result = parentNode.walkTree(Arrays.asList( - subject, "no child")); + val result = parentNode.walkTree(Arrays.asList(subject, "no child")); //then - assertFalse(result.isPresent()); + Assert.assertThat( + "when we walk the tree to a node that doesn't exists, nothing" + + " is found", result.isPresent(), is(false)); } /** - * Test {@link NodeItem#walkTree(java.util.List) } that when we pass null we - * get an exception. + * Test that when we pass null we get an exception. */ @Test(expected = NullPointerException.class) public void shouldThrowNEWhenWalkTreeNull() { @@ -324,8 +335,8 @@ public class NodeItemTest { } /** - * Test {@link NodeItem#walkTree(java.util.List) } that when we pass an - * empty path we get and empty {@link Optional} as a result. + * Test that when we pass an empty path we get and empty {@link Optional} as + * a result. */ @Test public void shouldReturnEmptyForEmptyWalkTreePath() { @@ -336,37 +347,58 @@ public class NodeItemTest { } /** - * Test {@link NodeItem#createDescendantLine(java.util.List) } that we can - * create a chain of descendant nodes. + * Test that we can create a chain of descendant nodes. */ @Test public void shouldCreateDescendantNodes() { //given node = new NodeItem<>("subject"); - final String alphaData = "alpha"; - final String betaData = "beta"; - final String gammaData = "gamma"; + val alphaData = "alpha"; + val betaData = "beta"; + val gammaData = "gamma"; //when node.createDescendantLine( Arrays.asList(alphaData, betaData, gammaData)); //then - final Optional> alphaOptional = node.getChild(alphaData); - assertTrue(alphaOptional.isPresent()); - Node alpha = alphaOptional.get(); - assertThat(alpha.getParent(), is(node)); - final Optional> betaOptional = alpha.getChild(betaData); - assertTrue(betaOptional.isPresent()); - Node beta = betaOptional.get(); - assertThat(beta.getParent(), is(alpha)); - final Optional> gammaOptional = beta.getChild(gammaData); - assertTrue(gammaOptional.isPresent()); - Node gamma = gammaOptional.get(); - assertThat(gamma.getParent(), is(beta)); + val alphaOptional = node.getChild(alphaData); + Assert.assertThat( + "when creating a descendant line, the first element is found", + alphaOptional.isPresent(), is(true)); + if (alphaOptional.isPresent()) { + val alpha = alphaOptional.get(); + Assert.assertThat( + "when creating a descendant line, the first element has " + + "the current node as its parent", + alpha.getParent(), is(node)); + val betaOptional = alpha.getChild(betaData); + Assert.assertThat( + "when creating a descendant line, the second element is " + + "found", betaOptional.isPresent(), is(true)); + if (betaOptional.isPresent()) { + val beta = betaOptional.get(); + Assert.assertThat( + "when creating a descendant line, the second element " + + "has the first as its parent", + beta.getParent(), is(alpha)); + val gammaOptional = beta.getChild(gammaData); + Assert.assertThat( + "when creating a descendant line, the third element " + + "is found", gammaOptional.isPresent(), + is(true)); + if (gammaOptional.isPresent()) { + val gamma = gammaOptional.get(); + Assert.assertThat( + "when creating a descendant line, the third " + + "element has the second as its parent", + gamma.getParent(), is(beta)); + } + } + } } /** - * Test {@link NodeItem#createDescendantLine(java.util.List) } that if we - * pass null to create a chain of descendant nodes we get an exception. + * Test that if we pass null to create a chain of descendant nodes we get an + * exception. */ @Test(expected = NullPointerException.class) public void shouldThrowNPEWhenCreateDescendantNull() { @@ -377,8 +409,7 @@ public class NodeItemTest { } /** - * Test {@link NodeItem#createDescendantLine(java.util.List) } that if we - * pass an empty list nothing is changed. + * Test that if we pass an empty list nothing is changed. */ @Test public void shouldChangeNothingWhenCreateDescendantEmpty() { @@ -387,43 +418,46 @@ public class NodeItemTest { //when node.createDescendantLine(Collections.emptyList()); //then - assertThat(node.getChildren().size(), is(0)); + Assert.assertThat( + "when creating a descendant line from an empty list, nothing " + + "is created", node.getChildren().size(), is(0)); } /** - * Test {@link NodeItem#findOrCreateChild(java.lang.Object) } that we can - * find a child of a node. + * Test that we can find a child of a node. */ @Test public void shouldFindExistingChildNode() { //given node = new NodeItem<>("subject"); - final String childData = "child"; - Node child = new NodeItem<>(childData, node); + val childData = "child"; + val child = new NodeItem(childData, node); //when - Node found = node.findOrCreateChild(childData); + val found = node.findOrCreateChild(childData); //then - assertThat(found, is(child)); + Assert.assertThat( + "when searching for a child by data, the matching child is " + + "found", found, is(child)); } /** - * Test {@link NodeItem#findOrCreateChild(java.lang.Object) } that we create - * a missing child of a node. + * Test that we create a missing child of a node. */ @Test public void shouldFindCreateNewChildNode() { //given node = new NodeItem<>("subject"); - final String childData = "child"; + val childData = "child"; //when - Node found = node.findOrCreateChild(childData); + val found = node.findOrCreateChild(childData); //then - assertThat(found.getData(), is(childData)); + Assert.assertThat( + "when searching for a child by data, a new node is created", + found.getData(), is(childData)); } /** - * Test {@link NodeItem#findOrCreateChild(java.lang.Object) } that if we - * pass null we get an exception. + * Test that if we pass null we get an exception. */ @Test(expected = NullPointerException.class) public void shouldThrowNPEFWhenFindOrCreateChildNull() { @@ -434,26 +468,29 @@ public class NodeItemTest { } /** - * Test {@link NodeItem#getChild(java.lang.Object) } that we can get the - * node for a child. + * Test that we can get the node for a child. */ @Test public void shouldGetChild() { //given node = new NodeItem<>("subject"); - final String childData = "child"; - Node child = new NodeItem<>(childData); + val childData = "child"; + val child = new NodeItem(childData); node.addChild(child); //when - Optional> found = node.getChild(childData); + val found = node.getChild(childData); //then - assertTrue(found.isPresent()); - assertThat(found.get(), is(child)); + Assert.assertThat("when retrieving a child by its data, it is found", + found.isPresent(), is(true)); + if (found.isPresent()) { + Assert.assertThat( + "when retrieving a child by its data, it is the expected " + + "node", found.get(), is(child)); + } } /** - * Test {@link NodeItem#getChild(java.lang.Object) } that we throw an - * exception when passed null. + * Test that we throw an exception when passed null. */ @Test(expected = NullPointerException.class) public void shouldThrowNPEWhenGetChildNull() { @@ -464,22 +501,29 @@ public class NodeItemTest { } /** - * Test {@link NodeItem#createChild(java.lang.Object) } that we create a - * child as a child of the current node and with the current node as its - * parent. + * Test that we create a child as a child of the current node and with the + * current node as its parent. */ @Test - public void shoudCreateChild() { + public void shouldCreateChild() { //given node = new NodeItem<>("subject"); - final String childData = "child"; + val childData = "child"; //when - Node child = node.createChild(childData); + val child = node.createChild(childData); //then - assertThat(child.getParent(), is(node)); - final Optional> foundChild = node.getChild(childData); - assertTrue(foundChild.isPresent()); - assertThat(foundChild.get(), is(child)); + Assert.assertThat( + "when creating a child node, the child has the current node " + + "as its parent", child.getParent(), is(node)); + val foundChild = node.getChild(childData); + Assert.assertThat( + "when creating a child node, the child can be found by its " + + "data", foundChild.isPresent(), is(true)); + if (foundChild.isPresent()) { + Assert.assertThat( + "when creating a child node, the correct child can be " + + "found by its data", foundChild.get(), is(child)); + } } /**