]> source.dussan.org Git - sonarqube.git/blob
51781aaa676d3ade9a3867cd7824b9a122a83e14
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2016 SonarSource SA
4  * mailto:contact AT sonarsource DOT com
5  *
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.
10  *
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.
15  *
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.
19  */
20 package org.sonar.server.computation.task.projectanalysis.batch;
21
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;
28 import java.util.Map;
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;
36
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<>();
53
54   @Override
55   public Statement apply(final Statement statement, Description description) {
56     return new Statement() {
57       @Override
58       public void evaluate() throws Throwable {
59         try {
60           statement.evaluate();
61         } finally {
62           clear();
63         }
64       }
65     };
66   }
67
68   private void clear() {
69     this.metadata = null;
70     this.scannerLogs = null;
71     this.measures.clear();
72     this.changesets.clear();
73     this.components.clear();
74     this.issues.clear();
75     this.duplications.clear();
76     this.duplicationBlocks.clear();
77     this.symbols.clear();
78     this.syntaxHighlightings.clear();
79     this.coverages.clear();
80     this.fileSources.clear();
81     this.tests.clear();
82     this.coverageDetails.clear();
83   }
84
85   @Override
86   public ScannerReport.Metadata readMetadata() {
87     if (metadata == null) {
88       throw new IllegalStateException("Metadata is missing");
89     }
90     return metadata;
91   }
92
93   public BatchReportReaderRule setMetadata(ScannerReport.Metadata metadata) {
94     this.metadata = metadata;
95     return this;
96   }
97
98   @Override
99   public CloseableIterator<String> readScannerLogs() {
100     if (scannerLogs == null) {
101       throw new IllegalStateException("Scanner logs are missing");
102     }
103     return CloseableIterator.from(scannerLogs.iterator());
104   }
105
106   public BatchReportReaderRule setScannerLogs(@Nullable List<String> logs) {
107     this.scannerLogs = logs;
108     return this;
109   }
110
111   @Override
112   public CloseableIterator<ScannerReport.ActiveRule> readActiveRules() {
113     if (activeRules == null) {
114       throw new IllegalStateException("Active rules are not set");
115     }
116     return CloseableIterator.from(activeRules.iterator());
117   }
118
119   public BatchReportReaderRule putActiveRules(List<ScannerReport.ActiveRule> activeRules) {
120     this.activeRules = activeRules;
121     return this;
122   }
123
124   @Override
125   public CloseableIterator<ScannerReport.Measure> readComponentMeasures(int componentRef) {
126     List<ScannerReport.Measure> res = this.measures.get(componentRef);
127     if (res == null) {
128       return CloseableIterator.emptyCloseableIterator();
129     }
130     return CloseableIterator.from(res.iterator());
131   }
132
133   public BatchReportReaderRule putMeasures(int componentRef, List<ScannerReport.Measure> measures) {
134     this.measures.put(componentRef, measures);
135     return this;
136   }
137
138   @Override
139   @CheckForNull
140   public ScannerReport.Changesets readChangesets(int componentRef) {
141     return changesets.get(componentRef);
142   }
143
144   public BatchReportReaderRule putChangesets(ScannerReport.Changesets changesets) {
145     this.changesets.put(changesets.getComponentRef(), changesets);
146     return this;
147   }
148
149   @Override
150   public ScannerReport.Component readComponent(int componentRef) {
151     return components.get(componentRef);
152   }
153
154   public BatchReportReaderRule putComponent(ScannerReport.Component component) {
155     this.components.put(component.getRef(), component);
156     return this;
157   }
158
159   @Override
160   public CloseableIterator<ScannerReport.Issue> readComponentIssues(int componentRef) {
161     return closeableIterator(issues.get(componentRef));
162   }
163
164   public BatchReportReaderRule putIssues(int componentRef, List<ScannerReport.Issue> issue) {
165     this.issues.put(componentRef, issue);
166     return this;
167   }
168
169   @Override
170   public CloseableIterator<ScannerReport.Duplication> readComponentDuplications(int componentRef) {
171     return closeableIterator(this.duplications.get(componentRef));
172   }
173
174   public BatchReportReaderRule putDuplications(int componentRef, ScannerReport.Duplication... duplications) {
175     this.duplications.put(componentRef, Arrays.asList(duplications));
176     return this;
177   }
178
179   @Override
180   public CloseableIterator<ScannerReport.CpdTextBlock> readCpdTextBlocks(int componentRef) {
181     return closeableIterator(this.duplicationBlocks.get(componentRef));
182   }
183
184   public BatchReportReaderRule putDuplicationBlocks(int componentRef, List<ScannerReport.CpdTextBlock> duplicationBlocks) {
185     this.duplicationBlocks.put(componentRef, duplicationBlocks);
186     return this;
187   }
188
189   @Override
190   public CloseableIterator<ScannerReport.Symbol> readComponentSymbols(int componentRef) {
191     return closeableIterator(this.symbols.get(componentRef));
192   }
193
194   private static <T> CloseableIterator<T> closeableIterator(@Nullable List<T> list) {
195     return list == null ? CloseableIterator.<T>emptyCloseableIterator() : CloseableIterator.from(list.iterator());
196   }
197
198   public BatchReportReaderRule putSymbols(int componentRef, List<ScannerReport.Symbol> symbols) {
199     this.symbols.put(componentRef, symbols);
200     return this;
201   }
202
203   @Override
204   public CloseableIterator<ScannerReport.SyntaxHighlightingRule> readComponentSyntaxHighlighting(int fileRef) {
205     List<ScannerReport.SyntaxHighlightingRule> res = this.syntaxHighlightings.get(fileRef);
206     if (res == null) {
207       return CloseableIterator.emptyCloseableIterator();
208     }
209
210     return CloseableIterator.from(res.iterator());
211   }
212
213   public BatchReportReaderRule putSyntaxHighlighting(int fileRef, List<ScannerReport.SyntaxHighlightingRule> syntaxHighlightings) {
214     this.syntaxHighlightings.put(fileRef, syntaxHighlightings);
215     return this;
216   }
217
218   @Override
219   public CloseableIterator<ScannerReport.LineCoverage> readComponentCoverage(int fileRef) {
220     List<ScannerReport.LineCoverage> res = this.coverages.get(fileRef);
221     if (res == null) {
222       return CloseableIterator.emptyCloseableIterator();
223     }
224
225     return CloseableIterator.from(res.iterator());
226   }
227
228   public BatchReportReaderRule putCoverage(int fileRef, List<ScannerReport.LineCoverage> coverages) {
229     this.coverages.put(fileRef, coverages);
230     return this;
231   }
232
233   @Override
234   public Optional<CloseableIterator<String>> readFileSource(int fileRef) {
235     List<String> lines = fileSources.get(fileRef);
236     if (lines == null) {
237       return Optional.absent();
238     }
239
240     return Optional.of(CloseableIterator.from(lines.iterator()));
241   }
242
243   public BatchReportReaderRule putFileSourceLines(int fileRef, @Nullable String... lines) {
244     Preconditions.checkNotNull(lines);
245     this.fileSources.put(fileRef, Arrays.asList(lines));
246     return this;
247   }
248
249   public BatchReportReaderRule putFileSourceLines(int fileRef, List<String> lines) {
250     this.fileSources.put(fileRef, lines);
251     return this;
252   }
253
254   @Override
255   public CloseableIterator<ScannerReport.Test> readTests(int testFileRef) {
256     List<ScannerReport.Test> res = this.tests.get(testFileRef);
257     if (res == null) {
258       return CloseableIterator.emptyCloseableIterator();
259     }
260
261     return CloseableIterator.from(res.iterator());
262   }
263
264   public BatchReportReaderRule putTests(int testFileRed, List<ScannerReport.Test> tests) {
265     this.tests.put(testFileRed, tests);
266     return this;
267   }
268
269   @Override
270   public CloseableIterator<ScannerReport.CoverageDetail> readCoverageDetails(int testFileRef) {
271     List<ScannerReport.CoverageDetail> res = this.coverageDetails.get(testFileRef);
272     if (res == null) {
273       return CloseableIterator.emptyCloseableIterator();
274     }
275
276     return CloseableIterator.from(res.iterator());
277   }
278
279   public BatchReportReaderRule putCoverageDetails(int testFileRef, List<ScannerReport.CoverageDetail> coverageDetails) {
280     this.coverageDetails.put(testFileRef, coverageDetails);
281     return this;
282   }
283 }