@Test public void support_deprecated_format() { // the deprecated format uses some attributes instead of nodes InputStream input = getClass().getResourceAsStream("RulesDefinitionXmlLoaderTest/deprecated.xml"); RulesDefinition.Repository repository = load(input, StandardCharsets.UTF_8.name()); assertThat(repository.rules()).hasSize(1); RulesDefinition.Rule rule = repository.rules().get(0); assertThat(rule.key()).isEqualTo("org.sonar.it.checkstyle.MethodsCountCheck"); assertThat(rule.internalKey()).isEqualTo("Checker/TreeWalker/org.sonar.it.checkstyle.MethodsCountCheck"); assertThat(rule.severity()).isEqualTo(Severity.CRITICAL); assertThat(rule.htmlDescription()).isEqualTo("Count methods"); assertThat(rule.param("minMethodsCount")).isNotNull(); }
@Test public void overridden_class() { RulesDefinition.Repository repository = load(OverridingRule.class); assertThat(repository.rules()).hasSize(1); RulesDefinition.Rule rule = repository.rules().get(0); assertThat(rule.key()).isEqualTo("overriding_foo"); assertThat(rule.name()).isEqualTo("Overriding Foo"); assertThat(rule.severity()).isEqualTo(Severity.MAJOR); assertThat(rule.htmlDescription()).isEqualTo("Desc of Overriding Foo"); assertThat(rule.params()).hasSize(2); }
@Test public void test_utf8_encoding() throws UnsupportedEncodingException { InputStream input = getClass().getResourceAsStream("RulesDefinitionXmlLoaderTest/utf8.xml"); RulesDefinition.Repository repository = load(input, StandardCharsets.UTF_8.name()); assertThat(repository.rules()).hasSize(1); RulesDefinition.Rule rule = repository.rules().get(0); assertThat(rule.key()).isEqualTo("com.puppycrawl.tools.checkstyle.checks.naming.LocalVariableNameCheck"); assertThat(rule.name()).isEqualTo("M & M"); assertThat(rule.htmlDescription().charAt(0)).isEqualTo('\u00E9'); assertThat(rule.htmlDescription().charAt(1)).isEqualTo('\u00E0'); assertThat(rule.htmlDescription().charAt(2)).isEqualTo('\u0026'); }
@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 mergeDescription(RulesDefinition.Rule def, RuleDefinitionDto dto) { boolean changed = false; if (def.htmlDescription() != null && !StringUtils.equals(dto.getDescription(), def.htmlDescription())) { dto.setDescription(def.htmlDescription()); dto.setDescriptionFormat(Format.HTML); changed = true; } else if (def.markdownDescription() != null && !StringUtils.equals(dto.getDescription(), def.markdownDescription())) { dto.setDescription(def.markdownDescription()); dto.setDescriptionFormat(Format.MARKDOWN); changed = true; } return changed; }
assertThat(rule.name()).isEqualTo("Detect NPE"); assertThat(rule.severity()).isEqualTo(Severity.BLOCKER); assertThat(rule.htmlDescription()).isEqualTo("Detect <code>java.lang.NullPointerException</code>"); assertThat(rule.markdownDescription()).isNull(); assertThat(rule.tags()).containsOnly("one", "two", "three", "four"); assertThat(otherRule.htmlDescription()).isNull(); assertThat(otherRule.markdownDescription()).isEqualTo("ABC");
assertThat(rule.name()).isEqualTo("Detect NPE"); assertThat(rule.severity()).isEqualTo(Severity.BLOCKER); assertThat(rule.htmlDescription()).isEqualTo("Detect <code>java.lang.NullPointerException</code>"); assertThat(rule.markdownDescription()).isNull(); assertThat(rule.tags()).containsOnly("one", "two", "three", "four"); assertThat(otherRule.htmlDescription()).isNull(); assertThat(otherRule.markdownDescription()).isEqualTo("ABC");
.setCreatedAt(system2.now()) .setUpdatedAt(system2.now()); if (ruleDef.htmlDescription() != null) { ruleDto.setDescription(ruleDef.htmlDescription()); ruleDto.setDescriptionFormat(Format.HTML); } else {
assertThat(rule.pluginKey()).isEqualTo("unittest"); assertThat(rule.name()).isEqualTo("Constant Name"); assertThat(rule.htmlDescription()).isEqualTo("Checks that constant names conform to the specified format"); assertThat(rule.severity()).isEqualTo(Severity.BLOCKER); assertThat(rule.internalKey()).isEqualTo("Checker/TreeWalker/ConstantName");
@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); }
@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); }
/** * This is temporarily accepted only for the support of the common-rules that are still declared * by plugins. It could be removed in 7.0 * @since 5.2 */ @Test public void allow_to_replace_an_existing_common_rule() { RulesDefinition.NewRepository newCommonJava1 = context.createRepository("common-java", "java").setName("Common Java"); newCommonJava1.createRule("coverage").setName("Lack of coverage").setHtmlDescription("Coverage must be high"); newCommonJava1.done(); RulesDefinition.NewRepository newCommonJava2 = context.createRepository("common-java", "java"); newCommonJava2.createRule("coverage").setName("Lack of coverage (V2)").setMarkdownDescription("Coverage must be high (V2)"); newCommonJava2.done(); RulesDefinition.Repository commonJava = context.repository("common-java"); assertThat(commonJava.rules()).hasSize(1); RulesDefinition.Rule rule = commonJava.rule("coverage"); assertThat(rule.name()).isEqualTo("Lack of coverage (V2)"); // replacement but not merge -> keep only the v2 (which has markdown but not html description) assertThat(rule.markdownDescription()).isEqualTo("Coverage must be high (V2)"); assertThat(rule.htmlDescription()).isNull(); // do not log warning assertThat(logTester.logs()).isEmpty(); }
@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"); }
@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 markdown_description() { String xml = "" + "<rules>" + " <rule>" + " <key>1</key>" + " <name>One</name>" + " <description>Desc</description>" + " <descriptionFormat>MARKDOWN</descriptionFormat>" + " </rule>" + "</rules>"; RulesDefinition.Rule rule = load(xml).rule("1"); assertThat(rule.markdownDescription()).isEqualTo("Desc"); assertThat(rule.htmlDescription()).isNull(); }
@Test public void complete_rule_name_and_description() { when(i18n.getName("squid", "S0001")).thenReturn("SOne"); when(i18n.getDescription("squid", "S0001")).thenReturn("S One"); RulesDefinition.Context context = new RulesDefinition.Context(); RulesDefinition.NewRepository repo = context.createRepository("squid", "java"); // rule without description repo.createRule("S0001"); loader.load(repo); repo.done(); RulesDefinition.Rule rule = context.repository("squid").rule("S0001"); assertThat(rule.name()).isEqualTo("SOne"); assertThat(rule.htmlDescription()).isEqualTo("S One"); }
@Test public void override_existing() { when(i18n.getName("squid", "S0001")).thenReturn("SOne"); when(i18n.getDescription("squid", "S0001")).thenReturn("S One"); RulesDefinition.Context context = new RulesDefinition.Context(); RulesDefinition.NewRepository repo = context.createRepository("squid", "java"); repo.createRule("S0001").setName("Bad").setHtmlDescription("Bad"); loader.load(repo); repo.done(); RulesDefinition.Rule rule = context.repository("squid").rule("S0001"); assertThat(rule.name()).isEqualTo("SOne"); assertThat(rule.htmlDescription()).isEqualTo("S One"); }
private static boolean mergeDescription(RulesDefinition.Rule def, RuleDefinitionDto dto) { boolean changed = false; if (def.htmlDescription() != null && !StringUtils.equals(dto.getDescription(), def.htmlDescription())) { dto.setDescription(def.htmlDescription()); dto.setDescriptionFormat(Format.HTML); changed = true; } else if (def.markdownDescription() != null && !StringUtils.equals(dto.getDescription(), def.markdownDescription())) { dto.setDescription(def.markdownDescription()); dto.setDescriptionFormat(Format.MARKDOWN); changed = true; } return changed; }
@Test public void do_not_override_if_no_bundle() { // i18n returns null values RulesDefinition.Context context = new RulesDefinition.Context(); RulesDefinition.NewRepository repo = context.createRepository("squid", "java"); repo.createRule("S0001").setName("SOne").setHtmlDescription("S One"); loader.load(repo); repo.done(); RulesDefinition.Rule rule = context.repository("squid").rule("S0001"); assertThat(rule.name()).isEqualTo("SOne"); assertThat(rule.htmlDescription()).isEqualTo("S One"); }
@Test public void load_rule_html_description_from_file() { RulesDefinition.NewRepository newRepository = context.createRepository("findbugs", "java"); newRepository.createRule("NPE").setName("NPE").setHtmlDescription(getClass().getResource("/org/sonar/api/server/rule/RulesDefinitionTest/sample.html")); newRepository.done(); RulesDefinition.Rule rule = context.repository("findbugs").rule("NPE"); assertThat(rule.htmlDescription()).isEqualTo("description of rule loaded from file"); }