Search in sources :

Example 31 with DefaultInputFile

use of org.sonar.api.batch.fs.internal.DefaultInputFile in project sonar-java by SonarSource.

the class JavaAstScannerTest method noSonarLines.

@Test
public void noSonarLines() throws Exception {
    File file = new File("src/test/files/metrics/NoSonar.java");
    DefaultInputFile resource = new TestInputFileBuilder("", "src/test/files/metrics/NoSonar.java").build();
    fs.add(resource);
    NoSonarFilter noSonarFilter = mock(NoSonarFilter.class);
    JavaAstScanner.scanSingleFileForTests(file, new VisitorsBridge(new Measurer(fs, context, noSonarFilter)));
    verify(noSonarFilter).noSonarInFile(resource, ImmutableSet.of(8));
    // No Sonar on tests files
    NoSonarFilter noSonarFilterForTest = mock(NoSonarFilter.class);
    JavaAstScanner.scanSingleFileForTests(file, new VisitorsBridge(new Measurer(fs, context, noSonarFilterForTest).new TestFileMeasurer()));
    verify(noSonarFilterForTest).noSonarInFile(resource, ImmutableSet.of(8));
}
Also used : TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) NoSonarFilter(org.sonar.api.issue.NoSonarFilter) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) Measurer(org.sonar.java.Measurer) VisitorsBridge(org.sonar.java.model.VisitorsBridge) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) File(java.io.File) Test(org.junit.Test)

Example 32 with DefaultInputFile

use of org.sonar.api.batch.fs.internal.DefaultInputFile in project sonar-java by SonarSource.

the class JavaIssueTest method test_addFlow.

@Test
public void test_addFlow() throws Exception {
    TestInputFileBuilder tifb = new TestInputFileBuilder("module", "relPath");
    tifb.setModuleBaseDir(new java.io.File("").toPath());
    tifb.setLines(3);
    tifb.setOriginalLineOffsets(new int[] { 0, 10, 15 });
    tifb.setLastValidOffset(25);
    DefaultInputFile file = tifb.build();
    RuleKey ruleKey = RuleKey.of("squid", "ruleKey");
    SensorContext sensorContext = mock(SensorContext.class);
    SensorStorage storage = mock(SensorStorage.class);
    DefaultIssue newIssueEmptyFlow = new DefaultIssue(storage);
    DefaultIssue newIssueWithFlow = new DefaultIssue(storage);
    Mockito.when(sensorContext.newIssue()).thenReturn(newIssueEmptyFlow, newIssueWithFlow);
    JavaIssue javaIssue = JavaIssue.create(sensorContext, ruleKey, null);
    javaIssue.setPrimaryLocation(file, "main message", 1, 2, 1, 6);
    javaIssue.addFlow(file, new ArrayList<>());
    javaIssue.save();
    Mockito.verify(storage, Mockito.times(1)).store(newIssueEmptyFlow);
    assertThat(newIssueEmptyFlow.flows()).isEmpty();
    javaIssue = JavaIssue.create(sensorContext, ruleKey, null);
    javaIssue.setPrimaryLocation(file, "main message", 1, 2, 1, 6);
    List<List<AnalyzerMessage>> flows = new ArrayList<>();
    flows.add(Lists.newArrayList(new AnalyzerMessage(null, file.file(), new AnalyzerMessage.TextSpan(2, 2, 2, 4), "flow message 1", 0)));
    flows.add(Lists.newArrayList(new AnalyzerMessage(null, file.file(), new AnalyzerMessage.TextSpan(3, 1, 3, 5), "flow message 2", 0)));
    javaIssue.addFlow(file, flows);
    javaIssue.save();
    Mockito.verify(storage, Mockito.times(1)).store(newIssueWithFlow);
    assertThat(newIssueWithFlow.flows()).hasSize(2);
}
Also used : SensorContext(org.sonar.api.batch.SensorContext) RuleKey(org.sonar.api.rule.RuleKey) ArrayList(java.util.ArrayList) DefaultIssue(org.sonar.api.batch.sensor.issue.internal.DefaultIssue) SensorStorage(org.sonar.api.batch.sensor.internal.SensorStorage) TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) ArrayList(java.util.ArrayList) List(java.util.List) InputFile(org.sonar.api.batch.fs.InputFile) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) Test(org.junit.Test)

