diff options
Diffstat (limited to 'sonar-batch-protocol/src/test/java')
4 files changed, 117 insertions, 22 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 new file mode 100644 index 00000000000..03d82c5629d --- /dev/null +++ b/sonar-batch-protocol/src/test/java/org/sonar/batch/protocol/FileStreamTest.java @@ -0,0 +1,75 @@ +/* + * 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 index 5523cafc34b..d71c071de7a 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 @@ -20,7 +20,6 @@ package org.sonar.batch.protocol; -import org.apache.commons.io.IOUtils; import org.junit.After; import org.junit.Before; import org.junit.Rule; @@ -36,7 +35,6 @@ 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 { @@ -56,33 +54,31 @@ public class ReportStreamTest { BatchReport.Coverage.newBuilder() .setLine(1) .build() - )); + )); file = new FileStructure(dir).fileFor(FileStructure.Domain.COVERAGE, 1); } @After public void tearDown() throws Exception { - IOUtils.closeQuietly(sut); + if (sut != null) { + sut.close(); + } } @Test public void read_report() throws Exception { sut = new ReportStream<>(file, BatchReport.Coverage.PARSER); - 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(); + assertThat(sut).hasSize(1); } - @Test - public void next_should_be_reentrant() throws Exception { + @Test(expected = IllegalStateException.class) + public void fail_to_get_iterator_twice() throws Exception { sut = new ReportStream<>(file, BatchReport.Coverage.PARSER); - assertThat(sut).hasSize(1); + sut.iterator(); - assertThat(sut.iterator().next()).isNotNull(); - assertThat(sut.iterator().next()).isNotNull(); + // Fail ! + sut.iterator(); } @Test(expected = NoSuchElementException.class) @@ -105,10 +101,4 @@ 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(); - } - } 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 a2fc91d48d5..73c3efca35f 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 @@ -24,6 +24,8 @@ import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; +import java.io.File; + import static org.assertj.core.api.Assertions.assertThat; public class BatchReportReaderTest { @@ -31,11 +33,14 @@ public class BatchReportReaderTest { @Rule public TemporaryFolder temp = new TemporaryFolder(); + File dir; + BatchReportReader sut; @Before public void setUp() throws Exception { - sut = new BatchReportReader(temp.newFolder()); + dir = temp.newFolder(); + sut = new BatchReportReader(dir); } @Test(expected = IllegalStateException.class) @@ -83,6 +88,11 @@ 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(); + } + /** * no file if no issues */ 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 9ce703d4f3c..580cf848d0e 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,12 +19,14 @@ */ 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; @@ -328,7 +330,7 @@ public class BatchReportWriterTest { .setItCoveredConditions(5) .setOverallCoveredConditions(5) .build() - )); + )); assertThat(writer.hasComponentData(FileStructure.Domain.COVERAGE, 1)).isTrue(); @@ -359,4 +361,22 @@ public class BatchReportWriterTest { 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(); + } + } + } |