use of org.sonar.api.batch.sensor.internal.SensorContextTester in project sonar-java by SonarSource.
the class JaCoCoSensorTest method do_not_save_measure_on_resource_which_doesnt_exist_in_the_context.
@Test
public void do_not_save_measure_on_resource_which_doesnt_exist_in_the_context() {
when(javaClasspath.getBinaryDirs()).thenReturn(ImmutableList.of(outputDir));
SensorContextTester context = spy(SensorContextTester.create(new File("")));
sensor.execute(context);
verify(context, never()).newCoverage();
}
use of org.sonar.api.batch.sensor.internal.SensorContextTester 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.sensor.internal.SensorContextTester 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.sensor.internal.SensorContextTester in project sonar-java by SonarSource.
the class SurefireSensorTest method ignoreSuiteAsInnerClass.
@Test
public void ignoreSuiteAsInnerClass() throws URISyntaxException {
SensorContextTester context = SensorContextTester.create(new File(""));
context.fileSystem().add(resource("org.apache.shindig.protocol.TestHandler"));
collect(context, "/org/sonar/plugins/surefire/SurefireSensorTest/ignoreSuiteAsInnerClass/");
// ignore TestHandler$Input.xml
assertThat(context.measure(":org.apache.shindig.protocol.TestHandler", CoreMetrics.TESTS).value()).isEqualTo(0);
assertThat(context.measure(":org.apache.shindig.protocol.TestHandler", CoreMetrics.SKIPPED_TESTS).value()).isEqualTo(1);
}
use of org.sonar.api.batch.sensor.internal.SensorContextTester in project sonar-java by SonarSource.
the class SurefireSensorTest method should_support_reportNameSuffix.
@Test
public void should_support_reportNameSuffix() throws URISyntaxException {
SensorContextTester context = SensorContextTester.create(new File(""));
context.fileSystem().add(resource("org.sonar.Foo"));
collect(context, "/org/sonar/plugins/surefire/SurefireSensorTest/should_support_reportNameSuffix/");
assertThat(context.measure(":org.sonar.Foo", CoreMetrics.TESTS).value()).isEqualTo(4);
assertThat(context.measure(":org.sonar.Foo", CoreMetrics.TEST_FAILURES).value()).isEqualTo(2);
assertThat(context.measure(":org.sonar.Foo", CoreMetrics.TEST_ERRORS).value()).isEqualTo(0);
assertThat(context.measure(":org.sonar.Foo", CoreMetrics.SKIPPED_TESTS).value()).isEqualTo(2);
}
Aggregations