diff options
author | Julien Lancelot <julien.lancelot@sonarsource.com> | 2015-09-21 14:57:16 +0200 |
---|---|---|
committer | Julien Lancelot <julien.lancelot@sonarsource.com> | 2015-09-22 17:05:11 +0200 |
commit | 9b9827ed18a6d3d379c20279c002bd311d53dc06 (patch) | |
tree | a0db9435bf8a47b3792364e8c31a985d495efaaf /sonar-batch-protocol | |
parent | ddc14ab698df614fa1e310136d0008ea0ad6d0da (diff) | |
download | sonarqube-9b9827ed18a6d3d379c20279c002bd311d53dc06.tar.gz sonarqube-9b9827ed18a6d3d379c20279c002bd311d53dc06.zip |
SONAR-6843 Not fail when file has no source
Diffstat (limited to 'sonar-batch-protocol')
2 files changed, 22 insertions, 3 deletions
diff --git a/sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/output/BatchReportReader.java b/sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/output/BatchReportReader.java index 4846bb0e1e9..7a650438204 100644 --- a/sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/output/BatchReportReader.java +++ b/sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/output/BatchReportReader.java @@ -120,12 +120,13 @@ public class BatchReportReader { return emptyCloseableIterator(); } + @CheckForNull public File readFileSource(int fileRef) { File file = fileStructure.fileFor(FileStructure.Domain.SOURCE, fileRef); - if (!fileExists(file)) { - throw new IllegalStateException("Unable to find source for file #" + fileRef + ". File does not exist: " + file); + if (fileExists(file)) { + return file; } - return file; + return null; } @CheckForNull diff --git a/sonar-batch-protocol/src/test/java/org/sonar/batch/protocol/output/BatchReportReaderTest.java b/sonar-batch-protocol/src/test/java/org/sonar/batch/protocol/output/BatchReportReaderTest.java index 19b29fb5f03..c1545c9aae9 100644 --- a/sonar-batch-protocol/src/test/java/org/sonar/batch/protocol/output/BatchReportReaderTest.java +++ b/sonar-batch-protocol/src/test/java/org/sonar/batch/protocol/output/BatchReportReaderTest.java @@ -21,9 +21,11 @@ package org.sonar.batch.protocol.output; import com.google.common.collect.Lists; import java.io.File; +import java.io.FileOutputStream; import java.io.InputStream; import java.util.Arrays; import org.apache.commons.io.FileUtils; +import org.apache.commons.io.IOUtils; import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -339,4 +341,20 @@ public class BatchReportReaderTest { assertThat(underTest.readCoverageDetails(UNKNOWN_COMPONENT_REF)).isNull(); } + @Test + public void read_file_source() throws Exception { + BatchReportWriter writer = new BatchReportWriter(dir); + try (FileOutputStream outputStream = new FileOutputStream(writer.getSourceFile(1))) { + IOUtils.write("line1\nline2", outputStream); + } + + try (InputStream inputStream = FileUtils.openInputStream(underTest.readFileSource(1))) { + assertThat(IOUtils.readLines(inputStream)).containsOnly("line1", "line2"); + } + } + + @Test + public void return_null_when_no_file_source() throws Exception { + assertThat(underTest.readFileSource(UNKNOWN_COMPONENT_REF)).isNull(); + } } |