use of org.sonar.java.AnalyzerMessage in project sonar-java by SonarSource.
the class XmlCheckContextImplTest method should_not_report_unknown_secondaries.
@Test
public void should_not_report_unknown_secondaries() throws Exception {
// manual parsing
Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(context.getFile());
Node node = firstNode(context, "//test2");
int nodeLine = XmlCheckUtils.nodeLine(node);
// uses document with recorded lines
Node child = Iterables.get(context.evaluate(context.compile("//test2/item"), doc), 0);
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", child)));
assertThat(reportedMessage).isEqualTo("onNode:message1(" + nodeLine + ")");
}
use of org.sonar.java.AnalyzerMessage in project sonar-java by SonarSource.
the class XmlCheckContextImpl method getSecondaryAnalyzerMessage.
@CheckForNull
private AnalyzerMessage getSecondaryAnalyzerMessage(JavaCheck check, File file, XmlDocumentLocation location) {
Integer startLine = nodeLine(location.node);
if (startLine == null) {
return null;
}
String line = sonarComponents.fileLines(file).get(startLine - 1);
TextSpan ts = new TextSpan(startLine, 0, startLine, line.length());
return new AnalyzerMessage(check, file, ts, location.msg, 0);
}
use of org.sonar.java.AnalyzerMessage in project sonar-java by SonarSource.
the class DefaultJavaFileScannerContextTest method report_issue_on_tree_with_secondary.
@Test
public void report_issue_on_tree_with_secondary() {
ClassTree tree = (ClassTree) compilationUnitTree.types().get(0);
Tree firstMember = tree.members().get(0);
Tree secondMember = tree.members().get(1);
ArrayList<Location> secondary = new ArrayList<>();
secondary.add(new JavaFileScannerContext.Location("secondary", firstMember));
secondary.add(new JavaFileScannerContext.Location("secondary", secondMember));
context.reportIssue(CHECK, tree.simpleName(), "msg", secondary, null);
assertThat(reportedMessage.getMessage()).isEqualTo("msg");
assertThat(reportedMessage.getCost()).isNull();
assertThat(reportedMessage.flows).hasSize(2);
assertMessagePosition(reportedMessage, 1, 6, 1, 7);
List<AnalyzerMessage> secondaries = reportedMessage.flows.stream().map(flow -> flow.get(0)).collect(Collectors.toList());
assertThat(secondaries).hasSize(2);
assertMessagePosition(secondaries.get(0), 2, 2, 2, 13);
assertMessagePosition(secondaries.get(1), 3, 2, 3, 15);
}
use of org.sonar.java.AnalyzerMessage in project sonar-java by SonarSource.
the class PomCheckContextImplTest method createSonarComponentsMock.
private static SonarComponents createSonarComponentsMock() {
SonarComponents sonarComponents = mock(SonarComponents.class);
Mockito.when(sonarComponents.fileLines(any(File.class))).thenReturn(Arrays.asList("line 1", "line 2", "line 3 is longer"));
doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
reportedMessage = "onLine:" + invocation.getArguments()[3];
return null;
}
}).when(sonarComponents).addIssue(any(File.class), eq(CHECK), eq(LINE), anyString(), eq(null));
doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
reportedMessage = "onFile:" + invocation.getArguments()[3];
return null;
}
}).when(sonarComponents).addIssue(any(File.class), eq(CHECK), eq(-1), anyString(), eq(null));
doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
AnalyzerMessage analyzerMessage = (AnalyzerMessage) invocation.getArguments()[0];
reportedMessage = "analyzerMessage:" + analyzerMessage.getMessage();
for (AnalyzerMessage secondary : analyzerMessage.flows.stream().map(l -> l.get(0)).collect(Collectors.toList())) {
TextSpan location = secondary.primaryLocation();
reportedMessage += ";" + secondary.getMessage() + "[" + location.startLine + ";" + location.startCharacter + "/" + location.endLine + ";" + location.endCharacter + "]";
}
return null;
}
}).when(sonarComponents).reportIssue(any(AnalyzerMessage.class));
return sonarComponents;
}
use of org.sonar.java.AnalyzerMessage in project sonar-java by SonarSource.
the class JavaCheckVerifierTest method rule_metadata_unknown_remediation_function.
@Test
public void rule_metadata_unknown_remediation_function() {
@Rule(key = "ExponentialRemediationFunc")
class WrongJson extends FakeVisitor {
}
WrongJson check = new WrongJson();
check.withPreciseIssue(new AnalyzerMessage(check, new File("a"), 1, "message", 0));
JavaCheckVerifier.verify("src/test/files/JavaCheckVerifierNoCost.java", check);
}
Aggregations