aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-plugin-api/src
diff options
context:
space:
mode:
authorJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>2014-07-16 12:15:19 +0200
committerJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>2014-07-16 15:47:03 +0200
commit61e930fee6e29e42a6df0189aefa61b4783c6427 (patch)
tree58df785aa53103f606cbb5d4430d0f4f1020e581 /sonar-plugin-api/src
parent3c25a824164a3f091a27215fd8086cbefd60da39 (diff)
downloadsonarqube-61e930fee6e29e42a6df0189aefa61b4783c6427.tar.gz
sonarqube-61e930fee6e29e42a6df0189aefa61b4783c6427.zip
SONAR-5001 Validate descriptions (exactly one of HTML or MD desc. must be provided)
Diffstat (limited to 'sonar-plugin-api/src')
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/server/rule/RulesDefinition.java4
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/server/rule/RulesDefinitionTest.java64
-rw-r--r--sonar-plugin-api/src/test/resources/org/sonar/api/server/rule/RulesDefinitionTest/sample.md1
3 files changed, 59 insertions, 10 deletions
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/server/rule/RulesDefinition.java b/sonar-plugin-api/src/main/java/org/sonar/api/server/rule/RulesDefinition.java
index 2076fd07b52..8498d328eac 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/server/rule/RulesDefinition.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/server/rule/RulesDefinition.java
@@ -706,8 +706,8 @@ public interface RulesDefinition extends ServerExtension {
if (Strings.isNullOrEmpty(name)) {
throw new IllegalStateException(String.format("Name of rule %s is empty", this));
}
- if (Strings.isNullOrEmpty(htmlDescription)) {
- throw new IllegalStateException(String.format("HTML description of rule %s is empty", this));
+ if (Strings.isNullOrEmpty(htmlDescription) && Strings.isNullOrEmpty(markdownDescription)) {
+ throw new IllegalStateException(String.format("One of HTML description or Markdown description must be defined for rule %s", this));
}
if ((Strings.isNullOrEmpty(debtSubCharacteristic) && debtRemediationFunction != null) || (!Strings.isNullOrEmpty(debtSubCharacteristic) && debtRemediationFunction == null)) {
throw new IllegalStateException(String.format("Both debt sub-characteristic and debt remediation function should be defined on rule '%s'", this));
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/server/rule/RulesDefinitionTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/server/rule/RulesDefinitionTest.java
index 1027e8ba3fc..562fdc8bd50 100644
--- a/sonar-plugin-api/src/test/java/org/sonar/api/server/rule/RulesDefinitionTest.java
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/server/rule/RulesDefinitionTest.java
@@ -74,7 +74,7 @@ public class RulesDefinitionTest {
.setTags("one", "two")
.addTags("two", "three", "four");
- newRepo.createRule("ABC").setName("ABC").setHtmlDescription("ABC");
+ newRepo.createRule("ABC").setName("ABC").setMarkdownDescription("ABC");
newRepo.done();
RulesDefinition.Repository repo = context.repository("findbugs");
@@ -85,6 +85,7 @@ public class RulesDefinitionTest {
assertThat(rule.name()).isEqualTo("Detect NPE");
assertThat(rule.severity()).isEqualTo(Severity.BLOCKER);
assertThat(rule.htmlDescription()).isEqualTo("Detect <code>java.lang.NullPointerException</code>");
+ assertThat(rule.markdownDescription()).isNull();
assertThat(rule.tags()).containsOnly("one", "two", "three", "four");
assertThat(rule.params()).isEmpty();
assertThat(rule.internalKey()).isEqualTo("/something");
@@ -93,8 +94,11 @@ public class RulesDefinitionTest {
assertThat(rule.toString()).isEqualTo("[repository=findbugs, key=NPE]");
assertThat(rule.repository()).isSameAs(repo);
- // test equals() and hashCode()
RulesDefinition.Rule otherRule = repo.rule("ABC");
+ assertThat(otherRule.htmlDescription()).isNull();
+ assertThat(otherRule.markdownDescription()).isEqualTo("ABC");
+
+ // test equals() and hashCode()
assertThat(rule).isEqualTo(rule).isNotEqualTo(otherRule).isNotEqualTo("NPE").isNotEqualTo(null);
assertThat(rule.hashCode()).isEqualTo(rule.hashCode());
}
@@ -260,7 +264,7 @@ public class RulesDefinitionTest {
}
@Test
- public void load_rule_description_from_file() {
+ public void load_rule_html_description_from_file() {
RulesDefinition.NewRepository newRepository = context.createRepository("findbugs", "java");
newRepository.createRule("NPE").setName("NPE").setHtmlDescription(getClass().getResource("/org/sonar/api/server/rule/RulesDefinitionTest/sample.html"));
newRepository.done();
@@ -270,26 +274,70 @@ public class RulesDefinitionTest {
}
@Test
- public void fail_to_load_rule_description_from_file() {
+ public void load_rule_markdown_description_from_file() {
+ RulesDefinition.NewRepository newRepository = context.createRepository("findbugs", "java");
+ newRepository.createRule("NPE").setName("NPE").setMarkdownDescription(getClass().getResource("/org/sonar/api/server/rule/RulesDefinitionTest/sample.md"));
+ newRepository.done();
+
+ RulesDefinition.Rule rule = context.repository("findbugs").rule("NPE");
+ assertThat(rule.markdownDescription()).isEqualTo("description of rule loaded from file");
+ }
+
+ @Test
+ public void fail_to_load_html_rule_description_from_file() {
RulesDefinition.NewRepository newRepository = context.createRepository("findbugs", "java");
newRepository.createRule("NPE").setName("NPE").setHtmlDescription((URL) null);
try {
newRepository.done();
fail();
} catch (IllegalStateException e) {
- assertThat(e).hasMessage("HTML description of rule [repository=findbugs, key=NPE] is empty");
+ assertThat(e).hasMessage("One of HTML description or Markdown description must be defined for rule [repository=findbugs, key=NPE]");
}
}
@Test
- public void fail_if_blank_rule_html_description() {
+ public void fail_to_load_markdown_rule_description_from_file() {
RulesDefinition.NewRepository newRepository = context.createRepository("findbugs", "java");
- newRepository.createRule("NPE").setName("NPE").setHtmlDescription((String) null);
+ newRepository.createRule("NPE").setName("NPE").setMarkdownDescription((URL) null);
try {
newRepository.done();
fail();
} catch (IllegalStateException e) {
- assertThat(e).hasMessage("HTML description of rule [repository=findbugs, key=NPE] is empty");
+ assertThat(e).hasMessage("One of HTML description or Markdown description must be defined for rule [repository=findbugs, key=NPE]");
+ }
+ }
+
+ @Test
+ public void fail_if_no_rule_description() {
+ RulesDefinition.NewRepository newRepository = context.createRepository("findbugs", "java");
+ newRepository.createRule("NPE").setName("NPE");
+ try {
+ newRepository.done();
+ fail();
+ } catch (IllegalStateException e) {
+ assertThat(e).hasMessage("One of HTML description or Markdown description must be defined for rule [repository=findbugs, key=NPE]");
+ }
+ }
+
+ @Test
+ public void fail_if_rule_already_has_html_description() {
+ RulesDefinition.NewRepository newRepository = context.createRepository("findbugs", "java");
+ try {
+ newRepository.createRule("NPE").setName("NPE").setHtmlDescription("polop").setMarkdownDescription("palap");
+ fail();
+ } catch (IllegalStateException e) {
+ assertThat(e).hasMessage("Rule '[repository=findbugs, key=NPE]' already has an HTML description");
+ }
+ }
+
+ @Test
+ public void fail_if_rule_already_has_markdown_description() {
+ RulesDefinition.NewRepository newRepository = context.createRepository("findbugs", "java");
+ try {
+ newRepository.createRule("NPE").setName("NPE").setMarkdownDescription("palap").setHtmlDescription("polop");
+ fail();
+ } catch (IllegalStateException e) {
+ assertThat(e).hasMessage("Rule '[repository=findbugs, key=NPE]' already has a Markdown description");
}
}
diff --git a/sonar-plugin-api/src/test/resources/org/sonar/api/server/rule/RulesDefinitionTest/sample.md b/sonar-plugin-api/src/test/resources/org/sonar/api/server/rule/RulesDefinitionTest/sample.md
new file mode 100644
index 00000000000..86c36936b5c
--- /dev/null
+++ b/sonar-plugin-api/src/test/resources/org/sonar/api/server/rule/RulesDefinitionTest/sample.md
@@ -0,0 +1 @@
+description of rule loaded from file