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());
}
}
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);
}
Aggregations