Search in sources :

Example 21 with AnalyzerMessage

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

the class FilterVerifier method verify.

public static void verify(String filename, JavaIssueFilter filter, JavaCheck... extraJavaChecks) {
    // set the component to the filter
    filter.setComponentKey(filename);
    IssueCollector issueCollector = new IssueCollector();
    ArrayList<JavaCheck> visitors = Lists.<JavaCheck>newArrayList(filter, issueCollector);
    // instantiate the rules filtered by the filter
    visitors.addAll(instantiateRules(filter.filteredRules()));
    for (JavaCheck visitor : extraJavaChecks) {
        visitors.add(visitor);
    }
    Collection<File> classpath = FileUtils.listFiles(new File("target/test-jars"), new String[] { "jar", "zip" }, true);
    VisitorsBridgeForTests visitorsBridge = new VisitorsBridgeForTests(visitors, Lists.newArrayList(classpath), null);
    JavaAstScanner.scanSingleFileForTests(new File(filename), visitorsBridge);
    VisitorsBridgeForTests.TestJavaFileScannerContext testJavaFileScannerContext = visitorsBridge.lastCreatedTestContext();
    Multimap<Integer, String> issuesByLines = HashMultimap.create();
    for (AnalyzerMessage analyzerMessage : testJavaFileScannerContext.getIssues()) {
        Integer issueLine = analyzerMessage.getLine();
        String ruleKey = AnnotationUtils.getAnnotation(analyzerMessage.getCheck().getClass(), Rule.class).key();
        FilterableIssue issue = mock(FilterableIssue.class);
        when(issue.ruleKey()).thenReturn(RuleKey.of("repo", ruleKey));
        when(issue.componentKey()).thenReturn(filename);
        when(issue.line()).thenReturn(issueLine);
        if (issueCollector.rejectedIssuesLines.contains(issueLine)) {
            assertThat(filter.accept(issue)).overridingErrorMessage("Line #" + issueLine + " has been marked with 'NoIssue' but issue of rule '" + ruleKey + "' has been accepted!").isFalse();
        } else if (issueCollector.acceptedIssuesLines.contains(issueLine)) {
            // force check on accepted issues
            assertThat(filter.accept(issue)).overridingErrorMessage("Line #" + issueLine + " has been marked with 'WithIssue' but no issue have been raised!").isTrue();
        } else {
            issuesByLines.put(issueLine, ruleKey);
        }
    }
    if (!issuesByLines.isEmpty()) {
        List<Integer> lines = Lists.newArrayList(issuesByLines.keySet());
        Collections.sort(lines);
        StringBuilder builder = new StringBuilder();
        for (Integer line : lines) {
            builder.append("\n#" + line + ": " + issuesByLines.get(line).toString());
        }
        Fail.fail("The following lines have not been marked with 'WithIssue' or 'NoIssue' and raised issues:" + builder.toString());
    }
}
Also used : JavaCheck(org.sonar.plugins.java.api.JavaCheck) FilterableIssue(org.sonar.api.scan.issue.filter.FilterableIssue) VisitorsBridgeForTests(org.sonar.java.model.VisitorsBridgeForTests) AnalyzerMessage(org.sonar.java.AnalyzerMessage) Rule(org.sonar.check.Rule) File(java.io.File)

Example 22 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.

@Test
public void should_report_issue_on_node_with_secondary() throws Exception {
    Node node = firstNode(context, "//test2");
    int nodeLine = XmlCheckUtils.nodeLine(node);
    Node childNode = node.getFirstChild();
    int childNodeLine = XmlCheckUtils.nodeLine(childNode);
    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", childNode)));
    String expectedMessage = "onNode:message1(" + nodeLine + ");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)

Example 23 with AnalyzerMessage

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

the class IssuableSubscriptionVisitorTest method test_custom_rules_report_issues.

@Test
public void test_custom_rules_report_issues() throws Exception {
    VisitorsBridgeForTests visitorsBridge = new VisitorsBridgeForTests(Lists.newArrayList(new CustomRule()), Lists.<File>newArrayList(), null);
    JavaAstScanner.scanSingleFileForTests(new File("src/test/resources/IssuableSubscriptionClass.java"), visitorsBridge);
    Set<AnalyzerMessage> issues = visitorsBridge.lastCreatedTestContext().getIssues();
    assertThat(issues).hasSize(7);
}
Also used : AnalyzerMessage(org.sonar.java.AnalyzerMessage) File(java.io.File) VisitorsBridgeForTests(org.sonar.java.model.VisitorsBridgeForTests) 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