use of org.sonar.java.AnalyzerMessage in project sonar-java by SonarSource.
the class JavaCheckVerifierTest method rule_metadata_unparsable.
@Test
public void rule_metadata_unparsable() {
@Rule(key = "BrokenJSON")
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);
}
use of org.sonar.java.AnalyzerMessage in project sonar-java by SonarSource.
the class JavaCheckVerifierTest method rule_with_constant_remediation_function_should_not_provide_cost.
@Test
public void rule_with_constant_remediation_function_should_not_provide_cost() throws Exception {
FakeVisitor fakeVisitor = new FakeVisitor();
fakeVisitor.withPreciseIssue(new AnalyzerMessage(fakeVisitor, new File("a"), new AnalyzerMessage.TextSpan(1), "message", 1));
Throwable throwable = catchThrowable(() -> JavaCheckVerifier.verify("src/test/files/JavaCheckVerifierNoCost.java", fakeVisitor));
assertThat(throwable).isInstanceOf(IllegalStateException.class).hasMessage("Rule with constant remediation function shall not provide cost");
}
use of org.sonar.java.AnalyzerMessage in project sonar-java by SonarSource.
the class CheckVerifier method assertMultipleIssue.
private void assertMultipleIssue(Set<AnalyzerMessage> issues) throws AssertionError {
Preconditions.checkState(!issues.isEmpty(), "At least one issue expected");
List<Integer> unexpectedLines = Lists.newLinkedList();
RemediationFunction remediationFunction = remediationFunction(issues.iterator().next());
for (AnalyzerMessage issue : issues) {
validateIssue(expected, unexpectedLines, issue, remediationFunction);
}
if (!expected.isEmpty() || !unexpectedLines.isEmpty()) {
Collections.sort(unexpectedLines);
String expectedMsg = expectedMessage();
String unexpectedMsg = unexpectedMessage(unexpectedLines);
Fail.fail(expectedMsg + unexpectedMsg);
}
}
use of org.sonar.java.AnalyzerMessage in project sonar-java by SonarSource.
the class CheckVerifier method validateAnalyzerMessage.
private static void validateAnalyzerMessage(Map<IssueAttribute, String> attrs, AnalyzerMessage analyzerMessage) {
Double effortToFix = analyzerMessage.getCost();
if (effortToFix != null) {
assertEquals(Integer.toString(effortToFix.intValue()), attrs, IssueAttribute.EFFORT_TO_FIX);
}
AnalyzerMessage.TextSpan textSpan = analyzerMessage.primaryLocation();
assertEquals(normalizeColumn(textSpan.startCharacter), attrs, IssueAttribute.START_COLUMN);
assertEquals(Integer.toString(textSpan.endLine), attrs, IssueAttribute.END_LINE);
assertEquals(normalizeColumn(textSpan.endCharacter), attrs, IssueAttribute.END_COLUMN);
if (attrs.containsKey(IssueAttribute.SECONDARY_LOCATIONS)) {
List<AnalyzerMessage> secondaryLocations = analyzerMessage.flows.stream().map(l -> l.get(0)).collect(Collectors.toList());
Multiset<String> actualLines = HashMultiset.create();
for (AnalyzerMessage secondaryLocation : secondaryLocations) {
actualLines.add(Integer.toString(secondaryLocation.getLine()));
}
List<String> expected = Lists.newArrayList(Splitter.on(",").omitEmptyStrings().trimResults().split(attrs.get(IssueAttribute.SECONDARY_LOCATIONS)));
List<String> unexpected = new ArrayList<>();
for (String actualLine : actualLines) {
if (expected.contains(actualLine)) {
expected.remove(actualLine);
} else {
unexpected.add(actualLine);
}
}
if (!expected.isEmpty() || !unexpected.isEmpty()) {
// Line is not covered by JaCoCo because of thrown exception but effectively covered in UT.
Fail.fail(String.format("Secondary locations: expected: %s unexpected:%s. In %s:%d", expected, unexpected, normalizedFilePath(analyzerMessage), analyzerMessage.getLine()));
}
}
}
use of org.sonar.java.AnalyzerMessage in project sonar-java by SonarSource.
the class PomCheckContextImpl method reportIssue.
@Override
public void reportIssue(PomCheck check, int line, String message, List<Location> secondary) {
File file = getFile();
AnalyzerMessage analyzerMessage = new AnalyzerMessage(check, file, line, message, 0);
for (Location location : secondary) {
AnalyzerMessage secondaryLocation = getSecondaryAnalyzerMessage(check, file, location);
analyzerMessage.flows.add(Collections.singletonList(secondaryLocation));
}
getSonarComponents().reportIssue(analyzerMessage);
}
Aggregations