]> source.dussan.org Git - sonarqube.git/blob
5366c016a36af5f0584e5c9f15ad14e9e6789530
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2017 SonarSource SA
4  * mailto:info 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 java.io.File;
23 import java.io.IOException;
24 import org.apache.commons.io.FileUtils;
25 import org.junit.Before;
26 import org.junit.Rule;
27 import org.junit.Test;
28 import org.sonar.api.utils.internal.JUnitTempFolder;
29 import org.sonar.core.util.CloseableIterator;
30 import org.sonar.scanner.protocol.output.ScannerReport;
31 import org.sonar.scanner.protocol.output.ScannerReportWriter;
32
33 import static com.google.common.collect.ImmutableList.of;
34 import static org.assertj.core.api.Assertions.assertThat;
35 import static org.assertj.guava.api.Assertions.assertThat;
36
37 public class BatchReportReaderImplTest {
38   private static final int COMPONENT_REF = 1;
39   private static final ScannerReport.Changesets CHANGESETS = ScannerReport.Changesets.newBuilder().setComponentRef(COMPONENT_REF).build();
40   private static final ScannerReport.Measure MEASURE = ScannerReport.Measure.newBuilder().build();
41   private static final ScannerReport.Component COMPONENT = ScannerReport.Component.newBuilder().setRef(COMPONENT_REF).build();
42   private static final ScannerReport.Issue ISSUE = ScannerReport.Issue.newBuilder().build();
43   private static final ScannerReport.Duplication DUPLICATION = ScannerReport.Duplication.newBuilder().build();
44   private static final ScannerReport.CpdTextBlock DUPLICATION_BLOCK = ScannerReport.CpdTextBlock.newBuilder().build();
45   private static final ScannerReport.Symbol SYMBOL = ScannerReport.Symbol.newBuilder().build();
46   private static final ScannerReport.SyntaxHighlightingRule SYNTAX_HIGHLIGHTING_1 = ScannerReport.SyntaxHighlightingRule.newBuilder().build();
47   private static final ScannerReport.SyntaxHighlightingRule SYNTAX_HIGHLIGHTING_2 = ScannerReport.SyntaxHighlightingRule.newBuilder().build();
48   private static final ScannerReport.LineCoverage COVERAGE_1 = ScannerReport.LineCoverage.newBuilder().build();
49   private static final ScannerReport.LineCoverage COVERAGE_2 = ScannerReport.LineCoverage.newBuilder().build();
50   private static final ScannerReport.Test TEST_1 = ScannerReport.Test.newBuilder().setName("1").build();
51   private static final ScannerReport.Test TEST_2 = ScannerReport.Test.newBuilder().setName("2").build();
52   private static final ScannerReport.CoverageDetail COVERAGE_DETAIL_1 = ScannerReport.CoverageDetail.newBuilder().setTestName("1").build();
53   private static final ScannerReport.CoverageDetail COVERAGE_DETAIL_2 = ScannerReport.CoverageDetail.newBuilder().setTestName("2").build();
54
55   @Rule
56   public JUnitTempFolder tempFolder = new JUnitTempFolder();
57
58   private ScannerReportWriter writer;
59   private BatchReportReaderImpl underTest;
60
61   @Before
62   public void setUp() {
63     BatchReportDirectoryHolder holder = new ImmutableBatchReportDirectoryHolder(tempFolder.newDir());
64     underTest = new BatchReportReaderImpl(holder);
65     writer = new ScannerReportWriter(holder.getDirectory());
66   }
67
68   @Test(expected = IllegalStateException.class)
69   public void readMetadata_throws_ISE_if_no_metadata() {
70     underTest.readMetadata();
71   }
72
73   @Test
74   public void readMetadata_result_is_cached() {
75     ScannerReport.Metadata metadata = ScannerReport.Metadata.newBuilder().build();
76
77     writer.writeMetadata(metadata);
78
79     ScannerReport.Metadata res = underTest.readMetadata();
80     assertThat(res).isEqualTo(metadata);
81     assertThat(underTest.readMetadata()).isSameAs(res);
82   }
83
84   @Test
85   public void readScannerLogs() throws IOException {
86     File scannerLogFile = writer.getFileStructure().analysisLog();
87     FileUtils.write(scannerLogFile, "log1\nlog2");
88
89     CloseableIterator<String> logs = underTest.readScannerLogs();
90     assertThat(logs).containsExactly("log1", "log2");
91   }
92
93   @Test
94   public void readScannerLogs_no_logs() {
95     CloseableIterator<String> logs = underTest.readScannerLogs();
96     assertThat(logs.hasNext()).isFalse();
97   }
98
99   @Test
100   public void readComponentMeasures_returns_empty_list_if_there_is_no_measure() {
101     assertThat(underTest.readComponentMeasures(COMPONENT_REF)).isEmpty();
102   }
103
104   @Test
105   public void verify_readComponentMeasures_returns_measures() {
106     writer.writeComponentMeasures(COMPONENT_REF, of(MEASURE));
107
108     try (CloseableIterator<ScannerReport.Measure> measures = underTest.readComponentMeasures(COMPONENT_REF)) {
109       assertThat(measures.next()).isEqualTo(MEASURE);
110       assertThat(measures.hasNext()).isFalse();
111     }
112   }
113
114   @Test
115   public void readComponentMeasures_is_not_cached() {
116     writer.writeComponentMeasures(COMPONENT_REF, of(MEASURE));
117
118     assertThat(underTest.readComponentMeasures(COMPONENT_REF)).isNotSameAs(underTest.readComponentMeasures(COMPONENT_REF));
119   }
120
121   @Test
122   public void readChangesets_returns_null_if_no_changeset() {
123     assertThat(underTest.readChangesets(COMPONENT_REF)).isNull();
124   }
125
126   @Test
127   public void verify_readChangesets_returns_changesets() {
128     writer.writeComponentChangesets(CHANGESETS);
129
130     ScannerReport.Changesets res = underTest.readChangesets(COMPONENT_REF);
131     assertThat(res).isEqualTo(CHANGESETS);
132   }
133
134   @Test
135   public void readChangesets_is_not_cached() {
136     writer.writeComponentChangesets(CHANGESETS);
137
138     assertThat(underTest.readChangesets(COMPONENT_REF)).isNotSameAs(underTest.readChangesets(COMPONENT_REF));
139   }
140
141   @Test(expected = IllegalStateException.class)
142   public void readComponent_throws_ISE_if_file_does_not_exist() {
143     underTest.readComponent(COMPONENT_REF);
144   }
145
146   @Test
147   public void verify_readComponent_returns_Component() {
148     writer.writeComponent(COMPONENT);
149
150     assertThat(underTest.readComponent(COMPONENT_REF)).isEqualTo(COMPONENT);
151   }
152
153   @Test
154   public void readComponent_is_not_cached() {
155     writer.writeComponent(COMPONENT);
156
157     assertThat(underTest.readComponent(COMPONENT_REF)).isNotSameAs(underTest.readComponent(COMPONENT_REF));
158   }
159
160   @Test
161   public void readComponentIssues_returns_empty_list_if_file_does_not_exist() {
162     assertThat(underTest.readComponentIssues(COMPONENT_REF)).isEmpty();
163   }
164
165   @Test
166   public void verify_readComponentIssues_returns_Issues() {
167     writer.writeComponentIssues(COMPONENT_REF, of(ISSUE));
168
169     try (CloseableIterator<ScannerReport.Issue> res = underTest.readComponentIssues(COMPONENT_REF)) {
170       assertThat(res.next()).isEqualTo(ISSUE);
171       assertThat(res.hasNext()).isFalse();
172     }
173   }
174
175   @Test
176   public void readComponentIssues_it_not_cached() {
177     writer.writeComponentIssues(COMPONENT_REF, of(ISSUE));
178
179     assertThat(underTest.readComponentIssues(COMPONENT_REF)).isNotSameAs(underTest.readComponentIssues(COMPONENT_REF));
180   }
181
182   @Test
183   public void readComponentDuplications_returns_empty_list_if_file_does_not_exist() {
184     assertThat(underTest.readComponentDuplications(COMPONENT_REF)).isEmpty();
185   }
186
187   @Test
188   public void verify_readComponentDuplications_returns_Issues() {
189     writer.writeComponentDuplications(COMPONENT_REF, of(DUPLICATION));
190
191     try (CloseableIterator<ScannerReport.Duplication> res = underTest.readComponentDuplications(COMPONENT_REF)) {
192       assertThat(res.next()).isEqualTo(DUPLICATION);
193       assertThat(res.hasNext()).isFalse();
194     }
195   }
196
197   @Test
198   public void readComponentDuplications_it_not_cached() {
199     writer.writeComponentDuplications(COMPONENT_REF, of(DUPLICATION));
200
201     assertThat(underTest.readComponentDuplications(COMPONENT_REF)).isNotSameAs(underTest.readComponentDuplications(COMPONENT_REF));
202   }
203
204   @Test
205   public void readComponentDuplicationBlocks_returns_empty_list_if_file_does_not_exist() {
206     assertThat(underTest.readCpdTextBlocks(COMPONENT_REF)).isEmpty();
207   }
208
209   @Test
210   public void verify_readComponentDuplicationBlocks_returns_Issues() {
211     writer.writeCpdTextBlocks(COMPONENT_REF, of(DUPLICATION_BLOCK));
212
213     try (CloseableIterator<ScannerReport.CpdTextBlock> res = underTest.readCpdTextBlocks(COMPONENT_REF)) {
214       assertThat(res.next()).isEqualTo(DUPLICATION_BLOCK);
215       assertThat(res.hasNext()).isFalse();
216     }
217   }
218
219   @Test
220   public void readComponentDuplicationBlocks_is_not_cached() {
221     writer.writeCpdTextBlocks(COMPONENT_REF, of(DUPLICATION_BLOCK));
222
223     assertThat(underTest.readCpdTextBlocks(COMPONENT_REF)).isNotSameAs(underTest.readCpdTextBlocks(COMPONENT_REF));
224   }
225
226   @Test
227   public void readComponentSymbols_returns_empty_list_if_file_does_not_exist() {
228     assertThat(underTest.readComponentSymbols(COMPONENT_REF)).isEmpty();
229   }
230
231   @Test
232   public void verify_readComponentSymbols_returns_Issues() {
233     writer.writeComponentSymbols(COMPONENT_REF, of(SYMBOL));
234
235     try (CloseableIterator<ScannerReport.Symbol> res = underTest.readComponentSymbols(COMPONENT_REF)) {
236       assertThat(res.next()).isEqualTo(SYMBOL);
237       assertThat(res.hasNext()).isFalse();
238     }
239   }
240
241   @Test
242   public void readComponentSymbols_it_not_cached() {
243     writer.writeComponentSymbols(COMPONENT_REF, of(SYMBOL));
244
245     assertThat(underTest.readComponentSymbols(COMPONENT_REF)).isNotSameAs(underTest.readComponentSymbols(COMPONENT_REF));
246   }
247
248   @Test
249   public void readComponentSyntaxHighlighting_returns_empty_CloseableIterator_when_file_does_not_exist() {
250     assertThat(underTest.readComponentSyntaxHighlighting(COMPONENT_REF)).isEmpty();
251   }
252
253   @Test
254   public void verify_readComponentSyntaxHighlighting() {
255     writer.writeComponentSyntaxHighlighting(COMPONENT_REF, of(SYNTAX_HIGHLIGHTING_1, SYNTAX_HIGHLIGHTING_2));
256
257     CloseableIterator<ScannerReport.SyntaxHighlightingRule> res = underTest.readComponentSyntaxHighlighting(COMPONENT_REF);
258     assertThat(res).containsExactly(SYNTAX_HIGHLIGHTING_1, SYNTAX_HIGHLIGHTING_2);
259     res.close();
260   }
261
262   @Test
263   public void readComponentCoverage_returns_empty_CloseableIterator_when_file_does_not_exist() {
264     assertThat(underTest.readComponentCoverage(COMPONENT_REF)).isEmpty();
265   }
266
267   @Test
268   public void verify_readComponentCoverage() {
269     writer.writeComponentCoverage(COMPONENT_REF, of(COVERAGE_1, COVERAGE_2));
270
271     CloseableIterator<ScannerReport.LineCoverage> res = underTest.readComponentCoverage(COMPONENT_REF);
272     assertThat(res).containsExactly(COVERAGE_1, COVERAGE_2);
273     res.close();
274   }
275
276   @Test
277   public void readFileSource_returns_absent_optional_when_file_does_not_exist() {
278     assertThat(underTest.readFileSource(COMPONENT_REF)).isAbsent();
279   }
280
281   @Test
282   public void verify_readFileSource() throws IOException {
283     File file = writer.getSourceFile(COMPONENT_REF);
284     FileUtils.writeLines(file, of("1", "2", "3"));
285
286     CloseableIterator<String> res = underTest.readFileSource(COMPONENT_REF).get();
287     assertThat(res).containsExactly("1", "2", "3");
288     res.close();
289   }
290
291   @Test
292   public void readTests_returns_empty_CloseableIterator_when_file_does_not_exist() {
293     assertThat(underTest.readTests(COMPONENT_REF)).isEmpty();
294   }
295
296   @Test
297   public void verify_readTests() {
298     writer.writeTests(COMPONENT_REF, of(TEST_1, TEST_2));
299
300     CloseableIterator<ScannerReport.Test> res = underTest.readTests(COMPONENT_REF);
301     assertThat(res).containsExactly(TEST_1, TEST_2);
302     res.close();
303   }
304
305   @Test
306   public void readCoverageDetails_returns_empty_CloseableIterator_when_file_does_not_exist() {
307     assertThat(underTest.readCoverageDetails(COMPONENT_REF)).isEmpty();
308   }
309
310   @Test
311   public void verify_readCoverageDetails() {
312     writer.writeCoverageDetails(COMPONENT_REF, of(COVERAGE_DETAIL_1, COVERAGE_DETAIL_2));
313
314     CloseableIterator<ScannerReport.CoverageDetail> res = underTest.readCoverageDetails(COMPONENT_REF);
315     assertThat(res).containsExactly(COVERAGE_DETAIL_1, COVERAGE_DETAIL_2);
316     res.close();
317   }
318 }