aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-scanner-protocol/src/test
diff options
context:
space:
mode:
authorJulien HENRY <julien.henry@sonarsource.com>2016-03-17 17:05:56 +0100
committerJulien HENRY <julien.henry@sonarsource.com>2016-03-18 09:35:05 +0100
commitcfcbe278f7ced12599d898d50f3fe68bfbf95155 (patch)
tree5b4116ad08a8ba87ffc5bf9f159a431b9609b48f /sonar-scanner-protocol/src/test
parent38ce80934961773a9a35ec0401994b3d726597dc (diff)
downloadsonarqube-cfcbe278f7ced12599d898d50f3fe68bfbf95155.tar.gz
sonarqube-cfcbe278f7ced12599d898d50f3fe68bfbf95155.zip
Rename batch into scanner
Diffstat (limited to 'sonar-scanner-protocol/src/test')
-rw-r--r--sonar-scanner-protocol/src/test/java/org/sonar/scanner/protocol/input/GlobalRepositoriesTest.java67
-rw-r--r--sonar-scanner-protocol/src/test/java/org/sonar/scanner/protocol/output/FileStructureTest.java74
-rw-r--r--sonar-scanner-protocol/src/test/java/org/sonar/scanner/protocol/output/ScannerReportReaderTest.java391
-rw-r--r--sonar-scanner-protocol/src/test/java/org/sonar/scanner/protocol/output/ScannerReportWriterTest.java308
-rw-r--r--sonar-scanner-protocol/src/test/resources/org/sonar/scanner/protocol/input/GlobalRepositoriesTest/expected.json21
-rw-r--r--sonar-scanner-protocol/src/test/resources/org/sonar/scanner/protocol/input/ProjectRepositoriesTest/testToJson.json23
-rw-r--r--sonar-scanner-protocol/src/test/resources/org/sonar/scanner/protocol/input/RulesSearchTest/empty.json1
-rw-r--r--sonar-scanner-protocol/src/test/resources/org/sonar/scanner/protocol/input/RulesSearchTest/expected.json1
-rw-r--r--sonar-scanner-protocol/src/test/resources/org/sonar/scanner/protocol/output/component/ReportComponentsTest/expected.json43
9 files changed, 929 insertions, 0 deletions
diff --git a/sonar-scanner-protocol/src/test/java/org/sonar/scanner/protocol/input/GlobalRepositoriesTest.java b/sonar-scanner-protocol/src/test/java/org/sonar/scanner/protocol/input/GlobalRepositoriesTest.java
new file mode 100644
index 00000000000..703e182ea8d
--- /dev/null
+++ b/sonar-scanner-protocol/src/test/java/org/sonar/scanner/protocol/input/GlobalRepositoriesTest.java
@@ -0,0 +1,67 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact AT sonarsource DOT com
+ *
+ * This program 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.
+ *
+ * This program 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.scanner.protocol.input;
+
+import org.apache.commons.io.IOUtils;
+import org.junit.Test;
+import org.sonar.scanner.protocol.input.GlobalRepositories;
+import org.sonar.scanner.protocol.input.Metric;
+
+import static net.javacrumbs.jsonunit.assertj.JsonAssert.assertThatJson;
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class GlobalRepositoriesTest {
+
+ @Test
+ public void to_json() throws Exception {
+ GlobalRepositories ref = new GlobalRepositories();
+ ref.addMetric(new Metric(1, "ncloc", "INT", "Description", -1, "NCLOC", true, false, 2.0, 1.0, true));
+ ref.addGlobalSetting("prop", "value");
+ ref.setTimestamp(10);
+
+ assertThatJson(ref.toJson())
+ .isEqualTo(IOUtils.toString(getClass().getResource("GlobalRepositoriesTest/expected.json")));
+ }
+
+ @Test
+ public void from_json() {
+ GlobalRepositories ref = GlobalRepositories
+ .fromJson(
+ "{timestamp:1,"
+ + "metrics:[{id:1,key:ncloc,valueType:DATA,description:Description,direction:-1,name:NCLOC,qualitative:true,userManaged:false,worstValue:2.0,bestValue:1.0,optimizedBestValue:true}],"
+ + "globalSettings:{prop:value}}");
+
+ assertThat(ref.timestamp()).isEqualTo(1);
+ Metric metric = ref.metrics().iterator().next();
+ assertThat(metric.id()).isEqualTo(1);
+ assertThat(metric.key()).isEqualTo("ncloc");
+ assertThat(metric.valueType()).isEqualTo("DATA");
+ assertThat(metric.description()).isEqualTo("Description");
+ assertThat(metric.direction()).isEqualTo(-1);
+ assertThat(metric.name()).isEqualTo("NCLOC");
+ assertThat(metric.isQualitative()).isTrue();
+ assertThat(metric.isUserManaged()).isFalse();
+ assertThat(metric.worstValue()).isEqualTo(2.0);
+ assertThat(metric.bestValue()).isEqualTo(1.0);
+ assertThat(metric.isOptimizedBestValue()).isTrue();
+
+ assertThat(ref.globalSettings()).containsEntry("prop", "value");
+ }
+}
diff --git a/sonar-scanner-protocol/src/test/java/org/sonar/scanner/protocol/output/FileStructureTest.java b/sonar-scanner-protocol/src/test/java/org/sonar/scanner/protocol/output/FileStructureTest.java
new file mode 100644
index 00000000000..69d5266cf50
--- /dev/null
+++ b/sonar-scanner-protocol/src/test/java/org/sonar/scanner/protocol/output/FileStructureTest.java
@@ -0,0 +1,74 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact AT sonarsource DOT com
+ *
+ * This program 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.
+ *
+ * This program 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.scanner.protocol.output;
+
+import org.apache.commons.io.FileUtils;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+import org.sonar.scanner.protocol.output.FileStructure;
+import java.io.File;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.junit.Assert.fail;
+
+public class FileStructureTest {
+
+ @Rule
+ public TemporaryFolder temp = new TemporaryFolder();
+
+ @Test
+ public void fail_if_dir_does_not_exist() throws Exception {
+ File dir = temp.newFolder();
+ FileUtils.deleteQuietly(dir);
+ try {
+ new FileStructure(dir);
+ fail();
+ } catch (IllegalArgumentException e) {
+ assertThat(e).hasMessageContaining("Directory of analysis report does not exist");
+ }
+ }
+
+ @Test
+ public void fail_if_invalid_dir() throws Exception {
+ // not a dir but a file
+ File dir = temp.newFile();
+ try {
+ new FileStructure(dir);
+ fail();
+ } catch (IllegalArgumentException e) {
+ assertThat(e).hasMessageContaining("Directory of analysis report does not exist");
+ }
+ }
+
+ @Test
+ public void locate_files() throws Exception {
+ File dir = temp.newFolder();
+ FileUtils.write(new File(dir, "metadata.pb"), "metadata content");
+ FileUtils.write(new File(dir, "issues-3.pb"), "issues of component 3");
+ FileUtils.write(new File(dir, "component-42.pb"), "details of component 42");
+
+ FileStructure structure = new FileStructure(dir);
+ assertThat(structure.metadataFile()).exists().isFile();
+ assertThat(structure.fileFor(FileStructure.Domain.COMPONENT, 42)).exists().isFile();
+ assertThat(structure.fileFor(FileStructure.Domain.ISSUES, 3)).exists().isFile();
+ assertThat(structure.fileFor(FileStructure.Domain.ISSUES, 42)).doesNotExist();
+ }
+}
diff --git a/sonar-scanner-protocol/src/test/java/org/sonar/scanner/protocol/output/ScannerReportReaderTest.java b/sonar-scanner-protocol/src/test/java/org/sonar/scanner/protocol/output/ScannerReportReaderTest.java
new file mode 100644
index 00000000000..ce2a6e86149
--- /dev/null
+++ b/sonar-scanner-protocol/src/test/java/org/sonar/scanner/protocol/output/ScannerReportReaderTest.java
@@ -0,0 +1,391 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact AT sonarsource DOT com
+ *
+ * This program 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.
+ *
+ * This program 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.scanner.protocol.output;
+
+import com.google.common.collect.Lists;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.InputStream;
+import org.apache.commons.io.FileUtils;
+import org.apache.commons.io.IOUtils;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+import org.sonar.core.util.CloseableIterator;
+import org.sonar.scanner.protocol.Constants;
+import org.sonar.scanner.protocol.output.ScannerReportReader;
+import org.sonar.scanner.protocol.output.ScannerReportWriter;
+import org.sonar.scanner.protocol.output.FileStructure;
+
+import static java.util.Arrays.asList;
+import static java.util.Collections.singletonList;
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class ScannerReportReaderTest {
+
+ private static int UNKNOWN_COMPONENT_REF = 123;
+
+ @Rule
+ public TemporaryFolder temp = new TemporaryFolder();
+
+ File dir;
+
+ ScannerReportReader underTest;
+
+ @Before
+ public void setUp() throws Exception {
+ dir = temp.newFolder();
+ underTest = new ScannerReportReader(dir);
+ }
+
+ @Test
+ public void read_metadata() {
+ ScannerReportWriter writer = new ScannerReportWriter(dir);
+ ScannerReport.Metadata.Builder metadata = ScannerReport.Metadata.newBuilder()
+ .setAnalysisDate(15000000L)
+ .setProjectKey("PROJECT_A")
+ .setRootComponentRef(1)
+ .setCrossProjectDuplicationActivated(true);
+ writer.writeMetadata(metadata.build());
+
+ ScannerReport.Metadata readMetadata = underTest.readMetadata();
+ assertThat(readMetadata.getAnalysisDate()).isEqualTo(15000000L);
+ assertThat(readMetadata.getProjectKey()).isEqualTo("PROJECT_A");
+ assertThat(readMetadata.getRootComponentRef()).isEqualTo(1);
+ assertThat(readMetadata.getCrossProjectDuplicationActivated()).isTrue();
+ }
+
+ @Test(expected = IllegalStateException.class)
+ public void fail_if_missing_metadata_file() {
+ underTest.readMetadata();
+ }
+
+ @Test
+ public void read_components() {
+ ScannerReportWriter writer = new ScannerReportWriter(dir);
+ ScannerReport.Component.Builder component = ScannerReport.Component.newBuilder()
+ .setRef(1)
+ .setPath("src/main/java/Foo.java");
+ writer.writeComponent(component.build());
+
+ assertThat(underTest.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);
+ }
+
+ @Test
+ public void read_issues() {
+ ScannerReportWriter writer = new ScannerReportWriter(dir);
+ ScannerReport.Issue issue = ScannerReport.Issue.newBuilder()
+ .setLine(50)
+ .build();
+ writer.writeComponentIssues(1, asList(issue));
+
+ assertThat(underTest.readComponentIssues(1)).hasSize(1);
+ assertThat(underTest.readComponentIssues(200)).isEmpty();
+ }
+
+ @Test
+ public void empty_list_if_no_issue_found() {
+ assertThat(underTest.readComponentIssues(UNKNOWN_COMPONENT_REF)).isEmpty();
+ }
+
+ @Test
+ public void read_measures() {
+ ScannerReportWriter writer = new ScannerReportWriter(dir);
+ ScannerReport.Measure.Builder measure = ScannerReport.Measure.newBuilder()
+ .setStringValue("value_a");
+ writer.writeComponentMeasures(1, asList(measure.build()));
+
+ assertThat(underTest.readComponentMeasures(1)).hasSize(1);
+ }
+
+ @Test
+ public void empty_list_if_no_measure_found() {
+ assertThat(underTest.readComponentMeasures(UNKNOWN_COMPONENT_REF)).isEmpty();
+ }
+
+ @Test
+ public void read_changesets() {
+ ScannerReportWriter writer = new ScannerReportWriter(dir);
+ ScannerReport.Changesets.Builder scm = ScannerReport.Changesets.newBuilder()
+ .setComponentRef(1)
+ .addChangeset(ScannerReport.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);
+ }
+
+ @Test
+ public void null_if_no_changeset_found() {
+ assertThat(underTest.readChangesets(UNKNOWN_COMPONENT_REF)).isNull();
+ }
+
+ @Test
+ public void read_duplications() {
+ ScannerReportWriter writer = new ScannerReportWriter(dir);
+ writer.writeMetadata(ScannerReport.Metadata.newBuilder()
+ .setRootComponentRef(1).build());
+ writer.writeComponent(ScannerReport.Component.newBuilder()
+ .setRef(1).build());
+
+ ScannerReport.Duplication duplication = ScannerReport.Duplication.newBuilder()
+ .setOriginPosition(ScannerReport.TextRange.newBuilder()
+ .setStartLine(1)
+ .setEndLine(5)
+ .build())
+ .addDuplicate(ScannerReport.Duplicate.newBuilder()
+ .setOtherFileRef(2)
+ .setRange(ScannerReport.TextRange.newBuilder()
+ .setStartLine(6)
+ .setEndLine(10)
+ .build())
+ .build())
+ .build();
+ writer.writeComponentDuplications(1, asList(duplication));
+
+ ScannerReportReader sut = new ScannerReportReader(dir);
+ assertThat(sut.readComponentDuplications(1)).hasSize(1);
+ }
+
+ @Test
+ public void empty_list_if_no_duplication_found() {
+ assertThat(underTest.readComponentDuplications(UNKNOWN_COMPONENT_REF)).isEmpty();
+ }
+
+ @Test
+ public void read_duplication_blocks() {
+ ScannerReportWriter writer = new ScannerReportWriter(dir);
+ writer.writeMetadata(ScannerReport.Metadata.newBuilder()
+ .setRootComponentRef(1).build());
+ writer.writeComponent(ScannerReport.Component.newBuilder()
+ .setRef(1).build());
+
+ ScannerReport.CpdTextBlock duplicationBlock = ScannerReport.CpdTextBlock.newBuilder()
+ .setHash("abcdefghijklmnop")
+ .setStartLine(1)
+ .setEndLine(2)
+ .setStartTokenIndex(10)
+ .setEndTokenIndex(15)
+ .build();
+ writer.writeCpdTextBlocks(1, singletonList(duplicationBlock));
+
+ ScannerReportReader sut = new ScannerReportReader(dir);
+ assertThat(sut.readCpdTextBlocks(1)).hasSize(1);
+ }
+
+ @Test
+ public void empty_list_if_no_duplication_block_found() {
+ assertThat(underTest.readComponentDuplications(UNKNOWN_COMPONENT_REF)).isEmpty();
+ }
+
+ @Test
+ public void read_syntax_highlighting() throws Exception {
+ ScannerReportWriter writer = new ScannerReportWriter(dir);
+ writer.writeMetadata(ScannerReport.Metadata.newBuilder()
+ .setRootComponentRef(1)
+ .build());
+ writer.writeComponent(ScannerReport.Component.newBuilder()
+ .setRef(1).build());
+
+ writer.writeComponentSyntaxHighlighting(1, asList(
+ ScannerReport.SyntaxHighlighting.newBuilder()
+ .setRange(ScannerReport.TextRange.newBuilder()
+ .setStartLine(1)
+ .setEndLine(10)
+ .build())
+ .setType(Constants.HighlightingType.ANNOTATION)
+ .build()));
+
+ try (CloseableIterator<ScannerReport.SyntaxHighlighting> it = underTest.readComponentSyntaxHighlighting(1)) {
+ ScannerReport.SyntaxHighlighting syntaxHighlighting = it.next();
+ assertThat(syntaxHighlighting.getRange()).isNotNull();
+ assertThat(syntaxHighlighting.getRange().getStartLine()).isEqualTo(1);
+ assertThat(syntaxHighlighting.getRange().getEndLine()).isEqualTo(10);
+ assertThat(syntaxHighlighting.getType()).isEqualTo(Constants.HighlightingType.ANNOTATION);
+ }
+ }
+
+ @Test
+ public void return_empty_if_no_highlighting_found() {
+ assertThat(underTest.readComponentSyntaxHighlighting(UNKNOWN_COMPONENT_REF)).isEmpty();
+ }
+
+ @Test
+ public void read_symbols() {
+ ScannerReportWriter writer = new ScannerReportWriter(dir);
+ writer.writeMetadata(ScannerReport.Metadata.newBuilder()
+ .setRootComponentRef(1)
+ .build());
+ writer.writeComponent(ScannerReport.Component.newBuilder()
+ .setRef(1).build());
+
+ writer.writeComponentSymbols(1, asList(ScannerReport.Symbol.newBuilder()
+ .setDeclaration(ScannerReport.TextRange.newBuilder()
+ .setStartLine(1)
+ .setStartOffset(3)
+ .setEndLine(1)
+ .setEndOffset(5)
+ .build())
+ .addReference(ScannerReport.TextRange.newBuilder()
+ .setStartLine(10)
+ .setStartOffset(15)
+ .setEndLine(11)
+ .setEndOffset(2)
+ .build())
+ .build()));
+
+ underTest = new ScannerReportReader(dir);
+ assertThat(underTest.readComponentSymbols(1)).hasSize(1);
+ }
+
+ @Test
+ public void empty_list_if_no_symbol_found() {
+ assertThat(underTest.readComponentSymbols(UNKNOWN_COMPONENT_REF)).isEmpty();
+ }
+
+ @Test
+ public void read_coverage() throws Exception {
+ ScannerReportWriter writer = new ScannerReportWriter(dir);
+ writer.writeMetadata(ScannerReport.Metadata.newBuilder()
+ .setRootComponentRef(1)
+ .build());
+ writer.writeComponent(ScannerReport.Component.newBuilder()
+ .setRef(1).build());
+
+ writer.writeComponentCoverage(1, asList(
+ ScannerReport.Coverage.newBuilder()
+ .setLine(1)
+ .setConditions(1)
+ .setUtHits(true)
+ .setItHits(false)
+ .setUtCoveredConditions(1)
+ .setItCoveredConditions(1)
+ .setOverallCoveredConditions(1)
+ .build(),
+ ScannerReport.Coverage.newBuilder()
+ .setLine(2)
+ .setConditions(5)
+ .setUtHits(false)
+ .setItHits(false)
+ .setUtCoveredConditions(4)
+ .setItCoveredConditions(5)
+ .setOverallCoveredConditions(5)
+ .build()));
+
+ underTest = new ScannerReportReader(dir);
+ try (CloseableIterator<ScannerReport.Coverage> it = new ScannerReportReader(dir).readComponentCoverage(1)) {
+ ScannerReport.Coverage coverage = it.next();
+ 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);
+ }
+ }
+
+ @Test
+ public void return_empty_iterator_if_no_coverage_found() {
+ assertThat(underTest.readComponentCoverage(UNKNOWN_COMPONENT_REF)).isEmpty();
+ }
+
+ @Test
+ public void read_source_lines() throws Exception {
+ ScannerReportWriter writer = new ScannerReportWriter(dir);
+ File file = writer.getFileStructure().fileFor(FileStructure.Domain.SOURCE, 1);
+ FileUtils.writeLines(file, Lists.newArrayList("line1", "line2"));
+
+ File sourceFile = new ScannerReportReader(dir).readFileSource(1);
+ assertThat(sourceFile).isEqualTo(file);
+ }
+
+ @Test
+ public void read_tests() throws Exception {
+ ScannerReportWriter writer = new ScannerReportWriter(dir);
+ writer.writeTests(1, asList(
+ ScannerReport.Test.newBuilder()
+ .setDurationInMs(60_000)
+ .setStacktrace("stacktrace")
+ .setMsg("message")
+ .setStatus(Constants.TestStatus.OK)
+ .build()));
+
+ try (InputStream inputStream = FileUtils.openInputStream(underTest.readTests(1))) {
+ ScannerReport.Test testResult = ScannerReport.Test.PARSER.parseDelimitedFrom(inputStream);
+ assertThat(testResult.getDurationInMs()).isEqualTo(60_000);
+ assertThat(testResult.getStacktrace()).isEqualTo("stacktrace");
+ assertThat(testResult.getMsg()).isEqualTo("message");
+ assertThat(testResult.getStatus()).isEqualTo(Constants.TestStatus.OK);
+ }
+ }
+
+ @Test
+ public void null_if_no_test_found() {
+ assertThat(underTest.readTests(UNKNOWN_COMPONENT_REF)).isNull();
+ }
+
+ @Test
+ public void read_coverage_details() throws Exception {
+ ScannerReportWriter writer = new ScannerReportWriter(dir);
+ writer.writeCoverageDetails(1, asList(
+ ScannerReport.CoverageDetail.newBuilder()
+ .setTestName("test-name")
+ .addCoveredFile(ScannerReport.CoverageDetail.CoveredFile.newBuilder()
+ .addAllCoveredLine(asList(1, 2, 3, 5, 7))
+ .setFileRef(2))
+ .build()));
+
+ try (InputStream inputStream = FileUtils.openInputStream(underTest.readCoverageDetails(1))) {
+ ScannerReport.CoverageDetail coverageDetail = ScannerReport.CoverageDetail.PARSER.parseDelimitedFrom(inputStream);
+ assertThat(coverageDetail.getTestName()).isEqualTo("test-name");
+ assertThat(coverageDetail.getCoveredFile(0).getFileRef()).isEqualTo(2);
+ assertThat(coverageDetail.getCoveredFile(0).getCoveredLineList()).containsExactly(1, 2, 3, 5, 7);
+ }
+ }
+
+ @Test
+ public void null_if_no_coverage_detail_found() {
+ assertThat(underTest.readCoverageDetails(UNKNOWN_COMPONENT_REF)).isNull();
+ }
+
+ @Test
+ public void read_file_source() throws Exception {
+ ScannerReportWriter writer = new ScannerReportWriter(dir);
+ try (FileOutputStream outputStream = new FileOutputStream(writer.getSourceFile(1))) {
+ IOUtils.write("line1\nline2", outputStream);
+ }
+
+ try (InputStream inputStream = FileUtils.openInputStream(underTest.readFileSource(1))) {
+ assertThat(IOUtils.readLines(inputStream)).containsOnly("line1", "line2");
+ }
+ }
+
+ @Test
+ public void return_null_when_no_file_source() throws Exception {
+ assertThat(underTest.readFileSource(UNKNOWN_COMPONENT_REF)).isNull();
+ }
+}
diff --git a/sonar-scanner-protocol/src/test/java/org/sonar/scanner/protocol/output/ScannerReportWriterTest.java b/sonar-scanner-protocol/src/test/java/org/sonar/scanner/protocol/output/ScannerReportWriterTest.java
new file mode 100644
index 00000000000..9a08cd726ac
--- /dev/null
+++ b/sonar-scanner-protocol/src/test/java/org/sonar/scanner/protocol/output/ScannerReportWriterTest.java
@@ -0,0 +1,308 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact AT sonarsource DOT com
+ *
+ * This program 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.
+ *
+ * This program 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.scanner.protocol.output;
+
+import com.google.common.collect.Iterators;
+import java.io.File;
+import org.apache.commons.io.FileUtils;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+import org.sonar.core.util.CloseableIterator;
+import org.sonar.core.util.Protobuf;
+import org.sonar.scanner.protocol.Constants;
+import org.sonar.scanner.protocol.output.ScannerReportWriter;
+import org.sonar.scanner.protocol.output.FileStructure;
+
+import static java.util.Arrays.asList;
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class ScannerReportWriterTest {
+
+ @Rule
+ public TemporaryFolder temp = new TemporaryFolder();
+ File dir;
+ ScannerReportWriter underTest;
+
+ @Before
+ public void setUp() throws Exception {
+ dir = temp.newFolder();
+ underTest = new ScannerReportWriter(dir);
+ }
+
+ @Test
+ public void create_dir_if_does_not_exist() {
+ FileUtils.deleteQuietly(dir);
+ underTest = new ScannerReportWriter(dir);
+
+ assertThat(dir).isDirectory().exists();
+ }
+
+ @Test
+ public void write_metadata() {
+ ScannerReport.Metadata.Builder metadata = ScannerReport.Metadata.newBuilder()
+ .setAnalysisDate(15000000L)
+ .setProjectKey("PROJECT_A")
+ .setRootComponentRef(1);
+ underTest.writeMetadata(metadata.build());
+
+ ScannerReport.Metadata read = Protobuf.read(underTest.getFileStructure().metadataFile(), ScannerReport.Metadata.PARSER);
+ assertThat(read.getAnalysisDate()).isEqualTo(15000000L);
+ assertThat(read.getProjectKey()).isEqualTo("PROJECT_A");
+ assertThat(read.getRootComponentRef()).isEqualTo(1);
+ }
+
+ @Test
+ public void write_component() {
+ // no data yet
+ assertThat(underTest.hasComponentData(FileStructure.Domain.COMPONENT, 1)).isFalse();
+
+ // write data
+ ScannerReport.Component.Builder component = ScannerReport.Component.newBuilder()
+ .setRef(1)
+ .setLanguage("java")
+ .setPath("src/Foo.java")
+ .setType(Constants.ComponentType.FILE)
+ .setIsTest(false)
+ .addChildRef(5)
+ .addChildRef(42);
+ underTest.writeComponent(component.build());
+
+ assertThat(underTest.hasComponentData(FileStructure.Domain.COMPONENT, 1)).isTrue();
+ File file = underTest.getFileStructure().fileFor(FileStructure.Domain.COMPONENT, 1);
+ assertThat(file).exists().isFile();
+ ScannerReport.Component read = Protobuf.read(file, ScannerReport.Component.PARSER);
+ assertThat(read.getRef()).isEqualTo(1);
+ assertThat(read.getChildRefList()).containsOnly(5, 42);
+ assertThat(read.hasName()).isFalse();
+ assertThat(read.getIsTest()).isFalse();
+ }
+
+ @Test
+ public void write_issues() {
+ // no data yet
+ assertThat(underTest.hasComponentData(FileStructure.Domain.ISSUES, 1)).isFalse();
+
+ // write data
+ ScannerReport.Issue issue = ScannerReport.Issue.newBuilder()
+ .setLine(50)
+ .setMsg("the message")
+ .build();
+
+ underTest.writeComponentIssues(1, asList(issue));
+
+ assertThat(underTest.hasComponentData(FileStructure.Domain.ISSUES, 1)).isTrue();
+ File file = underTest.getFileStructure().fileFor(FileStructure.Domain.ISSUES, 1);
+ assertThat(file).exists().isFile();
+ try (CloseableIterator<ScannerReport.Issue> read = Protobuf.readStream(file, ScannerReport.Issue.PARSER)) {
+ assertThat(Iterators.size(read)).isEqualTo(1);
+ }
+ }
+
+ @Test
+ public void write_measures() {
+ assertThat(underTest.hasComponentData(FileStructure.Domain.MEASURES, 1)).isFalse();
+
+ ScannerReport.Measure measure = ScannerReport.Measure.newBuilder()
+ .setStringValue("text-value")
+ .setDoubleValue(2.5d)
+ .setValueType(Constants.MeasureValueType.DOUBLE)
+ .build();
+
+ underTest.writeComponentMeasures(1, asList(measure));
+
+ assertThat(underTest.hasComponentData(FileStructure.Domain.MEASURES, 1)).isTrue();
+ File file = underTest.getFileStructure().fileFor(FileStructure.Domain.MEASURES, 1);
+ assertThat(file).exists().isFile();
+ try (CloseableIterator<ScannerReport.Measure> read = Protobuf.readStream(file, ScannerReport.Measure.PARSER)) {
+ assertThat(Iterators.size(read)).isEqualTo(1);
+ }
+ }
+
+ @Test
+ public void write_scm() {
+ assertThat(underTest.hasComponentData(FileStructure.Domain.CHANGESETS, 1)).isFalse();
+
+ ScannerReport.Changesets scm = ScannerReport.Changesets.newBuilder()
+ .setComponentRef(1)
+ .addChangesetIndexByLine(0)
+ .addChangeset(ScannerReport.Changesets.Changeset.newBuilder()
+ .setRevision("123-456-789")
+ .setAuthor("author")
+ .setDate(123_456_789L))
+ .build();
+
+ underTest.writeComponentChangesets(scm);
+
+ assertThat(underTest.hasComponentData(FileStructure.Domain.CHANGESETS, 1)).isTrue();
+ File file = underTest.getFileStructure().fileFor(FileStructure.Domain.CHANGESETS, 1);
+ assertThat(file).exists().isFile();
+ ScannerReport.Changesets read = Protobuf.read(file, ScannerReport.Changesets.PARSER);
+ assertThat(read.getComponentRef()).isEqualTo(1);
+ assertThat(read.getChangesetCount()).isEqualTo(1);
+ assertThat(read.getChangesetList()).hasSize(1);
+ assertThat(read.getChangeset(0).getDate()).isEqualTo(123_456_789L);
+ }
+
+ @Test
+ public void write_duplications() {
+ assertThat(underTest.hasComponentData(FileStructure.Domain.DUPLICATIONS, 1)).isFalse();
+
+ ScannerReport.Duplication duplication = ScannerReport.Duplication.newBuilder()
+ .setOriginPosition(ScannerReport.TextRange.newBuilder()
+ .setStartLine(1)
+ .setEndLine(5)
+ .build())
+ .addDuplicate(ScannerReport.Duplicate.newBuilder()
+ .setOtherFileRef(2)
+ .setRange(ScannerReport.TextRange.newBuilder()
+ .setStartLine(6)
+ .setEndLine(10)
+ .build())
+ .build())
+ .build();
+ underTest.writeComponentDuplications(1, asList(duplication));
+
+ assertThat(underTest.hasComponentData(FileStructure.Domain.DUPLICATIONS, 1)).isTrue();
+ File file = underTest.getFileStructure().fileFor(FileStructure.Domain.DUPLICATIONS, 1);
+ assertThat(file).exists().isFile();
+ try (CloseableIterator<ScannerReport.Duplication> duplications = Protobuf.readStream(file, ScannerReport.Duplication.PARSER)) {
+ ScannerReport.Duplication dup = duplications.next();
+ assertThat(dup.getOriginPosition()).isNotNull();
+ assertThat(dup.getDuplicateList()).hasSize(1);
+ }
+ }
+
+ @Test
+ public void write_duplication_blocks() {
+ assertThat(underTest.hasComponentData(FileStructure.Domain.CPD_TEXT_BLOCKS, 1)).isFalse();
+
+ ScannerReport.CpdTextBlock duplicationBlock = ScannerReport.CpdTextBlock.newBuilder()
+ .setHash("abcdefghijklmnop")
+ .setStartLine(1)
+ .setEndLine(2)
+ .setStartTokenIndex(10)
+ .setEndTokenIndex(15)
+ .build();
+ underTest.writeCpdTextBlocks(1, asList(duplicationBlock));
+
+ assertThat(underTest.hasComponentData(FileStructure.Domain.CPD_TEXT_BLOCKS, 1)).isTrue();
+ File file = underTest.getFileStructure().fileFor(FileStructure.Domain.CPD_TEXT_BLOCKS, 1);
+ assertThat(file).exists().isFile();
+ try (CloseableIterator<ScannerReport.CpdTextBlock> duplicationBlocks = Protobuf.readStream(file, ScannerReport.CpdTextBlock.parser())) {
+ ScannerReport.CpdTextBlock duplicationBlockResult = duplicationBlocks.next();
+ assertThat(duplicationBlockResult.getHash()).isEqualTo("abcdefghijklmnop");
+ assertThat(duplicationBlockResult.getStartLine()).isEqualTo(1);
+ assertThat(duplicationBlockResult.getEndLine()).isEqualTo(2);
+ assertThat(duplicationBlockResult.getStartTokenIndex()).isEqualTo(10);
+ assertThat(duplicationBlockResult.getEndTokenIndex()).isEqualTo(15);
+ }
+ }
+
+ @Test
+ public void write_symbols() {
+ // no data yet
+ assertThat(underTest.hasComponentData(FileStructure.Domain.SYMBOLS, 1)).isFalse();
+
+ // write data
+ ScannerReport.Symbol symbol = ScannerReport.Symbol.newBuilder()
+ .setDeclaration(ScannerReport.TextRange.newBuilder()
+ .setStartLine(1)
+ .setStartOffset(3)
+ .setEndLine(1)
+ .setEndOffset(5)
+ .build())
+ .addReference(ScannerReport.TextRange.newBuilder()
+ .setStartLine(10)
+ .setStartOffset(15)
+ .setEndLine(11)
+ .setEndOffset(2)
+ .build())
+ .build();
+
+ underTest.writeComponentSymbols(1, asList(symbol));
+
+ assertThat(underTest.hasComponentData(FileStructure.Domain.SYMBOLS, 1)).isTrue();
+
+ File file = underTest.getFileStructure().fileFor(FileStructure.Domain.SYMBOLS, 1);
+ assertThat(file).exists().isFile();
+ try (CloseableIterator<ScannerReport.Symbol> read = Protobuf.readStream(file, ScannerReport.Symbol.PARSER)) {
+ assertThat(read).hasSize(1);
+ }
+ }
+
+ @Test
+ public void write_syntax_highlighting() {
+ // no data yet
+ assertThat(underTest.hasComponentData(FileStructure.Domain.SYNTAX_HIGHLIGHTINGS, 1)).isFalse();
+
+ underTest.writeComponentSyntaxHighlighting(1, asList(
+ ScannerReport.SyntaxHighlighting.newBuilder()
+ .setRange(ScannerReport.TextRange.newBuilder()
+ .setStartLine(1)
+ .setEndLine(1)
+ .build())
+ .setType(Constants.HighlightingType.ANNOTATION)
+ .build()));
+
+ assertThat(underTest.hasComponentData(FileStructure.Domain.SYNTAX_HIGHLIGHTINGS, 1)).isTrue();
+ }
+
+ @Test
+ public void write_coverage() {
+ // no data yet
+ assertThat(underTest.hasComponentData(FileStructure.Domain.COVERAGES, 1)).isFalse();
+
+ underTest.writeComponentCoverage(1, asList(
+ ScannerReport.Coverage.newBuilder()
+ .setLine(1)
+ .setConditions(1)
+ .setUtHits(true)
+ .setItHits(false)
+ .setUtCoveredConditions(1)
+ .setItCoveredConditions(1)
+ .setOverallCoveredConditions(1)
+ .build()));
+
+ assertThat(underTest.hasComponentData(FileStructure.Domain.COVERAGES, 1)).isTrue();
+ }
+
+ @Test
+ public void write_tests() {
+ assertThat(underTest.hasComponentData(FileStructure.Domain.TESTS, 1)).isFalse();
+
+ underTest.writeTests(1, asList(
+ ScannerReport.Test.getDefaultInstance()));
+
+ assertThat(underTest.hasComponentData(FileStructure.Domain.TESTS, 1)).isTrue();
+
+ }
+
+ @Test
+ public void write_coverage_details() {
+ assertThat(underTest.hasComponentData(FileStructure.Domain.COVERAGE_DETAILS, 1)).isFalse();
+
+ underTest.writeCoverageDetails(1, asList(
+ ScannerReport.CoverageDetail.getDefaultInstance()));
+
+ assertThat(underTest.hasComponentData(FileStructure.Domain.COVERAGE_DETAILS, 1)).isTrue();
+ }
+}
diff --git a/sonar-scanner-protocol/src/test/resources/org/sonar/scanner/protocol/input/GlobalRepositoriesTest/expected.json b/sonar-scanner-protocol/src/test/resources/org/sonar/scanner/protocol/input/GlobalRepositoriesTest/expected.json
new file mode 100644
index 00000000000..de38ae0cb18
--- /dev/null
+++ b/sonar-scanner-protocol/src/test/resources/org/sonar/scanner/protocol/input/GlobalRepositoriesTest/expected.json
@@ -0,0 +1,21 @@
+{
+ "timestamp": 10,
+ "metrics": [
+ {
+ "id": 1,
+ "key": "ncloc",
+ "valueType": "INT",
+ "description": "Description",
+ "direction": -1,
+ "name": "NCLOC",
+ "qualitative": true,
+ "userManaged": false,
+ "worstValue": 2.0,
+ "bestValue": 1.0,
+ "optimizedBestValue": true
+ }
+ ],
+ "globalSettings": {
+ "prop": "value"
+ }
+}
diff --git a/sonar-scanner-protocol/src/test/resources/org/sonar/scanner/protocol/input/ProjectRepositoriesTest/testToJson.json b/sonar-scanner-protocol/src/test/resources/org/sonar/scanner/protocol/input/ProjectRepositoriesTest/testToJson.json
new file mode 100644
index 00000000000..ba7489143a6
--- /dev/null
+++ b/sonar-scanner-protocol/src/test/resources/org/sonar/scanner/protocol/input/ProjectRepositoriesTest/testToJson.json
@@ -0,0 +1,23 @@
+{
+ "timestamp": 10,
+ "settingsByModule": {
+ "foo": {
+ "prop1": "value1",
+ "prop2": "value2",
+ "prop": "value"
+ }
+ },
+ "fileDataByModuleAndPath": {
+ "foo": {
+ "src/main/java/Foo.java": {
+ "hash": "xyz",
+ "needBlame": true
+ },
+ "src/main/java/Foo2.java": {
+ "hash": "xyz",
+ "needBlame": false
+ }
+ }
+ },
+ "lastAnalysisDate": "2014-05-18T15:50:45+0100"
+}
diff --git a/sonar-scanner-protocol/src/test/resources/org/sonar/scanner/protocol/input/RulesSearchTest/empty.json b/sonar-scanner-protocol/src/test/resources/org/sonar/scanner/protocol/input/RulesSearchTest/empty.json
new file mode 100644
index 00000000000..055fe8b8d63
--- /dev/null
+++ b/sonar-scanner-protocol/src/test/resources/org/sonar/scanner/protocol/input/RulesSearchTest/empty.json
@@ -0,0 +1 @@
+{"total":3225,"p":30,"ps":500,"rules":[]} \ No newline at end of file
diff --git a/sonar-scanner-protocol/src/test/resources/org/sonar/scanner/protocol/input/RulesSearchTest/expected.json b/sonar-scanner-protocol/src/test/resources/org/sonar/scanner/protocol/input/RulesSearchTest/expected.json
new file mode 100644
index 00000000000..89350a7e331
--- /dev/null
+++ b/sonar-scanner-protocol/src/test/resources/org/sonar/scanner/protocol/input/RulesSearchTest/expected.json
@@ -0,0 +1 @@
+{"total":290,"p":1,"ps":2,"rules":[{"key":"squid:S1194","internalKey":"S1194","repo":"squid","name":"\"java.lang.Error\" should not be extended","severity":"MAJOR","lang":"java"},{"key":"squid:ObjectFinalizeOverridenCallsSuperFinalizeCheck","internalKey":"ObjectFinalizeOverridenCallsSuperFinalizeCheck","repo":"squid","name":"super.finalize() should be called at the end of Object.finalize() implementations","severity":"BLOCKER","lang":"java"}]} \ No newline at end of file
diff --git a/sonar-scanner-protocol/src/test/resources/org/sonar/scanner/protocol/output/component/ReportComponentsTest/expected.json b/sonar-scanner-protocol/src/test/resources/org/sonar/scanner/protocol/output/component/ReportComponentsTest/expected.json
new file mode 100644
index 00000000000..581bbc5ea23
--- /dev/null
+++ b/sonar-scanner-protocol/src/test/resources/org/sonar/scanner/protocol/output/component/ReportComponentsTest/expected.json
@@ -0,0 +1,43 @@
+{
+ "analysisDate": "2012-12-12T00:00:00+0100",
+ "root": {
+ "batchId": 1,
+ "id": 11,
+ "snapshotId": 111,
+ "name": "Root project",
+ "type": "PRJ",
+ "children": [
+ {
+ "batchId": 2,
+ "id": 22,
+ "snapshotId": 222,
+ "path": "module1",
+ "name": "Module",
+ "type": "MOD",
+ "children": [
+ {
+ "batchId": 3,
+ "id": 33,
+ "snapshotId": 333,
+ "path": "src",
+ "name": "src",
+ "type": "DIR",
+ "children": [
+ {
+ "batchId": 4,
+ "id": 44,
+ "snapshotId": 444,
+ "path": "Foo.java",
+ "name": "Foo.java",
+ "type": "FIL",
+ "languageKey": "java",
+ "isTest": true,
+ "children": []
+ }
+ ]
+ }
+ ]
+ }
+ ]
+ }
+} \ No newline at end of file