From: Julien Lancelot Date: Mon, 2 Feb 2015 09:29:58 +0000 (+0100) Subject: Set a hash even on empty file -> Will prevent NPE on empty file X-Git-Tag: 5.1-RC1~470 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=0565f090ed367130e201a3af8b87ea8a991bc537;p=sonarqube.git Set a hash even on empty file -> Will prevent NPE on empty file --- 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 4769bad4e56..b3b4d1469fd 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 @@ -28,12 +28,7 @@ import org.apache.commons.io.input.BOMInputStream; import javax.annotation.CheckForNull; -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.io.*; import java.nio.ByteBuffer; import java.nio.CharBuffer; import java.nio.charset.Charset; @@ -114,23 +109,20 @@ class FileMetadata { private class FileHashComputer extends CharHandler { private MessageDigest globalMd5Digest = DigestUtils.getMd5Digest(); - private boolean emptyFile = true;; @Override void handleIgnoreEoL(char c) { - emptyFile = false; updateDigestUTF8Char(c, globalMd5Digest); } @Override void newLine() { - emptyFile = false; updateDigestUTF8Char(LINE_FEED, globalMd5Digest); } @CheckForNull public String getHash() { - return emptyFile ? null : Hex.encodeHexString(globalMd5Digest.digest()); + return Hex.encodeHexString(globalMd5Digest.digest()); } } diff --git a/sonar-batch/src/test/java/org/sonar/batch/index/SourcePersisterTest.java b/sonar-batch/src/test/java/org/sonar/batch/index/SourcePersisterTest.java index 1aa65d51b20..b23b40aa541 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/index/SourcePersisterTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/index/SourcePersisterTest.java @@ -180,7 +180,7 @@ public class SourcePersisterTest extends AbstractDaoTestCase { String relativePathEmpty = "src/empty.java"; DefaultInputFile inputFileEmpty = new DefaultInputFile(PROJECT_KEY, relativePathEmpty) .setLines(0) - .setHash(null) + .setHash("abcd") .setLineHashes(new byte[][] {}); when(inputPathCache.all()).thenReturn(Arrays.asList(inputFileEmpty)); 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 ad06995c622..cbfcb03242b 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 @@ -48,7 +48,7 @@ public class FileMetadataTest { FileMetadata.Metadata metadata = new FileMetadata().read(tempFile, Charsets.UTF_8); assertThat(metadata.lines).isEqualTo(1); assertThat(metadata.nonBlankLines).isEqualTo(0); - assertThat(metadata.hash).isNull(); + assertThat(metadata.hash).isNotEmpty(); assertThat(metadata.originalLineOffsets).containsOnly(0); assertThat(metadata.lineHashes[0]).isNull(); assertThat(metadata.empty).isTrue(); diff --git a/sonar-batch/src/test/resources/org/sonar/batch/index/SourcePersisterTest/testPersistEmptyFile-result.xml b/sonar-batch/src/test/resources/org/sonar/batch/index/SourcePersisterTest/testPersistEmptyFile-result.xml index f1ed70ee380..184e155cb02 100644 --- a/sonar-batch/src/test/resources/org/sonar/batch/index/SourcePersisterTest/testPersistEmptyFile-result.xml +++ b/sonar-batch/src/test/resources/org/sonar/batch/index/SourcePersisterTest/testPersistEmptyFile-result.xml @@ -7,8 +7,8 @@ created_at="1412952242000" updated_at="1412952242000" />