Search in sources :

Example 6 with VisitorsBridgeForTests

use of org.sonar.java.model.VisitorsBridgeForTests 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 7 with VisitorsBridgeForTests

use of org.sonar.java.model.VisitorsBridgeForTests 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

File (java.io.File)7 VisitorsBridgeForTests (org.sonar.java.model.VisitorsBridgeForTests)7 AnalyzerMessage (org.sonar.java.AnalyzerMessage)3 Test (org.junit.Test)2 SonarComponents (org.sonar.java.SonarComponents)2 JavaFileScanner (org.sonar.plugins.java.api.JavaFileScanner)2 ArrayList (java.util.ArrayList)1 FilterableIssue (org.sonar.api.scan.issue.filter.FilterableIssue)1 Rule (org.sonar.check.Rule)1 JavaCheck (org.sonar.plugins.java.api.JavaCheck)1