Search in sources :

Example 11 with AnalyzerMessage

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

the class XmlCheckContextImplTest method should_not_report_unknown_secondaries.

@Test
public void should_not_report_unknown_secondaries() throws Exception {
    // manual parsing
    Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(context.getFile());
    Node node = firstNode(context, "//test2");
    int nodeLine = XmlCheckUtils.nodeLine(node);
    // uses document with recorded lines
    Node child = Iterables.get(context.evaluate(context.compile("//test2/item"), doc), 0);
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            AnalyzerMessage analyzerMessage = (AnalyzerMessage) invocation.getArguments()[0];
            reportedMessage = "onNode:" + analyzerMessage.getMessage() + "(" + analyzerMessage.getLine() + ")";
            for (AnalyzerMessage secondary : analyzerMessage.flows.stream().map(l -> l.get(0)).collect(Collectors.toList())) {
                reportedMessage += ";onChild:" + secondary.getMessage() + "(" + secondary.getLine() + ")";
            }
            return null;
        }
    }).when(sonarComponents).reportIssue(any(AnalyzerMessage.class));
    context.reportIssue(CHECK, node, "message1", Lists.newArrayList(new XmlCheckContext.XmlDocumentLocation("message2", child)));
    assertThat(reportedMessage).isEqualTo("onNode:message1(" + nodeLine + ")");
}
Also used : InvocationOnMock(org.mockito.invocation.InvocationOnMock) Node(org.w3c.dom.Node) AnalyzerMessage(org.sonar.java.AnalyzerMessage) Document(org.w3c.dom.Document) Test(org.junit.Test)

Example 12 with AnalyzerMessage

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

the class XmlCheckContextImpl method getSecondaryAnalyzerMessage.

@CheckForNull
private AnalyzerMessage getSecondaryAnalyzerMessage(JavaCheck check, File file, XmlDocumentLocation location) {
    Integer startLine = nodeLine(location.node);
    if (startLine == null) {
        return null;
    }
    String line = sonarComponents.fileLines(file).get(startLine - 1);
    TextSpan ts = new TextSpan(startLine, 0, startLine, line.length());
    return new AnalyzerMessage(check, file, ts, location.msg, 0);
}
Also used : TextSpan(org.sonar.java.AnalyzerMessage.TextSpan) AnalyzerMessage(org.sonar.java.AnalyzerMessage) CheckForNull(javax.annotation.CheckForNull)

Example 13 with AnalyzerMessage

use of org.sonar.java.AnalyzerMessage 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)

Example 14 with AnalyzerMessage

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

the class PomCheckContextImplTest method createSonarComponentsMock.

private static SonarComponents createSonarComponentsMock() {
    SonarComponents sonarComponents = mock(SonarComponents.class);
    Mockito.when(sonarComponents.fileLines(any(File.class))).thenReturn(Arrays.asList("line 1", "line 2", "line 3 is longer"));
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            reportedMessage = "onLine:" + invocation.getArguments()[3];
            return null;
        }
    }).when(sonarComponents).addIssue(any(File.class), eq(CHECK), eq(LINE), anyString(), eq(null));
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            reportedMessage = "onFile:" + invocation.getArguments()[3];
            return null;
        }
    }).when(sonarComponents).addIssue(any(File.class), eq(CHECK), eq(-1), anyString(), eq(null));
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            AnalyzerMessage analyzerMessage = (AnalyzerMessage) invocation.getArguments()[0];
            reportedMessage = "analyzerMessage:" + analyzerMessage.getMessage();
            for (AnalyzerMessage secondary : analyzerMessage.flows.stream().map(l -> l.get(0)).collect(Collectors.toList())) {
                TextSpan location = secondary.primaryLocation();
                reportedMessage += ";" + secondary.getMessage() + "[" + location.startLine + ";" + location.startCharacter + "/" + location.endLine + ";" + location.endCharacter + "]";
            }
            return null;
        }
    }).when(sonarComponents).reportIssue(any(AnalyzerMessage.class));
    return sonarComponents;
}
Also used : TextSpan(org.sonar.java.AnalyzerMessage.TextSpan) SonarComponents(org.sonar.java.SonarComponents) InvocationOnMock(org.mockito.invocation.InvocationOnMock) AnalyzerMessage(org.sonar.java.AnalyzerMessage) File(java.io.File)

Example 15 with AnalyzerMessage

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

the class JavaCheckVerifierTest method rule_metadata_unknown_remediation_function.

@Test
public void rule_metadata_unknown_remediation_function() {
    @Rule(key = "ExponentialRemediationFunc")
    class WrongJson extends FakeVisitor {
    }
    WrongJson check = new WrongJson();
    check.withPreciseIssue(new AnalyzerMessage(check, new File("a"), 1, "message", 0));
    JavaCheckVerifier.verify("src/test/files/JavaCheckVerifierNoCost.java", check);
}
Also used : AnalyzerMessage(org.sonar.java.AnalyzerMessage) Rule(org.sonar.check.Rule) File(java.io.File) Test(org.junit.Test)

Aggregations

AnalyzerMessage (org.sonar.java.AnalyzerMessage)23 File (java.io.File)13 Test (org.junit.Test)11 Rule (org.sonar.check.Rule)6 SonarComponents (org.sonar.java.SonarComponents)6 InvocationOnMock (org.mockito.invocation.InvocationOnMock)4 TextSpan (org.sonar.java.AnalyzerMessage.TextSpan)4 List (java.util.List)3 Collectors (java.util.stream.Collectors)3 VisitorsBridgeForTests (org.sonar.java.model.VisitorsBridgeForTests)3 JavaCheck (org.sonar.plugins.java.api.JavaCheck)3 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 ArrayList (java.util.ArrayList)2 Collections (java.util.Collections)2 CheckForNull (javax.annotation.CheckForNull)2 Nullable (javax.annotation.Nullable)2 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)2 Node (org.w3c.dom.Node)2 Preconditions (com.google.common.base.Preconditions)1 Splitter (com.google.common.base.Splitter)1