Example 33 with DefaultInputFile

use of org.sonar.api.batch.fs.internal.DefaultInputFile in project sonar-java by SonarSource.

the class JaCoCoSensorTest method should_log_when_class_are_not_matching_with_report.

@Test
public void should_log_when_class_are_not_matching_with_report() throws Exception {
    String testDir = "/org/sonar/plugins/jacoco/JaCoCoNoMatch/";
    outputDir = TestUtils.getResource(testDir);
    jacocoExecutionData = new File(outputDir, "jacoco.exec");
    Files.copy(TestUtils.getResource(testDir + "org/foo/bar/Example2.class.toCopy"), new File(outputDir, "org/foo/bar/Example2.class"));
    Files.copy(TestUtils.getResource(testDir + "Example.class.toCopy"), new File(outputDir, "Example.class"));
    DefaultInputFile resource = new TestInputFileBuilder("", "").setLines(10).build();
    when(javaResourceLocator.findResourceByClassName(anyString())).thenReturn(resource);
    when(javaClasspath.getBinaryDirs()).thenReturn(ImmutableList.of(outputDir));
    SensorContextTester context = SensorContextTester.create(outputDir);
    context.setRuntime(SonarRuntimeImpl.forSonarQube(SQ_6_7, SonarQubeSide.SCANNER));
    context.fileSystem().setWorkDir(temp.newFolder().toPath());
    context.setSettings(new MapSettings().setProperty(REPORT_PATHS_PROPERTY, jacocoExecutionData.getAbsolutePath()));
    sensor.execute(context);
    List<String> warnLogs = logTester.logs(LoggerLevel.WARN);
    assertThat(warnLogs).contains("The following class(es) did not match with execution data:", "> 'org/foo/bar/Example2'", "> 'Example'", "In order to have accurate coverage measures, the same class files must be used as at runtime for report generation.");
}
Also used : TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) SensorContextTester(org.sonar.api.batch.sensor.internal.SensorContextTester) MapSettings(org.sonar.api.config.internal.MapSettings) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) File(java.io.File) Test(org.junit.Test)

Example 34 with DefaultInputFile

use of org.sonar.api.batch.fs.internal.DefaultInputFile in project sonar-java by SonarSource.

the class JaCoCoSensorTest method test_read_execution_data_for_lines_covered_by_tests.

@Test
public void test_read_execution_data_for_lines_covered_by_tests() throws IOException {
    outputDir = TestUtils.getResource("/org/sonar/plugins/jacoco/JaCoCoSensorTest2/");
    jacocoExecutionData = new File(outputDir, "jacoco.exec");
    Files.copy(TestUtils.getResource("/org/sonar/plugins/jacoco/JaCoCoSensorTest2/org/example/App.class.toCopy"), new File(jacocoExecutionData.getParentFile(), "/org/example/App.class"));
    DefaultInputFile resource = new TestInputFileBuilder("", "").setLines(10).build();
    when(javaResourceLocator.findResourceByClassName(anyString())).thenReturn(resource);
    when(javaClasspath.getBinaryDirs()).thenReturn(ImmutableList.of(outputDir));
    MutableTestable testAbleFile = mock(MutableTestable.class);
    when(perspectives.as(eq(MutableTestable.class), eq(resource))).thenReturn(testAbleFile);
    MutableTestCase testCase = mock(MutableTestCase.class);
    when(testCase.name()).thenReturn("test");
    MutableTestPlan testPlan = mock(MutableTestPlan.class);
    when(testPlan.testCasesByName("test")).thenReturn(newArrayList(testCase));
    when(perspectives.as(eq(MutableTestPlan.class), eq(resource))).thenReturn(testPlan);
    context.settings().setProperty(REPORT_PATH_PROPERTY, jacocoExecutionData.getAbsolutePath());
    SensorContextTester spy = Mockito.spy(context);
    sensor.execute(spy);
    verify(spy, times(1)).newCoverage();
    verify(testCase).setCoverageBlock(testAbleFile, newArrayList(3, 6));
}
Also used : TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) MutableTestPlan(org.sonar.api.test.MutableTestPlan) MutableTestCase(org.sonar.api.test.MutableTestCase) SensorContextTester(org.sonar.api.batch.sensor.internal.SensorContextTester) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) MutableTestable(org.sonar.api.test.MutableTestable) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) File(java.io.File) Test(org.junit.Test)

