3 * Copyright (C) 2009-2017 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.server.computation.task.projectanalysis.step;
22 import com.tngtech.java.junit.dataprovider.DataProvider;
23 import com.tngtech.java.junit.dataprovider.DataProviderRunner;
24 import com.tngtech.java.junit.dataprovider.UseDataProvider;
25 import java.text.SimpleDateFormat;
26 import org.junit.Before;
27 import org.junit.Rule;
28 import org.junit.Test;
29 import org.junit.runner.RunWith;
30 import org.sonar.api.config.MapSettings;
31 import org.sonar.api.config.Settings;
32 import org.sonar.api.utils.System2;
33 import org.sonar.api.utils.log.LogTester;
34 import org.sonar.api.utils.log.LoggerLevel;
35 import org.sonar.db.DbClient;
36 import org.sonar.db.DbTester;
37 import org.sonar.server.computation.task.projectanalysis.analysis.AnalysisMetadataHolderRule;
38 import org.sonar.server.computation.task.projectanalysis.component.Component;
39 import org.sonar.server.computation.task.projectanalysis.component.ReportComponent;
40 import org.sonar.server.computation.task.projectanalysis.component.SettingsRepository;
41 import org.sonar.server.computation.task.projectanalysis.component.TreeRootHolderRule;
42 import org.sonar.server.computation.task.projectanalysis.component.ViewsComponent;
43 import org.sonar.server.computation.task.projectanalysis.period.Period;
44 import org.sonar.server.computation.task.projectanalysis.period.PeriodHolderImpl;
45 import org.sonar.server.computation.task.step.ComputationStep;
47 import static org.assertj.core.api.Assertions.assertThat;
48 import static org.mockito.Mockito.mock;
49 import static org.mockito.Mockito.when;
50 import static org.sonar.core.config.CorePropertyDefinitions.LEAK_PERIOD_MODE_DATE;
51 import static org.sonar.core.config.CorePropertyDefinitions.LEAK_PERIOD_MODE_DAYS;
52 import static org.sonar.core.config.CorePropertyDefinitions.LEAK_PERIOD_MODE_PREVIOUS_ANALYSIS;
53 import static org.sonar.core.config.CorePropertyDefinitions.LEAK_PERIOD_MODE_PREVIOUS_VERSION;
54 import static org.sonar.core.config.CorePropertyDefinitions.LEAK_PERIOD_MODE_VERSION;
56 @RunWith(DataProviderRunner.class)
57 public class LoadPeriodsStepTest extends BaseStepTest {
59 private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
60 private static final String ROOT_KEY = "ROOT_KEY";
61 private static final ReportComponent PROJECT_ROOT = ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("ABCD").setKey(ROOT_KEY).setVersion("1.1").build();
62 private static final ViewsComponent VIEW_ROOT = ViewsComponent.builder(Component.Type.VIEW, ROOT_KEY).setUuid("ABCD").build();
65 public DbTester dbTester = DbTester.create(System2.INSTANCE);
67 public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
69 public AnalysisMetadataHolderRule analysisMetadataHolder = new AnalysisMetadataHolderRule();
71 public LogTester logTester = new LogTester();
73 private PeriodHolderImpl periodsHolder = new PeriodHolderImpl();
74 private DbClient dbClient = dbTester.getDbClient();
75 private Settings settings = new MapSettings();
76 private SettingsRepository settingsRepository = mock(SettingsRepository.class);
78 private LoadPeriodsStep underTest = new LoadPeriodsStep(dbClient, settingsRepository, treeRootHolder, analysisMetadataHolder, periodsHolder);
81 protected ComputationStep step() {
86 public void setUp() throws Exception {
87 analysisMetadataHolder.setAnalysisDate(DATE_FORMAT.parse("2008-11-30").getTime());
90 private void setupRoot(Component root) {
91 treeRootHolder.setRoot(root);
92 when(settingsRepository.getSettings(root)).thenReturn(settings);
96 public static Object[][] projectAndViewRoots() {
97 return new Object[][] {
104 @UseDataProvider("projectAndViewRoots")
105 public void no_period_on_first_analysis(Component root) {
108 // No project, no snapshot
111 assertThat(periodsHolder.getPeriod()).isNull();
115 @UseDataProvider("projectAndViewRoots")
116 public void feed_one_period(Component root) {
118 dbTester.prepareDbUnit(getClass(), "shared.xml");
119 String textDate = "2008-11-22";
120 settings.setProperty("sonar.leak.period", textDate);
124 Period period = periodsHolder.getPeriod();
125 assertThat(period).isNotNull();
126 assertThat(period.getMode()).isEqualTo(LEAK_PERIOD_MODE_DATE);
127 assertThat(period.getModeParameter()).isEqualTo(textDate);
128 assertThat(period.getSnapshotDate()).isEqualTo(1227358680000L);
129 assertThat(period.getAnalysisUuid()).isEqualTo("u1003");
133 @UseDataProvider("projectAndViewRoots")
134 public void no_period_when_settings_match_no_analysis(Component root) {
136 dbTester.prepareDbUnit(getClass(), "shared.xml");
137 settings.setProperty("sonar.leak.period", "UNKNWOWN VERSION");
141 assertThat(periodsHolder.getPeriod()).isNull();
145 @UseDataProvider("projectAndViewRoots")
146 public void no_period_when_settings_is_empty(Component root) {
148 dbTester.prepareDbUnit(getClass(), "shared.xml");
149 settings.setProperty("sonar.leak.period", "");
153 assertThat(periodsHolder.getPeriod()).isNull();
157 @UseDataProvider("projectAndViewRoots")
158 public void ignore_unprocessed_snapshots(Component root) {
160 dbTester.prepareDbUnit(getClass(), "unprocessed_snapshots.xml");
161 settings.setProperty("sonar.leak.period", "100");
165 assertThat(periodsHolder.getPeriod()).isNull();
169 @UseDataProvider("projectAndViewRoots")
170 public void feed_period_by_date(Component root) {
172 dbTester.prepareDbUnit(getClass(), "shared.xml");
173 String textDate = "2008-11-22";
174 settings.setProperty("sonar.leak.period", textDate);
178 // Return analysis from given date 2008-11-22
179 Period period = periodsHolder.getPeriod();
180 assertThat(period).isNotNull();
181 assertThat(period.getMode()).isEqualTo(LEAK_PERIOD_MODE_DATE);
182 assertThat(period.getModeParameter()).isEqualTo(textDate);
183 assertThat(period.getSnapshotDate()).isEqualTo(1227358680000L);
184 assertThat(period.getAnalysisUuid()).isEqualTo("u1003");
186 assertThat(logTester.logs()).hasSize(1);
187 assertThat(logTester.logs().get(0)).startsWith("Compare to date 2008-11-22 (analysis of ");
191 @UseDataProvider("projectAndViewRoots")
192 public void search_by_date_return_nearest_later_analysis(Component root) {
194 dbTester.prepareDbUnit(getClass(), "shared.xml");
195 String date = "2008-11-24";
196 settings.setProperty("sonar.leak.period", date);
200 // Analysis form 2008-11-29
201 Period period = periodsHolder.getPeriod();
202 assertThat(period).isNotNull();
203 assertThat(period.getMode()).isEqualTo(LEAK_PERIOD_MODE_DATE);
204 assertThat(period.getModeParameter()).isEqualTo(date);
205 assertThat(period.getSnapshotDate()).isEqualTo(1227934800000L);
206 assertThat(period.getAnalysisUuid()).isEqualTo("u1004");
210 @UseDataProvider("projectAndViewRoots")
211 public void no_period_by_date(Component root) {
213 dbTester.prepareDbUnit(getClass(), "shared.xml");
214 // No analysis at and after this date
215 settings.setProperty("sonar.leak.period", "2008-11-30");
219 assertThat(periodsHolder.getPeriod()).isNull();
223 @UseDataProvider("projectAndViewRoots")
224 public void feed_period_by_days(Component root) {
226 dbTester.prepareDbUnit(getClass(), "shared.xml");
227 settings.setProperty("sonar.leak.period", "10");
231 // return analysis from 2008-11-20
232 Period period = periodsHolder.getPeriod();
233 assertThat(period).isNotNull();
234 assertThat(period.getMode()).isEqualTo(LEAK_PERIOD_MODE_DAYS);
235 assertThat(period.getModeParameter()).isEqualTo("10");
236 assertThat(period.getSnapshotDate()).isEqualTo(1227157200000L);
237 assertThat(period.getAnalysisUuid()).isEqualTo("u1002");
239 assertThat(logTester.logs()).hasSize(1);
240 assertThat(logTester.logs().get(0)).startsWith("Compare over 10 days (2008-11-20, analysis of ");
244 @UseDataProvider("projectAndViewRoots")
245 public void no_period_by_days(Component root) {
247 dbTester.prepareDbUnit(getClass(), "empty.xml");
248 settings.setProperty("sonar.leak.period", "0");
252 assertThat(periodsHolder.getPeriod()).isNull();
256 @UseDataProvider("projectAndViewRoots")
257 public void feed_period_by_previous_analysis(Component root) {
259 dbTester.prepareDbUnit(getClass(), "shared.xml");
260 settings.setProperty("sonar.leak.period", "previous_analysis");
264 // return analysis from 2008-11-29
265 Period period = periodsHolder.getPeriod();
266 assertThat(period).isNotNull();
267 assertThat(period.getMode()).isEqualTo(LEAK_PERIOD_MODE_PREVIOUS_ANALYSIS);
268 assertThat(period.getModeParameter()).isNotNull();
269 assertThat(period.getSnapshotDate()).isEqualTo(1227934800000L);
270 assertThat(period.getAnalysisUuid()).isEqualTo("u1004");
272 assertThat(logTester.logs(LoggerLevel.DEBUG)).hasSize(1);
273 assertThat(logTester.logs(LoggerLevel.DEBUG).get(0)).startsWith("Compare to previous analysis (");
277 @UseDataProvider("projectAndViewRoots")
278 public void no_period_by_previous_analysis(Component root) {
280 dbTester.prepareDbUnit(getClass(), "empty.xml");
281 settings.setProperty("sonar.leak.period", "previous_analysis");
285 assertThat(periodsHolder.getPeriod()).isNull();
289 public void display_warning_log_when_using_previous_analysis() {
290 setupRoot(PROJECT_ROOT);
291 dbTester.prepareDbUnit(getClass(), "shared.xml");
292 settings.setProperty("sonar.leak.period", "previous_analysis");
296 assertThat(logTester.logs(LoggerLevel.WARN)).containsOnly("Leak period is set to deprecated value 'previous_analysis'. This value will be removed in next SonarQube LTS, please use another one instead.");
300 public void feed_period_by_previous_version() {
301 setupRoot(PROJECT_ROOT);
302 dbTester.prepareDbUnit(getClass(), "shared.xml");
303 settings.setProperty("sonar.leak.period", "previous_version");
307 // Analysis form 2008-11-12
308 Period period = periodsHolder.getPeriod();
309 assertThat(period).isNotNull();
310 assertThat(period.getMode()).isEqualTo(LEAK_PERIOD_MODE_PREVIOUS_VERSION);
311 assertThat(period.getModeParameter()).isEqualTo("1.0");
312 assertThat(period.getSnapshotDate()).isEqualTo(1226494680000L);
313 assertThat(period.getAnalysisUuid()).isEqualTo("u1001");
315 assertThat(logTester.logs(LoggerLevel.DEBUG)).hasSize(1);
316 assertThat(logTester.logs(LoggerLevel.DEBUG).get(0)).startsWith("Compare to previous version (");
320 public void feed_period_by_previous_version_is_not_supported_for_views() {
321 setupRoot(VIEW_ROOT);
322 dbTester.prepareDbUnit(getClass(), "shared.xml");
323 settings.setProperty("sonar.leak.period", "previous_version");
327 assertThat(periodsHolder.getPeriod()).isNull();
331 public void feed_period_by_previous_version_with_previous_version_deleted() {
332 setupRoot(PROJECT_ROOT);
333 dbTester.prepareDbUnit(getClass(), "previous_version_deleted.xml");
334 settings.setProperty("sonar.leak.period", "previous_version");
338 // Analysis form 2008-11-11
339 Period period = periodsHolder.getPeriod();
340 assertThat(period).isNotNull();
341 assertThat(period.getMode()).isEqualTo(LEAK_PERIOD_MODE_PREVIOUS_VERSION);
342 assertThat(period.getModeParameter()).isEqualTo("0.9");
343 assertThat(period.getSnapshotDate()).isEqualTo(1226379600000L);
344 assertThat(period.getAnalysisUuid()).isEqualTo("u1000");
348 @UseDataProvider("projectAndViewRoots")
349 public void no_period_by_previous_version(Component root) {
351 dbTester.prepareDbUnit(getClass(), "empty.xml");
352 settings.setProperty("sonar.leak.period", "previous_version");
356 assertThat(periodsHolder.getPeriod()).isNull();
360 public void feed_period_by_previous_version_with_first_analysis_when_no_previous_version_found() {
361 setupRoot(PROJECT_ROOT);
362 dbTester.prepareDbUnit(getClass(), "no_previous_version.xml");
363 settings.setProperty("sonar.leak.period", "previous_version");
367 Period period = periodsHolder.getPeriod();
368 assertThat(period).isNotNull();
369 assertThat(period.getMode()).isEqualTo(LEAK_PERIOD_MODE_PREVIOUS_VERSION);
370 assertThat(period.getModeParameter()).isNull();
371 assertThat(period.getSnapshotDate()).isEqualTo(1226379600000L);
372 assertThat(period.getAnalysisUuid()).isEqualTo("u1000");
376 public void feed_period_by_previous_version_with_first_analysis_when_previous_snapshot_is_the_last_one() {
377 setupRoot(PROJECT_ROOT);
378 dbTester.prepareDbUnit(getClass(), "previous_version_is_last_one.xml");
379 settings.setProperty("sonar.leak.period", "previous_version");
383 Period period = periodsHolder.getPeriod();
384 assertThat(period).isNotNull();
385 assertThat(period.getMode()).isEqualTo(LEAK_PERIOD_MODE_PREVIOUS_VERSION);
386 assertThat(period.getModeParameter()).isNull();
387 assertThat(period.getSnapshotDate()).isEqualTo(1226379600000L);
388 assertThat(period.getAnalysisUuid()).isEqualTo("u1000");
392 public void no_period_by_previous_version_when_no_event_version_for_views() {
393 setupRoot(VIEW_ROOT);
394 dbTester.prepareDbUnit(getClass(), "no_previous_version.xml");
395 settings.setProperty("sonar.leak.period", "previous_version");
399 assertThat(periodsHolder.getPeriod()).isNull();
403 @UseDataProvider("projectAndViewRoots")
404 public void feed_period_by_version(Component root) {
406 dbTester.prepareDbUnit(getClass(), "shared.xml");
407 settings.setProperty("sonar.leak.period", "0.9");
411 // Analysis form 2008-11-11
412 Period period = periodsHolder.getPeriod();
413 assertThat(period).isNotNull();
414 assertThat(period.getMode()).isEqualTo(LEAK_PERIOD_MODE_VERSION);
415 assertThat(period.getModeParameter()).isEqualTo("0.9");
416 assertThat(period.getSnapshotDate()).isEqualTo(1226379600000L);
417 assertThat(period.getAnalysisUuid()).isEqualTo("u1000");
419 assertThat(logTester.logs()).hasSize(1);
420 assertThat(logTester.logs().get(0)).startsWith("Compare to version (0.9) (");
424 @UseDataProvider("projectAndViewRoots")
425 public void no_period_by_version(Component root) {
427 dbTester.prepareDbUnit(getClass(), "empty.xml");
428 settings.setProperty("sonar.leak.period", "0.8");
432 assertThat(periodsHolder.getPeriod()).isNull();