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

The following examples show how to use org.sonar.api.server.rule.RulesDefinition#Context . 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: LuaRulesDefinitionTest.java    From sonar-lua with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Test
public void test() {
  LuaRulesDefinition rulesDefinition = new LuaRulesDefinition();
  RulesDefinition.Context context = new RulesDefinition.Context();
  rulesDefinition.define(context);
  RulesDefinition.Repository repository = context.repository("lua");

  assertThat(repository.name()).isEqualTo("SonarQube");
  assertThat(repository.language()).isEqualTo("lua");
  assertThat(repository.rules()).hasSize(CheckList.getChecks().size());

  for (RulesDefinition.Rule rule : repository.rules()) {
      for (RulesDefinition.Param param : rule.params()) {
        assertThat(param.description()).as("description for " + param.key() + " of " + rule.key()).isNotEmpty();
      }
    }
  
}
 
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: 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 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: 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 8
Source File: HtlSensorTest.java    From AEM-Rules-for-SonarQube with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() {
    RulesDefinition rulesDefinition = new AemRulesRulesDefinition();
    RulesDefinition.Context context = new RulesDefinition.Context();
    rulesDefinition.define(context);
    RulesDefinition.Repository htlRepository = context.repository(HtlCheckClasses.REPOSITORY_KEY);

    FileLinesContextFactory fileLinesContextFactory = getMockedFileLinesContextFactory();
    Configuration configuration = getMockedConfiguration();
    CheckFactory checkFactory = getCheckFactory(htlRepository);

    sensor = new HtlSensor(fileLinesContextFactory, configuration, checkFactory);
    tester = SensorContextTester.create(TEST_DIR);
}
 
Example 9
Source File: CustomEsqlRulesDefinition.java    From sonar-esql-plugin with Apache License 2.0 5 votes vote down vote up
/**
 * Defines rule repository with check metadata from check classes' annotations.
 * This method should be overridden if check metadata are provided via another format,
 * e.g: XMl, JSON.
 */
@Override
public void define(RulesDefinition.Context context) {
  RulesDefinition.NewRepository repo = context.createRepository(repositoryKey(), "esql").setName(repositoryName());

  // Load metadata from check classes' annotations
  new AnnotationBasedRulesDefinition(repo, "esql").addRuleClasses(false, ImmutableList.copyOf(checkClasses()));

  repo.done();
}
 
Example 10
Source File: EsqlRulesDefinitionTest.java    From sonar-esql-plugin with Apache License 2.0 5 votes vote down vote up
private RulesDefinition.Repository buildRepository() {
  EsqlRulesDefinition rulesDefinition = new EsqlRulesDefinition();
  RulesDefinition.Context context = new RulesDefinition.Context();
  rulesDefinition.define(context);
  RulesDefinition.Repository repository = context.repository("esql");
  return repository;
}
 
Example 11
Source File: ApexRulesDefinitionTest.java    From enforce-sonarqube-plugin with MIT License 5 votes vote down vote up
@Test
public void test() {
    rulesDefinition = new ApexRulesDefinition();
    RulesDefinition.Context context = new RulesDefinition.Context();
    rulesDefinition.define(context);
    RulesDefinition.Repository repository = context.repository("apex");

    assertThat(repository.name(), equalTo("SonarQube"));
    assertThat(repository.language(), equalTo("apex"));
    assertThat(repository.rules().size(), is(CheckList.getChecks().size()));
}
 
Example 12
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 13
Source File: RubyRulesDefinitionTest.java    From sonar-ruby-plugin with MIT License 5 votes vote down vote up
@Test
public void testDefine() {
    RulesDefinition.Context context = new RulesDefinition.Context();

    rules.define(context);

    assertThat(context.repositories().size()).isEqualTo(1);
    assertThat(context.repository("rubocop")).isNotNull();
    assertThat(context.repository("rubocop").language()).isEqualTo("rb");
    assertThat(context.repository("rubocop").rules().size()).isEqualTo(341);
}
 
