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.DataProviderRunner;
23 import java.text.SimpleDateFormat;
24 import org.junit.Before;
25 import org.junit.Rule;
26 import org.junit.Test;
27 import org.junit.rules.ExpectedException;
28 import org.junit.runner.RunWith;
29 import org.sonar.api.config.internal.MapSettings;
30 import org.sonar.api.utils.System2;
31 import org.sonar.api.utils.log.LogTester;
32 import org.sonar.api.utils.log.LoggerLevel;
33 import org.sonar.db.DbClient;
34 import org.sonar.db.DbTester;
35 import org.sonar.server.computation.task.projectanalysis.analysis.AnalysisMetadataHolderRule;
36 import org.sonar.server.computation.task.projectanalysis.component.Component;
37 import org.sonar.server.computation.task.projectanalysis.component.ConfigurationRepository;
38 import org.sonar.server.computation.task.projectanalysis.component.ReportComponent;
39 import org.sonar.server.computation.task.projectanalysis.component.TreeRootHolderRule;
40 import org.sonar.server.computation.task.projectanalysis.component.ViewsComponent;
41 import org.sonar.server.computation.task.projectanalysis.period.Period;
42 import org.sonar.server.computation.task.projectanalysis.period.PeriodHolderImpl;
43 import org.sonar.server.computation.task.step.ComputationStep;
45 import static org.assertj.core.api.Assertions.assertThat;
46 import static org.mockito.Mockito.mock;
47 import static org.mockito.Mockito.when;
48 import static org.sonar.core.config.CorePropertyDefinitions.LEAK_PERIOD_MODE_DATE;
49 import static org.sonar.core.config.CorePropertyDefinitions.LEAK_PERIOD_MODE_DAYS;
50 import static org.sonar.core.config.CorePropertyDefinitions.LEAK_PERIOD_MODE_PREVIOUS_VERSION;
51 import static org.sonar.core.config.CorePropertyDefinitions.LEAK_PERIOD_MODE_VERSION;
53 @RunWith(DataProviderRunner.class)
54 public class LoadPeriodsStepTest extends BaseStepTest {
56 private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
57 private static final String ROOT_KEY = "ROOT_KEY";
58 private static final ReportComponent PROJECT_ROOT = ReportComponent.builder(Component.Type.PROJECT, 1).setUuid("ABCD").setKey(ROOT_KEY).setVersion("1.1").build();
59 private static final ViewsComponent VIEW_ROOT = ViewsComponent.builder(Component.Type.VIEW, ROOT_KEY).setUuid("ABCD").build();
62 public DbTester dbTester = DbTester.create(System2.INSTANCE);
64 public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
66 public AnalysisMetadataHolderRule analysisMetadataHolder = new AnalysisMetadataHolderRule();
68 public LogTester logTester = new LogTester();
70 public ExpectedException expectedException = ExpectedException.none();
72 private PeriodHolderImpl periodsHolder = new PeriodHolderImpl();
73 private DbClient dbClient = dbTester.getDbClient();
74 private MapSettings settings = new MapSettings();
75 private ConfigurationRepository settingsRepository = mock(ConfigurationRepository.class);
77 private LoadPeriodsStep underTest = new LoadPeriodsStep(dbClient, settingsRepository, treeRootHolder, analysisMetadataHolder, periodsHolder);
80 protected ComputationStep step() {
85 public void setUp() throws Exception {
86 analysisMetadataHolder.setAnalysisDate(DATE_FORMAT.parse("2008-11-30").getTime());
89 private void setupRoot(Component root) {
90 treeRootHolder.setRoot(root);
91 when(settingsRepository.getConfiguration()).thenReturn(settings.asConfig());
95 public void no_period_on_first_analysis() {
96 setupRoot(PROJECT_ROOT);
98 // No project, no snapshot
101 assertThat(periodsHolder.getPeriod()).isNull();
105 public void feed_one_period() {
106 setupRoot(PROJECT_ROOT);
107 dbTester.prepareDbUnit(getClass(), "shared.xml");
108 String textDate = "2008-11-22";
109 settings.setProperty("sonar.leak.period", textDate);
113 Period period = periodsHolder.getPeriod();
114 assertThat(period).isNotNull();
115 assertThat(period.getMode()).isEqualTo(LEAK_PERIOD_MODE_DATE);
116 assertThat(period.getModeParameter()).isEqualTo(textDate);
117 assertThat(period.getSnapshotDate()).isEqualTo(1227358680000L);
118 assertThat(period.getAnalysisUuid()).isEqualTo("u1003");
122 public void no_period_when_settings_match_no_analysis() {
123 setupRoot(PROJECT_ROOT);
124 dbTester.prepareDbUnit(getClass(), "shared.xml");
125 settings.setProperty("sonar.leak.period", "UNKNWOWN VERSION");
129 assertThat(periodsHolder.getPeriod()).isNull();
133 public void no_period_when_settings_is_empty() {
134 setupRoot(PROJECT_ROOT);
135 dbTester.prepareDbUnit(getClass(), "shared.xml");
136 settings.setProperty("sonar.leak.period", "");
140 assertThat(periodsHolder.getPeriod()).isNull();
144 public void ignore_unprocessed_snapshots() {
145 setupRoot(PROJECT_ROOT);
146 dbTester.prepareDbUnit(getClass(), "unprocessed_snapshots.xml");
147 settings.setProperty("sonar.leak.period", "100");
151 assertThat(periodsHolder.getPeriod()).isNull();
155 public void feed_period_by_date() {
156 setupRoot(PROJECT_ROOT);
157 dbTester.prepareDbUnit(getClass(), "shared.xml");
158 String textDate = "2008-11-22";
159 settings.setProperty("sonar.leak.period", textDate);
163 // Return analysis from given date 2008-11-22
164 Period period = periodsHolder.getPeriod();
165 assertThat(period).isNotNull();
166 assertThat(period.getMode()).isEqualTo(LEAK_PERIOD_MODE_DATE);
167 assertThat(period.getModeParameter()).isEqualTo(textDate);
168 assertThat(period.getSnapshotDate()).isEqualTo(1227358680000L);
169 assertThat(period.getAnalysisUuid()).isEqualTo("u1003");
171 assertThat(logTester.logs()).hasSize(1);
172 assertThat(logTester.logs().get(0)).startsWith("Compare to date 2008-11-22 (analysis of ");
176 public void search_by_date_return_nearest_later_analysis() {
177 setupRoot(PROJECT_ROOT);
178 dbTester.prepareDbUnit(getClass(), "shared.xml");
179 String date = "2008-11-24";
180 settings.setProperty("sonar.leak.period", date);
184 // Analysis form 2008-11-29
185 Period period = periodsHolder.getPeriod();
186 assertThat(period).isNotNull();
187 assertThat(period.getMode()).isEqualTo(LEAK_PERIOD_MODE_DATE);
188 assertThat(period.getModeParameter()).isEqualTo(date);
189 assertThat(period.getSnapshotDate()).isEqualTo(1227934800000L);
190 assertThat(period.getAnalysisUuid()).isEqualTo("u1004");
194 public void no_period_by_date() {
195 setupRoot(PROJECT_ROOT);
196 dbTester.prepareDbUnit(getClass(), "shared.xml");
197 // No analysis at and after this date
198 settings.setProperty("sonar.leak.period", "2008-11-30");
202 assertThat(periodsHolder.getPeriod()).isNull();
206 public void feed_period_by_days() {
207 setupRoot(PROJECT_ROOT);
208 dbTester.prepareDbUnit(getClass(), "shared.xml");
209 settings.setProperty("sonar.leak.period", "10");
213 // return analysis from 2008-11-20
214 Period period = periodsHolder.getPeriod();
215 assertThat(period).isNotNull();
216 assertThat(period.getMode()).isEqualTo(LEAK_PERIOD_MODE_DAYS);
217 assertThat(period.getModeParameter()).isEqualTo("10");
218 assertThat(period.getSnapshotDate()).isEqualTo(1227157200000L);
219 assertThat(period.getAnalysisUuid()).isEqualTo("u1002");
221 assertThat(logTester.logs()).hasSize(1);
222 assertThat(logTester.logs().get(0)).startsWith("Compare over 10 days (2008-11-20, analysis of ");
226 public void no_period_by_days() {
227 setupRoot(PROJECT_ROOT);
228 dbTester.prepareDbUnit(getClass(), "empty.xml");
229 settings.setProperty("sonar.leak.period", "0");
233 assertThat(periodsHolder.getPeriod()).isNull();
237 public void feed_period_by_previous_version() {
238 setupRoot(PROJECT_ROOT);
239 dbTester.prepareDbUnit(getClass(), "shared.xml");
240 settings.setProperty("sonar.leak.period", "previous_version");
244 // Analysis form 2008-11-12
245 Period period = periodsHolder.getPeriod();
246 assertThat(period).isNotNull();
247 assertThat(period.getMode()).isEqualTo(LEAK_PERIOD_MODE_PREVIOUS_VERSION);
248 assertThat(period.getModeParameter()).isEqualTo("1.0");
249 assertThat(period.getSnapshotDate()).isEqualTo(1226494680000L);
250 assertThat(period.getAnalysisUuid()).isEqualTo("u1001");
252 assertThat(logTester.logs(LoggerLevel.DEBUG)).hasSize(1);
253 assertThat(logTester.logs(LoggerLevel.DEBUG).get(0)).startsWith("Compare to previous version (");
257 public void feed_period_by_previous_version_with_previous_version_deleted() {
258 setupRoot(PROJECT_ROOT);
259 dbTester.prepareDbUnit(getClass(), "previous_version_deleted.xml");
260 settings.setProperty("sonar.leak.period", "previous_version");
264 // Analysis form 2008-11-11
265 Period period = periodsHolder.getPeriod();
266 assertThat(period).isNotNull();
267 assertThat(period.getMode()).isEqualTo(LEAK_PERIOD_MODE_PREVIOUS_VERSION);
268 assertThat(period.getModeParameter()).isEqualTo("0.9");
269 assertThat(period.getSnapshotDate()).isEqualTo(1226379600000L);
270 assertThat(period.getAnalysisUuid()).isEqualTo("u1000");
274 public void no_period_by_previous_version() {
275 setupRoot(PROJECT_ROOT);
276 dbTester.prepareDbUnit(getClass(), "empty.xml");
277 settings.setProperty("sonar.leak.period", "previous_version");
281 assertThat(periodsHolder.getPeriod()).isNull();
285 public void feed_period_by_previous_version_with_first_analysis_when_no_previous_version_found() {
286 setupRoot(PROJECT_ROOT);
287 dbTester.prepareDbUnit(getClass(), "no_previous_version.xml");
288 settings.setProperty("sonar.leak.period", "previous_version");
292 Period period = periodsHolder.getPeriod();
293 assertThat(period).isNotNull();
294 assertThat(period.getMode()).isEqualTo(LEAK_PERIOD_MODE_PREVIOUS_VERSION);
295 assertThat(period.getModeParameter()).isNull();
296 assertThat(period.getSnapshotDate()).isEqualTo(1226379600000L);
297 assertThat(period.getAnalysisUuid()).isEqualTo("u1000");
301 public void feed_period_by_previous_version_with_first_analysis_when_previous_snapshot_is_the_last_one() {
302 setupRoot(PROJECT_ROOT);
303 dbTester.prepareDbUnit(getClass(), "previous_version_is_last_one.xml");
304 settings.setProperty("sonar.leak.period", "previous_version");
308 Period period = periodsHolder.getPeriod();
309 assertThat(period).isNotNull();
310 assertThat(period.getMode()).isEqualTo(LEAK_PERIOD_MODE_PREVIOUS_VERSION);
311 assertThat(period.getModeParameter()).isNull();
312 assertThat(period.getSnapshotDate()).isEqualTo(1226379600000L);
313 assertThat(period.getAnalysisUuid()).isEqualTo("u1000");
317 public void feed_period_by_version() {
318 setupRoot(PROJECT_ROOT);
319 dbTester.prepareDbUnit(getClass(), "shared.xml");
320 settings.setProperty("sonar.leak.period", "0.9");
324 // Analysis form 2008-11-11
325 Period period = periodsHolder.getPeriod();
326 assertThat(period).isNotNull();
327 assertThat(period.getMode()).isEqualTo(LEAK_PERIOD_MODE_VERSION);
328 assertThat(period.getModeParameter()).isEqualTo("0.9");
329 assertThat(period.getSnapshotDate()).isEqualTo(1226379600000L);
330 assertThat(period.getAnalysisUuid()).isEqualTo("u1000");
332 assertThat(logTester.logs()).hasSize(1);
333 assertThat(logTester.logs().get(0)).startsWith("Compare to version (0.9) (");
337 public void no_period_by_version() {
338 setupRoot(PROJECT_ROOT);
339 dbTester.prepareDbUnit(getClass(), "empty.xml");
340 settings.setProperty("sonar.leak.period", "0.8");
344 assertThat(periodsHolder.getPeriod()).isNull();