diff options
author | Julien Lancelot <julien.lancelot@sonarsource.com> | 2015-03-31 18:30:05 +0200 |
---|---|---|
committer | Julien Lancelot <julien.lancelot@sonarsource.com> | 2015-04-01 08:27:28 +0200 |
commit | 2d67b3891b6ce1e9fec8d80107d5f373cf06869e (patch) | |
tree | 07f1a80529772ed4912f2742f419147ccf4d6980 /sonar-batch-protocol/src/test | |
parent | aea924c501cf7eb2be0a374d790459780e6abb76 (diff) | |
download | sonarqube-2d67b3891b6ce1e9fec8d80107d5f373cf06869e.tar.gz sonarqube-2d67b3891b6ce1e9fec8d80107d5f373cf06869e.zip |
Improve message streaming
Diffstat (limited to 'sonar-batch-protocol/src/test')
3 files changed, 135 insertions, 23 deletions
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 new file mode 100644 index 00000000000..0c965e90f64 --- /dev/null +++ b/sonar-batch-protocol/src/test/java/org/sonar/batch/protocol/ReportStreamTest.java @@ -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(); + } + +} 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 651a7c486b1..a2fc91d48d5 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 @@ -79,8 +79,8 @@ public class BatchReportReaderTest { } @Test - public void empty_list_if_no_coverage_found() throws Exception { - assertThat(sut.readFileCoverage(123)).isEmpty(); + public void return_null_if_no_coverage_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 1d52b985975..9ce703d4f3c 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 @@ -20,11 +20,13 @@ package org.sonar.batch.protocol.output; 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.ProtobufUtil; +import org.sonar.batch.protocol.ReportStream; import org.sonar.batch.protocol.output.BatchReport.Range; import java.io.File; @@ -326,29 +328,35 @@ public class BatchReportWriterTest { .setItCoveredConditions(5) .setOverallCoveredConditions(5) .build() - )); + )); assertThat(writer.hasComponentData(FileStructure.Domain.COVERAGE, 1)).isTrue(); - List<BatchReport.Coverage> coverageList = newArrayList(new BatchReportReader(dir).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); + 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); + } } } |