DefaultTreeForTreeLayout extends
		AbstractTreeForTreeLayout<TreeNode> { public void addChild(TreeNode parentNode, TreeNode node) { checkArg(hasNode(parentNode), "parentNode is not in the tree"); checkArg(!hasNode(node), "node is already in the tree"); List<TreeNode> list = childrenMap.get(parentNode); if (list == null) { list = new ArrayList<TreeNode>(); childrenMap.put(parentNode, list); } list.add(node); parents.put(node, parentNode); } DefaultTreeForTreeLayout(TreeNode root); @Override TreeNode getParent(TreeNode node); @Override List<TreeNode> getChildrenList(TreeNode node); boolean hasNode(TreeNode node); void addChild(TreeNode parentNode, TreeNode node); void addChildren(TreeNode parentNode, TreeNode... nodes);  }
@Test public void testAddChild_alreadyInTree() throws Exception { DefaultTreeForTreeLayout<String> tree = new DefaultTreeForTreeLayout<String>( "ROOT"); try { tree.addChild("ROOT", "ROOT"); fail("exception expected"); } catch (Exception ex) { assertEquals("node is already in the tree", ex.getMessage()); } tree.addChild("ROOT", "N1"); try { tree.addChild("ROOT", "N1"); fail("exception expected"); } catch (Exception ex) { assertEquals("node is already in the tree", ex.getMessage()); } }
@Test public void testAddChild_parentNotInTree() throws Exception { DefaultTreeForTreeLayout<String> tree = new DefaultTreeForTreeLayout<String>( "ROOT"); try { tree.addChild("N1", "N1.1"); fail("exception expected"); } catch (Exception ex) { assertEquals("parentNode is not in the tree", ex.getMessage()); } }
@Test public void testAddChild() throws Exception { DefaultTreeForTreeLayout<String> tree = new DefaultTreeForTreeLayout<String>( "ROOT"); tree.addChild("ROOT", "N1"); tree.addChild("ROOT", "N2"); tree.addChild("N1", "N1.1"); assertEquals("N1", tree.getChildrenList("ROOT").get(0)); assertEquals("N1.1", tree.getChildrenList("N1").get(0)); assertEquals("N2", tree.getChildrenList("ROOT").get(1)); }