use of org.sonar.java.reporting.AnalyzerMessage in project sonar-java by SonarSource.
the class SonarComponentsTest method add_issue_or_parse_error.
@Test
void add_issue_or_parse_error() throws Exception {
JavaCheck expectedCheck = new CustomCheck();
CheckRegistrar expectedRegistrar = getRegistrar(expectedCheck);
SensorContextTester context = SensorContextTester.create(new File("."));
DefaultFileSystem fileSystem = context.fileSystem();
TestInputFileBuilder inputFileBuilder = new TestInputFileBuilder("", "file.java");
inputFileBuilder.setLines(45);
int[] lineStartOffsets = new int[45];
lineStartOffsets[35] = 12;
lineStartOffsets[42] = 1;
int lastValidOffset = 420;
inputFileBuilder.setOriginalLineStartOffsets(lineStartOffsets);
inputFileBuilder.setOriginalLineEndOffsets(computeLineEndOffsets(lineStartOffsets, lastValidOffset));
inputFileBuilder.setLastValidOffset(lastValidOffset);
InputFile inputFile = inputFileBuilder.build();
fileSystem.add(inputFile);
when(this.checks.ruleKey(any(JavaCheck.class))).thenReturn(mock(RuleKey.class));
SonarComponents sonarComponents = new SonarComponents(fileLinesContextFactory, fileSystem, null, null, checkFactory, new CheckRegistrar[] { expectedRegistrar });
sonarComponents.setSensorContext(context);
sonarComponents.addIssue(inputFile, expectedCheck, -5, "message on wrong line", null);
sonarComponents.addIssue(inputFile, expectedCheck, 42, "message on line 42", 1);
sonarComponents.reportIssue(new AnalyzerMessage(expectedCheck, inputFile, 35, "other message", 0));
List<Issue> issues = new ArrayList<>(context.allIssues());
assertThat(issues).hasSize(3);
assertThat(issues.get(0).primaryLocation().message()).isEqualTo("message on wrong line");
assertThat(issues.get(1).primaryLocation().message()).isEqualTo("message on line 42");
assertThat(issues.get(2).primaryLocation().message()).isEqualTo("other message");
RecognitionException parseError = new RecognitionException(-1, "invalid code", new Exception("parse error"));
context.setRuntime(SonarRuntimeImpl.forSonarLint(V8_9));
assertThat(sonarComponents.reportAnalysisError(parseError, inputFile)).isTrue();
context.setRuntime(SonarRuntimeImpl.forSonarQube(V8_9, SonarQubeSide.SCANNER, SonarEdition.COMMUNITY));
assertThat(sonarComponents.reportAnalysisError(parseError, inputFile)).isFalse();
}
use of org.sonar.java.reporting.AnalyzerMessage in project sonar-java by SonarSource.
the class SonarComponentsTest method fail_on_empty_location.
@Test
void fail_on_empty_location() {
JavaCheck expectedCheck = new CustomCheck();
CheckRegistrar expectedRegistrar = getRegistrar(expectedCheck);
RuleKey ruleKey = RuleKey.of("MyRepo", "CustomCheck");
InputFile inputFile = new TestInputFileBuilder("", "file.java").initMetadata("class A {\n" + " void foo() {\n" + " System.out.println();\n" + " }\n" + "}\n").build();
SensorContextTester context = SensorContextTester.create(new File(""));
SonarComponents sonarComponents = new SonarComponents(fileLinesContextFactory, context.fileSystem(), null, null, checkFactory, new CheckRegistrar[] { expectedRegistrar });
sonarComponents.setSensorContext(context);
AnalyzerMessage.TextSpan emptyTextSpan = new AnalyzerMessage.TextSpan(3, 10, 3, 10);
AnalyzerMessage analyzerMessageEmptyLocation = new AnalyzerMessage(expectedCheck, inputFile, emptyTextSpan, "message", 0);
assertThatThrownBy(() -> sonarComponents.reportIssue(analyzerMessageEmptyLocation, ruleKey, inputFile, 0.0)).isInstanceOf(IllegalStateException.class).hasMessageContaining("Issue location should not be empty");
assertThat(context.allIssues()).isEmpty();
AnalyzerMessage.TextSpan nonEmptyTextSpan = new AnalyzerMessage.TextSpan(3, 10, 3, 15);
AnalyzerMessage analyzerMessageValidLocation = new AnalyzerMessage(expectedCheck, inputFile, nonEmptyTextSpan, "message", 0);
sonarComponents.reportIssue(analyzerMessageValidLocation, ruleKey, inputFile, 0.0);
assertThat(context.allIssues()).isNotEmpty();
}
use of org.sonar.java.reporting.AnalyzerMessage 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.reporting.AnalyzerMessage in project sonar-java by SonarSource.
the class DefaultInputFileScannerContextTest method createSonarComponentsMock.
private SonarComponents createSonarComponentsMock() {
SonarComponents sonarComponents = mock(SonarComponents.class);
doAnswer(invocation -> {
reportedMessage = (AnalyzerMessage) invocation.getArguments()[0];
return null;
}).when(sonarComponents).reportIssue(any(AnalyzerMessage.class));
doAnswer(invocation -> {
Integer cost = invocation.getArgument(4);
reportedMessage = new AnalyzerMessage(invocation.getArgument(1), invocation.getArgument(0), null, invocation.getArgument(3), cost != null ? cost : 0);
return null;
}).when(sonarComponents).addIssue(any(InputComponent.class), any(JavaCheck.class), anyInt(), anyString(), any());
when(sonarComponents.fileLines(any(InputFile.class))).thenReturn(Arrays.asList("1st line", "2nd line"));
when(sonarComponents.inputFileContents(any(InputFile.class))).thenReturn("content");
when(sonarComponents.projectLevelWorkDir()).thenReturn(WORK_DIR);
when(sonarComponents.project()).thenReturn(PROJECT_BASE_DIR);
return sonarComponents;
}
use of org.sonar.java.reporting.AnalyzerMessage in project sonar-java by SonarSource.
the class DefaultJavaFileScannerContextTest method report_issue_on_tree_with_secondary.
@Test
void report_issue_on_tree_with_secondary() {
ClassTree tree = (ClassTree) compilationUnitTree.types().get(0);
Tree firstMember = tree.members().get(0);
Tree secondMember = tree.members().get(1);
ArrayList<Location> secondary = new ArrayList<>();
secondary.add(new JavaFileScannerContext.Location("secondary", firstMember));
secondary.add(new JavaFileScannerContext.Location("secondary", secondMember));
context.reportIssue(CHECK, tree.simpleName(), "msg", secondary, null);
assertThat(reportedMessage.getMessage()).isEqualTo("msg");
assertThat(reportedMessage.getCost()).isNull();
assertThat(reportedMessage.flows).hasSize(2);
assertMessagePosition(reportedMessage, 3, 6, 3, 7);
List<AnalyzerMessage> secondaries = reportedMessage.flows.stream().map(flow -> flow.get(0)).collect(Collectors.toList());
assertThat(secondaries).hasSize(2);
assertMessagePosition(secondaries.get(0), 4, 2, 4, 13);
assertMessagePosition(secondaries.get(1), 5, 2, 5, 15);
}
Aggregations