3 * Copyright (C) 2009-2020 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.ce.task.projectanalysis.source;
22 import java.util.Arrays;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolder;
26 import org.sonar.ce.task.projectanalysis.component.Component;
27 import org.sonar.ce.task.projectanalysis.component.ReferenceBranchComponentUuids;
28 import org.sonar.db.DbClient;
29 import org.sonar.db.DbSession;
30 import org.sonar.db.component.ComponentDao;
31 import org.sonar.db.source.FileSourceDao;
33 import static org.assertj.core.api.Assertions.assertThat;
34 import static org.mockito.Mockito.mock;
35 import static org.mockito.Mockito.when;
36 import static org.sonar.ce.task.projectanalysis.component.Component.Type.FILE;
37 import static org.sonar.ce.task.projectanalysis.component.ReportComponent.builder;
39 public class SourceLinesDiffImplTest {
41 private DbClient dbClient = mock(DbClient.class);
42 private DbSession dbSession = mock(DbSession.class);
43 private ComponentDao componentDao = mock(ComponentDao.class);
44 private FileSourceDao fileSourceDao = mock(FileSourceDao.class);
45 private SourceLinesHashRepository sourceLinesHash = mock(SourceLinesHashRepository.class);
46 private AnalysisMetadataHolder analysisMetadataHolder = mock(AnalysisMetadataHolder.class);
47 private ReferenceBranchComponentUuids referenceBranchComponentUuids = mock(ReferenceBranchComponentUuids.class);
49 private SourceLinesDiffImpl underTest = new SourceLinesDiffImpl(dbClient, fileSourceDao, sourceLinesHash,
50 referenceBranchComponentUuids, analysisMetadataHolder);
52 private static final int FILE_REF = 1;
54 private static final String[] CONTENT = {
55 "package org.sonar.ce.task.projectanalysis.source_diff;",
58 " public String bar() {",
66 when(dbClient.openSession(false)).thenReturn(dbSession);
67 when(dbClient.componentDao()).thenReturn(componentDao);
68 when(dbClient.fileSourceDao()).thenReturn(fileSourceDao);
72 public void should_find_diff_with_reference_branch_for_prs() {
73 Component component = fileComponent(FILE_REF);
75 mockLineHashesInDb(2, CONTENT);
76 setLineHashesInReport(component, CONTENT);
78 when(analysisMetadataHolder.isPullRequest()).thenReturn(true);
79 when(referenceBranchComponentUuids.getComponentUuid(component.getKey())).thenReturn("uuid_2");
81 assertThat(underTest.computeMatchingLines(component)).containsExactly(1, 2, 3, 4, 5, 6, 7);
85 public void all_file_is_modified_if_no_source_in_db() {
86 Component component = fileComponent(FILE_REF);
88 setLineHashesInReport(component, CONTENT);
90 assertThat(underTest.computeMatchingLines(component)).containsExactly(0, 0, 0, 0, 0, 0, 0);
94 public void should_find_no_diff_when_report_and_db_content_are_identical() {
95 Component component = fileComponent(FILE_REF);
97 mockLineHashesInDb(FILE_REF, CONTENT);
98 setLineHashesInReport(component, CONTENT);
100 assertThat(underTest.computeMatchingLines(component)).containsExactly(1, 2, 3, 4, 5, 6, 7);
103 private void mockLineHashesInDb(int ref, String[] lineHashes) {
104 when(fileSourceDao.selectLineHashes(dbSession, componentUuidOf(String.valueOf(ref))))
105 .thenReturn(Arrays.asList(lineHashes));
108 private static String componentUuidOf(String key) {
109 return "uuid_" + key;
112 private static Component fileComponent(int ref) {
113 return builder(FILE, ref)
114 .setName("report_path" + ref)
115 .setUuid(componentUuidOf("" + ref))
119 private void setLineHashesInReport(Component component, String[] content) {
120 when(sourceLinesHash.getLineHashesMatchingDBVersion(component)).thenReturn(Arrays.asList(content));