Search in sources :

Example 31 with SensorContextTester

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

the class SonarComponentsTest method io_error_when_reading_file_should_fail_analysis.

@Test
public void io_error_when_reading_file_should_fail_analysis() {
    SensorContextTester context = SensorContextTester.create(new File(""));
    DefaultFileSystem fileSystem = context.fileSystem();
    fileSystem.add(new TestInputFileBuilder("", "unknown_file.java").setCharset(StandardCharsets.UTF_8).build());
    context.setRuntime(SonarRuntimeImpl.forSonarLint(V6_7));
    SonarComponents sonarComponents = new SonarComponents(null, fileSystem, null, null, null, null);
    sonarComponents.setSensorContext(context);
    File unknownFile = new File("unknown_file.java");
    try {
        sonarComponents.fileContent(unknownFile);
        fail("reading file content should have failed");
    } catch (AnalysisException e) {
        assertThat(e).hasMessage("Unable to read file unknown_file.java").hasCauseInstanceOf(NoSuchFileException.class);
    } catch (Exception e) {
        fail("reading file content should have failed", e);
    }
    try {
        sonarComponents.fileLines(unknownFile);
        fail("reading file lines should have failed");
    } catch (AnalysisException e) {
        assertThat(e).hasMessage("Unable to read file unknown_file.java").hasCauseInstanceOf(NoSuchFileException.class);
    } catch (Exception e) {
        fail("reading file content should have failed");
    }
}
Also used : TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) SensorContextTester(org.sonar.api.batch.sensor.internal.SensorContextTester) NoSuchFileException(java.nio.file.NoSuchFileException) InputFile(org.sonar.api.batch.fs.InputFile) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) File(java.io.File) DefaultFileSystem(org.sonar.api.batch.fs.internal.DefaultFileSystem) NoSuchFileException(java.nio.file.NoSuchFileException) RecognitionException(com.sonar.sslr.api.RecognitionException) LexerException(com.sonar.sslr.impl.LexerException) Test(org.junit.Test)

Example 32 with SensorContextTester

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

the class JavaSquidSensorTest method feedbackShouldNotBeFedIfNotSonarCloudHost.

@Test
public void feedbackShouldNotBeFedIfNotSonarCloudHost() throws IOException {
    SensorContextTester context = createParseErrorContext();
    executeJavaSquidSensor(context);
    assertThat(context.<String>measure("projectKey", "sonarjava_feedback")).isNull();
}
Also used : SensorContextTester(org.sonar.api.batch.sensor.internal.SensorContextTester) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.Test)

Example 33 with SensorContextTester

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

the class JavaSquidSensorTest method feedbackShouldNotBeFedIfNoErrors.

@Test
public void feedbackShouldNotBeFedIfNoErrors() throws IOException {
    SensorContextTester context = createContext(InputFile.Type.MAIN);
    context.settings().setProperty(SonarComponents.COLLECT_ANALYSIS_ERRORS_KEY, true);
    executeJavaSquidSensor(context);
    assertThat(context.<String>measure("projectKey", "sonarjava_feedback")).isNull();
}
Also used : SensorContextTester(org.sonar.api.batch.sensor.internal.SensorContextTester) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.Test)

Example 34 with SensorContextTester

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

the class XmlFileSensorTest method test_issues_creation.

@Test
public void test_issues_creation() throws Exception {
    SensorContextTester context = SensorContextTester.create(new File("src/test/files/maven/").getAbsoluteFile());
    DefaultFileSystem fs = context.fileSystem();
    final File file = new File("src/test/files/maven/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, times(1)).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 35 with SensorContextTester

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

the class XmlFileSensorTest method not_executed_without_xml_files_in_file_system.

@Test
public void not_executed_without_xml_files_in_file_system() throws Exception {
    SensorContextTester context = SensorContextTester.create(new File("src/test/files/maven/"));
    DefaultFileSystem fs = context.fileSystem();
    SonarComponents sonarComponents = createSonarComponentsMock(fs);
    XmlFileSensor sensor = new XmlFileSensor(sonarComponents, fs);
    sensor.execute(context);
    verify(sonarComponents, Mockito.never()).reportIssue(any());
}
Also used : SonarComponents(org.sonar.java.SonarComponents) SensorContextTester(org.sonar.api.batch.sensor.internal.SensorContextTester) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) File(java.io.File) DefaultFileSystem(org.sonar.api.batch.fs.internal.DefaultFileSystem) 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