PmdSensor implements Sensor { @Override public void execute(SensorContext context) { List<File> reportFiles = ExternalReportProvider.getReportFiles(context, REPORT_PROPERTY_KEY); reportFiles.forEach(report -> importIfExist(LINTER_NAME, context, report, PmdSensor::importReport)); }  @Override void describe(SensorDescriptor descriptor); @Override void execute(SensorContext context); static final String REPORT_PROPERTY_KEY; static final String LINTER_KEY; static final ExternalRuleLoader RULE_LOADER; }
@Test void no_report_path_set() throws IOException { List<ExternalIssue> externalIssues = execute(null); assertThat(externalIssues).isEmpty(); assertThat(logTester.logs()).isEmpty(); }
@Test void invalid_report_path() throws IOException { List<ExternalIssue> externalIssues = execute("invalid-path.txt"); assertThat(externalIssues).isEmpty(); assertThat(logTester.logs(LoggerLevel.ERROR)).isEmpty(); assertThat(onlyOneLogElement(logTester.logs(LoggerLevel.WARN))) .startsWith("PMD report not found: ") .endsWith("invalid-path.txt"); }
@Test void not_xml_report() throws IOException { List<ExternalIssue> externalIssues = execute("hello.txt"); assertThat(externalIssues).isEmpty(); assertThat(logTester.logs(LoggerLevel.ERROR).get(0)).startsWith("Failed to import external issues report:"); }
@Test void skip_issue_on_invalid_priority() throws IOException { List<ExternalIssue> externalIssues = execute("invalid-severity.xml"); assertThat(externalIssues).hasSize(1); assertThat(logTester.logs(LoggerLevel.WARN).get(0)) .contains("Can't import issue at line 8") .contains("invalid-severity.xml"); assertThat(logTester.logs(LoggerLevel.WARN).get(1)) .contains("Can't import issue at line 9") .contains("invalid-severity.xml"); }
@Test void invalid_text_range() throws IOException { List<ExternalIssue> externalIssues = execute("invalid-text-range.xml"); assertThat(externalIssues).hasSize(2); TextRange secondIssueRange = externalIssues.get(1).primaryLocation().textRange(); assertThat(secondIssueRange).isNotNull(); assertThat(secondIssueRange.start().line()).isEqualTo(4); assertThat(secondIssueRange.end().line()).isEqualTo(4); assertThat(logTester.logs(LoggerLevel.WARN).get(0)) .contains("Can't import issue at line 9") .contains("invalid-text-range.xml"); }
@Test void issues() throws IOException { List<ExternalIssue> externalIssues = execute("pmd-report.xml"); assertThat(externalIssues).hasSize(3); ExternalIssue first = externalIssues.get(0); ExternalIssueAssert.assertThat(first) .hasFileName(PROJECT_ID + ":file1.java") .hasEngineId("pmd") .hasRuleId("UnusedFormalParameter") .hasRuleKey("UnusedFormalParameter") .hasRuleType(RuleType.CODE_SMELL) .hasSeverity(Severity.MAJOR) .hasMessage("Avoid unused constructor parameters such as 'arg2'.") .hasTextRange(new DefaultTextRange(new DefaultTextPointer(3, 34), new DefaultTextPointer(3, 38))) .hasRemediationEffort(5) .verify(); ExternalIssue second = externalIssues.get(1); ExternalIssueAssert.assertThat(second) .hasFileName(PROJECT_ID + ":file1.java") .hasRuleKey("UnusedLocalVariable") .hasRuleType(RuleType.CODE_SMELL) .hasSeverity(Severity.MAJOR) .hasMessage("Avoid unused local variables such as 'x'.") .hasTextRange(new DefaultTextRange(new DefaultTextPointer(4, 8), new DefaultTextPointer(5, 10))) .hasRemediationEffort(5) .verify(); ExternalIssue third = externalIssues.get(2); ExternalIssueAssert.assertThat(third) .hasFileName(PROJECT_ID + ":file2.java") .hasRuleKey("UnusedPrivateMethod") .hasRuleType(RuleType.CODE_SMELL) .hasSeverity(Severity.CRITICAL) .hasMessage("Avoid unused private methods such as 'privateMethod()'.") .hasTextRangeStartLine(5) .hasRemediationEffort(5) .verify(); assertThat(logTester.logs(LoggerLevel.ERROR)).isEmpty(); assertThat(logTester.logs(LoggerLevel.WARN)).containsExactly("No input file found for unknown-file.java. No PMD issue will be imported on this file."); }