@Test public void testChainingVisitors() { TreeVisitor visitor1 = mock(TreeVisitor.class); TreeVisitor visitor2 = mock(TreeVisitor.class); TreeVisitor visitor3 = mock(TreeVisitor.class); Node l0 = new Node(); l0.setLeft(new Node()); l0.setRight(new Node()); l0.getRight().setRight(new Node()); when(visitor1.before(l0, 0)).thenReturn(l0); when(visitor1.before(l0.getLeft(), 1)).thenReturn(l0.getLeft()); when(visitor1.visit(l0.getLeft(), 1)).thenReturn(l0.getLeft()); when(visitor1.after(l0.getLeft(), 1)).thenReturn(l0.getLeft()); when(visitor1.visit(l0, 0)).thenReturn(l0); when(visitor1.before(l0.getRight(), 1)).thenReturn(l0.getRight()); when(visitor1.visit(l0.getRight(), 1)).thenReturn(l0.getRight()); when(visitor1.before(l0.getRight().getRight(), 2)).thenReturn(l0.getRight().getRight()); when(visitor1.visit(l0.getRight().getRight(), 2)).thenReturn(l0.getRight().getRight()); when(visitor1.after(l0.getRight().getRight(), 2)).thenReturn(l0.getRight().getRight()); when(visitor1.after(l0.getRight(), 1)).thenReturn(l0.getRight()); when(visitor1.after(l0, 0)).thenReturn(l0); when(visitor2.before(l0, 0)).thenReturn(l0); when(visitor2.before(l0.getLeft(), 1)).thenReturn(l0.getLeft()); when(visitor2.visit(l0.getLeft(), 1)).thenReturn(l0.getLeft()); when(visitor2.after(l0.getLeft(), 1)).thenReturn(l0.getLeft()); when(visitor2.visit(l0, 0)).thenReturn(l0); when(visitor2.before(l0.getRight(), 1)).thenReturn(l0.getRight()); when(visitor2.visit(l0.getRight(), 1)).thenReturn(l0.getRight()); when(visitor2.before(l0.getRight().getRight(), 2)).thenReturn(l0.getRight().getRight()); when(visitor2.visit(l0.getRight().getRight(), 2)).thenReturn(l0.getRight().getRight()); when(visitor2.after(l0.getRight().getRight(), 2)).thenReturn(l0.getRight().getRight()); when(visitor2.after(l0.getRight(), 1)).thenReturn(l0.getRight()); when(visitor2.after(l0, 0)).thenReturn(l0); when(visitor3.before(l0, 0)).thenReturn(l0); when(visitor3.before(l0.getLeft(), 1)).thenReturn(l0.getLeft()); when(visitor3.visit(l0.getLeft(), 1)).thenReturn(l0.getLeft()); when(visitor3.after(l0.getLeft(), 1)).thenReturn(l0.getLeft()); when(visitor3.visit(l0, 0)).thenReturn(l0); when(visitor3.before(l0.getRight(), 1)).thenReturn(l0.getRight()); when(visitor3.visit(l0.getRight(), 1)).thenReturn(l0.getRight()); when(visitor3.before(l0.getRight().getRight(), 2)).thenReturn(l0.getRight().getRight()); when(visitor3.visit(l0.getRight().getRight(), 2)).thenReturn(l0.getRight().getRight()); when(visitor3.after(l0.getRight().getRight(), 2)).thenReturn(l0.getRight().getRight()); when(visitor3.after(l0.getRight(), 1)).thenReturn(l0.getRight()); when(visitor3.after(l0, 0)).thenReturn(l0); Node result = new TreeTraverser().traverse(l0, visitor1, visitor2, visitor3); verify(visitor1).before(l0, 0); verify(visitor1).before(l0.getLeft(), 1); verify(visitor1).visit(l0.getLeft(), 1); verify(visitor1).after(l0.getLeft(), 1); verify(visitor1).visit(l0, 0); verify(visitor1).before(l0.getRight(), 1); verify(visitor1).visit(l0.getRight(), 1); verify(visitor1).before(l0.getRight().getRight(), 2); verify(visitor1).visit(l0.getRight().getRight(), 2); verify(visitor1).after(l0.getRight().getRight(), 2); verify(visitor1).after(l0.getRight(), 1); verify(visitor1).after(l0, 0); verifyNoMoreInteractions(visitor1); verify(visitor2).before(l0, 0); verify(visitor2).before(l0.getLeft(), 1); verify(visitor2).visit(l0.getLeft(), 1); verify(visitor2).after(l0.getLeft(), 1); verify(visitor2).visit(l0, 0); verify(visitor2).before(l0.getRight(), 1); verify(visitor2).visit(l0.getRight(), 1); verify(visitor2).before(l0.getRight().getRight(), 2); verify(visitor2).visit(l0.getRight().getRight(), 2); verify(visitor2).after(l0.getRight().getRight(), 2); verify(visitor2).after(l0.getRight(), 1); verify(visitor2).after(l0, 0); verifyNoMoreInteractions(visitor2); verify(visitor3).before(l0, 0); verify(visitor3).before(l0.getLeft(), 1); verify(visitor3).visit(l0.getLeft(), 1); verify(visitor3).after(l0.getLeft(), 1); verify(visitor3).visit(l0, 0); verify(visitor3).before(l0.getRight(), 1); verify(visitor3).visit(l0.getRight(), 1); verify(visitor3).before(l0.getRight().getRight(), 2); verify(visitor3).visit(l0.getRight().getRight(), 2); verify(visitor3).after(l0.getRight().getRight(), 2); verify(visitor3).after(l0.getRight(), 1); verify(visitor3).after(l0, 0); verifyNoMoreInteractions(visitor3); 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);  }