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