From 82c88c710622c568d91246e2e82cbe4e7be88128 Mon Sep 17 00:00:00 2001 From: =?utf8?q?S=C3=A9bastien=20Lesaint?= Date: Wed, 28 Sep 2016 17:19:26 +0200 Subject: [PATCH] SONAR-8185 FileSourceDao.readLineHashesStream supports null in DB --- .../org/sonar/db/source/FileSourceDao.java | 4 ++- .../sonar/db/source/FileSourceDaoTest.java | 34 ++++++++++++++++++- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/sonar-db/src/main/java/org/sonar/db/source/FileSourceDao.java b/sonar-db/src/main/java/org/sonar/db/source/FileSourceDao.java index efc7665bda2..a5df02d0e73 100644 --- a/sonar-db/src/main/java/org/sonar/db/source/FileSourceDao.java +++ b/sonar-db/src/main/java/org/sonar/db/source/FileSourceDao.java @@ -97,7 +97,9 @@ public class FileSourceDao implements Dao { rs = pstmt.executeQuery(); if (rs.next()) { reader = rs.getCharacterStream(1); - function.apply(reader); + if (reader != null) { + function.apply(reader); + } } } catch (SQLException e) { throw new IllegalStateException("Fail to read FILE_SOURCES.LINE_HASHES of file " + fileUuid, e); diff --git a/sonar-db/src/test/java/org/sonar/db/source/FileSourceDaoTest.java b/sonar-db/src/test/java/org/sonar/db/source/FileSourceDaoTest.java index edb21c475ea..d12cc86c0c0 100644 --- a/sonar-db/src/test/java/org/sonar/db/source/FileSourceDaoTest.java +++ b/sonar-db/src/test/java/org/sonar/db/source/FileSourceDaoTest.java @@ -20,8 +20,11 @@ package org.sonar.db.source; import com.google.common.base.Function; +import com.google.common.base.Throwables; import java.io.IOException; +import java.io.OutputStreamWriter; import java.io.Reader; +import javax.annotation.Nullable; import org.apache.commons.io.IOUtils; import org.junit.Rule; import org.junit.Test; @@ -31,6 +34,8 @@ import org.sonar.db.DbTester; import org.sonar.db.source.FileSourceDto.Type; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.fail; +import static org.assertj.core.api.Assertions.in; public class FileSourceDaoTest { @@ -110,7 +115,6 @@ public class FileSourceDaoTest { @Test public void selectLineHashes_does_not_fail_when_lineshashes_is_null() { - dbTester.prepareDbUnit(getClass(), "shared.xml"); underTest.insert(new FileSourceDto() @@ -127,6 +131,34 @@ public class FileSourceDaoTest { assertThat(underTest.selectLineHashes(dbTester.getSession(), "FILE2_UUID")).isEmpty(); } + @Test + public void readLineHashesStream_does_not_fail_when_lineshashes_is_null() { + dbTester.prepareDbUnit(getClass(), "shared.xml"); + + underTest.insert(new FileSourceDto() + .setProjectUuid("PRJ_UUID") + .setFileUuid("FILE2_UUID") + .setBinaryData("FILE2_BINARY_DATA".getBytes()) + .setDataHash("FILE2_DATA_HASH") + .setSrcHash("FILE2_HASH") + .setDataType(Type.SOURCE) + .setCreatedAt(1500000000000L) + .setUpdatedAt(1500000000001L) + .setRevision("123456789")); + + boolean[] flag = {false}; + underTest.readLineHashesStream(dbTester.getSession(), "FILE2_UUID", new Function() { + @Nullable + @Override + public Void apply(@Nullable Reader input) { + fail("function must never been called since there is no data to read"); + flag[0] = true; + return null; + } + }); + assertThat(flag[0]).isFalse(); + } + @Test public void update() { dbTester.prepareDbUnit(getClass(), "shared.xml"); -- 2.39.5