]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-7002 better exception message when decoding FileSources.DATA 928/head
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Wed, 4 May 2016 11:54:41 +0000 (13:54 +0200)
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Tue, 10 May 2016 07:06:08 +0000 (09:06 +0200)
server/sonar-server/src/test/java/org/sonar/server/computation/step/PersistFileSourcesStepTest.java
sonar-db/src/main/java/org/sonar/db/source/FileSourceDto.java
sonar-db/src/test/java/org/sonar/db/source/FileSourceDtoTest.java

index ec17dbd40df60a3d991d699ee7a21439c3913a75..c1c4a9964dfd7e6757aa040b109512e62638999f 100644 (file)
@@ -111,7 +111,7 @@ public class PersistFileSourcesStepTest extends BaseStepTest {
     assertThat(fileSourceDto.getCreatedAt()).isEqualTo(NOW);
     assertThat(fileSourceDto.getUpdatedAt()).isEqualTo(NOW);
 
-    DbFileSources.Data data = FileSourceDto.decodeSourceData(fileSourceDto.getBinaryData());
+    DbFileSources.Data data = fileSourceDto.getSourceData();
     assertThat(data.getLinesCount()).isEqualTo(2);
     assertThat(data.getLines(0).getLine()).isEqualTo(1);
     assertThat(data.getLines(0).getSource()).isEqualTo("line1");
@@ -149,7 +149,7 @@ public class PersistFileSourcesStepTest extends BaseStepTest {
 
     assertThat(dbTester.countRowsOfTable("file_sources")).isEqualTo(1);
     FileSourceDto fileSourceDto = dbClient.fileSourceDao().selectSourceByFileUuid(session, FILE_UUID);
-    DbFileSources.Data data = FileSourceDto.decodeSourceData(fileSourceDto.getBinaryData());
+    DbFileSources.Data data = fileSourceDto.getSourceData();
 
     assertThat(data.getLinesList()).hasSize(1);
 
@@ -180,7 +180,7 @@ public class PersistFileSourcesStepTest extends BaseStepTest {
 
     assertThat(fileSourceDto.getRevision()).isEqualTo("rev-1");
 
-    DbFileSources.Data data = FileSourceDto.decodeSourceData(fileSourceDto.getBinaryData());
+    DbFileSources.Data data = fileSourceDto.getSourceData();
 
     assertThat(data.getLinesList()).hasSize(1);
 
@@ -205,7 +205,7 @@ public class PersistFileSourcesStepTest extends BaseStepTest {
 
     assertThat(dbTester.countRowsOfTable("file_sources")).isEqualTo(1);
     FileSourceDto fileSourceDto = dbClient.fileSourceDao().selectSourceByFileUuid(session, FILE_UUID);
-    DbFileSources.Data data = FileSourceDto.decodeSourceData(fileSourceDto.getBinaryData());
+    DbFileSources.Data data = fileSourceDto.getSourceData();
 
     assertThat(data.getLinesList()).hasSize(1);
 
@@ -230,7 +230,7 @@ public class PersistFileSourcesStepTest extends BaseStepTest {
 
     assertThat(dbTester.countRowsOfTable("file_sources")).isEqualTo(1);
     FileSourceDto fileSourceDto = dbClient.fileSourceDao().selectSourceByFileUuid(session, FILE_UUID);
-    DbFileSources.Data data = FileSourceDto.decodeSourceData(fileSourceDto.getBinaryData());
+    DbFileSources.Data data = fileSourceDto.getSourceData();
 
     assertThat(data.getLinesList()).hasSize(3);
 
@@ -251,7 +251,7 @@ public class PersistFileSourcesStepTest extends BaseStepTest {
 
     assertThat(dbTester.countRowsOfTable("file_sources")).isEqualTo(1);
     FileSourceDto fileSourceDto = dbClient.fileSourceDao().selectSourceByFileUuid(session, FILE_UUID);
-    DbFileSources.Data data = FileSourceDto.decodeSourceData(fileSourceDto.getBinaryData());
+    DbFileSources.Data data = fileSourceDto.getSourceData();
 
     assertThat(data.getLinesList()).hasSize(1);
 
index da85e1014dbe4dad1170051dc651cd19a68a9fba..bf568bb1c90206042cb47f8b81a54b4aef233308 100644 (file)
@@ -32,6 +32,8 @@ import net.jpountz.lz4.LZ4BlockOutputStream;
 import org.apache.commons.io.IOUtils;
 import org.sonar.db.protobuf.DbFileSources;
 
+import static java.lang.String.format;
+
 public class FileSourceDto {
 
   private Long id;
@@ -86,9 +88,14 @@ public class FileSourceDto {
     return this;
   }
 
-  public static DbFileSources.Data decodeSourceData(byte[] binaryData) {
-    // stream is always closed
-    return decodeSourceData(new ByteArrayInputStream(binaryData));
+  public DbFileSources.Data decodeSourceData(byte[] binaryData) {
+    try {
+      return decodeSourceDataImpl(new ByteArrayInputStream(binaryData));
+    } catch (IOException e) {
+      throw new IllegalStateException(
+        format("Fail to decompress and deserialize source data [id=%s,fileUuid=%s,projectUuid=%s]", id, fileUuid, projectUuid),
+        e);
+    }
   }
 
   /**
@@ -96,14 +103,16 @@ public class FileSourceDto {
    * The parameter "input" is always closed by this method.
    */
   public static DbFileSources.Data decodeSourceData(InputStream binaryInput) {
-    LZ4BlockInputStream lz4Input = null;
     try {
-      lz4Input = new LZ4BlockInputStream(binaryInput);
-      return DbFileSources.Data.parseFrom(lz4Input);
+      return decodeSourceDataImpl(binaryInput);
     } catch (IOException e) {
       throw new IllegalStateException("Fail to decompress and deserialize source data", e);
-    } finally {
-      IOUtils.closeQuietly(lz4Input);
+    }
+  }
+
+  private static DbFileSources.Data decodeSourceDataImpl(InputStream binaryInput) throws IOException {
+    try (LZ4BlockInputStream lz4Input = new LZ4BlockInputStream(binaryInput)) {
+      return DbFileSources.Data.parseFrom(lz4Input);
     }
   }
 
index 15457ca0aea140e67e48cc879c54a487951aedbb..0d8dd7b29eef3d16ba6da7ad055742b50655ffb1 100644 (file)
@@ -21,12 +21,16 @@ package org.sonar.db.source;
 
 import java.util.Arrays;
 import java.util.List;
+import org.junit.Rule;
 import org.junit.Test;
+import org.junit.rules.ExpectedException;
 import org.sonar.db.protobuf.DbFileSources;
 
 import static org.assertj.core.api.Assertions.assertThat;
 
 public class FileSourceDtoTest {
+  @Rule
+  public ExpectedException expectedException = ExpectedException.none();
 
   @Test
   public void encode_and_decode_test_data() {
@@ -43,4 +47,21 @@ public class FileSourceDtoTest {
     assertThat(underTest.getTestData()).hasSize(2);
     assertThat(underTest.getTestData().get(0).getName()).isEqualTo("name#1");
   }
+
+  @Test
+  public void getSourceData_throws_ISE_with_id_fileUuid_and_projectUuid_in_message_when_data_cant_be_read() {
+    long id = 12L;
+    String fileUuid = "file uuid";
+    String projectUuid = "project uuid";
+    FileSourceDto underTest = new FileSourceDto()
+        .setBinaryData(new byte[]{1, 2, 3, 4, 5})
+        .setId(id)
+        .setFileUuid(fileUuid)
+        .setProjectUuid(projectUuid);
+
+    expectedException.expect(IllegalStateException.class);
+    expectedException.expectMessage("Fail to decompress and deserialize source data [id=" + id + ",fileUuid=" + fileUuid + ",projectUuid=" + projectUuid + "]");
+
+    underTest.getSourceData();
+  }
 }