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");
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);
assertThat(fileSourceDto.getRevision()).isEqualTo("rev-1");
- DbFileSources.Data data = FileSourceDto.decodeSourceData(fileSourceDto.getBinaryData());
+ DbFileSources.Data data = fileSourceDto.getSourceData();
assertThat(data.getLinesList()).hasSize(1);
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);
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);
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);
import org.apache.commons.io.IOUtils;
import org.sonar.db.protobuf.DbFileSources;
+import static java.lang.String.format;
+
public class FileSourceDto {
private Long id;
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);
+ }
}
/**
* 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);
}
}
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() {
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();
+ }
}