Merge pull request #2 from kemitix/update-code-style

Update code style
This commit is contained in:
Paul Campbell 2016-03-14 15:56:47 +00:00
commit c6cc3ec9d9
15 changed files with 502 additions and 183 deletions

3
.gitignore vendored
View file

@ -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

104
.idea/codeStyleSettings.xml Normal file
View file

@ -0,0 +1,104 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectCodeStyleSettingsManager">
<option name="PER_PROJECT_SETTINGS">
<value>
<option name="GENERATE_FINAL_LOCALS" value="true" />
<option name="GENERATE_FINAL_PARAMETERS" value="true" />
<option name="VISIBILITY" value="packageLocal" />
<option name="CLASS_COUNT_TO_USE_IMPORT_ON_DEMAND" value="9999" />
<option name="NAMES_COUNT_TO_USE_IMPORT_ON_DEMAND" value="9999" />
<option name="PACKAGES_TO_USE_IMPORT_ON_DEMAND">
<value />
</option>
<option name="IMPORT_LAYOUT_TABLE">
<value>
<package name="" withSubpackages="true" static="false" />
<emptyLine />
<package name="" withSubpackages="true" static="true" />
<emptyLine />
<package name="java" withSubpackages="true" static="false" />
<emptyLine />
<package name="javax" withSubpackages="true" static="false" />
<emptyLine />
<package name="org.springframework" withSubpackages="true" static="false" />
<emptyLine />
<package name="uk.ac.fife" withSubpackages="true" static="false" />
<emptyLine />
<package name="net.kemitix" withSubpackages="true" static="false" />
</value>
</option>
<option name="JD_ADD_BLANK_AFTER_PARM_COMMENTS" value="true" />
<option name="JD_ADD_BLANK_AFTER_RETURN" value="true" />
<option name="WRAP_COMMENTS" value="true" />
<JavaCodeStyleSettings>
<option name="ANNOTATION_PARAMETER_WRAP" value="1" />
</JavaCodeStyleSettings>
<XML>
<option name="XML_LEGACY_SETTINGS_IMPORTED" value="true" />
</XML>
<codeStyleSettings language="JAVA">
<option name="RIGHT_MARGIN" value="80" />
<option name="KEEP_LINE_BREAKS" value="false" />
<option name="KEEP_FIRST_COLUMN_COMMENT" value="false" />
<option name="KEEP_CONTROL_STATEMENT_IN_ONE_LINE" value="false" />
<option name="KEEP_BLANK_LINES_IN_DECLARATIONS" value="1" />
<option name="KEEP_BLANK_LINES_IN_CODE" value="1" />
<option name="KEEP_BLANK_LINES_BEFORE_RBRACE" value="1" />
<option name="BLANK_LINES_AROUND_FIELD" value="1" />
<option name="BLANK_LINES_AROUND_FIELD_IN_INTERFACE" value="1" />
<option name="BLANK_LINES_AFTER_CLASS_HEADER" value="1" />
<option name="INDENT_CASE_FROM_SWITCH" value="false" />
<option name="ALIGN_MULTILINE_CHAINED_METHODS" value="true" />
<option name="ALIGN_MULTILINE_THROWS_LIST" value="true" />
<option name="SPACE_BEFORE_ARRAY_INITIALIZER_LBRACE" value="true" />
<option name="SPACE_BEFORE_ANNOTATION_ARRAY_INITIALIZER_LBRACE" value="true" />
<option name="CALL_PARAMETERS_WRAP" value="1" />
<option name="METHOD_PARAMETERS_WRAP" value="1" />
<option name="METHOD_PARAMETERS_LPAREN_ON_NEXT_LINE" value="true" />
<option name="RESOURCE_LIST_WRAP" value="1" />
<option name="EXTENDS_LIST_WRAP" value="1" />
<option name="THROWS_LIST_WRAP" value="1" />
<option name="EXTENDS_KEYWORD_WRAP" value="1" />
<option name="THROWS_KEYWORD_WRAP" value="1" />
<option name="METHOD_CALL_CHAIN_WRAP" value="5" />
<option name="BINARY_OPERATION_WRAP" value="1" />
<option name="BINARY_OPERATION_SIGN_ON_NEXT_LINE" value="true" />
<option name="TERNARY_OPERATION_WRAP" value="5" />
<option name="TERNARY_OPERATION_SIGNS_ON_NEXT_LINE" value="true" />
<option name="FOR_STATEMENT_WRAP" value="1" />
<option name="ARRAY_INITIALIZER_WRAP" value="1" />
<option name="ASSIGNMENT_WRAP" value="1" />
<option name="PLACE_ASSIGNMENT_SIGN_ON_NEXT_LINE" value="true" />
<option name="ASSERT_STATEMENT_WRAP" value="1" />
<option name="ASSERT_STATEMENT_COLON_ON_NEXT_LINE" value="true" />
<option name="IF_BRACE_FORCE" value="3" />
<option name="DOWHILE_BRACE_FORCE" value="3" />
<option name="WHILE_BRACE_FORCE" value="3" />
<option name="FOR_BRACE_FORCE" value="3" />
<option name="WRAP_LONG_LINES" value="true" />
<option name="PARAMETER_ANNOTATION_WRAP" value="1" />
<option name="VARIABLE_ANNOTATION_WRAP" value="1" />
<option name="ENUM_CONSTANTS_WRAP" value="1" />
<arrangement>
<groups>
<group>
<type>GETTERS_AND_SETTERS</type>
<order>KEEP</order>
</group>
<group>
<type>OVERRIDDEN_METHODS</type>
<order>KEEP</order>
</group>
<group>
<type>DEPENDENT_METHODS</type>
<order>BREADTH_FIRST</order>
</group>
</groups>
</arrangement>
</codeStyleSettings>
</value>
</option>
<option name="USE_PER_PROJECT_SETTINGS" value="true" />
</component>
</project>

