Search in sources :

Example 6 with AnalyzerMessage

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

the class ParsingErrorCheckTest method test.

@Test
public void test() {
    SonarComponents sonarComponents = mock(SonarComponents.class);
    when(sonarComponents.fileContent(any())).thenCallRealMethod();
    when(sonarComponents.fileLength(any())).thenCallRealMethod();
    VisitorsBridgeForTests visitorsBridge = new VisitorsBridgeForTests(new ParsingErrorCheck(), sonarComponents);
    JavaAstScanner.scanSingleFileForTests(new File("src/test/files/checks/ParsingError.java"), visitorsBridge);
    Set<AnalyzerMessage> issues = visitorsBridge.lastCreatedTestContext().getIssues();
    assertThat(issues).hasSize(1);
    AnalyzerMessage issue = issues.iterator().next();
    assertThat(issue.getLine()).isEqualTo(1);
    assertThat(issue.getMessage()).isEqualTo("Parse error");
}
Also used : SonarComponents(org.sonar.java.SonarComponents) AnalyzerMessage(org.sonar.java.AnalyzerMessage) File(java.io.File) VisitorsBridgeForTests(org.sonar.java.model.VisitorsBridgeForTests) Test(org.junit.Test)

Example 7 with AnalyzerMessage

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

the class DefaultJavaFileScannerContextTest method createSonarComponentsMock.

private static SonarComponents createSonarComponentsMock() {
    SonarComponents sonarComponents = mock(SonarComponents.class);
    doAnswer(invocation -> {
        reportedMessage = (AnalyzerMessage) invocation.getArguments()[0];
        return null;
    }).when(sonarComponents).reportIssue(any(AnalyzerMessage.class));
    return sonarComponents;
}
Also used : SonarComponents(org.sonar.java.SonarComponents) AnalyzerMessage(org.sonar.java.AnalyzerMessage)

Example 8 with AnalyzerMessage

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

the class DefaultJavaFileScannerContext method createAnalyzerMessage.

protected static AnalyzerMessage createAnalyzerMessage(File file, JavaCheck javaCheck, Tree startTree, @Nullable Tree endTree, String message, Iterable<List<Location>> flows, @Nullable Integer cost) {
    AnalyzerMessage.TextSpan textSpan = endTree != null ? AnalyzerMessage.textSpanBetween(startTree, endTree) : AnalyzerMessage.textSpanFor(startTree);
    AnalyzerMessage analyzerMessage = new AnalyzerMessage(javaCheck, file, textSpan, message, cost != null ? cost : 0);
    for (List<Location> flow : flows) {
        List<AnalyzerMessage> sonarqubeFlow = flow.stream().map(l -> new AnalyzerMessage(javaCheck, file, AnalyzerMessage.textSpanFor(l.syntaxNode), l.msg, 0)).collect(Collectors.toList());
        analyzerMessage.flows.add(sonarqubeFlow);
    }
    return analyzerMessage;
}
Also used : AnalyzerMessage(org.sonar.java.AnalyzerMessage) ComplexityVisitor(org.sonar.java.ast.visitors.ComplexityVisitor) CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) Tree(org.sonar.plugins.java.api.tree.Tree) Collectors(java.util.stream.Collectors) JavaFileScannerContext(org.sonar.plugins.java.api.JavaFileScannerContext) File(java.io.File) List(java.util.List) JavaVersion(org.sonar.plugins.java.api.JavaVersion) ImmutableList(com.google.common.collect.ImmutableList) SemanticModel(org.sonar.java.resolve.SemanticModel) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) JavaCheck(org.sonar.plugins.java.api.JavaCheck) VisibleForTesting(com.google.common.annotations.VisibleForTesting) SonarComponents(org.sonar.java.SonarComponents) Collections(java.util.Collections) Nullable(javax.annotation.Nullable) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) AnalyzerMessage(org.sonar.java.AnalyzerMessage)

Example 9 with AnalyzerMessage

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

the class CheckVerifier method assertSingleIssue.

private void assertSingleIssue(Set<AnalyzerMessage> issues) {
    Preconditions.checkState(issues.size() == 1, "A single issue is expected on the file");
    AnalyzerMessage issue = Iterables.getFirst(issues, null);
    assertThat(issue.getLine()).isNull();
    assertThat(issue.getMessage()).isEqualTo(expectFileIssue);
}
Also used : AnalyzerMessage(org.sonar.java.AnalyzerMessage)

Example 10 with AnalyzerMessage

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

the class XmlCheckContextImplTest method should_report_issue_on_node_with_secondary_and_cost.

@Test
public void should_report_issue_on_node_with_secondary_and_cost() throws Exception {
    Node node = firstNode(context, "//test2");
    int nodeLine = XmlCheckUtils.nodeLine(node);
    Node childNode = node.getFirstChild();
    int childNodeLine = XmlCheckUtils.nodeLine(childNode);
    int cost = 42;
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            AnalyzerMessage analyzerMessage = (AnalyzerMessage) invocation.getArguments()[0];
            reportedMessage = "onNode:" + analyzerMessage.getMessage() + "(" + analyzerMessage.getLine() + ")[" + analyzerMessage.getCost() + "]";
            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", childNode)), cost);
    String expectedMessage = "onNode:message1(" + nodeLine + ")[42.0];onChild:message2(" + childNodeLine + ")";
    assertThat(reportedMessage).isEqualTo(expectedMessage);
}
Also used : InvocationOnMock(org.mockito.invocation.InvocationOnMock) Node(org.w3c.dom.Node) AnalyzerMessage(org.sonar.java.AnalyzerMessage) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) 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