Java Code Examples for org.sonar.api.server.rule.RulesDefinition#Rule

The following examples show how to use org.sonar.api.server.rule.RulesDefinition#Rule . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CustomScssRulesDefinitionTest.java    From sonar-css-plugin with GNU Lesser General Public License v3.0 7 votes vote down vote up
@Test
public void test() {
  MyCustomScssRulesDefinition rulesDefinition = new MyCustomScssRulesDefinition();
  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("scss");
  assertThat(repository.rules()).hasSize(1);

  RulesDefinition.Rule customRule = repository.rule(RULE_KEY);
  assertThat(customRule).isNotNull();
  assertThat(customRule.key()).isEqualTo(RULE_KEY);
  assertThat(customRule.name()).isEqualTo(RULE_NAME);

  RulesDefinition.Param param = repository.rules().get(0).params().get(0);
  assertThat(param.key()).isEqualTo("customParam");
  assertThat(param.description()).isEqualTo("Custom parameter");
  assertThat(param.defaultValue()).isEqualTo("Default value");
}
 
Example 2
Source File: PmdProfileExporterTest.java    From sonar-p3c-pmd with MIT License 6 votes vote down vote up
private static List<Rule> convert(List<RulesDefinition.Rule> rules) {
  List<Rule> results = Lists.newArrayListWithCapacity(rules.size());
  for (RulesDefinition.Rule rule : rules) {
    Rule newRule = Rule.create(rule.repository().key(), rule.key(), rule.name())
      .setDescription(rule.htmlDescription())
      .setRepositoryKey(rule.repository().key())
      .setConfigKey(rule.internalKey());
    if (!rule.params().isEmpty()) {
      List<RuleParam> ruleParams = Lists.newArrayList();
      for (Param param : rule.params()) {
        ruleParams.add(new RuleParam().setDefaultValue(param.defaultValue()).setKey(param.name()));
      }
      newRule.setParams(ruleParams);
    }
    results.add(newRule);
  }
  return results;
}
 
Example 3
Source File: CustomGherkinRulesDefinitionTest.java    From sonar-gherkin-plugin with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Test
public void test() {
  MyCustomGherkinRulesDefinition rulesDefinition = new MyCustomGherkinRulesDefinition();
  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("gherkin");
  assertThat(repository.rules()).hasSize(1);

  RulesDefinition.Rule customRule = repository.rule(RULE_KEY);
  assertThat(customRule).isNotNull();
  assertThat(customRule.key()).isEqualTo(RULE_KEY);
  assertThat(customRule.name()).isEqualTo(RULE_NAME);

  RulesDefinition.Param param = repository.rules().get(0).params().get(0);
  assertThat(param.key()).isEqualTo("customParam");
  assertThat(param.description()).isEqualTo("Custom parameter");
  assertThat(param.defaultValue()).isEqualTo("Default value");
}
 
Example 4
Source File: MyGherkinCustomRulesDefinitionTest.java    From sonar-gherkin-plugin with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Test
public void test() {
  MyGherkinCustomRulesDefinition rulesDefinition = new MyGherkinCustomRulesDefinition();

  RulesDefinition.Context context = new RulesDefinition.Context();
  rulesDefinition.define(context);

  RulesDefinition.Repository repository = context.repository("custom-gherkin");

  assertThat(repository.name()).isEqualTo("My Gherkin Custom Repository");
  assertThat(repository.language()).isEqualTo("gherkin");
  assertThat(repository.rules()).hasSize(2);

  RulesDefinition.Rule forbiddenKeysRule = repository.rule(ForbiddenTagCheck.class.getAnnotation(Rule.class).key());
  assertThat(forbiddenKeysRule).isNotNull();
  assertThat(forbiddenKeysRule.name()).isEqualTo(ForbiddenTagCheck.class.getAnnotation(Rule.class).name());
}
 
Example 5
Source File: CssRulesDefinitionTest.java    From sonar-css-plugin with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Test
public void test() {
  CssRulesDefinition rulesDefinition = new CssRulesDefinition();
  RulesDefinition.Context context = new RulesDefinition.Context();
  rulesDefinition.define(context);
  RulesDefinition.Repository repository = context.repository("css");

  assertThat(repository.name()).isEqualTo("SonarQube");
  assertThat(repository.language()).isEqualTo("css");
  assertThat(repository.rules()).hasSize(88);
  assertThat(CheckList.getEmbeddedCssChecks()).hasSize(repository.rules().size() - 6);


  RulesDefinition.Rule todoRule = repository.rule(TodoTagCheck.class.getAnnotation(Rule.class).key());
  assertThat(todoRule).isNotNull();
  assertThat(todoRule.name()).isEqualTo(TodoTagCheck.class.getAnnotation(Rule.class).name());
}
 
