use of org.sonar.java.AnalyzerMessage in project sonar-java by SonarSource.
the class ParsingErrorCheckTest method test.
@Test
public void test() {
SonarComponents sonarComponents = mock(SonarComponents.class);
when(sonarComponents.fileContent(any())).thenCallRealMethod();
when(sonarComponents.fileLength(any())).thenCallRealMethod();
VisitorsBridgeForTests visitorsBridge = new VisitorsBridgeForTests(new ParsingErrorCheck(), sonarComponents);
JavaAstScanner.scanSingleFileForTests(new File("src/test/files/checks/ParsingError.java"), visitorsBridge);
Set<AnalyzerMessage> issues = visitorsBridge.lastCreatedTestContext().getIssues();
assertThat(issues).hasSize(1);
AnalyzerMessage issue = issues.iterator().next();
assertThat(issue.getLine()).isEqualTo(1);
assertThat(issue.getMessage()).isEqualTo("Parse error");
}
use of org.sonar.java.AnalyzerMessage in project sonar-java by SonarSource.
the class DefaultJavaFileScannerContextTest method createSonarComponentsMock.
private static SonarComponents createSonarComponentsMock() {
SonarComponents sonarComponents = mock(SonarComponents.class);
doAnswer(invocation -> {
reportedMessage = (AnalyzerMessage) invocation.getArguments()[0];
return null;
}).when(sonarComponents).reportIssue(any(AnalyzerMessage.class));
return sonarComponents;
}
use of org.sonar.java.AnalyzerMessage in project sonar-java by SonarSource.
the class DefaultJavaFileScannerContext method createAnalyzerMessage.
protected static AnalyzerMessage createAnalyzerMessage(File file, JavaCheck javaCheck, Tree startTree, @Nullable Tree endTree, String message, Iterable<List<Location>> flows, @Nullable Integer cost) {
AnalyzerMessage.TextSpan textSpan = endTree != null ? AnalyzerMessage.textSpanBetween(startTree, endTree) : AnalyzerMessage.textSpanFor(startTree);
AnalyzerMessage analyzerMessage = new AnalyzerMessage(javaCheck, file, textSpan, message, cost != null ? cost : 0);
for (List<Location> flow : flows) {
List<AnalyzerMessage> sonarqubeFlow = flow.stream().map(l -> new AnalyzerMessage(javaCheck, file, AnalyzerMessage.textSpanFor(l.syntaxNode), l.msg, 0)).collect(Collectors.toList());
analyzerMessage.flows.add(sonarqubeFlow);
}
return analyzerMessage;
}
use of org.sonar.java.AnalyzerMessage in project sonar-java by SonarSource.
the class CheckVerifier method assertSingleIssue.
private void assertSingleIssue(Set<AnalyzerMessage> issues) {
Preconditions.checkState(issues.size() == 1, "A single issue is expected on the file");
AnalyzerMessage issue = Iterables.getFirst(issues, null);
assertThat(issue.getLine()).isNull();
assertThat(issue.getMessage()).isEqualTo(expectFileIssue);
}
use of org.sonar.java.AnalyzerMessage in project sonar-java by SonarSource.
the class XmlCheckContextImplTest method should_report_issue_on_node_with_secondary_and_cost.
@Test
public void should_report_issue_on_node_with_secondary_and_cost() throws Exception {
Node node = firstNode(context, "//test2");
int nodeLine = XmlCheckUtils.nodeLine(node);
Node childNode = node.getFirstChild();
int childNodeLine = XmlCheckUtils.nodeLine(childNode);
int cost = 42;
doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
AnalyzerMessage analyzerMessage = (AnalyzerMessage) invocation.getArguments()[0];
reportedMessage = "onNode:" + analyzerMessage.getMessage() + "(" + analyzerMessage.getLine() + ")[" + analyzerMessage.getCost() + "]";
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)), cost);
String expectedMessage = "onNode:message1(" + nodeLine + ")[42.0];onChild:message2(" + childNodeLine + ")";
assertThat(reportedMessage).isEqualTo(expectedMessage);
}
Aggregations