Search in sources :

Example 6 with AnalyzerMessage

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;
}
Also used : InputFile(org.sonar.api.batch.fs.InputFile) Collection(java.util.Collection) JavaQuickFix(org.sonar.java.reporting.JavaQuickFix) Set(java.util.Set) AnalyzerMessage(org.sonar.java.reporting.AnalyzerMessage) Supplier(java.util.function.Supplier) Collectors(java.util.stream.Collectors) JavaFileScannerContext(org.sonar.plugins.java.api.JavaFileScannerContext) List(java.util.List) InternalJavaIssueBuilder(org.sonar.java.reporting.InternalJavaIssueBuilder) Map(java.util.Map) Preconditions(org.sonar.java.Preconditions) JavaCheck(org.sonar.plugins.java.api.JavaCheck) Collections(java.util.Collections) JavaCheck(org.sonar.plugins.java.api.JavaCheck) Collection(java.util.Collection) AnalyzerMessage(org.sonar.java.reporting.AnalyzerMessage) InputFile(org.sonar.api.batch.fs.InputFile)

Example 7 with AnalyzerMessage

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);
}
Also used : JavaFileScannerContext(org.sonar.plugins.java.api.JavaFileScannerContext) AnalyzerMessage(org.sonar.java.reporting.AnalyzerMessage) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 8 with AnalyzerMessage

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);
}
Also used : SonarComponents(org.sonar.java.SonarComponents) SensorContextTester(org.sonar.api.batch.sensor.internal.SensorContextTester) JavaVersionImpl(org.sonar.java.model.JavaVersionImpl) CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) Tree(org.sonar.plugins.java.api.tree.Tree) AnalyzerMessage(org.sonar.java.reporting.AnalyzerMessage) InputFile(org.sonar.api.batch.fs.InputFile) File(java.io.File) Test(org.junit.jupiter.api.Test)

Example 9 with AnalyzerMessage

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()));
    }
}
Also used : AnalyzerMessage(org.sonar.java.reporting.AnalyzerMessage)

Example 10 with AnalyzerMessage

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);
    }
}
Also used : List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) AnalyzerMessage(org.sonar.java.reporting.AnalyzerMessage) LinkedList(java.util.LinkedList)

Aggregations

AnalyzerMessage (org.sonar.java.reporting.AnalyzerMessage)30 Test (org.junit.jupiter.api.Test)11 InputFile (org.sonar.api.batch.fs.InputFile)10 ArrayList (java.util.ArrayList)9 List (java.util.List)8 JavaFileScannerContext (org.sonar.plugins.java.api.JavaFileScannerContext)8 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)7 JavaCheck (org.sonar.plugins.java.api.JavaCheck)7 SonarComponents (org.sonar.java.SonarComponents)6 Tree (org.sonar.plugins.java.api.tree.Tree)5 File (java.io.File)4 Collections (java.util.Collections)4 Set (java.util.Set)4 Collectors (java.util.stream.Collectors)4 VisitorsBridgeForTests (org.sonar.java.testing.VisitorsBridgeForTests)4 HashSet (java.util.HashSet)3 Optional (java.util.Optional)3 ValueSource (org.junit.jupiter.params.provider.ValueSource)3 InputComponent (org.sonar.api.batch.fs.InputComponent)3 SensorContextTester (org.sonar.api.batch.sensor.internal.SensorContextTester)3