Search in sources :

Example 1 with AnalyzerMessage

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

the class JavaCheckVerifierTest method rule_metadata_undefined_remediation_function.

@Test
public void rule_metadata_undefined_remediation_function() {
    @Rule(key = "UndefinedRemediationFunc")
    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)

Example 2 with AnalyzerMessage

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

the class JavaCheckVerifierTest method rule_metadata_remediation_function.

@Test
public void rule_metadata_remediation_function() {
    @Rule(key = "DoesntExists")
    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)

Example 3 with AnalyzerMessage

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

the class JavaCheckVerifier method assertMultipleIssue.

private void assertMultipleIssue(Set<AnalyzerMessage> issues) throws AssertionError {
    if (issues.isEmpty()) {
        Fail.fail("At least one issue expected");
    }
    List<Integer> unexpectedLines = Lists.newLinkedList();
    Multimap<Integer, Expectations.Issue> expected = expectations.issues;
    for (AnalyzerMessage issue : issues) {
        validateIssue(expected, unexpectedLines, issue);
    }
    if (!expected.isEmpty() || !unexpectedLines.isEmpty()) {
        Collections.sort(unexpectedLines);
        List<Integer> expectedLines = expected.keys().stream().sorted().collect(Collectors.toList());
        String expectedMsg = !expectedLines.isEmpty() ? ("Expected at " + expectedLines) : "";
        String unexpectedMsg = !unexpectedLines.isEmpty() ? ((expectedMsg.isEmpty() ? "" : ", ") + "Unexpected at " + unexpectedLines) : "";
        fail(expectedMsg + unexpectedMsg);
    }
    assertSuperfluousFlows();
}
Also used : AnalyzerMessage(org.sonar.java.AnalyzerMessage)

Example 4 with AnalyzerMessage

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

the class JavaCheckVerifier method assertSingleIssue.

private static void assertSingleIssue(Integer expectFileIssueOnline, String expectFileIssue, Set<AnalyzerMessage> issues) {
    Preconditions.checkState(issues.size() == 1, "A single issue is expected with line " + expectFileIssueOnline);
    AnalyzerMessage issue = Iterables.getFirst(issues, null);
    assertThat(issue.getLine()).isEqualTo(expectFileIssueOnline);
    assertThat(issue.getMessage()).isEqualTo(expectFileIssue);
}
Also used : AnalyzerMessage(org.sonar.java.AnalyzerMessage)

Example 5 with AnalyzerMessage

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

the class PomCheckContextImpl method getSecondaryAnalyzerMessage.

@VisibleForTesting
AnalyzerMessage getSecondaryAnalyzerMessage(JavaCheck check, File file, Location location) {
    TextSpan ts;
    if (location.onLine()) {
        ts = textSpanForLine(file, location.line);
    } else {
        XmlLocation startLocation = location.tree.startLocation();
        XmlLocation endLocation = location.tree.endLocation();
        ts = new TextSpan(startLocation.line(), offsetFromColumn(startLocation.column()), endLocation.line(), offsetFromColumn(endLocation.column()));
        if (ts.isEmpty()) {
            ts = textSpanForLine(file, startLocation.line());
        }
    }
    return new AnalyzerMessage(check, file, ts, location.msg, 0);
}
Also used : TextSpan(org.sonar.java.AnalyzerMessage.TextSpan) XmlLocation(org.sonar.maven.model.XmlLocation) AnalyzerMessage(org.sonar.java.AnalyzerMessage) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

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