3 * Copyright (C) 2009-2016 SonarSource SA
4 * mailto:contact AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.server.computation.task.projectanalysis.batch;
22 import com.google.common.base.Optional;
23 import com.google.common.base.Preconditions;
24 import java.util.ArrayList;
25 import java.util.Arrays;
26 import java.util.HashMap;
27 import java.util.List;
29 import javax.annotation.CheckForNull;
30 import javax.annotation.Nullable;
31 import org.junit.rules.TestRule;
32 import org.junit.runner.Description;
33 import org.junit.runners.model.Statement;
34 import org.sonar.core.util.CloseableIterator;
35 import org.sonar.scanner.protocol.output.ScannerReport;
37 public class BatchReportReaderRule implements TestRule, BatchReportReader {
38 private ScannerReport.Metadata metadata;
39 private List<String> scannerLogs;
40 private List<ScannerReport.ActiveRule> activeRules = new ArrayList<>();
41 private Map<Integer, List<ScannerReport.Measure>> measures = new HashMap<>();
42 private Map<Integer, ScannerReport.Changesets> changesets = new HashMap<>();
43 private Map<Integer, ScannerReport.Component> components = new HashMap<>();
44 private Map<Integer, List<ScannerReport.Issue>> issues = new HashMap<>();
45 private Map<Integer, List<ScannerReport.Duplication>> duplications = new HashMap<>();
46 private Map<Integer, List<ScannerReport.CpdTextBlock>> duplicationBlocks = new HashMap<>();
47 private Map<Integer, List<ScannerReport.Symbol>> symbols = new HashMap<>();
48 private Map<Integer, List<ScannerReport.SyntaxHighlightingRule>> syntaxHighlightings = new HashMap<>();
49 private Map<Integer, List<ScannerReport.LineCoverage>> coverages = new HashMap<>();
50 private Map<Integer, List<String>> fileSources = new HashMap<>();
51 private Map<Integer, List<ScannerReport.Test>> tests = new HashMap<>();
52 private Map<Integer, List<ScannerReport.CoverageDetail>> coverageDetails = new HashMap<>();
55 public Statement apply(final Statement statement, Description description) {
56 return new Statement() {
58 public void evaluate() throws Throwable {
68 private void clear() {
70 this.scannerLogs = null;
71 this.measures.clear();
72 this.changesets.clear();
73 this.components.clear();
75 this.duplications.clear();
76 this.duplicationBlocks.clear();
78 this.syntaxHighlightings.clear();
79 this.coverages.clear();
80 this.fileSources.clear();
82 this.coverageDetails.clear();
86 public ScannerReport.Metadata readMetadata() {
87 if (metadata == null) {
88 throw new IllegalStateException("Metadata is missing");
93 public BatchReportReaderRule setMetadata(ScannerReport.Metadata metadata) {
94 this.metadata = metadata;
99 public CloseableIterator<String> readScannerLogs() {
100 if (scannerLogs == null) {
101 throw new IllegalStateException("Scanner logs are missing");
103 return CloseableIterator.from(scannerLogs.iterator());
106 public BatchReportReaderRule setScannerLogs(@Nullable List<String> logs) {
107 this.scannerLogs = logs;
112 public CloseableIterator<ScannerReport.ActiveRule> readActiveRules() {
113 if (activeRules == null) {
114 throw new IllegalStateException("Active rules are not set");
116 return CloseableIterator.from(activeRules.iterator());
119 public BatchReportReaderRule putActiveRules(List<ScannerReport.ActiveRule> activeRules) {
120 this.activeRules = activeRules;
125 public CloseableIterator<ScannerReport.Measure> readComponentMeasures(int componentRef) {
126 List<ScannerReport.Measure> res = this.measures.get(componentRef);
128 return CloseableIterator.emptyCloseableIterator();
130 return CloseableIterator.from(res.iterator());
133 public BatchReportReaderRule putMeasures(int componentRef, List<ScannerReport.Measure> measures) {
134 this.measures.put(componentRef, measures);
140 public ScannerReport.Changesets readChangesets(int componentRef) {
141 return changesets.get(componentRef);
144 public BatchReportReaderRule putChangesets(ScannerReport.Changesets changesets) {
145 this.changesets.put(changesets.getComponentRef(), changesets);
150 public ScannerReport.Component readComponent(int componentRef) {
151 return components.get(componentRef);
154 public BatchReportReaderRule putComponent(ScannerReport.Component component) {
155 this.components.put(component.getRef(), component);
160 public CloseableIterator<ScannerReport.Issue> readComponentIssues(int componentRef) {
161 return closeableIterator(issues.get(componentRef));
164 public BatchReportReaderRule putIssues(int componentRef, List<ScannerReport.Issue> issue) {
165 this.issues.put(componentRef, issue);
170 public CloseableIterator<ScannerReport.Duplication> readComponentDuplications(int componentRef) {
171 return closeableIterator(this.duplications.get(componentRef));
174 public BatchReportReaderRule putDuplications(int componentRef, ScannerReport.Duplication... duplications) {
175 this.duplications.put(componentRef, Arrays.asList(duplications));
180 public CloseableIterator<ScannerReport.CpdTextBlock> readCpdTextBlocks(int componentRef) {
181 return closeableIterator(this.duplicationBlocks.get(componentRef));
184 public BatchReportReaderRule putDuplicationBlocks(int componentRef, List<ScannerReport.CpdTextBlock> duplicationBlocks) {
185 this.duplicationBlocks.put(componentRef, duplicationBlocks);
190 public CloseableIterator<ScannerReport.Symbol> readComponentSymbols(int componentRef) {
191 return closeableIterator(this.symbols.get(componentRef));
194 private static <T> CloseableIterator<T> closeableIterator(@Nullable List<T> list) {
195 return list == null ? CloseableIterator.<T>emptyCloseableIterator() : CloseableIterator.from(list.iterator());
198 public BatchReportReaderRule putSymbols(int componentRef, List<ScannerReport.Symbol> symbols) {
199 this.symbols.put(componentRef, symbols);
204 public CloseableIterator<ScannerReport.SyntaxHighlightingRule> readComponentSyntaxHighlighting(int fileRef) {
205 List<ScannerReport.SyntaxHighlightingRule> res = this.syntaxHighlightings.get(fileRef);
207 return CloseableIterator.emptyCloseableIterator();
210 return CloseableIterator.from(res.iterator());
213 public BatchReportReaderRule putSyntaxHighlighting(int fileRef, List<ScannerReport.SyntaxHighlightingRule> syntaxHighlightings) {
214 this.syntaxHighlightings.put(fileRef, syntaxHighlightings);
219 public CloseableIterator<ScannerReport.LineCoverage> readComponentCoverage(int fileRef) {
220 List<ScannerReport.LineCoverage> res = this.coverages.get(fileRef);
222 return CloseableIterator.emptyCloseableIterator();
225 return CloseableIterator.from(res.iterator());
228 public BatchReportReaderRule putCoverage(int fileRef, List<ScannerReport.LineCoverage> coverages) {
229 this.coverages.put(fileRef, coverages);
234 public Optional<CloseableIterator<String>> readFileSource(int fileRef) {
235 List<String> lines = fileSources.get(fileRef);
237 return Optional.absent();
240 return Optional.of(CloseableIterator.from(lines.iterator()));
243 public BatchReportReaderRule putFileSourceLines(int fileRef, @Nullable String... lines) {
244 Preconditions.checkNotNull(lines);
245 this.fileSources.put(fileRef, Arrays.asList(lines));
249 public BatchReportReaderRule putFileSourceLines(int fileRef, List<String> lines) {
250 this.fileSources.put(fileRef, lines);
255 public CloseableIterator<ScannerReport.Test> readTests(int testFileRef) {
256 List<ScannerReport.Test> res = this.tests.get(testFileRef);
258 return CloseableIterator.emptyCloseableIterator();
261 return CloseableIterator.from(res.iterator());
264 public BatchReportReaderRule putTests(int testFileRed, List<ScannerReport.Test> tests) {
265 this.tests.put(testFileRed, tests);
270 public CloseableIterator<ScannerReport.CoverageDetail> readCoverageDetails(int testFileRef) {
271 List<ScannerReport.CoverageDetail> res = this.coverageDetails.get(testFileRef);
273 return CloseableIterator.emptyCloseableIterator();
276 return CloseableIterator.from(res.iterator());
279 public BatchReportReaderRule putCoverageDetails(int testFileRef, List<ScannerReport.CoverageDetail> coverageDetails) {
280 this.coverageDetails.put(testFileRef, coverageDetails);