/** * @return the key of the active rule */ public String getRuleKey() { return rule.getKey(); }
@Override public boolean equals(Object obj) { if (!(obj instanceof Rule)) { return false; } if (this == obj) { return true; } Rule other = (Rule) obj; return new EqualsBuilder() .append(pluginName, other.getRepositoryKey()) .append(key, other.getKey()) .isEquals(); }
/** * Note: disabled rules are excluded. */ @CheckForNull public ActiveRule getActiveRule(Rule rule) { return getActiveRule(rule.getRepositoryKey(), rule.getKey()); }
@CheckForNull private String ruleDescription(String repositoryKey, org.sonar.api.rules.Rule rule) { String description = i18n.getDescription(repositoryKey, rule.getKey()); if (StringUtils.isNotBlank(description)) { return description; } return StringUtils.defaultIfBlank(rule.getDescription(), null); }
/** * @since 3.6 */ public RuleKey ruleKey() { return RuleKey.of(getRepositoryKey(), getKey()); }
@CheckForNull private String ruleName(String repositoryKey, org.sonar.api.rules.Rule rule) { String name = i18n.getName(repositoryKey, rule.getKey()); if (StringUtils.isNotBlank(name)) { return name; } return StringUtils.defaultIfBlank(rule.getName(), null); }
@Override @CheckForNull public String getName(Rule rule) { String name = message(rule.getRepositoryKey(), rule.getKey(), NAME_SUFFIX); return name != null ? name : rule.getName(); }
/** * @param optionalSeverity if null, then the default rule severity is used */ public ActiveRule activateRule(final Rule rule, @Nullable RulePriority optionalSeverity) { if (activeRules.stream().anyMatch(ar -> ar.getRule().equals(rule))) { throw MessageException.of(String.format( "The definition of the profile '%s' (language '%s') contains multiple occurrences of the '%s:%s' rule. The plugin which declares this profile should fix this.", getName(), getLanguage(), rule.getRepositoryKey(), rule.getKey())); } ActiveRule activeRule = new ActiveRule(this, rule, optionalSeverity); activeRules.add(activeRule); return activeRule; }
@Test public void rule_without_key() { List<Rule> rules = parseAnnotatedClass(RuleWithoutKey.class); assertThat(rules).hasSize(1); Rule rule = rules.get(0); assertThat(rule.getKey()).isEqualTo(RuleWithoutKey.class.getCanonicalName()); assertThat(rule.getName()).isEqualTo("foo"); assertThat(rule.getDescription()).isNull(); assertThat(rule.getSeverity()).isEqualTo(RulePriority.MAJOR); }
@Test public void overridden_rule() { List<Rule> rules = parseAnnotatedClass(OverridingRule.class); assertThat(rules).hasSize(1); Rule rule = rules.get(0); assertThat(rule.getKey()).isEqualTo("overriding_foo"); assertThat(rule.getName()).isEqualTo("Overriding Foo"); assertThat(rule.getDescription()).isNull(); assertThat(rule.getSeverity()).isEqualTo(RulePriority.MAJOR); assertThat(rule.getParams()).hasSize(2); }
@Test public void rule_without_name_nor_description() { List<Rule> rules = parseAnnotatedClass(RuleWithoutNameNorDescription.class); assertThat(rules).hasSize(1); Rule rule = rules.get(0); assertThat(rule.getKey()).isEqualTo("foo"); assertThat(rule.getSeverity()).isEqualTo(RulePriority.MAJOR); assertThat(rule.getName()).isNull(); assertThat(rule.getDescription()).isNull(); }
if (Strings.isNullOrEmpty(rule.getKey())) { throw new SonarException("Node <key> is missing in <rule>");
private static RuleKey toRuleKey(Rule rule) { return RuleKey.of(rule.getRepositoryKey(), rule.getKey()); }
@Test public void rule_with_property() { List<Rule> rules = parseAnnotatedClass(RuleWithProperty.class); assertThat(rules).hasSize(1); Rule rule = rules.get(0); assertThat(rule.getKey()).isEqualTo("foo"); assertThat(rule.getName()).isEqualTo("bar"); assertThat(rule.getDescription()).isEqualTo("Foo Bar"); assertThat(rule.getSeverity()).isEqualTo(RulePriority.BLOCKER); assertThat(rule.getStatus()).isEqualTo(Rule.STATUS_READY); assertThat(rule.getParams()).hasSize(1); RuleParam prop = rule.getParam("property"); assertThat(prop.getKey()).isEqualTo("property"); assertThat(prop.getDescription()).isEqualTo("Ignore ?"); assertThat(prop.getDefaultValue()).isEqualTo("false"); assertThat(prop.getType()).isEqualTo(PropertyType.STRING.name()); }
private void verifyRule(Rule rule, RuleDefinitionDto ruleDefinition, RuleParamDto ruleParam) { assertThat(rule).isNotNull(); assertThat(rule.getName()).isEqualTo(ruleDefinition.getName()); assertThat(rule.getLanguage()).isEqualTo(ruleDefinition.getLanguage()); assertThat(rule.getKey()).isEqualTo(ruleDefinition.getRuleKey()); assertThat(rule.getConfigKey()).isEqualTo(ruleDefinition.getConfigKey()); assertThat(rule.isTemplate()).isEqualTo(ruleDefinition.isTemplate()); assertThat(rule.getCreatedAt().getTime()).isEqualTo(ruleDefinition.getCreatedAt()); assertThat(rule.getUpdatedAt().getTime()).isEqualTo(ruleDefinition.getUpdatedAt()); assertThat(rule.getRepositoryKey()).isEqualTo(ruleDefinition.getRepositoryKey()); assertThat(rule.getSeverity().name()).isEqualTo(ruleDefinition.getSeverityString()); assertThat(rule.getSystemTags()).isEqualTo(ruleDefinition.getSystemTags().stream().toArray(String[]::new)); assertThat(rule.getTags()).isEmpty(); assertThat(rule.getId()).isEqualTo(ruleDefinition.getId()); assertThat(rule.getDescription()).isEqualTo(ruleDefinition.getDescription()); assertThat(rule.getParams()).hasSize(1); org.sonar.api.rules.RuleParam param = rule.getParams().iterator().next(); assertThat(param.getRule()).isSameAs(rule); assertThat(param.getKey()).isEqualTo(ruleParam.getName()); assertThat(param.getDescription()).isEqualTo(ruleParam.getDescription()); assertThat(param.getType()).isEqualTo(ruleParam.getType()); assertThat(param.getDefaultValue()).isEqualTo(ruleParam.getDefaultValue()); } }
@Test public void should_success_finder_wrap() { // has Id assertThat(underTest.findById(rule1.getId()).getId()).isEqualTo(rule1.getId()); // should_find_by_id assertThat(underTest.findById(rule3.getId()).getConfigKey()).isEqualTo("Checker/Treewalker/AnnotationUseStyleCheck"); // should_not_find_disabled_rule_by_id assertThat(underTest.findById(rule2.getId())).isNull(); // should_find_by_key Rule rule = underTest.findByKey("checkstyle", "com.puppycrawl.tools.checkstyle.checks.header.HeaderCheck"); assertThat(rule).isNotNull(); assertThat(rule.getKey()).isEqualTo(("com.puppycrawl.tools.checkstyle.checks.header.HeaderCheck")); assertThat(rule.isEnabled()).isTrue(); // find_should_return_null_if_no_results assertThat(underTest.findByKey("checkstyle", "unknown")).isNull(); assertThat(underTest.find(RuleQuery.create().withRepositoryKey("checkstyle").withConfigKey("unknown"))).isNull(); // find_repository_rules assertThat(underTest.findAll(RuleQuery.create().withRepositoryKey("checkstyle"))).hasSize(2); // find_all_enabled assertThat(underTest.findAll(RuleQuery.create())).extracting("id").containsOnly(rule1.getId(), rule3.getId(), rule4.getId()); assertThat(underTest.findAll(RuleQuery.create())).hasSize(3); // do_not_find_disabled_rules assertThat(underTest.findByKey("checkstyle", "DisabledCheck")).isNull(); // do_not_find_unknown_rules assertThat(underTest.findAll(RuleQuery.create().withRepositoryKey("unknown_repository"))).isEmpty(); }
RulesDefinition.NewRule newRule = newRepository.createRule(rule.getKey()); newRule.setName(ruleName(repository.getKey(), rule)); newRule.setHtmlDescription(ruleDescription(repository.getKey(), rule)); 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);
/** * @return the key of the active rule */ public String getRuleKey() { return rule.getKey(); }
/** * Note: disabled rules are excluded. */ @CheckForNull public ActiveRule getActiveRule(Rule rule) { return getActiveRule(rule.getRepositoryKey(), rule.getKey()); }
/** * @since 3.6 */ public RuleKey ruleKey() { return RuleKey.of(getRepositoryKey(), getKey()); }