From 23176256275964c2c5587ac87fd4ce2d50d4fab1 Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Thu, 20 Feb 2014 17:25:28 +0100 Subject: [PATCH] SONAR-926 Add test on file hash --- .../batch/scan/filesystem/FileMetadata.java | 4 ++-- .../scan/filesystem/FileMetadataTest.java | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/FileMetadata.java b/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/FileMetadata.java index 28e7825ac96..54f7c0e02d9 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/FileMetadata.java +++ b/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/FileMetadata.java @@ -64,14 +64,14 @@ class FileMetadata { if (c == LINE_FEED) { // Ignore i = reader.read(); - lines++; continue; } } if (c == CARRIAGE_RETURN) { afterCR = true; c = LINE_FEED; - } else if (c == LINE_FEED) { + } + if (c == LINE_FEED) { lines++; } md5Digest.update(charToBytesUTF(c)); diff --git a/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/FileMetadataTest.java b/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/FileMetadataTest.java index 5af76f92bb1..deb08c8917b 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/FileMetadataTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/FileMetadataTest.java @@ -121,4 +121,23 @@ public class FileMetadataTest { FileMetadata.INSTANCE.read(file, Charsets.UTF_8); } + + @Test + public void line_feed_is_included_into_hash() throws Exception { + File file1 = temp.newFile(); + FileUtils.write(file1, "foo\nbar\n", Charsets.UTF_8, true); + + // same as file1, except an additional return carriage + File file1a = temp.newFile(); + FileUtils.write(file1a, "foo\r\nbar\n", Charsets.UTF_8, true); + + File file2 = temp.newFile(); + FileUtils.write(file2, "foo\nbar", Charsets.UTF_8, true); + + String hash1 = FileMetadata.INSTANCE.read(file1, Charsets.UTF_8).hash; + String hash1a = FileMetadata.INSTANCE.read(file1a, Charsets.UTF_8).hash; + String hash2 = FileMetadata.INSTANCE.read(file2, Charsets.UTF_8).hash; + assertThat(hash1).isEqualTo(hash1a); + assertThat(hash1).isNotEqualTo(hash2); + } } -- 2.39.5