Search in sources :

Example 11 with AnalyzerMessage

use of org.sonar.java.reporting.AnalyzerMessage in project sonar-java by SonarSource.

the class SonarComponentsTest method add_issue_or_parse_error.

@Test
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();
    TestInputFileBuilder inputFileBuilder = new TestInputFileBuilder("", "file.java");
    inputFileBuilder.setLines(45);
    int[] lineStartOffsets = new int[45];
    lineStartOffsets[35] = 12;
    lineStartOffsets[42] = 1;
    int lastValidOffset = 420;
    inputFileBuilder.setOriginalLineStartOffsets(lineStartOffsets);
    inputFileBuilder.setOriginalLineEndOffsets(computeLineEndOffsets(lineStartOffsets, lastValidOffset));
    inputFileBuilder.setLastValidOffset(lastValidOffset);
    InputFile inputFile = inputFileBuilder.build();
    fileSystem.add(inputFile);
    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(inputFile, expectedCheck, -5, "message on wrong line", null);
    sonarComponents.addIssue(inputFile, expectedCheck, 42, "message on line 42", 1);
    sonarComponents.reportIssue(new AnalyzerMessage(expectedCheck, inputFile, 35, "other message", 0));
    List<Issue> issues = new ArrayList<>(context.allIssues());
    assertThat(issues).hasSize(3);
    assertThat(issues.get(0).primaryLocation().message()).isEqualTo("message on wrong line");
    assertThat(issues.get(1).primaryLocation().message()).isEqualTo("message on line 42");
    assertThat(issues.get(2).primaryLocation().message()).isEqualTo("other message");
    RecognitionException parseError = new RecognitionException(-1, "invalid code", new Exception("parse error"));
    context.setRuntime(SonarRuntimeImpl.forSonarLint(V8_9));
    assertThat(sonarComponents.reportAnalysisError(parseError, inputFile)).isTrue();
    context.setRuntime(SonarRuntimeImpl.forSonarQube(V8_9, SonarQubeSide.SCANNER, SonarEdition.COMMUNITY));
    assertThat(sonarComponents.reportAnalysisError(parseError, inputFile)).isFalse();
}
Also used : Issue(org.sonar.api.batch.sensor.issue.Issue) SensorContextTester(org.sonar.api.batch.sensor.internal.SensorContextTester) RuleKey(org.sonar.api.rule.RuleKey) JavaCheck(org.sonar.plugins.java.api.JavaCheck) ArrayList(java.util.ArrayList) NoSuchFileException(java.nio.file.NoSuchFileException) RecognitionException(com.sonar.sslr.api.RecognitionException) ApiMismatchException(org.sonar.java.exceptions.ApiMismatchException) InputFile(org.sonar.api.batch.fs.InputFile) TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) CheckRegistrar(org.sonar.plugins.java.api.CheckRegistrar) AnalyzerMessage(org.sonar.java.reporting.AnalyzerMessage) InputFile(org.sonar.api.batch.fs.InputFile) File(java.io.File) DefaultFileSystem(org.sonar.api.batch.fs.internal.DefaultFileSystem) RecognitionException(com.sonar.sslr.api.RecognitionException) Test(org.junit.jupiter.api.Test) ClasspathForTest(org.sonar.java.classpath.ClasspathForTest) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 12 with AnalyzerMessage

use of org.sonar.java.reporting.AnalyzerMessage in project sonar-java by SonarSource.

the class SonarComponentsTest method fail_on_empty_location.

@Test
void fail_on_empty_location() {
    JavaCheck expectedCheck = new CustomCheck();
    CheckRegistrar expectedRegistrar = getRegistrar(expectedCheck);
    RuleKey ruleKey = RuleKey.of("MyRepo", "CustomCheck");
    InputFile inputFile = new TestInputFileBuilder("", "file.java").initMetadata("class A {\n" + "  void foo() {\n" + "    System.out.println();\n" + "  }\n" + "}\n").build();
    SensorContextTester context = SensorContextTester.create(new File(""));
    SonarComponents sonarComponents = new SonarComponents(fileLinesContextFactory, context.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, inputFile, 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, inputFile, 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) InputFile(org.sonar.api.batch.fs.InputFile) TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) CheckRegistrar(org.sonar.plugins.java.api.CheckRegistrar) AnalyzerMessage(org.sonar.java.reporting.AnalyzerMessage) InputFile(org.sonar.api.batch.fs.InputFile) File(java.io.File) Test(org.junit.jupiter.api.Test) ClasspathForTest(org.sonar.java.classpath.ClasspathForTest) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 13 with AnalyzerMessage

