Search in sources :

Example 6 with SensorContextTester

use of org.sonar.api.batch.sensor.internal.SensorContextTester in project sonar-java by SonarSource.

the class XmlFileSensorTest method test_no_issues_but_xml_file_still_published.

@Test
public void test_no_issues_but_xml_file_still_published() throws Exception {
    SensorContextTester context = SensorContextTester.create(new File("src/test/files/maven2/").getAbsoluteFile());
    DefaultFileSystem fs = context.fileSystem();
    final File file = new File("src/test/files/maven2/pom.xml");
    DefaultInputFile inputFile = new TestInputFileBuilder("", "pom.xml").setModuleBaseDir(fs.baseDirPath()).setPublish(false).build();
    fs.add(inputFile);
    SonarComponents sonarComponents = createSonarComponentsMock(fs, file);
    XmlFileSensor sensor = new XmlFileSensor(sonarComponents, fs);
    assertThat(inputFile.isPublished()).isFalse();
    sensor.execute(context);
    assertThat(inputFile.isPublished()).isTrue();
    verify(sonarComponents, never()).reportIssue(Mockito.argThat(argument -> file.getAbsolutePath().equals(argument.getFile().getAbsolutePath())));
}
Also used : TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) DefaultFileSystem(org.sonar.api.batch.fs.internal.DefaultFileSystem) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) SensorContextTester(org.sonar.api.batch.sensor.internal.SensorContextTester) Lists(com.google.common.collect.Lists) Files(com.google.common.io.Files) JavaCheck(org.sonar.plugins.java.api.JavaCheck) SonarComponents(org.sonar.java.SonarComponents) Checks(org.sonar.api.batch.rule.Checks) Nullable(javax.annotation.Nullable) Before(org.junit.Before) IOException(java.io.IOException) Test(org.junit.Test) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) RuleAnnotationUtils(org.sonar.api.rules.RuleAnnotationUtils) File(java.io.File) StandardCharsets(java.nio.charset.StandardCharsets) TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) SensorDescriptor(org.sonar.api.batch.sensor.SensorDescriptor) Mockito.verify(org.mockito.Mockito.verify) Mockito(org.mockito.Mockito) Mockito.never(org.mockito.Mockito.never) PomElementOrderCheck(org.sonar.java.checks.xml.maven.PomElementOrderCheck) RuleKey(org.sonar.api.rule.RuleKey) Mockito.mock(org.mockito.Mockito.mock) SonarComponents(org.sonar.java.SonarComponents) SensorContextTester(org.sonar.api.batch.sensor.internal.SensorContextTester) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) File(java.io.File) DefaultFileSystem(org.sonar.api.batch.fs.internal.DefaultFileSystem) Test(org.junit.Test)

Example 7 with SensorContextTester

use of org.sonar.api.batch.sensor.internal.SensorContextTester in project sonar-java by SonarSource.

the class SurefireJavaParserTest method should_log_missing_resource_with_debug_level.

@Test
public void should_log_missing_resource_with_debug_level() throws Exception {
    SensorContextTester context = mockContext();
    parser = new SurefireJavaParser(mock(ResourcePerspectives.class), mock(JavaResourceLocator.class));
    parser.collect(context, getDirs("resourceNotFound"), true);
    assertThat(logTester.logs(LoggerLevel.WARN)).isEmpty();
    assertThat(logTester.logs(LoggerLevel.DEBUG)).contains("Resource not found: org.sonar.Foo");
}
Also used : SensorContextTester(org.sonar.api.batch.sensor.internal.SensorContextTester) Test(org.junit.Test)

Example 8 with SensorContextTester

use of org.sonar.api.batch.sensor.internal.SensorContextTester in project sonar-java by SonarSource.

the class SurefireJavaParserTest method shouldAggregateReports.

@Test
public void shouldAggregateReports() throws URISyntaxException {
    SensorContextTester context = mockContext();
    parser.collect(context, getDirs("multipleReports"), 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 9 with SensorContextTester

use of org.sonar.api.batch.sensor.internal.SensorContextTester in project sonar-java by SonarSource.

the class SurefireJavaParserTest method shouldMergeNestedInnerClasses.

@Test
public void shouldMergeNestedInnerClasses() throws URISyntaxException {
    SensorContextTester context = mockContext();
    parser.collect(context, getDirs("nestedInnerClasses"), true);
    assertThat(context.measure(":org.sonar.plugins.surefire.NestedInnerTest", CoreMetrics.TESTS).value()).isEqualTo(3);
}
Also used : SensorContextTester(org.sonar.api.batch.sensor.internal.SensorContextTester) Test(org.junit.Test)

Example 10 with SensorContextTester

use of org.sonar.api.batch.sensor.internal.SensorContextTester in project sonar-java by SonarSource.

the class SyntaxHighlighterVisitorTest method parse_error.

@Test
public void parse_error() throws Exception {
    SensorContextTester spy = spy(context);
    File file = temp.newFile().getAbsoluteFile();
    Files.write("ParseError", file, StandardCharsets.UTF_8);
    fs.add(new TestInputFileBuilder("", file.getName()).build());
    scan(file);
    verify(spy, never()).newHighlighting();
}
Also used : TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) SensorContextTester(org.sonar.api.batch.sensor.internal.SensorContextTester) 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