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");
}
}
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();
}
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();
}
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())));
}
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());
}
Aggregations