aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-batch-protocol
diff options
context:
space:
mode:
authorJulien Lancelot <julien.lancelot@sonarsource.com>2015-03-31 16:04:06 +0200
committerJulien Lancelot <julien.lancelot@sonarsource.com>2015-03-31 16:04:06 +0200
commit0d0d869e1c850925e74c2640c1d8517550935c6c (patch)
treed5be80ab24e20db2bc9411809c5d1dc4104e9126 /sonar-batch-protocol
parentcf4106ffa318702ea4bf9cf62a3ae4a17261371c (diff)
downloadsonarqube-0d0d869e1c850925e74c2640c1d8517550935c6c.tar.gz
sonarqube-0d0d869e1c850925e74c2640c1d8517550935c6c.zip
Simplify tests
Diffstat (limited to 'sonar-batch-protocol')
-rw-r--r--sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/output/BatchReportWriter.java11
-rw-r--r--sonar-batch-protocol/src/test/java/org/sonar/batch/protocol/output/BatchReportReaderTest.java231
-rw-r--r--sonar-batch-protocol/src/test/java/org/sonar/batch/protocol/output/BatchReportWriterTest.java115
3 files changed, 83 insertions, 274 deletions
diff --git a/sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/output/BatchReportWriter.java b/sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/output/BatchReportWriter.java
index c08ed0e1156..51d17f9afa1 100644
--- a/sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/output/BatchReportWriter.java
+++ b/sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/output/BatchReportWriter.java
@@ -21,7 +21,7 @@ package org.sonar.batch.protocol.output;
import org.sonar.batch.protocol.ProtobufUtil;
-import java.io.*;
+import java.io.File;
public class BatchReportWriter {
@@ -118,13 +118,4 @@ public class BatchReportWriter {
ProtobufUtil.writeMessagesToFile(coverageList, file);
}
- public OutputStream openCoverageOutputStream(int componentRef) {
- File file = fileStructure.fileFor(FileStructure.Domain.COVERAGE, componentRef);
- try {
- return new BufferedOutputStream(new FileOutputStream(file, true));
- } catch (FileNotFoundException e) {
- throw new IllegalStateException("Unable to find file " + file, e);
- }
- }
-
}
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 817c0a6fd97..651a7c486b1 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
@@ -23,17 +23,8 @@ import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
-import org.sonar.batch.protocol.Constants;
-import org.sonar.batch.protocol.output.BatchReport.Issues;
-import org.sonar.batch.protocol.output.BatchReport.Metadata;
-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 BatchReportReaderTest {
@@ -47,175 +38,6 @@ public class BatchReportReaderTest {
sut = new BatchReportReader(temp.newFolder());
}
- @Test
- public void create_dir_if_does_not_exist() throws Exception {
- File dir = temp.newFolder();
-
- initFiles(dir);
-
- sut = new BatchReportReader(dir);
- Metadata readMetadata = sut.readMetadata();
- assertThat(readMetadata.getAnalysisDate()).isEqualTo(15000000L);
- assertThat(readMetadata.getDeletedComponentsCount()).isEqualTo(1);
- assertThat(sut.readComponentIssues(1)).hasSize(1);
- assertThat(sut.readComponentIssues(200)).isEmpty();
- assertThat(sut.readComponent(1).getUuid()).isEqualTo("UUID_A");
- Issues deletedComponentIssues = sut.readDeletedComponentIssues(1);
- assertThat(deletedComponentIssues.getComponentUuid()).isEqualTo("compUuid");
- assertThat(deletedComponentIssues.getIssueList()).hasSize(1);
- assertThat(sut.readComponentMeasures(1)).hasSize(1);
- assertThat(sut.readComponentMeasures(1).get(0).getStringValue()).isEqualTo("value_a");
- assertThat(sut.readComponentScm(1).getChangesetList()).hasSize(1);
- assertThat(sut.readComponentScm(1).getChangeset(0).getDate()).isEqualTo(123_456_789L);
- }
-
- @Test
- public void read_duplications() throws Exception {
- File dir = temp.newFolder();
- BatchReportWriter writer = new BatchReportWriter(dir);
-
- writer.writeMetadata(BatchReport.Metadata.newBuilder()
- .setRootComponentRef(1).build());
-
- writer.writeComponent(BatchReport.Component.newBuilder()
- .setRef(1).build());
-
- BatchReport.Duplication duplication = BatchReport.Duplication.newBuilder()
- .setOriginPosition(Range.newBuilder()
- .setStartLine(1)
- .setEndLine(5)
- .build())
- .addDuplicate(BatchReport.Duplicate.newBuilder()
- .setOtherFileKey("COMPONENT_A")
- .setOtherFileRef(2)
- .setRange(Range.newBuilder()
- .setStartLine(6)
- .setEndLine(10)
- .build())
- .build())
- .build();
- writer.writeComponentDuplications(1, Arrays.asList(duplication));
-
- BatchReportReader sut = new BatchReportReader(dir);
- assertThat(sut.readComponentDuplications(1)).hasSize(1);
- assertThat(sut.readComponentDuplications(1).get(0).getOriginPosition()).isNotNull();
- assertThat(sut.readComponentDuplications(1).get(0).getDuplicateList()).hasSize(1);
- }
-
- @Test
- public void read_syntax_highlighting() throws Exception {
- File dir = temp.newFolder();
- BatchReportWriter writer = new BatchReportWriter(dir);
-
- writer.writeMetadata(BatchReport.Metadata.newBuilder()
- .setRootComponentRef(1).build());
-
- writer.writeComponent(BatchReport.Component.newBuilder()
- .setRef(1).build());
-
- BatchReport.SyntaxHighlighting.HighlightingRule highlightingRule = BatchReport.SyntaxHighlighting.HighlightingRule.newBuilder()
- .setRange(BatchReport.Range.newBuilder()
- .setStartLine(1)
- .setEndLine(1)
- .build())
- .setType(Constants.HighlightingType.ANNOTATION)
- .build();
- writer.writeComponentSyntaxHighlighting(1, Arrays.asList(highlightingRule));
-
- BatchReportReader sut = new BatchReportReader(dir);
- assertThat(sut.readComponentSyntaxHighlighting(1)).hasSize(1);
- assertThat(sut.readComponentSyntaxHighlighting(1).get(0).getRange()).isNotNull();
- assertThat(sut.readComponentSyntaxHighlighting(1).get(0).getType()).isEqualTo(Constants.HighlightingType.ANNOTATION);
- }
-
- @Test
- public void read_symbols() throws Exception {
- File dir = temp.newFolder();
- BatchReportWriter writer = new BatchReportWriter(dir);
-
- writer.writeMetadata(BatchReport.Metadata.newBuilder()
- .setRootComponentRef(1)
- .build());
-
- writer.writeComponent(BatchReport.Component.newBuilder()
- .setRef(1).build());
-
- writer.writeComponentSymbols(1, Arrays.asList(BatchReport.Symbols.Symbol.newBuilder()
- .setDeclaration(BatchReport.Range.newBuilder()
- .setStartLine(1)
- .setStartOffset(3)
- .setEndLine(1)
- .setEndOffset(5)
- .build())
- .addReference(BatchReport.Range.newBuilder()
- .setStartLine(10)
- .setStartOffset(15)
- .setEndLine(11)
- .setEndOffset(2)
- .build())
- .build()));
-
- sut = new BatchReportReader(dir);
- assertThat(sut.readComponentSymbols(1)).hasSize(1);
- assertThat(sut.readComponentSymbols(1).get(0).getDeclaration().getStartLine()).isEqualTo(1);
- assertThat(sut.readComponentSymbols(1).get(0).getReference(0).getStartLine()).isEqualTo(10);
- }
-
- @Test
- public void read_coverage() throws Exception {
- File dir = temp.newFolder();
- BatchReportWriter writer = new BatchReportWriter(dir);
-
- writer.writeMetadata(BatchReport.Metadata.newBuilder()
- .setRootComponentRef(1)
- .build());
-
- writer.writeComponent(BatchReport.Component.newBuilder()
- .setRef(1).build());
-
- writer.writeFileCoverage(1, Arrays.asList(
- BatchReport.Coverage.newBuilder()
- .setLine(1)
- .setConditions(1)
- .setUtHits(true)
- .setItHits(false)
- .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()));
-
- 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);
- }
-
@Test(expected = IllegalStateException.class)
public void fail_if_missing_metadata_file() throws Exception {
sut.readMetadata();
@@ -241,6 +63,26 @@ public class BatchReportReaderTest {
assertThat(sut.readComponentScm(666)).isNull();
}
+ @Test
+ public void empty_list_if_no_duplication_found() throws Exception {
+ assertThat(sut.readComponentDuplications(123)).isEmpty();
+ }
+
+ @Test
+ public void empty_list_if_no_symbol_found() throws Exception {
+ assertThat(sut.readComponentSymbols(123)).isEmpty();
+ }
+
+ @Test
+ public void empty_list_if_no_highlighting_found() throws Exception {
+ assertThat(sut.readComponentSyntaxHighlighting(123)).isEmpty();
+ }
+
+ @Test
+ public void empty_list_if_no_coverage_found() throws Exception {
+ assertThat(sut.readFileCoverage(123)).isEmpty();
+ }
+
/**
* no file if no issues
*/
@@ -249,37 +91,4 @@ public class BatchReportReaderTest {
assertThat(sut.readComponentIssues(666)).isEmpty();
}
- private void initFiles(File dir) {
- BatchReportWriter writer = new BatchReportWriter(dir);
-
- BatchReport.Metadata.Builder metadata = BatchReport.Metadata.newBuilder()
- .setAnalysisDate(15000000L)
- .setProjectKey("PROJECT_A")
- .setRootComponentRef(1)
- .setDeletedComponentsCount(1);
- writer.writeMetadata(metadata.build());
-
- BatchReport.Component.Builder component = BatchReport.Component.newBuilder()
- .setRef(1)
- .setUuid("UUID_A");
- writer.writeComponent(component.build());
-
- BatchReport.Issue issue = BatchReport.Issue.newBuilder()
- .setUuid("ISSUE_A")
- .setLine(50)
- .build();
-
- writer.writeComponentIssues(1, Arrays.asList(issue));
-
- writer.writeDeletedComponentIssues(1, "compUuid", Arrays.asList(issue));
-
- BatchReport.Measure.Builder measure = BatchReport.Measure.newBuilder()
- .setStringValue("value_a");
- writer.writeComponentMeasures(1, Arrays.asList(measure.build()));
-
- BatchReport.Scm.Builder scm = BatchReport.Scm.newBuilder()
- .setComponentRef(1)
- .addChangeset(BatchReport.Scm.Changeset.newBuilder().setDate(123_456_789).setAuthor("jack.daniels").setRevision("123-456-789"));
- writer.writeComponentScm(scm.build());
- }
}
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 d0bb79169ba..1d52b985975 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
@@ -27,13 +27,12 @@ import org.sonar.batch.protocol.Constants;
import org.sonar.batch.protocol.ProtobufUtil;
import org.sonar.batch.protocol.output.BatchReport.Range;
-import java.io.BufferedInputStream;
import java.io.File;
-import java.io.FileInputStream;
-import java.io.InputStream;
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 {
@@ -124,29 +123,29 @@ public class BatchReportWriterTest {
}
@Test
- public void write_scm() throws Exception {
+ public void write_issues_of_deleted_component() throws Exception {
File dir = temp.newFolder();
BatchReportWriter writer = new BatchReportWriter(dir);
- assertThat(writer.hasComponentData(FileStructure.Domain.SCM, 1)).isFalse();
+ // no data yet
+ assertThat(writer.hasComponentData(FileStructure.Domain.ISSUES_ON_DELETED, 1)).isFalse();
- BatchReport.Scm scm = BatchReport.Scm.newBuilder()
- .setComponentRef(1)
- .addChangesetIndexByLine(0)
- .addChangeset(BatchReport.Scm.Changeset.newBuilder()
- .setRevision("123-456-789")
- .setAuthor("author")
- .setDate(123_456_789L))
+ // write data
+ BatchReport.Issue issue = BatchReport.Issue.newBuilder()
+ .setUuid("ISSUE_A")
+ .setLine(50)
+ .setMsg("the message")
.build();
- writer.writeComponentScm(scm);
+ writer.writeDeletedComponentIssues(1, "componentUuid", Arrays.asList(issue));
- assertThat(writer.hasComponentData(FileStructure.Domain.SCM, 1)).isTrue();
- File file = writer.getFileStructure().fileFor(FileStructure.Domain.SCM, 1);
+ assertThat(writer.hasComponentData(FileStructure.Domain.ISSUES_ON_DELETED, 1)).isTrue();
+ File file = writer.getFileStructure().fileFor(FileStructure.Domain.ISSUES_ON_DELETED, 1);
assertThat(file).exists().isFile();
- BatchReport.Scm read = ProtobufUtil.readFile(file, BatchReport.Scm.PARSER);
+ BatchReport.Issues read = ProtobufUtil.readFile(file, BatchReport.Issues.PARSER);
assertThat(read.getComponentRef()).isEqualTo(1);
- assertThat(read.getChangesetCount()).isEqualTo(1);
+ assertThat(read.getComponentUuid()).isEqualTo("componentUuid");
+ assertThat(read.getIssueCount()).isEqualTo(1);
}
@Test
@@ -171,32 +170,38 @@ public class BatchReportWriterTest {
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");
}
@Test
- public void write_issues_of_deleted_component() throws Exception {
+ public void write_scm() throws Exception {
File dir = temp.newFolder();
BatchReportWriter writer = new BatchReportWriter(dir);
- // no data yet
- assertThat(writer.hasComponentData(FileStructure.Domain.ISSUES_ON_DELETED, 1)).isFalse();
+ assertThat(writer.hasComponentData(FileStructure.Domain.SCM, 1)).isFalse();
- // write data
- BatchReport.Issue issue = BatchReport.Issue.newBuilder()
- .setUuid("ISSUE_A")
- .setLine(50)
- .setMsg("the message")
+ BatchReport.Scm scm = BatchReport.Scm.newBuilder()
+ .setComponentRef(1)
+ .addChangesetIndexByLine(0)
+ .addChangeset(BatchReport.Scm.Changeset.newBuilder()
+ .setRevision("123-456-789")
+ .setAuthor("author")
+ .setDate(123_456_789L))
.build();
- writer.writeDeletedComponentIssues(1, "componentUuid", Arrays.asList(issue));
+ writer.writeComponentScm(scm);
- assertThat(writer.hasComponentData(FileStructure.Domain.ISSUES_ON_DELETED, 1)).isTrue();
- File file = writer.getFileStructure().fileFor(FileStructure.Domain.ISSUES_ON_DELETED, 1);
+ assertThat(writer.hasComponentData(FileStructure.Domain.SCM, 1)).isTrue();
+ File file = writer.getFileStructure().fileFor(FileStructure.Domain.SCM, 1);
assertThat(file).exists().isFile();
- BatchReport.Issues read = ProtobufUtil.readFile(file, BatchReport.Issues.PARSER);
+ BatchReport.Scm read = ProtobufUtil.readFile(file, BatchReport.Scm.PARSER);
assertThat(read.getComponentRef()).isEqualTo(1);
- assertThat(read.getComponentUuid()).isEqualTo("componentUuid");
- assertThat(read.getIssueCount()).isEqualTo(1);
+ assertThat(read.getChangesetCount()).isEqualTo(1);
+ assertThat(read.getChangesetList()).hasSize(1);
+ assertThat(read.getChangeset(0).getDate()).isEqualTo(123_456_789L);
}
@Test
@@ -228,6 +233,8 @@ public class BatchReportWriterTest {
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);
}
@Test
@@ -263,6 +270,8 @@ public class BatchReportWriterTest {
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);
}
@Test
@@ -287,6 +296,7 @@ public class BatchReportWriterTest {
BatchReport.SyntaxHighlighting syntaxHighlighting = ProtobufUtil.readFile(file, BatchReport.SyntaxHighlighting.PARSER);
assertThat(syntaxHighlighting.getFileRef()).isEqualTo(1);
assertThat(syntaxHighlighting.getHighlightingRuleList()).hasSize(1);
+ assertThat(syntaxHighlighting.getHighlightingRule(0).getType()).isEqualTo(Constants.HighlightingType.ANNOTATION);
}
@Test
@@ -316,30 +326,29 @@ public class BatchReportWriterTest {
.setItCoveredConditions(5)
.setOverallCoveredConditions(5)
.build()
- ));
+ ));
assertThat(writer.hasComponentData(FileStructure.Domain.COVERAGE, 1)).isTrue();
- File file = writer.getFileStructure().fileFor(FileStructure.Domain.COVERAGE, 1);
- assertThat(file).exists().isFile();
-
- InputStream inputStream = new BufferedInputStream(new FileInputStream(file));
- BatchReport.Coverage read = BatchReport.Coverage.PARSER.parseDelimitedFrom(inputStream);
- assertThat(read.getLine()).isEqualTo(1);
- assertThat(read.getConditions()).isEqualTo(1);
- assertThat(read.getUtHits()).isTrue();
- assertThat(read.getItHits()).isFalse();
- assertThat(read.getUtCoveredConditions()).isEqualTo(1);
- assertThat(read.getItCoveredConditions()).isEqualTo(1);
- assertThat(read.getOverallCoveredConditions()).isEqualTo(1);
-
- read = BatchReport.Coverage.PARSER.parseDelimitedFrom(inputStream);
- assertThat(read.getLine()).isEqualTo(2);
- assertThat(read.getConditions()).isEqualTo(5);
- assertThat(read.getUtHits()).isFalse();
- assertThat(read.getItHits()).isFalse();
- assertThat(read.getUtCoveredConditions()).isEqualTo(4);
- assertThat(read.getItCoveredConditions()).isEqualTo(5);
- assertThat(read.getOverallCoveredConditions()).isEqualTo(5);
+ 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);
}
}