Search in sources :

Example 1 with CheckRegistrar

use of org.sonar.plugins.java.api.CheckRegistrar 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();
}
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) CheckRegistrar(org.sonar.plugins.java.api.CheckRegistrar) LexerException(com.sonar.sslr.impl.LexerException) 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) RecognitionException(com.sonar.sslr.api.RecognitionException) Test(org.junit.Test)

Example 2 with CheckRegistrar

use of org.sonar.plugins.java.api.CheckRegistrar in project sonar-java by SonarSource.

the class SonarComponentsTest method creation_of_custom_checks.

@Test
public void creation_of_custom_checks() {
    JavaCheck expectedCheck = new CustomCheck();
    CheckRegistrar expectedRegistrar = getRegistrar(expectedCheck);
    when(this.checks.all()).thenReturn(Lists.newArrayList(expectedCheck)).thenReturn(new ArrayList<>());
    SonarComponents sonarComponents = new SonarComponents(this.fileLinesContextFactory, null, null, null, this.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(0);
    postTestExecutionChecks();
}
Also used : JavaCheck(org.sonar.plugins.java.api.JavaCheck) CheckRegistrar(org.sonar.plugins.java.api.CheckRegistrar) Test(org.junit.Test)

Example 3 with CheckRegistrar

use of org.sonar.plugins.java.api.CheckRegistrar in project sonar-java by SonarSource.

the class SonarComponentsTest method no_issue_if_file_not_found.

@Test
public void no_issue_if_file_not_found() throws Exception {
    JavaCheck expectedCheck = new CustomCheck();
    CheckRegistrar expectedRegistrar = getRegistrar(expectedCheck);
    DefaultFileSystem fileSystem = new DefaultFileSystem(new File(""));
    File file = new File("file.java");
    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, 0, "message", null);
}
Also used : RuleKey(org.sonar.api.rule.RuleKey) JavaCheck(org.sonar.plugins.java.api.JavaCheck) 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)

Example 4 with CheckRegistrar

use of org.sonar.plugins.java.api.CheckRegistrar 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 5 with CheckRegistrar

use of org.sonar.plugins.java.api.CheckRegistrar 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)

Aggregations

Test (org.junit.Test)7 CheckRegistrar (org.sonar.plugins.java.api.CheckRegistrar)7 JavaCheck (org.sonar.plugins.java.api.JavaCheck)7 File (java.io.File)5 InputFile (org.sonar.api.batch.fs.InputFile)5 DefaultInputFile (org.sonar.api.batch.fs.internal.DefaultInputFile)5 DefaultFileSystem (org.sonar.api.batch.fs.internal.DefaultFileSystem)4 RuleKey (org.sonar.api.rule.RuleKey)4 TestInputFileBuilder (org.sonar.api.batch.fs.internal.TestInputFileBuilder)3 SensorContextTester (org.sonar.api.batch.sensor.internal.SensorContextTester)3 RecognitionException (com.sonar.sslr.api.RecognitionException)2 LexerException (com.sonar.sslr.impl.LexerException)2 ArrayList (java.util.ArrayList)2 Issuable (org.sonar.api.issue.Issuable)2 Issue (org.sonar.api.issue.Issue)2 ImmutableList (com.google.common.collect.ImmutableList)1 Lists (com.google.common.collect.Lists)1 Gson (com.google.gson.Gson)1 TypeToken (com.google.gson.reflect.TypeToken)1 JavaRules (com.sonar.plugins.security.api.JavaRules)1