Search in sources :

Example 16 with ActiveRulesBuilder

use of org.sonar.api.batch.rule.internal.ActiveRulesBuilder 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)

Example 17 with ActiveRulesBuilder

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

the class ActiveRulesBuilderTest method fail_to_add_twice_the_same_rule.

@Test
public void fail_to_add_twice_the_same_rule() {
    ActiveRulesBuilder builder = new ActiveRulesBuilder();
    NewActiveRule rule = new NewActiveRule.Builder().setRuleKey(RuleKey.of("squid", "S0001")).build();
    builder.addRule(rule);
    assertThatThrownBy(() -> builder.addRule(rule)).isInstanceOf(IllegalStateException.class).hasMessage("Rule 'squid:S0001' is already activated");
}
Also used : ActiveRulesBuilder(org.sonar.api.batch.rule.internal.ActiveRulesBuilder) NewActiveRule(org.sonar.api.batch.rule.internal.NewActiveRule) ActiveRulesBuilder(org.sonar.api.batch.rule.internal.ActiveRulesBuilder) Test(org.junit.Test)

Example 18 with ActiveRulesBuilder

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

the class ActiveRulesBuilderTest method build_rules.

@Test
public void build_rules() {
    NewActiveRule activeRule = new NewActiveRule.Builder().setRuleKey(RuleKey.of("squid", "S0001")).setName("My Rule").setSeverity(Severity.CRITICAL).setInternalKey("__S0001__").setParam("min", "20").build();
    ActiveRules activeRules = new ActiveRulesBuilder().addRule(activeRule).addRule(new NewActiveRule.Builder().setRuleKey(RuleKey.of("squid", "S0002")).build()).addRule(new NewActiveRule.Builder().setRuleKey(RuleKey.of("findbugs", "NPE")).setInternalKey(null).setSeverity(null).setParam("foo", null).build()).build();
    assertThat(activeRules.findAll()).hasSize(3);
    assertThat(activeRules.findByRepository("squid")).hasSize(2);
    assertThat(activeRules.findByRepository("findbugs")).hasSize(1);
    assertThat(activeRules.findByInternalKey("squid", "__S0001__")).isNotNull();
    assertThat(activeRules.findByRepository("unknown")).isEmpty();
    ActiveRule squid1 = activeRules.find(RuleKey.of("squid", "S0001"));
    assertThat(squid1.ruleKey().repository()).isEqualTo("squid");
    assertThat(squid1.ruleKey().rule()).isEqualTo("S0001");
    assertThat(squid1.severity()).isEqualTo(Severity.CRITICAL);
    assertThat(squid1.internalKey()).isEqualTo("__S0001__");
    assertThat(squid1.params()).hasSize(1);
    assertThat(squid1.param("min")).isEqualTo("20");
    ActiveRule squid2 = activeRules.find(RuleKey.of("squid", "S0002"));
    assertThat(squid2.ruleKey().repository()).isEqualTo("squid");
    assertThat(squid2.ruleKey().rule()).isEqualTo("S0002");
    assertThat(squid2.severity()).isEqualTo(Severity.defaultSeverity());
    assertThat(squid2.params()).isEmpty();
    ActiveRule findbugsRule = activeRules.find(RuleKey.of("findbugs", "NPE"));
    assertThat(findbugsRule.severity()).isEqualTo(Severity.defaultSeverity());
    assertThat(findbugsRule.internalKey()).isNull();
    assertThat(findbugsRule.params()).isEmpty();
}
Also used : ActiveRulesBuilder(org.sonar.api.batch.rule.internal.ActiveRulesBuilder) NewActiveRule(org.sonar.api.batch.rule.internal.NewActiveRule) ActiveRulesBuilder(org.sonar.api.batch.rule.internal.ActiveRulesBuilder) ActiveRule(org.sonar.api.batch.rule.ActiveRule) NewActiveRule(org.sonar.api.batch.rule.internal.NewActiveRule) ActiveRules(org.sonar.api.batch.rule.ActiveRules) Test(org.junit.Test)

Example 19 with ActiveRulesBuilder

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

the class ActiveRulesBuilderTest method no_rules.

@Test
public void no_rules() {
    ActiveRulesBuilder builder = new ActiveRulesBuilder();
    ActiveRules rules = builder.build();
    assertThat(rules.findAll()).isEmpty();
}
Also used : ActiveRulesBuilder(org.sonar.api.batch.rule.internal.ActiveRulesBuilder) ActiveRules(org.sonar.api.batch.rule.ActiveRules) Test(org.junit.Test)

Example 20 with ActiveRulesBuilder

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

the class ModuleSensorOptimizerTest method prepare.

@Before
public void prepare() throws Exception {
    fs = new DefaultFileSystem(temp.newFolder().toPath());
    settings = new MapSettings();
    optimizer = new ModuleSensorOptimizer(fs, new ActiveRulesBuilder().build(), settings.asConfig());
}
Also used : ActiveRulesBuilder(org.sonar.api.batch.rule.internal.ActiveRulesBuilder) MapSettings(org.sonar.api.config.internal.MapSettings) DefaultFileSystem(org.sonar.api.batch.fs.internal.DefaultFileSystem) Before(org.junit.Before)

Aggregations

ActiveRulesBuilder (org.sonar.api.batch.rule.internal.ActiveRulesBuilder)22 Test (org.junit.Test)11 ActiveRules (org.sonar.api.batch.rule.ActiveRules)9 NewActiveRule (org.sonar.api.batch.rule.internal.NewActiveRule)9 Before (org.junit.Before)6 DefaultFileSystem (org.sonar.api.batch.fs.internal.DefaultFileSystem)5 DefaultSensorDescriptor (org.sonar.api.batch.sensor.internal.DefaultSensorDescriptor)4 RulesProfile (org.sonar.api.profiles.RulesProfile)4 Map (java.util.Map)3 InputFile (org.sonar.api.batch.fs.InputFile)3 TestInputFileBuilder (org.sonar.api.batch.fs.internal.TestInputFileBuilder)3 MapSettings (org.sonar.api.config.internal.MapSettings)3 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 DefaultInputFile (org.sonar.api.batch.fs.internal.DefaultInputFile)2 MetricFinder (org.sonar.api.batch.measure.MetricFinder)2 CheckFactory (org.sonar.api.batch.rule.CheckFactory)2 SensorContextTester (org.sonar.api.batch.sensor.internal.SensorContextTester)2 SensorStorage (org.sonar.api.batch.sensor.internal.SensorStorage)2 File (java.io.File)1