Example 6
Source File: CustomCssRulesDefinitionTest.java    From sonar-css-plugin with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Test
public void test() {
  MyCustomCssRulesDefinition rulesDefinition = new MyCustomCssRulesDefinition();
  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("css");
  assertThat(repository.rules()).hasSize(1);

  RulesDefinition.Rule customRule = repository.rule(RULE_KEY);
  assertThat(customRule).isNotNull();
  assertThat(customRule.key()).isEqualTo(RULE_KEY);
  assertThat(customRule.name()).isEqualTo(RULE_NAME);

  RulesDefinition.Param param = repository.rules().get(0).params().get(0);
  assertThat(param.key()).isEqualTo("customParam");
  assertThat(param.description()).isEqualTo("Custom parameter");
  assertThat(param.defaultValue()).isEqualTo("Default value");
}
 
Example 7
Source File: CustomLessRulesDefinitionTest.java    From sonar-css-plugin with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Test
public void test() {
  MyCustomLessRulesDefinition rulesDefinition = new MyCustomLessRulesDefinition();
  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("less");
  assertThat(repository.rules()).hasSize(1);

  RulesDefinition.Rule customRule = repository.rule(RULE_KEY);
  assertThat(customRule).isNotNull();
  assertThat(customRule.key()).isEqualTo(RULE_KEY);
  assertThat(customRule.name()).isEqualTo(RULE_NAME);

  RulesDefinition.Param param = repository.rules().get(0).params().get(0);
  assertThat(param.key()).isEqualTo("customParam");
  assertThat(param.description()).isEqualTo("Custom parameter");
  assertThat(param.defaultValue()).isEqualTo("Default value");
}
 
Example 8
Source File: PmdProfileExporterTest.java    From sonar-p3c-pmd with MIT License 6 votes vote down vote up
static RuleFinder createRuleFinder(final List<RulesDefinition.Rule> rules) {
  RuleFinder ruleFinder = mock(RuleFinder.class);
  final List<Rule> convertedRules = convert(rules);

  when(ruleFinder.find(any(RuleQuery.class))).then(new Answer<Rule>() {
    @Override
    public Rule answer(InvocationOnMock invocation) {
      RuleQuery query = (RuleQuery) invocation.getArguments()[0];
      for (Rule rule : convertedRules) {
        if (query.getConfigKey().equals(rule.getConfigKey())) {
          return rule;
        }
      }
      return null;
    }
  });
  return ruleFinder;
}
 
Example 9
Source File: LessRulesDefinitionTest.java    From sonar-css-plugin with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
public void test() {
  LessRulesDefinition rulesDefinition = new LessRulesDefinition();
  RulesDefinition.Context context = new RulesDefinition.Context();
  rulesDefinition.define(context);
  RulesDefinition.Repository repository = context.repository("less");

  assertThat(repository.name()).isEqualTo("SonarQube");
  assertThat(repository.language()).isEqualTo("less");
  assertThat(repository.rules()).hasSize(89);

  RulesDefinition.Rule rule = repository.rule(DeprecatedEscapingFunctionCheck.class.getAnnotation(Rule.class).key());
  assertThat(rule).isNotNull();
  assertThat(rule.name()).isEqualTo(DeprecatedEscapingFunctionCheck.class.getAnnotation(Rule.class).name());
}
 
Example 10
Source File: ScssRulesDefinitionTest.java    From sonar-css-plugin with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
public void test() {
  ScssRulesDefinition rulesDefinition = new ScssRulesDefinition();
  RulesDefinition.Context context = new RulesDefinition.Context();
  rulesDefinition.define(context);
  RulesDefinition.Repository repository = context.repository("scss");

  assertThat(repository.name()).isEqualTo("SonarQube");
  assertThat(repository.language()).isEqualTo("scss");
  assertThat(repository.rules()).hasSize(100);

  RulesDefinition.Rule rule = repository.rule(ScssVariableNamingConventionCheck.class.getAnnotation(Rule.class).key());
  assertThat(rule).isNotNull();
  assertThat(rule.name()).isEqualTo(ScssVariableNamingConventionCheck.class.getAnnotation(Rule.class).name());
}
 
