From f27104c1d98ac616ba1c91c3cfe96e0e495b725a Mon Sep 17 00:00:00 2001 From: fmallet Date: Wed, 15 Sep 2010 22:01:00 +0000 Subject: fix SONAR-1766 - A warning is generated when the user tries to import a PMD configuration file with contains XPath rules. Those rules must be defined through the user web interface. --- plugins/sonar-pmd-plugin/pmd-result.xml | 2 +- .../main/java/org/sonar/plugins/pmd/PmdProfileImporter.java | 13 +++++++++++++ .../java/org/sonar/plugins/pmd/PmdProfileImporterTest.java | 9 +++++++++ 3 files changed, 23 insertions(+), 1 deletion(-) (limited to 'plugins') diff --git a/plugins/sonar-pmd-plugin/pmd-result.xml b/plugins/sonar-pmd-plugin/pmd-result.xml index f5ba165ad06..b95ea127b30 100644 --- a/plugins/sonar-pmd-plugin/pmd-result.xml +++ b/plugins/sonar-pmd-plugin/pmd-result.xml @@ -1,3 +1,3 @@ - + \ No newline at end of file diff --git a/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/PmdProfileImporter.java b/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/PmdProfileImporter.java index 21f3968d9ea..85ad6321f71 100644 --- a/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/PmdProfileImporter.java +++ b/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/PmdProfileImporter.java @@ -58,6 +58,16 @@ public class PmdProfileImporter extends ProfileImporter { protected RulesProfile createRuleProfile(PmdRuleset pmdRuleset, ValidationMessages messages) { RulesProfile profile = RulesProfile.create(); for (PmdRule pmdRule : pmdRuleset.getPmdRules()) { + if (PmdConstants.XPATH_CLASS.equals(pmdRule.getClazz())) { + messages.addWarningText("PMD XPath rule '" + pmdRule.getName() + + "' can't be imported automatically. The rule must be created manually through the Sonar web interface."); + continue; + } + if (pmdRule.getRef() == null) { + messages.addWarningText("A PMD rule without 'ref' attribute can't be imported. see '" + pmdRule.getClazz() + + "'"); + continue; + } Rule rule = ruleFinder.find(RuleQuery.create().withRepositoryKey(PmdConstants.REPOSITORY_KEY).withConfigKey(pmdRule.getRef())); if (rule != null) { ActiveRule activeRule = profile.activateRule(rule, PmdLevelUtils.fromLevel(pmdRule.getPriority())); @@ -86,6 +96,9 @@ public class PmdProfileImporter extends ProfileImporter { PmdRuleset pmdResultset = new PmdRuleset(); for (Element eltRule : getChildren(eltResultset, "rule", namespace)) { PmdRule pmdRule = new PmdRule(eltRule.getAttributeValue("ref")); + pmdRule.setClazz(eltRule.getAttributeValue("class")); + pmdRule.setName(eltRule.getAttributeValue("name")); + pmdRule.setMessage(eltRule.getAttributeValue("message")); parsePmdPriority(eltRule, pmdRule, namespace); parsePmdProperties(eltRule, pmdRule, namespace); pmdResultset.addRule(pmdRule); diff --git a/plugins/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdProfileImporterTest.java b/plugins/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdProfileImporterTest.java index dd2ca319122..fd386c398ab 100644 --- a/plugins/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdProfileImporterTest.java +++ b/plugins/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdProfileImporterTest.java @@ -74,6 +74,15 @@ public class PmdProfileImporterTest { assertThat(messages.hasErrors(), is(false)); } + @Test + public void testImportingProfileWithXPathRule() { + Reader reader = new StringReader(TestUtils.getResourceContent("/org/sonar/plugins/pmd/export_xpath_rules.xml")); + RulesProfile profile = importer.importProfile(reader, messages); + + assertThat(profile.getActiveRules().size(), is(0)); + assertThat(messages.hasWarnings(), is(true)); + } + @Test public void testImportingParameters() { Reader reader = new StringReader(TestUtils.getResourceContent("/org/sonar/plugins/pmd/simple.xml")); -- cgit v1.2.3