From bdbbb1b3b8aaa7d4f5d048fa8d20edbca90b38f8 Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Mon, 17 Mar 2014 09:27:56 +0100 Subject: SONAR-5056 Fail if not both characteristic and function are defined --- .../org/sonar/api/server/rule/RulesDefinition.java | 3 +++ .../sonar/api/server/rule/RulesDefinitionTest.java | 27 ++++++++++++++++++++++ 2 files changed, 30 insertions(+) (limited to 'sonar-plugin-api') 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 6825c6afb6f..662841909d3 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 @@ -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 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 29d3f6c7f3a..594a14e450d 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 @@ -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 java.lang.NullPointerException") + .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 java.lang.NullPointerException") + .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]'"); + } + } + } -- cgit v1.2.3