From da66ba341527c6cc9c9073664f72c7c8c9e374c5 Mon Sep 17 00:00:00 2001 From: fmallet Date: Sun, 12 Sep 2010 19:51:23 +0000 Subject: SONAR-1766 - start migrating to the new Sonar rule API before defining the PMD XPath rule --- plugins/sonar-pmd-plugin/pmd-result.xml | 3 + .../java/org/sonar/plugins/pmd/PmdConstants.java | 30 ++++++ .../java/org/sonar/plugins/pmd/PmdExecutor.java | 2 +- .../main/java/org/sonar/plugins/pmd/PmdPlugin.java | 9 +- .../org/sonar/plugins/pmd/PmdProfileExporter.java | 108 +++++++++++++++++++++ .../org/sonar/plugins/pmd/PmdRuleRepository.java | 52 ++++++++++ .../org/sonar/plugins/pmd/PmdRulesRepository.java | 34 +++---- .../java/org/sonar/plugins/pmd/xml/PmdRule.java | 92 ++++++++++++++++++ .../java/org/sonar/plugins/pmd/xml/PmdRuleset.java | 72 ++++++++++++++ .../main/java/org/sonar/plugins/pmd/xml/Rule.java | 92 ------------------ .../java/org/sonar/plugins/pmd/xml/Ruleset.java | 72 -------------- .../sonar/plugins/pmd/PmdProfileExporterTest.java | 81 ++++++++++++++++ .../sonar/plugins/pmd/PmdRuleRepositoryTest.java | 42 ++++++++ .../sonar/plugins/pmd/PmdRulesRepositoryTest.java | 57 +++++------ 14 files changed, 526 insertions(+), 220 deletions(-) create mode 100644 plugins/sonar-pmd-plugin/pmd-result.xml create mode 100644 plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/PmdConstants.java create mode 100644 plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/PmdProfileExporter.java create mode 100644 plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/PmdRuleRepository.java create mode 100644 plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/xml/PmdRule.java create mode 100644 plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/xml/PmdRuleset.java delete mode 100644 plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/xml/Rule.java delete mode 100644 plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/xml/Ruleset.java create mode 100644 plugins/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdProfileExporterTest.java create mode 100644 plugins/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdRuleRepositoryTest.java 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 @@ + + + \ 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 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 properties = null; + if (activeRule.getActiveRuleParams() != null && !activeRule.getActiveRuleParams().isEmpty()) { + properties = new ArrayList(); + 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 createRules() { + List rules = new ArrayList(); + 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 importConfiguration(String configuration, List rules) { List activeRules = new ArrayList(); - Ruleset moduleTree = buildModuleTreeFromXml(configuration); + PmdRuleset moduleTree = buildModuleTreeFromXml(configuration); buildActiveRulesFromModuleTree(moduleTree, activeRules, rules); return activeRules; } - protected Ruleset buildModuleTree(List activeRules) { + protected PmdRuleset buildModuleTree(List activeRules) { return buildModuleTree(activeRules, "Sonar PMD rules"); } - protected Ruleset buildModuleTree(List activeRules, String profileName) { - Ruleset ruleset = new Ruleset(profileName); + protected PmdRuleset buildModuleTree(List 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 properties = null; if (activeRule.getActiveRuleParams() != null && !activeRule.getActiveRuleParams().isEmpty()) { properties = new ArrayList(); @@ -95,11 +95,11 @@ public class PmdRulesRepository extends AbstractImportableRulesRepository activeRules, List rules) { + protected void buildActiveRulesFromModuleTree(PmdRuleset ruleset, List activeRules, List 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 getActiveRuleParams(Rule rule, org.sonar.api.rules.Rule dbRule, ActiveRule activeRule) { + private List getActiveRuleParams(PmdRule rule, org.sonar.api.rules.Rule dbRule, ActiveRule activeRule) { List activeRuleParams = new ArrayList(); if (rule.getProperties() != null) { for (Property property : rule.getProperties()) { diff --git a/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/xml/PmdRule.java b/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/xml/PmdRule.java new file mode 100644 index 00000000000..bf9774be315 --- /dev/null +++ b/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/xml/PmdRule.java @@ -0,0 +1,92 @@ +/* + * 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.xml; + +import com.thoughtworks.xstream.annotations.XStreamAlias; +import com.thoughtworks.xstream.annotations.XStreamAsAttribute; +import com.thoughtworks.xstream.annotations.XStreamOmitField; + +import java.util.ArrayList; +import java.util.List; + +@XStreamAlias("rule") +public class PmdRule implements Comparable { + + @XStreamAsAttribute + private String ref; + + private String priority; + + private List properties; + + @XStreamOmitField + private String description; //NOSONAR unused private field + + @XStreamOmitField + private String exclude;//NOSONAR unused private field + + @XStreamOmitField + private String example;//NOSONAR unused private field + + @XStreamOmitField + @XStreamAlias(value = "class") + private String clazz;//NOSONAR unused private field + + public PmdRule(String ref) { + this(ref, null); + } + + public PmdRule(String ref, String priority) { + this.ref = ref; + this.priority = priority; + } + + public String getRef() { + return ref; + } + + public void setProperties(List properties) { + this.properties = properties; + } + + public List getProperties() { + return properties; + } + + public int compareTo(String o) { + return o.compareTo(ref); + } + + public String getPriority() { + return priority; + } + + public void setPriority(String priority) { + this.priority = priority; + } + + public void addProperty(Property property) { + if (properties == null) { + properties = new ArrayList(); + } + properties.add(property); + } + +} \ No newline at end of file diff --git a/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/xml/PmdRuleset.java b/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/xml/PmdRuleset.java new file mode 100644 index 00000000000..01d70173a1a --- /dev/null +++ b/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/xml/PmdRuleset.java @@ -0,0 +1,72 @@ +/* + * 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.xml; + +import com.thoughtworks.xstream.annotations.XStreamAlias; +import com.thoughtworks.xstream.annotations.XStreamImplicit; +import com.thoughtworks.xstream.annotations.XStreamOmitField; + +import java.util.ArrayList; +import java.util.List; + +@XStreamAlias("ruleset") +public class PmdRuleset { + + private String description; + + @XStreamImplicit + private List rules = new ArrayList(); + + @XStreamOmitField + @XStreamAlias(value = "exclude-pattern") + private String excludePattern;//NOSONAR unused private field + + @XStreamOmitField + @XStreamAlias(value = "include-pattern") + private String includePattern;//NOSONAR unused private field + + public PmdRuleset() { + } + + public PmdRuleset(String description) { + this.description = description; + } + + public List getRules() { + return rules; + } + + public void setRules(List rules) { + this.rules = rules; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public void addRule(PmdRule rule) { + rules.add(rule); + } + +} 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/Rule.java deleted file mode 100644 index e2a2798e374..00000000000 --- a/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/xml/Rule.java +++ /dev/null @@ -1,92 +0,0 @@ -/* - * 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.xml; - -import com.thoughtworks.xstream.annotations.XStreamAlias; -import com.thoughtworks.xstream.annotations.XStreamAsAttribute; -import com.thoughtworks.xstream.annotations.XStreamOmitField; - -import java.util.ArrayList; -import java.util.List; - -@XStreamAlias("rule") -public class Rule implements Comparable { - - @XStreamAsAttribute - private String ref; - - private String priority; - - private List properties; - - @XStreamOmitField - private String description; //NOSONAR unused private field - - @XStreamOmitField - private String exclude;//NOSONAR unused private field - - @XStreamOmitField - private String example;//NOSONAR unused private field - - @XStreamOmitField - @XStreamAlias(value = "class") - private String clazz;//NOSONAR unused private field - - public Rule(String ref) { - this(ref, null); - } - - public Rule(String ref, String priority) { - this.ref = ref; - this.priority = priority; - } - - public String getRef() { - return ref; - } - - public void setProperties(List properties) { - this.properties = properties; - } - - public List getProperties() { - return properties; - } - - public int compareTo(String o) { - return o.compareTo(ref); - } - - public String getPriority() { - return priority; - } - - public void setPriority(String priority) { - this.priority = priority; - } - - public void addProperty(Property property) { - if (properties == null) { - properties = new ArrayList(); - } - properties.add(property); - } - -} \ No newline at end of file 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/Ruleset.java deleted file mode 100644 index 3db02492d1e..00000000000 --- a/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/xml/Ruleset.java +++ /dev/null @@ -1,72 +0,0 @@ -/* - * 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.xml; - -import com.thoughtworks.xstream.annotations.XStreamAlias; -import com.thoughtworks.xstream.annotations.XStreamImplicit; -import com.thoughtworks.xstream.annotations.XStreamOmitField; - -import java.util.ArrayList; -import java.util.List; - -@XStreamAlias("ruleset") -public class Ruleset { - - private String description; - - @XStreamImplicit - private List rules = new ArrayList(); - - @XStreamOmitField - @XStreamAlias(value = "exclude-pattern") - private String excludePattern;//NOSONAR unused private field - - @XStreamOmitField - @XStreamAlias(value = "include-pattern") - private String includePattern;//NOSONAR unused private field - - public Ruleset() { - } - - public Ruleset(String description) { - this.description = description; - } - - public List getRules() { - return rules; - } - - public void setRules(List rules) { - this.rules = rules; - } - - public String getDescription() { - return description; - } - - public void setDescription(String description) { - this.description = description; - } - - public void addRule(Rule 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 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 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 buildActiveRulesFixture(List rules) { + List activeRules = new ArrayList(); + + 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 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 activeRulesExpected = buildActiveRulesFixture(inputRules); List activeRules = new ArrayList(); - 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 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.emptyList()); + PmdRuleset tree = repository.buildModuleTree(Collections.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.emptyList(), "aprofile"); + PmdRuleset tree = repository.buildModuleTree(Collections.emptyList(), "aprofile"); assertThat(tree.getDescription(), is("aprofile")); } -- cgit v1.2.3