]> source.dussan.org Git - sonarqube.git/commitdiff
Allow ReportStream to call iterator() multiple times
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Wed, 1 Apr 2015 07:30:20 +0000 (09:30 +0200)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Wed, 1 Apr 2015 07:30:20 +0000 (09:30 +0200)
sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/ReportStream.java
sonar-batch-protocol/src/test/java/org/sonar/batch/protocol/ReportStreamTest.java

index 031c64f52e5f977523b7780e6ce028978a89c0aa..2d375733a2f600af69917bd861a0164dbedcf31a 100644 (file)
@@ -34,35 +34,30 @@ 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;
-  private ReportIterator<R> iterator;
 
   public ReportStream(File file, Parser<R> parser) {
+    this.file = file;
     this.parser = parser;
-    this.inputStream = ProtobufUtil.createInputStream(file);
   }
 
   @Override
   public Iterator<R> iterator() {
-    if (this.iterator != null) {
-      throw new IllegalStateException("Iterator already obtained");
-    } else {
-      this.iterator = new ReportIterator<>(inputStream, parser);
-      return this.iterator;
-    }
+    this.inputStream = ProtobufUtil.createInputStream(file);
+    return new ReportIterator<>(inputStream, parser);
   }
 
   @Override
   public void close() throws IOException {
-    inputStream.close();
+    if (inputStream != null) {
+      inputStream.close();
+    }
   }
 
   public static class ReportIterator<R extends Message> implements Iterator<R> {
index 0c965e90f645ccdb2fba2614e639003ae37511c6..5523cafc34b50284e7c7d9dff7d3925c141fa02d 100644 (file)
@@ -36,6 +36,7 @@ import java.util.Iterator;
 import java.util.NoSuchElementException;
 
 import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.util.Lists.newArrayList;
 
 public class ReportStreamTest {
 
@@ -68,17 +69,20 @@ public class ReportStreamTest {
   @Test
   public void read_report() throws Exception {
     sut = new ReportStream<>(file, BatchReport.Coverage.PARSER);
-    assertThat(sut).hasSize(1);
-    sut.close();
+    assertThat(newArrayList(sut)).hasSize(1);
+
+    assertThat(sut.iterator().next()).isNotNull();
+    // Shoudl not be null as it should return the first element
+    assertThat(sut.iterator().next()).isNotNull();
   }
 
-  @Test(expected = IllegalStateException.class)
-  public void fail_to_get_iterator_twice() throws Exception {
+  @Test
+  public void next_should_be_reentrant() throws Exception {
     sut = new ReportStream<>(file, BatchReport.Coverage.PARSER);
-    sut.iterator();
+    assertThat(sut).hasSize(1);
 
-    // Fail !
-    sut.iterator();
+    assertThat(sut.iterator().next()).isNotNull();
+    assertThat(sut.iterator().next()).isNotNull();
   }
 
   @Test(expected = NoSuchElementException.class)
@@ -101,4 +105,10 @@ public class ReportStreamTest {
     iterator.remove();
   }
 
+  @Test
+  public void not_fail_when_close_without_calling_iterator() throws Exception {
+    sut = new ReportStream<>(file, BatchReport.Coverage.PARSER);
+    sut.close();
+  }
+
 }