diff options
author | simonbrandhof <simon.brandhof@gmail.com> | 2010-10-08 15:48:07 +0000 |
---|---|---|
committer | simonbrandhof <simon.brandhof@gmail.com> | 2010-10-08 15:48:07 +0000 |
commit | fc24fe199eb5968ab7adb26fd23f650e4a8f8a9e (patch) | |
tree | 59332c8e45a7b6184cc602756c6c193898e62d94 /plugins | |
parent | 7ca495d67492c6b3dbb653102e345f8fc43f09ae (diff) | |
download | sonarqube-fc24fe199eb5968ab7adb26fd23f650e4a8f8a9e.tar.gz sonarqube-fc24fe199eb5968ab7adb26fd23f650e4a8f8a9e.zip |
API: apply the same pattern between rules and profiles API. The extension point to define rules is org.sonar.api.rules.RuleRepository. The classes AnnotationRuleParser and XMLRuleParser are components that can be used but not extended.
Diffstat (limited to 'plugins')
9 files changed, 29 insertions, 25 deletions
diff --git a/plugins/sonar-checkstyle-plugin/src/main/java/org/sonar/plugins/checkstyle/CheckstyleRuleRepository.java b/plugins/sonar-checkstyle-plugin/src/main/java/org/sonar/plugins/checkstyle/CheckstyleRuleRepository.java index ee533a1cffa..02dd0ad885c 100644 --- a/plugins/sonar-checkstyle-plugin/src/main/java/org/sonar/plugins/checkstyle/CheckstyleRuleRepository.java +++ b/plugins/sonar-checkstyle-plugin/src/main/java/org/sonar/plugins/checkstyle/CheckstyleRuleRepository.java @@ -33,19 +33,21 @@ public final class CheckstyleRuleRepository extends RuleRepository { // for user extensions private ServerFileSystem fileSystem; + private XMLRuleParser xmlRuleParser; - public CheckstyleRuleRepository(ServerFileSystem fileSystem) { + public CheckstyleRuleRepository(ServerFileSystem fileSystem, XMLRuleParser xmlRuleParser) { super(CheckstyleConstants.REPOSITORY_KEY, Java.KEY); setName(CheckstyleConstants.REPOSITORY_NAME); this.fileSystem = fileSystem; + this.xmlRuleParser = xmlRuleParser; } @Override public List<Rule> createRules() { List<Rule> rules = new ArrayList<Rule>(); - rules.addAll(XMLRuleParser.parseXML(getClass().getResourceAsStream("/org/sonar/plugins/checkstyle/rules.xml"))); + rules.addAll(xmlRuleParser.parse(getClass().getResourceAsStream("/org/sonar/plugins/checkstyle/rules.xml"))); for (File userExtensionXml : fileSystem.getExtensions(CheckstyleConstants.REPOSITORY_KEY, "xml")) { - rules.addAll(XMLRuleParser.parseXML(userExtensionXml)); + rules.addAll(xmlRuleParser.parse(userExtensionXml)); } return rules; } diff --git a/plugins/sonar-checkstyle-plugin/src/test/java/org/sonar/plugins/checkstyle/CheckstyleRuleRepositoryTest.java b/plugins/sonar-checkstyle-plugin/src/test/java/org/sonar/plugins/checkstyle/CheckstyleRuleRepositoryTest.java index 71d29f5c572..a501feb7308 100644 --- a/plugins/sonar-checkstyle-plugin/src/test/java/org/sonar/plugins/checkstyle/CheckstyleRuleRepositoryTest.java +++ b/plugins/sonar-checkstyle-plugin/src/test/java/org/sonar/plugins/checkstyle/CheckstyleRuleRepositoryTest.java @@ -22,6 +22,7 @@ package org.sonar.plugins.checkstyle; import org.junit.Test; import org.sonar.api.platform.ServerFileSystem; import org.sonar.api.rules.Rule; +import org.sonar.api.rules.XMLRuleParser; import java.util.List; @@ -34,7 +35,7 @@ public class CheckstyleRuleRepositoryTest { @Test public void loadRepositoryFromXml() { ServerFileSystem fileSystem = mock(ServerFileSystem.class); - CheckstyleRuleRepository repository = new CheckstyleRuleRepository(fileSystem); + CheckstyleRuleRepository repository = new CheckstyleRuleRepository(fileSystem, new XMLRuleParser()); List<Rule> rules = repository.createRules(); assertThat(rules.size(), greaterThan(100)); } diff --git a/plugins/sonar-findbugs-plugin/src/main/java/org/sonar/plugins/findbugs/FindbugsRuleRepository.java b/plugins/sonar-findbugs-plugin/src/main/java/org/sonar/plugins/findbugs/FindbugsRuleRepository.java index 0f43b2cd4d2..38150ecead9 100644 --- a/plugins/sonar-findbugs-plugin/src/main/java/org/sonar/plugins/findbugs/FindbugsRuleRepository.java +++ b/plugins/sonar-findbugs-plugin/src/main/java/org/sonar/plugins/findbugs/FindbugsRuleRepository.java @@ -28,13 +28,16 @@ import java.util.List; public final class FindbugsRuleRepository extends RuleRepository { - public FindbugsRuleRepository() { + private XMLRuleParser xmlRuleParser; + + public FindbugsRuleRepository(XMLRuleParser xmlRuleParser) { super(FindbugsConstants.REPOSITORY_KEY, Java.KEY); setName(FindbugsConstants.REPOSITORY_NAME); + this.xmlRuleParser = xmlRuleParser; } @Override public List<Rule> createRules() { - return XMLRuleParser.parseXML(getClass().getResourceAsStream("/org/sonar/plugins/findbugs/rules.xml")); + return xmlRuleParser.parse(getClass().getResourceAsStream("/org/sonar/plugins/findbugs/rules.xml")); } } diff --git a/plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FindbugsRuleFinder.java b/plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FindbugsRuleFinder.java index 44c279b4f6e..ab3af9d93a6 100644 --- a/plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FindbugsRuleFinder.java +++ b/plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FindbugsRuleFinder.java @@ -25,13 +25,14 @@ import java.util.List; import org.sonar.api.rules.Rule; import org.sonar.api.rules.RuleFinder; import org.sonar.api.rules.RuleQuery; +import org.sonar.api.rules.XMLRuleParser; public class FindbugsRuleFinder implements RuleFinder { private final List<Rule> findbugsRules; public FindbugsRuleFinder() { - FindbugsRuleRepository repo = new FindbugsRuleRepository(); + FindbugsRuleRepository repo = new FindbugsRuleRepository(new XMLRuleParser()); findbugsRules = repo.createRules(); for(Rule rule : findbugsRules){ rule.setRepositoryKey(FindbugsConstants.REPOSITORY_KEY); diff --git a/plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FindbugsRuleRepositoryTest.java b/plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FindbugsRuleRepositoryTest.java index d8c9ff8a32c..511232b7a6d 100644 --- a/plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FindbugsRuleRepositoryTest.java +++ b/plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FindbugsRuleRepositoryTest.java @@ -27,12 +27,13 @@ import java.util.List; import org.junit.Test; import org.sonar.api.rules.Rule; +import org.sonar.api.rules.XMLRuleParser; public class FindbugsRuleRepositoryTest { @Test public void testLoadRepositoryFromXml() { - FindbugsRuleRepository repository = new FindbugsRuleRepository(); + FindbugsRuleRepository repository = new FindbugsRuleRepository(new XMLRuleParser()); List<Rule> rules = repository.createRules(); assertThat(rules.size(), greaterThan(300)); for (Rule rule : rules) { diff --git a/plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FindbugsTests.java b/plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FindbugsTests.java index e051e7c52a7..f805a005459 100644 --- a/plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FindbugsTests.java +++ b/plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FindbugsTests.java @@ -35,11 +35,7 @@ import org.mockito.stubbing.Answer; import org.sonar.api.CoreProperties;
import org.sonar.api.profiles.RulesProfile;
import org.sonar.api.resources.Java;
-import org.sonar.api.rules.ActiveRule;
-import org.sonar.api.rules.Rule;
-import org.sonar.api.rules.RulePriority;
-import org.sonar.api.rules.RuleQuery;
-import org.sonar.api.rules.RulesManager;
+import org.sonar.api.rules.*;
import org.sonar.test.TestUtils;
import org.xml.sax.SAXException;
@@ -93,7 +89,7 @@ public abstract class FindbugsTests { RulesProfile profile = RulesProfile.create();
profile.setName(RulesProfile.SONAR_WAY_FINDBUGS_NAME);
profile.setLanguage(Java.KEY);
- for (Rule rule : new FindbugsRuleRepository().createRules()) {
+ for (Rule rule : new FindbugsRuleRepository(new XMLRuleParser()).createRules()) {
rule.setRepositoryKey(FindbugsConstants.REPOSITORY_KEY);
profile.activateRule(rule, null);
}
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 index 19214409b08..04fe1fdfe76 100644 --- 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 @@ -19,6 +19,7 @@ */ package org.sonar.plugins.pmd; +import com.google.common.collect.Lists; import org.sonar.api.platform.ServerFileSystem; import org.sonar.api.resources.Java; import org.sonar.api.rules.Rule; @@ -26,26 +27,27 @@ import org.sonar.api.rules.RuleRepository; import org.sonar.api.rules.XMLRuleParser; import java.io.File; -import java.util.ArrayList; import java.util.List; public final class PmdRuleRepository extends RuleRepository { // for user extensions private ServerFileSystem fileSystem; + private XMLRuleParser xmlRuleParser; - public PmdRuleRepository(ServerFileSystem fileSystem) { + public PmdRuleRepository(ServerFileSystem fileSystem, XMLRuleParser xmlRuleParser) { super(PmdConstants.REPOSITORY_KEY, Java.KEY); setName(PmdConstants.REPOSITORY_NAME); this.fileSystem = fileSystem; + this.xmlRuleParser = xmlRuleParser; } @Override public List<Rule> createRules() { - List<Rule> rules = new ArrayList<Rule>(); - rules.addAll(XMLRuleParser.parseXML(getClass().getResourceAsStream("/org/sonar/plugins/pmd/rules.xml"))); + List<Rule> rules = Lists.newArrayList(); + rules.addAll(xmlRuleParser.parse(getClass().getResourceAsStream("/org/sonar/plugins/pmd/rules.xml"))); for (File userExtensionXml : fileSystem.getExtensions(PmdConstants.REPOSITORY_KEY, "xml")) { - rules.addAll(XMLRuleParser.parseXML(userExtensionXml)); + rules.addAll(xmlRuleParser.parse(userExtensionXml)); } return rules; } 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 index d6189e13196..438bb9d3c4d 100644 --- 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 @@ -17,10 +17,7 @@ import org.apache.commons.lang.StringUtils; import org.junit.Test; import org.sonar.api.platform.ServerFileSystem; import org.sonar.api.profiles.RulesProfile; -import org.sonar.api.rules.ActiveRule; -import org.sonar.api.rules.Rule; -import org.sonar.api.rules.RuleFinder; -import org.sonar.api.rules.RuleQuery; +import org.sonar.api.rules.*; import org.sonar.api.utils.ValidationMessages; import org.sonar.plugins.pmd.xml.PmdProperty; import org.sonar.plugins.pmd.xml.PmdRule; @@ -34,7 +31,7 @@ public class PmdProfileExporterTest { @Test public void testExportProfile() throws IOException, SAXException { ServerFileSystem fileSystem = mock(ServerFileSystem.class); - PmdRuleRepository repository = new PmdRuleRepository(fileSystem); + PmdRuleRepository repository = new PmdRuleRepository(fileSystem, new XMLRuleParser()); List<Rule> rules = repository.createRules(); RuleFinder ruleFinder = new PmdRuleFinder(rules); 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 index ac55ffc35f9..d726c28564e 100644 --- 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 @@ -28,13 +28,14 @@ import java.util.List; import org.junit.Test; import org.sonar.api.platform.ServerFileSystem; import org.sonar.api.rules.Rule; +import org.sonar.api.rules.XMLRuleParser; public class PmdRuleRepositoryTest { @Test public void testLoadRepositoryFromXml() { ServerFileSystem fileSystem = mock(ServerFileSystem.class); - PmdRuleRepository repository = new PmdRuleRepository(fileSystem); + PmdRuleRepository repository = new PmdRuleRepository(fileSystem, new XMLRuleParser()); List<Rule> rules = repository.createRules(); assertThat(rules.size(), greaterThan(100)); } |