From 9391fdfd8207d9fb3adba94024528dd3890ceee3 Mon Sep 17 00:00:00 2001 From: =?utf8?q?S=C3=A9bastien=20Lesaint?= Date: Wed, 4 May 2016 13:54:41 +0200 Subject: [PATCH] SONAR-7002 better exception message when decoding FileSources.DATA --- .../step/PersistFileSourcesStepTest.java | 12 ++++----- .../org/sonar/db/source/FileSourceDto.java | 25 +++++++++++++------ .../sonar/db/source/FileSourceDtoTest.java | 21 ++++++++++++++++ 3 files changed, 44 insertions(+), 14 deletions(-) diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/step/PersistFileSourcesStepTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/step/PersistFileSourcesStepTest.java index ec17dbd40df..c1c4a9964df 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/step/PersistFileSourcesStepTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/step/PersistFileSourcesStepTest.java @@ -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); 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(); + } } -- 2.39.5