Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
aea924c
commit 2d67b38
Showing
9 changed files
with
260 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/ReportStream.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/* | ||
* 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 Parser<R> parser; | ||
private InputStream inputStream; | ||
private ReportIterator<R> iterator; | ||
|
||
public ReportStream(File file, Parser<R> parser) { | ||
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; | ||
} | ||
} | ||
|
||
@Override | ||
public void close() throws IOException { | ||
inputStream.close(); | ||
} | ||
|
||
public 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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
sonar-batch-protocol/src/test/java/org/sonar/batch/protocol/ReportStreamTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/* | ||
* 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.IOUtils; | ||
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 { | ||
IOUtils.closeQuietly(sut); | ||
} | ||
|
||
@Test | ||
public void read_report() throws Exception { | ||
sut = new ReportStream<>(file, BatchReport.Coverage.PARSER); | ||
assertThat(sut).hasSize(1); | ||
sut.close(); | ||
} | ||
|
||
@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(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.