]> source.dussan.org Git - sonarqube.git/blob
8ae9f4bd043e07107801a2f43bf474b46ed727c6
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2020 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.source;
21
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;
32
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;
38
39 public class SourceLinesDiffImplTest {
40
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);
48
49   private SourceLinesDiffImpl underTest = new SourceLinesDiffImpl(dbClient, fileSourceDao, sourceLinesHash,
50     referenceBranchComponentUuids, analysisMetadataHolder);
51
52   private static final int FILE_REF = 1;
53
54   private static final String[] CONTENT = {
55     "package org.sonar.ce.task.projectanalysis.source_diff;",
56     "",
57     "public class Foo {",
58     "  public String bar() {",
59     "    return \"Doh!\";",
60     "  }",
61     "}"
62   };
63
64   @Before
65   public void setUp() {
66     when(dbClient.openSession(false)).thenReturn(dbSession);
67     when(dbClient.componentDao()).thenReturn(componentDao);
68     when(dbClient.fileSourceDao()).thenReturn(fileSourceDao);
69   }
70
71   @Test
72   public void should_find_diff_with_reference_branch_for_prs() {
73     Component component = fileComponent(FILE_REF);
74
75     mockLineHashesInDb(2, CONTENT);
76     setLineHashesInReport(component, CONTENT);
77
78     when(analysisMetadataHolder.isPullRequest()).thenReturn(true);
79     when(referenceBranchComponentUuids.getComponentUuid(component.getKey())).thenReturn("uuid_2");
80
81     assertThat(underTest.computeMatchingLines(component)).containsExactly(1, 2, 3, 4, 5, 6, 7);
82   }
83
84   @Test
85   public void all_file_is_modified_if_no_source_in_db() {
86     Component component = fileComponent(FILE_REF);
87
88     setLineHashesInReport(component, CONTENT);
89
90     assertThat(underTest.computeMatchingLines(component)).containsExactly(0, 0, 0, 0, 0, 0, 0);
91   }
92
93   @Test
94   public void should_find_no_diff_when_report_and_db_content_are_identical() {
95     Component component = fileComponent(FILE_REF);
96
97     mockLineHashesInDb(FILE_REF, CONTENT);
98     setLineHashesInReport(component, CONTENT);
99
100     assertThat(underTest.computeMatchingLines(component)).containsExactly(1, 2, 3, 4, 5, 6, 7);
101   }
102
103   private void mockLineHashesInDb(int ref, String[] lineHashes) {
104     when(fileSourceDao.selectLineHashes(dbSession, componentUuidOf(String.valueOf(ref))))
105       .thenReturn(Arrays.asList(lineHashes));
106   }
107
108   private static String componentUuidOf(String key) {
109     return "uuid_" + key;
110   }
111
112   private static Component fileComponent(int ref) {
113     return builder(FILE, ref)
114       .setName("report_path" + ref)
115       .setUuid(componentUuidOf("" + ref))
116       .build();
117   }
118
119   private void setLineHashesInReport(Component component, String[] content) {
120     when(sourceLinesHash.getLineHashesMatchingDBVersion(component)).thenReturn(Arrays.asList(content));
121   }
122 }