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