diff options
author | Simon Brandhof <simon.brandhof@gmail.com> | 2014-02-20 15:46:52 +0100 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@gmail.com> | 2014-02-20 15:46:52 +0100 |
commit | c83cb3b0e6f90040cd073e0cf9753657e6e24ec6 (patch) | |
tree | dfe30b34b49cccd193cecba4b93b7d95beaa730b /sonar-batch | |
parent | 5d4f33d87453a9737b7716d9d3b55b3300c03108 (diff) | |
download | sonarqube-c83cb3b0e6f90040cd073e0cf9753657e6e24ec6.tar.gz sonarqube-c83cb3b0e6f90040cd073e0cf9753657e6e24ec6.zip |
SONAR-926 Fix computation of number of lines when the latest line is empty
Diffstat (limited to 'sonar-batch')
-rw-r--r-- | sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/FileMetadata.java | 2 | ||||
-rw-r--r-- | sonar-batch/src/test/java/org/sonar/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 @@ -42,6 +42,16 @@ public class FileMetadataTest { 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(); FileUtils.write(tempFile, "foo\r\nbar\r\nbaz", Charsets.UTF_8, true); @@ -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); } |