Search in sources :

Example 71 with SensorContextTester

use of org.sonar.api.batch.sensor.internal.SensorContextTester in project sonar-java by SonarSource.

the class JavaSensorTest method filter_checks_when_autoscan_true.

@Test
void filter_checks_when_autoscan_true() throws IOException {
    MapSettings settings = new MapSettings();
    settings.setProperty("sonar.internal.analysis.autoscan", "true");
    SensorContextTester context = analyzeTwoFilesWithIssues(settings);
    assertThat(context.allIssues()).extracting(issue -> issue.ruleKey().toString()).contains(// main check in SonarWay
    "java:S1220", // test check in SonarWay
    "java:S2187").doesNotContain("CustomRepository:CustomMainCheck", "CustomRepository:CustomJspCheck", "CustomRepository:CustomTestCheck", // main check in SonarWay, not supported by autoscan (CombineCatchCheck)
    "java:S2147", // not in SonarWay (FileHeaderCheck)
    "java:S1451", // SE check (BooleanGratuitousExpressionsCheck)
    "java:S2589");
}
Also used : MapSettings(org.sonar.api.config.internal.MapSettings) SensorContextTester(org.sonar.api.batch.sensor.internal.SensorContextTester) Test(org.junit.jupiter.api.Test) ClasspathForTest(org.sonar.java.classpath.ClasspathForTest)

Example 72 with SensorContextTester

use of org.sonar.api.batch.sensor.internal.SensorContextTester in project sonar-java by SonarSource.

the class JavaSensorTest method should_not_invoke_jasper_jsp_compilation_in_autoscan_for_security_reasons.

@Test
void should_not_invoke_jasper_jsp_compilation_in_autoscan_for_security_reasons() throws Exception {
    Path base = tmp.newFolder().toPath();
    MapSettings settings = new MapSettings();
    settings.setProperty("sonar.internal.analysis.autoscan", "true");
    SensorContextTester context = SensorContextTester.create(base);
    context.setSettings(settings);
    context.fileSystem().setWorkDir(tmp.newFolder().toPath());
    SonarComponents sonarComponents = createSonarComponentsMock(context);
    JspCodeScanner jspCodeVisitor = mock(JspCodeScanner.class);
    when(sonarComponents.mainChecks()).thenReturn(Collections.emptyList());
    when(sonarComponents.testChecks()).thenReturn(Collections.emptyList());
    when(sonarComponents.jspChecks()).thenReturn(Collections.singletonList(jspCodeVisitor));
    Jasper jasper = mock(Jasper.class);
    JavaSensor jss = new JavaSensor(sonarComponents, context.fileSystem(), mock(JavaResourceLocator.class), context.config(), mock(NoSonarFilter.class), null, jasper);
    jss.execute(context);
    verify(jasper, never()).generateFiles(any(), any());
    verify(jspCodeVisitor, never()).scanFile(any());
}
Also used : Path(java.nio.file.Path) SonarComponents(org.sonar.java.SonarComponents) Jasper(org.sonar.java.jsp.Jasper) MapSettings(org.sonar.api.config.internal.MapSettings) SensorContextTester(org.sonar.api.batch.sensor.internal.SensorContextTester) NoSonarFilter(org.sonar.api.issue.NoSonarFilter) DefaultJavaResourceLocator(org.sonar.java.DefaultJavaResourceLocator) JavaResourceLocator(org.sonar.plugins.java.api.JavaResourceLocator) Test(org.junit.jupiter.api.Test) ClasspathForTest(org.sonar.java.classpath.ClasspathForTest)

Example 73 with SensorContextTester

use of org.sonar.api.batch.sensor.internal.SensorContextTester in project sonar-java by SonarSource.

the class CheckstyleSensorTest method executeSensorImporting.

private List<ExternalIssue> executeSensorImporting(@Nullable String fileName) throws IOException {
    SensorContextTester context = ExternalReportTestUtils.createContext(PROJECT_DIR);
    if (fileName != null) {
        File reportFile = ExternalReportTestUtils.generateReport(PROJECT_DIR, tmp, fileName);
        context.settings().setProperty("sonar.java.checkstyle.reportPaths", reportFile.getPath());
    }
    checkstyleSensor.execute(context);
    return new ArrayList<>(context.allExternalIssues());
}
Also used : SensorContextTester(org.sonar.api.batch.sensor.internal.SensorContextTester) ArrayList(java.util.ArrayList) File(java.io.File)

Example 74 with SensorContextTester

use of org.sonar.api.batch.sensor.internal.SensorContextTester in project sonar-java by SonarSource.

the class PmdSensorTest method createContext.

public static SensorContextTester createContext(Path projectDir) throws IOException {
    SensorContextTester context = SensorContextTester.create(projectDir);
    Files.list(projectDir).filter(Files::isRegularFile).forEach(file -> addFileToContext(context, projectDir, file));
    return context;
}
Also used : SensorContextTester(org.sonar.api.batch.sensor.internal.SensorContextTester)

Example 75 with SensorContextTester

use of org.sonar.api.batch.sensor.internal.SensorContextTester in project sonar-java by SonarSource.

