]> source.dussan.org Git - sonarqube.git/blob
980c00bc6729be81d947dd8653f20de6d6973c2e
[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.scm;
21
22 import static org.assertj.core.api.Assertions.assertThat;
23 import static org.assertj.guava.api.Assertions.assertThat;
24 import static org.mockito.Mockito.mock;
25 import static org.mockito.Mockito.verifyNoMoreInteractions;
26 import static org.sonar.api.utils.log.LoggerLevel.TRACE;
27 import static org.sonar.server.computation.task.projectanalysis.component.ReportComponent.builder;
28
29 import java.util.EnumSet;
30 import java.util.Iterator;
31 import java.util.List;
32
33 import javax.annotation.Nullable;
34
35 import org.junit.Rule;
36 import org.junit.Test;
37 import org.junit.rules.ExpectedException;
38 import org.junit.runner.RunWith;
39 import org.sonar.api.utils.System2;
40 import org.sonar.api.utils.log.LogTester;
41 import org.sonar.core.hash.SourceHashComputer;
42 import org.sonar.db.DbClient;
43 import org.sonar.db.DbTester;
44 import org.sonar.db.protobuf.DbFileSources;
45 import org.sonar.db.source.FileSourceDto;
46 import org.sonar.scanner.protocol.output.ScannerReport;
47 import org.sonar.server.computation.task.projectanalysis.analysis.Analysis;
48 import org.sonar.server.computation.task.projectanalysis.analysis.AnalysisMetadataHolder;
49 import org.sonar.server.computation.task.projectanalysis.analysis.AnalysisMetadataHolderRule;
50 import org.sonar.server.computation.task.projectanalysis.batch.BatchReportReader;
51 import org.sonar.server.computation.task.projectanalysis.batch.BatchReportReaderRule;
52 import org.sonar.server.computation.task.projectanalysis.component.Component;
53 import org.sonar.server.computation.task.projectanalysis.component.Component.Status;
54 import org.sonar.server.computation.task.projectanalysis.component.ReportComponent;
55 import org.sonar.server.computation.task.projectanalysis.component.ViewsComponent;
56 import org.sonar.server.computation.task.projectanalysis.source.SourceHashRepository;
57 import org.sonar.server.computation.task.projectanalysis.source.SourceHashRepositoryImpl;
58 import org.sonar.server.computation.task.projectanalysis.source.SourceLinesRepositoryImpl;
59
60 import com.google.common.collect.ImmutableList;
61 import com.tngtech.java.junit.dataprovider.DataProvider;
62 import com.tngtech.java.junit.dataprovider.DataProviderRunner;
63 import com.tngtech.java.junit.dataprovider.UseDataProvider;
64
65 @RunWith(DataProviderRunner.class)
66 public class ScmInfoRepositoryImplTest {
67
68   static final int FILE_REF = 1;
69   static final Component FILE = builder(Component.Type.FILE, FILE_REF).setKey("FILE_KEY").setUuid("FILE_UUID").build();
70   static final long DATE_1 = 123456789L;
71   static final long DATE_2 = 1234567810L;
72
73   static Analysis baseProjectAnalysis = new Analysis.Builder()
74     .setId(1)
75     .setUuid("uuid_1")
76     .setCreatedAt(123456789L)
77     .build();
78
79   @Rule
80   public ExpectedException thrown = ExpectedException.none();
81   @Rule
82   public LogTester logTester = new LogTester();
83   @Rule
84   public BatchReportReaderRule reportReader = new BatchReportReaderRule();
85   @Rule
86   public AnalysisMetadataHolderRule analysisMetadataHolder = new AnalysisMetadataHolderRule();
87   @Rule
88   public DbTester dbTester = DbTester.create(System2.INSTANCE);
89
90   DbClient dbClient = dbTester.getDbClient();
91
92   ScmInfoRepositoryImpl underTest = new ScmInfoRepositoryImpl(reportReader, analysisMetadataHolder, dbClient,
93     new SourceHashRepositoryImpl(new SourceLinesRepositoryImpl(reportReader)));
94
95   @Test
96   public void dont_check_hash_for_unmodified_files_incremental_analysis() {
97     analysisMetadataHolder.setIncrementalAnalysis(true);
98     analysisMetadataHolder.setBaseAnalysis(baseProjectAnalysis);
99
100     addFileSourceInDb("henry", DATE_1, "rev-1", computeSourceHash(1));
101     addCopyFromPreviousChangesetInReport();
102
103     Component file = builder(Component.Type.FILE, FILE_REF).setKey("FILE_KEY").setUuid("FILE_UUID").setStatus(Status.SAME).build();
104     ScmInfo scmInfo = underTest.getScmInfo(file).get();
105     assertThat(scmInfo.getAllChangesets()).hasSize(1);
106
107     assertThat(logTester.logs(TRACE)).containsOnly("Reading SCM info from db for file 'FILE_KEY'");
108   }
109
110   @Test
111   public void read_from_report() throws Exception {
112     analysisMetadataHolder.setBaseAnalysis(baseProjectAnalysis);
113     addChangesetInReport("john", DATE_1, "rev-1");
114
115     ScmInfo scmInfo = underTest.getScmInfo(FILE).get();
116     assertThat(scmInfo.getAllChangesets()).hasSize(1);
117
118     assertThat(logTester.logs(TRACE)).containsOnly("Reading SCM info from report for file 'FILE_KEY'");
119   }
120
121   @Test
122   public void getScmInfo_returns_absent_if_CopyFromPrevious_is_false_and_there_is_no_changeset_in_report() {
123     analysisMetadataHolder.setBaseAnalysis(baseProjectAnalysis);
124     // put data in DB, which should not be used
125     addFileSourceInDb("henry", DATE_1, "rev-1", computeSourceHash(1));
126     addFileSourceInReport(1);
127
128     assertThat(underTest.getScmInfo(FILE)).isAbsent();
129   }
130
131   @Test
132   public void getScmInfo_returns_ScmInfo_from_DB_CopyFromPrevious_is_true_if_hashes_are_the_same() throws Exception {
133     analysisMetadataHolder.setBaseAnalysis(baseProjectAnalysis);
134     analysisMetadataHolder.setIncrementalAnalysis(false);
135
136     addFileSourceInDb("henry", DATE_1, "rev-1", computeSourceHash(1));
137     addFileSourceInReport(1);
138     addCopyFromPreviousChangesetInReport();
139
140     ScmInfo scmInfo = underTest.getScmInfo(FILE).get();
141     assertThat(scmInfo.getAllChangesets()).hasSize(1);
142
143     assertThat(logTester.logs(TRACE)).containsOnly("Reading SCM info from db for file 'FILE_KEY'");
144   }
145
146   @Test
147   public void getScmInfo_returns_absent_when_CopyFromPrevious_is_true_but_hashes_are_not_the_same() throws Exception {
148     analysisMetadataHolder.setBaseAnalysis(baseProjectAnalysis);
149     analysisMetadataHolder.setIncrementalAnalysis(false);
150
151     addFileSourceInDb("henry", DATE_1, "rev-1", computeSourceHash(1) + "_different");
152     addFileSourceInReport(1);
153     addCopyFromPreviousChangesetInReport();
154
155     assertThat(underTest.getScmInfo(FILE)).isAbsent();
156
157     assertThat(logTester.logs(TRACE)).containsOnly("Reading SCM info from db for file 'FILE_KEY'");
158   }
159
160   @Test
161   public void read_from_report_even_if_data_in_db_exists() throws Exception {
162     analysisMetadataHolder.setBaseAnalysis(baseProjectAnalysis);
163     addFileSourceInDb("henry", DATE_1, "rev-1", computeSourceHash(1));
164     addChangesetInReport("john", DATE_2, "rev-2");
165
166     ScmInfo scmInfo = underTest.getScmInfo(FILE).get();
167
168     Changeset changeset = scmInfo.getChangesetForLine(1);
169     assertThat(changeset.getAuthor()).isEqualTo("john");
170     assertThat(changeset.getDate()).isEqualTo(DATE_2);
171     assertThat(changeset.getRevision()).isEqualTo("rev-2");
172   }
173
174   @Test
175   public void read_from_db_even_if_data_in_report_exists_when_CopyFromPrevious_is_true() throws Exception {
176     analysisMetadataHolder.setBaseAnalysis(baseProjectAnalysis);
177     analysisMetadataHolder.setIncrementalAnalysis(false);
178
179     addFileSourceInDb("henry", DATE_1, "rev-1", computeSourceHash(1));
180     addFileSourceInReport(1);
181     addChangesetInReport("john", DATE_2, "rev-2", true);
182
183     ScmInfo scmInfo = underTest.getScmInfo(FILE).get();
184
185     Changeset changeset = scmInfo.getChangesetForLine(1);
186     assertThat(changeset.getAuthor()).isEqualTo("henry");
187     assertThat(changeset.getDate()).isEqualTo(DATE_1);
188     assertThat(changeset.getRevision()).isEqualTo("rev-1");
189   }
190
191   @Test
192   public void return_nothing_when_no_data_in_report_nor_db() throws Exception {
193     analysisMetadataHolder.setBaseAnalysis(baseProjectAnalysis);
194     assertThat(underTest.getScmInfo(FILE)).isAbsent();
195   }
196
197   @Test
198   public void return_nothing_when_nothing_in_report_and_db_has_no_scm() throws Exception {
199     analysisMetadataHolder.setBaseAnalysis(baseProjectAnalysis);
200     addFileSourceInDb(null, null, null, "don't care");
201     addFileSourceInReport(1);
202
203     assertThat(underTest.getScmInfo(FILE)).isAbsent();
204   }
205
206   @Test
207   public void fail_with_NPE_when_component_is_null() throws Exception {
208     analysisMetadataHolder.setBaseAnalysis(baseProjectAnalysis);
209
210     thrown.expect(NullPointerException.class);
211     thrown.expectMessage("Component cannot be bull");
212
213     underTest.getScmInfo(null);
214   }
215
216   @DataProvider
217   public static Object[][] allTypeComponentButFile() {
218     Object[][] res = new Object[Component.Type.values().length - 1][1];
219     int i = 0;
220     for (Component.Type type : EnumSet.complementOf(EnumSet.of(Component.Type.FILE))) {
221       if (type.isReportType()) {
222         res[i][0] = ReportComponent.builder(type, i).build();
223       } else {
224         res[i][0] = ViewsComponent.builder(type, i).build();
225       }
226       i++;
227     }
228     return res;
229   }
230
231   @Test
232   @UseDataProvider("allTypeComponentButFile")
233   public void do_not_query_db_nor_report_if_component_type_is_not_FILE(Component component) {
234     BatchReportReader batchReportReader = mock(BatchReportReader.class);
235     AnalysisMetadataHolder analysisMetadataHolder = mock(AnalysisMetadataHolder.class);
236     DbClient dbClient = mock(DbClient.class);
237     SourceHashRepository sourceHashRepository = mock(SourceHashRepository.class);
238     ScmInfoRepositoryImpl underTest = new ScmInfoRepositoryImpl(batchReportReader, analysisMetadataHolder, dbClient, sourceHashRepository);
239
240     assertThat(underTest.getScmInfo(component)).isAbsent();
241
242     verifyNoMoreInteractions(batchReportReader, analysisMetadataHolder, dbClient, sourceHashRepository);
243   }
244
245   @Test
246   public void load_scm_info_from_cache_when_already_read() throws Exception {
247     analysisMetadataHolder.setBaseAnalysis(baseProjectAnalysis);
248     addChangesetInReport("john", DATE_1, "rev-1");
249     ScmInfo scmInfo = underTest.getScmInfo(FILE).get();
250     assertThat(scmInfo.getAllChangesets()).hasSize(1);
251
252     assertThat(logTester.logs(TRACE)).hasSize(1);
253     logTester.clear();
254
255     underTest.getScmInfo(FILE);
256     assertThat(logTester.logs(TRACE)).isEmpty();
257   }
258
259   @Test
260   public void not_read_in_db_on_first_analysis_when_CopyFromPrevious_is_true() throws Exception {
261     analysisMetadataHolder.setBaseAnalysis(null);
262     addFileSourceInDb("henry", DATE_1, "rev-1", "don't care");
263     addFileSourceInReport(1);
264     addCopyFromPreviousChangesetInReport();
265
266     assertThat(underTest.getScmInfo(FILE)).isAbsent();
267     assertThat(logTester.logs(TRACE)).isEmpty();
268   }
269
270   private void addFileSourceInDb(@Nullable String author, @Nullable Long date, @Nullable String revision, String srcHash) {
271     DbFileSources.Data.Builder fileDataBuilder = DbFileSources.Data.newBuilder();
272     DbFileSources.Line.Builder builder = fileDataBuilder.addLinesBuilder()
273       .setLine(1);
274     if (author != null) {
275       builder.setScmAuthor(author);
276     }
277     if (date != null) {
278       builder.setScmDate(date);
279     }
280     if (revision != null) {
281       builder.setScmRevision(revision);
282     }
283     dbTester.getDbClient().fileSourceDao().insert(dbTester.getSession(), new FileSourceDto()
284       .setFileUuid(FILE.getUuid())
285       .setProjectUuid("PROJECT_UUID")
286       .setSourceData(fileDataBuilder.build())
287       .setSrcHash(srcHash));
288     dbTester.commit();
289   }
290
291   private void addCopyFromPreviousChangesetInReport() {
292     reportReader.putChangesets(ScannerReport.Changesets.newBuilder()
293       .setComponentRef(FILE_REF)
294       .setCopyFromPrevious(true)
295       .build());
296   }
297
298   private void addChangesetInReport(String author, Long date, String revision) {
299     addChangesetInReport(author, date, revision, false);
300   }
301
302   private void addChangesetInReport(String author, Long date, String revision, boolean copyFromPrevious) {
303     reportReader.putChangesets(ScannerReport.Changesets.newBuilder()
304       .setComponentRef(FILE_REF)
305       .setCopyFromPrevious(copyFromPrevious)
306       .addChangeset(ScannerReport.Changesets.Changeset.newBuilder()
307         .setAuthor(author)
308         .setDate(date)
309         .setRevision(revision)
310         .build())
311       .addChangesetIndexByLine(0)
312       .build());
313   }
314
315   private void addFileSourceInReport(int lineCount) {
316     reportReader.putFileSourceLines(FILE_REF, generateLines(lineCount));
317     reportReader.putComponent(ScannerReport.Component.newBuilder()
318       .setRef(FILE_REF)
319       .setLines(lineCount)
320       .build());
321   }
322
323   private static List<String> generateLines(int lineCount) {
324     ImmutableList.Builder<String> builder = ImmutableList.builder();
325     for (int i = 0; i < lineCount; i++) {
326       builder.add("line " + i);
327     }
328     return builder.build();
329   }
330
331   private static String computeSourceHash(int lineCount) {
332     SourceHashComputer sourceHashComputer = new SourceHashComputer();
333     Iterator<String> lines = generateLines(lineCount).iterator();
334     while (lines.hasNext()) {
335       sourceHashComputer.addLine(lines.next(), lines.hasNext());
336     }
337     return sourceHashComputer.getHash();
338   }
339 }