@Test public void parse_xml() { InputStream input = getClass().getResourceAsStream("RulesDefinitionXmlLoaderTest/rules.xml"); RulesDefinition.Repository repository = load(input, StandardCharsets.UTF_8.name()); assertThat(repository.rules()).hasSize(2); RulesDefinition.Rule rule = repository.rule("complete"); assertThat(rule.key()).isEqualTo("complete"); assertThat(rule.name()).isEqualTo("Complete"); assertThat(rule.htmlDescription()).isEqualTo("Description of Complete"); assertThat(rule.severity()).isEqualTo(Severity.BLOCKER); assertThat(rule.template()).isTrue(); assertThat(rule.status()).isEqualTo(RuleStatus.BETA); assertThat(rule.internalKey()).isEqualTo("Checker/TreeWalker/LocalVariableName"); assertThat(rule.type()).isEqualTo(RuleType.BUG); assertThat(rule.tags()).containsOnly("misra", "spring"); assertThat(rule.params()).hasSize(2); RulesDefinition.Param ignore = rule.param("ignore"); assertThat(ignore.key()).isEqualTo("ignore"); assertThat(ignore.description()).isEqualTo("Ignore ?"); assertThat(ignore.defaultValue()).isEqualTo("false"); rule = repository.rule("minimal"); assertThat(rule.key()).isEqualTo("minimal"); assertThat(rule.name()).isEqualTo("Minimal"); assertThat(rule.htmlDescription()).isEqualTo("Description of Minimal"); assertThat(rule.params()).isEmpty(); assertThat(rule.status()).isEqualTo(RuleStatus.READY); assertThat(rule.severity()).isEqualTo(Severity.MAJOR); assertThat(rule.type()).isEqualTo(RuleType.CODE_SMELL); }
private Rule(Repository repository, NewRule newRule) { this.pluginKey = newRule.pluginKey; this.repository = repository; this.repoKey = newRule.repoKey; this.key = newRule.key; this.name = newRule.name; this.htmlDescription = newRule.htmlDescription; this.markdownDescription = newRule.markdownDescription; this.internalKey = newRule.internalKey; this.severity = newRule.severity; this.template = newRule.template; this.status = newRule.status; this.debtRemediationFunction = newRule.debtRemediationFunction; this.gapDescription = newRule.gapDescription; this.scope = newRule.scope == null ? RuleScope.MAIN : newRule.scope; this.type = newRule.type == null ? RuleTagsToTypeConverter.convert(newRule.tags) : newRule.type; this.tags = ImmutableSortedSet.copyOf(Sets.difference(newRule.tags, RuleTagsToTypeConverter.RESERVED_TAGS)); this.securityStandards = ImmutableSortedSet.copyOf(newRule.securityStandards); Map<String, Param> paramsBuilder = new HashMap<>(); for (NewParam newParam : newRule.paramsByKey.values()) { paramsBuilder.put(newParam.key, new Param(newParam)); } this.params = Collections.unmodifiableMap(paramsBuilder); this.activatedByDefault = newRule.activatedByDefault; this.deprecatedRuleKeys = ImmutableSortedSet.copyOf(newRule.deprecatedRuleKeys); }
@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()); }
RuleParamDto paramDto = existingParamsByName.get(param.key()); if (paramDto != null) { continue; .setName(param.key()) .setDescription(param.description()) .setDefaultValue(param.defaultValue()) .setType(param.type().toString()); dbClient.ruleDao().insertRuleParam(session, rule, paramDto); if (StringUtils.isEmpty(param.defaultValue())) { continue; ActiveRuleParamDto activeParam = ActiveRuleParamDto.createFor(paramDto).setValue(param.defaultValue()); dbClient.activeRuleDao().insertParam(session, activeRule, activeParam);
.setInternalKey(rule.internalKey()); for (Param param : rule.params()) { newAr.setParam(param.key(), param.defaultValue());
private Rule(Repository repository, NewRule newRule) { this.pluginKey = newRule.pluginKey; this.repository = repository; this.repoKey = newRule.repoKey; this.key = newRule.key; this.name = newRule.name; this.htmlDescription = newRule.htmlDescription; this.markdownDescription = newRule.markdownDescription; this.internalKey = newRule.internalKey; this.severity = newRule.severity; this.template = newRule.template; this.status = newRule.status; this.debtRemediationFunction = newRule.debtRemediationFunction; this.gapDescription = newRule.gapDescription; this.scope = newRule.scope == null ? RuleScope.MAIN : newRule.scope; this.type = newRule.type == null ? RuleTagsToTypeConverter.convert(newRule.tags) : newRule.type; this.tags = ImmutableSortedSet.copyOf(Sets.difference(newRule.tags, RuleTagsToTypeConverter.RESERVED_TAGS)); this.securityStandards = ImmutableSortedSet.copyOf(newRule.securityStandards); Map<String, Param> paramsBuilder = new HashMap<>(); for (NewParam newParam : newRule.paramsByKey.values()) { paramsBuilder.put(newParam.key, new Param(newParam)); } this.params = Collections.unmodifiableMap(paramsBuilder); this.activatedByDefault = newRule.activatedByDefault; this.deprecatedRuleKeys = ImmutableSortedSet.copyOf(newRule.deprecatedRuleKeys); }
private static Rules createRules(StandaloneRuleDefinitionsLoader pluginRulesLoader) { RulesBuilder builder = new RulesBuilder(); for (RulesDefinition.Repository repoDef : pluginRulesLoader.getContext().repositories()) { for (RulesDefinition.Rule ruleDef : repoDef.rules()) { if (ruleDef.type() == RuleType.SECURITY_HOTSPOT) { continue; } NewRule newRule = builder.add(RuleKey.of(ruleDef.repository().key(), ruleDef.key())) .setInternalKey(ruleDef.internalKey()) .setDescription(ruleDef.htmlDescription() != null ? ruleDef.htmlDescription() : Markdown.convertToHtml(ruleDef.markdownDescription())) .setSeverity(ruleDef.severity()) .setType(ruleDef.type() != null ? ruleDef.type().toString() : null) .setName(ruleDef.name()); for (Param p : ruleDef.params()) { newRule.addParam(p.key()) .setDescription(p.description()); } } } return builder.build(); } }
private Rule(Repository repository, NewRule newRule) { this.repository = repository; this.repoKey = newRule.repoKey; this.key = newRule.key; this.name = newRule.name; this.htmlDescription = newRule.htmlDescription; this.markdownDescription = newRule.markdownDescription; this.internalKey = newRule.internalKey; this.severity = newRule.severity; this.template = newRule.template; this.status = newRule.status; this.debtSubCharacteristic = newRule.debtSubCharacteristic; this.debtRemediationFunction = newRule.debtRemediationFunction; this.effortToFixDescription = newRule.effortToFixDescription; this.tags = ImmutableSortedSet.copyOf(newRule.tags); ImmutableMap.Builder<String, Param> paramsBuilder = ImmutableMap.builder(); for (NewParam newParam : newRule.paramsByKey.values()) { paramsBuilder.put(newParam.key, new Param(newParam)); } this.params = paramsBuilder.build(); }
@Test public void test() { MyCustomPhpRulesDefinition rulesDefinition = new MyCustomPhpRulesDefinition(); RulesDefinition.Context context = new RulesDefinition.Context(); rulesDefinition.define(context); RulesDefinition.Repository repository = context.repository(REPOSITORY_KEY); assertThat(repository.name()).isEqualTo(REPOSITORY_NAME); assertThat(repository.language()).isEqualTo(PHPCustomRulesDefinition.LANGUAGE_KEY); assertThat(repository.rules()).hasSize(1); RulesDefinition.Rule customRule = repository.rule(RULE_KEY); assertThat(customRule).isNotNull(); assertThat(customRule.name()).isEqualTo(RULE_NAME); assertThat(customRule.htmlDescription()).isEqualTo("desc"); assertThat(customRule.tags()).contains("mybug"); assertThat(customRule.params()).hasSize(1); RulesDefinition.Param param = customRule.param("customParam"); assertThat(param.key()).isEqualTo("customParam"); assertThat(param.description()).isEqualTo("Custom parameter"); assertThat(param.defaultValue()).isEqualTo("value"); }
private static boolean mergeParam(RuleParamDto paramDto, RulesDefinition.Param paramDef) { boolean changed = false; if (!StringUtils.equals(paramDto.getType(), paramDef.type().toString())) { paramDto.setType(paramDef.type().toString()); changed = true; } if (!StringUtils.equals(paramDto.getDefaultValue(), paramDef.defaultValue())) { paramDto.setDefaultValue(paramDef.defaultValue()); changed = true; } if (!StringUtils.equals(paramDto.getDescription(), paramDef.description())) { paramDto.setDescription(paramDef.description()); changed = true; } return changed; }
private static Rule toRuleNotNull(RulesDefinition.Rule ruleDef) { Rule rule = Rule.create(ruleDef.repository().key(), ruleDef.key()) .setName(ruleDef.name()) .setSeverity(RulePriority.valueOf(ruleDef.severity())) .setLanguage(ruleDef.repository().language()) .setIsTemplate(ruleDef.template()) .setConfigKey(ruleDef.internalKey()); for (Param param : ruleDef.params()) { rule.createParameter(param.key()).setDefaultValue(param.defaultValue()).setDescription(param.description()); } return rule; }
private void assertAllRuleParametersHaveDescription(RulesDefinition.Repository repository) { for (RulesDefinition.Rule rule : repository.rules()) { for (RulesDefinition.Param param : rule.params()) { assertThat(param.description()).as("description for " + param.key()).isNotEmpty(); } } } }
RuleParamDto paramDto = existingParamsByName.get(param.key()); if (paramDto != null) { continue; .setName(param.key()) .setDescription(param.description()) .setDefaultValue(param.defaultValue()) .setType(param.type().toString()); dbClient.ruleDao().insertRuleParam(session, rule, paramDto); if (StringUtils.isEmpty(param.defaultValue())) { continue; ActiveRuleParamDto activeParam = ActiveRuleParamDto.createFor(paramDto).setValue(param.defaultValue()); dbClient.activeRuleDao().insertParam(session, activeRule, activeParam);
RulesDefinition.Param param = rule.param("format"); assertThat(param).isNotNull(); assertThat(param.key()).isEqualTo("format"); assertThat(param.name()).isEqualTo("format"); assertThat(param.description()).isEqualTo("Regular expression"); assertThat(param.defaultValue()).isEqualTo("A-Z");
@Test public void override_annotation_programmatically() { RulesDefinition.Context context = new RulesDefinition.Context(); RulesDefinition.NewRepository newRepository = context.createRepository("squid", "java"); NewRule newRule = annotationLoader.loadRule(newRepository, RuleWithProperty.class); newRule.setName("Overridden name"); newRule.param("property").setDefaultValue("true"); newRule.param("property").setDescription("Overridden"); newRepository.done(); RulesDefinition.Repository repository = context.repository("squid"); assertThat(repository.rules()).hasSize(1); RulesDefinition.Rule rule = repository.rules().get(0); assertThat(rule.key()).isEqualTo("foo"); assertThat(rule.status()).isEqualTo(RuleStatus.BETA); assertThat(rule.name()).isEqualTo("Overridden name"); assertThat(rule.htmlDescription()).isEqualTo("Foo Bar"); assertThat(rule.severity()).isEqualTo(Severity.BLOCKER); assertThat(rule.params()).hasSize(1); RulesDefinition.Param prop = rule.param("property"); assertThat(prop.key()).isEqualTo("property"); assertThat(prop.description()).isEqualTo("Overridden"); assertThat(prop.defaultValue()).isEqualTo("true"); assertThat(prop.type()).isEqualTo(RuleParamType.STRING); }
@Test public void use_l10n_bundles() { RulesDefinition.Context context = new RulesDefinition.Context(); when(i18n.getName("checkstyle", "ConstantName")).thenReturn("Constant Name"); when(i18n.getDescription("checkstyle", "ConstantName")).thenReturn("Checks that constant names conform to the specified format"); when(i18n.getParamDescription("checkstyle", "ConstantName", "format")).thenReturn("Regular expression"); new DeprecatedRulesDefinitionLoader(i18n, debtModelRepository, importer, pluginRepository, new RuleRepository[] {new UseBundles()}).complete(context); RulesDefinition.Repository checkstyle = context.repository("checkstyle"); RulesDefinition.Rule rule = checkstyle.rule("ConstantName"); assertThat(rule.key()).isEqualTo("ConstantName"); assertThat(rule.name()).isEqualTo("Constant Name"); assertThat(rule.htmlDescription()).isEqualTo("Checks that constant names conform to the specified format"); RulesDefinition.Param param = rule.param("format"); assertThat(param.key()).isEqualTo("format"); assertThat(param.name()).isEqualTo("format"); assertThat(param.description()).isEqualTo("Regular expression"); }
private static boolean mergeParam(RuleParamDto paramDto, RulesDefinition.Param paramDef) { boolean changed = false; if (!StringUtils.equals(paramDto.getType(), paramDef.type().toString())) { paramDto.setType(paramDef.type().toString()); changed = true; } if (!StringUtils.equals(paramDto.getDefaultValue(), paramDef.defaultValue())) { paramDto.setDefaultValue(paramDef.defaultValue()); changed = true; } if (!StringUtils.equals(paramDto.getDescription(), paramDef.description())) { paramDto.setDescription(paramDef.description()); changed = true; } return changed; }
@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); }
@Test public void rule_with_property() { RulesDefinition.Repository repository = load(RuleWithProperty.class); assertThat(repository.rules()).hasSize(1); RulesDefinition.Rule rule = repository.rules().get(0); assertThat(rule.key()).isEqualTo("foo"); assertThat(rule.status()).isEqualTo(RuleStatus.BETA); assertThat(rule.name()).isEqualTo("bar"); assertThat(rule.htmlDescription()).isEqualTo("Foo Bar"); assertThat(rule.severity()).isEqualTo(Severity.BLOCKER); assertThat(rule.params()).hasSize(1); assertThat(rule.tags()).isEmpty(); RulesDefinition.Param prop = rule.param("property"); assertThat(prop.key()).isEqualTo("property"); assertThat(prop.description()).isEqualTo("Ignore ?"); assertThat(prop.defaultValue()).isEqualTo("false"); assertThat(prop.type()).isEqualTo(RuleParamType.STRING); }
@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"); } }