use of org.sonar.java.reporting.AnalyzerMessage in project sonar-java by SonarSource.
the class JavaIssueBuilderForTests method report.
@Override
public void report() {
Preconditions.checkState(!reported, "Can only be reported once.");
JavaCheck rule = rule();
InputFile inputFile = inputFile();
AnalyzerMessage.TextSpan textSpan = textSpan();
AnalyzerMessage issue = new AnalyzerMessage(rule, inputFile, textSpan, message(), cost().orElse(0));
secondaries().map(JavaIssueBuilderForTests::toSingletonList).map(secondaries -> listOfLocationsToListOfAnalyzerMessages(secondaries, rule, inputFile)).ifPresent(issue.flows::addAll);
flows().map(flows -> listOfLocationsToListOfAnalyzerMessages(flows, rule, inputFile)).ifPresent(issue.flows::addAll);
quickFixes.put(textSpan, quickFixes().stream().map(Supplier::get).flatMap(Collection::stream).collect(Collectors.toList()));
issues.add(issue);
reported = true;
}
use of org.sonar.java.reporting.AnalyzerMessage in project sonar-java by SonarSource.
the class JavaFileScannerContextForTestsTest method reportIssue_between_trees_with_secondaries.
@ParameterizedTest
@ValueSource(booleans = { true, false })
void reportIssue_between_trees_with_secondaries(boolean withSecondariesAndCost) {
JavaFileScannerContext.Location location1 = new JavaFileScannerContext.Location("secondary message on }", classA.closeBraceToken());
JavaFileScannerContext.Location location2 = new JavaFileScannerContext.Location("secondary message on {", classA.openBraceToken());
List<JavaFileScannerContext.Location> secondaries = withSecondariesAndCost ? Arrays.asList(location1, location2) : Collections.emptyList();
Integer cost = withSecondariesAndCost ? 42 : null;
context.reportIssue(CHECK, classA, classB, "issue on A and B", secondaries, cost);
Set<AnalyzerMessage> issues = context.getIssues();
assertThat(issues).hasSize(1);
AnalyzerMessage issue = issues.iterator().next();
assertThat(issue.getCheck()).isInstanceOf(DummyRule.class);
assertThat(issue.getMessage()).isEqualTo("issue on A and B");
assertThat(issue.getInputComponent()).isEqualTo(inputFile);
assertThat(issue.getLine()).isEqualTo(2);
if (withSecondariesAndCost) {
assertThat(issue.getCost()).isEqualTo(42);
assertThat(issue.flows).hasSize(2).allMatch(secondary -> secondary.size() == 1);
AnalyzerMessage secondary = issue.flows.get(1).get(0);
assertThat(secondary.getMessage()).isEqualTo("secondary message on {");
} else {
assertThat(issue.getCost()).isNull();
assertThat(issue.flows).isEmpty();
}
assertPosition(issue.primaryLocation(), 2, 0, 3, 10);
}
use of org.sonar.java.reporting.AnalyzerMessage in project sonar-java by SonarSource.
the class VisitorsBridgeForTestsTest method test_report_with_analysis_message.
@Test
void test_report_with_analysis_message() {
SensorContextTester context = SensorContextTester.create(new File("")).setRuntime(SonarRuntimeImpl.forSonarLint(Version.create(6, 7)));
SonarComponents sonarComponents = new SonarComponents(null, context.fileSystem(), null, null, null);
sonarComponents.setSensorContext(context);
Tree parse = JParserTestUtils.parse("class A{}");
DummyVisitor javaCheck = new DummyVisitor();
VisitorsBridgeForTests visitorsBridgeForTests = new VisitorsBridgeForTests(Collections.singletonList(javaCheck), sonarComponents, new JavaVersionImpl());
visitorsBridgeForTests.setCurrentFile(TestUtils.emptyInputFile("dummy.java"));
visitorsBridgeForTests.visitFile(parse, false);
JavaFileScannerContextForTests lastContext = visitorsBridgeForTests.lastCreatedTestContext();
assertThat(lastContext.getIssues()).isEmpty();
AnalyzerMessage message = lastContext.createAnalyzerMessage(javaCheck, parse, "test");
lastContext.addIssue(-1, javaCheck, "test");
lastContext.addIssue(-1, javaCheck, "test", 15);
lastContext.addIssueOnFile(javaCheck, "test");
lastContext.addIssueOnProject(javaCheck, "test");
lastContext.reportIssue(message);
assertThat(message.getMessage()).isEqualTo("test");
assertThat(lastContext.getIssues()).hasSize(5);
}
use of org.sonar.java.reporting.AnalyzerMessage in project sonar-java by SonarSource.
the class InternalCheckVerifier method assertComponentIssue.
private void assertComponentIssue(Set<AnalyzerMessage> issues) {
String expectedMessage = expectations.expectedFileIssue();
String component = "file";
String otherComponent = "project";
if (expectations.expectIssueAtProjectLevel()) {
expectedMessage = expectations.expectedProjectIssue();
component = "project";
otherComponent = "file";
}
if (issues.size() != 1) {
String issueNumberMessage = issues.isEmpty() ? "none has been raised" : String.format("%d issues have been raised", issues.size());
throw new AssertionError(String.format("A single issue is expected on the %s, but %s", component, issueNumberMessage));
}
AnalyzerMessage issue = issues.iterator().next();
if (issue.getLine() != null) {
throw new AssertionError(String.format("Expected an issue directly on %s but was raised on line %d", component, issue.getLine()));
}
if ((expectations.expectIssueAtProjectLevel() && issue.getInputComponent().isFile()) || (expectations.expectIssueAtFileLevel() && !issue.getInputComponent().isFile())) {
throw new AssertionError(String.format("Expected the issue to be raised at %s level, not at %s level", component, otherComponent));
}
if (!expectedMessage.equals(issue.getMessage())) {
throw new AssertionError(String.format("Expected the issue message to be:%n\t\"%s\"%nbut was:%n\t\"%s\"", expectedMessage, issue.getMessage()));
}
}
use of org.sonar.java.reporting.AnalyzerMessage in project sonar-java by SonarSource.
the class InternalCheckVerifier method assertMultipleIssues.
private void assertMultipleIssues(Set<AnalyzerMessage> issues, Map<TextSpan, List<JavaQuickFix>> quickFixes) throws AssertionError {
if (issues.isEmpty()) {
throw new AssertionError("No issue raised. At least one issue expected");
}
List<Integer> unexpectedLines = new LinkedList<>();
Expectations.RemediationFunction remediationFunction = Expectations.remediationFunction(issues.iterator().next());
Map<Integer, List<Expectations.Issue>> expected = expectations.issues;
for (AnalyzerMessage issue : issues) {
validateIssue(expected, unexpectedLines, issue, remediationFunction);
}
if (!expected.isEmpty() || !unexpectedLines.isEmpty()) {
Collections.sort(unexpectedLines);
List<Integer> expectedLines = expected.keySet().stream().sorted().collect(Collectors.toList());
throw new AssertionError(new StringBuilder().append(expectedLines.isEmpty() ? "" : String.format("Expected at %s", expectedLines)).append(expectedLines.isEmpty() || unexpectedLines.isEmpty() ? "" : ", ").append(unexpectedLines.isEmpty() ? "" : String.format("Unexpected at %s", unexpectedLines)).toString());
}
assertSuperfluousFlows();
if (collectQuickFixes) {
new QuickFixesVerifier(expectations.quickFixes(), quickFixes).accept(issues);
}
}
Aggregations