From: Julien Lancelot Date: Tue, 7 Feb 2017 13:14:59 +0000 (+0100) Subject: SONAR-8743 Handle only leak period when loading period X-Git-Tag: 6.3-RC1~91 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=4802a893a7b5dc23a0a2784df78d4d7b4e372ef5;p=sonarqube.git SONAR-8743 Handle only leak period when loading period --- diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/LoadPeriodsStep.java b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/LoadPeriodsStep.java index c404f6763d3..9ef1217fff1 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/LoadPeriodsStep.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/LoadPeriodsStep.java @@ -20,11 +20,8 @@ package org.sonar.server.computation.task.projectanalysis.step; import com.google.common.base.Optional; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; +import javax.annotation.CheckForNull; import org.sonar.api.config.Settings; -import org.sonar.api.resources.Qualifiers; import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.component.ComponentDto; @@ -53,8 +50,6 @@ import static org.sonar.server.computation.task.projectanalysis.component.Crawle */ public class LoadPeriodsStep implements ComputationStep { - private static final int NUMBER_OF_PERIODS = 5; - private final DbClient dbClient; private final SettingsRepository settingsRepository; private final TreeRootHolder treeRootHolder; @@ -89,34 +84,31 @@ public class LoadPeriodsStep implements ComputationStep { public void execute(Component projectOrView) { DbSession session = dbClient.openSession(false); try { - periodsHolder.setPeriods(buildPeriods(projectOrView, session)); + periodsHolder.setPeriod(buildPeriod(projectOrView, session)); } finally { dbClient.closeSession(session); } } - private List buildPeriods(Component projectOrView, DbSession session) { + @CheckForNull + private Period buildPeriod(Component projectOrView, DbSession session) { Optional projectDto = dbClient.componentDao().selectByKey(session, projectOrView.getKey()); // No project on first analysis, no period if (!projectDto.isPresent()) { - return Collections.emptyList(); + return null; } boolean isReportType = projectOrView.getType().isReportType(); PeriodResolver periodResolver = new PeriodResolver(dbClient, session, projectDto.get().uuid(), analysisMetadataHolder.getAnalysisDate(), - isReportType ? projectOrView.getReportAttributes().getVersion() : null, - isReportType ? Qualifiers.PROJECT : Qualifiers.VIEW); + isReportType ? projectOrView.getReportAttributes().getVersion() : null); Settings settings = settingsRepository.getSettings(projectOrView); - List periods = new ArrayList<>(5); - for (int index = 1; index <= NUMBER_OF_PERIODS; index++) { - Period period = periodResolver.resolve(index, settings); - // SONAR-4700 Add a past snapshot only if it exists - if (period != null) { - periods.add(period); - } + Period period = periodResolver.resolve(settings); + // SONAR-4700 Add a past snapshot only if it exists + if (period != null) { + return period; } - return periods; + return null; } @Override diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/PeriodResolver.java b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/PeriodResolver.java index 99cda50ab7c..6ec616bcc99 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/PeriodResolver.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/PeriodResolver.java @@ -19,7 +19,6 @@ */ package org.sonar.server.computation.task.projectanalysis.step; -import com.google.common.base.Strings; import java.time.temporal.ChronoUnit; import java.util.Date; import java.util.List; @@ -56,60 +55,58 @@ public class PeriodResolver { private final long analysisDate; @CheckForNull private final String currentVersion; - private final String qualifier; - public PeriodResolver(DbClient dbClient, DbSession session, String projectUuid, long analysisDate, @Nullable String currentVersion, String qualifier) { + public PeriodResolver(DbClient dbClient, DbSession session, String projectUuid, long analysisDate, @Nullable String currentVersion) { this.dbClient = dbClient; this.session = session; this.projectUuid = projectUuid; this.analysisDate = analysisDate; this.currentVersion = currentVersion; - this.qualifier = qualifier; } @CheckForNull - public Period resolve(int index, Settings settings) { - String propertyValue = getPropertyValue(qualifier, settings, index); + public Period resolve(Settings settings) { + String propertyValue = getPropertyValue(settings); if (StringUtils.isBlank(propertyValue)) { return null; } - Period period = resolve(index, propertyValue); + Period period = resolve(propertyValue); if (period == null && StringUtils.isNotBlank(propertyValue)) { - LOG.debug("Property " + TIMEMACHINE_PERIOD_PREFIX + index + " is not valid: " + propertyValue); + LOG.debug("Property " + TIMEMACHINE_PERIOD_PREFIX + 1 + " is not valid: " + propertyValue); } return period; } @CheckForNull - private Period resolve(int index, String property) { + private Period resolve(String property) { Integer days = tryToResolveByDays(property); if (days != null) { - return findByDays(index, days); + return findByDays(days); } Date date = DateUtils.parseDateQuietly(property); if (date != null) { - return findByDate(index, date); + return findByDate(date); } if (StringUtils.equals(TIMEMACHINE_MODE_PREVIOUS_ANALYSIS, property)) { - return findByPreviousAnalysis(index); + return findByPreviousAnalysis(); } if (StringUtils.equals(TIMEMACHINE_MODE_PREVIOUS_VERSION, property)) { - return findByPreviousVersion(index); + return findByPreviousVersion(); } - return findByVersion(index, property); + return findByVersion(property); } - private Period findByDate(int index, Date date) { + private Period findByDate(Date date) { SnapshotDto snapshot = findFirstSnapshot(session, createCommonQuery(projectUuid).setCreatedAfter(date.getTime()).setSort(BY_DATE, ASC)); if (snapshot == null) { return null; } LOG.debug("Compare to date {} (analysis of {})", formatDate(date.getTime()), formatDate(snapshot.getCreatedAt())); - return new Period(index, TIMEMACHINE_MODE_DATE, DateUtils.formatDate(date), snapshot.getCreatedAt(), snapshot.getUuid()); + return new Period(TIMEMACHINE_MODE_DATE, DateUtils.formatDate(date), snapshot.getCreatedAt(), snapshot.getUuid()); } @CheckForNull - private Period findByDays(int index, int days) { + private Period findByDays(int days) { List snapshots = dbClient.snapshotDao().selectAnalysesByQuery(session, createCommonQuery(projectUuid).setCreatedBefore(analysisDate).setSort(BY_DATE, ASC)); long targetDate = DateUtils.addDays(new Date(analysisDate), -days).getTime(); SnapshotDto snapshot = findNearestSnapshotToTargetDate(snapshots, targetDate); @@ -117,52 +114,52 @@ public class PeriodResolver { return null; } LOG.debug("Compare over {} days ({}, analysis of {})", String.valueOf(days), formatDate(targetDate), formatDate(snapshot.getCreatedAt())); - return new Period(index, TIMEMACHINE_MODE_DAYS, String.valueOf(days), snapshot.getCreatedAt(), snapshot.getUuid()); + return new Period(TIMEMACHINE_MODE_DAYS, String.valueOf(days), snapshot.getCreatedAt(), snapshot.getUuid()); } @CheckForNull - private Period findByPreviousAnalysis(int index) { + private Period findByPreviousAnalysis() { SnapshotDto snapshot = findFirstSnapshot(session, createCommonQuery(projectUuid).setCreatedBefore(analysisDate).setIsLast(true).setSort(BY_DATE, DESC)); if (snapshot == null) { return null; } LOG.debug("Compare to previous analysis ({})", formatDate(snapshot.getCreatedAt())); - return new Period(index, TIMEMACHINE_MODE_PREVIOUS_ANALYSIS, formatDate(snapshot.getCreatedAt()), snapshot.getCreatedAt(), snapshot.getUuid()); + return new Period(TIMEMACHINE_MODE_PREVIOUS_ANALYSIS, formatDate(snapshot.getCreatedAt()), snapshot.getCreatedAt(), snapshot.getUuid()); } @CheckForNull - private Period findByPreviousVersion(int index) { + private Period findByPreviousVersion() { if (currentVersion == null) { return null; } List snapshotDtos = dbClient.snapshotDao().selectPreviousVersionSnapshots(session, projectUuid, currentVersion); if (snapshotDtos.isEmpty()) { // If no previous version is found, the first analysis is returned - return findByFirstAnalysis(index); + return findByFirstAnalysis(); } SnapshotDto snapshotDto = snapshotDtos.get(0); LOG.debug("Compare to previous version ({})", formatDate(snapshotDto.getCreatedAt())); - return new Period(index, TIMEMACHINE_MODE_PREVIOUS_VERSION, snapshotDto.getVersion(), snapshotDto.getCreatedAt(), snapshotDto.getUuid()); + return new Period(TIMEMACHINE_MODE_PREVIOUS_VERSION, snapshotDto.getVersion(), snapshotDto.getCreatedAt(), snapshotDto.getUuid()); } @CheckForNull - private Period findByFirstAnalysis(int index) { + private Period findByFirstAnalysis() { SnapshotDto snapshotDto = dbClient.snapshotDao().selectOldestSnapshot(session, projectUuid); if (snapshotDto == null) { return null; } LOG.debug("Compare to first analysis ({})", formatDate(snapshotDto.getCreatedAt())); - return new Period(index, TIMEMACHINE_MODE_PREVIOUS_VERSION, null, snapshotDto.getCreatedAt(), snapshotDto.getUuid()); + return new Period(TIMEMACHINE_MODE_PREVIOUS_VERSION, null, snapshotDto.getCreatedAt(), snapshotDto.getUuid()); } @CheckForNull - private Period findByVersion(int index, String version) { + private Period findByVersion(String version) { SnapshotDto snapshot = findFirstSnapshot(session, createCommonQuery(projectUuid).setVersion(version).setSort(BY_DATE, DESC)); if (snapshot == null) { return null; } LOG.debug("Compare to version ({}) ({})", version, formatDate(snapshot.getCreatedAt())); - return new Period(index, TIMEMACHINE_MODE_VERSION, version, snapshot.getCreatedAt(), snapshot.getUuid()); + return new Period(TIMEMACHINE_MODE_VERSION, version, snapshot.getCreatedAt(), snapshot.getUuid()); } @CheckForNull @@ -206,12 +203,7 @@ public class PeriodResolver { return DateUtils.formatDate(Date.from(new Date(date).toInstant().truncatedTo(ChronoUnit.SECONDS))); } - private static String getPropertyValue(@Nullable String qualifier, Settings settings, int index) { - String value = settings.getString(TIMEMACHINE_PERIOD_PREFIX + index); - // For periods 4 and 5 we're also searching for a property prefixed by the qualifier - if (index > 3 && Strings.isNullOrEmpty(value)) { - value = settings.getString(TIMEMACHINE_PERIOD_PREFIX + index + "." + qualifier); - } - return value; + private static String getPropertyValue(Settings settings) { + return settings.getString(TIMEMACHINE_PERIOD_PREFIX + 1); } } diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/LoadPeriodsStepTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/LoadPeriodsStepTest.java index 3b72acbd7dc..efc556e1095 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/LoadPeriodsStepTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/LoadPeriodsStepTest.java @@ -23,13 +23,12 @@ import com.tngtech.java.junit.dataprovider.DataProvider; import com.tngtech.java.junit.dataprovider.DataProviderRunner; import com.tngtech.java.junit.dataprovider.UseDataProvider; import java.text.SimpleDateFormat; -import java.util.List; import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; -import org.sonar.api.config.Settings; import org.sonar.api.config.MapSettings; +import org.sonar.api.config.Settings; import org.sonar.api.utils.System2; import org.sonar.api.utils.log.LogTester; import org.sonar.db.DbClient; @@ -53,7 +52,6 @@ import static org.sonar.core.config.CorePropertyDefinitions.TIMEMACHINE_MODE_PRE import static org.sonar.core.config.CorePropertyDefinitions.TIMEMACHINE_MODE_PREVIOUS_VERSION; import static org.sonar.core.config.CorePropertyDefinitions.TIMEMACHINE_MODE_VERSION; - @RunWith(DataProviderRunner.class) public class LoadPeriodsStepTest extends BaseStepTest { @@ -71,12 +69,12 @@ public class LoadPeriodsStepTest extends BaseStepTest { @Rule public LogTester logTester = new LogTester(); - PeriodsHolderImpl periodsHolder = new PeriodsHolderImpl(); - DbClient dbClient = dbTester.getDbClient(); - Settings settings = new MapSettings(); - SettingsRepository settingsRepository = mock(SettingsRepository.class); + private PeriodsHolderImpl periodsHolder = new PeriodsHolderImpl(); + private DbClient dbClient = dbTester.getDbClient(); + private Settings settings = new MapSettings(); + private SettingsRepository settingsRepository = mock(SettingsRepository.class); - LoadPeriodsStep underTest; + private LoadPeriodsStep underTest = new LoadPeriodsStep(dbClient, settingsRepository, treeRootHolder, analysisMetadataHolder, periodsHolder); @Override protected ComputationStep step() { @@ -86,8 +84,6 @@ public class LoadPeriodsStepTest extends BaseStepTest { @Before public void setUp() throws Exception { analysisMetadataHolder.setAnalysisDate(DATE_FORMAT.parse("2008-11-30").getTime()); - - underTest = new LoadPeriodsStep(dbClient, settingsRepository, treeRootHolder, analysisMetadataHolder, periodsHolder); } private void setupRoot(Component root) { @@ -109,26 +105,23 @@ public class LoadPeriodsStepTest extends BaseStepTest { setupRoot(root); // No project, no snapshot - underTest.execute(); - assertThat(periodsHolder.getPeriods()).isEmpty(); + + assertThat(periodsHolder.getPeriod()).isNull(); } @Test @UseDataProvider("projectAndViewRoots") public void feed_one_period(Component root) { setupRoot(root); - dbTester.prepareDbUnit(getClass(), "shared.xml"); - String textDate = "2008-11-22"; settings.setProperty("sonar.timemachine.period1", textDate); underTest.execute(); - List periods = periodsHolder.getPeriods(); - assertThat(periods).hasSize(1); - Period period = periods.get(0); + Period period = periodsHolder.getPeriod(); + assertThat(period).isNotNull(); assertThat(period.getMode()).isEqualTo(TIMEMACHINE_MODE_DATE); assertThat(period.getModeParameter()).isEqualTo(textDate); assertThat(period.getSnapshotDate()).isEqualTo(1227358680000L); @@ -139,57 +132,51 @@ public class LoadPeriodsStepTest extends BaseStepTest { @UseDataProvider("projectAndViewRoots") public void no_period_when_settings_match_no_analysis(Component root) { setupRoot(root); - dbTester.prepareDbUnit(getClass(), "shared.xml"); - settings.setProperty("sonar.timemachine.period1", "UNKNWOWN VERSION"); underTest.execute(); - assertThat(periodsHolder.getPeriods()).isEmpty(); + + assertThat(periodsHolder.getPeriod()).isNull(); } @Test @UseDataProvider("projectAndViewRoots") public void no_period_when_settings_is_empty(Component root) { setupRoot(root); - dbTester.prepareDbUnit(getClass(), "shared.xml"); - settings.setProperty("sonar.timemachine.period1", ""); underTest.execute(); - assertThat(periodsHolder.getPeriods()).isEmpty(); + + assertThat(periodsHolder.getPeriod()).isNull(); } @Test @UseDataProvider("projectAndViewRoots") public void ignore_unprocessed_snapshots(Component root) { setupRoot(root); - dbTester.prepareDbUnit(getClass(), "unprocessed_snapshots.xml"); - settings.setProperty("sonar.timemachine.period1", "100"); underTest.execute(); - assertThat(periodsHolder.getPeriods()).isEmpty(); + + assertThat(periodsHolder.getPeriod()).isNull(); } @Test @UseDataProvider("projectAndViewRoots") public void feed_period_by_date(Component root) { setupRoot(root); - dbTester.prepareDbUnit(getClass(), "shared.xml"); - String textDate = "2008-11-22"; settings.setProperty("sonar.timemachine.period1", textDate); underTest.execute(); - List periods = periodsHolder.getPeriods(); - assertThat(periods).hasSize(1); - Period period = periods.get(0); // Return analysis from given date 2008-11-22 + Period period = periodsHolder.getPeriod(); + assertThat(period).isNotNull(); assertThat(period.getMode()).isEqualTo(TIMEMACHINE_MODE_DATE); assertThat(period.getModeParameter()).isEqualTo(textDate); assertThat(period.getSnapshotDate()).isEqualTo(1227358680000L); @@ -203,18 +190,15 @@ public class LoadPeriodsStepTest extends BaseStepTest { @UseDataProvider("projectAndViewRoots") public void search_by_date_return_nearest_later_analysis(Component root) { setupRoot(root); - dbTester.prepareDbUnit(getClass(), "shared.xml"); String date = "2008-11-24"; - settings.setProperty("sonar.timemachine.period1", date); underTest.execute(); - List periods = periodsHolder.getPeriods(); - assertThat(periods).hasSize(1); // Analysis form 2008-11-29 - Period period = periods.get(0); + Period period = periodsHolder.getPeriod(); + assertThat(period).isNotNull(); assertThat(period.getMode()).isEqualTo(TIMEMACHINE_MODE_DATE); assertThat(period.getModeParameter()).isEqualTo(date); assertThat(period.getSnapshotDate()).isEqualTo(1227934800000L); @@ -225,31 +209,27 @@ public class LoadPeriodsStepTest extends BaseStepTest { @UseDataProvider("projectAndViewRoots") public void no_period_by_date(Component root) { setupRoot(root); - dbTester.prepareDbUnit(getClass(), "shared.xml"); - // No analysis at and after this date settings.setProperty("sonar.timemachine.period1", "2008-11-30"); underTest.execute(); - assertThat(periodsHolder.getPeriods()).isEmpty(); + + assertThat(periodsHolder.getPeriod()).isNull(); } @Test @UseDataProvider("projectAndViewRoots") public void feed_period_by_days(Component root) { setupRoot(root); - dbTester.prepareDbUnit(getClass(), "shared.xml"); - settings.setProperty("sonar.timemachine.period1", "10"); underTest.execute(); - List periods = periodsHolder.getPeriods(); - assertThat(periods).hasSize(1); // return analysis from 2008-11-20 - Period period = periods.get(0); + Period period = periodsHolder.getPeriod(); + assertThat(period).isNotNull(); assertThat(period.getMode()).isEqualTo(TIMEMACHINE_MODE_DAYS); assertThat(period.getModeParameter()).isEqualTo("10"); assertThat(period.getSnapshotDate()).isEqualTo(1227157200000L); @@ -263,30 +243,26 @@ public class LoadPeriodsStepTest extends BaseStepTest { @UseDataProvider("projectAndViewRoots") public void no_period_by_days(Component root) { setupRoot(root); - dbTester.prepareDbUnit(getClass(), "empty.xml"); - settings.setProperty("sonar.timemachine.period1", "0"); underTest.execute(); - assertThat(periodsHolder.getPeriods()).isEmpty(); + + assertThat(periodsHolder.getPeriod()).isNull(); } @Test @UseDataProvider("projectAndViewRoots") public void feed_period_by_previous_analysis(Component root) { setupRoot(root); - dbTester.prepareDbUnit(getClass(), "shared.xml"); - settings.setProperty("sonar.timemachine.period1", "previous_analysis"); underTest.execute(); - List periods = periodsHolder.getPeriods(); - assertThat(periods).hasSize(1); // return analysis from 2008-11-29 - Period period = periods.get(0); + Period period = periodsHolder.getPeriod(); + assertThat(period).isNotNull(); assertThat(period.getMode()).isEqualTo(TIMEMACHINE_MODE_PREVIOUS_ANALYSIS); assertThat(period.getModeParameter()).isNotNull(); assertThat(period.getSnapshotDate()).isEqualTo(1227934800000L); @@ -300,29 +276,25 @@ public class LoadPeriodsStepTest extends BaseStepTest { @UseDataProvider("projectAndViewRoots") public void no_period_by_previous_analysis(Component root) { setupRoot(root); - dbTester.prepareDbUnit(getClass(), "empty.xml"); - settings.setProperty("sonar.timemachine.period1", "previous_analysis"); underTest.execute(); - assertThat(periodsHolder.getPeriods()).isEmpty(); + + assertThat(periodsHolder.getPeriod()).isNull(); } @Test public void feed_period_by_previous_version() { setupRoot(PROJECT_ROOT); - dbTester.prepareDbUnit(getClass(), "shared.xml"); - settings.setProperty("sonar.timemachine.period1", "previous_version"); underTest.execute(); - List periods = periodsHolder.getPeriods(); - assertThat(periods).hasSize(1); // Analysis form 2008-11-12 - Period period = periods.get(0); + Period period = periodsHolder.getPeriod(); + assertThat(period).isNotNull(); assertThat(period.getMode()).isEqualTo(TIMEMACHINE_MODE_PREVIOUS_VERSION); assertThat(period.getModeParameter()).isEqualTo("1.0"); assertThat(period.getSnapshotDate()).isEqualTo(1226494680000L); @@ -335,30 +307,25 @@ public class LoadPeriodsStepTest extends BaseStepTest { @Test public void feed_period_by_previous_version_is_not_supported_for_views() { setupRoot(VIEW_ROOT); - dbTester.prepareDbUnit(getClass(), "shared.xml"); - settings.setProperty("sonar.timemachine.period1", "previous_version"); underTest.execute(); - List periods = periodsHolder.getPeriods(); - assertThat(periods).hasSize(0); + + assertThat(periodsHolder.getPeriod()).isNull(); } @Test public void feed_period_by_previous_version_with_previous_version_deleted() { setupRoot(PROJECT_ROOT); - dbTester.prepareDbUnit(getClass(), "previous_version_deleted.xml"); - settings.setProperty("sonar.timemachine.period1", "previous_version"); underTest.execute(); - List periods = periodsHolder.getPeriods(); - assertThat(periods).hasSize(1); // Analysis form 2008-11-11 - Period period = periods.get(0); + Period period = periodsHolder.getPeriod(); + assertThat(period).isNotNull(); assertThat(period.getMode()).isEqualTo(TIMEMACHINE_MODE_PREVIOUS_VERSION); assertThat(period.getModeParameter()).isEqualTo("0.9"); assertThat(period.getSnapshotDate()).isEqualTo(1226379600000L); @@ -369,28 +336,24 @@ public class LoadPeriodsStepTest extends BaseStepTest { @UseDataProvider("projectAndViewRoots") public void no_period_by_previous_version(Component root) { setupRoot(root); - dbTester.prepareDbUnit(getClass(), "empty.xml"); - settings.setProperty("sonar.timemachine.period1", "previous_version"); underTest.execute(); - assertThat(periodsHolder.getPeriods()).isEmpty(); + + assertThat(periodsHolder.getPeriod()).isNull(); } @Test public void feed_period_by_previous_version_with_first_analysis_when_no_previous_version_found() { setupRoot(PROJECT_ROOT); - dbTester.prepareDbUnit(getClass(), "no_previous_version.xml"); - settings.setProperty("sonar.timemachine.period1", "previous_version"); underTest.execute(); - List periods = periodsHolder.getPeriods(); - assertThat(periods).hasSize(1); - Period period = periods.get(0); + Period period = periodsHolder.getPeriod(); + assertThat(period).isNotNull(); assertThat(period.getMode()).isEqualTo(TIMEMACHINE_MODE_PREVIOUS_VERSION); assertThat(period.getModeParameter()).isNull(); assertThat(period.getSnapshotDate()).isEqualTo(1226379600000L); @@ -400,16 +363,13 @@ public class LoadPeriodsStepTest extends BaseStepTest { @Test public void feed_period_by_previous_version_with_first_analysis_when_previous_snapshot_is_the_last_one() { setupRoot(PROJECT_ROOT); - dbTester.prepareDbUnit(getClass(), "previous_version_is_last_one.xml"); - settings.setProperty("sonar.timemachine.period1", "previous_version"); underTest.execute(); - List periods = periodsHolder.getPeriods(); - assertThat(periods).hasSize(1); - Period period = periods.get(0); + Period period = periodsHolder.getPeriod(); + assertThat(period).isNotNull(); assertThat(period.getMode()).isEqualTo(TIMEMACHINE_MODE_PREVIOUS_VERSION); assertThat(period.getModeParameter()).isNull(); assertThat(period.getSnapshotDate()).isEqualTo(1226379600000L); @@ -419,30 +379,26 @@ public class LoadPeriodsStepTest extends BaseStepTest { @Test public void no_period_by_previous_version_when_no_event_version_for_views() { setupRoot(VIEW_ROOT); - dbTester.prepareDbUnit(getClass(), "no_previous_version.xml"); - settings.setProperty("sonar.timemachine.period1", "previous_version"); underTest.execute(); - assertThat(periodsHolder.getPeriods()).isEmpty(); + + assertThat(periodsHolder.getPeriod()).isNull(); } @Test @UseDataProvider("projectAndViewRoots") public void feed_period_by_version(Component root) { setupRoot(root); - dbTester.prepareDbUnit(getClass(), "shared.xml"); - settings.setProperty("sonar.timemachine.period1", "0.9"); underTest.execute(); - List periods = periodsHolder.getPeriods(); - assertThat(periods).hasSize(1); // Analysis form 2008-11-11 - Period period = periods.get(0); + Period period = periodsHolder.getPeriod(); + assertThat(period).isNotNull(); assertThat(period.getMode()).isEqualTo(TIMEMACHINE_MODE_VERSION); assertThat(period.getModeParameter()).isEqualTo("0.9"); assertThat(period.getSnapshotDate()).isEqualTo(1226379600000L); @@ -456,115 +412,12 @@ public class LoadPeriodsStepTest extends BaseStepTest { @UseDataProvider("projectAndViewRoots") public void no_period_by_version(Component root) { setupRoot(root); - dbTester.prepareDbUnit(getClass(), "empty.xml"); - settings.setProperty("sonar.timemachine.period1", "0.8"); underTest.execute(); - assertThat(periodsHolder.getPeriods()).isEmpty(); - } - - @Test - public void all_five_types_of_periods_are_supported_for_PROJECT_component_tree() { - setupRoot(PROJECT_ROOT); - - dbTester.prepareDbUnit(getClass(), "shared.xml"); - - settings.setProperty("sonar.timemachine.period1", "2008-11-22"); // Analysis from 2008-11-22 should be returned - settings.setProperty("sonar.timemachine.period2", "10"); // Analysis from 2008-11-20 should be returned - settings.setProperty("sonar.timemachine.period3", "previous_analysis"); // Analysis from 2008-11-29 should be returned - settings.setProperty("sonar.timemachine.period4", "previous_version"); // Analysis from 2008-11-12 should be returned - settings.setProperty("sonar.timemachine.period5", "0.9"); // Analysis from 2008-11-11 - - underTest.execute(); - List periods = periodsHolder.getPeriods(); - - assertThat(periods).extracting("mode").containsExactly(TIMEMACHINE_MODE_DATE, TIMEMACHINE_MODE_DAYS, - TIMEMACHINE_MODE_PREVIOUS_ANALYSIS, TIMEMACHINE_MODE_PREVIOUS_VERSION, - TIMEMACHINE_MODE_VERSION); - - assertThat(periods.get(0).getMode()).isEqualTo(TIMEMACHINE_MODE_DATE); - assertThat(periods.get(0).getIndex()).isEqualTo(1); - assertThat(periods.get(0).getSnapshotDate()).isEqualTo(1227358680000L); - - assertThat(periods.get(1).getMode()).isEqualTo(TIMEMACHINE_MODE_DAYS); - assertThat(periods.get(1).getIndex()).isEqualTo(2); - assertThat(periods.get(1).getSnapshotDate()).isEqualTo(1227157200000L); - - assertThat(periods.get(2).getMode()).isEqualTo(TIMEMACHINE_MODE_PREVIOUS_ANALYSIS); - assertThat(periods.get(2).getIndex()).isEqualTo(3); - assertThat(periods.get(2).getSnapshotDate()).isEqualTo(1227934800000L); - - assertThat(periods.get(3).getMode()).isEqualTo(TIMEMACHINE_MODE_PREVIOUS_VERSION); - assertThat(periods.get(3).getIndex()).isEqualTo(4); - assertThat(periods.get(3).getSnapshotDate()).isEqualTo(1226494680000L); - - assertThat(periods.get(4).getMode()).isEqualTo(TIMEMACHINE_MODE_VERSION); - assertThat(periods.get(4).getIndex()).isEqualTo(5); - assertThat(periods.get(4).getSnapshotDate()).isEqualTo(1226379600000L); - } - - @Test - public void feed_four_different_periods() { - setupRoot(VIEW_ROOT); - - dbTester.prepareDbUnit(getClass(), "shared.xml"); - - settings.setProperty("sonar.timemachine.period1", "2008-11-22"); // Analysis from 2008-11-22 should be returned - settings.setProperty("sonar.timemachine.period2", "10"); // Analysis from 2008-11-20 should be returned - settings.setProperty("sonar.timemachine.period3", "previous_analysis"); // Analysis from 2008-11-29 should be returned - settings.setProperty("sonar.timemachine.period4", "previous_version"); // Analysis from 2008-11-12 should be returned - settings.setProperty("sonar.timemachine.period5", "0.9"); // Analysis from 2008-11-11 - - underTest.execute(); - List periods = periodsHolder.getPeriods(); - - assertThat(periods).extracting("mode").containsExactly( - TIMEMACHINE_MODE_DATE, TIMEMACHINE_MODE_DAYS, - TIMEMACHINE_MODE_PREVIOUS_ANALYSIS, TIMEMACHINE_MODE_VERSION); - - assertThat(periods.get(0).getMode()).isEqualTo(TIMEMACHINE_MODE_DATE); - assertThat(periods.get(0).getIndex()).isEqualTo(1); - assertThat(periods.get(0).getSnapshotDate()).isEqualTo(1227358680000L); - - assertThat(periods.get(1).getMode()).isEqualTo(TIMEMACHINE_MODE_DAYS); - assertThat(periods.get(1).getIndex()).isEqualTo(2); - assertThat(periods.get(1).getSnapshotDate()).isEqualTo(1227157200000L); - - assertThat(periods.get(2).getMode()).isEqualTo(TIMEMACHINE_MODE_PREVIOUS_ANALYSIS); - assertThat(periods.get(2).getIndex()).isEqualTo(3); - assertThat(periods.get(2).getSnapshotDate()).isEqualTo(1227934800000L); - assertThat(periods.get(3).getMode()).isEqualTo(TIMEMACHINE_MODE_VERSION); - assertThat(periods.get(3).getIndex()).isEqualTo(5); - assertThat(periods.get(3).getSnapshotDate()).isEqualTo(1226379600000L); - } - - @Test - public void can_use_project_qualifier_in_settings() { - setupRoot(PROJECT_ROOT); - - dbTester.prepareDbUnit(getClass(), "shared.xml"); - - settings.setProperty("sonar.timemachine.period4.TRK", "2008-11-22"); - settings.setProperty("sonar.timemachine.period5.TRK", "previous_analysis"); - - underTest.execute(); - assertThat(periodsHolder.getPeriods()).hasSize(2); - } - - @Test - public void can_use_views_qualifier_in_settings() { - setupRoot(VIEW_ROOT); - - dbTester.prepareDbUnit(getClass(), "shared.xml"); - - settings.setProperty("sonar.timemachine.period4.VW", "2008-11-22"); - settings.setProperty("sonar.timemachine.period5.VW", "previous_analysis"); - - underTest.execute(); - assertThat(periodsHolder.getPeriods()).hasSize(2); + assertThat(periodsHolder.getPeriod()).isNull(); } }