Search in sources :

Example 6 with DefaultActiveRules

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

the class ActiveRulesProviderTest method testCombinationOfRules.

@Test
public void testCombinationOfRules() {
    LoadedActiveRule r1 = mockRule("rule1");
    LoadedActiveRule r2 = mockRule("rule2");
    LoadedActiveRule r3 = mockRule("rule3");
    List<LoadedActiveRule> qp1Rules = ImmutableList.of(r1, r2);
    List<LoadedActiveRule> qp2Rules = ImmutableList.of(r2, r3);
    List<LoadedActiveRule> qp3Rules = ImmutableList.of(r1, r3);
    when(loader.load("qp1")).thenReturn(qp1Rules);
    when(loader.load("qp2")).thenReturn(qp2Rules);
    when(loader.load("qp3")).thenReturn(qp3Rules);
    QualityProfiles profiles = mockProfiles("qp1", "qp2", "qp3");
    DefaultActiveRules activeRules = provider.provide(loader, profiles);
    assertThat(activeRules.findAll()).hasSize(3);
    assertThat(activeRules.findAll()).extracting("ruleKey").containsOnly(RuleKey.of("rule1", "rule1"), RuleKey.of("rule2", "rule2"), RuleKey.of("rule3", "rule3"));
    verify(loader).load("qp1");
    verify(loader).load("qp2");
    verify(loader).load("qp3");
    assertThat(activeRules.getDeprecatedRuleKeys(RuleKey.of("rule1", "rule1"))).containsOnly("rule1old:rule1old");
    verifyNoMoreInteractions(loader);
}
Also used : DefaultActiveRules(org.sonar.api.batch.rule.internal.DefaultActiveRules) LoadedActiveRule(org.sonar.api.batch.rule.LoadedActiveRule) Test(org.junit.Test)

Example 7 with DefaultActiveRules

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

the class EnforceIssuesFilterTest method shouldAcceptIssueIfFullyMatched.

@Test
public void shouldAcceptIssueIfFullyMatched() {
    DefaultActiveRules activeRules = new DefaultActiveRules(ImmutableSet.of());
    String path = "org/sonar/api/Issue.java";
    RuleKey ruleKey = RuleKey.of("repo", "rule");
    when(issue.ruleKey()).thenReturn(ruleKey);
    IssuePattern matching = new IssuePattern(path, ruleKey.toString());
    when(exclusionPatternInitializer.getMulticriteriaPatterns()).thenReturn(ImmutableList.of(matching));
    when(issue.getComponent()).thenReturn(createComponentWithPath(path));
    ignoreFilter = new EnforceIssuesFilter(exclusionPatternInitializer, analysisWarnings, activeRules);
    assertThat(ignoreFilter.accept(issue, chain)).isTrue();
    verifyNoInteractions(chain);
}
Also used : DefaultActiveRules(org.sonar.api.batch.rule.internal.DefaultActiveRules) RuleKey(org.sonar.api.rule.RuleKey) IssuePattern(org.sonar.scanner.issue.ignore.pattern.IssuePattern) Test(org.junit.Test)

Example 8 with DefaultActiveRules

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

the class ActiveRulesPublisherTest method write.

@Test
public void write() throws Exception {
    File outputDir = temp.newFolder();
    ScannerReportWriter writer = new ScannerReportWriter(outputDir);
    NewActiveRule ar = new NewActiveRule.Builder().setRuleKey(RuleKey.of("java", "S001")).setSeverity("BLOCKER").setParam("p1", "v1").setCreatedAt(1_000L).setUpdatedAt(2_000L).setQProfileKey("qp1").build();
    ActiveRules activeRules = new DefaultActiveRules(singletonList(ar));
    ActiveRulesPublisher underTest = new ActiveRulesPublisher(activeRules);
    underTest.publish(writer);
    ScannerReportReader reader = new ScannerReportReader(outputDir);
    try (CloseableIterator<ScannerReport.ActiveRule> readIt = reader.readActiveRules()) {
        ScannerReport.ActiveRule reportAr = readIt.next();
        assertThat(reportAr.getRuleRepository()).isEqualTo("java");
        assertThat(reportAr.getRuleKey()).isEqualTo("S001");
        assertThat(reportAr.getSeverity()).isEqualTo(Constants.Severity.BLOCKER);
        assertThat(reportAr.getCreatedAt()).isEqualTo(1_000L);
        assertThat(reportAr.getUpdatedAt()).isEqualTo(2_000L);
        assertThat(reportAr.getQProfileKey()).isEqualTo("qp1");
        assertThat(reportAr.getParamsByKeyMap()).hasSize(1);
        assertThat(reportAr.getParamsByKeyMap().entrySet().iterator().next().getKey()).isEqualTo("p1");
        assertThat(reportAr.getParamsByKeyMap().entrySet().iterator().next().getValue()).isEqualTo("v1");
        assertThat(readIt.hasNext()).isFalse();
    }
}
Also used : ScannerReportReader(org.sonar.scanner.protocol.output.ScannerReportReader) DefaultActiveRules(org.sonar.api.batch.rule.internal.DefaultActiveRules) NewActiveRule(org.sonar.api.batch.rule.internal.NewActiveRule) NewActiveRule(org.sonar.api.batch.rule.internal.NewActiveRule) ScannerReport(org.sonar.scanner.protocol.output.ScannerReport) DefaultActiveRules(org.sonar.api.batch.rule.internal.DefaultActiveRules) ActiveRules(org.sonar.api.batch.rule.ActiveRules) ScannerReportWriter(org.sonar.scanner.protocol.output.ScannerReportWriter) File(java.io.File) Test(org.junit.Test)