Example 11
Source File: CheckstyleRulesDefinitionTest.java    From sonar-checkstyle with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
public void test() {
    final CheckstyleRulesDefinition definition = new CheckstyleRulesDefinition();
    final RulesDefinition.Context context = new RulesDefinition.Context();
    definition.define(context);
    final RulesDefinition.Repository repository = context
            .repository(CheckstyleConstants.REPOSITORY_KEY);

    assertThat(repository.name()).isEqualTo(CheckstyleConstants.REPOSITORY_NAME);
    assertThat(repository.language()).isEqualTo("java");

    final List<RulesDefinition.Rule> rules = repository.rules();
    assertThat(rules).hasSize(174);

    for (RulesDefinition.Rule rule : rules) {
        assertThat(rule.key()).isNotNull();
        assertThat(rule.internalKey()).isNotNull();
        assertThat(rule.name()).isNotNull();
        assertThat(rule.htmlDescription()).isNotNull();
        assertThat(rule.severity()).isNotNull();

        for (RulesDefinition.Param param : rule.params()) {
            assertThat(param.name()).isNotNull();
            assertThat(param.description()).overridingErrorMessage(
                    "Description is not set for parameter '" + param.name() + "' of rule '"
                            + rule.key()).isNotNull();
        }

        if (NO_SQALE.contains(rule.key())) {
            assertThat(rule.debtRemediationFunction()).overridingErrorMessage(
                    "Sqale remediation function is set for rule '" + rule.key()).isNull();
        }
        else {
            assertThat(rule.debtRemediationFunction()).overridingErrorMessage(
                    "Sqale remediation function is not set for rule '" + rule.key())
                    .isNotNull();
        }
    }
}
 
Example 12
Source File: GherkinRulesDefinitionTest.java    From sonar-gherkin-plugin with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
public void test() {
  GherkinRulesDefinition rulesDefinition = new GherkinRulesDefinition();
  RulesDefinition.Context context = new RulesDefinition.Context();
  rulesDefinition.define(context);
  RulesDefinition.Repository repository = context.repository("gherkin");

  assertThat(repository.name()).isEqualTo("SonarQube");
  assertThat(repository.language()).isEqualTo("gherkin");
  assertThat(repository.rules()).hasSize(46);

  RulesDefinition.Rule lineLengthRule = repository.rule(TabCharacterCheck.class.getAnnotation(Rule.class).key());
  assertThat(lineLengthRule).isNotNull();
  assertThat(lineLengthRule.name()).isEqualTo(TabCharacterCheck.class.getAnnotation(Rule.class).name());
}
 
Example 13
Source File: HtlSensorTest.java    From AEM-Rules-for-SonarQube with Apache License 2.0 5 votes vote down vote up
private CheckFactory getCheckFactory(Repository htlRepository) {
    List<NewActiveRule> ar = new ArrayList<>();
    for (RulesDefinition.Rule rule : htlRepository.rules()) {
        ar.add(new NewActiveRule.Builder().setRuleKey(RuleKey.of(HtlCheckClasses.REPOSITORY_KEY, rule.key())).build());
    }
    return new CheckFactory(new DefaultActiveRules(ar));
}
 
Example 14
Source File: RulesLoaderTest.java    From AEM-Rules-for-SonarQube with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldLoadRuleWithAllSettings() {
    givenRulesLoaded(List.of(RuleWithAllSettings.class));

    RepositoryImpl repository = (RepositoryImpl) context.repository(JavaCheckClasses.REPOSITORY_KEY);
    RulesDefinition.Rule rule = repository.rule(RULE_KEY);

    Assert.assertThat(rule.markdownDescription(), is(RULE_MARKDOWN_TEST_DESCRIPTION));
    Assert.assertThat(rule.name(), is(RULE_NAME));
    Assert.assertThat(rule.severity(), is(Priority.MINOR.toString()));
    Assert.assertThat(rule.tags().contains(Tags.AEM), is(true));
    Assert.assertThat(rule.debtRemediationFunction().baseEffort(), is(TECHNICAL_DEBT));
}
 
