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));
}
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);
}
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.");
}
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));
}
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();
}
Aggregations