3 * Copyright (C) 2009-2017 SonarSource SA
4 * mailto:info AT sonarsource DOT com
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.
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.
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.
20 package org.sonar.server.computation.task.projectanalysis.scm;
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;
29 import java.util.EnumSet;
30 import java.util.Iterator;
31 import java.util.List;
33 import javax.annotation.Nullable;
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;
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;
65 @RunWith(DataProviderRunner.class)
66 public class ScmInfoRepositoryImplTest {
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;
73 static Analysis baseProjectAnalysis = new Analysis.Builder()
76 .setCreatedAt(123456789L)
80 public ExpectedException thrown = ExpectedException.none();
82 public LogTester logTester = new LogTester();
84 public BatchReportReaderRule reportReader = new BatchReportReaderRule();
86 public AnalysisMetadataHolderRule analysisMetadataHolder = new AnalysisMetadataHolderRule();
88 public DbTester dbTester = DbTester.create(System2.INSTANCE);
90 DbClient dbClient = dbTester.getDbClient();
92 ScmInfoRepositoryImpl underTest = new ScmInfoRepositoryImpl(reportReader, analysisMetadataHolder, dbClient,
93 new SourceHashRepositoryImpl(new SourceLinesRepositoryImpl(reportReader)));
96 public void dont_check_hash_for_unmodified_files_incremental_analysis() {
97 analysisMetadataHolder.setIncrementalAnalysis(true);
98 analysisMetadataHolder.setBaseAnalysis(baseProjectAnalysis);
100 addFileSourceInDb("henry", DATE_1, "rev-1", computeSourceHash(1));
101 addCopyFromPreviousChangesetInReport();
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);
107 assertThat(logTester.logs(TRACE)).containsOnly("Reading SCM info from db for file 'FILE_KEY'");
111 public void read_from_report() throws Exception {
112 analysisMetadataHolder.setBaseAnalysis(baseProjectAnalysis);
113 addChangesetInReport("john", DATE_1, "rev-1");
115 ScmInfo scmInfo = underTest.getScmInfo(FILE).get();
116 assertThat(scmInfo.getAllChangesets()).hasSize(1);
118 assertThat(logTester.logs(TRACE)).containsOnly("Reading SCM info from report for file 'FILE_KEY'");
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);
128 assertThat(underTest.getScmInfo(FILE)).isAbsent();
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);
136 addFileSourceInDb("henry", DATE_1, "rev-1", computeSourceHash(1));
137 addFileSourceInReport(1);
138 addCopyFromPreviousChangesetInReport();
140 ScmInfo scmInfo = underTest.getScmInfo(FILE).get();
141 assertThat(scmInfo.getAllChangesets()).hasSize(1);
143 assertThat(logTester.logs(TRACE)).containsOnly("Reading SCM info from db for file 'FILE_KEY'");
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);
151 addFileSourceInDb("henry", DATE_1, "rev-1", computeSourceHash(1) + "_different");
152 addFileSourceInReport(1);
153 addCopyFromPreviousChangesetInReport();
155 assertThat(underTest.getScmInfo(FILE)).isAbsent();
157 assertThat(logTester.logs(TRACE)).containsOnly("Reading SCM info from db for file 'FILE_KEY'");
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");
166 ScmInfo scmInfo = underTest.getScmInfo(FILE).get();
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");
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);
179 addFileSourceInDb("henry", DATE_1, "rev-1", computeSourceHash(1));
180 addFileSourceInReport(1);
181 addChangesetInReport("john", DATE_2, "rev-2", true);
183 ScmInfo scmInfo = underTest.getScmInfo(FILE).get();
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");
192 public void return_nothing_when_no_data_in_report_nor_db() throws Exception {
193 analysisMetadataHolder.setBaseAnalysis(baseProjectAnalysis);
194 assertThat(underTest.getScmInfo(FILE)).isAbsent();
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);
203 assertThat(underTest.getScmInfo(FILE)).isAbsent();
207 public void fail_with_NPE_when_component_is_null() throws Exception {
208 analysisMetadataHolder.setBaseAnalysis(baseProjectAnalysis);
210 thrown.expect(NullPointerException.class);
211 thrown.expectMessage("Component cannot be bull");
213 underTest.getScmInfo(null);
217 public static Object[][] allTypeComponentButFile() {
218 Object[][] res = new Object[Component.Type.values().length - 1][1];
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();
224 res[i][0] = ViewsComponent.builder(type, i).build();
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);
240 assertThat(underTest.getScmInfo(component)).isAbsent();
242 verifyNoMoreInteractions(batchReportReader, analysisMetadataHolder, dbClient, sourceHashRepository);
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);
252 assertThat(logTester.logs(TRACE)).hasSize(1);
255 underTest.getScmInfo(FILE);
256 assertThat(logTester.logs(TRACE)).isEmpty();
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();
266 assertThat(underTest.getScmInfo(FILE)).isAbsent();
267 assertThat(logTester.logs(TRACE)).isEmpty();
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()
274 if (author != null) {
275 builder.setScmAuthor(author);
278 builder.setScmDate(date);
280 if (revision != null) {
281 builder.setScmRevision(revision);
283 dbTester.getDbClient().fileSourceDao().insert(dbTester.getSession(), new FileSourceDto()
284 .setFileUuid(FILE.getUuid())
285 .setProjectUuid("PROJECT_UUID")
286 .setSourceData(fileDataBuilder.build())
287 .setSrcHash(srcHash));
291 private void addCopyFromPreviousChangesetInReport() {
292 reportReader.putChangesets(ScannerReport.Changesets.newBuilder()
293 .setComponentRef(FILE_REF)
294 .setCopyFromPrevious(true)
298 private void addChangesetInReport(String author, Long date, String revision) {
299 addChangesetInReport(author, date, revision, false);
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()
309 .setRevision(revision)
311 .addChangesetIndexByLine(0)
315 private void addFileSourceInReport(int lineCount) {
316 reportReader.putFileSourceLines(FILE_REF, generateLines(lineCount));
317 reportReader.putComponent(ScannerReport.Component.newBuilder()
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);
328 return builder.build();
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());
337 return sourceHashComputer.getHash();