]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-6843 Not fail when file has no source
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Mon, 21 Sep 2015 12:57:16 +0000 (14:57 +0200)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Tue, 22 Sep 2015 15:05:11 +0000 (17:05 +0200)
server/sonar-server/src/main/java/org/sonar/server/computation/batch/BatchReportReader.java
server/sonar-server/src/main/java/org/sonar/server/computation/batch/BatchReportReaderImpl.java
server/sonar-server/src/test/java/org/sonar/server/computation/batch/BatchReportReaderImplTest.java
server/sonar-server/src/test/java/org/sonar/server/computation/batch/BatchReportReaderRule.java
sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/output/BatchReportReader.java
sonar-batch-protocol/src/test/java/org/sonar/batch/protocol/output/BatchReportReaderTest.java

index 301821d186b8c4f05c1a18affb04ae2c7eb24d06..fec1660d8abbf8213ffb069369dbaa992737c426 100644 (file)
@@ -19,6 +19,7 @@
  */
 package org.sonar.server.computation.batch;
 
+import com.google.common.base.Optional;
 import javax.annotation.CheckForNull;
 import org.sonar.batch.protocol.output.BatchReport;
 import org.sonar.core.util.CloseableIterator;
@@ -46,10 +47,9 @@ public interface BatchReportReader {
   CloseableIterator<BatchReport.Coverage> readComponentCoverage(int fileRef);
 
   /**
-   * Reads file source line by line. Throws an exception if the ref does not relate
-   * to a file
+   * Reads file source line by line. Return an absent optional if the file doest not exist
    */
-  CloseableIterator<String> readFileSource(int fileRef);
+  Optional<CloseableIterator<String>> readFileSource(int fileRef);
 
   CloseableIterator<BatchReport.Test> readTests(int testFileRef);
 
index 687967057330243439234dfbaca3196d974b1103..c2dee4402bbda1f8c949434dc8e1c4fcda5278a8 100644 (file)
@@ -19,6 +19,7 @@
  */
 package org.sonar.server.computation.batch;
 
+import com.google.common.base.Optional;
 import com.google.common.base.Throwables;
 import com.google.protobuf.InvalidProtocolBufferException;
 import com.google.protobuf.Parser;
@@ -98,14 +99,14 @@ public class BatchReportReaderImpl implements BatchReportReader {
   }
 
   @Override
-  public CloseableIterator<String> readFileSource(int fileRef) {
+  public Optional<CloseableIterator<String>> readFileSource(int fileRef) {
     File file = delegate.readFileSource(fileRef);
     if (file == null) {
-      throw new IllegalStateException("Unable to find source for file #" + fileRef);
+      return Optional.absent();
     }
 
     try {
-      return new CloseableLineIterator(IOUtils.lineIterator(FileUtils.openInputStream(file), StandardCharsets.UTF_8));
+      return Optional.<CloseableIterator<String>>of(new CloseableLineIterator(IOUtils.lineIterator(FileUtils.openInputStream(file), StandardCharsets.UTF_8)));
     } catch (IOException e) {
       throw new IllegalStateException("Fail to traverse file: " + file, e);
     }
index a652c597e1b9be837f7fec8eaa8c23d111975642..d30f27b9e69959f10996f8cfc10dcb726b3b4067 100644 (file)
@@ -21,7 +21,6 @@ package org.sonar.server.computation.batch;
 
 import java.io.File;
 import java.io.IOException;
-import java.util.List;
 import org.apache.commons.io.FileUtils;
 import org.junit.Before;
 import org.junit.Rule;
@@ -29,15 +28,14 @@ import org.junit.Test;
 import org.sonar.api.utils.internal.JUnitTempFolder;
 import org.sonar.batch.protocol.output.BatchReport;
 import org.sonar.batch.protocol.output.BatchReportWriter;
-import org.sonar.batch.protocol.output.FileStructure;
 import org.sonar.core.util.CloseableIterator;
 
 import static com.google.common.collect.ImmutableList.of;
 import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.guava.api.Assertions.assertThat;
 
 public class BatchReportReaderImplTest {
   private static final int COMPONENT_REF = 1;
-  private static final String COMPONENT_UUID = "uuid";
   private static final BatchReport.Changesets CHANGESETS = BatchReport.Changesets.newBuilder().setComponentRef(COMPONENT_REF).build();
   private static final BatchReport.Measure MEASURE = BatchReport.Measure.newBuilder().build();
   private static final BatchReport.Component COMPONENT = BatchReport.Component.newBuilder().setRef(COMPONENT_REF).build();
@@ -58,14 +56,12 @@ public class BatchReportReaderImplTest {
 
   private BatchReportWriter writer;
   private BatchReportReaderImpl underTest;
-  private FileStructure fileStructure;
 
   @Before
   public void setUp() {
     BatchReportDirectoryHolder holder = new ImmutableBatchReportDirectoryHolder(tempFolder.newDir());
     underTest = new BatchReportReaderImpl(holder);
     writer = new BatchReportWriter(holder.getDirectory());
-    fileStructure = new FileStructure(holder.getDirectory());
   }
 
   @Test(expected = IllegalStateException.class)
@@ -239,9 +235,9 @@ public class BatchReportReaderImplTest {
     res.close();
   }
 
-  @Test(expected = IllegalStateException.class)
-  public void readFileSource_throws_ISE_when_file_does_not_exist() {
-    underTest.readFileSource(COMPONENT_REF);
+  @Test
+  public void readFileSource_returns_absent_optional_when_file_does_not_exist() {
+    assertThat(underTest.readFileSource(COMPONENT_REF)).isAbsent();
   }
 
   @Test
@@ -249,7 +245,7 @@ public class BatchReportReaderImplTest {
     File file = writer.getSourceFile(COMPONENT_REF);
     FileUtils.writeLines(file, of("1", "2", "3"));
 
-    CloseableIterator<String> res = underTest.readFileSource(COMPONENT_REF);
+    CloseableIterator<String> res = underTest.readFileSource(COMPONENT_REF).get();
     assertThat(res).containsExactly("1", "2", "3");
     res.close();
   }
index 04adf2322af8438667c81a8b8f1f8652652450ea..ea65d2c1cdc6e0cb243fdd8ec191b0dab4188686 100644 (file)
@@ -19,6 +19,7 @@
  */
 package org.sonar.server.computation.batch;
 
+import com.google.common.base.Optional;
 import com.google.common.base.Preconditions;
 import java.util.ArrayList;
 import java.util.Arrays;
@@ -193,13 +194,13 @@ public class BatchReportReaderRule implements TestRule, BatchReportReader {
   }
 
   @Override
-  public CloseableIterator<String> readFileSource(int fileRef) {
+  public Optional<CloseableIterator<String>> readFileSource(int fileRef) {
     List<String> lines = fileSources.get(fileRef);
     if (lines == null) {
-      throw new IllegalStateException("Unable to find source for file #" + fileRef + ". File does not exist: " + lines);
+      return Optional.absent();
     }
 
-    return CloseableIterator.from(lines.iterator());
+    return Optional.of(CloseableIterator.from(lines.iterator()));
   }
 
   public void putFileSourceLines(int fileRef, @Nullable String... lines) {
index 4846bb0e1e988233890c633efc0a3ecca31382e8..7a650438204224d9ee494ae72570624b1446b64f 100644 (file)
@@ -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
index 19b29fb5f033e201eb9e48e2dc0144497227bd63..c1545c9aae94326372f7275d48e1dd7baf18ca0b 100644 (file)
@@ -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();
+  }
 }