Search in sources :

Example 1 with Location

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

the class DefaultJavaFileScannerContextTest method report_issue_on_tree_with_secondary.

@Test
public 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, 1, 6, 1, 7);
    List<AnalyzerMessage> secondaries = reportedMessage.flows.stream().map(flow -> flow.get(0)).collect(Collectors.toList());
    assertThat(secondaries).hasSize(2);
    assertMessagePosition(secondaries.get(0), 2, 2, 2, 13);
    assertMessagePosition(secondaries.get(1), 3, 2, 3, 15);
}
Also used : JavaParser(org.sonar.java.ast.parser.JavaParser) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) AnalyzerMessage(org.sonar.java.AnalyzerMessage) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Test(org.junit.Test) CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) Tree(org.sonar.plugins.java.api.tree.Tree) JavaFileScannerContext(org.sonar.plugins.java.api.JavaFileScannerContext) Collectors(java.util.stream.Collectors) File(java.io.File) ArrayList(java.util.ArrayList) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) List(java.util.List) Location(org.sonar.plugins.java.api.JavaFileScannerContext.Location) TextSpan(org.sonar.java.AnalyzerMessage.TextSpan) Mockito.doAnswer(org.mockito.Mockito.doAnswer) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) JavaCheck(org.sonar.plugins.java.api.JavaCheck) SonarComponents(org.sonar.java.SonarComponents) Before(org.junit.Before) 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) CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) Tree(org.sonar.plugins.java.api.tree.Tree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) AnalyzerMessage(org.sonar.java.AnalyzerMessage) Location(org.sonar.plugins.java.api.JavaFileScannerContext.Location) Test(org.junit.Test)

Aggregations

File (java.io.File)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Collectors (java.util.stream.Collectors)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1 Before (org.junit.Before)1 Test (org.junit.Test)1 ArgumentMatchers.any (org.mockito.ArgumentMatchers.any)1 Mockito.doAnswer (org.mockito.Mockito.doAnswer)1 Mockito.mock (org.mockito.Mockito.mock)1 AnalyzerMessage (org.sonar.java.AnalyzerMessage)1 TextSpan (org.sonar.java.AnalyzerMessage.TextSpan)1 SonarComponents (org.sonar.java.SonarComponents)1 JavaParser (org.sonar.java.ast.parser.JavaParser)1 JavaCheck (org.sonar.plugins.java.api.JavaCheck)1 JavaFileScannerContext (org.sonar.plugins.java.api.JavaFileScannerContext)1 Location (org.sonar.plugins.java.api.JavaFileScannerContext.Location)1 ClassTree (org.sonar.plugins.java.api.tree.ClassTree)1 CompilationUnitTree (org.sonar.plugins.java.api.tree.CompilationUnitTree)1 Tree (org.sonar.plugins.java.api.tree.Tree)1