Skip to content

Commit

Permalink
SONAR-6277 Replace ReportStream by CloseableIterator and remove FileS…
Browse files Browse the repository at this point in the history
…tream (IOUtils.lineIterator should be used instead)
  • Loading branch information
julienlancelot committed Apr 2, 2015
1 parent 5904b95 commit a18f0a9
Show file tree
Hide file tree
Showing 11 changed files with 129 additions and 401 deletions.
@@ -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);
}
}
Expand Up @@ -26,8 +26,11 @@
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
*/
Expand All @@ -51,9 +54,10 @@ private void recursivelyProcessComponent(ComputationContext context, int compone
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);
}
}

Expand All @@ -62,10 +66,11 @@ private void recursivelyProcessComponent(ComputationContext context, int compone
}
}

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);
}
Expand Down
Expand Up @@ -18,7 +18,7 @@
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

package org.sonar.batch.protocol;
package org.sonar.server.computation.source;

import org.junit.After;
import org.junit.Before;
Expand All @@ -30,27 +30,26 @@
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 com.google.common.collect.Lists.newArrayList;
import static org.assertj.core.api.Assertions.assertThat;

public class ReportStreamTest {
public class ReportIteratorTest {

@Rule
public TemporaryFolder temp = new TemporaryFolder();

File file;

ReportStream<BatchReport.Coverage> sut;
ReportIterator<BatchReport.Coverage> sut;

@Before
public void setUp() throws Exception {
File dir = temp.newFolder();
BatchReportWriter writer = new BatchReportWriter(dir);

writer.writeFileCoverage(1, Arrays.asList(
writer.writeFileCoverage(1, newArrayList(
BatchReport.Coverage.newBuilder()
.setLine(1)
.build()
Expand All @@ -68,37 +67,17 @@ public void tearDown() throws Exception {

@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();
sut = new ReportIterator<>(file, BatchReport.Coverage.PARSER);
assertThat(sut.next().getLine()).isEqualTo(1);
}

@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();
public void test_error() throws Exception {
sut = new ReportIterator<>(file, BatchReport.Coverage.PARSER);
sut.next();

// Fail !
iterator.remove();
// fail !
sut.next();
}

}

This file was deleted.

Expand Up @@ -19,7 +19,6 @@
*/
package org.sonar.batch.protocol;

import com.google.protobuf.InvalidProtocolBufferException;
import com.google.protobuf.Message;
import com.google.protobuf.Parser;

Expand All @@ -38,22 +37,6 @@ public static <T extends Message> T readFile(File file, Parser<T> parser) {
}
}

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);
Expand Down

This file was deleted.

0 comments on commit a18f0a9

Please sign in to comment.