Example 9 with DefaultActiveRules

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

the class HtmlSensorTest method test_descriptor_sonarqube_9_3.

@Test
public void test_descriptor_sonarqube_9_3() {
    final boolean[] called = { false };
    DefaultSensorDescriptor sensorDescriptor = new DefaultSensorDescriptor() {

        public SensorDescriptor processesFilesIndependently() {
            called[0] = true;
            return this;
        }
    };
    SonarRuntime sonarRuntime = SonarRuntimeImpl.forSonarQube(Version.create(9, 3), SonarQubeSide.SCANNER, SonarEdition.COMMUNITY);
    new HtmlSensor(sonarRuntime, null, null, new CheckFactory(new DefaultActiveRules(Collections.emptyList()))).describe(sensorDescriptor);
    assertThat(sensorDescriptor.name()).isEqualTo("HTML");
    assertThat(sensorDescriptor.languages()).isEmpty();
    assertTrue(called[0]);
}
Also used : DefaultActiveRules(org.sonar.api.batch.rule.internal.DefaultActiveRules) DefaultSensorDescriptor(org.sonar.api.batch.sensor.internal.DefaultSensorDescriptor) SonarRuntime(org.sonar.api.SonarRuntime) CheckFactory(org.sonar.api.batch.rule.CheckFactory) Test(org.junit.Test)

Example 10 with DefaultActiveRules

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

the class IgnoreIssuesFilterTest method shouldRejectIfRulePatternMatches.

@Test
public void shouldRejectIfRulePatternMatches() {
    DefaultActiveRules activeRules = new DefaultActiveRules(ImmutableSet.of());
    IgnoreIssuesFilter underTest = new IgnoreIssuesFilter(activeRules, analysisWarnings);
    WildcardPattern pattern = mock(WildcardPattern.class);
    when(pattern.match(ruleKey.toString())).thenReturn(true);
    underTest.addRuleExclusionPatternForComponent(component, pattern);
    assertThat(underTest.accept(issue, chain)).isFalse();
    verifyNoInteractions(analysisWarnings);
}
Also used : DefaultActiveRules(org.sonar.api.batch.rule.internal.DefaultActiveRules) WildcardPattern(org.sonar.api.utils.WildcardPattern) Test(org.junit.Test)

Aggregations

DefaultActiveRules (org.sonar.api.batch.rule.internal.DefaultActiveRules)18 Test (org.junit.Test)15 CheckFactory (org.sonar.api.batch.rule.CheckFactory)5 NewActiveRule (org.sonar.api.batch.rule.internal.NewActiveRule)5 RuleKey (org.sonar.api.rule.RuleKey)5 IssuePattern (org.sonar.scanner.issue.ignore.pattern.IssuePattern)5 SonarRuntime (org.sonar.api.SonarRuntime)4 ActiveRules (org.sonar.api.batch.rule.ActiveRules)3 DefaultSensorDescriptor (org.sonar.api.batch.sensor.internal.DefaultSensorDescriptor)3 WildcardPattern (org.sonar.api.utils.WildcardPattern)3 ArrayList (java.util.ArrayList)2 Before (org.junit.Before)2 InputFile (org.sonar.api.batch.fs.InputFile)2 DefaultInputFile (org.sonar.api.batch.fs.internal.DefaultInputFile)2 NoSonarFilter (org.sonar.api.issue.NoSonarFilter)2 FileLinesContext (org.sonar.api.measures.FileLinesContext)2 FileLinesContextFactory (org.sonar.api.measures.FileLinesContextFactory)2 RulesDefinition (org.sonar.api.server.rule.RulesDefinition)2 File (java.io.File)1 LoadedActiveRule (org.sonar.api.batch.rule.LoadedActiveRule)1