From 7a10498a31b51f78e3af567a9d5cd62070cd0b0c Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Sun, 21 Aug 2016 12:42:21 +0100 Subject: [PATCH] NodeItem: remove dynamic name support for nodes --- src/main/java/net/kemitix/node/NodeItem.java | 32 ---- .../java/net/kemitix/node/NodeItemTest.java | 168 ------------------ 2 files changed, 200 deletions(-) diff --git a/src/main/java/net/kemitix/node/NodeItem.java b/src/main/java/net/kemitix/node/NodeItem.java index 9e34a83..1a83c73 100644 --- a/src/main/java/net/kemitix/node/NodeItem.java +++ b/src/main/java/net/kemitix/node/NodeItem.java @@ -47,21 +47,6 @@ public class NodeItem implements Node { this.nameSupplier = (n) -> null; } - /** - * Creates root node with a name supplier. - * - * @param data the data or null - * @param nameSupplier the name supplier function - * - * @deprecated dynamic names don't work on immutable objects - */ - @Deprecated - public NodeItem( - final T data, final Function, String> nameSupplier) { - this(data); - this.nameSupplier = nameSupplier; - } - /** * Creates a node with a parent. * @@ -86,23 +71,6 @@ public class NodeItem implements Node { setParent(parent); } - /** - * Creates a node with a name supplier and a parent. - * - * @param data the data or null - * @param nameSupplier the name supplier function - * @param parent the parent node - * - * @deprecated dynamic names don't work on immutable objects - */ - @Deprecated - public NodeItem( - final T data, final Function, String> nameSupplier, - final Node parent) { - this(data, nameSupplier); - setParent(parent); - } - private String generateName() { return getNameSupplier().apply(this); } diff --git a/src/test/java/net/kemitix/node/NodeItemTest.java b/src/test/java/net/kemitix/node/NodeItemTest.java index 87b5a69..247e7fb 100644 --- a/src/test/java/net/kemitix/node/NodeItemTest.java +++ b/src/test/java/net/kemitix/node/NodeItemTest.java @@ -8,13 +8,9 @@ import org.junit.rules.ExpectedException; import static org.assertj.core.api.Assertions.assertThat; -import java.time.LocalDate; -import java.time.format.DateTimeFormatter; import java.util.Arrays; import java.util.Collections; import java.util.Optional; -import java.util.concurrent.atomic.AtomicInteger; -import java.util.function.Function; /** * Test for {@link NodeItem}. @@ -50,17 +46,6 @@ public class NodeItemTest { softly.assertAll(); } - @Test - public void canCreateNodeWithParentAndCustomNameSupplier() { - //given - node = new NodeItem<>(null, n -> "root name supplier"); - //when - val child = new NodeItem(null, n -> "overridden", node); - //then - assertThat(child.getName()).isEqualTo("overridden"); - assertThat(child.getParent()).contains(node); - } - @Test public void canSetName() { //given @@ -587,69 +572,12 @@ public class NodeItemTest { node.createChild(null); } - @Test - public void getNameShouldBeCorrect() { - //given - node = new NodeItem<>("subject", n -> n.getData().get()); - //then - assertThat(node.getName()).isEqualTo("subject"); - } - - @Test - public void getNameShouldUseParentNameSupplier() { - //given - val root = new NodeItem("root", n -> n.getData().get()); - node = Nodes.unnamedChild("child", root); - //then - assertThat(node.getName()).isEqualTo("child"); - } - - @Test - public void getNameShouldReturnNameForNonStringData() { - val root = new NodeItem(LocalDate.parse("2016-05-23"), n -> { - if (n.isEmpty()) { - return null; - } - return n.getData().get().format(DateTimeFormatter.BASIC_ISO_DATE); - - }); - //then - assertThat(root.getName()).isEqualTo("20160523"); - } - - @Test - public void getNameShouldUseClosestNameSupplier() { - node = new NodeItem<>("root", n -> n.getData().get()); - val child = new NodeItem("child", Object::toString); - node.addChild(child); - val grandChild = Nodes.unnamedChild("grandchild", child); - //then - assertThat(node.getName()).isEqualTo("root"); - assertThat(child.getName()).isNotEqualTo("child"); - assertThat(grandChild.getName()).isNotEqualTo("grandchild"); - } - - @Test - public void getNameShouldWorkWithoutNameSupplier() { - node = Nodes.namedRoot(null, "root"); - val namedchild = Nodes.namedChild("named", "Alice", node); - //then - assertThat(node.getName()).isEqualTo("root"); - assertThat(namedchild.getName()).isEqualTo("Alice"); - } - @Test public void canCreateRootNodeWithoutData() { node = Nodes.namedRoot(null, "empty"); assertThat(node.getData()).isEmpty(); } - @Test - public void canCreateRootNodeWithoutDataButWithNameSupplier() { - node = Nodes.unnamedRoot(null); - assertThat(node.getData()).isEmpty(); - } - @Test public void getChildNamedFindsChild() { //given @@ -852,22 +780,6 @@ public class NodeItemTest { assertThat(node.isNamed()).isTrue(); } - @Test - public void removeParentNodeProvidesSameNameSupplier() { - // once a node has it's parent removed it should provide a default name - // provider - //given - node = new NodeItem<>("data", n -> n.getData().get()); - val child = Nodes.unnamedChild("other", node); - assertThat(node.getName()).as("initial root name").isEqualTo("data"); - assertThat(child.getName()).as("initial child name").isEqualTo("other"); - //when - child.removeParent(); - //then - assertThat(node.getName()).as("final root name").isEqualTo("data"); - assertThat(child.getName()).as("final child name").isEqualTo("other"); - } - @Test @SuppressWarnings("unchecked") public void removeChildRemovesTheChild() { @@ -961,86 +873,6 @@ public class NodeItemTest { assertThat(node.getChildren()).containsExactly(child); } - @Test - @SuppressWarnings("unchecked") - public void removeParentCopiesRootNameSupplier() { - //given - node = new NodeItem<>("root data", n -> "root supplier"); - val child = Nodes.unnamedChild("child data", node); - assertThat(child.getName()).isEqualTo("root supplier"); - //when - child.removeParent(); - //then - assertThat(child.getName()).isEqualTo("root supplier"); - } - - @Test - @SuppressWarnings("unchecked") - public void removeParentDoesNotReplaceLocalNameSupplier() { - //given - node = new NodeItem<>("root data", n -> "root supplier"); - val child = new NodeItem<>("child data", n -> "local supplier", node); - assertThat(child.getName()).isEqualTo("local supplier"); - //when - child.removeParent(); - //then - assertThat(child.getName()).isEqualTo("local supplier"); - } - - @Test - public void setNameToNullRevertsToParentNameSupplier() { - //given - node = new NodeItem<>(null, n -> "root supplier"); - val child = Nodes.namedChild("child data", "child name", node); - assertThat(child.getName()).isEqualTo("child name"); - //when - child.setName(null); - //then - assertThat(child.getName()).isEqualTo("root supplier"); - } - - @Test - public void getNameWithNameSupplierIsRecalculatedEachCall() { - val counter = new AtomicInteger(0); - node = new NodeItem<>(null, - n -> Integer.toString(counter.incrementAndGet())); - //then - assertThat(node.getName()).isNotEqualTo(node.getName()); - } - - @Test - public void isNamedWithNameSupplierIsRecalculatedEachCall() { - val counter = new AtomicInteger(0); - node = new NodeItem<>(null, n -> { - // alternate between even numbers and nulls: null, 2, null, 4, null - final int i = counter.incrementAndGet(); - if (i % 2 == 0) { - return Integer.toString(i); - } - return null; - }); - //then - assertThat(node.isNamed()).isFalse(); - assertThat(node.isNamed()).isTrue(); - } - - @Test - public void canUseNameSupplierToBuildFullPath() { - //given - final Function, String> pathNameSupplier = node -> { - Optional> parent = node.getParent(); - if (parent.isPresent()) { - return parent.get().getName() + "/" + node.getData().get(); - } - return ""; - }; - node = new NodeItem<>(null, pathNameSupplier); - val child = Nodes.unnamedChild("child", node); - val grandchild = Nodes.unnamedChild("grandchild", child); - //then - assertThat(grandchild.getName()).isEqualTo("/child/grandchild"); - } - @Test public void canSafelyHandleFindChildWhenAChildHasNoData() { //given