Tabnine Logo
Rule
Code IndexAdd Tabnine to your IDE (free)

How to use
Rule
in
org.sonar.api.rules

Best Java code snippets using org.sonar.api.rules.Rule (Showing top 20 results out of 369)

Refine searchRefine arrow

  • RulesProfile
  • Assertions
  • RuleKey
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()]));
org.sonar.api.rulesRule

Most used methods

  • create
    Create with all required fields
  • getKey
  • getConfigKey
  • setConfigKey
    Sets the configuration key
  • setName
    Sets the rule name
  • setSeverity
  • getName
  • getRepositoryKey
  • ruleKey
  • setDescription
    Sets the rule description
  • createParameter
  • getSeverity
  • createParameter,
  • getSeverity,
  • setTags,
  • <init>,
  • getParam,
  • getParams,
  • setKey,
  • setStatus,
  • getDescription,
  • setRepositoryKey

Popular in Java

  • Creating JSON documents from java classes using gson
  • findViewById (Activity)
  • requestLocationUpdates (LocationManager)
  • orElseThrow (Optional)
    Return the contained value, if present, otherwise throw an exception to be created by the provided s
  • Path (java.nio.file)
  • SQLException (java.sql)
    An exception that indicates a failed JDBC operation. It provides the following information about pro
  • SortedSet (java.util)
    SortedSet is a Set which iterates over its elements in a sorted order. The order is determined eithe
  • ConcurrentHashMap (java.util.concurrent)
    A plug-in replacement for JDK1.5 java.util.concurrent.ConcurrentHashMap. This version is based on or
  • JFrame (javax.swing)
  • XPath (javax.xml.xpath)
    XPath provides access to the XPath evaluation environment and expressions. Evaluation of XPath Expr
  • From CI to AI: The AI layer in your organization
Tabnine Logo
  • Products

    Search for Java codeSearch for JavaScript code
  • IDE Plugins

    IntelliJ IDEAWebStormVisual StudioAndroid StudioEclipseVisual Studio CodePyCharmSublime TextPhpStormVimGoLandRubyMineEmacsJupyter NotebookJupyter LabRiderDataGripAppCode
  • Company

    About UsContact UsCareers
  • Resources

    FAQBlogTabnine AcademyTerms of usePrivacy policyJava Code IndexJavascript Code Index
Get Tabnine for your IDE now