diff options
author | Julien Lancelot <julien.lancelot@sonarsource.com> | 2015-04-02 16:25:50 +0200 |
---|---|---|
committer | Julien Lancelot <julien.lancelot@sonarsource.com> | 2015-04-02 16:31:38 +0200 |
commit | a18f0a986d74edad8d61d188334910093071c2a8 (patch) | |
tree | 0983850ca1f476872a3259c2bdfe150cc3d25b8e /sonar-batch-protocol/src/test | |
parent | 5904b952979414d585507a4f3ae66e29ca8ba6fc (diff) | |
download | sonarqube-a18f0a986d74edad8d61d188334910093071c2a8.tar.gz sonarqube-a18f0a986d74edad8d61d188334910093071c2a8.zip |
SONAR-6277 Replace ReportStream by CloseableIterator and remove FileStream (IOUtils.lineIterator should be used instead)
Diffstat (limited to 'sonar-batch-protocol/src/test')
4 files changed, 41 insertions, 264 deletions
diff --git a/sonar-batch-protocol/src/test/java/org/sonar/batch/protocol/FileStreamTest.java b/sonar-batch-protocol/src/test/java/org/sonar/batch/protocol/FileStreamTest.java deleted file mode 100644 index 03d82c5629d..00000000000 --- a/sonar-batch-protocol/src/test/java/org/sonar/batch/protocol/FileStreamTest.java +++ /dev/null @@ -1,75 +0,0 @@ -/* - * 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.common.io.Resources; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; - -import java.io.File; -import java.util.Iterator; - -import static org.assertj.core.api.Assertions.assertThat; - -public class FileStreamTest { - - File file; - - FileStream sut; - - @Before - public void setUp() throws Exception { - file = new File(Resources.getResource(getClass(), "FileStreamTest/file.txt").getFile()); - } - - @After - public void tearDown() throws Exception { - if (sut != null) { - sut.close(); - } - } - - @Test - public void read_lines() throws Exception { - sut = new FileStream(file); - - Iterator<String> lines = sut.iterator(); - assertThat(lines.next()).isEqualTo("line1"); - assertThat(lines.next()).isEqualTo("line2"); - assertThat(lines.next()).isEqualTo("line3"); - } - - @Test(expected = IllegalStateException.class) - public void fail_to_get_iterator_twice() throws Exception { - sut = new FileStream(file); - sut.iterator(); - - // Fail ! - sut.iterator(); - } - - @Test - public void not_fail_when_close_without_calling_iterator() throws Exception { - sut = new FileStream(file); - sut.close(); - } -} 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 deleted file mode 100644 index d71c071de7a..00000000000 --- a/sonar-batch-protocol/src/test/java/org/sonar/batch/protocol/ReportStreamTest.java +++ /dev/null @@ -1,104 +0,0 @@ -/* - * 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.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 { - if (sut != null) { - sut.close(); - } - } - - @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(); - } - - @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(); - } - -} diff --git a/sonar-batch-protocol/src/test/java/org/sonar/batch/protocol/output/BatchReportReaderTest.java b/sonar-batch-protocol/src/test/java/org/sonar/batch/protocol/output/BatchReportReaderTest.java index 4aaebff9a48..c109796fd35 100644 --- a/sonar-batch-protocol/src/test/java/org/sonar/batch/protocol/output/BatchReportReaderTest.java +++ b/sonar-batch-protocol/src/test/java/org/sonar/batch/protocol/output/BatchReportReaderTest.java @@ -19,6 +19,8 @@ */ package org.sonar.batch.protocol.output; +import com.google.common.collect.Lists; +import org.apache.commons.io.FileUtils; import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -26,10 +28,9 @@ import org.junit.rules.TemporaryFolder; import org.sonar.batch.protocol.Constants; import java.io.File; +import java.io.InputStream; import java.util.Arrays; -import java.util.List; -import static com.google.common.collect.Lists.newArrayList; import static org.assertj.core.api.Assertions.assertThat; public class BatchReportReaderTest { @@ -194,26 +195,40 @@ public class BatchReportReaderTest { .build())); sut = new BatchReportReader(dir); - List<BatchReport.Coverage> coverageList = newArrayList(sut.readFileCoverage(1)); - assertThat(coverageList).hasSize(2); - - BatchReport.Coverage coverage = coverageList.get(0); - assertThat(coverage.getLine()).isEqualTo(1); - assertThat(coverage.getConditions()).isEqualTo(1); - assertThat(coverage.getUtHits()).isTrue(); - assertThat(coverage.getItHits()).isFalse(); - assertThat(coverage.getUtCoveredConditions()).isEqualTo(1); - assertThat(coverage.getItCoveredConditions()).isEqualTo(1); - assertThat(coverage.getOverallCoveredConditions()).isEqualTo(1); - - coverage = coverageList.get(1); - assertThat(coverage.getLine()).isEqualTo(2); - assertThat(coverage.getConditions()).isEqualTo(5); - assertThat(coverage.getUtHits()).isFalse(); - assertThat(coverage.getItHits()).isFalse(); - assertThat(coverage.getUtCoveredConditions()).isEqualTo(4); - assertThat(coverage.getItCoveredConditions()).isEqualTo(5); - assertThat(coverage.getOverallCoveredConditions()).isEqualTo(5); + + InputStream inputStream = FileUtils.openInputStream(new BatchReportReader(dir).readFileCoverage(1)); + try { + BatchReport.Coverage coverage = BatchReport.Coverage.PARSER.parseDelimitedFrom(inputStream); + assertThat(coverage.getLine()).isEqualTo(1); + assertThat(coverage.getConditions()).isEqualTo(1); + assertThat(coverage.getUtHits()).isTrue(); + assertThat(coverage.getItHits()).isFalse(); + assertThat(coverage.getUtCoveredConditions()).isEqualTo(1); + assertThat(coverage.getItCoveredConditions()).isEqualTo(1); + assertThat(coverage.getOverallCoveredConditions()).isEqualTo(1); + + coverage = BatchReport.Coverage.PARSER.parseDelimitedFrom(inputStream); + assertThat(coverage.getLine()).isEqualTo(2); + assertThat(coverage.getConditions()).isEqualTo(5); + assertThat(coverage.getUtHits()).isFalse(); + assertThat(coverage.getItHits()).isFalse(); + assertThat(coverage.getUtCoveredConditions()).isEqualTo(4); + assertThat(coverage.getItCoveredConditions()).isEqualTo(5); + assertThat(coverage.getOverallCoveredConditions()).isEqualTo(5); + } finally { + inputStream.close(); + } + } + + @Test + public void read_source_lines() throws Exception { + File dir = temp.newFolder(); + BatchReportWriter writer = new BatchReportWriter(dir); + File file = writer.getFileStructure().fileFor(FileStructure.Domain.SOURCE, 1); + FileUtils.writeLines(file, Lists.newArrayList("line1", "line2")); + + File sourceFile = new BatchReportReader(dir).readFileSource(1); + assertThat(sourceFile).isEqualTo(file); } @Test(expected = IllegalStateException.class) @@ -261,9 +276,9 @@ public class BatchReportReaderTest { assertThat(sut.readFileCoverage(123)).isNull(); } - @Test(expected = IllegalStateException.class) - public void fail_if_no_source_found() throws Exception { - assertThat(sut.readSourceLines(123)).isNull(); + @Test + public void return_null_if_no_source_found() throws Exception { + assertThat(sut.readFileCoverage(123)).isNull(); } /** diff --git a/sonar-batch-protocol/src/test/java/org/sonar/batch/protocol/output/BatchReportWriterTest.java b/sonar-batch-protocol/src/test/java/org/sonar/batch/protocol/output/BatchReportWriterTest.java index 580cf848d0e..b0d666a52d7 100644 --- a/sonar-batch-protocol/src/test/java/org/sonar/batch/protocol/output/BatchReportWriterTest.java +++ b/sonar-batch-protocol/src/test/java/org/sonar/batch/protocol/output/BatchReportWriterTest.java @@ -19,24 +19,18 @@ */ package org.sonar.batch.protocol.output; -import com.google.common.collect.Lists; import org.apache.commons.io.FileUtils; -import org.apache.commons.io.IOUtils; import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; import org.sonar.batch.protocol.Constants; -import org.sonar.batch.protocol.FileStream; import org.sonar.batch.protocol.ProtobufUtil; -import org.sonar.batch.protocol.ReportStream; import org.sonar.batch.protocol.output.BatchReport.Range; import java.io.File; import java.util.Arrays; -import java.util.List; import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.util.Lists.newArrayList; public class BatchReportWriterTest { @@ -320,63 +314,10 @@ public class BatchReportWriterTest { .setUtCoveredConditions(1) .setItCoveredConditions(1) .setOverallCoveredConditions(1) - .build(), - BatchReport.Coverage.newBuilder() - .setLine(2) - .setConditions(5) - .setUtHits(false) - .setItHits(false) - .setUtCoveredConditions(4) - .setItCoveredConditions(5) - .setOverallCoveredConditions(5) .build() - )); + )); assertThat(writer.hasComponentData(FileStructure.Domain.COVERAGE, 1)).isTrue(); - - ReportStream coverageReportStream = null; - try { - coverageReportStream = new BatchReportReader(dir).readFileCoverage(1); - List<BatchReport.Coverage> coverageList = newArrayList(coverageReportStream); - assertThat(coverageList).hasSize(2); - - BatchReport.Coverage coverage = coverageList.get(0); - assertThat(coverage.getLine()).isEqualTo(1); - assertThat(coverage.getConditions()).isEqualTo(1); - assertThat(coverage.getUtHits()).isTrue(); - assertThat(coverage.getItHits()).isFalse(); - assertThat(coverage.getUtCoveredConditions()).isEqualTo(1); - assertThat(coverage.getItCoveredConditions()).isEqualTo(1); - assertThat(coverage.getOverallCoveredConditions()).isEqualTo(1); - - coverage = coverageList.get(1); - assertThat(coverage.getLine()).isEqualTo(2); - assertThat(coverage.getConditions()).isEqualTo(5); - assertThat(coverage.getUtHits()).isFalse(); - assertThat(coverage.getItHits()).isFalse(); - assertThat(coverage.getUtCoveredConditions()).isEqualTo(4); - assertThat(coverage.getItCoveredConditions()).isEqualTo(5); - assertThat(coverage.getOverallCoveredConditions()).isEqualTo(5); - } finally { - IOUtils.closeQuietly(coverageReportStream); - } - } - - @Test - public void read_source_lines() throws Exception { - File dir = temp.newFolder(); - BatchReportWriter writer = new BatchReportWriter(dir); - File file = writer.getFileStructure().fileFor(FileStructure.Domain.SOURCE, 1); - assertThat(file.exists()); - FileUtils.writeLines(file, Lists.newArrayList("line1", "line2")); - - FileStream fileStream = null; - try { - fileStream = new BatchReportReader(dir).readSourceLines(1); - assertThat(fileStream).hasSize(2); - } finally { - fileStream.close(); - } } } |