]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-5056 Fail if not both characteristic and function are defined
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Mon, 17 Mar 2014 08:27:56 +0000 (09:27 +0100)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Mon, 17 Mar 2014 08:27:56 +0000 (09:27 +0100)
sonar-plugin-api/src/main/java/org/sonar/api/server/rule/RulesDefinition.java
sonar-plugin-api/src/test/java/org/sonar/api/server/rule/RulesDefinitionTest.java

index 6825c6afb6f2b71d19b51a0a2e24a0b65de775a2..662841909d38c2215eb510954a88ef505a876ae4 100644 (file)
@@ -439,6 +439,9 @@ public interface RulesDefinition extends ServerExtension {
       if (StringUtils.isBlank(htmlDescription)) {
         throw new IllegalStateException(String.format("HTML description of rule %s is empty", this));
       }
+      if ((StringUtils.isBlank(debtCharacteristic) && debtRemediationFunction != null) || (!StringUtils.isBlank(debtCharacteristic) && debtRemediationFunction == null)) {
+        throw new IllegalStateException(String.format("Both debt characteristic and debt remediation function should be defined on rule '%s'", this));
+      }
     }
 
     @Override
index 29d3f6c7f3a929dfbb6f49f60b7b85d61f95506b..594a14e450d29c71cfce91e8a1dc1243a0be9dd1 100644 (file)
@@ -294,4 +294,31 @@ public class RulesDefinitionTest {
     }
   }
 
+  @Test
+  public void fail_if_define_characteristic_without_function() {
+    RulesDefinition.NewRepository newRepository = context.createRepository("findbugs", "java");
+    newRepository.createRule("NPE").setName("NPE").setHtmlDescription("Detect <code>java.lang.NullPointerException</code>")
+      .setDebtCharacteristic("COMPILER");
+    try {
+      newRepository.done();
+      fail();
+    } catch (IllegalStateException e) {
+      assertThat(e).hasMessage("Both debt characteristic and debt remediation function should be defined on rule '[repository=findbugs, key=NPE]'");
+    }
+  }
+
+  @Test
+  public void fail_if_define_function_without_characteristic() {
+    RulesDefinition.NewRepository newRepository = context.createRepository("findbugs", "java");
+    newRepository.createRule("NPE").setName("NPE").setHtmlDescription("Detect <code>java.lang.NullPointerException</code>")
+      .setDebtCharacteristic("")
+      .setDebtRemediationFunction(DebtRemediationFunction.create(DebtRemediationFunction.Type.LINEAR_OFFSET, "1h", "10min"));
+    try {
+      newRepository.done();
+      fail();
+    } catch (IllegalStateException e) {
+      assertThat(e).hasMessage("Both debt characteristic and debt remediation function should be defined on rule '[repository=findbugs, key=NPE]'");
+    }
+  }
+
 }