Search in sources :

Example 1 with ActiveRules

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

the class ActiveRulesBuilderTest method build_rules.

@Test
public void build_rules() {
    ActiveRules activeRules = new ActiveRulesBuilder().create(RuleKey.of("squid", "S0001")).setName("My Rule").setSeverity(Severity.CRITICAL).setInternalKey("__S0001__").setParam("min", "20").activate().create(RuleKey.of("squid", "S0002")).activate().create(RuleKey.of("findbugs", "NPE")).setInternalKey(null).setSeverity(null).setParam("foo", null).activate().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 : ActiveRule(org.sonar.api.batch.rule.ActiveRule) ActiveRules(org.sonar.api.batch.rule.ActiveRules) Test(org.junit.Test)

Example 2 with ActiveRules

use of org.sonar.api.batch.rule.ActiveRules 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 : ActiveRules(org.sonar.api.batch.rule.ActiveRules) Test(org.junit.Test)

Example 3 with ActiveRules

use of org.sonar.api.batch.rule.ActiveRules in project sonar-web by SonarSource.

the class WebSensorTest method setUp.

@Before
public void setUp() throws Exception {
    WebRulesDefinition rulesDefinition = new WebRulesDefinition();
    RulesDefinition.Context context = new RulesDefinition.Context();
    rulesDefinition.define(context);
    RulesDefinition.Repository repository = context.repository(WebRulesDefinition.REPOSITORY_KEY);
    List<NewActiveRule> ar = new ArrayList<>();
    for (RulesDefinition.Rule rule : repository.rules()) {
        ar.add(new ActiveRulesBuilder().create(RuleKey.of(WebRulesDefinition.REPOSITORY_KEY, rule.key())));
    }
    ActiveRules activeRules = new DefaultActiveRules(ar);
    CheckFactory checkFactory = new CheckFactory(activeRules);
    FileLinesContextFactory fileLinesContextFactory = mock(FileLinesContextFactory.class);
    when(fileLinesContextFactory.createFor(Mockito.any(InputFile.class))).thenReturn(mock(FileLinesContext.class));
    sensor = new WebSensor(new NoSonarFilter(), fileLinesContextFactory, checkFactory);
    tester = SensorContextTester.create(TEST_DIR);
}
Also used : FileLinesContext(org.sonar.api.measures.FileLinesContext) ActiveRulesBuilder(org.sonar.api.batch.rule.internal.ActiveRulesBuilder) DefaultActiveRules(org.sonar.api.batch.rule.internal.DefaultActiveRules) NoSonarFilter(org.sonar.api.issue.NoSonarFilter) FileLinesContextFactory(org.sonar.api.measures.FileLinesContextFactory) ArrayList(java.util.ArrayList) InputFile(org.sonar.api.batch.fs.InputFile) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) RulesDefinition(org.sonar.api.server.rule.RulesDefinition) WebRulesDefinition(org.sonar.plugins.web.rules.WebRulesDefinition) NewActiveRule(org.sonar.api.batch.rule.internal.NewActiveRule) DefaultActiveRules(org.sonar.api.batch.rule.internal.DefaultActiveRules) ActiveRules(org.sonar.api.batch.rule.ActiveRules) CheckFactory(org.sonar.api.batch.rule.CheckFactory) WebRulesDefinition(org.sonar.plugins.web.rules.WebRulesDefinition) FileLinesContext(org.sonar.api.measures.FileLinesContext) Before(org.junit.Before)

Example 4 with ActiveRules

use of org.sonar.api.batch.rule.ActiveRules in project sonar-java by SonarSource.

the class SonarComponentsTest method ucfg_activation_should_rely_on_active_rules.

@Test
public void ucfg_activation_should_rely_on_active_rules() {
    File file = new File("src/test/files/ParseError.java");
    SensorContextTester sensorContext = SensorContextTester.create(file.getParentFile().getAbsoluteFile());
    SonarComponents sonarComponents = new SonarComponents(null, null, null, null, null);
    sonarComponents.setRuleRepositoryKey("squid");
    sonarComponents.setSensorContext(sensorContext);
    // no security rules available
    JavaRules.ruleKeys = new HashSet<>();
    assertThat(sonarComponents.shouldGenerateUCFG()).isFalse();
    ActiveRules activeRules = new ActiveRulesBuilder().create(RuleKey.of("squid", "S3649")).activate().build();
    // one security rule available
    JavaRules.ruleKeys = new HashSet<>(Arrays.asList("S3649"));
    sensorContext.setActiveRules(activeRules);
    assertThat(sonarComponents.shouldGenerateUCFG()).isTrue();
}
Also used : ActiveRulesBuilder(org.sonar.api.batch.rule.internal.ActiveRulesBuilder) SensorContextTester(org.sonar.api.batch.sensor.internal.SensorContextTester) ActiveRules(org.sonar.api.batch.rule.ActiveRules) InputFile(org.sonar.api.batch.fs.InputFile) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) File(java.io.File) Test(org.junit.Test)

