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
}
}
- /**
- * 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.");
- }
- }
-
}
}
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());
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<RuleDebt> ruleDebts) {
+ private void updateRuleDebtDefinitions(NewRule newRule, String repoKey, String ruleKey, List<RuleDebt> 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);
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;
assertThat(context.repositories()).isEmpty();
}
- /**
- * SONAR-5195
- */
- @Test
- public void remove_debt_on_rule_templates() {
- RulesDefinition.Context context = new RulesDefinition.Context();
-
- List<DebtModelXMLExporter.RuleDebt> 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<Rule> 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();
- }
-
}