33
.idea/compiler.xml Normal file
View file

@ -0,0 +1,33 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CompilerConfiguration">
<option name="DEFAULT_COMPILER" value="Javac" />
<resourceExtensions />
<wildcardResourcePatterns>
<entry name="!?*.java" />
<entry name="!?*.form" />
<entry name="!?*.class" />
<entry name="!?*.groovy" />
<entry name="!?*.scala" />
<entry name="!?*.flex" />
<entry name="!?*.kt" />
<entry name="!?*.clj" />
<entry name="!?*.aj" />
</wildcardResourcePatterns>
<annotationProcessing>
<profile default="true" name="Default" enabled="false">
<processorPath useClasspath="true" />
</profile>
<profile default="false" name="Maven default annotation processors profile" enabled="true">
<sourceOutputDir name="target/generated-sources/annotations" />
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
<outputRelativeToContentRoot value="true" />
<processorPath useClasspath="true" />
<module name="node" />
</profile>
</annotationProcessing>
<bytecodeTargetLevel>
<module name="node" target="1.8" />
</bytecodeTargetLevel>
</component>
</project>

7
.idea/encodings.xml Normal file
View file

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding">
<file url="file://$PROJECT_DIR$" charset="UTF-8" />
<file url="PROJECT" charset="UTF-8" />
</component>
</project>

View file

