Search in sources :

Example 41 with SensorContextTester

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");
}
Also used : MutableTestPlan(org.sonar.api.test.MutableTestPlan) MutableTestCase(org.sonar.api.test.MutableTestCase) SensorContextTester(org.sonar.api.batch.sensor.internal.SensorContextTester) ArgumentMatcher(org.mockito.ArgumentMatcher) InputFile(org.sonar.api.batch.fs.InputFile) File(java.io.File) Test(org.junit.Test)

Example 42 with SensorContextTester

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);
}
Also used : SensorContextTester(org.sonar.api.batch.sensor.internal.SensorContextTester) Test(org.junit.Test)

Example 43 with SensorContextTester

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;
}
Also used : TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) SonarComponents(org.sonar.java.SonarComponents) SensorContextTester(org.sonar.api.batch.sensor.internal.SensorContextTester) MapSettings(org.sonar.api.config.internal.MapSettings) File(java.io.File) RecognitionException(com.sonar.sslr.api.RecognitionException)

Example 44 with SensorContextTester

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);
}
Also used : Path(java.nio.file.Path) TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) JavaClasspath(org.sonar.java.JavaClasspath) SensorContextTester(org.sonar.api.batch.sensor.internal.SensorContextTester) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) JavaResourceLocator(org.sonar.plugins.java.api.JavaResourceLocator) Test(org.junit.Test)

Example 45 with SensorContextTester

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);
}
Also used : SensorContextTester(org.sonar.api.batch.sensor.internal.SensorContextTester) InputFile(org.sonar.api.batch.fs.InputFile) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) File(java.io.File) Test(org.junit.Test)

Aggregations

SensorContextTester (org.sonar.api.batch.sensor.internal.SensorContextTester)56 Test (org.junit.Test)48 DefaultInputFile (org.sonar.api.batch.fs.internal.DefaultInputFile)35 File (java.io.File)34 TestInputFileBuilder (org.sonar.api.batch.fs.internal.TestInputFileBuilder)23 InputFile (org.sonar.api.batch.fs.InputFile)22 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)10 DefaultFileSystem (org.sonar.api.batch.fs.internal.DefaultFileSystem)10 MapSettings (org.sonar.api.config.internal.MapSettings)8 SonarComponents (org.sonar.java.SonarComponents)7 Issue (org.sonar.api.batch.sensor.issue.Issue)5 RecognitionException (com.sonar.sslr.api.RecognitionException)4 Path (java.nio.file.Path)4 JavaCheck (org.sonar.plugins.java.api.JavaCheck)4 Lists (com.google.common.collect.Lists)3 Nullable (javax.annotation.Nullable)3 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)3 Test (org.junit.jupiter.api.Test)3 RuleKey (org.sonar.api.rule.RuleKey)3 JavaResourceLocator (org.sonar.plugins.java.api.JavaResourceLocator)3