diff options
author | Julien Lancelot <julien.lancelot@sonarsource.com> | 2015-04-01 09:30:20 +0200 |
---|---|---|
committer | Julien Lancelot <julien.lancelot@sonarsource.com> | 2015-04-01 09:30:20 +0200 |
commit | 76d0e41bf8e394d43b4a6e7a2d8593223ae11634 (patch) | |
tree | e489c1a591c7400c9d78814375222c8919fd3598 /sonar-batch-protocol | |
parent | 2d67b3891b6ce1e9fec8d80107d5f373cf06869e (diff) | |
download | sonarqube-76d0e41bf8e394d43b4a6e7a2d8593223ae11634.tar.gz sonarqube-76d0e41bf8e394d43b4a6e7a2d8593223ae11634.zip |
Allow ReportStream to call iterator() multiple times
Diffstat (limited to 'sonar-batch-protocol')
-rw-r--r-- | sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/ReportStream.java | 19 | ||||
-rw-r--r-- | sonar-batch-protocol/src/test/java/org/sonar/batch/protocol/ReportStreamTest.java | 24 |
2 files changed, 24 insertions, 19 deletions
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 index 031c64f52e5..2d375733a2f 100644 --- 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 @@ -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> { 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 index 0c965e90f64..5523cafc34b 100644 --- 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 @@ -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(); + } + } |