diff --git a/src/main/java/edu/hm/hafner/analysis/FingerprintGenerator.java b/src/main/java/edu/hm/hafner/analysis/FingerprintGenerator.java index 1405268a0..d9cbbd5d2 100644 --- a/src/main/java/edu/hm/hafner/analysis/FingerprintGenerator.java +++ b/src/main/java/edu/hm/hafner/analysis/FingerprintGenerator.java @@ -7,7 +7,10 @@ import java.nio.charset.MalformedInputException; import java.nio.file.InvalidPathException; import java.nio.file.NoSuchFileException; +import java.util.Set; +import org.apache.commons.io.FilenameUtils; +import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.builder.HashCodeBuilder; import edu.hm.hafner.util.FilteredLog; @@ -19,6 +22,9 @@ * @author Ullrich Hafner */ public class FingerprintGenerator { + private static final Set NON_SOURCE_CODE_EXTENSIONS = Set.of( + "o", "exe", "dll", "so", "a", "lib", "jar", "war", "zip", "7z", "gz", "bz2"); + /** * Creates fingerprints for the specified set of issues. * @@ -34,14 +40,22 @@ public void run(final FullTextFingerprint algorithm, final Report report, final int sum = 0; for (Issue issue : report) { if (!issue.hasFingerprint()) { - sum += computeFingerprint(issue, algorithm, charset, log); + if (hasAllowedExtension(issue.getFileName())) { + sum += computeFingerprint(issue, algorithm, charset, log); + } + else { + issue.setFingerprint(createDefaultFingerprint(issue)); + } } } - log.logSummary(); report.mergeLogMessages(log); report.logInfo("-> created fingerprints for %d issues (skipped %d issues)", sum, report.size() - sum); } + private boolean hasAllowedExtension(final String fileName) { + return !NON_SOURCE_CODE_EXTENSIONS.contains(StringUtils.lowerCase(FilenameUtils.getExtension(fileName))); + } + private int computeFingerprint(final Issue issue, final FullTextFingerprint algorithm, final Charset charset, final FilteredLog log) { String absolutePath = issue.getAbsolutePath(); diff --git a/src/test/java/edu/hm/hafner/analysis/FingerprintGeneratorTest.java b/src/test/java/edu/hm/hafner/analysis/FingerprintGeneratorTest.java index f60f48d14..298d00e30 100644 --- a/src/test/java/edu/hm/hafner/analysis/FingerprintGeneratorTest.java +++ b/src/test/java/edu/hm/hafner/analysis/FingerprintGeneratorTest.java @@ -38,7 +38,7 @@ void shouldSkipFingerprintingIfEncodingIsWrong() throws IOException { FileSystem fileSystem = mock(FileSystem.class); when(fileSystem.readLinesFromFile(anyString(), any())) - .thenThrow(new UncheckedIOException(new MalformedInputException(1))); + .thenThrow(new UncheckedIOException(new MalformedInputException(1))); generator.run(new FullTextFingerprint(fileSystem), report, CHARSET_AFFECTED_FILE); @@ -59,7 +59,7 @@ void shouldNotChangeIssuesWithFingerPrint() { String alreadySet = "already-set"; report.add(issueBuilder.setFingerprint(alreadySet).setMessage(AFFECTED_FILE_NAME).build()); - generator.run(createFullTextFingerprint("fingerprint-one.txt", "fingerprint-two.txt"), + generator.run(createFullTextFingerprint("fingerprint-two.txt"), report, CHARSET_AFFECTED_FILE); assertThat(report.get(0).hasFingerprint()).isTrue(); @@ -69,22 +69,24 @@ void shouldNotChangeIssuesWithFingerPrint() { @Test void shouldSetDefaultFingerprintIfNoFileIsGiven() { - FingerprintGenerator generator = new FingerprintGenerator(); + try (IssueBuilder issueBuilder = new IssueBuilder()) { + FingerprintGenerator generator = new FingerprintGenerator(); - Report report = new Report(); - report.add(new IssueBuilder().build()); + Report report = new Report(); + report.add(issueBuilder.build()); - generator.run(new FullTextFingerprint(), report, CHARSET_AFFECTED_FILE); + generator.run(new FullTextFingerprint(), report, CHARSET_AFFECTED_FILE); - assertThatIssueHasDefaultFingerprint(report); - assertThat(report.getErrorMessages()).isEmpty(); + assertThatIssueHasDefaultFingerprint(report); + assertThat(report.getErrorMessages()).isEmpty(); + } } @Test void shouldAssignIdenticalFingerprint() { Report report = createTwoIssues(); FingerprintGenerator generator = new FingerprintGenerator(); - FullTextFingerprint fingerprint = createFullTextFingerprint("fingerprint-one.txt", "fingerprint-one.txt"); + FullTextFingerprint fingerprint = createFullTextFingerprint("fingerprint-one.txt"); generator.run(fingerprint, report, CHARSET_AFFECTED_FILE); @@ -117,7 +119,7 @@ private FileSystem stubFileSystem(final String firstFile, final String secondFil void shouldAssignDifferentFingerprint() { Report report = createTwoIssues(); FingerprintGenerator generator = new FingerprintGenerator(); - FullTextFingerprint fingerprint = createFullTextFingerprint("fingerprint-one.txt", "fingerprint-two.txt"); + FullTextFingerprint fingerprint = createFullTextFingerprint("fingerprint-two.txt"); generator.run(fingerprint, report, CHARSET_AFFECTED_FILE); @@ -128,14 +130,10 @@ void shouldAssignDifferentFingerprint() { assertThat(referenceIssue.getFingerprint()).isNotEqualTo(currentIssue.getFingerprint()); } - @ParameterizedTest(name = "[{index}] Illegal filename") + @ParameterizedTest(name = "[{index}] Illegal filename {0}") @ValueSource(strings = {"/does/not/exist", "!<>$&/&(", "\0 Null-Byte"}) void shouldUseFallbackFingerprintOnError(final String fileName) { - Report report; - try (IssueBuilder issueBuilder = new IssueBuilder()) { - report = new Report(); - report.add(issueBuilder.setFileName(fileName).build()); - } + var report = createReportWithOneIssueFor(fileName); FingerprintGenerator generator = new FingerprintGenerator(); generator.run(new FullTextFingerprint(), report, CHARSET_AFFECTED_FILE); @@ -143,12 +141,36 @@ void shouldUseFallbackFingerprintOnError(final String fileName) { assertThatIssueHasDefaultFingerprint(report); } + @ParameterizedTest(name = "[{index}] Skip non source code file {0}") + @ValueSource(strings = {"library.o", "program.exe", "library.dll", "program.so", "library.a", "program.lib", + "library.jar", "library.war", "program.zip", "library.7z", "program.tar.gz", "library.tar.bz2", + "UPPER_CASE.EXE"}) + void shouldUseFallbackFingerprintOnNonSourceFiles(final String fileName) { + var report = createReportWithOneIssueFor(fileName); + + FingerprintGenerator generator = new FingerprintGenerator(); + generator.run(createFullTextFingerprint("fingerprint-two.txt"), + report, CHARSET_AFFECTED_FILE); + + assertThatIssueHasDefaultFingerprint(report); + } + + private Report createReportWithOneIssueFor(final String fileName) { + Report report; + try (IssueBuilder issueBuilder = new IssueBuilder()) { + report = new Report(); + report.add(issueBuilder.setFileName(fileName).build()); + } + return report; + } + private void assertThatIssueHasDefaultFingerprint(final Report report) { assertThat(report.get(0)).hasFingerprint(FingerprintGenerator.createDefaultFingerprint(report.get(0))); } - private FullTextFingerprint createFullTextFingerprint(final String firstFile, final String secondFile) { - FileSystem fileSystem = stubFileSystem(firstFile, secondFile); + private FullTextFingerprint createFullTextFingerprint(final String secondFile) { + FileSystem fileSystem = stubFileSystem("fingerprint-one.txt", secondFile); + return new FullTextFingerprint(fileSystem); }