@ -0,0 +1,59 @@
<component name="InspectionProjectProfileManager">
<profile version="1.0">
<option name="myName" value="Project Default" />
<option name="myLocal" value="true" />
<inspection_tool class="AssertEqualsCalledOnArray" enabled="true" level="WARNING" enabled_by_default="true" />
<inspection_tool class="AssertEqualsMayBeAssertSame" enabled="true" level="WARNING" enabled_by_default="true" />
<inspection_tool class="AssertsWithoutMessages" enabled="true" level="WARNING" enabled_by_default="true" />
<inspection_tool class="BeforeClassOrAfterClassIsPublicStaticVoidNoArg" enabled="true" level="WARNING" enabled_by_default="true" />
<inspection_tool class="BeforeOrAfterIsPublicVoidNoArg" enabled="true" level="WARNING" enabled_by_default="true" />
<inspection_tool class="ConstantJUnitAssertArgument" enabled="true" level="WARNING" enabled_by_default="true" />
<inspection_tool class="ExpectedExceptionNeverThrown" enabled="true" level="WARNING" enabled_by_default="true" />
<inspection_tool class="IgnoredJUnitTest" enabled="true" level="WARNING" enabled_by_default="true" />
<inspection_tool class="JUnit3MethodNamingConvention" enabled="true" level="WARNING" enabled_by_default="true" />
<inspection_tool class="JUnit3StyleTestMethodInJUnit4Class" enabled="true" level="WARNING" enabled_by_default="true" />
<inspection_tool class="JUnit4MethodNamingConvention" enabled="true" level="WARNING" enabled_by_default="true" />
<inspection_tool class="JUnitAbstractTestClassNamingConvention" enabled="true" level="WARNING" enabled_by_default="true">
<option name="m_regex" value="[A-Z][A-Za-z\d]*TestCase" />
<option name="m_minLength" value="12" />
<option name="m_maxLength" value="64" />
</inspection_tool>
<inspection_tool class="JUnitDatapoint" enabled="true" level="WARNING" enabled_by_default="true" />
<inspection_tool class="JUnitRule" enabled="true" level="WARNING" enabled_by_default="true" />
<inspection_tool class="JUnitTestClassNamingConvention" enabled="true" level="WARNING" enabled_by_default="true">
<option name="m_regex" value="[A-Z][A-Za-z\d]*(Test|IT)" />
<option name="m_minLength" value="8" />
<option name="m_maxLength" value="64" />
</inspection_tool>
<inspection_tool class="LoggerInitializedWithForeignClass" enabled="false" level="WARNING" enabled_by_default="false">
<option name="loggerClassName" value="org.apache.log4j.Logger,org.slf4j.LoggerFactory,org.apache.commons.logging.LogFactory,java.util.logging.Logger" />
<option name="loggerFactoryMethodName" value="getLogger,getLogger,getLog,getLogger" />
</inspection_tool>
<inspection_tool class="MigrateAssertToMatcherAssert" enabled="true" level="WARNING" enabled_by_default="true" />
<inspection_tool class="MisorderedAssertEqualsParameters" enabled="true" level="WARNING" enabled_by_default="true" />
<inspection_tool class="MisspelledSetUp" enabled="true" level="WARNING" enabled_by_default="true" />
<inspection_tool class="MisspelledTearDown" enabled="true" level="WARNING" enabled_by_default="true" />
<inspection_tool class="MultipleExceptionsDeclaredOnTestMethod" enabled="true" level="WARNING" enabled_by_default="true" />
<inspection_tool class="ParameterizedParametersStaticCollection" enabled="true" level="WARNING" enabled_by_default="true" />
<inspection_tool class="SetupCallsSuperSetup" enabled="true" level="WARNING" enabled_by_default="true" />
<inspection_tool class="SetupIsPublicVoidNoArg" enabled="true" level="WARNING" enabled_by_default="true" />
<inspection_tool class="SimplifiableJUnitAssertion" enabled="true" level="WARNING" enabled_by_default="true" />
<inspection_tool class="StaticSuite" enabled="true" level="WARNING" enabled_by_default="true" />
<inspection_tool class="SuperTearDownInFinally" enabled="true" level="WARNING" enabled_by_default="true" />
<inspection_tool class="TeardownCallsSuperTeardown" enabled="true" level="WARNING" enabled_by_default="true" />
<inspection_tool class="TeardownIsPublicVoidNoArg" enabled="true" level="WARNING" enabled_by_default="true" />
<inspection_tool class="TestCaseInProductCode" enabled="true" level="WARNING" enabled_by_default="true" />
<inspection_tool class="TestCaseWithConstructor" enabled="true" level="WARNING" enabled_by_default="true" />
<inspection_tool class="TestCaseWithNoTestMethods" enabled="true" level="WARNING" enabled_by_default="true">
<option name="ignoreSupers" value="false" />
</inspection_tool>
<inspection_tool class="TestMethodInProductCode" enabled="true" level="WARNING" enabled_by_default="true" />
<inspection_tool class="TestMethodIsPublicVoidNoArg" enabled="true" level="WARNING" enabled_by_default="true" />
<inspection_tool class="TestMethodWithoutAssertion" enabled="true" level="WARNING" enabled_by_default="true">
<option name="assertionMethods" value="org.junit.Assert,assert.*|fail.*,junit.framework.Assert,assert.*|fail.*,org.mockito.Mockito,verify.*,org.mockito.InOrder,verify,org.junit.rules.ExpectedException,expect.*,org.hamcrest.MatcherAssert,assertThat" />
<option name="assertKeywordIsAssertion" value="false" />
</inspection_tool>
<inspection_tool class="UnconstructableTestCase" enabled="true" level="WARNING" enabled_by_default="true" />
<inspection_tool class="UseOfObsoleteAssert" enabled="true" level="WARNING" enabled_by_default="true" />
</profile>
</component>

