diff --git a/external-reports/src/main/java/org/sonar/java/externalreport/CheckstyleXmlReportReader.java b/external-reports/src/main/java/org/sonar/java/externalreport/CheckstyleXmlReportReader.java index 0931bf5ebaf..91d8f795de7 100644 --- a/external-reports/src/main/java/org/sonar/java/externalreport/CheckstyleXmlReportReader.java +++ b/external-reports/src/main/java/org/sonar/java/externalreport/CheckstyleXmlReportReader.java @@ -112,15 +112,15 @@ private void onErrorElement(StartElement element) { String source = getAttributeValue(element, SOURCE); String line = getAttributeValue(element, LINE); String message = getAttributeValue(element, MESSAGE); - if (!source.startsWith(CHECKSTYLE_PREFIX)) { - LOG.debug("Unexpected rule key without '{}' prefix: '{}'", CHECKSTYLE_PREFIX, source); + if (source.isEmpty()) { + LOG.debug("Unexpected error without rule key (missing 'source' attribute)."); return; } if (message.isEmpty()) { LOG.debug("Unexpected error without message for rule: '{}'", source); return; } - String key = source.substring(CHECKSTYLE_PREFIX.length()); + String key = source.startsWith(CHECKSTYLE_PREFIX) ? source.substring(CHECKSTYLE_PREFIX.length()) : source; consumer.onError(context, inputFile, key, line, message); } diff --git a/external-reports/src/test/java/org/sonar/java/externalreport/CheckstyleSensorTest.java b/external-reports/src/test/java/org/sonar/java/externalreport/CheckstyleSensorTest.java index 0fd5d200319..fb7576e7fa1 100644 --- a/external-reports/src/test/java/org/sonar/java/externalreport/CheckstyleSensorTest.java +++ b/external-reports/src/test/java/org/sonar/java/externalreport/CheckstyleSensorTest.java @@ -155,7 +155,7 @@ void no_issues_with_invalid_report(String fileName) throws IOException { @Test void issues_when_xml_file_has_errors() throws IOException { List externalIssues = executeSensorImporting("checkstyle-with-errors.xml"); - assertThat(externalIssues).hasSize(1); + assertThat(externalIssues).hasSize(2); ExternalIssue first = externalIssues.get(0); assertThat(first.primaryLocation().inputComponent().key()).isEqualTo("checkstyle-project:Main.java"); @@ -165,13 +165,22 @@ void issues_when_xml_file_has_errors() throws IOException { assertThat(first.primaryLocation().message()).isEqualTo("Error at file level with an unknown rule key."); assertThat(first.primaryLocation().textRange()).isNull(); + ExternalIssue second = externalIssues.get(1); + assertThat(second.primaryLocation().inputComponent().key()).isEqualTo("checkstyle-project:A.java"); + assertThat(second.engineId()).isEqualTo("checkstyle"); + assertThat(second.ruleId()).isEqualTo("com.example.CustomCheck"); + assertThat(second.ruleKey().rule()).isEqualTo("com.example.CustomCheck"); + assertThat(second.type()).isEqualTo(RuleType.CODE_SMELL); + assertThat(second.severity()).isEqualTo(Severity.MAJOR); + assertThat(second.primaryLocation().message()).isEqualTo("Custom rule issue."); + assertThat(second.primaryLocation().textRange().start().line()).isEqualTo(1); + assertThat(logTester.logs(Level.ERROR)).isEmpty(); assertThat(onlyOneLogElement(logTester.logs(Level.WARN))) .startsWith("No input file found for '") .endsWith("not-existing-file.java'. No checkstyle issues will be imported on this file."); - assertThat(logTester.logs(Level.DEBUG)).containsExactlyInAnyOrder( - "Unexpected error without message for rule: 'com.puppycrawl.tools.checkstyle.checks.ArrayTypeStyleCheck'", - "Unexpected rule key without 'com.puppycrawl.tools.checkstyle.checks.' prefix: 'invalid-format'"); + assertThat(logTester.logs(Level.DEBUG)).containsExactly( + "Unexpected error without message for rule: 'com.puppycrawl.tools.checkstyle.checks.ArrayTypeStyleCheck'"); } private List executeSensorImporting(@Nullable String fileName) throws IOException { diff --git a/external-reports/src/test/resources/checkstyle/checkstyle-with-errors.xml b/external-reports/src/test/resources/checkstyle/checkstyle-with-errors.xml index 7b4f6195342..d6e61fbb833 100644 --- a/external-reports/src/test/resources/checkstyle/checkstyle-with-errors.xml +++ b/external-reports/src/test/resources/checkstyle/checkstyle-with-errors.xml @@ -12,6 +12,6 @@ - +