*/
package org.sonar.server.computation.batch;
+import com.google.common.base.Optional;
import javax.annotation.CheckForNull;
import org.sonar.batch.protocol.output.BatchReport;
import org.sonar.core.util.CloseableIterator;
CloseableIterator<BatchReport.Coverage> readComponentCoverage(int fileRef);
/**
- * Reads file source line by line. Throws an exception if the ref does not relate
- * to a file
+ * Reads file source line by line. Return an absent optional if the file doest not exist
*/
- CloseableIterator<String> readFileSource(int fileRef);
+ Optional<CloseableIterator<String>> readFileSource(int fileRef);
CloseableIterator<BatchReport.Test> readTests(int testFileRef);
*/
package org.sonar.server.computation.batch;
+import com.google.common.base.Optional;
import com.google.common.base.Throwables;
import com.google.protobuf.InvalidProtocolBufferException;
import com.google.protobuf.Parser;
}
@Override
- public CloseableIterator<String> readFileSource(int fileRef) {
+ public Optional<CloseableIterator<String>> readFileSource(int fileRef) {
File file = delegate.readFileSource(fileRef);
if (file == null) {
- throw new IllegalStateException("Unable to find source for file #" + fileRef);
+ return Optional.absent();
}
try {
- return new CloseableLineIterator(IOUtils.lineIterator(FileUtils.openInputStream(file), StandardCharsets.UTF_8));
+ return Optional.<CloseableIterator<String>>of(new CloseableLineIterator(IOUtils.lineIterator(FileUtils.openInputStream(file), StandardCharsets.UTF_8)));
} catch (IOException e) {
throw new IllegalStateException("Fail to traverse file: " + file, e);
}
import java.io.File;
import java.io.IOException;
-import java.util.List;
import org.apache.commons.io.FileUtils;
import org.junit.Before;
import org.junit.Rule;
import org.sonar.api.utils.internal.JUnitTempFolder;
import org.sonar.batch.protocol.output.BatchReport;
import org.sonar.batch.protocol.output.BatchReportWriter;
-import org.sonar.batch.protocol.output.FileStructure;
import org.sonar.core.util.CloseableIterator;
import static com.google.common.collect.ImmutableList.of;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.guava.api.Assertions.assertThat;
public class BatchReportReaderImplTest {
private static final int COMPONENT_REF = 1;
- private static final String COMPONENT_UUID = "uuid";
private static final BatchReport.Changesets CHANGESETS = BatchReport.Changesets.newBuilder().setComponentRef(COMPONENT_REF).build();
private static final BatchReport.Measure MEASURE = BatchReport.Measure.newBuilder().build();
private static final BatchReport.Component COMPONENT = BatchReport.Component.newBuilder().setRef(COMPONENT_REF).build();
private BatchReportWriter writer;
private BatchReportReaderImpl underTest;
- private FileStructure fileStructure;
@Before
public void setUp() {
BatchReportDirectoryHolder holder = new ImmutableBatchReportDirectoryHolder(tempFolder.newDir());
underTest = new BatchReportReaderImpl(holder);
writer = new BatchReportWriter(holder.getDirectory());
- fileStructure = new FileStructure(holder.getDirectory());
}
@Test(expected = IllegalStateException.class)
res.close();
}
- @Test(expected = IllegalStateException.class)
- public void readFileSource_throws_ISE_when_file_does_not_exist() {
- underTest.readFileSource(COMPONENT_REF);
+ @Test
+ public void readFileSource_returns_absent_optional_when_file_does_not_exist() {
+ assertThat(underTest.readFileSource(COMPONENT_REF)).isAbsent();
}
@Test
File file = writer.getSourceFile(COMPONENT_REF);
FileUtils.writeLines(file, of("1", "2", "3"));
- CloseableIterator<String> res = underTest.readFileSource(COMPONENT_REF);
+ CloseableIterator<String> res = underTest.readFileSource(COMPONENT_REF).get();
assertThat(res).containsExactly("1", "2", "3");
res.close();
}
*/
package org.sonar.server.computation.batch;
+import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
import java.util.ArrayList;
import java.util.Arrays;
}
@Override
- public CloseableIterator<String> readFileSource(int fileRef) {
+ public Optional<CloseableIterator<String>> readFileSource(int fileRef) {
List<String> lines = fileSources.get(fileRef);
if (lines == null) {
- throw new IllegalStateException("Unable to find source for file #" + fileRef + ". File does not exist: " + lines);
+ return Optional.absent();
}
- return CloseableIterator.from(lines.iterator());
+ return Optional.of(CloseableIterator.from(lines.iterator()));
}
public void putFileSourceLines(int fileRef, @Nullable String... lines) {
return emptyCloseableIterator();
}
+ @CheckForNull
public File readFileSource(int fileRef) {
File file = fileStructure.fileFor(FileStructure.Domain.SOURCE, fileRef);
- if (!fileExists(file)) {
- throw new IllegalStateException("Unable to find source for file #" + fileRef + ". File does not exist: " + file);
+ if (fileExists(file)) {
+ return file;
}
- return file;
+ return null;
}
@CheckForNull
import com.google.common.collect.Lists;
import java.io.File;
+import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Arrays;
import org.apache.commons.io.FileUtils;
+import org.apache.commons.io.IOUtils;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
assertThat(underTest.readCoverageDetails(UNKNOWN_COMPONENT_REF)).isNull();
}
+ @Test
+ public void read_file_source() throws Exception {
+ BatchReportWriter writer = new BatchReportWriter(dir);
+ try (FileOutputStream outputStream = new FileOutputStream(writer.getSourceFile(1))) {
+ IOUtils.write("line1\nline2", outputStream);
+ }
+
+ try (InputStream inputStream = FileUtils.openInputStream(underTest.readFileSource(1))) {
+ assertThat(IOUtils.readLines(inputStream)).containsOnly("line1", "line2");
+ }
+ }
+
+ @Test
+ public void return_null_when_no_file_source() throws Exception {
+ assertThat(underTest.readFileSource(UNKNOWN_COMPONENT_REF)).isNull();
+ }
}