]> source.dussan.org Git - sonarqube.git/blob
0288d225d2e7953ac32fe76b75cf3f170efdc574
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2024 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.component;
21
22 import java.util.Optional;
23 import org.junit.Before;
24 import org.junit.Rule;
25 import org.junit.Test;
26 import org.sonar.ce.task.projectanalysis.analysis.Analysis;
27 import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolderRule;
28 import org.sonar.ce.task.projectanalysis.source.SourceHashRepository;
29 import org.sonar.db.source.FileHashesDto;
30
31 import static org.assertj.core.api.Assertions.assertThat;
32 import static org.assertj.core.api.Assertions.assertThatThrownBy;
33 import static org.mockito.Mockito.mock;
34 import static org.mockito.Mockito.verify;
35 import static org.mockito.Mockito.when;
36
37 public class FileStatusesImplTest {
38   private static final String PROJECT_KEY = "PROJECT_KEY";
39   private static final String PROJECT_UUID = "UUID-1234";
40
41   @Rule
42   public final TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
43   private final PreviousSourceHashRepository previousSourceHashRepository = mock(PreviousSourceHashRepository.class);
44   private final SourceHashRepository sourceHashRepository = mock(SourceHashRepository.class);
45   @Rule
46   public final AnalysisMetadataHolderRule analysisMetadataHolder = new AnalysisMetadataHolderRule();
47   private final FileStatusesImpl fileStatuses = new FileStatusesImpl(analysisMetadataHolder, treeRootHolder, previousSourceHashRepository, sourceHashRepository);
48
49   @Before
50   public void before() {
51     analysisMetadataHolder.setBaseAnalysis(new Analysis.Builder().setUuid(PROJECT_UUID).setCreatedAt(1000L).build());
52   }
53
54   @Test
55   public void file_is_unchanged_only_if_status_is_SAME_and_hashes_equal() {
56     Component file1 = ReportComponent.builder(Component.Type.FILE, 2, "FILE1_KEY").setStatus(Component.Status.SAME).build();
57     Component file2 = ReportComponent.builder(Component.Type.FILE, 3, "FILE2_KEY").setStatus(Component.Status.SAME).build();
58     Component file3 = ReportComponent.builder(Component.Type.FILE, 4, "FILE3_KEY").setStatus(Component.Status.CHANGED).build();
59
60     addDbFileHash(file1, "hash1");
61     addDbFileHash(file2, "different");
62     addDbFileHash(file3, "hash3");
63
64     addReportFileHash(file1, "hash1");
65     addReportFileHash(file2, "hash2");
66     addReportFileHash(file3, "hash3");
67
68     Component project = ReportComponent.builder(Component.Type.PROJECT, 1)
69       .setUuid(PROJECT_UUID)
70       .setKey(PROJECT_KEY)
71       .addChildren(file1, file2, file3)
72       .build();
73     treeRootHolder.setRoot(project);
74   fileStatuses.initialize();
75     assertThat(fileStatuses.isUnchanged(file1)).isTrue();
76     assertThat(fileStatuses.isUnchanged(file2)).isFalse();
77     assertThat(fileStatuses.isUnchanged(file2)).isFalse();
78   }
79
80   @Test
81   public void isDataUnchanged_returns_false_if_any_SAME_status_is_incorrect() {
82     Component file1 = ReportComponent.builder(Component.Type.FILE, 2, "FILE1_KEY").setStatus(Component.Status.SAME)
83       .setFileAttributes(new FileAttributes(false, null, 10, true, null)).build();
84     Component file2 = ReportComponent.builder(Component.Type.FILE, 3, "FILE2_KEY").setStatus(Component.Status.SAME).build();
85
86     addDbFileHash(file1, "hash1");
87     addDbFileHash(file2, "different");
88
89     addReportFileHash(file1, "hash1");
90     addReportFileHash(file2, "hash2");
91
92     Component project = ReportComponent.builder(Component.Type.PROJECT, 1)
93       .setUuid(PROJECT_UUID)
94       .setKey(PROJECT_KEY)
95       .addChildren(file1, file2)
96       .build();
97     treeRootHolder.setRoot(project);
98     fileStatuses.initialize();
99     assertThat(fileStatuses.isDataUnchanged(file1)).isFalse();
100     assertThat(fileStatuses.isDataUnchanged(file2)).isFalse();
101   }
102
103   @Test
104   public void isDataUnchanged_returns_false_no_previous_analysis() {
105     analysisMetadataHolder.setBaseAnalysis(null);
106
107     Component file1 = ReportComponent.builder(Component.Type.FILE, 2, "FILE1_KEY").setStatus(Component.Status.SAME)
108       .setFileAttributes(new FileAttributes(false, null, 10, true, null)).build();
109     Component file2 = ReportComponent.builder(Component.Type.FILE, 3, "FILE2_KEY").setStatus(Component.Status.SAME).build();
110
111     addReportFileHash(file1, "hash1");
112     addReportFileHash(file2, "hash2");
113
114     Component project = ReportComponent.builder(Component.Type.PROJECT, 1)
115       .setUuid(PROJECT_UUID)
116       .setKey(PROJECT_KEY)
117       .addChildren(file1, file2)
118       .build();
119     treeRootHolder.setRoot(project);
120     fileStatuses.initialize();
121
122     assertThat(fileStatuses.isDataUnchanged(file1)).isFalse();
123     assertThat(fileStatuses.isDataUnchanged(file2)).isFalse();
124   }
125
126   @Test
127   public void isDataUnchanged_returns_false_if_not_set_by_analyzer() {
128     Component file1 = ReportComponent.builder(Component.Type.FILE, 2, "FILE1_KEY").setStatus(Component.Status.SAME)
129       .setFileAttributes(new FileAttributes(false, null, 10, false,null)).build();
130     Component file2 = ReportComponent.builder(Component.Type.FILE, 3, "FILE2_KEY").setStatus(Component.Status.SAME).build();
131
132     addDbFileHash(file1, "hash1");
133     addDbFileHash(file2, "hash2");
134
135     addReportFileHash(file1, "hash1");
136     addReportFileHash(file2, "hash2");
137
138     Component project = ReportComponent.builder(Component.Type.PROJECT, 1)
139       .setUuid(PROJECT_UUID)
140       .setKey(PROJECT_KEY)
141       .addChildren(file1, file2)
142       .build();
143     treeRootHolder.setRoot(project);
144     fileStatuses.initialize();
145     assertThat(fileStatuses.isDataUnchanged(file1)).isFalse();
146     assertThat(fileStatuses.isDataUnchanged(file2)).isFalse();
147   }
148
149   @Test
150   public void isDataUnchanged_returns_true_if_set_by_analyzer_and_all_SAME_status_are_correct() {
151     Component file1 = ReportComponent.builder(Component.Type.FILE, 2, "FILE1_KEY").setStatus(Component.Status.SAME)
152       .setFileAttributes(new FileAttributes(false, null, 10, true,null)).build();
153     Component file2 = ReportComponent.builder(Component.Type.FILE, 3, "FILE2_KEY").setStatus(Component.Status.SAME).build();
154     Component file3 = ReportComponent.builder(Component.Type.FILE, 4, "FILE3_KEY").setStatus(Component.Status.CHANGED).build();
155
156     addDbFileHash(file1, "hash1");
157     addDbFileHash(file2, "hash2");
158     addDbFileHash(file3, "hash3");
159
160     addReportFileHash(file1, "hash1");
161     addReportFileHash(file2, "hash2");
162     addReportFileHash(file3, "different");
163
164     Component project = ReportComponent.builder(Component.Type.PROJECT, 1)
165       .setUuid(PROJECT_UUID)
166       .setKey(PROJECT_KEY)
167       .addChildren(file1, file2, file3)
168       .build();
169     treeRootHolder.setRoot(project);
170     fileStatuses.initialize();
171     assertThat(fileStatuses.isDataUnchanged(file1)).isTrue();
172     assertThat(fileStatuses.isDataUnchanged(file2)).isFalse();
173
174     verify(previousSourceHashRepository).getDbFile(file1);
175   }
176
177   @Test
178   public void getFileUuidsMarkedAsUnchanged_whenNotInitialized_shouldFail() {
179     assertThatThrownBy(fileStatuses::getFileUuidsMarkedAsUnchanged)
180       .isInstanceOf(IllegalStateException.class)
181       .hasMessage("Not initialized");
182   }
183
184   @Test
185   public void getFileUuidsMarkedAsUnchanged_shouldReturnMarkAsUnchangedFileUuids() {
186     Component file1 = ReportComponent.builder(Component.Type.FILE, 2, "FILE1_KEY").setStatus(Component.Status.SAME)
187       .setFileAttributes(new FileAttributes(false, null, 10, true, null)).build();
188     Component file2 = ReportComponent.builder(Component.Type.FILE, 3, "FILE2_KEY").setStatus(Component.Status.SAME).build();
189     addDbFileHash(file1, "hash1");
190     addDbFileHash(file2, "hash2");
191     addReportFileHash(file1, "hash1");
192     addReportFileHash(file2, "hash2");
193     Component project = ReportComponent.builder(Component.Type.PROJECT, 1)
194       .setUuid(PROJECT_UUID)
195       .setKey(PROJECT_KEY)
196       .addChildren(file1, file2)
197       .build();
198     treeRootHolder.setRoot(project);
199     fileStatuses.initialize();
200
201     assertThat(fileStatuses.getFileUuidsMarkedAsUnchanged()).contains(file1.getUuid());
202   }
203
204   private void addDbFileHash(Component file, String hash) {
205     FileHashesDto fileHashesDto = new FileHashesDto().setSrcHash(hash);
206     when(previousSourceHashRepository.getDbFile(file)).thenReturn(Optional.of(fileHashesDto));
207   }
208
209   private void addReportFileHash(Component file, String hash) {
210     when(sourceHashRepository.getRawSourceHash(file)).thenReturn(hash);
211   }
212 }