JavacFailuresSupplier implements FailuresSupplier { @Override public List<Failure> getFailures() { List<Failure> failures = new ArrayList<>(); Failure.Builder failureBuilder = null; StringBuilder details = null; for (String line : Splitter.on("\n").split(errorStream.toString())) { if (failureBuilder != null) { if (line.startsWith(" ")) { details.append("\n").append(line); continue; } else { failures.add(failureBuilder.details(details.toString()).build()); failureBuilder = null; details = null; } } Matcher matcher = ERROR_LINE.matcher(line); if (matcher.matches()) { failureBuilder = new Failure.Builder() .file(new File(matcher.group(1))) .line(Integer.parseInt(matcher.group(2))) .severity("ERROR") .message(matcher.group(3)); details = new StringBuilder(); } } if (failureBuilder != null) { failures.add(failureBuilder.details(details.toString()).build()); } return failures; } JavacFailuresSupplier(StringBuilder errorStream); static JavacFailuresSupplier create(JavaCompile javac); @Override List<Failure> getFailures(); @Override @SuppressWarnings("StrictUnusedVariable") RuntimeException handleInternalFailure(Path reportDir, RuntimeException ex);  }
@Test public void noFailuresInEmptyOutput() { String javacOutput = ""; JavacFailuresSupplier supplier = new JavacFailuresSupplier(new StringBuilder(javacOutput)); assertThat(supplier.getFailures()).isEmpty(); }
@Test public void noFailuresInOutputWithOnlyWarnings() { String javacOutput = "warning: [options] bootstrap class path not set in conjunction with -source 1.7\n" + "Note: " + CLASS_FILE + " uses unchecked or unsafe operations.\n" + "Note: Recompile with -Xlint:unchecked for details. \n" + "1 warning"; JavacFailuresSupplier supplier = new JavacFailuresSupplier(new StringBuilder(javacOutput)); assertThat(supplier.getFailures()).isEmpty(); }
@Test public void twoFailuresInOutputWithNoWarnings() { String javacOutput = CLASS_FILE + ":" + LINE_1 + ": error: " + ERROR_1 + DETAIL_1 + "\n" + CLASS_FILE + ":" + LINE_2 + ": error: " + ERROR_2 + DETAIL_2 + "\n"; JavacFailuresSupplier supplier = new JavacFailuresSupplier(new StringBuilder(javacOutput)); assertThat(supplier.getFailures()) .containsExactly( new Failure.Builder() .file(new File(CLASS_FILE)) .line(LINE_1) .severity("ERROR") .message(ERROR_1) .details(DETAIL_1) .build(), new Failure.Builder() .file(new File(CLASS_FILE)) .line(12) .severity("ERROR") .message(ERROR_2) .details(DETAIL_2) .build()); }