本文整理了Java中org.sonar.api.rules.Rule
类的一些代码示例,展示了Rule
类的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Rule
类的具体详情如下:
包路径:org.sonar.api.rules.Rule
类名称:Rule
暂无
代码示例来源:origin: SonarSource/sonarqube
@Test
public void activateRuleWithDefaultPriority() {
RulesProfile profile = RulesProfile.create();
Rule rule = Rule.create("repo", "key1", "name1").setSeverity(RulePriority.CRITICAL);
profile.activateRule(rule, null);
assertThat(profile.getActiveRule("repo", "key1").getSeverity()).isEqualTo(RulePriority.CRITICAL);
}
代码示例来源:origin: SonarSource/sonarqube
@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);
}
代码示例来源:origin: SonarSource/sonarqube
/**
* @return the config key the active rule belongs to
*/
public String getConfigKey() {
return rule.getConfigKey();
}
代码示例来源:origin: SonarSource/sonarqube
/**
* Create with all required fields
*/
public static Rule create(String repositoryKey, String key, String name) {
return new Rule().setUniqueKey(repositoryKey, key).setName(name);
}
代码示例来源:origin: SonarSource/sonarqube
public Rule setUniqueKey(String repositoryKey, String key) {
return setRepositoryKey(repositoryKey).setKey(key).setConfigKey(key);
}
代码示例来源:origin: SonarSource/sonarqube
/**
* @since 3.6
*/
public RuleKey ruleKey() {
return RuleKey.of(getRepositoryKey(), getKey());
}
代码示例来源:origin: SonarSource/sonarqube
@Test
public void should_remove_new_line_characters_in_name_with_second_constructor() {
Rule rule;
for (String example : getExamplesContainingNewLineCharacter()) {
rule = new Rule(null, null).setName(example);
assertThat(rule.getName()).isEqualTo("test");
}
}
代码示例来源:origin: SonarSource/sonarqube
@Test
public void shouldMatchRule() {
RuleKey rule = Rule.create("checkstyle", "IllegalRegexp", "").ruleKey();
assertThat(new IssuePattern("*", "*").matchRule(rule)).isTrue();
assertThat(new IssuePattern("*", "checkstyle:*").matchRule(rule)).isTrue();
assertThat(new IssuePattern("*", "checkstyle:IllegalRegexp").matchRule(rule)).isTrue();
assertThat(new IssuePattern("*", "checkstyle:Illegal*").matchRule(rule)).isTrue();
assertThat(new IssuePattern("*", "*:*Illegal*").matchRule(rule)).isTrue();
assertThat(new IssuePattern("*", "pmd:IllegalRegexp").matchRule(rule)).isFalse();
assertThat(new IssuePattern("*", "pmd:*").matchRule(rule)).isFalse();
assertThat(new IssuePattern("*", "*:Foo*IllegalRegexp").matchRule(rule)).isFalse();
}
代码示例来源:origin: SonarSource/sonarqube
@Test
public void default_priority_is_major() {
Rule rule = Rule.create();
assertThat(rule.getSeverity()).isEqualTo(RulePriority.MAJOR);
rule = new Rule("name", "key");
assertThat(rule.getSeverity()).isEqualTo(RulePriority.MAJOR);
rule.setSeverity(RulePriority.BLOCKER);
assertThat(rule.getSeverity()).isEqualTo(RulePriority.BLOCKER);
rule.setSeverity(null);
assertThat(rule.getSeverity()).isEqualTo(RulePriority.MAJOR);
}
代码示例来源:origin: SonarSource/sonarqube
@Test
public void should_set_valid_status() {
Rule rule = Rule.create().setStatus(Rule.STATUS_DEPRECATED);
assertThat(rule.getStatus()).isEqualTo(Rule.STATUS_DEPRECATED);
rule = Rule.create().setStatus(Rule.STATUS_REMOVED);
assertThat(rule.getStatus()).isEqualTo(Rule.STATUS_REMOVED);
rule = Rule.create().setStatus(Rule.STATUS_BETA);
assertThat(rule.getStatus()).isEqualTo(Rule.STATUS_BETA);
rule = Rule.create().setStatus(Rule.STATUS_READY);
assertThat(rule.getStatus()).isEqualTo(Rule.STATUS_READY);
}
代码示例来源:origin: SonarSource/sonarqube
@Test
public void searchRulesByConfigKey() {
RulesProfile profile = RulesProfile.create();
profile.activateRule(Rule.create("repo", "key1", "name1"), null);
profile.activateRule(Rule.create("repo", "key2", "name2").setConfigKey("config2"), null);
assertThat(profile.getActiveRuleByConfigKey("repo", "unknown")).isNull();
assertThat(profile.getActiveRuleByConfigKey("repo", "config2").getRuleKey()).isEqualTo("key2");
}
代码示例来源:origin: SonarSource/sonarqube
@Test
public void description_should_be_cleaned() {
Rule rule = Rule.create().setDescription(" my description ");
Assert.assertEquals("my description", rule.getDescription());
rule.setDescription(null);
assertThat(rule.getDescription()).isNull();
}
代码示例来源:origin: SonarSource/sonarqube
@Test
public void should_remove_new_line_characters_in_name_with_setter() {
Rule rule = Rule.create();
for (String example : getExamplesContainingNewLineCharacter()) {
rule.setName(example);
assertThat(rule.getName()).isEqualTo("test");
}
}
代码示例来源:origin: SonarSource/sonarqube
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());
}
}
代码示例来源:origin: SonarSource/sonarqube
@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());
}
代码示例来源:origin: SonarSource/sonarqube
@Test
public void testTags() {
Rule rule = Rule.create();
assertThat(rule.getTags()).isEmpty();
assertThat(rule.getSystemTags()).isEmpty();
rule.setTags(new String[] {"tag1", "tag2"});
assertThat(rule.getTags()).containsOnly("tag1", "tag2");
assertThat(rule.getSystemTags()).isEmpty();
}
代码示例来源:origin: SonarSource/sonarqube
@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();
}
代码示例来源:origin: SonarSource/sonarqube
RuleDto.Format descriptionFormat = ruleDefinition.getDescriptionFormat();
Rule apiRule = new Rule();
apiRule
.setName(ruleDefinition.getName())
.setLanguage(ruleDefinition.getLanguage())
.setKey(ruleDefinition.getRuleKey())
.setConfigKey(ruleDefinition.getConfigKey())
.setIsTemplate(ruleDefinition.isTemplate())
.setCreatedAt(new Date(ruleDefinition.getCreatedAt()))
.setUpdatedAt(new Date(ruleDefinition.getUpdatedAt()))
.setRepositoryKey(ruleDefinition.getRepositoryKey())
.setSeverity(severity != null ? RulePriority.valueOf(severity) : null)
.setStatus(ruleDefinition.getStatus().name())
.setSystemTags(ruleDefinition.getSystemTags().toArray(new String[ruleDefinition.getSystemTags().size()]))
.setTags(new String[0])
.setId(ruleDefinition.getId());
if (description != null && descriptionFormat != null) {
if (RuleDto.Format.HTML.equals(descriptionFormat)) {
apiRule.setDescription(description);
} else {
apiRule.setDescription(Markdown.convertToHtml(description));
.setDefaultValue(param.getDefaultValue()));
apiRule.setParams(apiParams);
代码示例来源:origin: org.codehaus.sonar/sonar-batch
private RulesProfile select(QProfile qProfile, ActiveRules activeRules) {
RulesProfile deprecatedProfile = new RulesProfile();
// TODO deprecatedProfile.setVersion(qProfile.version());
deprecatedProfile.setName(qProfile.getName());
deprecatedProfile.setLanguage(qProfile.getLanguage());
for (org.sonar.api.batch.rule.ActiveRule activeRule : ((DefaultActiveRules) activeRules).findByLanguage(qProfile.getLanguage())) {
Rule rule = Rule.create(activeRule.ruleKey().repository(), activeRule.ruleKey().rule());
rule.setConfigKey(activeRule.internalKey());
ActiveRule deprecatedActiveRule = deprecatedProfile.activateRule(rule,
RulePriority.valueOf(activeRule.severity()));
for (Map.Entry<String, String> param : activeRule.params().entrySet()) {
rule.createParameter(param.getKey());
deprecatedActiveRule.setParameter(param.getKey(), param.getValue());
}
}
return deprecatedProfile;
}
代码示例来源:origin: SonarSource/sonarqube
rule.setKey(StringUtils.trim(keyAttribute));
rule.setSeverity(RulePriority.valueOf(StringUtils.trim(priorityAttribute)));
rule.setName(StringUtils.trim(cursor.collectDescendantText(false)));
rule.setDescription(StringUtils.trim(cursor.collectDescendantText(false)));
rule.setKey(StringUtils.trim(cursor.collectDescendantText(false)));
rule.setConfigKey(StringUtils.trim(cursor.collectDescendantText(false)));
rule.setSeverity(RulePriority.valueOf(StringUtils.trim(cursor.collectDescendantText(false))));
rule.setCardinality(Cardinality.valueOf(StringUtils.trim(cursor.collectDescendantText(false))));
rule.setStatus(StringUtils.trim(cursor.collectDescendantText(false)));
if (Strings.isNullOrEmpty(rule.getKey())) {
throw new SonarException("Node <key> is missing in <rule>");
rule.setTags(tags.toArray(new String[tags.size()]));
内容来源于网络,如有侵权,请联系作者删除!