]> source.dussan.org Git - sonarqube.git/blob
b3fc362bbb32acd06cf1c4d7f8b5363e97bdd83c
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2017 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.server.computation.task.projectanalysis.step;
21
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;
46
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;
55
56 @RunWith(DataProviderRunner.class)
57 public class LoadPeriodsStepTest extends BaseStepTest {
58
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();
63
64   @Rule
65   public DbTester dbTester = DbTester.create(System2.INSTANCE);
66   @Rule
67   public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
68   @Rule
69   public AnalysisMetadataHolderRule analysisMetadataHolder = new AnalysisMetadataHolderRule();
70   @Rule
71   public LogTester logTester = new LogTester();
72
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);
77
78   private LoadPeriodsStep underTest = new LoadPeriodsStep(dbClient, settingsRepository, treeRootHolder, analysisMetadataHolder, periodsHolder);
79
80   @Override
81   protected ComputationStep step() {
82     return underTest;
83   }
84
85   @Before
86   public void setUp() throws Exception {
87     analysisMetadataHolder.setAnalysisDate(DATE_FORMAT.parse("2008-11-30").getTime());
88   }
89
90   private void setupRoot(Component root) {
91     treeRootHolder.setRoot(root);
92     when(settingsRepository.getSettings(root)).thenReturn(settings);
93   }
94
95   @DataProvider
96   public static Object[][] projectAndViewRoots() {
97     return new Object[][] {
98       {PROJECT_ROOT},
99       {VIEW_ROOT}
100     };
101   }
102
103   @Test
104   @UseDataProvider("projectAndViewRoots")
105   public void no_period_on_first_analysis(Component root) {
106     setupRoot(root);
107
108     // No project, no snapshot
109     underTest.execute();
110
111     assertThat(periodsHolder.getPeriod()).isNull();
112   }
113
114   @Test
115   @UseDataProvider("projectAndViewRoots")
116   public void feed_one_period(Component root) {
117     setupRoot(root);
118     dbTester.prepareDbUnit(getClass(), "shared.xml");
119     String textDate = "2008-11-22";
120     settings.setProperty("sonar.leak.period", textDate);
121
122     underTest.execute();
123
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");
130   }
131
132   @Test
133   @UseDataProvider("projectAndViewRoots")
134   public void no_period_when_settings_match_no_analysis(Component root) {
135     setupRoot(root);
136     dbTester.prepareDbUnit(getClass(), "shared.xml");
137     settings.setProperty("sonar.leak.period", "UNKNWOWN VERSION");
138
139     underTest.execute();
140
141     assertThat(periodsHolder.getPeriod()).isNull();
142   }
143
144   @Test
145   @UseDataProvider("projectAndViewRoots")
146   public void no_period_when_settings_is_empty(Component root) {
147     setupRoot(root);
148     dbTester.prepareDbUnit(getClass(), "shared.xml");
149     settings.setProperty("sonar.leak.period", "");
150
151     underTest.execute();
152
153     assertThat(periodsHolder.getPeriod()).isNull();
154   }
155
156   @Test
157   @UseDataProvider("projectAndViewRoots")
158   public void ignore_unprocessed_snapshots(Component root) {
159     setupRoot(root);
160     dbTester.prepareDbUnit(getClass(), "unprocessed_snapshots.xml");
161     settings.setProperty("sonar.leak.period", "100");
162
163     underTest.execute();
164
165     assertThat(periodsHolder.getPeriod()).isNull();
166   }
167
168   @Test
169   @UseDataProvider("projectAndViewRoots")
170   public void feed_period_by_date(Component root) {
171     setupRoot(root);
172     dbTester.prepareDbUnit(getClass(), "shared.xml");
173     String textDate = "2008-11-22";
174     settings.setProperty("sonar.leak.period", textDate);
175
176     underTest.execute();
177
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");
185
186     assertThat(logTester.logs()).hasSize(1);
187     assertThat(logTester.logs().get(0)).startsWith("Compare to date 2008-11-22 (analysis of ");
188   }
189
190   @Test
191   @UseDataProvider("projectAndViewRoots")
192   public void search_by_date_return_nearest_later_analysis(Component root) {
193     setupRoot(root);
194     dbTester.prepareDbUnit(getClass(), "shared.xml");
195     String date = "2008-11-24";
196     settings.setProperty("sonar.leak.period", date);
197
198     underTest.execute();
199
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");
207   }
208
209   @Test
210   @UseDataProvider("projectAndViewRoots")
211   public void no_period_by_date(Component root) {
212     setupRoot(root);
213     dbTester.prepareDbUnit(getClass(), "shared.xml");
214     // No analysis at and after this date
215     settings.setProperty("sonar.leak.period", "2008-11-30");
216
217     underTest.execute();
218
219     assertThat(periodsHolder.getPeriod()).isNull();
220   }
221
222   @Test
223   @UseDataProvider("projectAndViewRoots")
224   public void feed_period_by_days(Component root) {
225     setupRoot(root);
226     dbTester.prepareDbUnit(getClass(), "shared.xml");
227     settings.setProperty("sonar.leak.period", "10");
228
229     underTest.execute();
230
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");
238
239     assertThat(logTester.logs()).hasSize(1);
240     assertThat(logTester.logs().get(0)).startsWith("Compare over 10 days (2008-11-20, analysis of ");
241   }
242
243   @Test
244   @UseDataProvider("projectAndViewRoots")
245   public void no_period_by_days(Component root) {
246     setupRoot(root);
247     dbTester.prepareDbUnit(getClass(), "empty.xml");
248     settings.setProperty("sonar.leak.period", "0");
249
250     underTest.execute();
251
252     assertThat(periodsHolder.getPeriod()).isNull();
253   }
254
255   @Test
256   @UseDataProvider("projectAndViewRoots")
257   public void feed_period_by_previous_analysis(Component root) {
258     setupRoot(root);
259     dbTester.prepareDbUnit(getClass(), "shared.xml");
260     settings.setProperty("sonar.leak.period", "previous_analysis");
261
262     underTest.execute();
263
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");
271
272     assertThat(logTester.logs(LoggerLevel.DEBUG)).hasSize(1);
273     assertThat(logTester.logs(LoggerLevel.DEBUG).get(0)).startsWith("Compare to previous analysis (");
274   }
275
276   @Test
277   @UseDataProvider("projectAndViewRoots")
278   public void no_period_by_previous_analysis(Component root) {
279     setupRoot(root);
280     dbTester.prepareDbUnit(getClass(), "empty.xml");
281     settings.setProperty("sonar.leak.period", "previous_analysis");
282
283     underTest.execute();
284
285     assertThat(periodsHolder.getPeriod()).isNull();
286   }
287
288   @Test
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");
293
294     underTest.execute();
295
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.");
297   }
298
299   @Test
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");
304
305     underTest.execute();
306
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");
314
315     assertThat(logTester.logs(LoggerLevel.DEBUG)).hasSize(1);
316     assertThat(logTester.logs(LoggerLevel.DEBUG).get(0)).startsWith("Compare to previous version (");
317   }
318
319   @Test
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");
324
325     underTest.execute();
326
327     assertThat(periodsHolder.getPeriod()).isNull();
328   }
329
330   @Test
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");
335
336     underTest.execute();
337
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");
345   }
346
347   @Test
348   @UseDataProvider("projectAndViewRoots")
349   public void no_period_by_previous_version(Component root) {
350     setupRoot(root);
351     dbTester.prepareDbUnit(getClass(), "empty.xml");
352     settings.setProperty("sonar.leak.period", "previous_version");
353
354     underTest.execute();
355
356     assertThat(periodsHolder.getPeriod()).isNull();
357   }
358
359   @Test
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");
364
365     underTest.execute();
366
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");
373   }
374
375   @Test
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");
380
381     underTest.execute();
382
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");
389   }
390
391   @Test
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");
396
397     underTest.execute();
398
399     assertThat(periodsHolder.getPeriod()).isNull();
400   }
401
402   @Test
403   @UseDataProvider("projectAndViewRoots")
404   public void feed_period_by_version(Component root) {
405     setupRoot(root);
406     dbTester.prepareDbUnit(getClass(), "shared.xml");
407     settings.setProperty("sonar.leak.period", "0.9");
408
409     underTest.execute();
410
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");
418
419     assertThat(logTester.logs()).hasSize(1);
420     assertThat(logTester.logs().get(0)).startsWith("Compare to version (0.9) (");
421   }
422
423   @Test
424   @UseDataProvider("projectAndViewRoots")
425   public void no_period_by_version(Component root) {
426     setupRoot(root);
427     dbTester.prepareDbUnit(getClass(), "empty.xml");
428     settings.setProperty("sonar.leak.period", "0.8");
429
430     underTest.execute();
431
432     assertThat(periodsHolder.getPeriod()).isNull();
433   }
434
435 }