3 * Copyright (C) 2009-2022 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.mockito.Mockito.mock;
33 import static org.mockito.Mockito.verify;
34 import static org.mockito.Mockito.when;
36 public class FileStatusesImplTest {
37 private static final String PROJECT_KEY = "PROJECT_KEY";
38 private static final String PROJECT_UUID = "UUID-1234";
41 public final TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
42 private final PreviousSourceHashRepository previousSourceHashRepository = mock(PreviousSourceHashRepository.class);
43 private final SourceHashRepository sourceHashRepository = mock(SourceHashRepository.class);
45 public final AnalysisMetadataHolderRule analysisMetadataHolder = new AnalysisMetadataHolderRule();
46 private final FileStatusesImpl fileStatuses = new FileStatusesImpl(analysisMetadataHolder, treeRootHolder, previousSourceHashRepository, sourceHashRepository);
49 public void before() {
50 analysisMetadataHolder.setBaseAnalysis(new Analysis.Builder().setUuid(PROJECT_UUID).setCreatedAt(1000L).build());
54 public void file_is_unchanged_only_if_status_is_SAME_and_hashes_equal() {
55 Component file1 = ReportComponent.builder(Component.Type.FILE, 2, "FILE1_KEY").setStatus(Component.Status.SAME).build();
56 Component file2 = ReportComponent.builder(Component.Type.FILE, 3, "FILE2_KEY").setStatus(Component.Status.SAME).build();
57 Component file3 = ReportComponent.builder(Component.Type.FILE, 4, "FILE3_KEY").setStatus(Component.Status.CHANGED).build();
59 addDbFileHash(file1, "hash1");
60 addDbFileHash(file2, "different");
61 addDbFileHash(file3, "hash3");
63 addReportFileHash(file1, "hash1");
64 addReportFileHash(file2, "hash2");
65 addReportFileHash(file3, "hash3");
67 Component project = ReportComponent.builder(Component.Type.PROJECT, 1)
68 .setUuid(PROJECT_UUID)
70 .addChildren(file1, file2, file3)
72 treeRootHolder.setRoot(project);
73 fileStatuses.initialize();
74 assertThat(fileStatuses.isUnchanged(file1)).isTrue();
75 assertThat(fileStatuses.isUnchanged(file2)).isFalse();
76 assertThat(fileStatuses.isUnchanged(file2)).isFalse();
80 public void isDataUnchanged_returns_false_if_any_SAME_status_is_incorrect() {
81 Component file1 = ReportComponent.builder(Component.Type.FILE, 2, "FILE1_KEY").setStatus(Component.Status.SAME)
82 .setFileAttributes(new FileAttributes(false, null, 10, true)).build();
83 Component file2 = ReportComponent.builder(Component.Type.FILE, 3, "FILE2_KEY").setStatus(Component.Status.SAME).build();
85 addDbFileHash(file1, "hash1");
86 addDbFileHash(file2, "different");
88 addReportFileHash(file1, "hash1");
89 addReportFileHash(file2, "hash2");
91 Component project = ReportComponent.builder(Component.Type.PROJECT, 1)
92 .setUuid(PROJECT_UUID)
94 .addChildren(file1, file2)
96 treeRootHolder.setRoot(project);
97 fileStatuses.initialize();
98 assertThat(fileStatuses.isDataUnchanged(file1)).isFalse();
99 assertThat(fileStatuses.isDataUnchanged(file2)).isFalse();
103 public void isDataUnchanged_returns_false_no_previous_analysis() {
104 analysisMetadataHolder.setBaseAnalysis(null);
106 Component file1 = ReportComponent.builder(Component.Type.FILE, 2, "FILE1_KEY").setStatus(Component.Status.SAME)
107 .setFileAttributes(new FileAttributes(false, null, 10, true)).build();
108 Component file2 = ReportComponent.builder(Component.Type.FILE, 3, "FILE2_KEY").setStatus(Component.Status.SAME).build();
110 addReportFileHash(file1, "hash1");
111 addReportFileHash(file2, "hash2");
113 Component project = ReportComponent.builder(Component.Type.PROJECT, 1)
114 .setUuid(PROJECT_UUID)
116 .addChildren(file1, file2)
118 treeRootHolder.setRoot(project);
119 fileStatuses.initialize();
121 assertThat(fileStatuses.isDataUnchanged(file1)).isFalse();
122 assertThat(fileStatuses.isDataUnchanged(file2)).isFalse();
126 public void isDataUnchanged_returns_false_if_not_set_by_analyzer() {
127 Component file1 = ReportComponent.builder(Component.Type.FILE, 2, "FILE1_KEY").setStatus(Component.Status.SAME)
128 .setFileAttributes(new FileAttributes(false, null, 10, false)).build();
129 Component file2 = ReportComponent.builder(Component.Type.FILE, 3, "FILE2_KEY").setStatus(Component.Status.SAME).build();
131 addDbFileHash(file1, "hash1");
132 addDbFileHash(file2, "hash2");
134 addReportFileHash(file1, "hash1");
135 addReportFileHash(file2, "hash2");
137 Component project = ReportComponent.builder(Component.Type.PROJECT, 1)
138 .setUuid(PROJECT_UUID)
140 .addChildren(file1, file2)
142 treeRootHolder.setRoot(project);
143 fileStatuses.initialize();
144 assertThat(fileStatuses.isDataUnchanged(file1)).isFalse();
145 assertThat(fileStatuses.isDataUnchanged(file2)).isFalse();
149 public void isDataUnchanged_returns_true_if_set_by_analyzer_and_all_SAME_status_are_correct() {
150 Component file1 = ReportComponent.builder(Component.Type.FILE, 2, "FILE1_KEY").setStatus(Component.Status.SAME)
151 .setFileAttributes(new FileAttributes(false, null, 10, true)).build();
152 Component file2 = ReportComponent.builder(Component.Type.FILE, 3, "FILE2_KEY").setStatus(Component.Status.SAME).build();
153 Component file3 = ReportComponent.builder(Component.Type.FILE, 4, "FILE3_KEY").setStatus(Component.Status.CHANGED).build();
155 addDbFileHash(file1, "hash1");
156 addDbFileHash(file2, "hash2");
157 addDbFileHash(file3, "hash3");
159 addReportFileHash(file1, "hash1");
160 addReportFileHash(file2, "hash2");
161 addReportFileHash(file3, "different");
163 Component project = ReportComponent.builder(Component.Type.PROJECT, 1)
164 .setUuid(PROJECT_UUID)
166 .addChildren(file1, file2, file3)
168 treeRootHolder.setRoot(project);
169 fileStatuses.initialize();
170 assertThat(fileStatuses.isDataUnchanged(file1)).isTrue();
171 assertThat(fileStatuses.isDataUnchanged(file2)).isFalse();
173 verify(previousSourceHashRepository).getDbFile(file1);
176 private void addDbFileHash(Component file, String hash) {
177 FileHashesDto fileHashesDto = new FileHashesDto().setSrcHash(hash);
178 when(previousSourceHashRepository.getDbFile(file)).thenReturn(Optional.of(fileHashesDto));
181 private void addReportFileHash(Component file, String hash) {
182 when(sourceHashRepository.getRawSourceHash(file)).thenReturn(hash);