@Test public void testTraverseTree() { TreeVisitor visitor = mock(TreeVisitor.class); Node l0 = new Node(); l0.setLeft(new Node()); l0.setRight(new Node()); l0.getLeft().setLeft(new Node()); l0.getLeft().setRight(new Node()); l0.getLeft().getRight().setLeft(new Node()); l0.getLeft().getRight().setRight(new Node()); when(visitor.before(l0, 0)).thenReturn(l0); when(visitor.before(l0.getLeft(), 1)).thenReturn(l0.getLeft()); when(visitor.before(l0.getLeft().getLeft(), 2)).thenReturn(l0.getLeft().getLeft()); when(visitor.visit(l0.getLeft().getLeft(), 2)).thenReturn(l0.getLeft().getLeft()); when(visitor.after(l0.getLeft().getLeft(), 2)).thenReturn(l0.getLeft().getLeft()); when(visitor.visit(l0.getLeft(), 1)).thenReturn(l0.getLeft()); when(visitor.before(l0.getLeft().getRight(), 2)).thenReturn(l0.getLeft().getRight()); when(visitor.before(l0.getLeft().getRight().getLeft(), 3)).thenReturn(l0.getLeft().getRight().getLeft()); when(visitor.visit(l0.getLeft().getRight().getLeft(), 3)).thenReturn(l0.getLeft().getRight().getLeft()); when(visitor.after(l0.getLeft().getRight().getLeft(), 3)).thenReturn(l0.getLeft().getRight().getLeft()); when(visitor.visit(l0.getLeft().getRight(), 2)).thenReturn(l0.getLeft().getRight()); when(visitor.before(l0.getLeft().getRight().getRight(), 3)).thenReturn(l0.getLeft().getRight().getRight()); when(visitor.visit(l0.getLeft().getRight().getRight(), 3)).thenReturn(l0.getLeft().getRight().getRight()); when(visitor.after(l0.getLeft().getRight().getRight(), 3)).thenReturn(l0.getLeft().getRight().getRight()); when(visitor.after(l0.getLeft().getRight(), 2)).thenReturn(l0.getLeft().getRight()); when(visitor.after(l0.getLeft(), 1)).thenReturn(l0.getLeft()); when(visitor.visit(l0, 0)).thenReturn(l0); when(visitor.before(l0.getRight(), 1)).thenReturn(l0.getRight()); when(visitor.visit(l0.getRight(), 1)).thenReturn(l0.getRight()); when(visitor.after(l0.getRight(), 1)).thenReturn(l0.getRight()); when(visitor.after(l0, 0)).thenReturn(l0); Node result = new TreeTraverser().traverse(l0, visitor); verify(visitor).before(l0, 0); verify(visitor).before(l0.getLeft(), 1); verify(visitor).before(l0.getLeft().getLeft(), 2); verify(visitor).visit(l0.getLeft().getLeft(), 2); verify(visitor).after(l0.getLeft().getLeft(), 2); verify(visitor).visit(l0.getLeft(), 1); verify(visitor).before(l0.getLeft().getRight(), 2); verify(visitor).before(l0.getLeft().getRight().getLeft(), 3); verify(visitor).visit(l0.getLeft().getRight().getLeft(), 3); verify(visitor).after(l0.getLeft().getRight().getLeft(), 3); verify(visitor).visit(l0.getLeft().getRight(), 2); verify(visitor).before(l0.getLeft().getRight().getRight(), 3); verify(visitor).visit(l0.getLeft().getRight().getRight(), 3); verify(visitor).after(l0.getLeft().getRight().getRight(), 3); verify(visitor).after(l0.getLeft().getRight(), 2); verify(visitor).after(l0.getLeft(), 1); verify(visitor).visit(l0, 0); verify(visitor).before(l0.getRight(), 1); verify(visitor).visit(l0.getRight(), 1); verify(visitor).after(l0.getRight(), 1); verify(visitor).after(l0, 0); verifyNoMoreInteractions(visitor); assertSame(l0, result); }
public Node traverse(Node node, TreeVisitor... visitors) { if (ArrayUtils.isEmpty(visitors)) { throw new IllegalArgumentException("You need to provide at least one visitor for this traverser"); } Node result = node; for (TreeVisitor visitor : visitors) { result = traverse(result, visitor, 0); } return result; }
TreeTraverser { public Node traverse(Node node, TreeVisitor... visitors) { if (ArrayUtils.isEmpty(visitors)) { throw new IllegalArgumentException("You need to provide at least one visitor for this traverser"); } Node result = node; for (TreeVisitor visitor : visitors) { result = traverse(result, visitor, 0); } return result; } }
TreeTraverser { public Node traverse(Node node, TreeVisitor... visitors) { if (ArrayUtils.isEmpty(visitors)) { throw new IllegalArgumentException("You need to provide at least one visitor for this traverser"); } Node result = node; for (TreeVisitor visitor : visitors) { result = traverse(result, visitor, 0); } return result; }  }
TreeTraverser { public Node traverse(Node node, TreeVisitor... visitors) { if (ArrayUtils.isEmpty(visitors)) { throw new IllegalArgumentException("You need to provide at least one visitor for this traverser"); } Node result = node; for (TreeVisitor visitor : visitors) { result = traverse(result, visitor, 0); } return result; }  Node traverse(Node node, TreeVisitor... visitors); }
TreeTraverser { public Node traverse(Node node, TreeVisitor... visitors) { if (ArrayUtils.isEmpty(visitors)) { throw new IllegalArgumentException("You need to provide at least one visitor for this traverser"); } Node result = node; for (TreeVisitor visitor : visitors) { result = traverse(result, visitor, 0); } return result; }  Node traverse(Node node, TreeVisitor... visitors);  }