aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-plugin-api
diff options
context:
space:
mode:
authorSimon Brandhof <simon.brandhof@gmail.com>2014-01-13 17:22:14 +0100
committerSimon Brandhof <simon.brandhof@gmail.com>2014-01-13 17:22:43 +0100
commitf5521caa09810aac91e1dbb86dee52e435f4bbd5 (patch)
tree9d4a2cc54787bdbdf6f4a0f036f9a63d762bd22a /sonar-plugin-api
parentf4d4c2fa8c1d7b4f04496c4f8b78ec02c86473ff (diff)
downloadsonarqube-f5521caa09810aac91e1dbb86dee52e435f4bbd5.tar.gz
sonarqube-f5521caa09810aac91e1dbb86dee52e435f4bbd5.zip
SONAR-4908 add template rules and param types
Diffstat (limited to 'sonar-plugin-api')
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/rule/RuleDefinitions.java43
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/rule/RuleParamType.java61
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/rule/RuleDefinitionsTest.java32
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/rule/RuleParamTypeTest.java45
4 files changed, 160 insertions, 21 deletions
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/rule/RuleDefinitions.java b/sonar-plugin-api/src/main/java/org/sonar/api/rule/RuleDefinitions.java
index b8333948133..939377f2d40 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/rule/RuleDefinitions.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/rule/RuleDefinitions.java
@@ -168,6 +168,7 @@ public interface RuleDefinitions extends ServerExtension {
this.name = newRepository.name;
ImmutableMap.Builder<String, Rule> ruleBuilder = ImmutableMap.builder();
for (NewRule newRule : newRepository.newRules.values()) {
+ newRule.validate();
ruleBuilder.put(newRule.key, new Rule(newRule));
}
this.rulesByKey = ruleBuilder.build();
@@ -221,22 +222,25 @@ public interface RuleDefinitions extends ServerExtension {
static class NewRule {
private final String repoKey, key;
private String name, htmlDescription, metadata, defaultSeverity = Severity.MAJOR;
+ private boolean template;
private final Set<String> tags = Sets.newHashSet();
private final Map<String, NewParam> paramsByKey = Maps.newHashMap();
private NewRule(String repoKey, String key) {
this.repoKey = repoKey;
- this.key = this.name = key;
+ this.key = key;
}
public NewRule setName(String s) {
- if (StringUtils.isBlank(s)) {
- throw new IllegalArgumentException(String.format("Name of rule %s is blank", this));
- }
this.name = s;
return this;
}
+ public NewRule setTemplate(boolean template) {
+ this.template = template;
+ return this;
+ }
+
public NewRule setDefaultSeverity(String s) {
if (!Severity.ALL.contains(s)) {
throw new IllegalArgumentException(String.format("Default severity of rule %s is not correct: %s", this, s));
@@ -246,9 +250,6 @@ public interface RuleDefinitions extends ServerExtension {
}
public NewRule setHtmlDescription(String s) {
- if (StringUtils.isBlank(s)) {
- throw new IllegalArgumentException(String.format("HTML description of rule %s is blank", this));
- }
this.htmlDescription = s;
return this;
}
@@ -292,6 +293,15 @@ public interface RuleDefinitions extends ServerExtension {
return this;
}
+ private void validate() {
+ if (StringUtils.isBlank(name)) {
+ throw new IllegalStateException(String.format("Name of rule %s is empty", this));
+ }
+ if (StringUtils.isBlank(htmlDescription)) {
+ throw new IllegalStateException(String.format("HTML description of rule %s is empty", this));
+ }
+ }
+
@Override
public String toString() {
return String.format("[repository=%s, key=%s]", repoKey, key);
@@ -300,6 +310,7 @@ public interface RuleDefinitions extends ServerExtension {
static class Rule {
private final String repoKey, key, name, htmlDescription, metadata, defaultSeverity;
+ private final boolean template;
private final Set<String> tags;
private final Map<String, Param> params;
@@ -310,6 +321,7 @@ public interface RuleDefinitions extends ServerExtension {
this.htmlDescription = newRule.htmlDescription;
this.metadata = newRule.metadata;
this.defaultSeverity = newRule.defaultSeverity;
+ this.template = newRule.template;
this.tags = ImmutableSet.copyOf(newRule.tags);
ImmutableMap.Builder<String, Param> paramsBuilder = ImmutableMap.builder();
for (NewParam newParam : newRule.paramsByKey.values()) {
@@ -335,6 +347,10 @@ public interface RuleDefinitions extends ServerExtension {
return htmlDescription;
}
+ public boolean template() {
+ return template;
+ }
+
@CheckForNull
public Param param(String key) {
return params.get(key);
@@ -385,7 +401,7 @@ public interface RuleDefinitions extends ServerExtension {
static class NewParam {
private final String key;
private String name, description, defaultValue;
- // TODO type
+ private RuleParamType type = RuleParamType.STRING;
private NewParam(String key) {
this.key = this.name = key;
@@ -397,6 +413,11 @@ public interface RuleDefinitions extends ServerExtension {
return this;
}
+ public NewParam setType(RuleParamType t) {
+ this.type = t;
+ return this;
+ }
+
/**
* Plain-text description. Can be null.
*/
@@ -413,12 +434,14 @@ public interface RuleDefinitions extends ServerExtension {
static class Param {
private final String key, name, description, defaultValue;
+ private final RuleParamType type;
private Param(NewParam newParam) {
this.key = newParam.key;
this.name = newParam.name;
this.description = newParam.description;
this.defaultValue = newParam.defaultValue;
+ this.type = newParam.type;
}
public String key() {
@@ -439,6 +462,10 @@ public interface RuleDefinitions extends ServerExtension {
return defaultValue;
}
+ public RuleParamType type() {
+ return type;
+ }
+
@Override
public boolean equals(Object o) {
if (this == o) {
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/rule/RuleParamType.java b/sonar-plugin-api/src/main/java/org/sonar/api/rule/RuleParamType.java
new file mode 100644
index 00000000000..5b9599db989
--- /dev/null
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/rule/RuleParamType.java
@@ -0,0 +1,61 @@
+/*
+ * 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.api.rule;
+
+/**
+ * @since 4.2
+ */
+public final class RuleParamType {
+
+ public static final RuleParamType STRING = new RuleParamType("STRING");
+ public static final RuleParamType TEXT = new RuleParamType("TEXT");
+ public static final RuleParamType BOOLEAN = new RuleParamType("BOOLEAN");
+ public static final RuleParamType INTEGER = new RuleParamType("INTEGER");
+ public static final RuleParamType REGULAR_EXPRESSION = new RuleParamType("REGULAR_EXPRESSION");
+
+ private final String key;
+
+ private RuleParamType(String key) {
+ this.key = key;
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) {
+ return true;
+ }
+ if (o == null || getClass() != o.getClass()) {
+ return false;
+ }
+
+ RuleParamType that = (RuleParamType) o;
+ return key.equals(that.key);
+ }
+
+ @Override
+ public int hashCode() {
+ return key.hashCode();
+ }
+
+ @Override
+ public String toString() {
+ return key;
+ }
+}
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/rule/RuleDefinitionsTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/rule/RuleDefinitionsTest.java
index 4f391609c51..87b891d04ac 100644
--- a/sonar-plugin-api/src/test/java/org/sonar/api/rule/RuleDefinitionsTest.java
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/rule/RuleDefinitionsTest.java
@@ -62,12 +62,13 @@ public class RuleDefinitionsTest {
RuleDefinitions.NewRepository newFindbugs = context.newRepository("findbugs", "java");
newFindbugs.newRule("NPE")
.setName("Detect NPE")
+ .setHtmlDescription("Detect <code>NPE</code>")
.setHtmlDescription("Detect <code>java.lang.NullPointerException</code>")
.setDefaultSeverity(Severity.BLOCKER)
.setMetadata("/something")
.setTags("one", "two")
.addTags("two", "three", "four");
- newFindbugs.newRule("ABC");
+ newFindbugs.newRule("ABC").setName("ABC").setHtmlDescription("ABC");
newFindbugs.done();
RuleDefinitions.Repository findbugs = context.repository("findbugs");
@@ -81,6 +82,7 @@ public class RuleDefinitionsTest {
assertThat(npeRule.tags()).containsOnly("one", "two", "three", "four");
assertThat(npeRule.params()).isEmpty();
assertThat(npeRule.metadata()).isEqualTo("/something");
+ assertThat(npeRule.template()).isFalse();
assertThat(npeRule.toString()).isEqualTo("[repository=findbugs, key=NPE]");
// test equals() and hashCode()
@@ -92,14 +94,12 @@ public class RuleDefinitionsTest {
@Test
public void define_rule_with_default_fields() {
RuleDefinitions.NewRepository newFindbugs = context.newRepository("findbugs", "java");
- newFindbugs.newRule("NPE");
+ newFindbugs.newRule("NPE").setName("NPE").setHtmlDescription("NPE");
newFindbugs.done();
RuleDefinitions.Rule rule = context.repository("findbugs").rule("NPE");
assertThat(rule.key()).isEqualTo("NPE");
- assertThat(rule.name()).isEqualTo("NPE");
assertThat(rule.defaultSeverity()).isEqualTo(Severity.MAJOR);
- assertThat(rule.htmlDescription()).isNull();
assertThat(rule.params()).isEmpty();
assertThat(rule.metadata()).isNull();
assertThat(rule.tags()).isEmpty();
@@ -108,8 +108,8 @@ public class RuleDefinitionsTest {
@Test
public void define_rule_parameters() {
RuleDefinitions.NewRepository newFindbugs = context.newRepository("findbugs", "java");
- RuleDefinitions.NewRule newNpe = newFindbugs.newRule("NPE");
- newNpe.newParam("level").setDefaultValue("LOW").setName("Level").setDescription("The level");
+ RuleDefinitions.NewRule newNpe = newFindbugs.newRule("NPE").setName("NPE").setHtmlDescription("NPE");
+ newNpe.newParam("level").setDefaultValue("LOW").setName("Level").setDescription("The level").setType(RuleParamType.INTEGER);
newNpe.newParam("effort");
newFindbugs.done();
@@ -121,11 +121,13 @@ public class RuleDefinitionsTest {
assertThat(level.name()).isEqualTo("Level");
assertThat(level.description()).isEqualTo("The level");
assertThat(level.defaultValue()).isEqualTo("LOW");
+ assertThat(level.type()).isEqualTo(RuleParamType.INTEGER);
RuleDefinitions.Param effort = rule.param("effort");
assertThat(effort.key()).isEqualTo("effort").isEqualTo(effort.name());
assertThat(effort.description()).isNull();
assertThat(effort.defaultValue()).isNull();
+ assertThat(effort.type()).isEqualTo(RuleParamType.STRING);
// test equals() and hashCode()
assertThat(level).isEqualTo(level).isNotEqualTo(effort).isNotEqualTo("level").isNotEqualTo(null);
@@ -138,7 +140,7 @@ public class RuleDefinitionsTest {
// for example fb-contrib
RuleDefinitions.NewExtendedRepository newFindbugs = context.extendRepository("findbugs");
- newFindbugs.newRule("NPE");
+ newFindbugs.newRule("NPE").setName("NPE").setHtmlDescription("NPE");
newFindbugs.done();
assertThat(context.repositories()).isEmpty();
@@ -187,11 +189,13 @@ public class RuleDefinitionsTest {
@Test
public void fail_if_blank_rule_name() {
+ RuleDefinitions.NewRepository newRepository = context.newRepository("findbugs", "java");
+ newRepository.newRule("NPE").setName(null).setHtmlDescription("NPE");
try {
- context.newRepository("findbugs", "java").newRule("NPE").setName(null);
+ newRepository.done();
fail();
- } catch (IllegalArgumentException e) {
- assertThat(e).hasMessage("Name of rule [repository=findbugs, key=NPE] is blank");
+ } catch (IllegalStateException e) {
+ assertThat(e).hasMessage("Name of rule [repository=findbugs, key=NPE] is empty");
}
}
@@ -208,11 +212,13 @@ public class RuleDefinitionsTest {
@Test
public void fail_if_blank_rule_html_description() {
+ RuleDefinitions.NewRepository newRepository = context.newRepository("findbugs", "java");
+ newRepository.newRule("NPE").setName("NPE").setHtmlDescription(null);
try {
- context.newRepository("findbugs", "java").newRule("NPE").setHtmlDescription(null);
+ newRepository.done();
fail();
- } catch (IllegalArgumentException e) {
- assertThat(e).hasMessage("HTML description of rule [repository=findbugs, key=NPE] is blank");
+ } catch (IllegalStateException e) {
+ assertThat(e).hasMessage("HTML description of rule [repository=findbugs, key=NPE] is empty");
}
}
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/rule/RuleParamTypeTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/rule/RuleParamTypeTest.java
new file mode 100644
index 00000000000..722c672696f
--- /dev/null
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/rule/RuleParamTypeTest.java
@@ -0,0 +1,45 @@
+/*
+ * 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.api.rule;
+
+import org.junit.Test;
+
+import static org.fest.assertions.Assertions.assertThat;
+
+public class RuleParamTypeTest {
+ @Test
+ public void testEquals() throws Exception {
+ assertThat(RuleParamType.INTEGER)
+ .isEqualTo(RuleParamType.INTEGER)
+ .isNotEqualTo(RuleParamType.STRING)
+ .isNotEqualTo("INTEGER")
+ .isNotEqualTo(null);
+ }
+
+ @Test
+ public void testHashCode() throws Exception {
+ assertThat(RuleParamType.INTEGER.hashCode()).isEqualTo(RuleParamType.INTEGER.hashCode());
+ }
+
+ @Test
+ public void testToString() throws Exception {
+ assertThat(RuleParamType.INTEGER.toString()).isEqualTo("INTEGER");
+ }
+}