Search in sources :

Example 16 with AnalyzerMessage

use of org.sonar.java.reporting.AnalyzerMessage in project sonar-java by SonarSource.

the class DefaultJavaFileScannerContextTest method report_issue_with_message.

@Test
void report_issue_with_message() {
    AnalyzerMessage message = context.createAnalyzerMessage(CHECK, compilationUnitTree, "msg");
    context.reportIssue(message);
    assertThat(reportedMessage.getMessage()).isEqualTo("msg");
    assertThat(reportedMessage.getCost()).isNull();
    assertThat(reportedMessage.flows).isEmpty();
    assertMessagePosition(reportedMessage, 1, 0, 6, 1);
}
Also used : AnalyzerMessage(org.sonar.java.reporting.AnalyzerMessage) Test(org.junit.jupiter.api.Test)

Example 17 with AnalyzerMessage

use of org.sonar.java.reporting.AnalyzerMessage in project sonar-java by SonarSource.

the class JavaFileScannerContextForTestsTest method reportIssue_on_tree_with_secondaries.

@ParameterizedTest
@ValueSource(booleans = { true, false })
void reportIssue_on_tree_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, "issue on A", 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");
    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, 2, 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 18 with AnalyzerMessage

use of org.sonar.java.reporting.AnalyzerMessage in project sonar-java by SonarSource.

the class JavaFileScannerContextForTestsTest method reportIssue_between_trees_with_flows.

@ParameterizedTest
@ValueSource(booleans = { true, false })
void reportIssue_between_trees_with_flows(boolean withFlowsAndCost) {
    JavaFileScannerContext.Location location1 = new JavaFileScannerContext.Location("secondary message on }", classA.closeBraceToken());
    JavaFileScannerContext.Location location2 = new JavaFileScannerContext.Location("secondary message on {", classA.openBraceToken());
    Iterable<List<JavaFileScannerContext.Location>> flows = withFlowsAndCost ? Collections.singletonList(Arrays.asList(location1, location2)) : Collections.emptyList();
    Integer cost = withFlowsAndCost ? 42 : null;
    context.reportIssueWithFlow(CHECK, classA, "issue on A", flows, 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");
    assertThat(issue.getInputComponent()).isEqualTo(inputFile);
    assertThat(issue.getLine()).isEqualTo(2);
    if (withFlowsAndCost) {
        assertThat(issue.getCost()).isEqualTo(42);
        assertThat(issue.flows).hasSize(1).allMatch(secondary -> secondary.size() == 2);
        AnalyzerMessage secondary = issue.flows.get(0).get(0);
        assertThat(secondary.getMessage()).isEqualTo("secondary message on }");
    } else {
        assertThat(issue.getCost()).isNull();
        assertThat(issue.flows).isEmpty();
    }
    assertPosition(issue.primaryLocation(), 2, 0, 2, 10);
}
Also used : JavaFileScannerContext(org.sonar.plugins.java.api.JavaFileScannerContext) List(java.util.List) AnalyzerMessage(org.sonar.java.reporting.AnalyzerMessage) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 19 with AnalyzerMessage

use of org.sonar.java.reporting.AnalyzerMessage in project sonar-java by SonarSource.

the class JavaFileScannerContextForTestsTest method reportIssue_on_tree.

@Test
void reportIssue_on_tree() {
    context.reportIssue(CHECK, classA, "issue on A");
    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");
    assertThat(issue.getInputComponent()).isEqualTo(inputFile);
    assertThat(issue.getLine()).isEqualTo(2);
    assertThat(issue.getCost()).isNull();
    assertThat(issue.flows).isEmpty();
    assertPosition(issue.primaryLocation(), 2, 0, 2, 10);
}
Also used : AnalyzerMessage(org.sonar.java.reporting.AnalyzerMessage) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 20 with AnalyzerMessage

use of org.sonar.java.reporting.AnalyzerMessage in project sonar-java by SonarSource.

the class JavaFileScannerContextForTestsTest method reportIssue_between_trees.

@Test
void reportIssue_between_trees() {
    context.reportIssue(CHECK, classA, classB, "issue on A and B");
    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);
    assertThat(issue.getCost()).isNull();
    assertThat(issue.flows).isEmpty();
    assertPosition(issue.primaryLocation(), 2, 0, 3, 10);
}
Also used : AnalyzerMessage(org.sonar.java.reporting.AnalyzerMessage) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

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