aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJulien HENRY <julien.henry@sonarsource.com>2014-10-09 15:09:43 +0200
committerJulien HENRY <julien.henry@sonarsource.com>2014-10-09 15:09:43 +0200
commit6aeb90e78f84b5cd6060ea1a8843be8b9eabe2f1 (patch)
treef7edb67b43bdcb11effde52606644518165ece90
parent50cc8ffd0be7c535c816659c54a46eb19df0f7d9 (diff)
downloadsonarqube-6aeb90e78f84b5cd6060ea1a8843be8b9eabe2f1.tar.gz
sonarqube-6aeb90e78f84b5cd6060ea1a8843be8b9eabe2f1.zip
Fix line count when file starts with a new line
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/FileMetadata.java15
-rw-r--r--sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/FileMetadataTest.java11
2 files changed, 21 insertions, 5 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 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
@@ -112,6 +113,16 @@ public class FileMetadataTest {
}
@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();
File file = new File(tempFolder, "doesNotExist.txt");