]> source.dussan.org Git - sonarqube.git/commitdiff
Set a hash even on empty file -> Will prevent NPE on empty file
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Mon, 2 Feb 2015 09:29:58 +0000 (10:29 +0100)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Mon, 2 Feb 2015 09:30:11 +0000 (10:30 +0100)
sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/FileMetadata.java
sonar-batch/src/test/java/org/sonar/batch/index/SourcePersisterTest.java
sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/FileMetadataTest.java
sonar-batch/src/test/resources/org/sonar/batch/index/SourcePersisterTest/testPersistEmptyFile-result.xml

index 4769bad4e563eb68288969de608a9c0ae6952ca4..b3b4d1469fdb9753f480524af9f4097199fed162 100644 (file)
@@ -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());
     }
   }
 
index 1aa65d51b20199285d9a2d1d33feb1c0c6ee2122..b23b40aa5418f0e7693b670c2ae488111aa52a6b 100644 (file)
@@ -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.<InputPath>asList(inputFileEmpty));
 
index ad06995c6220ea42992c192bc027262e65d55575..cbfcb03242bce927e64fb64062dcfdecdcaf9070 100644 (file)
@@ -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();
index f1ed70ee380111c7e7b1474c976374c0c16f85c8..184e155cb024f350c1e39543b38d5a8dfa584d4a 100644 (file)
@@ -7,8 +7,8 @@
       created_at="1412952242000" updated_at="1412952242000" />
       
     <file_sources id="102" project_uuid="projectUuid" file_uuid="uuidempty" data="[null]"
-       line_hashes="[null]" 
-        src_hash="[null]"
+       line_hashes="[null]"
+       src_hash="abcd"
        data_hash="0" created_at="1414597442000" updated_at="1414597442000" />
 </dataset>