### Question:
AcyclicDirectedGraphRule extends RuleExtensionHandler<AcyclicDirectedGraphRule, GraphConnectionContext> { @Override @SuppressWarnings("unchecked") public RuleViolations evaluate(final RuleExtension rule, final GraphConnectionContext context) { final GraphEvaluationState state = context.getState(); final Graph<?, Node> graph = (Graph<?, Node>) state.getGraph(); final Optional<Node<? extends View<?>, ? extends Edge>> oSource = context.getSource(); final Optional<Node<? extends View<?>, ? extends Edge>> oTarget = context.getTarget(); final Edge<? extends View<?>, ? extends Node> oConnector = context.getConnector(); final DefaultRuleViolations result = new DefaultRuleViolations(); if (!(oSource.isPresent() && oTarget.isPresent())) { return result; } final Node<?, Edge> source = (Node<?, Edge>) oSource.get(); final Node<?, Edge> target = (Node<?, Edge>) oTarget.get(); final Edge<?, Node> connector = (Edge<?, Node>) oConnector; try { final TreeWalkTraverseProcessor walker = getTreeWalker(source, target, connector); walker.traverse(graph, new TreeTraverseCallback<Graph, Node, Edge>() { final Set<Node> inProgress = new HashSet<>(); @Override public void startGraphTraversal(final Graph graph) { } @Override public boolean startNodeTraversal(final Node node) { if (inProgress.contains(node)) { throw new DirectedAcrylicGraphViolationException(); } inProgress.add(node); return true; } @Override public boolean startEdgeTraversal(final Edge edge) { return true; } @Override public void endNodeTraversal(final Node node) { inProgress.remove(node); } @Override public void endEdgeTraversal(final Edge edge) { } @Override public void endGraphTraversal() { } }); } catch (DirectedAcrylicGraphViolationException e) { result.addViolation(new RuleViolationImpl(ERROR_MESSAGE)); } return result; }  @Override Class<AcyclicDirectedGraphRule> getExtensionType(); @Override Class<GraphConnectionContext> getContextType(); @Override boolean accepts(final RuleExtension rule,
                           final GraphConnectionContext context); @Override @SuppressWarnings("unchecked") RuleViolations evaluate(final RuleExtension rule,
                                   final GraphConnectionContext context);  }### Answer:
@Test @SuppressWarnings("unchecked") public void checkCyclicalConnection() { final Node node1 = new NodeImpl<>("node1"); final Node node2 = new NodeImpl<>("node2"); final Edge c1 = new EdgeImpl<>("edge1"); node1.getOutEdges().add(c1); node2.getInEdges().add(c1); c1.setSourceNode(node1); c1.setTargetNode(node2); graph.addNode(node1); graph.addNode(node2); when(context.getSource()).thenReturn(Optional.of(node2)); when(context.getTarget()).thenReturn(Optional.of(node1)); when(context.getConnector()).thenReturn(connector); final RuleViolations result = check.evaluate(rule, context); assertNotNull(result); assertTrue(result.violations().iterator().hasNext()); final RuleViolation violation = result.violations().iterator().next(); assertNotNull(violation); assertTrue(violation.getArguments().isPresent()); assertEquals(1, violation.getArguments().get().length); assertEquals(AcyclicDirectedGraphRule.ERROR_MESSAGE, violation.getArguments().get()[0]); }