use of org.sonar.java.regex.RegexCheck in project sonar-java by SonarSource.
the class DefaultJavaFileScannerContextTest method report_issue_on_regex_tree_with_secondary.
@Test
void report_issue_on_regex_tree_with_secondary() {
RegexCheck regexCheck = new RegexCheck() {
};
String regex = "x{42}|y{23}";
RegexTree regexTree = RegexParserTestUtils.assertSuccessfulParse(regex);
DisjunctionTree disjunctionTree = (DisjunctionTree) regexTree;
RepetitionTree x42 = (RepetitionTree) disjunctionTree.getAlternatives().get(0);
CurlyBraceQuantifier rep42 = (CurlyBraceQuantifier) x42.getQuantifier();
RepetitionTree y23 = (RepetitionTree) disjunctionTree.getAlternatives().get(1);
CurlyBraceQuantifier rep23 = (CurlyBraceQuantifier) y23.getQuantifier();
RegexCheck.RegexIssueLocation secondary = new RegexCheck.RegexIssueLocation(rep42, "regexSecondary");
context.reportIssue(regexCheck, rep23, "regexMsg", null, Collections.singletonList(secondary));
assertThat(reportedMessage.getMessage()).isEqualTo("regexMsg");
assertThat(reportedMessage.getCost()).isNull();
assertMessagePosition(reportedMessage, 3, 8, 3, 12);
assertThat(reportedMessage.flows).hasSize(1);
List<AnalyzerMessage> reportedSecondaries = reportedMessage.flows.get(0);
assertThat(reportedSecondaries).hasSize(1);
AnalyzerMessage reportedSecondary = reportedSecondaries.get(0);
assertThat(reportedSecondary.getMessage()).isEqualTo("regexSecondary");
assertThat(reportedSecondary.getCost()).isNull();
assertMessagePosition(reportedSecondary, 3, 2, 3, 6);
}
use of org.sonar.java.regex.RegexCheck in project sonar-java by SonarSource.
the class DefaultJavaFileScannerContextTest method report_issue_on_regex_tree.
@Test
void report_issue_on_regex_tree() {
RegexCheck regexCheck = new RegexCheck() {
};
String regex = "x{42}|y{23}";
RegexTree regexTree = RegexParserTestUtils.assertSuccessfulParse(regex);
DisjunctionTree disjunctionTree = (DisjunctionTree) regexTree;
RepetitionTree y23 = (RepetitionTree) disjunctionTree.getAlternatives().get(1);
CurlyBraceQuantifier rep23 = (CurlyBraceQuantifier) y23.getQuantifier();
int cost = 42;
context.reportIssue(regexCheck, rep23, "regexMsg", cost, Collections.emptyList());
assertThat(reportedMessage.getMessage()).isEqualTo("regexMsg");
assertThat(reportedMessage.getCost()).isEqualTo(Double.valueOf(cost));
assertThat(reportedMessage.flows).isEmpty();
assertMessagePosition(reportedMessage, 3, 8, 3, 12);
}
use of org.sonar.java.regex.RegexCheck in project sonar-java by SonarSource.
the class DefaultJavaFileScannerContext method reportIssue.
private void reportIssue(RegexCheck regexCheck, AnalyzerMessage.TextSpan mainLocation, String message, @Nullable Integer cost, List<RegexCheck.RegexIssueLocation> secondaries) {
List<List<RegexCheck.RegexIssueLocation>> secondariesAsFlows = new ArrayList<>();
secondaries.stream().flatMap(regexIssueLocation -> regexIssueLocation.toSingleLocationItems().stream()).map(Collections::singletonList).forEach(secondariesAsFlows::add);
AnalyzerMessage analyzerMessage = new AnalyzerMessage(regexCheck, inputFile, mainLocation, message, cost != null ? cost : 0);
completeAnalyzerMessageWithFlows(analyzerMessage, secondariesAsFlows, ril -> ril.locations().get(0), RegexCheck.RegexIssueLocation::message);
reportIssue(analyzerMessage);
}
Aggregations