Example 14
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 15
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 16
Source File: AbstractAnsibleRulesDefinitionTest.java    From sonar-ansible with Apache License 2.0 5 votes vote down vote up
@Test
public void testDefineWithNonExistingRules() {
    // Existing rules
    MyRulesDefinition rulesDefinition = new MyRulesDefinition("no-rules");
    RulesDefinition.Context context = new RulesDefinition.Context();
    rulesDefinition.define(context);
    RulesDefinition.Repository repository = context.repository(AnsibleCheckRepository.REPOSITORY_KEY);

    assertTrue(repository.rules().isEmpty());
    // Expected an info message for rules not found
    assertTrue(logTester.logs(LoggerLevel.INFO).contains("No Ansible Lint rules found"));
}
 
Example 17
Source File: AbstractAnsibleRulesDefinition.java    From sonar-ansible with Apache License 2.0 5 votes vote down vote up
@Override
public void define(RulesDefinition.Context context) {
    RulesDefinition.NewRepository repository = context.createRepository(AnsibleCheckRepository.REPOSITORY_KEY, YamlLanguage.KEY).setName(AnsibleCheckRepository.REPOSITORY_NAME);

    RuleMetadataLoader metadataLoader = new RuleMetadataLoader(getRuleDefinitionPath());

    List<String> keys = new ArrayList<>();
    getRuleKeys().forEach(keys::add);
    metadataLoader.addRulesByRuleKey(repository, keys);

    repository.done();
}
 
Example 18
Source File: PmdProfileExporterTest.java    From sonar-p3c-pmd with MIT License 5 votes vote down vote up
static RulesProfile importProfile(String configuration) {
  PmdRulesDefinition definition = new PmdRulesDefinition();
  RulesDefinition.Context context = new RulesDefinition.Context();
  definition.define(context);
  RulesDefinition.Repository repository = context.repository(PmdConstants.REPOSITORY_KEY);
  RuleFinder ruleFinder = createRuleFinder(repository.rules());
  PmdProfileImporter importer = new PmdProfileImporter(ruleFinder);

  return importer.importProfile(new StringReader(configuration), ValidationMessages.create());
}
 
Example 19
Source File: PmdRulesDefinitionTest.java    From sonar-p3c-pmd with MIT License 5 votes vote down vote up
@Test
public void should_exclude_junit_rules() {
  PmdRulesDefinition definition = new PmdRulesDefinition();
  RulesDefinition.Context context = new RulesDefinition.Context();
  definition.define(context);
  RulesDefinition.Repository repository = context.repository(PmdConstants.REPOSITORY_KEY);

  for (Rule rule : repository.rules()) {
    assertThat(rule.key()).excludes("JUnitStaticSuite");
  }
}
 
Example 20
Source File: PmdRulesDefinitionTest.java    From sonar-p3c-pmd with MIT License 5 votes vote down vote up
@Test
public void test() {
  PmdRulesDefinition definition = new PmdRulesDefinition();
  RulesDefinition.Context context = new RulesDefinition.Context();
  definition.define(context);
  RulesDefinition.Repository repository = context.repository(PmdConstants.REPOSITORY_KEY);

  assertThat(repository.name()).isEqualTo(PmdConstants.REPOSITORY_NAME);
  assertThat(repository.language()).isEqualTo(Java.KEY);

  List<Rule> rules = repository.rules();
  //assertThat(rules).hasSize(268);

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

    for (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 (!"XPathRule".equals(rule.key())) {
      assertThat(rule.debtRemediationFunction())
        .overridingErrorMessage("Sqale remediation function is not set for rule '" + rule.key())
        .isNotNull();
      assertThat(rule.debtSubCharacteristic())
        .overridingErrorMessage("Sqale characteristic is not set for rule '" + rule.key())
        .isNotNull();
    }
  }
}