use of org.sonar.api.rules.RuleRepository in project sonarqube by SonarSource.
the class DeprecatedRulesDefinitionLoaderTest method fail_on_invalid_rule_debt.
@Test
public void fail_on_invalid_rule_debt() {
RulesDefinition.Context context = new RulesDefinition.Context();
List<DebtModelXMLExporter.RuleDebt> ruleDebts = newArrayList(new DebtModelXMLExporter.RuleDebt().setRuleKey(RuleKey.of("checkstyle", "ConstantName")).setFunction(DebtRemediationFunction.Type.LINEAR_OFFSET.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);
try {
new DeprecatedRulesDefinitionLoader(i18n, debtModelRepository, importer, new RuleRepository[] { new CheckstyleRules() }).complete(context);
fail();
} catch (Exception e) {
assertThat(e).isInstanceOf(IllegalArgumentException.class);
}
assertThat(context.repositories()).isEmpty();
}
use of org.sonar.api.rules.RuleRepository in project sonarqube by SonarSource.
the class DeprecatedRulesDefinitionLoaderTest method define_rule_debt.
@Test
public void define_rule_debt() {
RulesDefinition.Context context = new RulesDefinition.Context();
List<DebtModelXMLExporter.RuleDebt> ruleDebts = newArrayList(new DebtModelXMLExporter.RuleDebt().setRuleKey(RuleKey.of("checkstyle", "ConstantName")).setFunction(DebtRemediationFunction.Type.LINEAR_OFFSET.name()).setCoefficient("1d").setOffset("10min"));
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 DeprecatedRulesDefinitionLoader(i18n, debtModelRepository, importer, new RuleRepository[] { new CheckstyleRules() }).complete(context);
assertThat(context.repositories()).hasSize(1);
RulesDefinition.Repository checkstyle = context.repository("checkstyle");
assertThat(checkstyle.rules()).hasSize(1);
RulesDefinition.Rule rule = checkstyle.rule("ConstantName");
assertThat(rule).isNotNull();
assertThat(rule.key()).isEqualTo("ConstantName");
assertThat(rule.debtRemediationFunction().type()).isEqualTo(DebtRemediationFunction.Type.LINEAR_OFFSET);
assertThat(rule.debtRemediationFunction().gapMultiplier()).isEqualTo("1d");
assertThat(rule.debtRemediationFunction().baseEffort()).isEqualTo("10min");
}
use of org.sonar.api.rules.RuleRepository in project sonarqube by SonarSource.
the class DeprecatedRulesDefinitionLoader method complete.
void complete(RulesDefinition.Context context) {
// Load rule debt definitions from xml files provided by plugin
List<RuleDebt> ruleDebts = loadRuleDebtList();
for (RuleRepository repository : repositories) {
// RuleRepository API does not handle difference between new and extended repositories,
RulesDefinition.NewRepository newRepository;
if (context.repository(repository.getKey()) == null) {
newRepository = context.createRepository(repository.getKey(), repository.getLanguage());
newRepository.setName(repository.getName());
} else {
newRepository = context.extendRepository(repository.getKey(), repository.getLanguage());
}
for (org.sonar.api.rules.Rule rule : repository.createRules()) {
RulesDefinition.NewRule newRule = newRepository.createRule(rule.getKey());
newRule.setName(ruleName(repository.getKey(), rule));
newRule.setHtmlDescription(ruleDescription(repository.getKey(), rule));
newRule.setInternalKey(rule.getConfigKey());
newRule.setTemplate(rule.isTemplate());
newRule.setSeverity(rule.getSeverity().toString());
newRule.setStatus(rule.getStatus() == null ? RuleStatus.defaultStatus() : RuleStatus.valueOf(rule.getStatus()));
newRule.setTags(rule.getTags());
for (RuleParam param : rule.getParams()) {
RulesDefinition.NewParam newParam = newRule.createParam(param.getKey());
newParam.setDefaultValue(param.getDefaultValue());
newParam.setDescription(paramDescription(repository.getKey(), rule.getKey(), param));
newParam.setType(RuleParamType.parse(param.getType()));
}
updateRuleDebtDefinitions(newRule, repository.getKey(), rule.getKey(), ruleDebts);
}
newRepository.done();
}
}
Aggregations