Search in sources :

Example 21 with NewActiveRule

use of org.sonar.api.batch.rule.internal.NewActiveRule in project sonarqube by SonarSource.

the class CheckFactoryTest method param_as_inherited_field.

/**
 * SONAR-3164
 */
@Test
public void param_as_inherited_field() {
    RuleKey ruleKey = RuleKey.of("squid", "org.sonar.api.batch.rule.CheckWithPrimitiveProperties");
    NewActiveRule rule = new NewActiveRule.Builder().setRuleKey(ruleKey).setParam("max", "300").build();
    builder.addRule(rule);
    CheckFactory checkFactory = new CheckFactory(builder.build());
    Checks checks = checkFactory.create("squid").addAnnotatedChecks(CheckWithPrimitiveProperties.class);
    Object check = checks.of(ruleKey);
    assertThat(check).isInstanceOf(CheckWithPrimitiveProperties.class);
    assertThat(((CheckWithPrimitiveProperties) check).getMax()).isEqualTo(300);
}
Also used : NewActiveRule(org.sonar.api.batch.rule.internal.NewActiveRule) RuleKey(org.sonar.api.rule.RuleKey) Test(org.junit.Test)

Example 22 with NewActiveRule

use of org.sonar.api.batch.rule.internal.NewActiveRule in project sonarqube by SonarSource.

the class CheckFactoryTest method param_as_primitive_fields.

@Test
public void param_as_primitive_fields() {
    RuleKey ruleKey = RuleKey.of("squid", "org.sonar.api.batch.rule.CheckWithPrimitiveProperties");
    NewActiveRule rule = new NewActiveRule.Builder().setRuleKey(ruleKey).setParam("max", "300").setParam("ignore", "true").build();
    builder.addRule(rule);
    CheckFactory checkFactory = new CheckFactory(builder.build());
    Checks checks = checkFactory.create("squid").addAnnotatedChecks(CheckWithPrimitiveProperties.class);
    Object check = checks.of(ruleKey);
    assertThat(check).isInstanceOf(CheckWithPrimitiveProperties.class);
    assertThat(((CheckWithPrimitiveProperties) check).getMax()).isEqualTo(300);
    assertThat(((CheckWithPrimitiveProperties) check).isIgnore()).isTrue();
}
Also used : NewActiveRule(org.sonar.api.batch.rule.internal.NewActiveRule) RuleKey(org.sonar.api.rule.RuleKey) ActiveRulesBuilder(org.sonar.api.batch.rule.internal.ActiveRulesBuilder) Test(org.junit.Test)

Example 23 with NewActiveRule

use of org.sonar.api.batch.rule.internal.NewActiveRule in project sonarqube by SonarSource.

the class CheckFactoryTest method use_template_rule_key.

@Test
public void use_template_rule_key() {
    RuleKey ruleKey = RuleKey.of("squid", "S0001_123");
    NewActiveRule rule = new NewActiveRule.Builder().setRuleKey(ruleKey).setTemplateRuleKey("S0001").build();
    builder.addRule(rule);
    CheckFactory checkFactory = new CheckFactory(builder.build());
    Checks checks = checkFactory.create("squid").addAnnotatedChecks(CheckWithKey.class);
    Object check = checks.of(ruleKey);
    assertThat(check).isInstanceOf(CheckWithKey.class);
    assertThat(checks.of(ruleKey)).isSameAs(check);
    assertThat(checks.ruleKey(check)).isEqualTo(ruleKey);
    assertThat(checks.all()).containsOnly(check);
}
Also used : NewActiveRule(org.sonar.api.batch.rule.internal.NewActiveRule) RuleKey(org.sonar.api.rule.RuleKey) Test(org.junit.Test)

Example 24 with NewActiveRule

use of org.sonar.api.batch.rule.internal.NewActiveRule in project sonar-go by SonarSource.

the class GoSensorTest method getSensor.

