### Question:
PmdProfileImporter extends ProfileImporter { @Override public RulesProfile importProfile(Reader pmdConfigurationFile, ValidationMessages messages) { PmdRuleset pmdRuleset = parsePmdRuleset(pmdConfigurationFile, messages); return createRuleProfile(pmdRuleset, messages); } PmdProfileImporter(RuleFinder ruleFinder); @Override RulesProfile importProfile(Reader pmdConfigurationFile, ValidationMessages messages);  }### Answer:
@Test public void should_import_simple_profile() { Reader reader = read("/org/sonar/plugins/pmd/simple.xml"); RulesProfile profile = importer.importProfile(reader, messages); assertThat(profile.getActiveRules()).hasSize(3); assertThat(profile.getActiveRuleByConfigKey("pmd", "rulesets/java/coupling.xml/ExcessiveImports")).isNotNull(); assertThat(profile.getActiveRuleByConfigKey("pmd", "rulesets/java/design.xml/UseNotifyAllInsteadOfNotify")).isNotNull(); assertThat(messages.hasErrors()).isFalse(); }
@Test public void should_import_profile_with_xpath_rule() { Reader reader = read("/org/sonar/plugins/pmd/export_xpath_rules.xml"); RulesProfile profile = importer.importProfile(reader, messages); assertThat(profile.getActiveRules()).isEmpty(); assertThat(messages.hasWarnings()).isTrue(); }
@Test public void should_import_parameter() { Reader reader = read("/org/sonar/plugins/pmd/simple.xml"); RulesProfile profile = importer.importProfile(reader, messages); ActiveRule activeRule = profile.getActiveRuleByConfigKey("pmd", "rulesets/java/coupling.xml/ExcessiveImports"); assertThat(activeRule.getParameter("minimum")).isEqualTo("30"); }
@Test public void should_import_default_priority() { Reader reader = read("/org/sonar/plugins/pmd/simple.xml"); RulesProfile profile = importer.importProfile(reader, messages); ActiveRule activeRule = profile.getActiveRuleByConfigKey("pmd", "rulesets/java/coupling.xml/ExcessiveImports"); assertThat(activeRule.getSeverity()).isSameAs(RulePriority.BLOCKER); }
@Test public void should_import_priority() { Reader reader = read("/org/sonar/plugins/pmd/simple.xml"); RulesProfile profile = importer.importProfile(reader, messages); ActiveRule activeRule = profile.getActiveRuleByConfigKey("pmd", "rulesets/java/design.xml/UseNotifyAllInsteadOfNotify"); assertThat(activeRule.getSeverity()).isSameAs(RulePriority.MINOR); activeRule = profile.getActiveRuleByConfigKey("pmd", "rulesets/java/coupling.xml/CouplingBetweenObjects"); assertThat(activeRule.getSeverity()).isSameAs(RulePriority.CRITICAL); }
@Test public void should_import_pmd_configuration_with_unknown_nodes() { Reader reader = read("/org/sonar/plugins/pmd/complex-with-unknown-nodes.xml"); RulesProfile profile = importer.importProfile(reader, messages); assertThat(profile.getActiveRules()).hasSize(3); }
@Test public void should_deal_with_unsupported_property() { Reader reader = read("/org/sonar/plugins/pmd/simple.xml"); RulesProfile profile = importer.importProfile(reader, messages); ActiveRule check = profile.getActiveRuleByConfigKey("pmd", "rulesets/java/coupling.xml/CouplingBetweenObjects"); assertThat(check.getParameter("threshold")).isNull(); assertThat(messages.getWarnings()).hasSize(2); }
@Test public void should_fail_on_invalid_xml() { Reader reader = new StringReader("not xml"); importer.importProfile(reader, messages); assertThat(messages.getErrors()).hasSize(1); }
@Test public void should_warn_on_unknown_rule() { Reader reader = read("/org/sonar/plugins/pmd/simple.xml"); importer = new PmdProfileImporter(mock(RuleFinder.class)); RulesProfile profile = importer.importProfile(reader, messages); assertThat(profile.getActiveRules()).isEmpty(); assertThat(messages.getWarnings()).hasSize(4); }