### Question:
OrderSorter { public List<String> sort(List<Class<?>> classes) throws OrderSortCycleException { List<String> result = new ArrayList<>(); SimpleDirectedGraph<String, DefaultEdge> graph = new SimpleDirectedGraph<>(DefaultEdge.class); for (OrderConstraint orderConstraint : getConstraints(classes)) { graph.addVertex(orderConstraint.getFirst()); graph.addVertex(orderConstraint.getSecond()); graph.addEdge(orderConstraint.getFirst(), orderConstraint.getSecond()); } CycleDetector<String, DefaultEdge> cycleDetector = new CycleDetector<>(graph); if(!cycleDetector.detectCycles()) { for (TopologicalOrderIterator<String, DefaultEdge> iterator = new TopologicalOrderIterator<>(graph); iterator.hasNext();) { result.add(iterator.next()); } } else { String cycles = Joiner.on(", ").join(cycleDetector.findCycles()); throw new OrderSortCycleException("The given order constraints contain (at least one) cycle. Cycles can only " + "be caused by static references because we have single inherintance only in Java (involved classes: '" + cycles + "')."); } return result; } OrderSorter();  OrderSorter(OrderSorter nextCollector); Set<OrderConstraint> getConstraints(List<Class<?>> classes); List<String> sort(List<Class<?>> classes);  }### Answer:
@Test public void sortInheritanceConstraints() throws OrderSortCycleException { ArrayList<Class<?>> classes = Lists.newArrayList(new Class<?>[]{SubClass.class, SuperClass.class, SuperInterface.class}); OrderSorter orderSorter = new InheritanceOrderSorter(); List<String> result = orderSorter.sort(classes); assertThat(result.indexOf(SuperClass.class.getName()), is(lessThan(result.indexOf(SubClass.class.getName())))); assertThat(result.indexOf(SuperInterface.class.getName()), is(lessThan(result.indexOf(SuperClass.class.getName())))); assertThat(result.indexOf(SuperInterface.class.getName()), is(lessThan(result.indexOf(SubClass.class.getName())))); }
@Test(expected = OrderSortCycleException.class) public void sortStaticReferenceCycleError() throws OrderSortCycleException { ArrayList<Class<?>> classes = Lists.newArrayList(new Class<?>[]{ParentCycle.class, ClientCycle.class}); OrderSorter orderSorter = new StaticUsageOrderSorter(); orderSorter.sort(classes); }