Example 15
Source File: RulesLoaderTest.java    From AEM-Rules-for-SonarQube with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldNotSetTechnicalDebtWhenAnnotationNotPresent() {
    givenRulesLoaded(List.of(RuleWithoutMetadataAnnotation.class));

    RepositoryImpl repository = (RepositoryImpl) context.repository(JavaCheckClasses.REPOSITORY_KEY);
    RulesDefinition.Rule rule = repository.rule(RULE_KEY);

    Assert.assertThat(rule.markdownDescription(), is(RULE_MARKDOWN_TEST_DESCRIPTION));
    Assert.assertThat(rule.name(), is(RULE_NAME));
    Assert.assertThat(rule.severity(), is(Priority.MINOR.toString()));
    Assert.assertThat(rule.tags().contains(Tags.AEM), is(true));
    Assert.assertThat(rule.debtRemediationFunction(), is(nullValue()));
}
 
Example 16
Source File: RulesLoaderTest.java    From AEM-Rules-for-SonarQube with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldNotSetTechnicalDebtWhenTechnicalDebtNotSetInMetadata() {
    givenRulesLoaded(List.of(RuleWithEmptyTechnicalDebt.class));

    RepositoryImpl repository = (RepositoryImpl) context.repository(JavaCheckClasses.REPOSITORY_KEY);
    RulesDefinition.Rule rule = repository.rule(RULE_KEY);

    Assert.assertThat(rule.markdownDescription(), is(RULE_MARKDOWN_TEST_DESCRIPTION));
    Assert.assertThat(rule.name(), is(RULE_NAME));
    Assert.assertThat(rule.severity(), is(Priority.MINOR.toString()));
    Assert.assertThat(rule.tags().contains(Tags.AEM), is(true));
    Assert.assertThat(rule.debtRemediationFunction(), is(nullValue()));
}
 
Example 17
Source File: RulesLoaderTest.java    From AEM-Rules-for-SonarQube with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldNotLoadRuleWhenRuleAnnotationIsNotPresent() {
    givenRulesLoaded(List.of(RuleWithoutRuleAnnotation.class));

    RepositoryImpl repository = (RepositoryImpl) context.repository(JavaCheckClasses.REPOSITORY_KEY);
    RulesDefinition.Rule rule = repository.rule(RULE_KEY);

    Assert.assertThat(rule, is(nullValue()));
}
 
Example 18
Source File: RulesLoaderTest.java    From AEM-Rules-for-SonarQube with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldSetDefaultValuesWhenRuleAttributeWithNameOnly() {
    givenRulesLoaded(List.of(RuleWithOnlyNameAttribute.class));

    RepositoryImpl repository = (RepositoryImpl) context.repository(JavaCheckClasses.REPOSITORY_KEY);
    RulesDefinition.Rule rule = repository.rule("com.cognifide.aemrules.extensions.RulesLoaderTest.RuleWithOnlyNameAttribute");

    Assert.assertThat(rule.markdownDescription(), is("No description yet."));
    Assert.assertThat(rule.name(), is(RULE_NAME));
    Assert.assertThat(rule.severity(), is(Priority.MAJOR.toString()));
    Assert.assertThat(rule.tags().size(), is(0));
    Assert.assertThat(rule.debtRemediationFunction().baseEffort(), is(TECHNICAL_DEBT));
}
 
Example 19
Source File: RulesLoaderTest.java    From AEM-Rules-for-SonarQube with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldLoadRuleWithProperty() {
    givenRulesLoaded(List.of(RuleWithRuleProperty.class));

    RepositoryImpl repository = (RepositoryImpl) context.repository(JavaCheckClasses.REPOSITORY_KEY);
    RulesDefinition.Rule rule = repository.rule(RULE_KEY);
    RulesDefinition.Param param = rule.param(RULE_PROPERTY_KEY);

    Assert.assertThat(param.description(), is(RULE_PROPERTY_DESCRIPTION));
    Assert.assertThat(param.defaultValue(), is(RULES_PROPERTY_DEFAULT_VALUE));
    Assert.assertThat(param.type().type(), is(RULES_PROPERTY_TYPE));
}
 
Example 20
Source File: RulesLoaderTest.java    From AEM-Rules-for-SonarQube with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldLoadRuleWithPropertyWithoutAttributes() {
    givenRulesLoaded(List.of(RuleWithRulePropertyWithoutAttributes.class));

    RepositoryImpl repository = (RepositoryImpl) context.repository(JavaCheckClasses.REPOSITORY_KEY);
    RulesDefinition.Rule rule = repository.rule(RULE_KEY);
    RulesDefinition.Param param = rule.param("testProperty");

    Assert.assertThat(param.description(), is(nullValue()));
    Assert.assertThat(param.defaultValue(), is(nullValue()));
    Assert.assertThat(param.type().type(), is("STRING"));
}