Example 35 with DefaultInputFile

use of org.sonar.api.batch.fs.internal.DefaultInputFile in project sonar-java by SonarSource.

the class SonarComponentsTest method fail_on_empty_location.

@Test
public void fail_on_empty_location() {
    JavaCheck expectedCheck = new CustomCheck();
    CheckRegistrar expectedRegistrar = getRegistrar(expectedCheck);
    RuleKey ruleKey = RuleKey.of("MyRepo", "CustomCheck");
    File file = new File("file.java");
    DefaultInputFile inputFile = new TestInputFileBuilder("", file.getPath()).initMetadata("class A {\n" + "  void foo() {\n" + "    System.out.println();\n" + "  }\n" + "}\n").build();
    SensorContextTester context = SensorContextTester.create(new File(""));
    DefaultFileSystem fileSystem = context.fileSystem();
    SonarComponents sonarComponents = new SonarComponents(fileLinesContextFactory, fileSystem, null, null, checkFactory, new CheckRegistrar[] { expectedRegistrar });
    sonarComponents.setSensorContext(context);
    AnalyzerMessage.TextSpan emptyTextSpan = new AnalyzerMessage.TextSpan(3, 10, 3, 10);
    AnalyzerMessage analyzerMessageEmptyLocation = new AnalyzerMessage(expectedCheck, file, emptyTextSpan, "message", 0);
    assertThatThrownBy(() -> sonarComponents.reportIssue(analyzerMessageEmptyLocation, ruleKey, inputFile, 0.0)).isInstanceOf(IllegalStateException.class).hasMessageContaining("Issue location should not be empty");
    assertThat(context.allIssues()).isEmpty();
    AnalyzerMessage.TextSpan nonEmptyTextSpan = new AnalyzerMessage.TextSpan(3, 10, 3, 15);
    AnalyzerMessage analyzerMessageValidLocation = new AnalyzerMessage(expectedCheck, file, nonEmptyTextSpan, "message", 0);
    sonarComponents.reportIssue(analyzerMessageValidLocation, ruleKey, inputFile, 0.0);
    assertThat(context.allIssues()).isNotEmpty();
}
Also used : SensorContextTester(org.sonar.api.batch.sensor.internal.SensorContextTester) RuleKey(org.sonar.api.rule.RuleKey) JavaCheck(org.sonar.plugins.java.api.JavaCheck) TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) CheckRegistrar(org.sonar.plugins.java.api.CheckRegistrar) InputFile(org.sonar.api.batch.fs.InputFile) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) File(java.io.File) DefaultFileSystem(org.sonar.api.batch.fs.internal.DefaultFileSystem) Test(org.junit.Test)

Aggregations

DefaultInputFile (org.sonar.api.batch.fs.internal.DefaultInputFile)173 Test (org.junit.Test)117 TestInputFileBuilder (org.sonar.api.batch.fs.internal.TestInputFileBuilder)75 File (java.io.File)63 InputFile (org.sonar.api.batch.fs.InputFile)46 Path (java.nio.file.Path)25 DefaultFileSystem (org.sonar.api.batch.fs.internal.DefaultFileSystem)18 SensorContextTester (org.sonar.api.batch.sensor.internal.SensorContextTester)17 BlameOutput (org.sonar.api.batch.scm.BlameCommand.BlameOutput)16 FileMetadata (org.sonar.api.batch.fs.internal.FileMetadata)14 IOException (java.io.IOException)13 DefaultIndexedFile (org.sonar.api.batch.fs.internal.DefaultIndexedFile)11 Metadata (org.sonar.api.batch.fs.internal.Metadata)11 Before (org.junit.Before)10 DefaultInputModule (org.sonar.api.batch.fs.internal.DefaultInputModule)10 BlameLine (org.sonar.api.batch.scm.BlameLine)9 List (java.util.List)8 ScannerReport (org.sonar.scanner.protocol.output.ScannerReport)8 ProjectDefinition (org.sonar.api.batch.bootstrap.ProjectDefinition)7 TextRange (org.sonar.api.batch.fs.TextRange)7