View file

@ -0,0 +1,7 @@
<component name="InspectionProjectProfileManager">
<settings>
<option name="PROJECT_PROFILE" value="Project Default" />
<option name="USE_PROJECT_PROFILE" value="true" />
<version value="1.0" />
</settings>
</component>

42
.idea/misc.xml Normal file
View file

@ -0,0 +1,42 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="EntryPointsManager">
<entry_points version="2.0" />
</component>
<component name="MavenProjectsManager">
<option name="originalFiles">
<list>
<option value="$PROJECT_DIR$/pom.xml" />
</list>
</option>
</component>
<component name="ProjectLevelVcsManager" settingsEditedManually="false">
<OptionsSetting value="true" id="Add" />
<OptionsSetting value="true" id="Remove" />
<OptionsSetting value="true" id="Checkout" />
<OptionsSetting value="true" id="Update" />
<OptionsSetting value="true" id="Status" />
<OptionsSetting value="true" id="Edit" />
<ConfirmationsSetting value="0" id="Add" />
<ConfirmationsSetting value="0" id="Remove" />
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_6" assert-keyword="true" jdk-15="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/classes" />
</component>
<component name="masterDetails">
<states>
<state key="ProjectJDKs.UI">
<settings>
<last-edited>1.8</last-edited>
<splitter-proportions>
<option name="proportions">
<list>
<option value="0.2" />
</list>
</option>
</splitter-proportions>
</settings>
</state>
</states>
</component>
</project>

8
.idea/modules.xml Normal file
View file

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/node.iml" filepath="$PROJECT_DIR$/node.iml" />
</modules>
</component>
</project>

6
.idea/vcs.xml Normal file
View file

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>

18
node.iml Normal file
View file

@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8" inherit-compiler-output="false">
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" scope="TEST" name="Maven: org.projectlombok:lombok:1.16.8" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: junit:junit:4.12" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.hamcrest:hamcrest-core:1.3" level="project" />
</component>
</module>

View file

@ -1,7 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<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>
<groupId>net.kemitix</groupId>
<artifactId>node</artifactId>
<version>0.2.0-SNAPSHOT</version>
<packaging>jar</packaging>
@ -31,6 +30,12 @@
<inceptionYear>2016</inceptionYear>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.8</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>

View file

