Search in sources :

Example 11 with JavaCheck

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));
}
Also used : Issue(org.sonar.api.issue.Issue) JavaCheck(org.sonar.plugins.java.api.JavaCheck) Issuable(org.sonar.api.issue.Issuable) CheckRegistrar(org.sonar.plugins.java.api.CheckRegistrar) InputFile(org.sonar.api.batch.fs.InputFile) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) File(java.io.File) Test(org.junit.Test)

Example 12 with JavaCheck

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();
}
Also used : NoSuchFileException(java.nio.file.NoSuchFileException) Arrays(java.util.Arrays) DefaultFileSystem(org.sonar.api.batch.fs.internal.DefaultFileSystem) TypeToken(com.google.gson.reflect.TypeToken) Measure(org.sonar.api.batch.sensor.measure.Measure) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ActiveRules(org.sonar.api.batch.rule.ActiveRules) FileLinesContext(org.sonar.api.measures.FileLinesContext) FileLinesContextFactory(org.sonar.api.measures.FileLinesContextFactory) SensorContextTester(org.sonar.api.batch.sensor.internal.SensorContextTester) Gson(com.google.gson.Gson) JavaCheck(org.sonar.plugins.java.api.JavaCheck) Checks(org.sonar.api.batch.rule.Checks) Collection(java.util.Collection) ActiveRulesBuilder(org.sonar.api.batch.rule.internal.ActiveRulesBuilder) SensorContext(org.sonar.api.batch.sensor.SensorContext) StandardCharsets(java.nio.charset.StandardCharsets) Version(org.sonar.api.utils.Version) TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) List(java.util.List) Assertions.fail(org.assertj.core.api.Assertions.fail) RuleKey(org.sonar.api.rule.RuleKey) CheckRegistrar(org.sonar.plugins.java.api.CheckRegistrar) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) InputFile(org.sonar.api.batch.fs.InputFile) Mock(org.mockito.Mock) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) RunWith(org.junit.runner.RunWith) Mockito.spy(org.mockito.Mockito.spy) ArrayList(java.util.ArrayList) Issuable(org.sonar.api.issue.Issuable) HashSet(java.util.HashSet) CheckFactory(org.sonar.api.batch.rule.CheckFactory) Lists(com.google.common.collect.Lists) ImmutableList(com.google.common.collect.ImmutableList) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) RecognitionException(com.sonar.sslr.api.RecognitionException) SonarQubeSide(org.sonar.api.SonarQubeSide) Before(org.junit.Before) NewSymbolTable(org.sonar.api.batch.sensor.symbol.NewSymbolTable) LexerException(com.sonar.sslr.impl.LexerException) JavaRules(com.sonar.plugins.security.api.JavaRules) Test(org.junit.Test) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) File(java.io.File) Mockito.verify(org.mockito.Mockito.verify) NewHighlighting(org.sonar.api.batch.sensor.highlighting.NewHighlighting) Mockito.never(org.mockito.Mockito.never) SonarRuntimeImpl(org.sonar.api.internal.SonarRuntimeImpl) Issue(org.sonar.api.issue.Issue) Mockito.reset(org.mockito.Mockito.reset) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) JavaCheck(org.sonar.plugins.java.api.JavaCheck) CheckRegistrar(org.sonar.plugins.java.api.CheckRegistrar) Test(org.junit.Test)

Example 13 with JavaCheck

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();
}
Also used : JavaCheck(org.sonar.plugins.java.api.JavaCheck) ArrayList(java.util.ArrayList) CheckRegistrar(org.sonar.plugins.java.api.CheckRegistrar) Test(org.junit.Test)

Example 14 with JavaCheck

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);
}
Also used : SensorContextTester(org.sonar.api.batch.sensor.internal.SensorContextTester) JavaCheck(org.sonar.plugins.java.api.JavaCheck) NewHighlighting(org.sonar.api.batch.sensor.highlighting.NewHighlighting) NewSymbolTable(org.sonar.api.batch.sensor.symbol.NewSymbolTable) InputFile(org.sonar.api.batch.fs.InputFile) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) InputFile(org.sonar.api.batch.fs.InputFile) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) File(java.io.File) DefaultFileSystem(org.sonar.api.batch.fs.internal.DefaultFileSystem) FileLinesContext(org.sonar.api.measures.FileLinesContext) Test(org.junit.Test)

Example 15 with JavaCheck

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();
}
Also used : SensorContextTester(org.sonar.api.batch.sensor.internal.SensorContextTester) RuleKey(org.sonar.api.rule.RuleKey) JavaCheck(org.sonar.plugins.java.api.JavaCheck) TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) CheckRegistrar(org.sonar.plugins.java.api.CheckRegistrar) InputFile(org.sonar.api.batch.fs.InputFile) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) File(java.io.File) DefaultFileSystem(org.sonar.api.batch.fs.internal.DefaultFileSystem) Test(org.junit.Test)

Aggregations

JavaCheck (org.sonar.plugins.java.api.JavaCheck)21 File (java.io.File)16 Test (org.junit.Test)14 InputFile (org.sonar.api.batch.fs.InputFile)8 DefaultInputFile (org.sonar.api.batch.fs.internal.DefaultInputFile)7 CheckRegistrar (org.sonar.plugins.java.api.CheckRegistrar)7 DefaultFileSystem (org.sonar.api.batch.fs.internal.DefaultFileSystem)6 RuleKey (org.sonar.api.rule.RuleKey)6 TestInputFileBuilder (org.sonar.api.batch.fs.internal.TestInputFileBuilder)5 SensorContextTester (org.sonar.api.batch.sensor.internal.SensorContextTester)4 ImmutableList (com.google.common.collect.ImmutableList)2 RecognitionException (com.sonar.sslr.api.RecognitionException)2 LexerException (com.sonar.sslr.impl.LexerException)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 NewHighlighting (org.sonar.api.batch.sensor.highlighting.NewHighlighting)2 NewSymbolTable (org.sonar.api.batch.sensor.symbol.NewSymbolTable)2 Issuable (org.sonar.api.issue.Issuable)2 Issue (org.sonar.api.issue.Issue)2 FileLinesContext (org.sonar.api.measures.FileLinesContext)2