private Optional<RuleDefinitionDto> getDbRuleFor(RulesDefinition.Rule ruleDef) { RuleKey ruleKey = RuleKey.of(ruleDef.repository().key(), ruleDef.key()); Optional<RuleDefinitionDto> res = Stream.concat(Stream.of(ruleKey), ruleDef.deprecatedRuleKeys().stream()) .map(dbRules::get) .filter(Objects::nonNull) .findFirst(); // may occur in case of plugin downgrade if (!res.isPresent()) { return Optional.ofNullable(dbRulesByDbDeprecatedKey.get(ruleKey)); } return res; }
private static void verifyRuleKeyConsistency(List<RulesDefinition.ExtendedRepository> repositories, RegisterRulesContext registerRulesContext) { List<RulesDefinition.Rule> definedRules = repositories.stream() .flatMap(r -> r.rules().stream()) .collect(toList()); Set<RuleKey> definedRuleKeys = definedRules.stream() .map(r -> RuleKey.of(r.repository().key(), r.key())) .collect(toSet()); List<RuleKey> definedDeprecatedRuleKeys = definedRules.stream() .flatMap(r -> r.deprecatedRuleKeys().stream()) .collect(toList()); // Find duplicates in declared deprecated rule keys Set<RuleKey> duplicates = findDuplicates(definedDeprecatedRuleKeys); checkState(duplicates.isEmpty(), "The following deprecated rule keys are declared at least twice [%s]", lazyToString(() -> duplicates.stream().map(RuleKey::toString).collect(Collectors.joining(",")))); // Find rule keys that are both deprecated and used Set<RuleKey> intersection = intersection(new HashSet<>(definedRuleKeys), new HashSet<>(definedDeprecatedRuleKeys)).immutableCopy(); checkState(intersection.isEmpty(), "The following rule keys are declared both as deprecated and used key [%s]", lazyToString(() -> intersection.stream().map(RuleKey::toString).collect(Collectors.joining(",")))); // Find incorrect usage of deprecated keys ImmutableMap<RuleKey, SingleDeprecatedRuleKey> dbDeprecatedRuleKeysByOldRuleKey = registerRulesContext.getDbDeprecatedKeysByOldRuleKey(); Set<String> incorrectRuleKeyMessage = definedRules.stream() .flatMap(r -> filterInvalidDeprecatedRuleKeys(dbDeprecatedRuleKeysByOldRuleKey, r)) .filter(Objects::nonNull) .collect(Collectors.toSet()); checkState(incorrectRuleKeyMessage.isEmpty(), "An incorrect state of deprecated rule keys has been detected.\n %s", lazyToString(() -> incorrectRuleKeyMessage.stream().collect(Collectors.joining("\n")))); }
private RepositoryImpl(NewRepositoryImpl newRepository, @Nullable Repository mergeInto) { this.key = newRepository.key; this.language = newRepository.language; this.isExternal = newRepository.isExternal; Map<String, Rule> ruleBuilder = new HashMap<>(); if (mergeInto != null) { if (!StringUtils.equals(newRepository.language, mergeInto.language()) || !StringUtils.equals(newRepository.key, mergeInto.key())) { throw new IllegalArgumentException(format("Bug - language and key of the repositories to be merged should be the sames: %s and %s", newRepository, mergeInto)); } this.name = StringUtils.defaultIfBlank(mergeInto.name(), newRepository.name); for (Rule rule : mergeInto.rules()) { if (!newRepository.key().startsWith("common-") && ruleBuilder.containsKey(rule.key())) { Loggers.get(getClass()).warn("The rule '{}' of repository '{}' is declared several times", rule.key(), mergeInto.key()); } ruleBuilder.put(rule.key(), rule); } } else { this.name = newRepository.name; } for (NewRule newRule : newRepository.newRules.values()) { newRule.validate(); ruleBuilder.put(newRule.key, new Rule(this, newRule)); } this.rulesByKey = unmodifiableMap(ruleBuilder); }
private RuleDefinitionDto createRuleDto(RulesDefinition.Rule ruleDef, DbSession session) { RuleDefinitionDto ruleDto = new RuleDefinitionDto() .setRuleKey(RuleKey.of(ruleDef.repository().key(), ruleDef.key())) .setPluginKey(ruleDef.pluginKey()) .setIsTemplate(ruleDef.template())
RulesDefinition.Rule rule = checkstyle.rule("ConstantName"); assertThat(rule).isNotNull(); assertThat(rule.key()).isEqualTo("ConstantName"); assertThat(rule.pluginKey()).isEqualTo("unittest"); assertThat(rule.name()).isEqualTo("Constant Name");
@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 static Stream<String> filterInvalidDeprecatedRuleKeys(ImmutableMap<RuleKey, SingleDeprecatedRuleKey> dbDeprecatedRuleKeysByOldRuleKey, RulesDefinition.Rule rule) { return rule.deprecatedRuleKeys().stream() .map(rk -> { SingleDeprecatedRuleKey singleDeprecatedRuleKey = dbDeprecatedRuleKeysByOldRuleKey.get(rk); if (singleDeprecatedRuleKey == null) { // new deprecated rule key : OK return null; } RuleKey parentRuleKey = RuleKey.of(rule.repository().key(), rule.key()); if (parentRuleKey.equals(singleDeprecatedRuleKey.getNewRuleKeyAsRuleKey())) { // same parent : OK return null; } if (rule.deprecatedRuleKeys().contains(parentRuleKey)) { // the new rule is deprecating the old parentRuleKey : OK return null; } return format("The deprecated rule key [%s] was previously deprecated by [%s]. [%s] should be a deprecated key of [%s],", rk.toString(), singleDeprecatedRuleKey.getNewRuleKeyAsRuleKey().toString(), singleDeprecatedRuleKey.getNewRuleKeyAsRuleKey().toString(), RuleKey.of(rule.repository().key(), rule.key()).toString()); }); }
@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 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, pluginRepository, 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"); }
@Test public void test_creation_from_RulesDefinitionRule() { // Creation from RulesDefinition.Rule ImmutableSet<RuleKey> deprecatedRuleKeys = ImmutableSet.of( RuleKey.of(randomAlphanumeric(50), randomAlphanumeric(50)), RuleKey.of(randomAlphanumeric(50), randomAlphanumeric(50)), RuleKey.of(randomAlphanumeric(50), randomAlphanumeric(50)) ); RulesDefinition.Repository repository = mock(RulesDefinition.Repository.class); when(repository.key()).thenReturn(randomAlphanumeric(50)); RulesDefinition.Rule rule = mock(RulesDefinition.Rule.class); when(rule.key()).thenReturn(randomAlphanumeric(50)); when(rule.deprecatedRuleKeys()).thenReturn(deprecatedRuleKeys); when(rule.repository()).thenReturn(repository); Set<SingleDeprecatedRuleKey> singleDeprecatedRuleKeys = SingleDeprecatedRuleKey.from(rule); assertThat(singleDeprecatedRuleKeys).hasSize(deprecatedRuleKeys.size()); assertThat(singleDeprecatedRuleKeys) .extracting(SingleDeprecatedRuleKey::getUuid, SingleDeprecatedRuleKey::getOldRepositoryKey, SingleDeprecatedRuleKey::getOldRuleKey, SingleDeprecatedRuleKey::getNewRepositoryKey, SingleDeprecatedRuleKey::getNewRuleKey, SingleDeprecatedRuleKey::getOldRuleKeyAsRuleKey) .containsExactlyInAnyOrder( deprecatedRuleKeys.stream().map( r -> tuple(null, r.repository(), r.rule(), rule.repository().key(), rule.key(), RuleKey.of(r.repository(), r.rule())) ).collect(MoreCollectors.toArrayList(deprecatedRuleKeys.size())).toArray(new Tuple[deprecatedRuleKeys.size()]) ); }
@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 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 define_rule_with_default_fields() { RulesDefinition.NewRepository newFindbugs = context.createRepository("findbugs", "java"); newFindbugs.createRule("NPE").setName("NPE").setHtmlDescription("NPE"); newFindbugs.done(); RulesDefinition.Rule rule = context.repository("findbugs").rule("NPE"); assertThat(rule.key()).isEqualTo("NPE"); assertThat(rule.severity()).isEqualTo(Severity.MAJOR); assertThat(rule.params()).isEmpty(); assertThat(rule.internalKey()).isNull(); assertThat(rule.status()).isEqualTo(RuleStatus.defaultStatus()); assertThat(rule.tags()).isEmpty(); assertThat(rule.securityStandards()).isEmpty(); assertThat(rule.debtRemediationFunction()).isNull(); }
private static boolean noTemplateRuleWithOrganizationsEnabled(RegisterRulesContext registerRulesContext, boolean orgsEnabled, RulesDefinition.Rule ruleDef) { if (!ruleDef.template() || !orgsEnabled) { return false; } Optional<RuleDefinitionDto> dbRule = registerRulesContext.getDbRuleFor(ruleDef); if (dbRule.isPresent() && dbRule.get().getStatus() == RuleStatus.REMOVED) { RuleDefinitionDto dto = dbRule.get(); LOG.debug("Template rule {} kept removed, because organizations are enabled.", dto.getKey()); registerRulesContext.removed(dto); } else { LOG.info("Template rule {} will not be imported, because organizations are enabled.", RuleKey.of(ruleDef.repository().key(), ruleDef.key())); } return true; }
@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(); }
public static Set<SingleDeprecatedRuleKey> from(RulesDefinition.Rule rule) { return rule.deprecatedRuleKeys().stream() .map(r -> new SingleDeprecatedRuleKey() .setNewRepositoryKey(rule.repository().key()) .setNewRuleKey(rule.key()) .setOldRepositoryKey(r.repository()) .setOldRuleKey(r.rule())) .collect(MoreCollectors.toSet(rule.deprecatedRuleKeys().size())); }
@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'); }
public ScannerMediumTester addRules(RulesDefinition rulesDefinition) { RulesDefinition.Context context = new RulesDefinition.Context(); rulesDefinition.define(context); List<Repository> repositories = context.repositories(); for (Repository repo : repositories) { for (RulesDefinition.Rule rule : repo.rules()) { this.addRule(rule.key(), rule.repository().key(), rule.internalKey(), rule.name()); } } return this; }
@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 use_classname_when_missing_key() { RulesDefinition.Repository repository = load(RuleWithoutKey.class); assertThat(repository.rules()).hasSize(1); RulesDefinition.Rule rule = repository.rules().get(0); assertThat(rule.key()).isEqualTo(RuleWithoutKey.class.getCanonicalName()); assertThat(rule.name()).isEqualTo("foo"); }