### Question:
PmdConfiguration implements BatchExtension { public File dumpXmlReport(Report report) { if (!settings.getBoolean(PROPERTY_GENERATE_XML)) { return null; } try { String reportAsString = reportToString(report); File reportFile = writeToWorkingDirectory(reportAsString, PMD_RESULT_XML); LOG.info("PMD output report: " + reportFile.getAbsolutePath()); return reportFile; } catch (IOException e) { throw new IllegalStateException("Fail to save the PMD report", e); } } PmdConfiguration(FileSystem fileSystem, Settings settings); File dumpXmlRuleSet(String repositoryKey, String rulesXml); File dumpXmlReport(Report report); static final String PROPERTY_GENERATE_XML; static final String PMD_RESULT_XML; }### Answer:
@Test public void should_dump_xml_report() throws IOException { when(fs.workDir()).thenReturn(WORK_DIR); settings.setProperty(PmdConfiguration.PROPERTY_GENERATE_XML, true); File reportFile = configuration.dumpXmlReport(new Report()); assertThat(reportFile).isEqualTo(new File(WORK_DIR, "pmd-result.xml")); List<String> writtenLines = Files.readLines(reportFile, Charsets.UTF_8); assertThat(writtenLines).hasSize(3); assertThat(writtenLines.get(1)).contains("<pmd"); }
@Test public void should_fail_to_dump_xml_report() throws Exception { when(fs.workDir()).thenReturn(new File("xxx")); settings.setProperty(PmdConfiguration.PROPERTY_GENERATE_XML, true); expectedException.expect(IllegalStateException.class); expectedException.expectMessage("Fail to save the PMD report"); configuration.dumpXmlReport(new Report()); }
@Test public void should_ignore_xml_report_when_property_is_not_set() { File reportFile = configuration.dumpXmlReport(new Report()); assertThat(reportFile).isNull(); verifyZeroInteractions(fs); }