use of org.sonar.api.batch.sensor.internal.SensorContextTester in project sonar-java by SonarSource.
the class SonarComponentsTest method feedback_should_not_be_sent_in_sonarLintContext_or_when_collecting_is_disabled_or_when_no_errors.
@Test
public void feedback_should_not_be_sent_in_sonarLintContext_or_when_collecting_is_disabled_or_when_no_errors() {
File file = new File("src/test/files/ParseError.java");
SensorContextTester sensorContext = SensorContextTester.create(file.getParentFile().getAbsoluteFile());
Measure<String> feedback = analysisWithAnError(sensorContext);
assertThat(feedback).isNull();
sensorContext = SensorContextTester.create(file.getParentFile().getAbsoluteFile());
sensorContext.settings().setProperty(SonarComponents.COLLECT_ANALYSIS_ERRORS_KEY, true);
sensorContext.setRuntime(SonarRuntimeImpl.forSonarLint(Version.create(6, 7)));
feedback = analysisWithAnError(sensorContext);
assertThat(feedback).isNull();
// analysis with no error
sensorContext = SensorContextTester.create(file.getParentFile().getAbsoluteFile());
SonarComponents sonarComponents = new SonarComponents(null, null, null, null, null);
sonarComponents.setSensorContext(sensorContext);
sonarComponents.saveAnalysisErrors();
assertThat(sensorContext.measure("projectKey", "sonarjava_feedback")).isNull();
}
use of org.sonar.api.batch.sensor.internal.SensorContextTester in project sonar-java by SonarSource.
the class SonarComponentsTest method test_sonar_components.
@Test
public void test_sonar_components() {
SensorContextTester sensorContextTester = spy(SensorContextTester.create(new File("")));
DefaultFileSystem fs = sensorContextTester.fileSystem();
JavaTestClasspath javaTestClasspath = mock(JavaTestClasspath.class);
ImmutableList<File> javaTestClasspathList = ImmutableList.of();
when(javaTestClasspath.getElements()).thenReturn(javaTestClasspathList);
File file = new File("foo.java");
fs.add(new TestInputFileBuilder("", "foo.java").build());
FileLinesContext fileLinesContext = mock(FileLinesContext.class);
when(fileLinesContextFactory.createFor(any(InputFile.class))).thenReturn(fileLinesContext);
SonarComponents sonarComponents = new SonarComponents(fileLinesContextFactory, fs, null, javaTestClasspath, checkFactory);
sonarComponents.setSensorContext(sensorContextTester);
JavaCheck[] visitors = sonarComponents.checkClasses();
assertThat(visitors).hasSize(0);
Collection<JavaCheck> testChecks = sonarComponents.testCheckClasses();
assertThat(testChecks).hasSize(0);
assertThat(sonarComponents.getFileSystem()).isEqualTo(fs);
assertThat(sonarComponents.getJavaClasspath()).isEmpty();
assertThat(sonarComponents.getJavaTestClasspath()).isEqualTo(javaTestClasspathList);
NewHighlighting newHighlighting = sonarComponents.highlightableFor(file);
assertThat(newHighlighting).isNotNull();
verify(sensorContextTester, times(1)).newHighlighting();
NewSymbolTable newSymbolTable = sonarComponents.symbolizableFor(file);
assertThat(newSymbolTable).isNotNull();
verify(sensorContextTester, times(1)).newSymbolTable();
assertThat(sonarComponents.fileLinesContextFor(file)).isEqualTo(fileLinesContext);
JavaClasspath javaClasspath = mock(JavaClasspath.class);
List<File> list = mock(List.class);
when(javaClasspath.getElements()).thenReturn(list);
sonarComponents = new SonarComponents(fileLinesContextFactory, fs, javaClasspath, javaTestClasspath, checkFactory);
assertThat(sonarComponents.getJavaClasspath()).isEqualTo(list);
}
use of org.sonar.api.batch.sensor.internal.SensorContextTester in project sonar-java by SonarSource.
the class SonarComponentsTest method fail_on_empty_location.
@Test
public void fail_on_empty_location() {
JavaCheck expectedCheck = new CustomCheck();
CheckRegistrar expectedRegistrar = getRegistrar(expectedCheck);
RuleKey ruleKey = RuleKey.of("MyRepo", "CustomCheck");
File file = new File("file.java");
DefaultInputFile inputFile = new TestInputFileBuilder("", file.getPath()).initMetadata("class A {\n" + " void foo() {\n" + " System.out.println();\n" + " }\n" + "}\n").build();
SensorContextTester context = SensorContextTester.create(new File(""));
DefaultFileSystem fileSystem = context.fileSystem();
SonarComponents sonarComponents = new SonarComponents(fileLinesContextFactory, fileSystem, null, null, checkFactory, new CheckRegistrar[] { expectedRegistrar });
sonarComponents.setSensorContext(context);
AnalyzerMessage.TextSpan emptyTextSpan = new AnalyzerMessage.TextSpan(3, 10, 3, 10);
AnalyzerMessage analyzerMessageEmptyLocation = new AnalyzerMessage(expectedCheck, file, emptyTextSpan, "message", 0);
assertThatThrownBy(() -> sonarComponents.reportIssue(analyzerMessageEmptyLocation, ruleKey, inputFile, 0.0)).isInstanceOf(IllegalStateException.class).hasMessageContaining("Issue location should not be empty");
assertThat(context.allIssues()).isEmpty();
AnalyzerMessage.TextSpan nonEmptyTextSpan = new AnalyzerMessage.TextSpan(3, 10, 3, 15);
AnalyzerMessage analyzerMessageValidLocation = new AnalyzerMessage(expectedCheck, file, nonEmptyTextSpan, "message", 0);
sonarComponents.reportIssue(analyzerMessageValidLocation, ruleKey, inputFile, 0.0);
assertThat(context.allIssues()).isNotEmpty();
}
use of org.sonar.api.batch.sensor.internal.SensorContextTester in project sonar-go by SonarSource.
the class GoCoverageReportTest method get_report_paths.
@Test
public void get_report_paths() {
SensorContextTester context = SensorContextTester.create(COVERAGE_DIR);
context.setSettings(new MapSettings());
Path coverageFile1 = COVERAGE_DIR.resolve("coverage.linux.relative.out").toAbsolutePath();
context.settings().setProperty("sonar.go.coverage.reportPaths", coverageFile1 + ",coverage.linux.absolute.out");
List<Path> reportPaths = GoCoverageReport.getReportPaths(context);
assertThat(reportPaths).containsExactlyInAnyOrder(coverageFile1, Paths.get("src", "test", "resources", "coverage", "coverage.linux.absolute.out"));
}
use of org.sonar.api.batch.sensor.internal.SensorContextTester in project sonarqube by SonarSource.
the class OneIssuePerLineSensorTest method testProvideGap.
@Test
public void testProvideGap() throws IOException {
DefaultInputFile inputFile = new TestInputFileBuilder("foo", "src/Foo.xoo").setLanguage(Xoo.KEY).initMetadata("a\nb\nc\nd\ne\nf\ng\nh\ni\n").build();
SensorContextTester context = SensorContextTester.create(temp.newFolder());
context.fileSystem().add(inputFile);
context.setSettings(new MapSettings().setProperty(OneIssuePerLineSensor.EFFORT_TO_FIX_PROPERTY, "1.2"));
sensor.execute(context);
// One issue per line
assertThat(context.allIssues()).hasSize(10);
for (Issue issue : context.allIssues()) {
assertThat(issue.gap()).isEqualTo(1.2d);
}
}
Aggregations