private GoSensor getSensor(String... activeRuleArray) {
    Set<String> activeRuleSet = new HashSet<>(Arrays.asList(activeRuleArray));
    List<Class> ruleClasses = GoChecks.getChecks();
    List<String> allKeys = ruleClasses.stream().map(ruleClass -> ((org.sonar.check.Rule) ruleClass.getAnnotations()[0]).key()).collect(Collectors.toList());
    ActiveRulesBuilder rulesBuilder = new ActiveRulesBuilder();
    allKeys.forEach(key -> {
        NewActiveRule newActiveRule = rulesBuilder.create(RuleKey.of(GoRulesDefinition.REPOSITORY_KEY, key));
        if (activeRuleSet.contains(key)) {
            newActiveRule.activate();
        }
    });
    ActiveRules activeRules = rulesBuilder.build();
    CheckFactory checkFactory = new CheckFactory(activeRules);
    Checks<Check> checks = checkFactory.create(GoRulesDefinition.REPOSITORY_KEY);
    checks.addAnnotatedChecks((Iterable) ruleClasses);
    return new GoSensor(checkFactory, fileLinesContextFactory);
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) InputFile(org.sonar.api.batch.fs.InputFile) Arrays(java.util.Arrays) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) DefaultSensorDescriptor(org.sonar.api.batch.sensor.internal.DefaultSensorDescriptor) HashMap(java.util.HashMap) ActiveRules(org.sonar.api.batch.rule.ActiveRules) Mockito.spy(org.mockito.Mockito.spy) HashSet(java.util.HashSet) FileLinesContext(org.sonar.api.measures.FileLinesContext) FileLinesContextFactory(org.sonar.api.measures.FileLinesContextFactory) CheckFactory(org.sonar.api.batch.rule.CheckFactory) SensorContextTester(org.sonar.api.batch.sensor.internal.SensorContextTester) RegisterExtension(org.junit.jupiter.api.extension.RegisterExtension) Map(java.util.Map) Checks(org.sonar.api.batch.rule.Checks) Path(java.nio.file.Path) Nullable(javax.annotation.Nullable) Check(org.sonar.commonruleengine.checks.Check) Awaitility.await(org.awaitility.Awaitility.await) Files(java.nio.file.Files) NewActiveRule(org.sonar.api.batch.rule.internal.NewActiveRule) ActiveRulesBuilder(org.sonar.api.batch.rule.internal.ActiveRulesBuilder) Set(java.util.Set) IOException(java.io.IOException) Mockito.when(org.mockito.Mockito.when) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) CoreMetrics(org.sonar.api.measures.CoreMetrics) TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) TypeOfText(org.sonar.api.batch.sensor.highlighting.TypeOfText) Test(org.junit.jupiter.api.Test) List(java.util.List) RuleKey(org.sonar.api.rule.RuleKey) Mockito.any(org.mockito.Mockito.any) LoggerLevel(org.sonar.api.utils.log.LoggerLevel) Mockito.mock(org.mockito.Mockito.mock) ActiveRulesBuilder(org.sonar.api.batch.rule.internal.ActiveRulesBuilder) Check(org.sonar.commonruleengine.checks.Check) NewActiveRule(org.sonar.api.batch.rule.internal.NewActiveRule) ActiveRules(org.sonar.api.batch.rule.ActiveRules) CheckFactory(org.sonar.api.batch.rule.CheckFactory) NewActiveRule(org.sonar.api.batch.rule.internal.NewActiveRule) HashSet(java.util.HashSet)

Aggregations

NewActiveRule (org.sonar.api.batch.rule.internal.NewActiveRule)24 Test (org.junit.Test)17 RuleKey (org.sonar.api.rule.RuleKey)11 ActiveRulesBuilder (org.sonar.api.batch.rule.internal.ActiveRulesBuilder)9 ActiveRules (org.sonar.api.batch.rule.ActiveRules)7 Map (java.util.Map)3 InputFile (org.sonar.api.batch.fs.InputFile)3 CheckFactory (org.sonar.api.batch.rule.CheckFactory)3 DefaultActiveRules (org.sonar.api.batch.rule.internal.DefaultActiveRules)3 FileLinesContext (org.sonar.api.measures.FileLinesContext)3 FileLinesContextFactory (org.sonar.api.measures.FileLinesContextFactory)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Before (org.junit.Before)2 DefaultInputFile (org.sonar.api.batch.fs.internal.DefaultInputFile)2 TestInputFileBuilder (org.sonar.api.batch.fs.internal.TestInputFileBuilder)2 NoSonarFilter (org.sonar.api.issue.NoSonarFilter)2 RulesProfile (org.sonar.api.profiles.RulesProfile)2 RulesDefinition (org.sonar.api.server.rule.RulesDefinition)2 File (java.io.File)1