]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-6277 Replace ReportStream by CloseableIterator and remove FileStream (IOUtils...
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Thu, 2 Apr 2015 14:25:50 +0000 (16:25 +0200)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Thu, 2 Apr 2015 14:31:38 +0000 (16:31 +0200)
12 files changed:
server/sonar-server/src/main/java/org/sonar/server/computation/source/ReportIterator.java [new file with mode: 0644]
server/sonar-server/src/main/java/org/sonar/server/computation/step/PersistCoverageStep.java
server/sonar-server/src/test/java/org/sonar/server/computation/source/ReportIteratorTest.java [new file with mode: 0644]
sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/FileStream.java [deleted file]
sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/ProtobufUtil.java
sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/ReportStream.java [deleted file]
sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/output/BatchReportReader.java
sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/output/BatchReportWriter.java
sonar-batch-protocol/src/test/java/org/sonar/batch/protocol/FileStreamTest.java [deleted file]
sonar-batch-protocol/src/test/java/org/sonar/batch/protocol/ReportStreamTest.java [deleted file]
sonar-batch-protocol/src/test/java/org/sonar/batch/protocol/output/BatchReportReaderTest.java
sonar-batch-protocol/src/test/java/org/sonar/batch/protocol/output/BatchReportWriterTest.java

diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/source/ReportIterator.java b/server/sonar-server/src/main/java/org/sonar/server/computation/source/ReportIterator.java
new file mode 100644 (file)
index 0000000..4f54808
--- /dev/null
@@ -0,0 +1,61 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+
+package org.sonar.server.computation.source;
+
+import com.google.common.base.Throwables;
+import com.google.protobuf.InvalidProtocolBufferException;
+import com.google.protobuf.Parser;
+import org.apache.commons.io.FileUtils;
+import org.apache.commons.io.IOUtils;
+import org.sonar.server.util.CloseableIterator;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+
+public class ReportIterator<E> extends CloseableIterator<E> {
+
+  private final Parser<E> parser;
+  private InputStream stream;
+
+  public ReportIterator(File file, Parser<E> parser) {
+    try {
+      this.parser = parser;
+      this.stream = FileUtils.openInputStream(file);
+    } catch (IOException e) {
+      throw Throwables.propagate(e);
+    }
+  }
+
+  @Override
+  protected E doNext() {
+    try {
+      return parser.parseDelimitedFrom(stream);
+    } catch (InvalidProtocolBufferException e) {
+      throw Throwables.propagate(e);
+    }
+  }
+
+  @Override
+  protected void doClose() {
+    IOUtils.closeQuietly(stream);
+  }
+}
index c9db4bb9b66201371927741a43dfe8a53d3f1b7c..28b2e9dcb273fbc7b60d022c96857d438bb4248f 100644 (file)
@@ -26,8 +26,11 @@ import org.sonar.batch.protocol.Constants;
 import org.sonar.batch.protocol.output.BatchReport;
 import org.sonar.batch.protocol.output.BatchReportReader;
 import org.sonar.server.computation.ComputationContext;
+import org.sonar.server.computation.source.ReportIterator;
 import org.sonar.server.source.db.FileSourceDb;
 
+import java.io.File;
+
 /**
  * Nothing is persist for the moment. Only Coverage are read and not persist for the moment
  */
@@ -51,9 +54,10 @@ public class PersistCoverageStep implements ComputationStep {
     BatchReportReader reportReader = context.getReportReader();
     BatchReport.Component component = reportReader.readComponent(componentRef);
     if (component.getType().equals(Constants.ComponentType.FILE)) {
-      Iterable<BatchReport.Coverage> coverageList = reportReader.readFileCoverage(componentRef);
-      if (coverageList != null) {
-        processCoverage(component, coverageList);
+      File coverageFile = reportReader.readFileCoverage(componentRef);
+      if (coverageFile != null) {
+        ReportIterator<BatchReport.Coverage> coverageReport = new ReportIterator<>(coverageFile, BatchReport.Coverage.PARSER);
+        processCoverage(component, coverageReport);
       }
     }
 
@@ -62,10 +66,11 @@ public class PersistCoverageStep implements ComputationStep {
     }
   }
 
-  private void processCoverage(BatchReport.Component component, Iterable<BatchReport.Coverage> coverageList) {
+  private void processCoverage(BatchReport.Component component, ReportIterator<BatchReport.Coverage> coverageReport) {
     fileSourceData = null;
     FileSourceDb.Data.Builder dataBuilder = FileSourceDb.Data.newBuilder();
-    for (BatchReport.Coverage coverage : coverageList) {
+    while (coverageReport.hasNext()) {
+      BatchReport.Coverage coverage = coverageReport.next();
       FileSourceDb.Line.Builder lineBuilder = dataBuilder.addLinesBuilder().setLine(coverage.getLine());
       processLineCoverage(coverage.getLine(), lineBuilder, coverage);
     }
diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/source/ReportIteratorTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/source/ReportIteratorTest.java
new file mode 100644 (file)
index 0000000..91b340b
--- /dev/null
@@ -0,0 +1,83 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+
+package org.sonar.server.computation.source;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+import org.sonar.batch.protocol.output.BatchReport;
+import org.sonar.batch.protocol.output.BatchReportWriter;
+import org.sonar.batch.protocol.output.FileStructure;
+
+import java.io.File;
+import java.util.NoSuchElementException;
+
+import static com.google.common.collect.Lists.newArrayList;
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class ReportIteratorTest {
+
+  @Rule
+  public TemporaryFolder temp = new TemporaryFolder();
+
+  File file;
+
+  ReportIterator<BatchReport.Coverage> sut;
+
+  @Before
+  public void setUp() throws Exception {
+    File dir = temp.newFolder();
+    BatchReportWriter writer = new BatchReportWriter(dir);
+
+    writer.writeFileCoverage(1, newArrayList(
+      BatchReport.Coverage.newBuilder()
+        .setLine(1)
+        .build()
+      ));
+
+    file = new FileStructure(dir).fileFor(FileStructure.Domain.COVERAGE, 1);
+  }
+
+  @After
+  public void tearDown() throws Exception {
+    if (sut != null) {
+      sut.close();
+    }
+  }
+
+  @Test
+  public void read_report() throws Exception {
+    sut = new ReportIterator<>(file, BatchReport.Coverage.PARSER);
+    assertThat(sut.next().getLine()).isEqualTo(1);
+  }
+
+  @Test(expected = NoSuchElementException.class)
+  public void test_error() throws Exception {
+    sut = new ReportIterator<>(file, BatchReport.Coverage.PARSER);
+    sut.next();
+
+    // fail !
+    sut.next();
+  }
+
+}
diff --git a/sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/FileStream.java b/sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/FileStream.java
deleted file mode 100644 (file)
index b07daee..0000000
+++ /dev/null
@@ -1,71 +0,0 @@
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * SonarQube is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
- */
-
-package org.sonar.batch.protocol;
-
-import org.apache.commons.io.Charsets;
-import org.apache.commons.io.IOUtils;
-
-import java.io.Closeable;
-import java.io.File;
-import java.io.IOException;
-import java.io.InputStream;
-import java.util.Iterator;
-
-/**
- * An object to iterate over lines in a file.
- * A LineStream is opened upon creation and is closed by invoking the close method.
- *
- * Inspired by {@link java.nio.file.DirectoryStream}
- */
-public class FileStream implements Closeable, Iterable<String> {
-
-  private final File file;
-  private InputStream inputStream;
-
-  public FileStream(File file) {
-    this.file = file;
-  }
-
-  @Override
-  public Iterator<String> iterator() {
-    if (this.inputStream != null) {
-      throw new IllegalStateException("Iterator already obtained");
-    } else {
-      try {
-        this.inputStream = ProtobufUtil.createInputStream(file);
-        return IOUtils.lineIterator(inputStream, Charsets.UTF_8);
-      } catch (IOException e) {
-        throw new IllegalStateException("Failed to read lines from file " + file, e);
-      }
-    }
-  }
-
-  @Override
-  public void close() {
-    if (inputStream != null) {
-      try {
-        inputStream.close();
-      } catch (IOException e) {
-        throw new IllegalStateException("Failed to close input stream of file " + file, e);
-      }
-    }
-  }
-}
index 1e39578c692a3640d0a776d02de3f571cc519b75..a2579c079373e953ac3258dbbc96688507f516a5 100644 (file)
@@ -19,7 +19,6 @@
  */
 package org.sonar.batch.protocol;
 
-import com.google.protobuf.InvalidProtocolBufferException;
 import com.google.protobuf.Message;
 import com.google.protobuf.Parser;
 
@@ -38,22 +37,6 @@ public class ProtobufUtil {
     }
   }
 
-  static <T extends Message> T readInputStream(InputStream inputStream, Parser<T> parser) {
-    try {
-      return parser.parseDelimitedFrom(inputStream);
-    } catch (InvalidProtocolBufferException e) {
-      throw new IllegalStateException("Failed to read input stream", e);
-    }
-  }
-
-  static InputStream createInputStream(File file) {
-    try {
-      return new BufferedInputStream(new FileInputStream(file));
-    } catch (FileNotFoundException e) {
-      throw new IllegalStateException("Unable to find file " + file, e);
-    }
-  }
-
   public static void writeToFile(Message message, File toFile) {
     try (OutputStream out = new BufferedOutputStream(new FileOutputStream(toFile, false))) {
       message.writeTo(out);
diff --git a/sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/ReportStream.java b/sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/ReportStream.java
deleted file mode 100644 (file)
index 315bc42..0000000
+++ /dev/null
@@ -1,108 +0,0 @@
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * SonarQube is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
- */
-
-package org.sonar.batch.protocol;
-
-import com.google.protobuf.Message;
-import com.google.protobuf.Parser;
-
-import java.io.Closeable;
-import java.io.File;
-import java.io.IOException;
-import java.io.InputStream;
-import java.util.Iterator;
-import java.util.NoSuchElementException;
-
-/**
- * An object to iterate over protobuf messages in a file.
- * A ReportStream is opened upon creation and is closed by invoking the close method.
- *
- * Warning, while it extends Iterable, it is not a general-purpose Iterable as it supports only a single Iterator;
- * invoking the iterator method to obtain a second or subsequent iterator throws IllegalStateException.
- *
- * Inspired by {@link java.nio.file.DirectoryStream}
- */
-public class ReportStream<R extends Message> implements Closeable, Iterable<R> {
-
-  private final File file;
-  private final Parser<R> parser;
-  private InputStream inputStream;
-
-  public ReportStream(File file, Parser<R> parser) {
-    this.file = file;
-    this.parser = parser;
-  }
-
-  @Override
-  public Iterator<R> iterator() {
-    if (this.inputStream != null) {
-      throw new IllegalStateException("Iterator already obtained");
-    } else {
-      this.inputStream = ProtobufUtil.createInputStream(file);
-      return new ReportIterator<>(inputStream, parser);
-    }
-  }
-
-  @Override
-  public void close() {
-    if (inputStream != null) {
-      try {
-        inputStream.close();
-      } catch (IOException e) {
-        throw new IllegalStateException("Failed to close input stream of file " + file, e);
-      }
-    }
-  }
-
-  private static class ReportIterator<R extends Message> implements Iterator<R> {
-
-    private final Parser<R> parser;
-    private InputStream inputStream;
-    private R currentMessage;
-
-    public ReportIterator(InputStream inputStream, Parser<R> parser) {
-      this.inputStream = inputStream;
-      this.parser = parser;
-    }
-
-    @Override
-    public boolean hasNext() {
-      if (currentMessage == null) {
-        currentMessage = ProtobufUtil.readInputStream(inputStream, parser);
-      }
-      return currentMessage != null;
-    }
-
-    @Override
-    public R next() {
-      if (!hasNext()) {
-        throw new NoSuchElementException();
-      }
-      R messageToReturn = currentMessage;
-      currentMessage = null;
-      return messageToReturn;
-    }
-
-    @Override
-    public void remove() {
-      throw new UnsupportedOperationException();
-    }
-  }
-}
index ba90c68b4c802dd5dcf22ba1e39e878e2391c2ea..90720f6e5e16dca16b1a288f73061a15074a157a 100644 (file)
@@ -19,9 +19,7 @@
  */
 package org.sonar.batch.protocol.output;
 
-import org.sonar.batch.protocol.FileStream;
 import org.sonar.batch.protocol.ProtobufUtil;
-import org.sonar.batch.protocol.ReportStream;
 import org.sonar.batch.protocol.output.BatchReport.Issues;
 
 import javax.annotation.CheckForNull;
@@ -128,20 +126,20 @@ public class BatchReportReader {
   }
 
   @CheckForNull
-  public ReportStream<BatchReport.Coverage> readFileCoverage(int fileRef) {
+  public File readFileCoverage(int fileRef) {
     File file = fileStructure.fileFor(FileStructure.Domain.COVERAGE, fileRef);
     if (doesFileExists(file)) {
-      return new ReportStream<>(file, BatchReport.Coverage.PARSER);
+      return file;
     }
     return null;
   }
 
-  public FileStream readSourceLines(int fileRef) {
+  public File readFileSource(int fileRef) {
     File file = fileStructure.fileFor(FileStructure.Domain.SOURCE, fileRef);
     if (!doesFileExists(file)) {
       throw new IllegalStateException("Unable to find source for file #" + fileRef + ". File does not exist: " + file);
     }
-    return new FileStream(file);
+    return file;
   }
 
   private boolean doesFileExists(File file) {
index 51d17f9afa125418bfac2608918160bd8d9e575c..97b7e5f2caa7d32ed4d392d83dc6fa0f0c38b5dc 100644 (file)
@@ -34,7 +34,7 @@ public class BatchReportWriter {
     this.fileStructure = new FileStructure(dir);
   }
 
-  FileStructure getFileStructure() {
+  public FileStructure getFileStructure() {
     return fileStructure;
   }
 
diff --git a/sonar-batch-protocol/src/test/java/org/sonar/batch/protocol/FileStreamTest.java b/sonar-batch-protocol/src/test/java/org/sonar/batch/protocol/FileStreamTest.java
deleted file mode 100644 (file)
index 03d82c5..0000000
+++ /dev/null
@@ -1,75 +0,0 @@
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * SonarQube is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
- */
-
-package org.sonar.batch.protocol;
-
-import com.google.common.io.Resources;
-import org.junit.After;
-import org.junit.Before;
-import org.junit.Test;
-
-import java.io.File;
-import java.util.Iterator;
-
-import static org.assertj.core.api.Assertions.assertThat;
-
-public class FileStreamTest {
-
-  File file;
-
-  FileStream sut;
-
-  @Before
-  public void setUp() throws Exception {
-    file = new File(Resources.getResource(getClass(), "FileStreamTest/file.txt").getFile());
-  }
-
-  @After
-  public void tearDown() throws Exception {
-    if (sut != null) {
-      sut.close();
-    }
-  }
-
-  @Test
-  public void read_lines() throws Exception {
-    sut = new FileStream(file);
-
-    Iterator<String> lines = sut.iterator();
-    assertThat(lines.next()).isEqualTo("line1");
-    assertThat(lines.next()).isEqualTo("line2");
-    assertThat(lines.next()).isEqualTo("line3");
-  }
-
-  @Test(expected = IllegalStateException.class)
-  public void fail_to_get_iterator_twice() throws Exception {
-    sut = new FileStream(file);
-    sut.iterator();
-
-    // Fail !
-    sut.iterator();
-  }
-
-  @Test
-  public void not_fail_when_close_without_calling_iterator() throws Exception {
-    sut = new FileStream(file);
-    sut.close();
-  }
-}
diff --git a/sonar-batch-protocol/src/test/java/org/sonar/batch/protocol/ReportStreamTest.java b/sonar-batch-protocol/src/test/java/org/sonar/batch/protocol/ReportStreamTest.java
deleted file mode 100644 (file)
index d71c071..0000000
+++ /dev/null
@@ -1,104 +0,0 @@
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * SonarQube is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
- */
-
-package org.sonar.batch.protocol;
-
-import org.junit.After;
-import org.junit.Before;
-import org.junit.Rule;
-import org.junit.Test;
-import org.junit.rules.TemporaryFolder;
-import org.sonar.batch.protocol.output.BatchReport;
-import org.sonar.batch.protocol.output.BatchReportWriter;
-import org.sonar.batch.protocol.output.FileStructure;
-
-import java.io.File;
-import java.util.Arrays;
-import java.util.Iterator;
-import java.util.NoSuchElementException;
-
-import static org.assertj.core.api.Assertions.assertThat;
-
-public class ReportStreamTest {
-
-  @Rule
-  public TemporaryFolder temp = new TemporaryFolder();
-
-  File file;
-
-  ReportStream<BatchReport.Coverage> sut;
-
-  @Before
-  public void setUp() throws Exception {
-    File dir = temp.newFolder();
-    BatchReportWriter writer = new BatchReportWriter(dir);
-
-    writer.writeFileCoverage(1, Arrays.asList(
-      BatchReport.Coverage.newBuilder()
-        .setLine(1)
-        .build()
-      ));
-
-    file = new FileStructure(dir).fileFor(FileStructure.Domain.COVERAGE, 1);
-  }
-
-  @After
-  public void tearDown() throws Exception {
-    if (sut != null) {
-      sut.close();
-    }
-  }
-
-  @Test
-  public void read_report() throws Exception {
-    sut = new ReportStream<>(file, BatchReport.Coverage.PARSER);
-    assertThat(sut).hasSize(1);
-  }
-
-  @Test(expected = IllegalStateException.class)
-  public void fail_to_get_iterator_twice() throws Exception {
-    sut = new ReportStream<>(file, BatchReport.Coverage.PARSER);
-    sut.iterator();
-
-    // Fail !
-    sut.iterator();
-  }
-
-  @Test(expected = NoSuchElementException.class)
-  public void fail_to_get_next_when_no_next() throws Exception {
-    sut = new ReportStream<>(file, BatchReport.Coverage.PARSER);
-    Iterator<BatchReport.Coverage> iterator = sut.iterator();
-    // Get first element
-    iterator.next();
-
-    // Fail !
-    iterator.next();
-  }
-
-  @Test(expected = UnsupportedOperationException.class)
-  public void fail_to_remove() throws Exception {
-    sut = new ReportStream<>(file, BatchReport.Coverage.PARSER);
-    Iterator<BatchReport.Coverage> iterator = sut.iterator();
-
-    // Fail !
-    iterator.remove();
-  }
-
-}
index 4aaebff9a48eac970af44a6cd075af419cb386de..c109796fd3529cbe71fcb5c13f080bed204663bd 100644 (file)
@@ -19,6 +19,8 @@
  */
 package org.sonar.batch.protocol.output;
 
+import com.google.common.collect.Lists;
+import org.apache.commons.io.FileUtils;
 import org.junit.Before;
 import org.junit.Rule;
 import org.junit.Test;
@@ -26,10 +28,9 @@ import org.junit.rules.TemporaryFolder;
 import org.sonar.batch.protocol.Constants;
 
 import java.io.File;
+import java.io.InputStream;
 import java.util.Arrays;
-import java.util.List;
 
-import static com.google.common.collect.Lists.newArrayList;
 import static org.assertj.core.api.Assertions.assertThat;
 
 public class BatchReportReaderTest {
@@ -194,26 +195,40 @@ public class BatchReportReaderTest {
         .build()));
 
     sut = new BatchReportReader(dir);
-    List<BatchReport.Coverage> coverageList = newArrayList(sut.readFileCoverage(1));
-    assertThat(coverageList).hasSize(2);
-
-    BatchReport.Coverage coverage = coverageList.get(0);
-    assertThat(coverage.getLine()).isEqualTo(1);
-    assertThat(coverage.getConditions()).isEqualTo(1);
-    assertThat(coverage.getUtHits()).isTrue();
-    assertThat(coverage.getItHits()).isFalse();
-    assertThat(coverage.getUtCoveredConditions()).isEqualTo(1);
-    assertThat(coverage.getItCoveredConditions()).isEqualTo(1);
-    assertThat(coverage.getOverallCoveredConditions()).isEqualTo(1);
-
-    coverage = coverageList.get(1);
-    assertThat(coverage.getLine()).isEqualTo(2);
-    assertThat(coverage.getConditions()).isEqualTo(5);
-    assertThat(coverage.getUtHits()).isFalse();
-    assertThat(coverage.getItHits()).isFalse();
-    assertThat(coverage.getUtCoveredConditions()).isEqualTo(4);
-    assertThat(coverage.getItCoveredConditions()).isEqualTo(5);
-    assertThat(coverage.getOverallCoveredConditions()).isEqualTo(5);
+
+    InputStream inputStream = FileUtils.openInputStream(new BatchReportReader(dir).readFileCoverage(1));
+    try {
+      BatchReport.Coverage coverage = BatchReport.Coverage.PARSER.parseDelimitedFrom(inputStream);
+      assertThat(coverage.getLine()).isEqualTo(1);
+      assertThat(coverage.getConditions()).isEqualTo(1);
+      assertThat(coverage.getUtHits()).isTrue();
+      assertThat(coverage.getItHits()).isFalse();
+      assertThat(coverage.getUtCoveredConditions()).isEqualTo(1);
+      assertThat(coverage.getItCoveredConditions()).isEqualTo(1);
+      assertThat(coverage.getOverallCoveredConditions()).isEqualTo(1);
+
+      coverage = BatchReport.Coverage.PARSER.parseDelimitedFrom(inputStream);
+      assertThat(coverage.getLine()).isEqualTo(2);
+      assertThat(coverage.getConditions()).isEqualTo(5);
+      assertThat(coverage.getUtHits()).isFalse();
+      assertThat(coverage.getItHits()).isFalse();
+      assertThat(coverage.getUtCoveredConditions()).isEqualTo(4);
+      assertThat(coverage.getItCoveredConditions()).isEqualTo(5);
+      assertThat(coverage.getOverallCoveredConditions()).isEqualTo(5);
+    } finally {
+      inputStream.close();
+    }
+  }
+
+  @Test
+  public void read_source_lines() throws Exception {
+    File dir = temp.newFolder();
+    BatchReportWriter writer = new BatchReportWriter(dir);
+    File file = writer.getFileStructure().fileFor(FileStructure.Domain.SOURCE, 1);
+    FileUtils.writeLines(file, Lists.newArrayList("line1", "line2"));
+
+    File sourceFile = new BatchReportReader(dir).readFileSource(1);
+    assertThat(sourceFile).isEqualTo(file);
   }
 
   @Test(expected = IllegalStateException.class)
@@ -261,9 +276,9 @@ public class BatchReportReaderTest {
     assertThat(sut.readFileCoverage(123)).isNull();
   }
 
-  @Test(expected = IllegalStateException.class)
-  public void fail_if_no_source_found() throws Exception {
-    assertThat(sut.readSourceLines(123)).isNull();
+  @Test
+  public void return_null_if_no_source_found() throws Exception {
+    assertThat(sut.readFileCoverage(123)).isNull();
   }
 
   /**
index 580cf848d0eb873b0f495913fe40d4c3242d417a..b0d666a52d727871112d70d5256f0705c9e3677c 100644 (file)
  */
 package org.sonar.batch.protocol.output;
 
-import com.google.common.collect.Lists;
 import org.apache.commons.io.FileUtils;
-import org.apache.commons.io.IOUtils;
 import org.junit.Rule;
 import org.junit.Test;
 import org.junit.rules.TemporaryFolder;
 import org.sonar.batch.protocol.Constants;
-import org.sonar.batch.protocol.FileStream;
 import org.sonar.batch.protocol.ProtobufUtil;
-import org.sonar.batch.protocol.ReportStream;
 import org.sonar.batch.protocol.output.BatchReport.Range;
 
 import java.io.File;
 import java.util.Arrays;
-import java.util.List;
 
 import static org.assertj.core.api.Assertions.assertThat;
-import static org.assertj.core.util.Lists.newArrayList;
 
 public class BatchReportWriterTest {
 
@@ -320,63 +314,10 @@ public class BatchReportWriterTest {
         .setUtCoveredConditions(1)
         .setItCoveredConditions(1)
         .setOverallCoveredConditions(1)
-        .build(),
-      BatchReport.Coverage.newBuilder()
-        .setLine(2)
-        .setConditions(5)
-        .setUtHits(false)
-        .setItHits(false)
-        .setUtCoveredConditions(4)
-        .setItCoveredConditions(5)
-        .setOverallCoveredConditions(5)
         .build()
-    ));
+      ));
 
     assertThat(writer.hasComponentData(FileStructure.Domain.COVERAGE, 1)).isTrue();
-
-    ReportStream coverageReportStream = null;
-    try {
-      coverageReportStream = new BatchReportReader(dir).readFileCoverage(1);
-      List<BatchReport.Coverage> coverageList = newArrayList(coverageReportStream);
-      assertThat(coverageList).hasSize(2);
-
-      BatchReport.Coverage coverage = coverageList.get(0);
-      assertThat(coverage.getLine()).isEqualTo(1);
-      assertThat(coverage.getConditions()).isEqualTo(1);
-      assertThat(coverage.getUtHits()).isTrue();
-      assertThat(coverage.getItHits()).isFalse();
-      assertThat(coverage.getUtCoveredConditions()).isEqualTo(1);
-      assertThat(coverage.getItCoveredConditions()).isEqualTo(1);
-      assertThat(coverage.getOverallCoveredConditions()).isEqualTo(1);
-
-      coverage = coverageList.get(1);
-      assertThat(coverage.getLine()).isEqualTo(2);
-      assertThat(coverage.getConditions()).isEqualTo(5);
-      assertThat(coverage.getUtHits()).isFalse();
-      assertThat(coverage.getItHits()).isFalse();
-      assertThat(coverage.getUtCoveredConditions()).isEqualTo(4);
-      assertThat(coverage.getItCoveredConditions()).isEqualTo(5);
-      assertThat(coverage.getOverallCoveredConditions()).isEqualTo(5);
-    } finally {
-     IOUtils.closeQuietly(coverageReportStream);
-    }
-  }
-
-  @Test
-  public void read_source_lines() throws Exception {
-    File dir = temp.newFolder();
-    BatchReportWriter writer = new BatchReportWriter(dir);
-    File file = writer.getFileStructure().fileFor(FileStructure.Domain.SOURCE, 1);
-    assertThat(file.exists());
-    FileUtils.writeLines(file, Lists.newArrayList("line1", "line2"));
-
-    FileStream fileStream = null;
-    try {
-      fileStream = new BatchReportReader(dir).readSourceLines(1);
-      assertThat(fileStream).hasSize(2);
-    } finally {
-      fileStream.close();
-    }
   }
 
 }