private static <T> void compareAndSetUserObject( final TypedDefaultMutableTreeNode<T> node, final T oldData, final T newData) { Preconditions.checkNotNull(node); Preconditions.checkNotNull(oldData); Preconditions.checkNotNull(newData); Assert.assertEquals(node.getUserObject(), oldData); node.setUserObjectSafely(newData); }
@Test public void createTree_singleNode() { { final Object nodeObject = new Integer(0); final TypedDefaultMutableTreeNode<Object> singleNode = new TypedDefaultMutableTreeNode<>(nodeObject, false); Assert.assertEquals(singleNode.getUserObject(), nodeObject); } { final Object nodeObject = Lists.newArrayList("1", "2", "3"); final TypedDefaultMutableTreeNode<Object> singleNode = new TypedDefaultMutableTreeNode<>(nodeObject, false); Assert.assertEquals(singleNode.getUserObject(), nodeObject); } }
@Test public void builder_forSimpleType() { // Given(SetUp) // When(Exercise) final TypedDefaultMutableTreeNodeBuilder<String> builder = TypedDefaultMutableTreeNodeBuilder.newBuilder(TypeToken.of(String.class)); final String data1 = "1"; final String data2 = "2"; final TypedDefaultMutableTreeNode<String> node = builder.build(data1); // Then(Verify) TypedDefaultMutableTreeNodeTest.compareAndSetUserObject(node, data1, data2); Assert.assertEquals(node.getUserObject(), data2); }
@Test public void createTree_twoLevel() { final Object rootNodeObject = new Integer(0); final TypedDefaultMutableTreeNode<Object> rootNode = new TypedDefaultMutableTreeNode<Object>(rootNodeObject, true); Assert.assertEquals(rootNode.getUserObject(), rootNodeObject); final ArrayList<Integer> secondLevelNodeObjects = Iterables.IntRangeForward(10, 10, 1).ToArrayList(); final ArrayList<TypedDefaultMutableTreeNode<Object>> secondLevelNodes = Iterables.Map( secondLevelNodeObjects, obj -> new TypedDefaultMutableTreeNode<Object>(obj, false)) .ToArrayList(); secondLevelNodes.forEach(rootNode::addSafely); Assert.assertTrue( Iterables.Zip(secondLevelNodeObjects, secondLevelNodes) .All(tuple -> tuple.GetItem2().getUserObject() == tuple.GetItem1())); secondLevelNodes.forEach(child -> Assert.assertEquals(child.getParent(), rootNode)); }