use of org.sonar.plugins.java.api.CheckRegistrar in project sonar-java by SonarSource.
the class SonarComponentsTest method creation_of_custom_test_checks.
@Test
public void creation_of_custom_test_checks() {
JavaCheck expectedCheck = new CustomTestCheck();
CheckRegistrar expectedRegistrar = getRegistrar(expectedCheck);
when(checks.all()).thenReturn(new ArrayList<>()).thenReturn(Lists.newArrayList(expectedCheck));
SonarComponents sonarComponents = new SonarComponents(fileLinesContextFactory, null, null, null, checkFactory, new CheckRegistrar[] { expectedRegistrar });
sonarComponents.setSensorContext(context);
JavaCheck[] visitors = sonarComponents.checkClasses();
assertThat(visitors).hasSize(0);
Collection<JavaCheck> testChecks = sonarComponents.testCheckClasses();
assertThat(testChecks).hasSize(1);
assertThat(testChecks.iterator().next()).isEqualTo(expectedCheck);
postTestExecutionChecks();
}
use of org.sonar.plugins.java.api.CheckRegistrar 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();
}
Aggregations