@ -7,8 +7,9 @@ import java.util.Set;
/**
* An interface for tree node items.
*
* @author pcampbell
* @param <T> the type of data held in each node
*
* @author pcampbell
*/
public interface Node<T> {
@ -21,7 +22,6 @@ public interface Node<T> {
/**
* Fetch the parent node.
*
* <p>
* If the node is a root node, i.e. has no parent, then this will return
* null.

View file

@ -18,7 +18,7 @@ public class NodeItem<T> implements Node<T> {
private Node<T> parent;
private Set<Node<T>> children;
private final Set<Node<T>> children = new HashSet<>();
/**
* Creates a root node.
@ -43,7 +43,6 @@ public class NodeItem<T> implements Node<T> {
if (parent != null) {
setParent(parent);
}
this.children = new HashSet<>();
}
@Override
@ -109,19 +108,15 @@ public class NodeItem<T> implements Node<T> {
*/
@Override
public boolean isChildOf(final Node<T> 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));
}
/**
* 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
@ -153,9 +148,8 @@ public class NodeItem<T> implements Node<T> {
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()));
}
}
@ -172,12 +166,7 @@ public class NodeItem<T> implements Node<T> {
if (child == null) {
throw new NullPointerException("child");
}
Optional<Node<T>> found = getChild(child);
if (found.isPresent()) {
return found.get();
} else {
return createChild(child);
}
return getChild(child).orElseGet(() -> createChild(child));
}
/**
@ -193,8 +182,8 @@ public class NodeItem<T> implements Node<T> {
throw new NullPointerException("child");
}
return children.stream()
.filter((Node<T> t) -> t.getData().equals(child))
.findAny();
.filter((Node<T> t) -> t.getData().equals(child))
.findAny();
}
/**

View file

@ -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));
}

View file

@ -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<String> 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<String> parent = new NodeItem<>("parent");
val parent = new NodeItem<String>("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<String> child = new NodeItem<>("child", node);
val child = new NodeItem<String>("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<String> parent = new NodeItem<>("parent");
val parent = new NodeItem<String>("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<String> parent = new NodeItem<>("parent");
val parent = new NodeItem<String>("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<String> child = node.createChild("child");
Node<String> newParent = new NodeItem<>("newParent");
val child = node.createChild("child");
val newParent = new NodeItem<String>("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<String> child = node.createChild("child");
Node<String> newParent = new NodeItem<>("newParent");
val child = node.createChild("child");
val newParent = new NodeItem<String>("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<String> child = new NodeItem<>("child");
node = new NodeItem<>("subject");
val child = new NodeItem<String>("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<String> parent = new NodeItem<>("parent");
val parent = new NodeItem<String>("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<String> grandParent = new NodeItem<>("grandparent");
Node<String> parent = new NodeItem<>("parent", grandParent);
val grandParent = new NodeItem<String>("grandparent");
val parent = new NodeItem<String>("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<String> child = new NodeItem<>("child");
val child = new NodeItem<String>("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<String> grandParentNode = new NodeItem<>(grandparent);
final String parent = "parent";
Node<String> parentNode = new NodeItem<>(parent, grandParentNode);
final String subject = "subject";
val grandparent = "grandparent";
val grandParentNode = new NodeItem<String>(grandparent);
val parent = "parent";
val parentNode = new NodeItem<String>(parent, grandParentNode);
val subject = "subject";
node = new NodeItem<>(subject, parentNode);
//when
Optional<Node<String>> 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<String> parentNode = new NodeItem<>(parent);
final String subject = "subject";
val parent = "parent";
val parentNode = new NodeItem<String>(parent);
val subject = "subject";
node = new NodeItem<>(subject, parentNode);
//when
Optional<Node<String>> 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<Node<String>> alphaOptional = node.getChild(alphaData);
assertTrue(alphaOptional.isPresent());
Node<String> alpha = alphaOptional.get();
assertThat(alpha.getParent(), is(node));
final Optional<Node<String>> betaOptional = alpha.getChild(betaData);
assertTrue(betaOptional.isPresent());
Node<String> beta = betaOptional.get();
assertThat(beta.getParent(), is(alpha));
final Optional<Node<String>> gammaOptional = beta.getChild(gammaData);
assertTrue(gammaOptional.isPresent());
Node<String> 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<String> child = new NodeItem<>(childData, node);
val childData = "child";
val child = new NodeItem<String>(childData, node);
//when
Node<String> 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<String> 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<String> child = new NodeItem<>(childData);
val childData = "child";
val child = new NodeItem<String>(childData);
node.addChild(child);
//when
Optional<Node<String>> 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<String> child = node.createChild(childData);
val child = node.createChild(childData);
//then
assertThat(child.getParent(), is(node));
final Optional<Node<String>> 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));
}
}
/**