### Question:
PmdProfileExporter extends ProfileExporter { @Override public void exportProfile(RulesProfile profile, Writer writer) { final PmdRuleSet tree = PmdRuleSets.from(profile, PmdConstants.REPOSITORY_KEY); try { tree.writeTo(writer); } catch (IllegalStateException e) { throw new IllegalStateException("An exception occurred while generating the PMD configuration file from profile: " + profile.getName(), e); } } PmdProfileExporter(); @Override void exportProfile(RulesProfile profile, Writer writer);  }### Answer:
@Test void should_export_pmd_profile_on_writer() { String importedXml = PmdTestUtils.getResourceContent("/org/sonar/plugins/pmd/export_simple.xml"); StringWriter stringWriter = new StringWriter(); exporter.exportProfile(importProfile(importedXml), stringWriter); assertThat(stringWriter.toString()).satisfies(equalsIgnoreEOL(importedXml)); }
@Test void should_export_pmd_profile_on_writer_exception() throws IOException { final String importedXml = PmdTestUtils.getResourceContent("/org/sonar/plugins/pmd/export_simple.xml"); final Writer writer = mock(Writer.class); doThrow(new IOException("test exception")).when(writer).write(anyString()); final Throwable thrown = catchThrowable(() -> exporter.exportProfile(importProfile(importedXml), writer)); assertThat(thrown) .isInstanceOf(IllegalStateException.class) .hasMessage("An exception occurred while generating the PMD configuration file from profile: null"); }
@Test void should_skip_all_empty_params() { String importedXml = PmdTestUtils.getResourceContent("/org/sonar/plugins/pmd/export_rule_with_all_params_empty.xml"); String expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<ruleset name=\"pmd\">\n" + " <description>Sonar Profile: pmd</description>\n" + " <rule ref=\"category/java/codestyle.xml/CommentDefaultAccessModifier\">\n" + " <priority>2</priority>\n" + " </rule>\n" + "</ruleset>"; final StringWriter writer = new StringWriter(); exporter.exportProfile(importProfile(importedXml), writer); assertThat(writer.toString()).satisfies(equalsIgnoreEOL(expected)); }
@Test void should_export_empty_configuration_as_xml() { final StringWriter writer = new StringWriter(); exporter.exportProfile(RulesProfile.create(), writer); assertThat(writer.toString()).satisfies(equalsIgnoreEOL("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<ruleset name=\"pmd\"> <description>Sonar Profile: pmd</description></ruleset>")); }
@Test void should_export_xPath_rule() { Rule rule = Rule.create(PmdConstants.REPOSITORY_KEY, "MyOwnRule", "This is my own xpath rule.") .setConfigKey(PmdConstants.XPATH_CLASS) .setRepositoryKey(PmdConstants.REPOSITORY_KEY); rule.createParameter(PmdConstants.XPATH_EXPRESSION_PARAM); rule.createParameter(PmdConstants.XPATH_MESSAGE_PARAM); RulesProfile profile = RulesProfile.create(); ActiveRule xpath = profile.activateRule(rule, null); xpath.setParameter(PmdConstants.XPATH_EXPRESSION_PARAM, " xpath.setParameter(PmdConstants.XPATH_MESSAGE_PARAM, "This is bad"); final StringWriter writer = new StringWriter(); exporter.exportProfile(profile, writer); assertThat(writer.toString()).satisfies(equalsIgnoreEOL(PmdTestUtils.getResourceContent("/org/sonar/plugins/pmd/export_xpath_rules.xml"))); }