diff options
author | Simon Brandhof <simon.brandhof@sonarsource.com> | 2015-07-20 00:22:54 +0200 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@sonarsource.com> | 2015-07-20 22:30:42 +0200 |
commit | 399f6c100092eff8eef6f99b7bfb8051af2d4f68 (patch) | |
tree | 208532e628886a7207d450a3eb1d90f115333ada /sonar-batch-protocol/src/test/java | |
parent | 548a1ccb48cd1d403602670d923bf73b7bd70c0e (diff) | |
download | sonarqube-399f6c100092eff8eef6f99b7bfb8051af2d4f68.tar.gz sonarqube-399f6c100092eff8eef6f99b7bfb8051af2d4f68.zip |
SONAR-6703 load common rule parameters from batch report
Diffstat (limited to 'sonar-batch-protocol/src/test/java')
3 files changed, 89 insertions, 62 deletions
diff --git a/sonar-batch-protocol/src/test/java/org/sonar/batch/protocol/ProtobufUtilTest.java b/sonar-batch-protocol/src/test/java/org/sonar/batch/protocol/ProtobufUtilTest.java index e9e57cf74dd..04ebcf7f989 100644 --- a/sonar-batch-protocol/src/test/java/org/sonar/batch/protocol/ProtobufUtilTest.java +++ b/sonar-batch-protocol/src/test/java/org/sonar/batch/protocol/ProtobufUtilTest.java @@ -19,16 +19,53 @@ */ package org.sonar.batch.protocol; +import java.io.File; +import org.apache.commons.io.FileUtils; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.junit.rules.TemporaryFolder; +import org.sonar.batch.protocol.output.BatchReport; import org.sonar.test.TestUtils; import static org.assertj.core.api.Assertions.assertThat; public class ProtobufUtilTest { + @Rule + public ExpectedException thrown = ExpectedException.none(); + + @Rule + public TemporaryFolder temp = new TemporaryFolder(); + @Test public void only_utils() { assertThat(TestUtils.hasOnlyPrivateConstructors(ProtobufUtil.class)); } + @Test + public void readFile_fails_if_file_does_not_exist() throws Exception { + thrown.expect(IllegalStateException.class); + + File file = temp.newFile(); + FileUtils.forceDelete(file); + ProtobufUtil.readFile(file, BatchReport.Metadata.PARSER); + } + + @Test + public void readFile_returns_empty_message_if_file_is_empty() throws Exception { + File file = temp.newFile(); + BatchReport.Metadata msg = ProtobufUtil.readFile(file, BatchReport.Metadata.PARSER); + assertThat(msg).isNotNull(); + assertThat(msg.isInitialized()).isTrue(); + } + + @Test + public void readFile_returns_message() throws Exception { + File file = temp.newFile(); + ProtobufUtil.writeToFile(BatchReport.Metadata.getDefaultInstance(), file); + BatchReport.Metadata message = ProtobufUtil.readFile(file, BatchReport.Metadata.PARSER); + assertThat(message).isNotNull(); + assertThat(message.isInitialized()).isTrue(); + } } 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 50afe349b6d..9250a23042e 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 @@ -42,12 +42,12 @@ public class BatchReportReaderTest { File dir; - BatchReportReader underTest; + BatchReportReader sut; @Before public void setUp() throws Exception { dir = temp.newFolder(); - underTest = new BatchReportReader(dir); + sut = new BatchReportReader(dir); } @Test @@ -59,7 +59,7 @@ public class BatchReportReaderTest { .setRootComponentRef(1); writer.writeMetadata(metadata.build()); - BatchReport.Metadata readMetadata = underTest.readMetadata(); + BatchReport.Metadata readMetadata = sut.readMetadata(); assertThat(readMetadata.getAnalysisDate()).isEqualTo(15000000L); assertThat(readMetadata.getProjectKey()).isEqualTo("PROJECT_A"); assertThat(readMetadata.getRootComponentRef()).isEqualTo(1); @@ -67,7 +67,7 @@ public class BatchReportReaderTest { @Test(expected = IllegalStateException.class) public void fail_if_missing_metadata_file() { - underTest.readMetadata(); + sut.readMetadata(); } @Test @@ -78,12 +78,12 @@ public class BatchReportReaderTest { .setPath("src/main/java/Foo.java"); writer.writeComponent(component.build()); - assertThat(underTest.readComponent(1).getPath()).isEqualTo("src/main/java/Foo.java"); + assertThat(sut.readComponent(1).getPath()).isEqualTo("src/main/java/Foo.java"); } @Test(expected = IllegalStateException.class) public void fail_if_missing_file_on_component() { - underTest.readComponent(UNKNOWN_COMPONENT_REF); + sut.readComponent(UNKNOWN_COMPONENT_REF); } @Test @@ -94,13 +94,13 @@ public class BatchReportReaderTest { .build(); writer.writeComponentIssues(1, Arrays.asList(issue)); - assertThat(underTest.readComponentIssues(1)).hasSize(1); - assertThat(underTest.readComponentIssues(200)).isEmpty(); + assertThat(sut.readComponentIssues(1)).hasSize(1); + assertThat(sut.readComponentIssues(200)).isEmpty(); } @Test public void empty_list_if_no_issue_found() { - assertThat(underTest.readComponentIssues(UNKNOWN_COMPONENT_REF)).isEmpty(); + assertThat(sut.readComponentIssues(UNKNOWN_COMPONENT_REF)).isEmpty(); } @Test @@ -110,13 +110,12 @@ public class BatchReportReaderTest { .setStringValue("value_a"); writer.writeComponentMeasures(1, Arrays.asList(measure.build())); - assertThat(underTest.readComponentMeasures(1)).hasSize(1); - assertThat(underTest.readComponentMeasures(1).get(0).getStringValue()).isEqualTo("value_a"); + assertThat(sut.readComponentMeasures(1)).hasSize(1); } @Test public void empty_list_if_no_measure_found() { - assertThat(underTest.readComponentMeasures(UNKNOWN_COMPONENT_REF)).isEmpty(); + assertThat(sut.readComponentMeasures(UNKNOWN_COMPONENT_REF)).isEmpty(); } @Test @@ -127,13 +126,13 @@ public class BatchReportReaderTest { .addChangeset(BatchReport.Changesets.Changeset.newBuilder().setDate(123_456_789).setAuthor("jack.daniels").setRevision("123-456-789")); writer.writeComponentChangesets(scm.build()); - assertThat(underTest.readChangesets(1).getChangesetList()).hasSize(1); - assertThat(underTest.readChangesets(1).getChangeset(0).getDate()).isEqualTo(123_456_789L); + assertThat(sut.readChangesets(1).getChangesetList()).hasSize(1); + assertThat(sut.readChangesets(1).getChangeset(0).getDate()).isEqualTo(123_456_789L); } @Test public void null_if_no_changeset_found() { - assertThat(underTest.readChangesets(UNKNOWN_COMPONENT_REF)).isNull(); + assertThat(sut.readChangesets(UNKNOWN_COMPONENT_REF)).isNull(); } @Test @@ -160,15 +159,13 @@ public class BatchReportReaderTest { .build(); writer.writeComponentDuplications(1, Arrays.asList(duplication)); - BatchReportReader underTest = new BatchReportReader(dir); - assertThat(underTest.readComponentDuplications(1)).hasSize(1); - assertThat(underTest.readComponentDuplications(1).get(0).getOriginPosition()).isNotNull(); - assertThat(underTest.readComponentDuplications(1).get(0).getDuplicateList()).hasSize(1); + BatchReportReader sut = new BatchReportReader(dir); + assertThat(sut.readComponentDuplications(1)).hasSize(1); } @Test public void empty_list_if_no_duplication_found() { - assertThat(underTest.readComponentDuplications(UNKNOWN_COMPONENT_REF)).isEmpty(); + assertThat(sut.readComponentDuplications(UNKNOWN_COMPONENT_REF)).isEmpty(); } @Test @@ -188,9 +185,9 @@ public class BatchReportReaderTest { .build()) .setType(Constants.HighlightingType.ANNOTATION) .build() - )); + )); - try (InputStream inputStream = FileUtils.openInputStream(underTest.readComponentSyntaxHighlighting(1))) { + try (InputStream inputStream = FileUtils.openInputStream(sut.readComponentSyntaxHighlighting(1))) { BatchReport.SyntaxHighlighting syntaxHighlighting = BatchReport.SyntaxHighlighting.PARSER.parseDelimitedFrom(inputStream); assertThat(syntaxHighlighting.getRange()).isNotNull(); assertThat(syntaxHighlighting.getRange().getStartLine()).isEqualTo(1); @@ -201,7 +198,7 @@ public class BatchReportReaderTest { @Test public void return_null_if_no_highlighting_found() { - assertThat(underTest.readComponentSyntaxHighlighting(UNKNOWN_COMPONENT_REF)).isNull(); + assertThat(sut.readComponentSyntaxHighlighting(UNKNOWN_COMPONENT_REF)).isNull(); } @Test @@ -213,7 +210,7 @@ public class BatchReportReaderTest { writer.writeComponent(BatchReport.Component.newBuilder() .setRef(1).build()); - writer.writeComponentSymbols(1, Arrays.asList(BatchReport.Symbols.Symbol.newBuilder() + writer.writeComponentSymbols(1, Arrays.asList(BatchReport.Symbol.newBuilder() .setDeclaration(BatchReport.Range.newBuilder() .setStartLine(1) .setStartOffset(3) @@ -228,15 +225,13 @@ public class BatchReportReaderTest { .build()) .build())); - underTest = new BatchReportReader(dir); - assertThat(underTest.readComponentSymbols(1)).hasSize(1); - assertThat(underTest.readComponentSymbols(1).get(0).getDeclaration().getStartLine()).isEqualTo(1); - assertThat(underTest.readComponentSymbols(1).get(0).getReference(0).getStartLine()).isEqualTo(10); + sut = new BatchReportReader(dir); + assertThat(sut.readComponentSymbols(1)).hasSize(1); } @Test public void empty_list_if_no_symbol_found() { - assertThat(underTest.readComponentSymbols(UNKNOWN_COMPONENT_REF)).isEmpty(); + assertThat(sut.readComponentSymbols(UNKNOWN_COMPONENT_REF)).isEmpty(); } @Test @@ -268,7 +263,7 @@ public class BatchReportReaderTest { .setOverallCoveredConditions(5) .build())); - underTest = new BatchReportReader(dir); + sut = new BatchReportReader(dir); try (InputStream inputStream = FileUtils.openInputStream(new BatchReportReader(dir).readComponentCoverage(1))) { BatchReport.Coverage coverage = BatchReport.Coverage.PARSER.parseDelimitedFrom(inputStream); @@ -293,7 +288,7 @@ public class BatchReportReaderTest { @Test public void return_null_if_no_coverage_found() { - assertThat(underTest.readComponentCoverage(UNKNOWN_COMPONENT_REF)).isNull(); + assertThat(sut.readComponentCoverage(UNKNOWN_COMPONENT_REF)).isNull(); } @Test @@ -317,7 +312,7 @@ public class BatchReportReaderTest { .setStatus(Constants.TestStatus.OK) .build())); - try (InputStream inputStream = FileUtils.openInputStream(underTest.readTests(1))) { + try (InputStream inputStream = FileUtils.openInputStream(sut.readTests(1))) { BatchReport.Test testResult = BatchReport.Test.PARSER.parseDelimitedFrom(inputStream); assertThat(testResult.getDurationInMs()).isEqualTo(60_000); assertThat(testResult.getStacktrace()).isEqualTo("stacktrace"); @@ -328,7 +323,7 @@ public class BatchReportReaderTest { @Test public void null_if_no_test_found() { - assertThat(underTest.readTests(UNKNOWN_COMPONENT_REF)).isNull(); + assertThat(sut.readTests(UNKNOWN_COMPONENT_REF)).isNull(); } @Test @@ -338,13 +333,13 @@ public class BatchReportReaderTest { BatchReport.CoverageDetail.newBuilder() .setTestName("test-name") .addCoveredFile(BatchReport.CoverageDetail.CoveredFile.newBuilder() - .addAllCoveredLine(Arrays.asList(1, 2, 3, 5, 7)) - .setFileRef(2) + .addAllCoveredLine(Arrays.asList(1, 2, 3, 5, 7)) + .setFileRef(2) ) .build() - )); + )); - try (InputStream inputStream = FileUtils.openInputStream(underTest.readCoverageDetails(1))) { + try (InputStream inputStream = FileUtils.openInputStream(sut.readCoverageDetails(1))) { BatchReport.CoverageDetail coverageDetail = BatchReport.CoverageDetail.PARSER.parseDelimitedFrom(inputStream); assertThat(coverageDetail.getTestName()).isEqualTo("test-name"); assertThat(coverageDetail.getCoveredFile(0).getFileRef()).isEqualTo(2); @@ -354,7 +349,7 @@ public class BatchReportReaderTest { @Test public void null_if_no_coverage_detail_found() { - assertThat(underTest.readCoverageDetails(UNKNOWN_COMPONENT_REF)).isNull(); + assertThat(sut.readCoverageDetails(UNKNOWN_COMPONENT_REF)).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 18716f7332f..9154effb05c 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,6 +19,9 @@ */ package org.sonar.batch.protocol.output; +import com.google.common.collect.Iterators; +import java.io.File; +import java.util.Arrays; import org.apache.commons.io.FileUtils; import org.junit.Before; import org.junit.Rule; @@ -27,9 +30,7 @@ import org.junit.rules.TemporaryFolder; import org.sonar.batch.protocol.Constants; import org.sonar.batch.protocol.ProtobufUtil; import org.sonar.batch.protocol.output.BatchReport.Range; - -import java.io.File; -import java.util.Arrays; +import org.sonar.core.util.CloseableIterator; import static org.assertj.core.api.Assertions.assertThat; @@ -110,9 +111,9 @@ public class BatchReportWriterTest { assertThat(underTest.hasComponentData(FileStructure.Domain.ISSUES, 1)).isTrue(); File file = underTest.getFileStructure().fileFor(FileStructure.Domain.ISSUES, 1); assertThat(file).exists().isFile(); - BatchReport.Issues read = ProtobufUtil.readFile(file, BatchReport.Issues.PARSER); - assertThat(read.getComponentRef()).isEqualTo(1); - assertThat(read.getIssueCount()).isEqualTo(1); + try (CloseableIterator<BatchReport.Issue> read = ProtobufUtil.readStreamFromFile(file, BatchReport.Issue.PARSER)) { + assertThat(Iterators.size(read)).isEqualTo(1); + } } @Test @@ -131,13 +132,9 @@ public class BatchReportWriterTest { assertThat(underTest.hasComponentData(FileStructure.Domain.MEASURES, 1)).isTrue(); File file = underTest.getFileStructure().fileFor(FileStructure.Domain.MEASURES, 1); assertThat(file).exists().isFile(); - BatchReport.Measures measures = ProtobufUtil.readFile(file, BatchReport.Measures.PARSER); - assertThat(measures.getComponentRef()).isEqualTo(1); - assertThat(measures.getMeasureCount()).isEqualTo(1); - assertThat(measures.getMeasure(0).getStringValue()).isEqualTo("text-value"); - assertThat(measures.getMeasure(0).getDoubleValue()).isEqualTo(2.5d); - assertThat(measures.getMeasure(0).getValueType()).isEqualTo(Constants.MeasureValueType.DOUBLE); - assertThat(measures.getMeasure(0).getDescription()).isEqualTo("description"); + try (CloseableIterator<BatchReport.Measure> read = ProtobufUtil.readStreamFromFile(file, BatchReport.Measure.PARSER)) { + assertThat(Iterators.size(read)).isEqualTo(1); + } } @Test @@ -188,11 +185,11 @@ public class BatchReportWriterTest { assertThat(underTest.hasComponentData(FileStructure.Domain.DUPLICATIONS, 1)).isTrue(); File file = underTest.getFileStructure().fileFor(FileStructure.Domain.DUPLICATIONS, 1); assertThat(file).exists().isFile(); - BatchReport.Duplications duplications = ProtobufUtil.readFile(file, BatchReport.Duplications.PARSER); - assertThat(duplications.getComponentRef()).isEqualTo(1); - assertThat(duplications.getDuplicationList()).hasSize(1); - assertThat(duplications.getDuplication(0).getOriginPosition()).isNotNull(); - assertThat(duplications.getDuplication(0).getDuplicateList()).hasSize(1); + try (CloseableIterator<BatchReport.Duplication> duplications = ProtobufUtil.readStreamFromFile(file, BatchReport.Duplication.PARSER)) { + BatchReport.Duplication dup = duplications.next(); + assertThat(dup.getOriginPosition()).isNotNull(); + assertThat(dup.getDuplicateList()).hasSize(1); + } } @Test @@ -201,7 +198,7 @@ public class BatchReportWriterTest { assertThat(underTest.hasComponentData(FileStructure.Domain.SYMBOLS, 1)).isFalse(); // write data - BatchReport.Symbols.Symbol symbol = BatchReport.Symbols.Symbol.newBuilder() + BatchReport.Symbol symbol = BatchReport.Symbol.newBuilder() .setDeclaration(BatchReport.Range.newBuilder() .setStartLine(1) .setStartOffset(3) @@ -222,11 +219,9 @@ public class BatchReportWriterTest { File file = underTest.getFileStructure().fileFor(FileStructure.Domain.SYMBOLS, 1); assertThat(file).exists().isFile(); - BatchReport.Symbols read = ProtobufUtil.readFile(file, BatchReport.Symbols.PARSER); - assertThat(read.getFileRef()).isEqualTo(1); - assertThat(read.getSymbolList()).hasSize(1); - assertThat(read.getSymbol(0).getDeclaration().getStartLine()).isEqualTo(1); - assertThat(read.getSymbol(0).getReference(0).getStartLine()).isEqualTo(10); + try (CloseableIterator<BatchReport.Symbol> read = ProtobufUtil.readStreamFromFile(file, BatchReport.Symbol.PARSER)) { + assertThat(read).hasSize(1); + } } @Test |