StepCatalogueWriter { void printStepCatalogue(Set<CataloguedStep> cataloguedSteps, Consumer<String> messageWriter) { messageWriter.accept(""); messageWriter.accept("Step Catalogue: (All Steps By Category) --> "); messageWriter.accept(""); Map<String, List<CataloguedStep>> invokersByCategory = cataloguedSteps .stream() .collect(Collectors.groupingBy(CataloguedStep::getCategory)); SortedMap<String, List<CataloguedStep>> sortedByCategory = new TreeMap<>(invokersByCategory); int maxPatternWidth = sortedByCategory.values().stream().flatMap(Collection::stream).mapToInt(cs -> cs.getPattern().length()).max().orElse(0); int maxCategoryWidth = sortedByCategory.keySet().stream().mapToInt(String::length).max().orElse(0); String format = "%-" + maxCategoryWidth + "s %-" + maxPatternWidth + "s %12s %12s %18s"; messageWriter.accept(format(format, "Category:", "Pattern:", "Invocations:", "Failures:", "Cumulative Time:")); sortedByCategory.values().forEach(l -> { List<CataloguedStep> snapshot = new LinkedList<>(l); sort(snapshot, comparing(CataloguedStep::getPattern)); printCataloguedSteps(format, snapshot, messageWriter, CataloguedStep::getCumulativeTime); }); messageWriter.accept(""); printTopSteps("Step Catalogue: (Longest Running, Top 6) --> ", comparing(CataloguedStep::getMaxTime).reversed(), s -> s.getMaxTime() > 0, cataloguedSteps, format, messageWriter, CataloguedStep::getMaxTime, "Max Time:" ); printTopSteps("Step Catalogue: (Cumulative Time, Top 6) --> ", comparing(CataloguedStep::getCumulativeTime).reversed(), s -> s.getCumulativeTime() > 0, cataloguedSteps, format, messageWriter, CataloguedStep::getCumulativeTime, "Cumulative Time:" ); }    }
@Test public void printStepCatalogue() throws Exception { String output = captureOutput(this::writeTestOutput); assertEquals(expected, output); }