use of org.sonar.api.batch.sensor.internal.SensorContextTester in project sonar-java by SonarSource.
the class SurefireSensorTest method measuresShouldNotIncludeSkippedTests.
@Test
public void measuresShouldNotIncludeSkippedTests() throws URISyntaxException {
SensorContextTester context = SensorContextTester.create(new File(""));
context.fileSystem().add(resource("org.sonar.Foo"));
collect(context, "/org/sonar/plugins/surefire/SurefireSensorTest/measuresShouldNotIncludeSkippedTests/");
assertThat(context.measure(":org.sonar.Foo", CoreMetrics.TESTS).value()).isEqualTo(2);
assertThat(context.measure(":org.sonar.Foo", CoreMetrics.TEST_FAILURES).value()).isEqualTo(1);
assertThat(context.measure(":org.sonar.Foo", CoreMetrics.TEST_ERRORS).value()).isEqualTo(0);
assertThat(context.measure(":org.sonar.Foo", CoreMetrics.SKIPPED_TESTS).value()).isEqualTo(1);
}
use of org.sonar.api.batch.sensor.internal.SensorContextTester in project sonar-java by SonarSource.
the class SurefireSensorTest method noSuccessRatioIfNoTests.
@Test
public void noSuccessRatioIfNoTests() throws URISyntaxException {
SensorContextTester context = SensorContextTester.create(new File(""));
context.fileSystem().add(resource("org.sonar.Foo"));
collect(context, "/org/sonar/plugins/surefire/SurefireSensorTest/noSuccessRatioIfNoTests/");
assertThat(context.measure(":org.sonar.Foo", CoreMetrics.TESTS).value()).isEqualTo(0);
assertThat(context.measure(":org.sonar.Foo", CoreMetrics.TEST_FAILURES).value()).isEqualTo(0);
assertThat(context.measure(":org.sonar.Foo", CoreMetrics.TEST_ERRORS).value()).isEqualTo(0);
assertThat(context.measure(":org.sonar.Foo", CoreMetrics.SKIPPED_TESTS).value()).isEqualTo(2);
}
use of org.sonar.api.batch.sensor.internal.SensorContextTester in project sonar-java by SonarSource.
the class SonarComponentsTest method add_issue_or_parse_error.
@Test
public void add_issue_or_parse_error() throws Exception {
JavaCheck expectedCheck = new CustomCheck();
CheckRegistrar expectedRegistrar = getRegistrar(expectedCheck);
SensorContextTester context = SensorContextTester.create(new File(""));
DefaultFileSystem fileSystem = context.fileSystem();
File file = new File("file.java");
TestInputFileBuilder inputFile = new TestInputFileBuilder("", "file.java");
inputFile.setLines(45);
int[] linesOffset = new int[45];
linesOffset[35] = 12;
linesOffset[42] = 1;
inputFile.setOriginalLineOffsets(linesOffset);
inputFile.setLastValidOffset(420);
fileSystem.add(inputFile.build());
when(this.checks.ruleKey(any(JavaCheck.class))).thenReturn(mock(RuleKey.class));
SonarComponents sonarComponents = new SonarComponents(fileLinesContextFactory, fileSystem, null, null, checkFactory, new CheckRegistrar[] { expectedRegistrar });
sonarComponents.setSensorContext(context);
sonarComponents.addIssue(file, expectedCheck, -5, "message on wrong line", null);
sonarComponents.addIssue(file, expectedCheck, 42, "message on line", 1);
sonarComponents.addIssue(new File("."), expectedCheck, 42, "message on line", 1);
sonarComponents.addIssue(new File("unknown_file"), expectedCheck, 42, "message on line", 1);
sonarComponents.reportIssue(new AnalyzerMessage(expectedCheck, file, 35, "other message", 0));
assertThat(context.allIssues()).hasSize(3);
RecognitionException parseError = new RecognitionException(new LexerException("parse error"));
context.setRuntime(SonarRuntimeImpl.forSonarLint(V6_7));
assertThat(sonarComponents.reportAnalysisError(parseError, file)).isTrue();
context.setRuntime(SonarRuntimeImpl.forSonarQube(V6_7, SonarQubeSide.SCANNER));
assertThat(sonarComponents.reportAnalysisError(parseError, file)).isFalse();
}
use of org.sonar.api.batch.sensor.internal.SensorContextTester in project sonar-java by SonarSource.
the class SonarComponentsTest method ucfg_activation_should_rely_on_active_rules.
@Test
public void ucfg_activation_should_rely_on_active_rules() {
File file = new File("src/test/files/ParseError.java");
SensorContextTester sensorContext = SensorContextTester.create(file.getParentFile().getAbsoluteFile());
SonarComponents sonarComponents = new SonarComponents(null, null, null, null, null);
sonarComponents.setRuleRepositoryKey("squid");
sonarComponents.setSensorContext(sensorContext);
// no security rules available
JavaRules.ruleKeys = new HashSet<>();
assertThat(sonarComponents.shouldGenerateUCFG()).isFalse();
ActiveRules activeRules = new ActiveRulesBuilder().create(RuleKey.of("squid", "S3649")).activate().build();
// one security rule available
JavaRules.ruleKeys = new HashSet<>(Arrays.asList("S3649"));
sensorContext.setActiveRules(activeRules);
assertThat(sonarComponents.shouldGenerateUCFG()).isTrue();
}
use of org.sonar.api.batch.sensor.internal.SensorContextTester in project sonar-java by SonarSource.
the class SonarComponentsTest method sonarcloud_feedback_metric_should_not_exceed_roughly_200ko.
@Test
public void sonarcloud_feedback_metric_should_not_exceed_roughly_200ko() {
File file = new File("src/test/files/ParseError.java");
SensorContextTester sensorContext = SensorContextTester.create(file.getParentFile().getAbsoluteFile());
sensorContext.settings().setProperty(SonarComponents.COLLECT_ANALYSIS_ERRORS_KEY, true);
Measure<String> feedback = analysisWithAnError(sensorContext);
Collection<AnalysisError> analysisErrorsDeserialized = new Gson().fromJson(feedback.value(), new TypeToken<Collection<AnalysisError>>() {
}.getType());
assertThat(analysisErrorsDeserialized.size()).isBetween(35, 45);
assertThat(analysisErrorsDeserialized.iterator().next().getKind()).isEqualTo(AnalysisError.Kind.PARSE_ERROR);
}
Aggregations