the class SonarComponentsTest method add_issue_or_parse_error.

@Test
void add_issue_or_parse_error() throws Exception {
    JavaCheck expectedCheck = new CustomCheck();
    CheckRegistrar expectedRegistrar = getRegistrar(expectedCheck);
    SensorContextTester context = SensorContextTester.create(new File("."));
    DefaultFileSystem fileSystem = context.fileSystem();
    TestInputFileBuilder inputFileBuilder = new TestInputFileBuilder("", "file.java");
    inputFileBuilder.setLines(45);
    int[] lineStartOffsets = new int[45];
    lineStartOffsets[35] = 12;
    lineStartOffsets[42] = 1;
    int lastValidOffset = 420;
    inputFileBuilder.setOriginalLineStartOffsets(lineStartOffsets);
    inputFileBuilder.setOriginalLineEndOffsets(computeLineEndOffsets(lineStartOffsets, lastValidOffset));
    inputFileBuilder.setLastValidOffset(lastValidOffset);
    InputFile inputFile = inputFileBuilder.build();
    fileSystem.add(inputFile);
    when(this.checks.ruleKey(any(JavaCheck.class))).thenReturn(mock(RuleKey.class));
    SonarComponents sonarComponents = new SonarComponents(fileLinesContextFactory, fileSystem, null, null, checkFactory, new CheckRegistrar[] { expectedRegistrar });
    sonarComponents.setSensorContext(context);
    sonarComponents.addIssue(inputFile, expectedCheck, -5, "message on wrong line", null);
    sonarComponents.addIssue(inputFile, expectedCheck, 42, "message on line 42", 1);
    sonarComponents.reportIssue(new AnalyzerMessage(expectedCheck, inputFile, 35, "other message", 0));
    List<Issue> issues = new ArrayList<>(context.allIssues());
    assertThat(issues).hasSize(3);
    assertThat(issues.get(0).primaryLocation().message()).isEqualTo("message on wrong line");
    assertThat(issues.get(1).primaryLocation().message()).isEqualTo("message on line 42");
    assertThat(issues.get(2).primaryLocation().message()).isEqualTo("other message");
    RecognitionException parseError = new RecognitionException(-1, "invalid code", new Exception("parse error"));
    context.setRuntime(SonarRuntimeImpl.forSonarLint(V8_9));
    assertThat(sonarComponents.reportAnalysisError(parseError, inputFile)).isTrue();
    context.setRuntime(SonarRuntimeImpl.forSonarQube(V8_9, SonarQubeSide.SCANNER, SonarEdition.COMMUNITY));
    assertThat(sonarComponents.reportAnalysisError(parseError, inputFile)).isFalse();
}
Also used : Issue(org.sonar.api.batch.sensor.issue.Issue) SensorContextTester(org.sonar.api.batch.sensor.internal.SensorContextTester) RuleKey(org.sonar.api.rule.RuleKey) JavaCheck(org.sonar.plugins.java.api.JavaCheck) ArrayList(java.util.ArrayList) NoSuchFileException(java.nio.file.NoSuchFileException) RecognitionException(com.sonar.sslr.api.RecognitionException) ApiMismatchException(org.sonar.java.exceptions.ApiMismatchException) InputFile(org.sonar.api.batch.fs.InputFile) TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) CheckRegistrar(org.sonar.plugins.java.api.CheckRegistrar) AnalyzerMessage(org.sonar.java.reporting.AnalyzerMessage) InputFile(org.sonar.api.batch.fs.InputFile) File(java.io.File) DefaultFileSystem(org.sonar.api.batch.fs.internal.DefaultFileSystem) RecognitionException(com.sonar.sslr.api.RecognitionException) Test(org.junit.jupiter.api.Test) ClasspathForTest(org.sonar.java.classpath.ClasspathForTest) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

SensorContextTester (org.sonar.api.batch.sensor.internal.SensorContextTester)224 File (java.io.File)96 Test (org.junit.jupiter.api.Test)94 InputFile (org.sonar.api.batch.fs.InputFile)75 DefaultInputFile (org.sonar.api.batch.fs.internal.DefaultInputFile)63 MapSettings (org.sonar.api.config.internal.MapSettings)60 Test (org.junit.Test)59 TestInputFileBuilder (org.sonar.api.batch.fs.internal.TestInputFileBuilder)49 Test (org.testng.annotations.Test)42 DefaultFileSystem (org.sonar.api.batch.fs.internal.DefaultFileSystem)40 OpenEdgePluginTest (org.sonar.plugins.openedge.OpenEdgePluginTest)37 Path (java.nio.file.Path)35 OpenEdgeSettings (org.sonar.plugins.openedge.foundation.OpenEdgeSettings)30 ClasspathForTest (org.sonar.java.classpath.ClasspathForTest)25 SonarComponents (org.sonar.java.SonarComponents)22 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)20 GeneratedFile (org.sonar.java.model.GeneratedFile)18 ArrayList (java.util.ArrayList)15 ExternalIssue (org.sonar.api.batch.sensor.issue.ExternalIssue)15 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)14