diff options
author | fmallet <freddy.mallet@gmail.com> | 2010-09-12 19:51:23 +0000 |
---|---|---|
committer | fmallet <freddy.mallet@gmail.com> | 2010-09-12 19:51:23 +0000 |
commit | da66ba341527c6cc9c9073664f72c7c8c9e374c5 (patch) | |
tree | e574c26fe52c8c314807685f45f1e7891eb482fb | |
parent | 555b263f31d1105829d14e9312e50b6739aa5b18 (diff) | |
download | sonarqube-da66ba341527c6cc9c9073664f72c7c8c9e374c5.tar.gz sonarqube-da66ba341527c6cc9c9073664f72c7c8c9e374c5.zip |
SONAR-1766 - start migrating to the new Sonar rule API before defining the PMD XPath rule
12 files changed, 372 insertions, 66 deletions
diff --git a/plugins/sonar-pmd-plugin/pmd-result.xml b/plugins/sonar-pmd-plugin/pmd-result.xml new file mode 100644 index 00000000000..bffb2ec7aec --- /dev/null +++ b/plugins/sonar-pmd-plugin/pmd-result.xml @@ -0,0 +1,3 @@ +<?xml version="1.0" encoding="UTF-8"?> +<pmd version="4.2.5" timestamp="2010-09-12T21:47:59.064"> +</pmd>
\ No newline at end of file diff --git a/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/PmdConstants.java b/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/PmdConstants.java new file mode 100644 index 00000000000..ed7d5444940 --- /dev/null +++ b/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/PmdConstants.java @@ -0,0 +1,30 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2009 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * Sonar is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * Sonar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.plugins.pmd; + +import org.sonar.api.CoreProperties; + +public final class PmdConstants { + + public static final String REPOSITORY_KEY = CoreProperties.PMD_PLUGIN; + public static final String REPOSITORY_NAME = "PMD"; + public static final String PLUGIN_NAME = "PMD"; + public static final String PLUGIN_KEY = CoreProperties.PMD_PLUGIN; +} diff --git a/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/PmdExecutor.java b/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/PmdExecutor.java index a747f0225a7..ea55bee3499 100644 --- a/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/PmdExecutor.java +++ b/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/PmdExecutor.java @@ -60,7 +60,7 @@ public class PmdExecutor implements BatchExtension { ruleContext.setReport(report); RuleSets rulesets = createRulesets(); - + for (File file : project.getFileSystem().getSourceFiles(Java.INSTANCE)) { ruleContext.setSourceCodeFilename(file.getAbsolutePath()); Reader fileReader = new InputStreamReader(new FileInputStream(file), project.getFileSystem().getSourceCharset()); diff --git a/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/PmdPlugin.java b/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/PmdPlugin.java index b1386c9ef23..70801611d85 100644 --- a/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/PmdPlugin.java +++ b/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/PmdPlugin.java @@ -19,20 +19,19 @@ */ package org.sonar.plugins.pmd; -import org.sonar.api.CoreProperties; -import org.sonar.api.Plugin; - import java.util.Arrays; import java.util.List; +import org.sonar.api.Plugin; + public class PmdPlugin implements Plugin { public String getKey() { - return CoreProperties.PMD_PLUGIN; + return PmdConstants.PLUGIN_KEY; } public String getName() { - return "PMD"; + return PmdConstants.PLUGIN_NAME; } public String getDescription() { diff --git a/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/PmdProfileExporter.java b/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/PmdProfileExporter.java new file mode 100644 index 00000000000..d74da7072f3 --- /dev/null +++ b/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/PmdProfileExporter.java @@ -0,0 +1,108 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2009 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * Sonar is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * Sonar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.plugins.pmd; + +import java.io.IOException; +import java.io.Writer; +import java.util.ArrayList; +import java.util.List; + +import org.sonar.api.CoreProperties; +import org.sonar.api.profiles.ProfileExporter; +import org.sonar.api.profiles.RulesProfile; +import org.sonar.api.resources.Java; +import org.sonar.api.rules.ActiveRule; +import org.sonar.api.rules.ActiveRuleParam; +import org.sonar.api.rules.RulePriority; +import org.sonar.api.utils.SonarException; +import org.sonar.plugins.pmd.xml.Property; +import org.sonar.plugins.pmd.xml.PmdRule; +import org.sonar.plugins.pmd.xml.PmdRuleset; + +import com.thoughtworks.xstream.XStream; + +public class PmdProfileExporter extends ProfileExporter { + + public PmdProfileExporter() { + super(PmdConstants.REPOSITORY_KEY, PmdConstants.PLUGIN_NAME); + setSupportedLanguages(Java.KEY); + setMimeType("application/xml"); + } + + @Override + public void exportProfile(RulesProfile profile, Writer writer) { + try { + PmdRuleset tree = buildModuleTree(profile.getActiveRulesByRepository(PmdConstants.REPOSITORY_KEY), profile.getName()); + String xmlModules = buildXmlFromModuleTree(tree); + writer.append(xmlModules); + } catch (IOException e) { + throw new SonarException("Fail to export the profile " + profile, e); + } + } + + protected PmdRuleset buildModuleTree(List<ActiveRule> activeRules, String profileName) { + PmdRuleset ruleset = new PmdRuleset(profileName); + for (ActiveRule activeRule : activeRules) { + if (activeRule.getRule().getPluginName().equals(CoreProperties.PMD_PLUGIN)) { + String configKey = activeRule.getRule().getConfigKey(); + PmdRule rule = new PmdRule(configKey, to(activeRule.getPriority())); + List<Property> properties = null; + if (activeRule.getActiveRuleParams() != null && !activeRule.getActiveRuleParams().isEmpty()) { + properties = new ArrayList<Property>(); + for (ActiveRuleParam activeRuleParam : activeRule.getActiveRuleParams()) { + properties.add(new Property(activeRuleParam.getRuleParam().getKey(), activeRuleParam.getValue())); + } + } + rule.setProperties(properties); + ruleset.addRule(rule); + } + } + return ruleset; + } + + protected String buildXmlFromModuleTree(PmdRuleset tree) { + XStream xstream = new XStream(); + xstream.setClassLoader(getClass().getClassLoader()); + xstream.processAnnotations(PmdRuleset.class); + xstream.processAnnotations(PmdRule.class); + xstream.processAnnotations(Property.class); + return xstream.toXML(tree); + } + + private String to(RulePriority priority) { + if (priority.equals(RulePriority.BLOCKER)) { + return "1"; + } + if (priority.equals(RulePriority.CRITICAL)) { + return "2"; + } + if (priority.equals(RulePriority.MAJOR)) { + return "3"; + } + if (priority.equals(RulePriority.MINOR)) { + return "4"; + } + if (priority.equals(RulePriority.INFO)) { + return "5"; + } + throw new IllegalArgumentException("Level not supported: " + priority); + } + +} diff --git a/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/PmdRuleRepository.java b/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/PmdRuleRepository.java new file mode 100644 index 00000000000..308a93b90fe --- /dev/null +++ b/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/PmdRuleRepository.java @@ -0,0 +1,52 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2009 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * Sonar is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * Sonar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.plugins.pmd; + +import org.sonar.api.platform.ServerFileSystem; +import org.sonar.api.resources.Java; +import org.sonar.api.rules.Rule; +import org.sonar.api.rules.RuleRepository; +import org.sonar.api.rules.StandardRuleXmlFormat; + +import java.io.File; +import java.util.ArrayList; +import java.util.List; + +public final class PmdRuleRepository extends RuleRepository { + + // for user extensions + private ServerFileSystem fileSystem; + + public PmdRuleRepository(ServerFileSystem fileSystem) { + super(PmdConstants.REPOSITORY_KEY, Java.KEY); + setName(PmdConstants.REPOSITORY_NAME); + this.fileSystem = fileSystem; + } + + @Override + public List<Rule> createRules() { + List<Rule> rules = new ArrayList<Rule>(); + rules.addAll(StandardRuleXmlFormat.parseXml(getClass().getResourceAsStream("/org/sonar/plugins/pmd/rules.xml"))); + for (File userExtensionXml : fileSystem.getExtensions(PmdConstants.REPOSITORY_KEY, "xml")) { + rules.addAll(StandardRuleXmlFormat.parseXml(userExtensionXml)); + } + return rules; + } +} diff --git a/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/PmdRulesRepository.java b/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/PmdRulesRepository.java index 7e0c5946d27..dcb41c8ca9f 100644 --- a/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/PmdRulesRepository.java +++ b/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/PmdRulesRepository.java @@ -28,8 +28,8 @@ import org.sonar.api.resources.Java; import org.sonar.api.rules.*;
import org.sonar.api.utils.SonarException;
import org.sonar.plugins.pmd.xml.Property;
-import org.sonar.plugins.pmd.xml.Rule;
-import org.sonar.plugins.pmd.xml.Ruleset;
+import org.sonar.plugins.pmd.xml.PmdRule;
+import org.sonar.plugins.pmd.xml.PmdRuleset;
import java.io.IOException;
import java.io.InputStream;
@@ -59,28 +59,28 @@ public class PmdRulesRepository extends AbstractImportableRulesRepository<Java, }
public String exportConfiguration(RulesProfile activeProfile) {
- Ruleset tree = buildModuleTree(activeProfile.getActiveRulesByPlugin(CoreProperties.PMD_PLUGIN), activeProfile.getName());
+ PmdRuleset tree = buildModuleTree(activeProfile.getActiveRulesByPlugin(CoreProperties.PMD_PLUGIN), activeProfile.getName());
String xmlModules = buildXmlFromModuleTree(tree);
return addHeaderToXml(xmlModules);
}
public List<ActiveRule> importConfiguration(String configuration, List<org.sonar.api.rules.Rule> rules) {
List<ActiveRule> activeRules = new ArrayList<ActiveRule>();
- Ruleset moduleTree = buildModuleTreeFromXml(configuration);
+ PmdRuleset moduleTree = buildModuleTreeFromXml(configuration);
buildActiveRulesFromModuleTree(moduleTree, activeRules, rules);
return activeRules;
}
- protected Ruleset buildModuleTree(List<ActiveRule> activeRules) {
+ protected PmdRuleset buildModuleTree(List<ActiveRule> activeRules) {
return buildModuleTree(activeRules, "Sonar PMD rules");
}
- protected Ruleset buildModuleTree(List<ActiveRule> activeRules, String profileName) {
- Ruleset ruleset = new Ruleset(profileName);
+ protected PmdRuleset buildModuleTree(List<ActiveRule> activeRules, String profileName) {
+ PmdRuleset ruleset = new PmdRuleset(profileName);
for (ActiveRule activeRule : activeRules) {
if (activeRule.getRule().getPluginName().equals(CoreProperties.PMD_PLUGIN)) {
String configKey = activeRule.getRule().getConfigKey();
- Rule rule = new Rule(configKey, getRulePriorityMapper().to(activeRule.getPriority()));
+ PmdRule rule = new PmdRule(configKey, getRulePriorityMapper().to(activeRule.getPriority()));
List<Property> properties = null;
if (activeRule.getActiveRuleParams() != null && !activeRule.getActiveRuleParams().isEmpty()) {
properties = new ArrayList<Property>();
@@ -95,11 +95,11 @@ public class PmdRulesRepository extends AbstractImportableRulesRepository<Java, return ruleset;
}
- protected String buildXmlFromModuleTree(Ruleset tree) {
+ protected String buildXmlFromModuleTree(PmdRuleset tree) {
XStream xstream = new XStream();
xstream.setClassLoader(getClass().getClassLoader());
- xstream.processAnnotations(Ruleset.class);
- xstream.processAnnotations(Rule.class);
+ xstream.processAnnotations(PmdRuleset.class);
+ xstream.processAnnotations(PmdRule.class);
xstream.processAnnotations(Property.class);
return xstream.toXML(tree);
}
@@ -110,17 +110,17 @@ public class PmdRulesRepository extends AbstractImportableRulesRepository<Java, return header + xmlModules;
}
- protected Ruleset buildModuleTreeFromXml(String configuration) {
+ protected PmdRuleset buildModuleTreeFromXml(String configuration) {
InputStream inputStream = null;
try {
XStream xstream = new XStream();
xstream.setClassLoader(getClass().getClassLoader());
- xstream.processAnnotations(Ruleset.class);
+ xstream.processAnnotations(PmdRuleset.class);
xstream.processAnnotations(org.sonar.api.rules.Rule.class);
xstream.processAnnotations(Property.class);
inputStream = IOUtils.toInputStream(configuration, CharEncoding.UTF_8);
- return (Ruleset) xstream.fromXML(inputStream);
+ return (PmdRuleset) xstream.fromXML(inputStream);
}
catch (IOException e) {
@@ -132,9 +132,9 @@ public class PmdRulesRepository extends AbstractImportableRulesRepository<Java, }
}
- protected void buildActiveRulesFromModuleTree(Ruleset ruleset, List<ActiveRule> activeRules, List<org.sonar.api.rules.Rule> rules) {
+ protected void buildActiveRulesFromModuleTree(PmdRuleset ruleset, List<ActiveRule> activeRules, List<org.sonar.api.rules.Rule> rules) {
if (ruleset.getRules() != null && !ruleset.getRules().isEmpty()) {
- for (Rule rule : ruleset.getRules()) {
+ for (PmdRule rule : ruleset.getRules()) {
String ref = rule.getRef();
for (org.sonar.api.rules.Rule dbRule : rules) {
if (dbRule.getConfigKey().equals(ref)) {
@@ -149,7 +149,7 @@ public class PmdRulesRepository extends AbstractImportableRulesRepository<Java, }
}
- private List<ActiveRuleParam> getActiveRuleParams(Rule rule, org.sonar.api.rules.Rule dbRule, ActiveRule activeRule) {
+ private List<ActiveRuleParam> getActiveRuleParams(PmdRule rule, org.sonar.api.rules.Rule dbRule, ActiveRule activeRule) {
List<ActiveRuleParam> activeRuleParams = new ArrayList<ActiveRuleParam>();
if (rule.getProperties() != null) {
for (Property property : rule.getProperties()) {
diff --git a/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/xml/Rule.java b/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/xml/PmdRule.java index e2a2798e374..bf9774be315 100644 --- a/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/xml/Rule.java +++ b/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/xml/PmdRule.java @@ -27,7 +27,7 @@ import java.util.ArrayList; import java.util.List;
@XStreamAlias("rule")
-public class Rule implements Comparable<String> {
+public class PmdRule implements Comparable<String> {
@XStreamAsAttribute
private String ref;
@@ -49,11 +49,11 @@ public class Rule implements Comparable<String> { @XStreamAlias(value = "class")
private String clazz;//NOSONAR unused private field
- public Rule(String ref) {
+ public PmdRule(String ref) {
this(ref, null);
}
- public Rule(String ref, String priority) {
+ public PmdRule(String ref, String priority) {
this.ref = ref;
this.priority = priority;
}
diff --git a/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/xml/Ruleset.java b/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/xml/PmdRuleset.java index 3db02492d1e..01d70173a1a 100644 --- a/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/xml/Ruleset.java +++ b/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/xml/PmdRuleset.java @@ -27,12 +27,12 @@ import java.util.ArrayList; import java.util.List; @XStreamAlias("ruleset") -public class Ruleset { +public class PmdRuleset { private String description; @XStreamImplicit - private List<Rule> rules = new ArrayList<Rule>(); + private List<PmdRule> rules = new ArrayList<PmdRule>(); @XStreamOmitField @XStreamAlias(value = "exclude-pattern") @@ -42,18 +42,18 @@ public class Ruleset { @XStreamAlias(value = "include-pattern") private String includePattern;//NOSONAR unused private field - public Ruleset() { + public PmdRuleset() { } - public Ruleset(String description) { + public PmdRuleset(String description) { this.description = description; } - public List<Rule> getRules() { + public List<PmdRule> getRules() { return rules; } - public void setRules(List<Rule> rules) { + public void setRules(List<PmdRule> rules) { this.rules = rules; } @@ -65,7 +65,7 @@ public class Ruleset { this.description = description; } - public void addRule(Rule rule) { + public void addRule(PmdRule rule) { rules.add(rule); } diff --git a/plugins/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdProfileExporterTest.java b/plugins/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdProfileExporterTest.java new file mode 100644 index 00000000000..c83b01bf8a4 --- /dev/null +++ b/plugins/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdProfileExporterTest.java @@ -0,0 +1,81 @@ +package org.sonar.plugins.pmd; + +import java.io.IOException; +import java.io.InputStream; +import java.io.StringWriter; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import org.apache.commons.io.IOUtils; +import org.junit.Test; +import org.sonar.api.CoreProperties; +import org.sonar.api.profiles.RulesProfile; +import org.sonar.api.rules.ActiveRule; +import org.sonar.api.rules.ActiveRuleParam; +import org.sonar.api.rules.Rule; +import org.sonar.api.rules.RuleParam; +import org.sonar.api.rules.RulePriority; +import org.sonar.test.TestUtils; +import org.xml.sax.SAXException; + +public class PmdProfileExporterTest { + + private PmdProfileExporter exporter = new PmdProfileExporter(); + + @Test + public void testExportProfile() throws IOException, SAXException { + List<ActiveRule> activeRulesExpected = buildActiveRulesFixture(buildRulesFixture()); + RulesProfile activeProfile = new RulesProfile(); + activeProfile.setActiveRules(activeRulesExpected); + activeProfile.setName("A test profile"); + StringWriter xmlOutput = new StringWriter(); + exporter.exportProfile(activeProfile, xmlOutput); + assertXmlAreSimilar(xmlOutput.toString(), "test_xml_complete.xml"); + } + + private List<org.sonar.api.rules.Rule> buildRulesFixture() { + final Rule rule1 = new Rule("Coupling Between Objects", "CouplingBetweenObjects", + "rulesets/coupling.xml/CouplingBetweenObjects", null, CoreProperties.PMD_PLUGIN, null); + RuleParam ruleParam1 = new RuleParam(rule1, "threshold", null, "i"); + rule1.setParams(Arrays.asList(ruleParam1)); + + final Rule rule2 = new Rule("Excessive Imports", "ExcessiveImports", + "rulesets/coupling.xml/ExcessiveImports", null, CoreProperties.PMD_PLUGIN, null); + RuleParam ruleParam2 = new RuleParam(rule2, "max", null, "i"); + rule2.setParams(Arrays.asList(ruleParam2)); + + final Rule rule3 = new Rule("Use Notify All Instead Of Notify", "UseNotifyAllInsteadOfNotify", + "rulesets/design.xml/UseNotifyAllInsteadOfNotify", null, CoreProperties.PMD_PLUGIN, null); + + final org.sonar.api.rules.Rule rule4 = new org.sonar.api.rules.Rule("Class names should always begin with an upper case character.", + "ClassNamingConventions", + "rulesets/naming.xml/ClassNamingConventions", null, CoreProperties.PMD_PLUGIN, null); + + return Arrays.asList(rule1, rule2, rule3, rule4); + } + + private List<ActiveRule> buildActiveRulesFixture(List<org.sonar.api.rules.Rule> rules) { + List<ActiveRule> activeRules = new ArrayList<ActiveRule>(); + + ActiveRule activeRule1 = new ActiveRule(null, rules.get(0), RulePriority.CRITICAL); + activeRule1.setActiveRuleParams(Arrays.asList(new ActiveRuleParam(activeRule1, rules.get(0).getParams().get(0), "20"))); + activeRules.add(activeRule1); + + ActiveRule activeRule2 = new ActiveRule(null, rules.get(1), RulePriority.MAJOR); + activeRule2.setActiveRuleParams(Arrays.asList(new ActiveRuleParam(activeRule2, rules.get(1).getParams().get(0), "30"))); + activeRules.add(activeRule2); + + ActiveRule activeRule3 = new ActiveRule(null, rules.get(2), RulePriority.MINOR); + activeRules.add(activeRule3); + + return activeRules; + } + + private void assertXmlAreSimilar(String xml, String xmlFileToFind) throws IOException, SAXException { + InputStream input = getClass().getResourceAsStream("/org/sonar/plugins/pmd/" + xmlFileToFind); + String xmlToFind = IOUtils.toString(input); + TestUtils.assertSimilarXml(xmlToFind, xml); + } + +} diff --git a/plugins/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdRuleRepositoryTest.java b/plugins/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdRuleRepositoryTest.java new file mode 100644 index 00000000000..ac55ffc35f9 --- /dev/null +++ b/plugins/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdRuleRepositoryTest.java @@ -0,0 +1,42 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2009 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * Sonar is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * Sonar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.plugins.pmd; + +import static org.hamcrest.Matchers.greaterThan; +import static org.junit.Assert.assertThat; +import static org.mockito.Mockito.mock; + +import java.util.List; + +import org.junit.Test; +import org.sonar.api.platform.ServerFileSystem; +import org.sonar.api.rules.Rule; + +public class PmdRuleRepositoryTest { + + @Test + public void testLoadRepositoryFromXml() { + ServerFileSystem fileSystem = mock(ServerFileSystem.class); + PmdRuleRepository repository = new PmdRuleRepository(fileSystem); + List<Rule> rules = repository.createRules(); + assertThat(rules.size(), greaterThan(100)); + } + +} diff --git a/plugins/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdRulesRepositoryTest.java b/plugins/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdRulesRepositoryTest.java index 3296ff4a564..8c1f4e1bf19 100644 --- a/plugins/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdRulesRepositoryTest.java +++ b/plugins/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdRulesRepositoryTest.java @@ -32,8 +32,8 @@ import org.sonar.api.rules.ActiveRuleParam; import org.sonar.api.rules.RuleParam; import org.sonar.api.rules.RulePriority; import org.sonar.plugins.pmd.xml.Property; -import org.sonar.plugins.pmd.xml.Rule; -import org.sonar.plugins.pmd.xml.Ruleset; +import org.sonar.plugins.pmd.xml.PmdRule; +import org.sonar.plugins.pmd.xml.PmdRuleset; import org.sonar.test.TestUtils; import org.xml.sax.SAXException; @@ -87,11 +87,11 @@ public class PmdRulesRepositoryTest { ActiveRule activeRule = new ActiveRule(null, dbRule, RulePriority.MAJOR); activeRule.setActiveRuleParams(Arrays.asList(new ActiveRuleParam(activeRule, ruleParam, "Connection,Statement,ResultSet"))); - Ruleset ruleset = repository.buildModuleTree(Arrays.asList(activeRule)); + PmdRuleset ruleset = repository.buildModuleTree(Arrays.asList(activeRule)); assertThat(ruleset.getRules().size(), is(1)); - Rule rule = ruleset.getRules().get(0); + PmdRule rule = ruleset.getRules().get(0); assertThat(rule.getRef(), is("rulesets/design.xml/CloseResource")); assertThat(rule.getProperties().size(), is(1)); @@ -114,7 +114,7 @@ public class PmdRulesRepositoryTest { rule2.setConfigKey("rulesets/braces.xml/IfElseStmtsMustUseBraces"); ActiveRule activeRule2 = new ActiveRule(null, rule2, RulePriority.MAJOR); - Ruleset ruleset = repository.buildModuleTree(Arrays.asList(activeRule1, activeRule2)); + PmdRuleset ruleset = repository.buildModuleTree(Arrays.asList(activeRule1, activeRule2)); assertThat(ruleset.getRules().size(), is(2)); assertThat(ruleset.getRules().get(0).getRef(), is("rulesets/design.xml/CloseResource")); @@ -124,11 +124,11 @@ public class PmdRulesRepositoryTest { @Test public void shouldBuilModuleTreeFromXml() throws IOException { InputStream input = getClass().getResourceAsStream("/org/sonar/plugins/pmd/test_module_tree.xml"); - Ruleset ruleset = repository.buildModuleTreeFromXml(IOUtils.toString(input)); + PmdRuleset ruleset = repository.buildModuleTreeFromXml(IOUtils.toString(input)); assertThat(ruleset.getRules().size(), is(3)); - Rule rule1 = ruleset.getRules().get(0); + PmdRule rule1 = ruleset.getRules().get(0); assertThat(rule1.getRef(), is("rulesets/coupling.xml/CouplingBetweenObjects")); assertThat(rule1.getPriority(), is("2")); assertThat(rule1.getProperties().size(), is(1)); @@ -137,7 +137,7 @@ public class PmdRulesRepositoryTest { assertThat(module1Property.getName(), is("threshold")); assertThat(module1Property.getValue(), is("20")); - Rule rule2 = ruleset.getRules().get(1); + PmdRule rule2 = ruleset.getRules().get(1); assertThat(rule2.getRef(), is("rulesets/coupling.xml/ExcessiveImports")); assertThat(rule2.getPriority(), is("3")); assertThat(rule2.getProperties().size(), is(1)); @@ -146,7 +146,7 @@ public class PmdRulesRepositoryTest { assertThat(module2Property.getName(), is("max")); assertThat(module2Property.getValue(), is("30")); - Rule rule3 = ruleset.getRules().get(2); + PmdRule rule3 = ruleset.getRules().get(2); assertThat(rule3.getRef(), is("rulesets/design.xml/UseNotifyAllInsteadOfNotify")); assertThat(rule3.getPriority(), is("4")); assertNull(rule3.getProperties()); @@ -155,16 +155,16 @@ public class PmdRulesRepositoryTest { @Test public void shouldBuilModuleTreeFromXmlInUtf8() throws IOException { InputStream input = getClass().getResourceAsStream("/org/sonar/plugins/pmd/test_xml_utf8.xml"); - Ruleset ruleset = repository.buildModuleTreeFromXml(IOUtils.toString(input, CharEncoding.UTF_8)); + PmdRuleset ruleset = repository.buildModuleTreeFromXml(IOUtils.toString(input, CharEncoding.UTF_8)); - Rule rule1 = ruleset.getRules().get(0); + PmdRule rule1 = ruleset.getRules().get(0); assertThat(rule1.getRef(), is("rulesets/coupling.xml/CouplingBetweenObjects")); assertThat(rule1.getProperties().get(0).getValue(), is("\u00E9")); } @Test public void shouldBuilXmlFromModuleTree() throws IOException, SAXException { - Ruleset ruleset = buildModuleTreeFixture(); + PmdRuleset ruleset = buildModuleTreeFixture(); String xml = repository.buildXmlFromModuleTree(ruleset); assertXmlAreSimilar(xml, "test_module_tree.xml"); } @@ -221,7 +221,7 @@ public class PmdRulesRepositoryTest { List<ActiveRule> activeRulesExpected = buildActiveRulesFixture(inputRules); List<ActiveRule> activeRules = new ArrayList<ActiveRule>(); - Ruleset ruleset = buildModuleTreeFixture(); + PmdRuleset ruleset = buildModuleTreeFixture(); repository.buildActiveRulesFromModuleTree(ruleset, activeRules, inputRules); assertThat(activeRulesExpected.size(), is(activeRules.size())); @@ -246,15 +246,6 @@ public class PmdRulesRepositoryTest { assertTrue(profile3.getActiveRules().size() + "", profile3.getActiveRules().size() > 1); } - @Test - public void shouldExportConfiguration() throws IOException, SAXException { - List<ActiveRule> activeRulesExpected = buildActiveRulesFixture(buildRulesFixture()); - RulesProfile activeProfile = new RulesProfile(); - activeProfile.setActiveRules(activeRulesExpected); - activeProfile.setName("A test profile"); - String xml = repository.exportConfiguration(activeProfile); - assertXmlAreSimilar(xml, "test_xml_complete.xml"); - } @Test public void shouldAddHeaderToXml() throws IOException, SAXException { @@ -271,13 +262,13 @@ public class PmdRulesRepositoryTest { rule2.setPluginName("not-a-pmd-plugin"); ActiveRule activeRule2 = new ActiveRule(null, rule1, RulePriority.CRITICAL); - Ruleset tree = repository.buildModuleTree(Arrays.asList(activeRule1, activeRule2)); + PmdRuleset tree = repository.buildModuleTree(Arrays.asList(activeRule1, activeRule2)); assertThat(tree.getRules().size(), is(0)); } @Test public void shouldBuildOnlyOneModuleWhenNoActiveRules() { - Ruleset tree = repository.buildModuleTree(Collections.<ActiveRule>emptyList()); + PmdRuleset tree = repository.buildModuleTree(Collections.<ActiveRule>emptyList()); assertThat(tree.getRules().size(), is(0)); } @@ -292,13 +283,13 @@ public class PmdRulesRepositoryTest { dbRule2.setConfigKey("rulesets/coupling.xml/CouplingBetweenObjects"); ActiveRule activeRule2 = new ActiveRule(null, dbRule2, RulePriority.CRITICAL); - Ruleset tree = repository.buildModuleTree(Arrays.asList(activeRule1, activeRule2)); + PmdRuleset tree = repository.buildModuleTree(Arrays.asList(activeRule1, activeRule2)); assertThat(tree.getRules().size(), is(2)); - Rule rule1 = tree.getRules().get(0); + PmdRule rule1 = tree.getRules().get(0); assertThat(rule1.getRef(), is("rulesets/coupling.xml/CouplingBetweenObjects")); - Rule rule2 = tree.getRules().get(1); + PmdRule rule2 = tree.getRules().get(1); assertThat(rule2.getRef(), is("rulesets/coupling.xml/CouplingBetweenObjects")); } @@ -312,19 +303,19 @@ public class PmdRulesRepositoryTest { TestUtils.assertSimilarXml(xmlToFind, xml); } - private Ruleset buildModuleTreeFixture() { - Ruleset ruleset = new Ruleset(); + private PmdRuleset buildModuleTreeFixture() { + PmdRuleset ruleset = new PmdRuleset(); ruleset.setDescription("Sonar PMD rules"); - Rule rule1 = new Rule("rulesets/coupling.xml/CouplingBetweenObjects", "2"); + PmdRule rule1 = new PmdRule("rulesets/coupling.xml/CouplingBetweenObjects", "2"); rule1.addProperty(new Property("threshold", "20")); ruleset.addRule(rule1); - Rule rule2 = new Rule("rulesets/coupling.xml/ExcessiveImports", "3"); + PmdRule rule2 = new PmdRule("rulesets/coupling.xml/ExcessiveImports", "3"); rule2.addProperty(new Property("max", "30")); ruleset.addRule(rule2); - Rule rule3 = new Rule("rulesets/design.xml/UseNotifyAllInsteadOfNotify", "4"); + PmdRule rule3 = new PmdRule("rulesets/design.xml/UseNotifyAllInsteadOfNotify", "4"); ruleset.addRule(rule3); return ruleset; @@ -398,7 +389,7 @@ public class PmdRulesRepositoryTest { @Test public void shouldUseTheProfileNameWhenBuildingTheRulesSet() { - Ruleset tree = repository.buildModuleTree(Collections.<ActiveRule>emptyList(), "aprofile"); + PmdRuleset tree = repository.buildModuleTree(Collections.<ActiveRule>emptyList(), "aprofile"); assertThat(tree.getDescription(), is("aprofile")); } |