]> source.dussan.org Git - sonarqube.git/blob
d181d85263f8a7943676142060162dccad795053
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2024 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.ce.task.projectanalysis.batch;
21
22 import com.google.common.collect.ImmutableList;
23 import java.io.File;
24 import java.io.IOException;
25 import org.apache.commons.io.FileUtils;
26 import org.junit.Before;
27 import org.junit.Rule;
28 import org.junit.Test;
29 import org.sonar.api.impl.utils.JUnitTempFolder;
30 import org.sonar.core.util.CloseableIterator;
31 import org.sonar.scanner.protocol.output.FileStructure;
32 import org.sonar.scanner.protocol.output.ScannerReport;
33 import org.sonar.scanner.protocol.output.ScannerReportWriter;
34
35 import static com.google.common.collect.ImmutableList.of;
36 import static org.assertj.core.api.Assertions.assertThat;
37 import static org.assertj.core.api.Assertions.assertThatThrownBy;
38
39 public class BatchReportReaderImplTest {
40   private static final int COMPONENT_REF = 1;
41   private static final ScannerReport.Changesets CHANGESETS = ScannerReport.Changesets.newBuilder().setComponentRef(COMPONENT_REF).build();
42   private static final ScannerReport.Measure MEASURE = ScannerReport.Measure.newBuilder().build();
43   private static final ScannerReport.Component COMPONENT = ScannerReport.Component.newBuilder().setRef(COMPONENT_REF).build();
44   private static final ScannerReport.Issue ISSUE = ScannerReport.Issue.newBuilder().build();
45   private static final ScannerReport.Duplication DUPLICATION = ScannerReport.Duplication.newBuilder().build();
46   private static final ScannerReport.CpdTextBlock DUPLICATION_BLOCK = ScannerReport.CpdTextBlock.newBuilder().build();
47   private static final ScannerReport.Symbol SYMBOL = ScannerReport.Symbol.newBuilder().build();
48   private static final ScannerReport.SyntaxHighlightingRule SYNTAX_HIGHLIGHTING_1 = ScannerReport.SyntaxHighlightingRule.newBuilder().build();
49   private static final ScannerReport.SyntaxHighlightingRule SYNTAX_HIGHLIGHTING_2 = ScannerReport.SyntaxHighlightingRule.newBuilder().build();
50   private static final ScannerReport.LineCoverage COVERAGE_1 = ScannerReport.LineCoverage.newBuilder().build();
51   private static final ScannerReport.LineCoverage COVERAGE_2 = ScannerReport.LineCoverage.newBuilder().build();
52
53   @Rule
54   public JUnitTempFolder tempFolder = new JUnitTempFolder();
55
56   private ScannerReportWriter writer;
57   private BatchReportReaderImpl underTest;
58
59   @Before
60   public void setUp() {
61     BatchReportDirectoryHolder holder = new ImmutableBatchReportDirectoryHolder(tempFolder.newDir());
62     underTest = new BatchReportReaderImpl(holder);
63     FileStructure fileStructure = new FileStructure(holder.getDirectory());
64     writer = new ScannerReportWriter(fileStructure);
65   }
66
67   @Test
68   public void readMetadata_throws_ISE_if_no_metadata() {
69     assertThatThrownBy(() -> underTest.readMetadata())
70       .isInstanceOf(IllegalStateException.class);
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).toIterable().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)).isExhausted();
102   }
103
104   @Test
105   public void verify_readComponentMeasures_returns_measures() {
106     writer.appendComponentMeasure(COMPONENT_REF, 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.appendComponentMeasure(COMPONENT_REF, 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
142   public void readComponent_throws_ISE_if_file_does_not_exist() {
143     assertThatThrownBy(() -> underTest.readComponent(COMPONENT_REF))
144       .isInstanceOf(IllegalStateException.class);
145   }
146
147   @Test
148   public void verify_readComponent_returns_Component() {
149     writer.writeComponent(COMPONENT);
150
151     assertThat(underTest.readComponent(COMPONENT_REF)).isEqualTo(COMPONENT);
152   }
153
154   @Test
155   public void readComponent_is_not_cached() {
156     writer.writeComponent(COMPONENT);
157
158     assertThat(underTest.readComponent(COMPONENT_REF)).isNotSameAs(underTest.readComponent(COMPONENT_REF));
159   }
160
161   @Test
162   public void readComponentIssues_returns_empty_list_if_file_does_not_exist() {
163     assertThat(underTest.readComponentIssues(COMPONENT_REF)).isExhausted();
164   }
165
166   @Test
167   public void verify_readComponentIssues_returns_Issues() {
168     writer.writeComponentIssues(COMPONENT_REF, of(ISSUE));
169
170     try (CloseableIterator<ScannerReport.Issue> res = underTest.readComponentIssues(COMPONENT_REF)) {
171       assertThat(res.next()).isEqualTo(ISSUE);
172       assertThat(res.hasNext()).isFalse();
173     }
174   }
175
176   @Test
177   public void readComponentIssues_it_not_cached() {
178     writer.writeComponentIssues(COMPONENT_REF, of(ISSUE));
179
180     assertThat(underTest.readComponentIssues(COMPONENT_REF)).isNotSameAs(underTest.readComponentIssues(COMPONENT_REF));
181   }
182
183   @Test
184   public void readComponentDuplications_returns_empty_list_if_file_does_not_exist() {
185     assertThat(underTest.readComponentDuplications(COMPONENT_REF)).isExhausted();
186   }
187
188   @Test
189   public void verify_readComponentDuplications_returns_Issues() {
190     writer.writeComponentDuplications(COMPONENT_REF, of(DUPLICATION));
191
192     try (CloseableIterator<ScannerReport.Duplication> res = underTest.readComponentDuplications(COMPONENT_REF)) {
193       assertThat(res.next()).isEqualTo(DUPLICATION);
194       assertThat(res.hasNext()).isFalse();
195     }
196   }
197
198   @Test
199   public void readComponentDuplications_it_not_cached() {
200     writer.writeComponentDuplications(COMPONENT_REF, of(DUPLICATION));
201
202     assertThat(underTest.readComponentDuplications(COMPONENT_REF)).isNotSameAs(underTest.readComponentDuplications(COMPONENT_REF));
203   }
204
205   @Test
206   public void readComponentDuplicationBlocks_returns_empty_list_if_file_does_not_exist() {
207     assertThat(underTest.readCpdTextBlocks(COMPONENT_REF)).isExhausted();
208   }
209
210   @Test
211   public void verify_readComponentDuplicationBlocks_returns_Issues() {
212     writer.writeCpdTextBlocks(COMPONENT_REF, of(DUPLICATION_BLOCK));
213
214     try (CloseableIterator<ScannerReport.CpdTextBlock> res = underTest.readCpdTextBlocks(COMPONENT_REF)) {
215       assertThat(res.next()).isEqualTo(DUPLICATION_BLOCK);
216       assertThat(res.hasNext()).isFalse();
217     }
218   }
219
220   @Test
221   public void readComponentDuplicationBlocks_is_not_cached() {
222     writer.writeCpdTextBlocks(COMPONENT_REF, of(DUPLICATION_BLOCK));
223
224     assertThat(underTest.readCpdTextBlocks(COMPONENT_REF)).isNotSameAs(underTest.readCpdTextBlocks(COMPONENT_REF));
225   }
226
227   @Test
228   public void readComponentSymbols_returns_empty_list_if_file_does_not_exist() {
229     assertThat(underTest.readComponentSymbols(COMPONENT_REF)).isExhausted();
230   }
231
232   @Test
233   public void verify_readComponentSymbols_returns_Issues() {
234     writer.writeComponentSymbols(COMPONENT_REF, of(SYMBOL));
235
236     try (CloseableIterator<ScannerReport.Symbol> res = underTest.readComponentSymbols(COMPONENT_REF)) {
237       assertThat(res.next()).isEqualTo(SYMBOL);
238       assertThat(res.hasNext()).isFalse();
239     }
240   }
241
242   @Test
243   public void readComponentSymbols_it_not_cached() {
244     writer.writeComponentSymbols(COMPONENT_REF, of(SYMBOL));
245
246     assertThat(underTest.readComponentSymbols(COMPONENT_REF)).isNotSameAs(underTest.readComponentSymbols(COMPONENT_REF));
247   }
248
249   @Test
250   public void readComponentSyntaxHighlighting_returns_empty_CloseableIterator_when_file_does_not_exist() {
251     assertThat(underTest.readComponentSyntaxHighlighting(COMPONENT_REF)).isExhausted();
252   }
253
254   @Test
255   public void verify_readComponentSyntaxHighlighting() {
256     writer.writeComponentSyntaxHighlighting(COMPONENT_REF, of(SYNTAX_HIGHLIGHTING_1, SYNTAX_HIGHLIGHTING_2));
257
258     CloseableIterator<ScannerReport.SyntaxHighlightingRule> res = underTest.readComponentSyntaxHighlighting(COMPONENT_REF);
259     assertThat(res).toIterable().containsExactly(SYNTAX_HIGHLIGHTING_1, SYNTAX_HIGHLIGHTING_2);
260     res.close();
261   }
262
263   @Test
264   public void readComponentCoverage_returns_empty_CloseableIterator_when_file_does_not_exist() {
265     assertThat(underTest.readComponentCoverage(COMPONENT_REF)).isExhausted();
266   }
267
268   @Test
269   public void verify_readComponentCoverage() {
270     writer.writeComponentCoverage(COMPONENT_REF, of(COVERAGE_1, COVERAGE_2));
271
272     CloseableIterator<ScannerReport.LineCoverage> res = underTest.readComponentCoverage(COMPONENT_REF);
273     assertThat(res).toIterable().containsExactly(COVERAGE_1, COVERAGE_2);
274     res.close();
275   }
276
277   @Test
278   public void readFileSource_returns_absent_optional_when_file_does_not_exist() {
279     assertThat(underTest.readFileSource(COMPONENT_REF)).isEmpty();
280   }
281
282   @Test
283   public void verify_readFileSource() throws IOException {
284     File file = writer.getSourceFile(COMPONENT_REF);
285     FileUtils.writeLines(file, of("1", "2", "3"));
286
287     CloseableIterator<String> res = underTest.readFileSource(COMPONENT_REF).get();
288     assertThat(res).toIterable().containsExactly("1", "2", "3");
289     res.close();
290   }
291
292   @Test
293   public void verify_readAnalysisWarnings() {
294     ScannerReport.AnalysisWarning warning1 = ScannerReport.AnalysisWarning.newBuilder().setText("warning 1").build();
295     ScannerReport.AnalysisWarning warning2 = ScannerReport.AnalysisWarning.newBuilder().setText("warning 2").build();
296     ImmutableList<ScannerReport.AnalysisWarning> warnings = of(warning1, warning2);
297     writer.writeAnalysisWarnings(warnings);
298
299     CloseableIterator<ScannerReport.AnalysisWarning> res = underTest.readAnalysisWarnings();
300     assertThat(res).toIterable().containsExactlyElementsOf(warnings);
301     res.close();
302   }
303 }