private static void loadParameters(RulesDefinition.NewRule rule, Field field) { org.sonar.check.RuleProperty propertyAnnotation = field.getAnnotation(org.sonar.check.RuleProperty.class); if (propertyAnnotation != null) { String fieldKey = StringUtils.defaultIfEmpty(propertyAnnotation.key(), field.getName()); RulesDefinition.NewParam param = rule.createParam(fieldKey) .setDescription(propertyAnnotation.description()) .setDefaultValue(propertyAnnotation.defaultValue()); if (!StringUtils.isBlank(propertyAnnotation.type())) { try { param.setType(RuleParamType.parse(propertyAnnotation.type().trim())); } catch (IllegalArgumentException e) { throw new IllegalArgumentException("Invalid property type [" + propertyAnnotation.type() + "]", e); } } else { param.setType(guessType(field.getType())); } } }
@Test public void fail_if_duplicated_rule_param_keys() { RulesDefinition.NewRule rule = context.createRepository("findbugs", "java").createRule("NPE"); rule.createParam("level"); try { rule.createParam("level"); fail(); } catch (Exception e) { assertThat(e).isInstanceOf(IllegalArgumentException.class).hasMessage("The parameter 'level' is declared several times on the rule [repository=findbugs, key=NPE]"); } }
@Test public void define_rule_parameters() { RulesDefinition.NewRepository newFindbugs = context.createRepository("findbugs", "java"); RulesDefinition.NewRule newNpe = newFindbugs.createRule("NPE").setName("NPE").setHtmlDescription("NPE"); newNpe.createParam("level").setDefaultValue("LOW").setName("Level").setDescription("The level").setType(RuleParamType.INTEGER); newNpe.createParam("effort"); newFindbugs.done(); RulesDefinition.Rule rule = context.repository("findbugs").rule("NPE"); assertThat(rule.params()).hasSize(2); RulesDefinition.Param level = rule.param("level"); assertThat(level.key()).isEqualTo("level"); assertThat(level.name()).isEqualTo("Level"); assertThat(level.description()).isEqualTo("The level"); assertThat(level.defaultValue()).isEqualTo("LOW"); assertThat(level.type()).isEqualTo(RuleParamType.INTEGER); RulesDefinition.Param effort = rule.param("effort"); assertThat(effort.key()).isEqualTo("effort").isEqualTo(effort.name()); assertThat(effort.description()).isNull(); assertThat(effort.defaultValue()).isNull(); assertThat(effort.type()).isEqualTo(RuleParamType.STRING); // test equals() and hashCode() assertThat(level).isEqualTo(level).isNotEqualTo(effort).isNotEqualTo("level").isNotEqualTo(null); assertThat(level.hashCode()).isEqualTo(level.hashCode()); }
private void loadParameters(RulesDefinition.NewRule rule, Field field) { RuleProperty propertyAnnotation = field.getAnnotation(RuleProperty.class); if (propertyAnnotation != null) { String fieldKey = StringUtils.defaultIfEmpty(propertyAnnotation.key(), field.getName()); RulesDefinition.NewParam param = rule.createParam(fieldKey) .setDescription(propertyAnnotation.description()) .setDefaultValue(propertyAnnotation.defaultValue()); if (!StringUtils.isBlank(propertyAnnotation.type())) { try { param.setType(RuleParamType.parse(propertyAnnotation.type().trim())); } catch (IllegalArgumentException e) { throw new IllegalArgumentException("Invalid property type [" + propertyAnnotation.type() + "]", e); } } else { param.setType(guessType(field.getType())); } } }
private void loadParameters(RulesDefinition.NewRule rule, Field field) { org.sonar.check.RuleProperty propertyAnnotation = field.getAnnotation(org.sonar.check.RuleProperty.class); if (propertyAnnotation != null) { String fieldKey = StringUtils.defaultIfEmpty(propertyAnnotation.key(), field.getName()); RulesDefinition.NewParam param = rule.createParam(fieldKey) .setDescription(propertyAnnotation.description()) .setDefaultValue(propertyAnnotation.defaultValue()); if (!StringUtils.isBlank(propertyAnnotation.type())) { try { param.setType(RuleParamType.parse(propertyAnnotation.type().trim())); } catch (IllegalArgumentException e) { throw new IllegalArgumentException("Invalid property type [" + propertyAnnotation.type() + "]", e); } } else { param.setType(guessType(field.getType())); } } }
private static void defineBranchCoverageRule(RulesDefinition.NewRepository repo) { RulesDefinition.NewRule rule = repo.createRule(CommonRuleKeys.INSUFFICIENT_BRANCH_COVERAGE); rule.setName("Branches should have sufficient coverage by tests") .addTags("bad-practice") .setHtmlDescription("An issue is created on a file as soon as the branch coverage on this file is less than the required threshold." + "It gives the number of branches to be covered in order to reach the required threshold.") .setDebtRemediationFunction(rule.debtRemediationFunctions().linear("5min")) .setGapDescription("number of uncovered conditions") .setSeverity(Severity.MAJOR); rule.createParam(CommonRuleKeys.INSUFFICIENT_BRANCH_COVERAGE_PROPERTY) .setName("The minimum required branch coverage ratio") .setDefaultValue("65") .setType(RuleParamType.FLOAT); }
private static void defineCommentDensityRule(RulesDefinition.NewRepository repo) { RulesDefinition.NewRule rule = repo.createRule(CommonRuleKeys.INSUFFICIENT_COMMENT_DENSITY); rule.setName("Source files should have a sufficient density of comment lines") .addTags("convention") .setHtmlDescription("An issue is created on a file as soon as the density of comment lines on this file is less than the required threshold. " + "The number of comment lines to be written in order to reach the required threshold is provided by each issue message.") .setDebtRemediationFunction(rule.debtRemediationFunctions().linear("2min")) .setGapDescription("number of lines required to meet minimum density") .setSeverity(Severity.MAJOR); rule.createParam(CommonRuleKeys.INSUFFICIENT_COMMENT_DENSITY_PROPERTY) .setName("The minimum required comment density") .setDefaultValue("25") .setType(RuleParamType.FLOAT); }
private static void defineLineCoverageRule(RulesDefinition.NewRepository repo) { RulesDefinition.NewRule rule = repo.createRule(CommonRuleKeys.INSUFFICIENT_LINE_COVERAGE); rule.setName("Lines should have sufficient coverage by tests") .addTags("bad-practice") .setHtmlDescription("An issue is created on a file as soon as the line coverage on this file is less than the required threshold. " + "It gives the number of lines to be covered in order to reach the required threshold.") .setDebtRemediationFunction(rule.debtRemediationFunctions().linear("2min")) .setGapDescription("number of lines under the coverage threshold") .setSeverity(Severity.MAJOR); rule.createParam(CommonRuleKeys.INSUFFICIENT_LINE_COVERAGE_PROPERTY) .setName("The minimum required line coverage ratio") .setDefaultValue("65") .setType(RuleParamType.FLOAT); }
private static void fillParams(RulesDefinition.NewRule rule, List<ParamStruct> params) { for (ParamStruct param : params) { rule.createParam(param.key) .setDefaultValue(param.defaultValue) .setType(param.type) .setDescription(param.description); } }
hasTag .setDebtRemediationFunction(hasTag.debtRemediationFunctions().constantPerIssue("2min")); hasTag.createParam("tag") .setDefaultValue("xoo") .setDescription("The tag to search for"); ruleWithParameters.createParam("string").setType(RuleParamType.STRING); ruleWithParameters.createParam("text").setType(RuleParamType.TEXT); ruleWithParameters.createParam("boolean").setType(RuleParamType.BOOLEAN); ruleWithParameters.createParam("integer").setType(RuleParamType.INTEGER); ruleWithParameters.createParam("float").setType(RuleParamType.FLOAT);
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));
@Override public void define(Context context) { NewRepository repo = context.createRepository("fake", "java"); NewRule rule1 = repo.createRule(RULE_KEY1.rule()) .setName("One") .setHtmlDescription("Description of One") .setSeverity(BLOCKER) .setInternalKey("config1") .setTags("tag1", "tag2", "tag3") .setScope(RuleScope.ALL) .setType(RuleType.CODE_SMELL) .setStatus(RuleStatus.BETA) .setGapDescription("squid.S115.effortToFix"); rule1.setDebtRemediationFunction(rule1.debtRemediationFunctions().linearWithOffset("5d", "10h")); rule1.createParam("param1").setDescription("parameter one").setDefaultValue("default1"); rule1.createParam("param2").setDescription("parameter two").setDefaultValue("default2"); repo.createRule(HOTSPOT_RULE_KEY.rule()) .setName("Hotspot") .setHtmlDescription("Minimal hotspot") .setType(RuleType.SECURITY_HOTSPOT) .addOwaspTop10(OwaspTop10.A1, OwaspTop10.A3) .addCwe(1, 123, 863); repo.createRule(RULE_KEY2.rule()) .setName("Two") .setHtmlDescription("Minimal rule"); repo.done(); } }
@Override public void define(Context context) { NewRepository repo = context.createRepository("fake", "java"); // almost all the attributes of rule1 are changed NewRule rule1 = repo.createRule(RULE_KEY1.rule()) .setName("One v2") .setHtmlDescription("Description of One v2") .setSeverity(INFO) .setInternalKey("config1 v2") // tag2 and tag3 removed, tag4 added .setTags("tag1", "tag4") .setType(RuleType.BUG) .setStatus(READY) .setGapDescription("squid.S115.effortToFix.v2"); rule1.setDebtRemediationFunction(rule1.debtRemediationFunctions().linearWithOffset("6d", "2h")); rule1.createParam("param1").setDescription("parameter one v2").setDefaultValue("default1 v2"); rule1.createParam("param2").setDescription("parameter two v2").setDefaultValue("default2 v2"); // rule2 is dropped, rule3 is new repo.createRule(RULE_KEY3.rule()) .setName("Three") .setHtmlDescription("Rule Three"); repo.done(); } }
@Test public void define_rule_parameter_with_empty_default_value() { RulesDefinition.NewRepository newFindbugs = context.createRepository("findbugs", "java"); RulesDefinition.NewRule newNpe = newFindbugs.createRule("NPE").setName("NPE").setHtmlDescription("NPE"); newNpe.createParam("level").setDefaultValue("").setName("Level").setDescription("The level").setType(RuleParamType.INTEGER); newFindbugs.done(); RulesDefinition.Rule rule = context.repository("findbugs").rule("NPE"); assertThat(rule.params()).hasSize(1); RulesDefinition.Param level = rule.param("level"); assertThat(level.key()).isEqualTo("level"); assertThat(level.name()).isEqualTo("Level"); assertThat(level.description()).isEqualTo("The level"); // Empty value is converted in null value assertThat(level.defaultValue()).isNull(); assertThat(level.type()).isEqualTo(RuleParamType.INTEGER); }
@Override public void define(Context context) { NewRepository repo = context.createRepository("big", "java"); for (int i = 0; i < SIZE; i++) { NewRule rule = repo.createRule("rule" + i) .setName("name of " + i) .setHtmlDescription("description of " + i); for (int j = 0; j < 20; j++) { rule.createParam("param" + j); } } repo.done(); } }
private static void defineBranchCoverageRule(RulesDefinition.NewRepository repo) { RulesDefinition.NewRule rule = repo.createRule(CommonRuleKeys.INSUFFICIENT_BRANCH_COVERAGE); rule.setName("Branches should have sufficient coverage by tests") .addTags("bad-practice") .setHtmlDescription("An issue is created on a file as soon as the branch coverage on this file is less than the required threshold. " + "It gives the number of branches to be covered in order to reach the required threshold.") .setDebtRemediationFunction(rule.debtRemediationFunctions().linear("5min")) .setGapDescription("number of uncovered conditions") .setSeverity(Severity.MAJOR); rule.createParam(CommonRuleKeys.INSUFFICIENT_BRANCH_COVERAGE_PROPERTY) .setName("The minimum required branch coverage ratio") .setDefaultValue("65") .setType(RuleParamType.FLOAT); }
private static void defineCommentDensityRule(RulesDefinition.NewRepository repo) { RulesDefinition.NewRule rule = repo.createRule(CommonRuleKeys.INSUFFICIENT_COMMENT_DENSITY); rule.setName("Source files should have a sufficient density of comment lines") .addTags("convention") .setHtmlDescription("An issue is created on a file as soon as the density of comment lines on this file is less than the required threshold. " + "The number of comment lines to be written in order to reach the required threshold is provided by each issue message.") .setDebtRemediationFunction(rule.debtRemediationFunctions().linear("2min")) .setGapDescription("number of lines required to meet minimum density") .setSeverity(Severity.MAJOR); rule.createParam(CommonRuleKeys.INSUFFICIENT_COMMENT_DENSITY_PROPERTY) .setName("The minimum required comment density") .setDefaultValue("25") .setType(RuleParamType.FLOAT); }
private static void defineLineCoverageRule(RulesDefinition.NewRepository repo) { RulesDefinition.NewRule rule = repo.createRule(CommonRuleKeys.INSUFFICIENT_LINE_COVERAGE); rule.setName("Lines should have sufficient coverage by tests") .addTags("bad-practice") .setHtmlDescription("An issue is created on a file as soon as the line coverage on this file is less than the required threshold. " + "It gives the number of lines to be covered in order to reach the required threshold.") .setDebtRemediationFunction(rule.debtRemediationFunctions().linear("2min")) .setGapDescription("number of lines under the coverage threshold") .setSeverity(Severity.MAJOR); rule.createParam(CommonRuleKeys.INSUFFICIENT_LINE_COVERAGE_PROPERTY) .setName("The minimum required line coverage ratio") .setDefaultValue("65") .setType(RuleParamType.FLOAT); }
private static void fillParams(RulesDefinition.NewRule rule, List<ParamStruct> params) { for (ParamStruct param : params) { rule.createParam(param.key) .setDefaultValue(param.defaultValue) .setType(param.type) .setDescription(param.description); } }
@Test public void complete_param_description() { when(i18n.getParamDescription("squid", "S0001", "max")).thenReturn("Maximum"); RulesDefinition.Context context = new RulesDefinition.Context(); RulesDefinition.NewRepository repo = context.createRepository("squid", "java"); repo.createRule("S0001").setName("SOne").setHtmlDescription("S One").createParam("max"); loader.load(repo); repo.done(); RulesDefinition.Rule rule = context.repository("squid").rule("S0001"); assertThat(rule.param("max").description()).isEqualTo("Maximum"); } }