Example 5 with ActiveRules

use of org.sonar.api.batch.rule.ActiveRules in project sonar-web by SonarSource.

the class HtmlSensorTest method setUp.

@Before
public void setUp() {
    HtmlRulesDefinition rulesDefinition = new HtmlRulesDefinition();
    RulesDefinition.Context context = new RulesDefinition.Context();
    rulesDefinition.define(context);
    RulesDefinition.Repository repository = context.repository(HtmlRulesDefinition.REPOSITORY_KEY);
    List<NewActiveRule> ar = new ArrayList<>();
    for (RulesDefinition.Rule rule : repository.rules()) {
        ar.add(new NewActiveRule.Builder().setRuleKey(RuleKey.of(HtmlRulesDefinition.REPOSITORY_KEY, rule.key())).build());
    }
    ActiveRules activeRules = new DefaultActiveRules(ar);
    CheckFactory checkFactory = new CheckFactory(activeRules);
    FileLinesContextFactory fileLinesContextFactory = mock(FileLinesContextFactory.class);
    when(fileLinesContextFactory.createFor(Mockito.any(InputFile.class))).thenReturn(mock(FileLinesContext.class));
    final SonarRuntime sonarRuntime = SonarRuntimeImpl.forSonarQube(Version.create(8, 9), SonarQubeSide.SCANNER, SonarEdition.COMMUNITY);
    sensor = new HtmlSensor(sonarRuntime, new NoSonarFilter(), fileLinesContextFactory, checkFactory);
    tester = SensorContextTester.create(TEST_DIR).setRuntime(sonarRuntime);
}
Also used : FileLinesContext(org.sonar.api.measures.FileLinesContext) DefaultActiveRules(org.sonar.api.batch.rule.internal.DefaultActiveRules) HtmlRulesDefinition(org.sonar.plugins.html.rules.HtmlRulesDefinition) NoSonarFilter(org.sonar.api.issue.NoSonarFilter) FileLinesContextFactory(org.sonar.api.measures.FileLinesContextFactory) ArrayList(java.util.ArrayList) InputFile(org.sonar.api.batch.fs.InputFile) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) RulesDefinition(org.sonar.api.server.rule.RulesDefinition) HtmlRulesDefinition(org.sonar.plugins.html.rules.HtmlRulesDefinition) NewActiveRule(org.sonar.api.batch.rule.internal.NewActiveRule) DefaultActiveRules(org.sonar.api.batch.rule.internal.DefaultActiveRules) ActiveRules(org.sonar.api.batch.rule.ActiveRules) CheckFactory(org.sonar.api.batch.rule.CheckFactory) SonarRuntime(org.sonar.api.SonarRuntime) FileLinesContext(org.sonar.api.measures.FileLinesContext) Before(org.junit.Before)

Aggregations

ActiveRules (org.sonar.api.batch.rule.ActiveRules)14 Test (org.junit.Test)11 ActiveRulesBuilder (org.sonar.api.batch.rule.internal.ActiveRulesBuilder)9 NewActiveRule (org.sonar.api.batch.rule.internal.NewActiveRule)7 InputFile (org.sonar.api.batch.fs.InputFile)4 DefaultActiveRules (org.sonar.api.batch.rule.internal.DefaultActiveRules)4 DefaultSensorDescriptor (org.sonar.api.batch.sensor.internal.DefaultSensorDescriptor)4 DefaultInputFile (org.sonar.api.batch.fs.internal.DefaultInputFile)3 TestInputFileBuilder (org.sonar.api.batch.fs.internal.TestInputFileBuilder)3 CheckFactory (org.sonar.api.batch.rule.CheckFactory)3 FileLinesContext (org.sonar.api.measures.FileLinesContext)3 FileLinesContextFactory (org.sonar.api.measures.FileLinesContextFactory)3 File (java.io.File)2 ArrayList (java.util.ArrayList)2 Before (org.junit.Before)2 ActiveRule (org.sonar.api.batch.rule.ActiveRule)2 SensorContextTester (org.sonar.api.batch.sensor.internal.SensorContextTester)2 NoSonarFilter (org.sonar.api.issue.NoSonarFilter)2 RulesDefinition (org.sonar.api.server.rule.RulesDefinition)2 IOException (java.io.IOException)1