use of org.sonar.plugins.java.api.JavaCheck in project sonar-java by SonarSource.
the class SonarComponentsTest method no_issue_when_check_not_found.
@Test
public void no_issue_when_check_not_found() throws Exception {
JavaCheck expectedCheck = new CustomCheck();
CheckRegistrar expectedRegistrar = getRegistrar(expectedCheck);
Issuable issuable = mock(Issuable.class);
when(this.checks.ruleKey(any(JavaCheck.class))).thenReturn(null);
SonarComponents sonarComponents = new SonarComponents(fileLinesContextFactory, null, null, null, checkFactory, new CheckRegistrar[] { expectedRegistrar });
sonarComponents.setSensorContext(context);
sonarComponents.addIssue(new File(""), expectedCheck, 0, "message", null);
verify(issuable, never()).addIssue(any(Issue.class));
}
use of org.sonar.plugins.java.api.JavaCheck in project sonar-java by SonarSource.
the class SonarComponentsTest method creation_of_both_types_test_checks.
@Test
public void creation_of_both_types_test_checks() {
JavaCheck expectedCheck = new CustomCheck();
JavaCheck expectedTestCheck = new CustomTestCheck();
CheckRegistrar expectedRegistrar = registrarContext -> registrarContext.registerClassesForRepository(REPOSITORY_NAME, Lists.<Class<? extends JavaCheck>>newArrayList(CustomCheck.class), Lists.<Class<? extends JavaCheck>>newArrayList(CustomTestCheck.class));
when(this.checks.all()).thenReturn(Lists.newArrayList(expectedCheck)).thenReturn(Lists.newArrayList(expectedTestCheck));
SonarComponents sonarComponents = new SonarComponents(fileLinesContextFactory, null, null, null, checkFactory, new CheckRegistrar[] { expectedRegistrar });
sonarComponents.setSensorContext(context);
JavaCheck[] visitors = sonarComponents.checkClasses();
assertThat(visitors).hasSize(1);
assertThat(visitors[0]).isEqualTo(expectedCheck);
Collection<JavaCheck> testChecks = sonarComponents.testCheckClasses();
assertThat(testChecks).hasSize(1);
assertThat(testChecks.iterator().next()).isEqualTo(expectedTestCheck);
assertThat(sonarComponents.checks()).hasSize(2);
postTestExecutionChecks();
}
use of org.sonar.plugins.java.api.JavaCheck 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.JavaCheck 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.plugins.java.api.JavaCheck 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