use of org.sonar.java.reporting.AnalyzerMessage in project sonar-java by SonarSource.

the class DefaultJavaFileScannerContextTest method report_issue_on_regex_tree_with_secondary.

@Test
void report_issue_on_regex_tree_with_secondary() {
    RegexCheck regexCheck = new RegexCheck() {
    };
    String regex = "x{42}|y{23}";
    RegexTree regexTree = RegexParserTestUtils.assertSuccessfulParse(regex);
    DisjunctionTree disjunctionTree = (DisjunctionTree) regexTree;
    RepetitionTree x42 = (RepetitionTree) disjunctionTree.getAlternatives().get(0);
    CurlyBraceQuantifier rep42 = (CurlyBraceQuantifier) x42.getQuantifier();
    RepetitionTree y23 = (RepetitionTree) disjunctionTree.getAlternatives().get(1);
    CurlyBraceQuantifier rep23 = (CurlyBraceQuantifier) y23.getQuantifier();
    RegexCheck.RegexIssueLocation secondary = new RegexCheck.RegexIssueLocation(rep42, "regexSecondary");
    context.reportIssue(regexCheck, rep23, "regexMsg", null, Collections.singletonList(secondary));
    assertThat(reportedMessage.getMessage()).isEqualTo("regexMsg");
    assertThat(reportedMessage.getCost()).isNull();
    assertMessagePosition(reportedMessage, 3, 8, 3, 12);
    assertThat(reportedMessage.flows).hasSize(1);
    List<AnalyzerMessage> reportedSecondaries = reportedMessage.flows.get(0);
    assertThat(reportedSecondaries).hasSize(1);
    AnalyzerMessage reportedSecondary = reportedSecondaries.get(0);
    assertThat(reportedSecondary.getMessage()).isEqualTo("regexSecondary");
    assertThat(reportedSecondary.getCost()).isNull();
    assertMessagePosition(reportedSecondary, 3, 2, 3, 6);
}
Also used : RepetitionTree(org.sonarsource.analyzer.commons.regex.ast.RepetitionTree) RegexTree(org.sonarsource.analyzer.commons.regex.ast.RegexTree) AnalyzerMessage(org.sonar.java.reporting.AnalyzerMessage) RegexCheck(org.sonar.java.regex.RegexCheck) DisjunctionTree(org.sonarsource.analyzer.commons.regex.ast.DisjunctionTree) CurlyBraceQuantifier(org.sonarsource.analyzer.commons.regex.ast.CurlyBraceQuantifier) Test(org.junit.jupiter.api.Test)

Example 14 with AnalyzerMessage

use of org.sonar.java.reporting.AnalyzerMessage in project sonar-java by SonarSource.

the class DefaultInputFileScannerContextTest method createSonarComponentsMock.

private SonarComponents createSonarComponentsMock() {
    SonarComponents sonarComponents = mock(SonarComponents.class);
    doAnswer(invocation -> {
        reportedMessage = (AnalyzerMessage) invocation.getArguments()[0];
        return null;
    }).when(sonarComponents).reportIssue(any(AnalyzerMessage.class));
    doAnswer(invocation -> {
        Integer cost = invocation.getArgument(4);
        reportedMessage = new AnalyzerMessage(invocation.getArgument(1), invocation.getArgument(0), null, invocation.getArgument(3), cost != null ? cost : 0);
        return null;
    }).when(sonarComponents).addIssue(any(InputComponent.class), any(JavaCheck.class), anyInt(), anyString(), any());
    when(sonarComponents.fileLines(any(InputFile.class))).thenReturn(Arrays.asList("1st line", "2nd line"));
    when(sonarComponents.inputFileContents(any(InputFile.class))).thenReturn("content");
    when(sonarComponents.projectLevelWorkDir()).thenReturn(WORK_DIR);
    when(sonarComponents.project()).thenReturn(PROJECT_BASE_DIR);
    return sonarComponents;
}
Also used : SonarComponents(org.sonar.java.SonarComponents) InputComponent(org.sonar.api.batch.fs.InputComponent) JavaCheck(org.sonar.plugins.java.api.JavaCheck) AnalyzerMessage(org.sonar.java.reporting.AnalyzerMessage) InputFile(org.sonar.api.batch.fs.InputFile)

Example 15 with AnalyzerMessage

use of org.sonar.java.reporting.AnalyzerMessage in project sonar-java by SonarSource.

the class DefaultJavaFileScannerContextTest method report_issue_on_tree_with_secondary.

