From: Julien HENRY Date: Thu, 9 Oct 2014 13:09:43 +0000 (+0200) Subject: Fix line count when file starts with a new line X-Git-Tag: 4.5.1-RC1~49 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=6aeb90e78f84b5cd6060ea1a8843be8b9eabe2f1;p=sonarqube.git Fix line count when file starts with a new line --- 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 95c6ed4cc3b..4759db79b68 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 @@ -23,7 +23,12 @@ import org.apache.commons.codec.binary.Hex; import org.apache.commons.codec.digest.DigestUtils; import org.apache.commons.io.IOUtils; -import java.io.*; +import java.io.BufferedReader; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.Reader; import java.nio.charset.Charset; import java.security.MessageDigest; @@ -50,13 +55,13 @@ class FileMetadata { Metadata read(File file, Charset encoding) { Reader reader = null; int lines = 0; - char c = (char)-1; + char c = (char) -1; try { MessageDigest md5Digest = DigestUtils.getMd5Digest(); md5Digest.reset(); reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), encoding)); int i = reader.read(); - boolean afterCR = true; + boolean afterCR = false; while (i != -1) { c = (char) i; if (afterCR) { @@ -77,7 +82,7 @@ class FileMetadata { md5Digest.update(charToBytesUTF(c)); i = reader.read(); } - if (c != (char)-1) { + if (c != (char) -1) { lines++; } String hash = Hex.encodeHexString(md5Digest.digest()); @@ -91,7 +96,7 @@ class FileMetadata { } private byte[] charToBytesUTF(char c) { - char[] buffer = new char[]{c}; + char[] buffer = new char[] {c}; byte[] b = new byte[buffer.length << 1]; for (int i = 0; i < buffer.length; i++) { int bpos = i << 1; 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 749a87408d3..2b4c64df8a0 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 @@ -33,6 +33,7 @@ import static org.fest.assertions.Assertions.assertThat; public class FileMetadataTest { private static final String EXPECTED_HASH_WITHOUT_LATEST_EOL = "c80cc50d65ace6c4eb63f189d274dbeb"; + private static final String EXPECTED_HASH_NEW_LINE_FIRST = "cf2d41454b5b451eeb5122f0848c1d2a"; private static final String EXPECTED_HASH_WITH_LATEST_EOL = "bf77e51d219e7d7d643faac86f1b5d15"; @Rule @@ -111,6 +112,16 @@ public class FileMetadataTest { assertThat(metadata.hash).isEqualTo(EXPECTED_HASH_WITHOUT_LATEST_EOL); } + @Test + public void start_with_newline() throws Exception { + File tempFile = temp.newFile(); + FileUtils.write(tempFile, "\nfoo\nbar\r\nbaz", Charsets.UTF_8, true); + + FileMetadata.Metadata metadata = FileMetadata.INSTANCE.read(tempFile, Charsets.UTF_8); + assertThat(metadata.lines).isEqualTo(4); + assertThat(metadata.hash).isEqualTo(EXPECTED_HASH_NEW_LINE_FIRST); + } + @Test public void should_throw_if_file_does_not_exist() throws Exception { File tempFolder = temp.newFolder();