AbstractFileReporter implements Reporter { @Override public void initialize(@Nonnull AnalysisContext analysis) { if (this.analysis != null) { try { flushReports(); } catch (IOException e) { throw new IllegalStateException("Failed to output previous analysis report."); } } this.analysis = analysis; String minLevel = analysis.getConfiguration().get("minSeverity").asString(null); String minCrit = analysis.getConfiguration().get("minCriticality").asString(null); String output = analysis.getConfiguration().get("output").asString(null); output = output == null ? "out" : output; boolean append = analysis.getConfiguration().get("append").asBoolean(false); keepEmptyFile = append || analysis.getConfiguration().get("keepEmptyFile").asBoolean(true); if (minLevel == null && minCrit == null) { LOG.warn("At least one of `minLevel` and `minCriticality` should to be defined. Defaulting to" + " the obsolete behavior of reporting all potentially breaking elements."); this.minLevel = DifferenceSeverity.POTENTIALLY_BREAKING; } if (minLevel != null) { this.minLevel = DifferenceSeverity.valueOf(minLevel); } if (minCrit != null) { this.minCriticality = analysis.getCriticalityByName(minCrit); if (minCriticality == null) { throw new IllegalArgumentException("Unknown criticality '" + minCrit + "'."); } } OutputStream out; switch (output) { case "out": out = System.out; break; case "err": out = System.err; break; default: file = new File(output); if (file.exists()) { if (!file.isFile()) { LOG.warn( "The configured file, '" + file.getAbsolutePath() + "' is not a file." + " Defaulting the output to standard output."); out = System.out; break; } else if (!file.canWrite()) { LOG.warn( "The configured file, '" + file.getAbsolutePath() + "' is not a writable." + " Defaulting the output to standard output."); out = System.out; break; } } else { File parent = file.getParentFile(); if (parent != null && !parent.exists()) { if (!parent.mkdirs()) { LOG.warn("Failed to create directory structure to write to the configured output file '" + file.getAbsolutePath() + "'. Defaulting the output to standard output."); out = System.out; break; } } } try { out = new FileOutputStream(output, append); } catch (FileNotFoundException e) { LOG.warn("Failed to create the configured output file '" + file.getAbsolutePath() + "'." + " Defaulting the output to standard output.", e); out = System.out; } } shouldClose = out != System.out && out != System.err; this.output = createOutputWriter(out, analysis); }  @Override void initialize(@Nonnull AnalysisContext analysis); @Override void report(@Nonnull Report report); @Override void close();  }
@Test public void testStdOutIsDefaultOutput() { Reporter reporter = new Reporter(); reporter.initialize(ctxWithOutput(null)); assertSame(System.out, reporter.origStream); }
@Test public void testStdOutAsOutput() { Reporter reporter = new Reporter(); reporter.initialize(ctxWithOutput("out")); assertSame(System.out, reporter.origStream); }
@Test public void testStdErrAsOutput() { Reporter reporter = new Reporter(); reporter.initialize(ctxWithOutput("err")); assertSame(System.err, reporter.origStream); }