@Test
void report_issue_on_tree_with_secondary() {
    ClassTree tree = (ClassTree) compilationUnitTree.types().get(0);
    Tree firstMember = tree.members().get(0);
    Tree secondMember = tree.members().get(1);
    ArrayList<Location> secondary = new ArrayList<>();
    secondary.add(new JavaFileScannerContext.Location("secondary", firstMember));
    secondary.add(new JavaFileScannerContext.Location("secondary", secondMember));
    context.reportIssue(CHECK, tree.simpleName(), "msg", secondary, null);
    assertThat(reportedMessage.getMessage()).isEqualTo("msg");
    assertThat(reportedMessage.getCost()).isNull();
    assertThat(reportedMessage.flows).hasSize(2);
    assertMessagePosition(reportedMessage, 3, 6, 3, 7);
    List<AnalyzerMessage> secondaries = reportedMessage.flows.stream().map(flow -> flow.get(0)).collect(Collectors.toList());
    assertThat(secondaries).hasSize(2);
    assertMessagePosition(secondaries.get(0), 4, 2, 4, 13);
    assertMessagePosition(secondaries.get(1), 5, 2, 5, 15);
}
Also used : FluentReporting(org.sonar.java.reporting.FluentReporting) RegexTree(org.sonarsource.analyzer.commons.regex.ast.RegexTree) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ArrayList(java.util.ArrayList) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) RepetitionTree(org.sonarsource.analyzer.commons.regex.ast.RepetitionTree) AnalyzerMessage(org.sonar.java.reporting.AnalyzerMessage) TextSpan(org.sonar.java.reporting.AnalyzerMessage.TextSpan) Tree(org.sonar.plugins.java.api.tree.Tree) Mockito.when(org.mockito.Mockito.when) Collectors(java.util.stream.Collectors) SensorContext(org.sonar.api.batch.sensor.SensorContext) JavaFileScannerContext(org.sonar.plugins.java.api.JavaFileScannerContext) SourceMap(org.sonar.plugins.java.api.SourceMap) RegexParserTestUtils(org.sonar.java.regex.RegexParserTestUtils) Test(org.junit.jupiter.api.Test) List(java.util.List) RegexCheck(org.sonar.java.regex.RegexCheck) Location(org.sonar.plugins.java.api.JavaFileScannerContext.Location) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) Collections(java.util.Collections) DisjunctionTree(org.sonarsource.analyzer.commons.regex.ast.DisjunctionTree) CurlyBraceQuantifier(org.sonarsource.analyzer.commons.regex.ast.CurlyBraceQuantifier) Mockito.mock(org.mockito.Mockito.mock) Location(org.sonar.plugins.java.api.JavaFileScannerContext.Location) JavaFileScannerContext(org.sonar.plugins.java.api.JavaFileScannerContext) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) ArrayList(java.util.ArrayList) RegexTree(org.sonarsource.analyzer.commons.regex.ast.RegexTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) RepetitionTree(org.sonarsource.analyzer.commons.regex.ast.RepetitionTree) Tree(org.sonar.plugins.java.api.tree.Tree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) DisjunctionTree(org.sonarsource.analyzer.commons.regex.ast.DisjunctionTree) AnalyzerMessage(org.sonar.java.reporting.AnalyzerMessage) Location(org.sonar.plugins.java.api.JavaFileScannerContext.Location) Test(org.junit.jupiter.api.Test)

Aggregations

AnalyzerMessage (org.sonar.java.reporting.AnalyzerMessage)30 Test (org.junit.jupiter.api.Test)11 InputFile (org.sonar.api.batch.fs.InputFile)10 ArrayList (java.util.ArrayList)9 List (java.util.List)8 JavaFileScannerContext (org.sonar.plugins.java.api.JavaFileScannerContext)8 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)7 JavaCheck (org.sonar.plugins.java.api.JavaCheck)7 SonarComponents (org.sonar.java.SonarComponents)6 Tree (org.sonar.plugins.java.api.tree.Tree)5 File (java.io.File)4 Collections (java.util.Collections)4 Set (java.util.Set)4 Collectors (java.util.stream.Collectors)4 VisitorsBridgeForTests (org.sonar.java.testing.VisitorsBridgeForTests)4 HashSet (java.util.HashSet)3 Optional (java.util.Optional)3 ValueSource (org.junit.jupiter.params.provider.ValueSource)3 InputComponent (org.sonar.api.batch.fs.InputComponent)3 SensorContextTester (org.sonar.api.batch.sensor.internal.SensorContextTester)3