From: Julien Lancelot Date: Mon, 14 Apr 2014 13:35:26 +0000 (+0200) Subject: SONAR-5195 Revert preventing rule template to declare technical debt X-Git-Tag: 4.3~27 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=897b2d8df05c14640158d62b89c99dad478aa248;p=sonarqube.git SONAR-5195 Revert preventing rule template to declare technical debt --- 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 ba5870588ca..831a05f1155 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 @@ -687,9 +687,6 @@ public interface RulesDefinition extends ServerExtension { 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)); } - if (!Strings.isNullOrEmpty(debtSubCharacteristic) && template) { - throw new IllegalStateException(String.format("'%s' is a rule template, it should not define technical debt.", 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 43a88eaffe0..1027e8ba3fc 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 @@ -340,26 +340,4 @@ public class RulesDefinitionTest { } } - /** - * SONAR-5195 - */ - @Test - public void fail_if_rule_template_define_technical_debt() { - RulesDefinition.NewRepository newRepo = context.createRepository("squid", "java"); - RulesDefinition.NewRule newRule = newRepo.createRule("XPath rule") - .setTemplate(true) - .setName("Insufficient branch coverage") - .setHtmlDescription("This rule allows to define some homemade Java rules with help of an XPath expression.") - .setSeverity(Severity.MAJOR) - .setDebtSubCharacteristic(RulesDefinition.SubCharacteristics.UNIT_TESTS); - newRule.setDebtRemediationFunction(newRule.debtRemediationFunctions().linearWithOffset("1h", "10min")); - - try { - newRepo.done(); - fail(); - } catch (IllegalStateException e) { - assertThat(e).hasMessage("'[repository=squid, key=XPath rule]' is a rule template, it should not define technical debt."); - } - } - } diff --git a/sonar-server/src/main/java/org/sonar/server/rule/DeprecatedRulesDefinition.java b/sonar-server/src/main/java/org/sonar/server/rule/DeprecatedRulesDefinition.java index dc57c863c73..bedb756b036 100644 --- a/sonar-server/src/main/java/org/sonar/server/rule/DeprecatedRulesDefinition.java +++ b/sonar-server/src/main/java/org/sonar/server/rule/DeprecatedRulesDefinition.java @@ -91,11 +91,10 @@ public class DeprecatedRulesDefinition implements RulesDefinition { } for (org.sonar.api.rules.Rule rule : repository.createRules()) { NewRule newRule = newRepository.createRule(rule.getKey()); - boolean isTemplate = Cardinality.MULTIPLE.equals(rule.getCardinality()); newRule.setName(ruleName(repository.getKey(), rule)); newRule.setHtmlDescription(ruleDescription(repository.getKey(), rule)); newRule.setInternalKey(rule.getConfigKey()); - newRule.setTemplate(isTemplate); + newRule.setTemplate(Cardinality.MULTIPLE.equals(rule.getCardinality())); newRule.setSeverity(rule.getSeverity().toString()); newRule.setStatus(rule.getStatus() == null ? RuleStatus.defaultStatus() : RuleStatus.valueOf(rule.getStatus())); newRule.setTags(rule.getTags()); @@ -105,30 +104,26 @@ public class DeprecatedRulesDefinition implements RulesDefinition { newParam.setDescription(paramDescription(repository.getKey(), rule.getKey(), param)); newParam.setType(RuleParamType.parse(param.getType())); } - updateRuleDebtDefinitions(newRule, repository.getKey(), rule.getKey(), isTemplate, ruleDebts); + updateRuleDebtDefinitions(newRule, repository.getKey(), rule.getKey(), ruleDebts); } newRepository.done(); } } - private void updateRuleDebtDefinitions(NewRule newRule, String repoKey, String ruleKey, boolean isTemplate, List ruleDebts) { + private void updateRuleDebtDefinitions(NewRule newRule, String repoKey, String ruleKey, List ruleDebts) { RuleDebt ruleDebt = findRequirement(ruleDebts, repoKey, ruleKey); if (ruleDebt != null) { - if (isTemplate) { - LOG.warn(String.format("'%s:%s' is a rule template, it should not define technical debt. Its debt definition will be ignored.", repoKey, ruleKey)); - } else { - newRule.setDebtSubCharacteristic(ruleDebt.subCharacteristicKey()); - newRule.setDebtRemediationFunction(remediationFunction(DebtRemediationFunction.Type.valueOf(ruleDebt.function()), - ruleDebt.coefficient(), - ruleDebt.offset(), - newRule.debtRemediationFunctions(), - repoKey, ruleKey - )); - } + newRule.setDebtSubCharacteristic(ruleDebt.subCharacteristicKey()); + newRule.setDebtRemediationFunction(remediationFunction(DebtRemediationFunction.Type.valueOf(ruleDebt.function()), + ruleDebt.coefficient(), + ruleDebt.offset(), + newRule.debtRemediationFunctions(), + repoKey, ruleKey + )); } } - private DebtRemediationFunction remediationFunction(DebtRemediationFunction.Type function, @Nullable String coefficient, @Nullable String offset, + private DebtRemediationFunction remediationFunction(DebtRemediationFunction.Type function, @Nullable String coefficient, @Nullable String offset, DebtRemediationFunctions functions, String repoKey, String ruleKey) { if (DebtRemediationFunction.Type.LINEAR.equals(function) && coefficient != null) { return functions.linear(coefficient); diff --git a/sonar-server/src/test/java/org/sonar/server/rule/DeprecatedRulesDefinitionTest.java b/sonar-server/src/test/java/org/sonar/server/rule/DeprecatedRulesDefinitionTest.java index 367df67e908..59bfb301f1e 100644 --- a/sonar-server/src/test/java/org/sonar/server/rule/DeprecatedRulesDefinitionTest.java +++ b/sonar-server/src/test/java/org/sonar/server/rule/DeprecatedRulesDefinitionTest.java @@ -32,7 +32,6 @@ import org.sonar.api.rules.RuleRepository; import org.sonar.api.server.debt.DebtRemediationFunction; import org.sonar.api.server.rule.RulesDefinition; import org.sonar.api.utils.ValidationMessages; -import org.sonar.check.Cardinality; import org.sonar.core.i18n.RuleI18nManager; import org.sonar.server.debt.DebtModelPluginRepository; import org.sonar.server.debt.DebtModelXMLExporter; @@ -215,45 +214,4 @@ public class DeprecatedRulesDefinitionTest { assertThat(context.repositories()).isEmpty(); } - /** - * SONAR-5195 - */ - @Test - public void remove_debt_on_rule_templates() { - RulesDefinition.Context context = new RulesDefinition.Context(); - - List ruleDebts = newArrayList( - new DebtModelXMLExporter.RuleDebt() - .setSubCharacteristicKey("MEMORY_EFFICIENCY") - .setRuleKey(RuleKey.of("squid", "XPath")) - .setFunction(DebtRemediationFunction.Type.LINEAR.name()) - .setCoefficient("1d") - ); - - Reader javaModelReader = mock(Reader.class); - when(debtModelRepository.createReaderForXMLFile("java")).thenReturn(javaModelReader); - when(debtModelRepository.getContributingPluginList()).thenReturn(newArrayList("java")); - when(importer.importXML(eq(javaModelReader), any(ValidationMessages.class))).thenReturn(ruleDebts); - - new DeprecatedRulesDefinition(i18n, new RuleRepository[]{new RuleRepository("squid", "java") { - @Override - public List createRules() { - return newArrayList( - Rule.create("squid", "XPath", "XPath rule") - .setCardinality(Cardinality.MULTIPLE) - .setDescription("This rule allows to define some homemade Java rules with help of an XPath expression.") - ); - } - }}, debtModelRepository, importer).define(context); - - assertThat(context.repositories()).hasSize(1); - RulesDefinition.Repository repo = context.repository("squid"); - assertThat(repo.rules()).hasSize(1); - - RulesDefinition.Rule rule = repo.rule("XPath"); - assertThat(rule).isNotNull(); - assertThat(rule.debtSubCharacteristic()).isNull(); - assertThat(rule.debtRemediationFunction()).isNull(); - } - }