From c83cb3b0e6f90040cd073e0cf9753657e6e24ec6 Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Thu, 20 Feb 2014 15:46:52 +0100 Subject: [PATCH] SONAR-926 Fix computation of number of lines when the latest line is empty --- .../batch/scan/filesystem/FileMetadata.java | 2 +- .../batch/scan/filesystem/FileMetadataTest.java | 16 +++++++++++++--- 2 files changed, 14 insertions(+), 4 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 166db70aecc..28e7825ac96 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 @@ -77,7 +77,7 @@ class FileMetadata { md5Digest.update(charToBytesUTF(c)); i = reader.read(); } - if (c != LINE_FEED) { + if (c != (char)-1) { lines++; } String hash = Hex.encodeHexString(md5Digest.digest()); 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 2a92f70d84a..5af76f92bb1 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 @@ -41,6 +41,16 @@ public class FileMetadataTest { @Rule public TemporaryFolder temp = new TemporaryFolder(); + @Test + public void empty_file() throws Exception { + File tempFile = temp.newFile(); + FileUtils.touch(tempFile); + + FileMetadata.Metadata metadata = FileMetadata.INSTANCE.read(tempFile, Charsets.UTF_8); + assertThat(metadata.lines).isEqualTo(0); + assertThat(metadata.hash).isNotEmpty(); + } + @Test public void windows_without_latest_eol() throws Exception { File tempFile = temp.newFile(); @@ -57,7 +67,7 @@ public class FileMetadataTest { FileUtils.write(tempFile, "foo\r\nbar\r\nbaz\r\n", Charsets.UTF_8, true); FileMetadata.Metadata metadata = FileMetadata.INSTANCE.read(tempFile, Charsets.UTF_8); - assertThat(metadata.lines).isEqualTo(3); + assertThat(metadata.lines).isEqualTo(4); assertThat(metadata.hash).isEqualTo(EXPECTED_HASH_WITH_LATEST_EOL); } @@ -77,7 +87,7 @@ public class FileMetadataTest { FileUtils.write(tempFile, "foo\nbar\nbaz\n", Charsets.UTF_8, true); FileMetadata.Metadata metadata = FileMetadata.INSTANCE.read(tempFile, Charsets.UTF_8); - assertThat(metadata.lines).isEqualTo(3); + assertThat(metadata.lines).isEqualTo(4); assertThat(metadata.hash).isEqualTo(EXPECTED_HASH_WITH_LATEST_EOL); } @@ -87,7 +97,7 @@ public class FileMetadataTest { FileUtils.write(tempFile, "foo\nbar\r\nbaz\n", Charsets.UTF_8, true); FileMetadata.Metadata metadata = FileMetadata.INSTANCE.read(tempFile, Charsets.UTF_8); - assertThat(metadata.lines).isEqualTo(3); + assertThat(metadata.lines).isEqualTo(4); assertThat(metadata.hash).isEqualTo(EXPECTED_HASH_WITH_LATEST_EOL); } -- 2.39.5