diff options
author | Simon Brandhof <simon.brandhof@gmail.com> | 2014-01-27 23:27:51 +0100 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@gmail.com> | 2014-01-27 23:28:03 +0100 |
commit | 4e5a1b8ea2fa37ac5064f7010d70ec2c9926c18b (patch) | |
tree | 148219f8f40b3981a1c72318139b7266a52f82d3 /plugins/sonar-xoo-plugin | |
parent | 36805421f21c3dbc2b0638d27ff9ece3d2cf3156 (diff) | |
download | sonarqube-4e5a1b8ea2fa37ac5064f7010d70ec2c9926c18b.tar.gz sonarqube-4e5a1b8ea2fa37ac5064f7010d70ec2c9926c18b.zip |
Refactor Xoo plugin
Diffstat (limited to 'plugins/sonar-xoo-plugin')
-rw-r--r-- | plugins/sonar-xoo-plugin/pom.xml | 11 | ||||
-rw-r--r-- | plugins/sonar-xoo-plugin/src/main/java/org/sonar/plugins/xoo/rules/XooRuleDefinitions.java | 88 | ||||
-rw-r--r-- | plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/Xoo.java (renamed from plugins/sonar-xoo-plugin/src/main/java/org/sonar/plugins/xoo/Xoo.java) | 14 | ||||
-rw-r--r-- | plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/XooPlugin.java (renamed from plugins/sonar-xoo-plugin/src/main/java/org/sonar/plugins/xoo/XooPlugin.java) | 18 | ||||
-rw-r--r-- | plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/package-info.java (renamed from plugins/sonar-xoo-plugin/src/main/java/org/sonar/plugins/xoo/package-info.java) | 2 | ||||
-rw-r--r-- | plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/rule/XooQualityProfile.java (renamed from plugins/sonar-xoo-plugin/src/main/java/org/sonar/plugins/xoo/XooProfile.java) | 11 | ||||
-rw-r--r-- | plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/rule/XooRuleDefinitions.java | 63 | ||||
-rw-r--r-- | plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/rule/package-info.java (renamed from plugins/sonar-xoo-plugin/src/main/java/org/sonar/plugins/xoo/base/package-info.java) | 2 | ||||
-rw-r--r-- | plugins/sonar-xoo-plugin/src/test/java/org/sonar/xoo/XooPluginTest.java (renamed from plugins/sonar-xoo-plugin/src/test/java/org/sonar/plugins/xoo/XooPluginTest.java) | 15 | ||||
-rw-r--r-- | plugins/sonar-xoo-plugin/src/test/java/org/sonar/xoo/rule/XooRuleDefinitionsTest.java (renamed from plugins/sonar-xoo-plugin/src/test/java/org/sonar/plugins/xoo/rules/XooRuleDefinitionsTest.java) | 25 |
10 files changed, 116 insertions, 133 deletions
diff --git a/plugins/sonar-xoo-plugin/pom.xml b/plugins/sonar-xoo-plugin/pom.xml index 239ace4bb2e..3c7e16bc6ee 100644 --- a/plugins/sonar-xoo-plugin/pom.xml +++ b/plugins/sonar-xoo-plugin/pom.xml @@ -11,7 +11,7 @@ <artifactId>sonar-xoo-plugin</artifactId> <name>SonarQube :: Plugins :: Xoo</name> <packaging>sonar-plugin</packaging> - <description>Fake language plugin for backend tests.</description> + <description>Sample of plugin to document and test available APIs</description> <dependencies> <dependency> @@ -24,6 +24,13 @@ <artifactId>sonar-plugin-api</artifactId> <scope>provided</scope> </dependency> + + <!-- unit testing --> + <dependency> + <groupId>org.easytesting</groupId> + <artifactId>fest-assert</artifactId> + <scope>test</scope> + </dependency> <dependency> <groupId>org.codehaus.sonar</groupId> <artifactId>sonar-testing-harness</artifactId> @@ -39,7 +46,7 @@ <configuration> <pluginKey>xoo</pluginKey> <pluginName>Xoo</pluginName> - <pluginClass>org.sonar.plugins.xoo.XooPlugin</pluginClass> + <pluginClass>org.sonar.xoo.XooPlugin</pluginClass> </configuration> </plugin> </plugins> diff --git a/plugins/sonar-xoo-plugin/src/main/java/org/sonar/plugins/xoo/rules/XooRuleDefinitions.java b/plugins/sonar-xoo-plugin/src/main/java/org/sonar/plugins/xoo/rules/XooRuleDefinitions.java deleted file mode 100644 index ba60b4bbbdc..00000000000 --- a/plugins/sonar-xoo-plugin/src/main/java/org/sonar/plugins/xoo/rules/XooRuleDefinitions.java +++ /dev/null @@ -1,88 +0,0 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2013 SonarSource - * mailto:contact AT sonarsource DOT com - * - * SonarQube 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. - * - * SonarQube 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 this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.plugins.xoo.rules; - -import org.sonar.api.server.rule.RuleDefinitions; -import org.sonar.api.server.rule.RuleParamType; -import org.sonar.plugins.xoo.base.XooConstants; -import org.sonar.plugins.xoo.base.XooRuleKeys; - -public class XooRuleDefinitions implements RuleDefinitions { - - private static final String TAG1 = "tag1", TAG2 = "tag2", TAG3 = "tag3", TAG4 = "tag4"; - - @Override - public void define(Context context) { - final NewRepository xooRepository = context.newRepository(XooConstants.REPOSITORY_KEY, XooConstants.LANGUAGE_KEY).setName("Xoo"); - - - xooRepository.newRule(XooRuleKeys.RULE_MINIMAL) - .setName("Minimal rule") - .setHtmlDescription("Minimal rule, with only required fields"); - - final NewRule ruleWithParams = xooRepository.newRule(XooRuleKeys.RULE_WITH_PARAMS) - .setName("Rule with parameters") - .setHtmlDescription("This rule defines parameters, one for each supported type"); - ruleWithParams.newParam("string") - .setName("String") - .setType(RuleParamType.STRING) - .setDescription("A string parameter"); - ruleWithParams.newParam("text") - .setName("Text") - .setType(RuleParamType.TEXT) - .setDescription("A text parameter"); - ruleWithParams.newParam("bool") - .setName("Boolean") - .setType(RuleParamType.BOOLEAN) - .setDescription("A boolean parameter"); - ruleWithParams.newParam("float") - .setName("Float") - .setType(RuleParamType.FLOAT) - .setDescription("A float parameter"); - ruleWithParams.newParam("int") - .setName("Integer") - .setType(RuleParamType.INTEGER) - .setDescription("An integer parameter"); - - xooRepository.newRule(XooRuleKeys.RULE_WITH_TAGS1) - .setName("Tag 1") - .setHtmlDescription("Rule with tag <code>tag1</code>") - .setTags(TAG1); - xooRepository.newRule(XooRuleKeys.RULE_WITH_TAGS12) - .setName("Tags 1 and 2") - .setHtmlDescription("Rule with tags <code>tag1</code> and <code>tag2</code>") - .setTags(TAG1, TAG2); - xooRepository.newRule(XooRuleKeys.RULE_WITH_TAGS123) - .setName("Tags 1, 2 and 3") - .setHtmlDescription("Rule with tags <code>tag1</code>, <code>tag2</code> and <code>tag3</code>") - .setTags(TAG1, TAG2, TAG3); - xooRepository.newRule(XooRuleKeys.RULE_WITH_TAGS23) - .setName("Tags 2 and 3") - .setHtmlDescription("Rule with tags <code>tag2</code> and <code>tag3</code>") - .setTags(TAG2, TAG3); - xooRepository.newRule(XooRuleKeys.RULE_WITH_TAGS34) - .setName("Tags 3 and 4") - .setHtmlDescription("Rule with tags <code>tag3</code> and <code>tag4</code>") - .setTags(TAG3, TAG4); - - xooRepository.done(); - } - -} diff --git a/plugins/sonar-xoo-plugin/src/main/java/org/sonar/plugins/xoo/Xoo.java b/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/Xoo.java index cb9fe5847ac..709efaa9d09 100644 --- a/plugins/sonar-xoo-plugin/src/main/java/org/sonar/plugins/xoo/Xoo.java +++ b/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/Xoo.java @@ -17,28 +17,28 @@ * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -package org.sonar.plugins.xoo; +package org.sonar.xoo; import org.sonar.api.resources.Language; -import org.sonar.plugins.xoo.base.XooConstants; public class Xoo implements Language { - public static final String XOO_LANGUAGE_NAME = "Xoo"; - public static final String XOO_SUFFIX = ".xoo"; + public static final String KEY = "xoo"; + public static final String NAME = "Xoo"; + public static final String FILE_SUFFIX = ".xoo"; private static final String[] XOO_SUFFIXES = { - XOO_SUFFIX + FILE_SUFFIX }; @Override public String getKey() { - return XooConstants.LANGUAGE_KEY; + return KEY; } @Override public String getName() { - return XOO_LANGUAGE_NAME; + return NAME; } @Override diff --git a/plugins/sonar-xoo-plugin/src/main/java/org/sonar/plugins/xoo/XooPlugin.java b/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/XooPlugin.java index 95935db874e..077c445022b 100644 --- a/plugins/sonar-xoo-plugin/src/main/java/org/sonar/plugins/xoo/XooPlugin.java +++ b/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/XooPlugin.java @@ -17,23 +17,29 @@ * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -package org.sonar.plugins.xoo; +package org.sonar.xoo; -import com.google.common.collect.ImmutableList; import org.sonar.api.SonarPlugin; -import org.sonar.plugins.xoo.rules.XooRuleDefinitions; +import org.sonar.xoo.rule.XooQualityProfile; +import org.sonar.xoo.rule.XooRuleDefinitions; +import java.util.Arrays; import java.util.List; +/** + * Plugin entry-point, as declared in pom.xml. + */ public class XooPlugin extends SonarPlugin { + /** + * Declares all the extensions implemented in the plugin + */ @Override public List getExtensions() { - return ImmutableList.of( + return Arrays.asList( Xoo.class, XooRuleDefinitions.class, - XooProfile.class); + XooQualityProfile.class); } - } diff --git a/plugins/sonar-xoo-plugin/src/main/java/org/sonar/plugins/xoo/package-info.java b/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/package-info.java index 79dac1a80c1..c49fa5cd361 100644 --- a/plugins/sonar-xoo-plugin/src/main/java/org/sonar/plugins/xoo/package-info.java +++ b/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/package-info.java @@ -18,6 +18,6 @@ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ @ParametersAreNonnullByDefault -package org.sonar.plugins.xoo; +package org.sonar.xoo; import javax.annotation.ParametersAreNonnullByDefault; diff --git a/plugins/sonar-xoo-plugin/src/main/java/org/sonar/plugins/xoo/XooProfile.java b/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/rule/XooQualityProfile.java index cfe1fa3b6d6..edc0ce51592 100644 --- a/plugins/sonar-xoo-plugin/src/main/java/org/sonar/plugins/xoo/XooProfile.java +++ b/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/rule/XooQualityProfile.java @@ -17,23 +17,22 @@ * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -package org.sonar.plugins.xoo; +package org.sonar.xoo.rule; import org.sonar.api.profiles.ProfileDefinition; import org.sonar.api.profiles.RulesProfile; import org.sonar.api.rules.Rule; import org.sonar.api.rules.RulePriority; import org.sonar.api.utils.ValidationMessages; -import org.sonar.plugins.xoo.base.XooConstants; -import org.sonar.plugins.xoo.base.XooRuleKeys; +import org.sonar.xoo.Xoo; -public class XooProfile extends ProfileDefinition { +public class XooQualityProfile extends ProfileDefinition { @Override public RulesProfile createProfile(ValidationMessages validation) { - final RulesProfile profile = RulesProfile.create("Basic", XooConstants.LANGUAGE_KEY); + final RulesProfile profile = RulesProfile.create("Basic", Xoo.KEY); - profile.activateRule(Rule.create(XooConstants.REPOSITORY_KEY, XooRuleKeys.RULE_MINIMAL), RulePriority.MAJOR); + profile.activateRule(Rule.create(XooRuleDefinitions.XOO_REPOSITORY, "x1"), RulePriority.MAJOR); return profile; } diff --git a/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/rule/XooRuleDefinitions.java b/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/rule/XooRuleDefinitions.java new file mode 100644 index 00000000000..ac47acea83b --- /dev/null +++ b/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/rule/XooRuleDefinitions.java @@ -0,0 +1,63 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2013 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube 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. + * + * SonarQube 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 this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.xoo.rule; + +import org.sonar.api.rule.RuleStatus; +import org.sonar.api.rule.Severity; +import org.sonar.api.server.rule.RuleDefinitions; +import org.sonar.api.server.rule.RuleParamType; +import org.sonar.xoo.Xoo; + +/** + * Define all the coding rules that are supported on the repository named "xoo". + */ +public class XooRuleDefinitions implements RuleDefinitions { + + public static final String XOO_REPOSITORY = "xoo"; + + @Override + public void define(Context context) { + NewRepository repository = context.newRepository(XOO_REPOSITORY, Xoo.KEY).setName("Xoo"); + + // define a single rule programmatically. Note that rules + // can be loaded from JSON or XML files too. + NewRule x1Rule = repository.newRule("x1") + .setName("No empty line") + .setHtmlDescription("Generate an issue on empty lines of Xoo source files") + + // optional tags + .addTags("style") + + // optional status. Default value is READY. + .setStatus(RuleStatus.BETA) + + // default severity when the rule is activated on a Quality profile. Default value is MAJOR. + .setSeverity(Severity.MINOR); + + x1Rule.newParam("acceptWhitespace") + .setDefaultValue("false") + .setType(RuleParamType.BOOLEAN) + .setDescription("Accept whitespaces on the line"); + + // don't forget to call done() to finalize the definition + repository.done(); + } + +} diff --git a/plugins/sonar-xoo-plugin/src/main/java/org/sonar/plugins/xoo/base/package-info.java b/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/rule/package-info.java index 75caceb3818..b6cfb03d1f9 100644 --- a/plugins/sonar-xoo-plugin/src/main/java/org/sonar/plugins/xoo/base/package-info.java +++ b/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/rule/package-info.java @@ -18,6 +18,6 @@ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ @ParametersAreNonnullByDefault -package org.sonar.plugins.xoo.base; +package org.sonar.xoo.rule; import javax.annotation.ParametersAreNonnullByDefault; diff --git a/plugins/sonar-xoo-plugin/src/test/java/org/sonar/plugins/xoo/XooPluginTest.java b/plugins/sonar-xoo-plugin/src/test/java/org/sonar/xoo/XooPluginTest.java index 35835363589..0e9095e8d4c 100644 --- a/plugins/sonar-xoo-plugin/src/test/java/org/sonar/plugins/xoo/XooPluginTest.java +++ b/plugins/sonar-xoo-plugin/src/test/java/org/sonar/xoo/XooPluginTest.java @@ -1,9 +1,3 @@ -package org.sonar.plugins.xoo; - -import org.junit.Test; -import org.sonar.plugins.xoo.rules.XooRuleDefinitions; -import static org.fest.assertions.Assertions.assertThat; - /* * SonarQube, open source software quality management tool. * Copyright (C) 2008-2013 SonarSource @@ -23,11 +17,16 @@ import static org.fest.assertions.Assertions.assertThat; * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ +package org.sonar.xoo; + +import org.junit.Test; + +import static org.fest.assertions.Assertions.assertThat; public class XooPluginTest { @Test - public void should_provide_rule_repository() { - assertThat(new XooPlugin().getExtensions()).containsOnly(Xoo.class, XooRuleDefinitions.class, XooProfile.class); + public void provide_extensions() { + assertThat(new XooPlugin().getExtensions()).hasSize(3); } } diff --git a/plugins/sonar-xoo-plugin/src/test/java/org/sonar/plugins/xoo/rules/XooRuleDefinitionsTest.java b/plugins/sonar-xoo-plugin/src/test/java/org/sonar/xoo/rule/XooRuleDefinitionsTest.java index 5ca6394ab1f..1618a2995b7 100644 --- a/plugins/sonar-xoo-plugin/src/test/java/org/sonar/plugins/xoo/rules/XooRuleDefinitionsTest.java +++ b/plugins/sonar-xoo-plugin/src/test/java/org/sonar/xoo/rule/XooRuleDefinitionsTest.java @@ -17,29 +17,26 @@ * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -package org.sonar.plugins.xoo.rules; +package org.sonar.xoo.rule; import org.junit.Test; import org.sonar.api.server.rule.RuleDefinitions; -import org.sonar.api.server.rule.RuleDefinitions.Repository; import static org.fest.assertions.Assertions.assertThat; public class XooRuleDefinitionsTest { - @Test - public void should_define_xoo_repository() { + public void define_xoo_rules() { + XooRuleDefinitions def = new XooRuleDefinitions(); RuleDefinitions.Context context = new RuleDefinitions.Context(); - new XooRuleDefinitions().define(context); - - assertThat(context.repositories()).hasSize(1); - - Repository xooRepository = context.repositories().get(0); - assertThat(xooRepository.key()).isEqualTo("xoo"); - assertThat(xooRepository.language()).isEqualTo("xoo"); - assertThat(xooRepository.name()).isEqualTo("Xoo"); - - assertThat(xooRepository.rules()).hasSize(7); + def.define(context); + + RuleDefinitions.Repository repo = context.repository("xoo"); + assertThat(repo).isNotNull(); + assertThat(repo.name()).isEqualTo("Xoo"); + assertThat(repo.language()).isEqualTo("xoo"); + assertThat(repo.rules()).hasSize(1); + assertThat(repo.rules().get(0).key()).isEqualTo("x1"); } } |