diff options
author | Sébastien Lesaint <sebastien.lesaint@sonarsource.com> | 2016-05-04 13:54:41 +0200 |
---|---|---|
committer | Sébastien Lesaint <sebastien.lesaint@sonarsource.com> | 2016-05-10 09:06:08 +0200 |
commit | 9391fdfd8207d9fb3adba94024528dd3890ceee3 (patch) | |
tree | f1c155a8eb206d729f796c46ff076304a6e8f296 /sonar-db | |
parent | 451e2fce1f097daff6c82b56ad31fed6cf5ac709 (diff) | |
download | sonarqube-9391fdfd8207d9fb3adba94024528dd3890ceee3.tar.gz sonarqube-9391fdfd8207d9fb3adba94024528dd3890ceee3.zip |
SONAR-7002 better exception message when decoding FileSources.DATA
Diffstat (limited to 'sonar-db')
-rw-r--r-- | sonar-db/src/main/java/org/sonar/db/source/FileSourceDto.java | 25 | ||||
-rw-r--r-- | sonar-db/src/test/java/org/sonar/db/source/FileSourceDtoTest.java | 21 |
2 files changed, 38 insertions, 8 deletions
diff --git a/sonar-db/src/main/java/org/sonar/db/source/FileSourceDto.java b/sonar-db/src/main/java/org/sonar/db/source/FileSourceDto.java index da85e1014db..bf568bb1c90 100644 --- a/sonar-db/src/main/java/org/sonar/db/source/FileSourceDto.java +++ b/sonar-db/src/main/java/org/sonar/db/source/FileSourceDto.java @@ -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); } } diff --git a/sonar-db/src/test/java/org/sonar/db/source/FileSourceDtoTest.java b/sonar-db/src/test/java/org/sonar/db/source/FileSourceDtoTest.java index 15457ca0aea..0d8dd7b29ee 100644 --- a/sonar-db/src/test/java/org/sonar/db/source/FileSourceDtoTest.java +++ b/sonar-db/src/test/java/org/sonar/db/source/FileSourceDtoTest.java @@ -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(); + } } |