3 * Copyright (C) 2009-2024 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.component;
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;
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;
37 public class FileStatusesImplTest {
38 private static final String PROJECT_KEY = "PROJECT_KEY";
39 private static final String PROJECT_UUID = "UUID-1234";
42 public final TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
43 private final PreviousSourceHashRepository previousSourceHashRepository = mock(PreviousSourceHashRepository.class);
44 private final SourceHashRepository sourceHashRepository = mock(SourceHashRepository.class);
46 public final AnalysisMetadataHolderRule analysisMetadataHolder = new AnalysisMetadataHolderRule();
47 private final FileStatusesImpl fileStatuses = new FileStatusesImpl(analysisMetadataHolder, treeRootHolder, previousSourceHashRepository, sourceHashRepository);
50 public void before() {
51 analysisMetadataHolder.setBaseAnalysis(new Analysis.Builder().setUuid(PROJECT_UUID).setCreatedAt(1000L).build());
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();
60 addDbFileHash(file1, "hash1");
61 addDbFileHash(file2, "different");
62 addDbFileHash(file3, "hash3");
64 addReportFileHash(file1, "hash1");
65 addReportFileHash(file2, "hash2");
66 addReportFileHash(file3, "hash3");
68 Component project = ReportComponent.builder(Component.Type.PROJECT, 1)
69 .setUuid(PROJECT_UUID)
71 .addChildren(file1, file2, file3)
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();
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();
86 addDbFileHash(file1, "hash1");
87 addDbFileHash(file2, "different");
89 addReportFileHash(file1, "hash1");
90 addReportFileHash(file2, "hash2");
92 Component project = ReportComponent.builder(Component.Type.PROJECT, 1)
93 .setUuid(PROJECT_UUID)
95 .addChildren(file1, file2)
97 treeRootHolder.setRoot(project);
98 fileStatuses.initialize();
99 assertThat(fileStatuses.isDataUnchanged(file1)).isFalse();
100 assertThat(fileStatuses.isDataUnchanged(file2)).isFalse();
104 public void isDataUnchanged_returns_false_no_previous_analysis() {
105 analysisMetadataHolder.setBaseAnalysis(null);
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();
111 addReportFileHash(file1, "hash1");
112 addReportFileHash(file2, "hash2");
114 Component project = ReportComponent.builder(Component.Type.PROJECT, 1)
115 .setUuid(PROJECT_UUID)
117 .addChildren(file1, file2)
119 treeRootHolder.setRoot(project);
120 fileStatuses.initialize();
122 assertThat(fileStatuses.isDataUnchanged(file1)).isFalse();
123 assertThat(fileStatuses.isDataUnchanged(file2)).isFalse();
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();
132 addDbFileHash(file1, "hash1");
133 addDbFileHash(file2, "hash2");
135 addReportFileHash(file1, "hash1");
136 addReportFileHash(file2, "hash2");
138 Component project = ReportComponent.builder(Component.Type.PROJECT, 1)
139 .setUuid(PROJECT_UUID)
141 .addChildren(file1, file2)
143 treeRootHolder.setRoot(project);
144 fileStatuses.initialize();
145 assertThat(fileStatuses.isDataUnchanged(file1)).isFalse();
146 assertThat(fileStatuses.isDataUnchanged(file2)).isFalse();
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();
156 addDbFileHash(file1, "hash1");
157 addDbFileHash(file2, "hash2");
158 addDbFileHash(file3, "hash3");
160 addReportFileHash(file1, "hash1");
161 addReportFileHash(file2, "hash2");
162 addReportFileHash(file3, "different");
164 Component project = ReportComponent.builder(Component.Type.PROJECT, 1)
165 .setUuid(PROJECT_UUID)
167 .addChildren(file1, file2, file3)
169 treeRootHolder.setRoot(project);
170 fileStatuses.initialize();
171 assertThat(fileStatuses.isDataUnchanged(file1)).isTrue();
172 assertThat(fileStatuses.isDataUnchanged(file2)).isFalse();
174 verify(previousSourceHashRepository).getDbFile(file1);
178 public void getFileUuidsMarkedAsUnchanged_whenNotInitialized_shouldFail() {
179 assertThatThrownBy(fileStatuses::getFileUuidsMarkedAsUnchanged)
180 .isInstanceOf(IllegalStateException.class)
181 .hasMessage("Not initialized");
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)
196 .addChildren(file1, file2)
198 treeRootHolder.setRoot(project);
199 fileStatuses.initialize();
201 assertThat(fileStatuses.getFileUuidsMarkedAsUnchanged()).contains(file1.getUuid());
204 private void addDbFileHash(Component file, String hash) {
205 FileHashesDto fileHashesDto = new FileHashesDto().setSrcHash(hash);
206 when(previousSourceHashRepository.getDbFile(file)).thenReturn(Optional.of(fileHashesDto));
209 private void addReportFileHash(Component file, String hash) {
210 when(sourceHashRepository.getRawSourceHash(file)).thenReturn(hash);