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

The following examples show how to use org.sonar.api.server.rule.RulesDefinition#Repository . 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: PmdRulesDefinitionTest.java    From sonar-p3c-pmd with MIT License 6 votes vote down vote up
@Test
public void should_use_text_parameter_for_xpath_rule() {
  PmdRulesDefinition definition = new PmdRulesDefinition();
  RulesDefinition.Context context = new RulesDefinition.Context();
  definition.define(context);
  RulesDefinition.Repository repository = context.repository(PmdConstants.REPOSITORY_KEY);

  Rule xpathRule = Iterables.find(repository.rules(), new Predicate<Rule>() {
    @Override
    public boolean apply(Rule rule) {
      return rule.key().equals("XPathRule");
    }
  });

  assertThat(xpathRule.param("xpath").type().type()).isEqualTo(PropertyType.TEXT.name());
}
 
Example 3
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 4
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 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: 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 7
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 8
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 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: 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: EsqlRulesDefinitionTest.java    From sonar-esql-plugin with Apache License 2.0 5 votes vote down vote up
@Test
public void test() {
  RulesDefinition.Repository repository = buildRepository();

  assertThat(repository.name()).isEqualTo("SonarAnalyzer");
  assertThat(repository.language()).isEqualTo("esql");
  assertThat(repository.rules()).hasSize(CheckList.getChecks().size());

  assertRuleProperties(repository);
  assertParameterProperties(repository);
  assertAllRuleParametersHaveDescription(repository);
}
 
Example 12
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 13
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 14
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 15
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 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: 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 18
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 19
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();
    }
  }
}
 
Example 20
Source File: PmdUnitTestsRulesDefinitionTest.java    From sonar-p3c-pmd with MIT License 4 votes vote down vote up
@Test
public void test() {

  PmdUnitTestsRulesDefinition definition = new PmdUnitTestsRulesDefinition();
  RulesDefinition.Context context = new RulesDefinition.Context();
  definition.define(context);
  RulesDefinition.Repository repository = context.repository(PmdConstants.TEST_REPOSITORY_KEY);

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

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

  for (Rule rule : rules) {
    assertThat(rule.key()).isNotNull();
    assertThat(rule.key()).isIn(
      "JUnitStaticSuite",
      "JUnitSpelling",
      "JUnitAssertionsShouldIncludeMessage",
      "JUnitTestsShouldIncludeAssert",
      "TestClassWithoutTestCases",
      "UnnecessaryBooleanAssertion",
      "UseAssertEqualsInsteadOfAssertTrue",
      "UseAssertSameInsteadOfAssertTrue",
      "UseAssertNullInsteadOfAssertTrue",
      "SimplifyBooleanAssertion",
      "UseAssertTrueInsteadOfAssertEquals",
      "JUnitTestContainsTooManyAsserts",
      "JUnit4SuitesShouldUseSuiteAnnotation",
      "JUnit4TestShouldUseAfterAnnotation",
      "JUnit4TestShouldUseBeforeAnnotation",
      "JUnit4TestShouldUseTestAnnotation",
      "JUnitUseExpected");
    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();
    }
  }
}