use of org.sonar.api.batch.sensor.internal.SensorContextTester in project sonar-java by SonarSource.
the class SurefireJavaParserTest method should_register_tests.
@Test
public void should_register_tests() throws URISyntaxException {
SensorContextTester context = SensorContextTester.create(new File(""));
MutableTestCase testCase = mock(MutableTestCase.class);
when(testCase.setDurationInMs(anyLong())).thenReturn(testCase);
when(testCase.setStatus(any(TestCase.Status.class))).thenReturn(testCase);
when(testCase.setMessage(isNull())).thenReturn(testCase);
when(testCase.setStackTrace(anyString())).thenReturn(testCase);
when(testCase.setType(anyString())).thenReturn(testCase);
MutableTestPlan testPlan = mock(MutableTestPlan.class);
when(testPlan.addTestCase(anyString())).thenReturn(testCase);
when(perspectives.as(eq(MutableTestPlan.class), argThat((ArgumentMatcher<InputFile>) o -> ":ch.hortis.sonar.mvn.mc.MetricsCollectorRegistryTest".equals(o.key())))).thenReturn(testPlan);
parser.collect(context, getDirs("multipleReports"), true);
verify(testPlan).addTestCase("testGetUnKnownCollector");
verify(testPlan).addTestCase("testGetJDependsCollector");
}
use of org.sonar.api.batch.sensor.internal.SensorContextTester in project sonar-java by SonarSource.
the class SurefireJavaParserTest method shouldAggregateReportsFromMultipleDirectories.
@Test
public void shouldAggregateReportsFromMultipleDirectories() throws URISyntaxException {
SensorContextTester context = mockContext();
parser.collect(context, getDirs("multipleDirectories/dir1", "multipleDirectories/dir2"), true);
assertThat(context.measures(":ch.hortis.sonar.mvn.mc.MetricsCollectorRegistryTest")).hasSize(5);
assertThat(context.measures(":ch.hortis.sonar.mvn.mc.CloverCollectorTest")).hasSize(5);
assertThat(context.measures(":ch.hortis.sonar.mvn.mc.CheckstyleCollectorTest")).hasSize(5);
assertThat(context.measures(":ch.hortis.sonar.mvn.SonarMojoTest")).hasSize(5);
assertThat(context.measures(":ch.hortis.sonar.mvn.mc.JDependsCollectorTest")).hasSize(5);
assertThat(context.measures(":ch.hortis.sonar.mvn.mc.JavaNCSSCollectorTest")).hasSize(5);
}
use of org.sonar.api.batch.sensor.internal.SensorContextTester in project sonar-java by SonarSource.
the class CheckVerifier method sonarComponents.
static SonarComponents sonarComponents(File file) {
SensorContextTester context = SensorContextTester.create(new File("")).setRuntime(SonarRuntimeImpl.forSonarLint(Version.create(6, 7)));
context.setSettings(new MapSettings().setProperty("sonar.java.failOnException", true));
SonarComponents sonarComponents = new SonarComponents(null, context.fileSystem(), null, null, null, null) {
@Override
public boolean reportAnalysisError(RecognitionException re, File file) {
return false;
}
};
sonarComponents.setSensorContext(context);
context.fileSystem().add(new TestInputFileBuilder("", file.getPath()).setCharset(StandardCharsets.UTF_8).build());
return sonarComponents;
}
use of org.sonar.api.batch.sensor.internal.SensorContextTester in project sonar-java by SonarSource.
the class JacocoSensorJava9Test method test_jacoco_java9_coverage.
@Test
public void test_jacoco_java9_coverage() throws Exception {
Path baseDir = Paths.get(getClass().getResource("/org/sonar/plugins/jacoco/JaCoCoJava9Test/").toURI());
Files.copy(baseDir.resolve("Hello9.class.toCopy"), baseDir.resolve("Hello9.class"), StandardCopyOption.REPLACE_EXISTING);
DefaultInputFile resource = new TestInputFileBuilder("", "").setLines(10).build();
JavaResourceLocator javaResourceLocator = mock(JavaResourceLocator.class);
when(javaResourceLocator.findResourceByClassName(anyString())).thenReturn(resource);
JavaClasspath javaClasspath = mock(JavaClasspath.class);
when(javaClasspath.getBinaryDirs()).thenReturn(Collections.singletonList(baseDir.toFile()));
SensorContextTester context = SensorContextTester.create(baseDir);
context.settings().setProperty(REPORT_PATHS_PROPERTY, "jacoco.exec");
JaCoCoSensor sensor = new JaCoCoSensor(null, javaResourceLocator, javaClasspath);
sensor.execute(context);
assertThat(context.lineHits(resource.key(), 5)).isEqualTo(1);
}
use of org.sonar.api.batch.sensor.internal.SensorContextTester in project sonar-java by SonarSource.
the class SurefireSensorTest method shouldHandleTestSuiteDetails.
@Test
public void shouldHandleTestSuiteDetails() throws URISyntaxException {
SensorContextTester context = SensorContextTester.create(new File(""));
context.fileSystem().add(resource("org.sonar.core.ExtensionsFinderTest")).add(resource("org.sonar.core.ExtensionsFinderTest2")).add(resource("org.sonar.core.ExtensionsFinderTest3"));
collect(context, "/org/sonar/plugins/surefire/SurefireSensorTest/shouldHandleTestSuiteDetails/");
assertThat(context.measures(":org.sonar.core.ExtensionsFinderTest")).hasSize(5);
assertThat(context.measures(":org.sonar.core.ExtensionsFinderTest2")).hasSize(5);
assertThat(context.measures(":org.sonar.core.ExtensionsFinderTest3")).hasSize(5);
assertThat(context.measure(":org.sonar.core.ExtensionsFinderTest", CoreMetrics.TESTS).value()).isEqualTo(4);
assertThat(context.measure(":org.sonar.core.ExtensionsFinderTest", CoreMetrics.TEST_EXECUTION_TIME).value()).isEqualTo(111L);
assertThat(context.measure(":org.sonar.core.ExtensionsFinderTest", CoreMetrics.TEST_FAILURES).value()).isEqualTo(1);
assertThat(context.measure(":org.sonar.core.ExtensionsFinderTest", CoreMetrics.TEST_ERRORS).value()).isEqualTo(1);
assertThat(context.measure(":org.sonar.core.ExtensionsFinderTest", CoreMetrics.SKIPPED_TESTS).value()).isEqualTo(0);
assertThat(context.measure(":org.sonar.core.ExtensionsFinderTest2", CoreMetrics.TESTS).value()).isEqualTo(2);
assertThat(context.measure(":org.sonar.core.ExtensionsFinderTest2", CoreMetrics.TEST_EXECUTION_TIME).value()).isEqualTo(2);
assertThat(context.measure(":org.sonar.core.ExtensionsFinderTest2", CoreMetrics.TEST_FAILURES).value()).isEqualTo(0);
assertThat(context.measure(":org.sonar.core.ExtensionsFinderTest2", CoreMetrics.TEST_ERRORS).value()).isEqualTo(0);
assertThat(context.measure(":org.sonar.core.ExtensionsFinderTest2", CoreMetrics.SKIPPED_TESTS).value()).isEqualTo(0);
assertThat(context.measure(":org.sonar.core.ExtensionsFinderTest3", CoreMetrics.TESTS).value()).isEqualTo(1);
assertThat(context.measure(":org.sonar.core.ExtensionsFinderTest3", CoreMetrics.TEST_EXECUTION_TIME).value()).isEqualTo(16);
assertThat(context.measure(":org.sonar.core.ExtensionsFinderTest3", CoreMetrics.TEST_FAILURES).value()).isEqualTo(0);
assertThat(context.measure(":org.sonar.core.ExtensionsFinderTest3", CoreMetrics.TEST_ERRORS).value()).isEqualTo(0);
assertThat(context.measure(":org.sonar.core.ExtensionsFinderTest3", CoreMetrics.SKIPPED_TESTS).value()).isEqualTo(1);
}
Aggregations