]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-926 Add test on file hash
authorSimon Brandhof <simon.brandhof@gmail.com>
Thu, 20 Feb 2014 16:25:28 +0000 (17:25 +0100)
committerSimon Brandhof <simon.brandhof@gmail.com>
Thu, 20 Feb 2014 16:25:28 +0000 (17:25 +0100)
sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/FileMetadata.java
sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/FileMetadataTest.java

index 28e7825ac969abe1ec4a578486cc3e0711afe9ac..54f7c0e02d9a0f8fde7b27c99975666eb713341c 100644 (file)
@@ -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));
index 5af76f92bb14c64e34869ec507c60d801e6fdc2f..deb08c8917b11c0f60e86912a7f898166b52d37f 100644 (file)
@@ -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);
+  }
 }