From 64032ec6f5173ab3523c64405c958eb54b886951 Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Mon, 8 Jun 2015 14:22:20 +0200 Subject: [PATCH] SONAR-6260 Merge PeriodFinder and FeedPeriodsStep --- .../container/ComputeEngineContainerImpl.java | 3 - .../server/computation/period/Period.java | 75 +----- .../computation/period/PeriodFinder.java | 125 --------- .../computation/step/FeedPeriodsStep.java | 129 ++++++++-- .../computation/period/PeriodFinderTest.java | 243 ------------------ .../server/computation/period/PeriodTest.java | 50 +--- .../period/PeriodsHolderImplTest.java | 3 +- .../computation/step/FeedPeriodsStepTest.java | 234 ++++++++++++++++- .../previous_version_deleted.xml | 46 ---- .../step/FeedPeriodsStepTest/empty.xml | 7 + .../FeedPeriodsStepTest}/find_by_date.xml | 0 .../FeedPeriodsStepTest}/find_by_days.xml | 0 .../find_by_previous_analysis.xml | 0 .../find_by_previous_version.xml | 0 .../FeedPeriodsStepTest}/find_by_version.xml | 0 .../no_previous_version.xml | 0 .../previous_version_deleted.xml | 39 +++ .../unprocessed_snapshots.xml | 15 ++ 18 files changed, 397 insertions(+), 572 deletions(-) delete mode 100644 server/sonar-server/src/main/java/org/sonar/server/computation/period/PeriodFinder.java delete mode 100644 server/sonar-server/src/test/java/org/sonar/server/computation/period/PeriodFinderTest.java delete mode 100644 server/sonar-server/src/test/resources/org/sonar/server/computation/period/PeriodFinderTest/previous_version_deleted.xml create mode 100644 server/sonar-server/src/test/resources/org/sonar/server/computation/step/FeedPeriodsStepTest/empty.xml rename server/sonar-server/src/test/resources/org/sonar/server/computation/{period/PeriodFinderTest => step/FeedPeriodsStepTest}/find_by_date.xml (100%) rename server/sonar-server/src/test/resources/org/sonar/server/computation/{period/PeriodFinderTest => step/FeedPeriodsStepTest}/find_by_days.xml (100%) rename server/sonar-server/src/test/resources/org/sonar/server/computation/{period/PeriodFinderTest => step/FeedPeriodsStepTest}/find_by_previous_analysis.xml (100%) rename server/sonar-server/src/test/resources/org/sonar/server/computation/{period/PeriodFinderTest => step/FeedPeriodsStepTest}/find_by_previous_version.xml (100%) rename server/sonar-server/src/test/resources/org/sonar/server/computation/{period/PeriodFinderTest => step/FeedPeriodsStepTest}/find_by_version.xml (100%) rename server/sonar-server/src/test/resources/org/sonar/server/computation/{period/PeriodFinderTest => step/FeedPeriodsStepTest}/no_previous_version.xml (100%) create mode 100644 server/sonar-server/src/test/resources/org/sonar/server/computation/step/FeedPeriodsStepTest/previous_version_deleted.xml create mode 100644 server/sonar-server/src/test/resources/org/sonar/server/computation/step/FeedPeriodsStepTest/unprocessed_snapshots.xml diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/container/ComputeEngineContainerImpl.java b/server/sonar-server/src/main/java/org/sonar/server/computation/container/ComputeEngineContainerImpl.java index b92b939d01d..c53fb8a933b 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/container/ComputeEngineContainerImpl.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/container/ComputeEngineContainerImpl.java @@ -50,7 +50,6 @@ import org.sonar.server.computation.issue.SourceLinesCache; import org.sonar.server.computation.language.PlatformLanguageRepository; import org.sonar.server.computation.measure.MeasureRepositoryImpl; import org.sonar.server.computation.measure.MetricCache; -import org.sonar.server.computation.period.PeriodFinder; import org.sonar.server.computation.period.PeriodsHolderImpl; import org.sonar.server.computation.step.ComputationStep; import org.sonar.server.computation.step.ComputationSteps; @@ -121,8 +120,6 @@ public class ComputeEngineContainerImpl extends ComponentContainer implements Co BatchReportDirectoryHolderImpl.class, - PeriodFinder.class, - // repositories PlatformLanguageRepository.class, MeasureRepositoryImpl.class, diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/period/Period.java b/server/sonar-server/src/main/java/org/sonar/server/computation/period/Period.java index 6b7af6e0391..fc1dcdcdc8c 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/period/Period.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/period/Period.java @@ -20,102 +20,43 @@ package org.sonar.server.computation.period; -import java.util.Calendar; -import java.util.Date; +import javax.annotation.CheckForNull; import javax.annotation.Nullable; -import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.builder.ReflectionToStringBuilder; -import org.sonar.api.CoreProperties; -import org.sonar.api.utils.DateUtils; -import org.sonar.core.component.SnapshotDto; public class Period { private int index; private String mode, modeParameter; - private SnapshotDto projectSnapshot; - /** - * Date resolved from the settings of the period - * - date : the given date - * - days : nearest analysis date found - * - previous analysis : date of the last analysis - * - previous version : date of the analysis of the previous version - * - version : date of the analysis of the given version - */ - private Long targetDate = null; + private long snapshotDate; - public Period(String mode, @Nullable Long targetDate, SnapshotDto projectSnapshot) { - this.mode = mode; - if (targetDate != null) { - this.targetDate = targetDate; - } - this.projectSnapshot = projectSnapshot; - } - - public Period setIndex(int index) { + public Period(int index, String mode, @Nullable String modeParameter, long snapshotDate) { this.index = index; - return this; + this.mode = mode; + this.modeParameter = modeParameter; + this.snapshotDate = snapshotDate; } public int getIndex() { return index; } - public SnapshotDto getProjectSnapshot() { - return projectSnapshot; - } - - /** - * Date of the snapshot - */ public Long getSnapshotDate() { - return projectSnapshot != null ? projectSnapshot.getCreatedAt() : null; + return snapshotDate; } public String getMode() { return mode; } + @CheckForNull public String getModeParameter() { return modeParameter; } - public Period setModeParameter(String s) { - this.modeParameter = s; - return this; - } - - public Long getTargetDate() { - return targetDate; - } - @Override public String toString() { - Date snapshotDate = new Date(getSnapshotDate()); - if (StringUtils.equals(mode, CoreProperties.TIMEMACHINE_MODE_VERSION)) { - String label = String.format("Compare to version %s", modeParameter); - if (targetDate != null) { - label += String.format(" (%s)", DateUtils.formatDate(snapshotDate)); - } - return label; - } - if (StringUtils.equals(mode, CoreProperties.TIMEMACHINE_MODE_DAYS)) { - return String.format("Compare over %s days (%s, analysis of %s)", modeParameter, formatDate(),DateUtils.formatDate(snapshotDate)); - } - if (StringUtils.equals(mode, CoreProperties.TIMEMACHINE_MODE_PREVIOUS_ANALYSIS)) { - return String.format("Compare to previous analysis (%s)", DateUtils.formatDate(snapshotDate)); - } - if (StringUtils.equals(mode, CoreProperties.TIMEMACHINE_MODE_PREVIOUS_VERSION)) { - return String.format("Compare to previous version (%s)", DateUtils.formatDate(snapshotDate)); - } - if (StringUtils.equals(mode, CoreProperties.TIMEMACHINE_MODE_DATE)) { - return String.format("Compare to date %s (analysis of %s)", formatDate(), DateUtils.formatDate(snapshotDate)); - } return ReflectionToStringBuilder.toString(this); } - private String formatDate() { - return DateUtils.formatDate(org.apache.commons.lang.time.DateUtils.truncate(new Date(targetDate), Calendar.SECOND)); - } - } diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/period/PeriodFinder.java b/server/sonar-server/src/main/java/org/sonar/server/computation/period/PeriodFinder.java deleted file mode 100644 index 8b7efa5e5ed..00000000000 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/period/PeriodFinder.java +++ /dev/null @@ -1,125 +0,0 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2014 SonarSource - * mailto:contact AT sonarsource DOT com - * - * SonarQube is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * SonarQube is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ - -package org.sonar.server.computation.period; - -import com.google.common.annotations.VisibleForTesting; -import java.text.SimpleDateFormat; -import java.util.Date; -import java.util.List; -import javax.annotation.CheckForNull; -import org.sonar.api.CoreProperties; -import org.sonar.api.utils.DateUtils; -import org.sonar.core.component.SnapshotDto; -import org.sonar.core.component.SnapshotQuery; -import org.sonar.core.persistence.DbSession; -import org.sonar.server.db.DbClient; - -import static org.sonar.core.component.SnapshotQuery.SORT_FIELD.BY_DATE; -import static org.sonar.core.component.SnapshotQuery.SORT_ORDER.ASC; -import static org.sonar.core.component.SnapshotQuery.SORT_ORDER.DESC; - -public class PeriodFinder { - - private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat(DateUtils.DATE_FORMAT); - - private final DbClient dbClient; - - public PeriodFinder(DbClient dbClient) { - this.dbClient = dbClient; - } - - @CheckForNull - public Period findByDate(DbSession session, Long projectId, Date date) { - SnapshotDto snapshot = findFirstSnapshot(session, createCommonQuery(projectId).setCreatedAfter(date.getTime()).setSort(BY_DATE, ASC)); - if (snapshot == null) { - return null; - } - return new Period(CoreProperties.TIMEMACHINE_MODE_DATE, date.getTime(), snapshot).setModeParameter(DATE_FORMAT.format(date)); - } - - @CheckForNull - public Period findByDays(DbSession session, Long projectId, long analysisDate, int days) { - List snapshots = dbClient.snapshotDao().selectSnapshotsByQuery(session, createCommonQuery(projectId).setCreatedBefore(analysisDate).setSort(BY_DATE, ASC)); - long targetDate = DateUtils.addDays(new Date(analysisDate), -days).getTime(); - SnapshotDto snapshot = findNearestSnapshotToTargetDate(snapshots, targetDate); - if (snapshot == null) { - return null; - } - return new Period(CoreProperties.TIMEMACHINE_MODE_DAYS, targetDate, snapshot).setModeParameter(String.valueOf(days)); - } - - @CheckForNull - public Period findByPreviousAnalysis(DbSession session, Long projectId, long analysisDate) { - SnapshotDto snapshot = findFirstSnapshot(session, createCommonQuery(projectId).setCreatedBefore(analysisDate).setIsLast(true).setSort(BY_DATE, DESC)); - if (snapshot == null) { - return null; - } - return new Period(CoreProperties.TIMEMACHINE_MODE_PREVIOUS_ANALYSIS, snapshot.getCreatedAt(), snapshot).setModeParameter(DATE_FORMAT.format(snapshot.getCreatedAt())); - } - - @CheckForNull - public Period findByPreviousVersion(DbSession session, Long projectId, String version) { - List snapshotDtos = dbClient.snapshotDao().selectPreviousVersionSnapshots(session, projectId, version); - if (snapshotDtos.isEmpty()) { - return null; - } - SnapshotDto snapshotDto = snapshotDtos.get(0); - return new Period(CoreProperties.TIMEMACHINE_MODE_PREVIOUS_VERSION, snapshotDto.getCreatedAt(), snapshotDto).setModeParameter(snapshotDto.getVersion()); - } - - @CheckForNull - public Period findByVersion(DbSession session, Long projectId, String version) { - SnapshotDto snapshot = findFirstSnapshot(session, createCommonQuery(projectId).setVersion(version).setSort(BY_DATE, DESC)); - if (snapshot == null) { - return null; - } - return new Period(CoreProperties.TIMEMACHINE_MODE_VERSION, snapshot.getCreatedAt(), snapshot).setModeParameter(version); - } - - @VisibleForTesting - @CheckForNull - static SnapshotDto findNearestSnapshotToTargetDate(List snapshots, Long targetDate) { - long bestDistance = Long.MAX_VALUE; - SnapshotDto nearest = null; - for (SnapshotDto snapshot : snapshots) { - long distance = Math.abs(snapshot.getCreatedAt() - targetDate); - if (distance <= bestDistance) { - bestDistance = distance; - nearest = snapshot; - } - } - return nearest; - } - - @CheckForNull - private SnapshotDto findFirstSnapshot(DbSession session, SnapshotQuery query) { - List snapshots = dbClient.snapshotDao().selectSnapshotsByQuery(session, query); - if (snapshots.size() >= 1) { - return snapshots.get(0); - } - return null; - } - - private static SnapshotQuery createCommonQuery(Long projectId) { - return new SnapshotQuery().setComponentId(projectId).setStatus(SnapshotDto.STATUS_PROCESSED); - } - -} diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/step/FeedPeriodsStep.java b/server/sonar-server/src/main/java/org/sonar/server/computation/step/FeedPeriodsStep.java index 7612c910331..4732af8e7b8 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/step/FeedPeriodsStep.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/step/FeedPeriodsStep.java @@ -21,31 +21,35 @@ package org.sonar.server.computation.step; import com.google.common.base.Strings; -import java.text.ParseException; -import java.text.SimpleDateFormat; import java.util.ArrayList; +import java.util.Calendar; import java.util.Date; import java.util.List; import javax.annotation.CheckForNull; import javax.annotation.Nullable; import org.apache.commons.lang.StringUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import org.sonar.api.CoreProperties; import org.sonar.api.config.Settings; import org.sonar.api.resources.Qualifiers; import org.sonar.api.utils.DateUtils; +import org.sonar.api.utils.log.Logger; +import org.sonar.api.utils.log.Loggers; import org.sonar.batch.protocol.output.BatchReport; import org.sonar.core.component.ComponentDto; +import org.sonar.core.component.SnapshotDto; +import org.sonar.core.component.SnapshotQuery; import org.sonar.core.persistence.DbSession; import org.sonar.server.computation.batch.BatchReportReader; import org.sonar.server.computation.component.Component; import org.sonar.server.computation.component.TreeRootHolder; import org.sonar.server.computation.period.Period; -import org.sonar.server.computation.period.PeriodFinder; import org.sonar.server.computation.period.PeriodsHolderImpl; import org.sonar.server.db.DbClient; +import static org.sonar.core.component.SnapshotQuery.SORT_FIELD.BY_DATE; +import static org.sonar.core.component.SnapshotQuery.SORT_ORDER.ASC; +import static org.sonar.core.component.SnapshotQuery.SORT_ORDER.DESC; + /** * Populates the {@link org.sonar.server.computation.period.PeriodsHolder} * @@ -55,25 +59,22 @@ import org.sonar.server.db.DbClient; * - If a snapshot is found, a new period is added to the repository */ public class FeedPeriodsStep implements ComputationStep { - private static final Logger LOG = LoggerFactory.getLogger(PeriodsHolderImpl.class); - private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat(DateUtils.DATE_FORMAT); + private static final Logger LOG = Loggers.get(FeedPeriodsStep.class); private static final int NUMBER_OF_PERIODS = 5; private final DbClient dbClient; private final Settings settings; private final TreeRootHolder treeRootHolder; - private final PeriodFinder periodFinder; private final BatchReportReader batchReportReader; private final PeriodsHolderImpl periodsHolder; - public FeedPeriodsStep(DbClient dbClient, Settings settings, TreeRootHolder treeRootHolder, PeriodFinder periodFinder, BatchReportReader batchReportReader, + public FeedPeriodsStep(DbClient dbClient, Settings settings, TreeRootHolder treeRootHolder, BatchReportReader batchReportReader, PeriodsHolderImpl periodsHolder) { this.dbClient = dbClient; this.settings = settings; this.treeRootHolder = treeRootHolder; - this.periodFinder = periodFinder; this.batchReportReader = batchReportReader; this.periodsHolder = periodsHolder; } @@ -100,8 +101,7 @@ public class FeedPeriodsStep implements ComputationStep { Period period = periodResolver.resolve(index); // SONAR-4700 Add a past snapshot only if it exists if (period != null) { - periods.add(period.setIndex(index)); - LOG.debug(period.toString()); + periods.add(period); } } } @@ -130,6 +130,9 @@ public class FeedPeriodsStep implements ComputationStep { @CheckForNull private Period resolve(int index) { String propertyValue = getPropertyValue(qualifier, settings, index); + if (StringUtils.isBlank(propertyValue)) { + return null; + } Period period = resolve(index, propertyValue); if (period == null && StringUtils.isNotBlank(propertyValue)) { LOG.debug("Property " + CoreProperties.TIMEMACHINE_PERIOD_PREFIX + index + " is not valid: " + propertyValue); @@ -139,25 +142,21 @@ public class FeedPeriodsStep implements ComputationStep { @CheckForNull private Period resolve(int index, String property) { - if (StringUtils.isBlank(property)) { - return null; - } - Integer days = tryToResolveByDays(property); if (days != null) { - return periodFinder.findByDays(session, projectId, analysisDate, days); + return findByDays(index, days); } Date date = tryToResolveByDate(property); if (date != null) { - return periodFinder.findByDate(session, projectId, date); + return findByDate(index, date); } if (StringUtils.equals(CoreProperties.TIMEMACHINE_MODE_PREVIOUS_ANALYSIS, property)) { - return periodFinder.findByPreviousAnalysis(session, projectId, analysisDate); + return findByPreviousAnalysis(index); } if (StringUtils.equals(CoreProperties.TIMEMACHINE_MODE_PREVIOUS_VERSION, property)) { - return periodFinder.findByPreviousVersion(session, projectId, currentVersion); + return findByPreviousVersion(index); } - return periodFinder.findByVersion(session, projectId, property); + return findByVersion(index, property); } @CheckForNull @@ -172,11 +171,95 @@ public class FeedPeriodsStep implements ComputationStep { @CheckForNull private Date tryToResolveByDate(String property) { try { - return DATE_FORMAT.parse(property); - } catch (ParseException e) { + return DateUtils.parseDate(property); + } catch (Exception e) { + return null; + } + } + + private Period findByDate(int index, Date date) { + SnapshotDto snapshot = findFirstSnapshot(session, createCommonQuery(projectId).setCreatedAfter(date.getTime()).setSort(BY_DATE, ASC)); + if (snapshot == null) { + return null; + } + LOG.debug(String.format("Compare to date %s (analysis of %s)", formatDate(date.getTime()), formatDate(snapshot.getCreatedAt()))); + return new Period(index, CoreProperties.TIMEMACHINE_MODE_DATE, DateUtils.formatDate(date), snapshot.getCreatedAt()); + } + + @CheckForNull + private Period findByDays(int index, int days) { + List snapshots = dbClient.snapshotDao().selectSnapshotsByQuery(session, createCommonQuery(projectId).setCreatedBefore(analysisDate).setSort(BY_DATE, ASC)); + long targetDate = DateUtils.addDays(new Date(analysisDate), -days).getTime(); + SnapshotDto snapshot = findNearestSnapshotToTargetDate(snapshots, targetDate); + if (snapshot == null) { return null; } + LOG.debug(String.format("Compare over %s days (%s, analysis of %s)", String.valueOf(days), formatDate(targetDate), formatDate(snapshot.getCreatedAt()))); + return new Period(index, CoreProperties.TIMEMACHINE_MODE_DAYS, String.valueOf(days), snapshot.getCreatedAt()); } + + @CheckForNull + private Period findByPreviousAnalysis(int index) { + SnapshotDto snapshot = findFirstSnapshot(session, createCommonQuery(projectId).setCreatedBefore(analysisDate).setIsLast(true).setSort(BY_DATE, DESC)); + if (snapshot == null) { + return null; + } + LOG.debug(String.format("Compare to previous analysis (%s)", formatDate(snapshot.getCreatedAt()))); + return new Period(index, CoreProperties.TIMEMACHINE_MODE_PREVIOUS_ANALYSIS, formatDate(snapshot.getCreatedAt()), snapshot.getCreatedAt()); + } + + @CheckForNull + private Period findByPreviousVersion(int index) { + List snapshotDtos = dbClient.snapshotDao().selectPreviousVersionSnapshots(session, projectId, currentVersion); + if (snapshotDtos.isEmpty()) { + return null; + } + SnapshotDto snapshotDto = snapshotDtos.get(0); + LOG.debug(String.format("Compare to previous version (%s)", formatDate(snapshotDto.getCreatedAt()))); + return new Period(index, CoreProperties.TIMEMACHINE_MODE_PREVIOUS_VERSION, snapshotDto.getVersion(), snapshotDto.getCreatedAt()); + } + + @CheckForNull + private Period findByVersion(int index, String version) { + SnapshotDto snapshot = findFirstSnapshot(session, createCommonQuery(projectId).setVersion(version).setSort(BY_DATE, DESC)); + if (snapshot == null) { + return null; + } + LOG.debug(String.format("Compare to version (%s) (%s)", version, formatDate(snapshot.getCreatedAt()))); + return new Period(index, CoreProperties.TIMEMACHINE_MODE_VERSION, version, snapshot.getCreatedAt()); + } + + @CheckForNull + private SnapshotDto findFirstSnapshot(DbSession session, SnapshotQuery query) { + List snapshots = dbClient.snapshotDao().selectSnapshotsByQuery(session, query); + if (!snapshots.isEmpty()) { + return snapshots.get(0); + } + return null; + } + + } + + @CheckForNull + private static SnapshotDto findNearestSnapshotToTargetDate(List snapshots, Long targetDate) { + long bestDistance = Long.MAX_VALUE; + SnapshotDto nearest = null; + for (SnapshotDto snapshot : snapshots) { + long distance = Math.abs(snapshot.getCreatedAt() - targetDate); + if (distance <= bestDistance) { + bestDistance = distance; + nearest = snapshot; + } + } + return nearest; + } + + private static SnapshotQuery createCommonQuery(Long projectId) { + return new SnapshotQuery().setComponentId(projectId).setStatus(SnapshotDto.STATUS_PROCESSED); + } + + private static String formatDate(long date) { + return DateUtils.formatDate(org.apache.commons.lang.time.DateUtils.truncate(new Date(date), Calendar.SECOND)); } private static String getPropertyValue(@Nullable String qualifier, Settings settings, int index) { diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/period/PeriodFinderTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/period/PeriodFinderTest.java deleted file mode 100644 index 37dd19862fd..00000000000 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/period/PeriodFinderTest.java +++ /dev/null @@ -1,243 +0,0 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2014 SonarSource - * mailto:contact AT sonarsource DOT com - * - * SonarQube is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * SonarQube is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ - -package org.sonar.server.computation.period; - -import java.text.ParseException; -import java.text.SimpleDateFormat; -import java.util.Arrays; -import java.util.Date; -import java.util.List; -import org.apache.commons.lang.time.DateUtils; -import org.junit.After; -import org.junit.Before; -import org.junit.ClassRule; -import org.junit.Test; -import org.junit.experimental.categories.Category; -import org.sonar.api.CoreProperties; -import org.sonar.core.component.SnapshotDto; -import org.sonar.core.persistence.DbSession; -import org.sonar.core.persistence.DbTester; -import org.sonar.server.component.db.SnapshotDao; -import org.sonar.server.db.DbClient; -import org.sonar.test.DbTests; - -import static org.assertj.core.api.Assertions.assertThat; - -@Category(DbTests.class) -public class PeriodFinderTest { - - private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd"); - - @ClassRule - public static final DbTester dbTester = new DbTester(); - - DbClient dbClient; - - DbSession dbSession; - - PeriodFinder periodFinder; - - @Before - public void setUp() throws Exception { - dbClient = new DbClient(dbTester.database(), dbTester.myBatis(), new SnapshotDao()); - dbSession = dbClient.openSession(false); - periodFinder = new PeriodFinder(dbClient); - } - - @After - public void tearDown() throws Exception { - dbSession.close(); - } - - @Test - public void find_by_date() throws Exception { - dbTester.prepareDbUnit(getClass(), "find_by_date.xml"); - String textDate = "2008-11-22"; - Date date = DATE_FORMAT.parse(textDate); - - Period result = periodFinder.findByDate(dbSession, 1L, date); - - // Return analysis from given date 2008-11-22 - assertThat(result.getMode()).isEqualTo(CoreProperties.TIMEMACHINE_MODE_DATE); - assertThat(result.getModeParameter()).isEqualTo(textDate); - assertThat(result.getSnapshotDate()).isEqualTo(1227358680000L); - assertThat(result.getTargetDate()).isEqualTo(date.getTime()); - assertThat(result.getProjectSnapshot().getId()).isEqualTo(1006L); - } - - @Test - public void find_by_date_search_for_nearest_later_analysis() throws Exception { - dbTester.prepareDbUnit(getClass(), "find_by_date.xml"); - String date = "2008-11-24"; - - Period result = periodFinder.findByDate(dbSession, 1L, DATE_FORMAT.parse(date)); - - // No analysis have been done at this date, it should return the one from the date after (2008-11-25) - assertThat(result.getSnapshotDate()).isEqualTo(1227617880000L); - assertThat(result.getProjectSnapshot().getId()).isEqualTo(1009L); - } - - @Test - public void not_find_by_date() throws Exception { - dbTester.prepareDbUnit(getClass(), "find_by_date.xml"); - String date = "2008-11-22"; - - // No analysis for this project - assertThat(periodFinder.findByDate(dbSession, 123L, DATE_FORMAT.parse(date))).isNull(); - } - - @Test - public void find_by_days() throws Exception { - dbTester.prepareDbUnit(getClass(), "find_by_days.xml"); - - Period result = periodFinder.findByDays(dbSession, 1L, DATE_FORMAT.parse("2008-11-16").getTime(), 50); - - // Return analysis from the 2008-11-01 - assertThat(result.getMode()).isEqualTo(CoreProperties.TIMEMACHINE_MODE_DAYS); - assertThat(result.getModeParameter()).isEqualTo("50"); - assertThat(result.getSnapshotDate()).isEqualTo(1225544280000L); - assertThat(result.getTargetDate()).isBetween(DATE_FORMAT.parse("2008-09-26").getTime(), DATE_FORMAT.parse("2008-09-27").getTime()); - assertThat(result.getProjectSnapshot().getId()).isEqualTo(1000L); - } - - @Test - public void ignore_unprocessed_snapshots() throws Exception { - dbTester.prepareDbUnit(getClass(), "find_by_days.xml"); - - Period result = periodFinder.findByDays(dbSession, 1L, DATE_FORMAT.parse("2008-11-16").getTime(), 7); - - // Return analysis from the 2008-11-13, the matching one from 2008-11-12 should be ignored - assertThat(result.getProjectSnapshot().getId()).isEqualTo(1006L); - } - - @Test - public void not_find_by_days() throws Exception { - dbTester.prepareDbUnit(getClass(), "find_by_days.xml"); - - // No analysis for this project - assertThat(periodFinder.findByDays(dbSession, 123L, DATE_FORMAT.parse("2008-11-16").getTime(), 7)).isNull(); - } - - @Test - public void locate_nearest_snapshot_before() throws ParseException { - Date current = DATE_FORMAT.parse("2010-10-20"); - // distance: 15 => target is 2010-10-05 - - List snapshots = Arrays.asList( - new SnapshotDto().setId(1L).setCreatedAt(DATE_FORMAT.parse("2010-09-30").getTime()), - new SnapshotDto().setId(2L).setCreatedAt(DATE_FORMAT.parse("2010-10-03").getTime()), // -2 days - new SnapshotDto().setId(3L).setCreatedAt(DATE_FORMAT.parse("2010-10-08").getTime()), // +3 days - new SnapshotDto().setId(4L).setCreatedAt(DATE_FORMAT.parse("2010-10-12").getTime()) // +7 days - ); - assertThat(PeriodFinder.findNearestSnapshotToTargetDate(snapshots, DateUtils.addDays(current, -15).getTime()).getId()).isEqualTo(2); - } - - @Test - public void locate_nearest_snapshot_after() throws ParseException { - Date current = DATE_FORMAT.parse("2010-10-20"); - // distance: 15 => target is 2010-10-05 - - List snapshots = Arrays.asList( - new SnapshotDto().setId(1L).setCreatedAt(DATE_FORMAT.parse("2010-09-30").getTime()), - new SnapshotDto().setId(2L).setCreatedAt(DATE_FORMAT.parse("2010-10-01").getTime()), // -4 days - new SnapshotDto().setId(3L).setCreatedAt(DATE_FORMAT.parse("2010-10-08").getTime()), // +3 days - new SnapshotDto().setId(4L).setCreatedAt(DATE_FORMAT.parse("2010-10-12").getTime()) // +7 days - ); - assertThat(PeriodFinder.findNearestSnapshotToTargetDate(snapshots, DateUtils.addDays(current, -15).getTime()).getId()).isEqualTo(3); - } - - @Test - public void find_by_previous_analysis() throws Exception { - dbTester.prepareDbUnit(getClass(), "find_by_previous_analysis.xml"); - - Period result = periodFinder.findByPreviousAnalysis(dbSession, 1L, DATE_FORMAT.parse("2008-11-27").getTime()); - - // Return analysis from given date 2008-11-22 - assertThat(result.getMode()).isEqualTo(CoreProperties.TIMEMACHINE_MODE_PREVIOUS_ANALYSIS); - assertThat(result.getModeParameter()).isEqualTo(DATE_FORMAT.format(new Date(1227617880000L))); - assertThat(result.getSnapshotDate()).isEqualTo(1227617880000L); - assertThat(result.getTargetDate()).isEqualTo(1227617880000L); - assertThat(result.getProjectSnapshot().getId()).isEqualTo(1009L); - } - - @Test - public void not_find_by_previous_analysis() throws Exception { - dbTester.prepareDbUnit(getClass(), "find_by_previous_analysis.xml"); - - // No analysis for this project - assertThat(periodFinder.findByPreviousAnalysis(dbSession, 2L, DATE_FORMAT.parse("2008-11-27").getTime())).isNull(); - } - - @Test - public void find_by_previous_version() throws Exception { - dbTester.prepareDbUnit(getClass(), "find_by_previous_version.xml"); - - Period result = periodFinder.findByPreviousVersion(dbSession, 1L, "1.2-SNAPSHOT"); - - // Return analysis from given date 2008-11-22 - assertThat(result.getMode()).isEqualTo(CoreProperties.TIMEMACHINE_MODE_PREVIOUS_VERSION); - assertThat(result.getModeParameter()).isEqualTo("1.1"); - assertThat(result.getSnapshotDate()).isEqualTo(1225803480000L); - assertThat(result.getTargetDate()).isEqualTo(1225803480000L); - assertThat(result.getProjectSnapshot().getId()).isEqualTo(1001L); - } - - @Test - public void find_by_previous_version_when_previous_version_deleted() throws Exception { - dbTester.prepareDbUnit(getClass(), "previous_version_deleted.xml"); - - Period result = periodFinder.findByPreviousVersion(dbSession, 1L, "1.2-SNAPSHOT"); - - // Return analysis from given date 2008-11-22 - assertThat(result.getModeParameter()).isEqualTo("1.0"); - assertThat(result.getSnapshotDate()).isEqualTo(1225630680000L); - assertThat(result.getTargetDate()).isEqualTo(1225630680000L); - assertThat(result.getProjectSnapshot().getId()).isEqualTo(1000L); - } - - @Test - public void not_find_previous_version() throws Exception { - dbTester.prepareDbUnit(getClass(), "no_previous_version.xml"); - - assertThat(periodFinder.findByPreviousVersion(dbSession, 1L, "1.2-SNAPSHOT")).isNull(); - } - - @Test - public void find_by_version() throws Exception { - dbTester.prepareDbUnit(getClass(), "find_by_version.xml"); - - Period result = periodFinder.findByVersion(dbSession, 1L, "1.1"); - - assertThat(result.getMode()).isEqualTo(CoreProperties.TIMEMACHINE_MODE_VERSION); - assertThat(result.getModeParameter()).isEqualTo("1.1"); - assertThat(result.getSnapshotDate()).isEqualTo(1225803480000L); - assertThat(result.getTargetDate()).isEqualTo(1225803480000L); - assertThat(result.getProjectSnapshot().getId()).isEqualTo(1009L); - } - - @Test - public void not_find_by_version() throws Exception { - dbTester.prepareDbUnit(getClass(), "find_by_version.xml"); - - assertThat(periodFinder.findByVersion(dbSession, 1L, "1.0")).isNull(); - } - -} diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/period/PeriodTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/period/PeriodTest.java index 99cfae19792..f4a59189676 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/period/PeriodTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/period/PeriodTest.java @@ -22,7 +22,6 @@ package org.sonar.server.computation.period; import org.junit.Test; import org.sonar.api.CoreProperties; -import org.sonar.core.component.SnapshotDto; import static org.assertj.core.api.Assertions.assertThat; @@ -31,59 +30,12 @@ public class PeriodTest { @Test public void test_some_setters_and_getters() { Long date = System.currentTimeMillis(); - SnapshotDto snapshotDto = new SnapshotDto().setCreatedAt(date); - Period period = new Period(CoreProperties.TIMEMACHINE_MODE_VERSION, 1000L, snapshotDto) - .setModeParameter("2.3") - .setIndex(1); + Period period = new Period(1, CoreProperties.TIMEMACHINE_MODE_VERSION, "2.3", date); assertThat(period.getMode()).isEqualTo(CoreProperties.TIMEMACHINE_MODE_VERSION); assertThat(period.getModeParameter()).isEqualTo("2.3"); assertThat(period.getIndex()).isEqualTo(1); - assertThat(period.getProjectSnapshot()).isEqualTo(snapshotDto); assertThat(period.getSnapshotDate()).isEqualTo(date); - assertThat(period.getTargetDate()).isEqualTo(1000L); - } - - @Test - public void to_string_for_version() { - Period period = new Period(CoreProperties.TIMEMACHINE_MODE_VERSION, 1000L, new SnapshotDto().setCreatedAt(System.currentTimeMillis())).setModeParameter("2.3"); - assertThat(period.toString()).startsWith("Compare to version 2.3"); - } - - @Test - public void to_string_for_version_without_date() { - Period period = new Period(CoreProperties.TIMEMACHINE_MODE_VERSION, null, new SnapshotDto().setCreatedAt(System.currentTimeMillis())).setModeParameter("2.3"); - assertThat(period.toString()).isEqualTo("Compare to version 2.3"); - } - - @Test - public void to_string_for_number_of_days() { - Period period = new Period(CoreProperties.TIMEMACHINE_MODE_DAYS, 1000L, new SnapshotDto().setCreatedAt(System.currentTimeMillis())).setModeParameter("30"); - assertThat(period.toString()).startsWith("Compare over 30 days ("); - } - - @Test - public void to_string_for_number_of_days_with_snapshot() { - Period period = new Period(CoreProperties.TIMEMACHINE_MODE_DAYS, 1000L, new SnapshotDto().setCreatedAt(System.currentTimeMillis())).setModeParameter("30"); - assertThat(period.toString()).startsWith("Compare over 30 days ("); - } - - @Test - public void to_string_for_date() { - Period period = new Period(CoreProperties.TIMEMACHINE_MODE_DATE, 1000L, new SnapshotDto().setCreatedAt(System.currentTimeMillis())); - assertThat(period.toString()).startsWith("Compare to date "); - } - - @Test - public void to_string_for_date_with_snapshot() { - Period period = new Period(CoreProperties.TIMEMACHINE_MODE_DATE, 1000L, new SnapshotDto().setCreatedAt(System.currentTimeMillis())); - assertThat(period.toString()).startsWith("Compare to date "); - } - - @Test - public void to_string_for_previous_analysis() { - Period period = new Period(CoreProperties.TIMEMACHINE_MODE_PREVIOUS_ANALYSIS, 1000L, new SnapshotDto().setCreatedAt(System.currentTimeMillis())); - assertThat(period.toString()).startsWith("Compare to previous analysis "); } } diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/period/PeriodsHolderImplTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/period/PeriodsHolderImplTest.java index 6f7f6d863ba..cf5dacc8ec9 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/period/PeriodsHolderImplTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/period/PeriodsHolderImplTest.java @@ -25,7 +25,6 @@ import java.util.List; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; -import org.sonar.core.component.SnapshotDto; import static org.assertj.core.api.Assertions.assertThat; @@ -37,7 +36,7 @@ public class PeriodsHolderImplTest { @Test public void get_periods() throws Exception { List periods = new ArrayList<>(); - periods.add(new Period("mode", null, new SnapshotDto())); + periods.add(new Period(1, "mode", null, 1000L)); PeriodsHolderImpl periodsHolder = new PeriodsHolderImpl(); periodsHolder.setPeriods(periods); diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/step/FeedPeriodsStepTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/step/FeedPeriodsStepTest.java index 699d5bdf79d..a8ce47afa02 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/step/FeedPeriodsStepTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/step/FeedPeriodsStepTest.java @@ -23,7 +23,6 @@ package org.sonar.server.computation.step; import com.google.common.base.Function; import com.google.common.collect.Iterables; import java.text.SimpleDateFormat; -import java.util.Date; import java.util.List; import org.junit.After; import org.junit.Before; @@ -33,6 +32,7 @@ import org.junit.Test; import org.junit.experimental.categories.Category; import org.sonar.api.CoreProperties; import org.sonar.api.config.Settings; +import org.sonar.api.utils.log.LogTester; import org.sonar.batch.protocol.Constants; import org.sonar.batch.protocol.output.BatchReport; import org.sonar.core.persistence.DbSession; @@ -44,7 +44,6 @@ import org.sonar.server.computation.batch.TreeRootHolderRule; import org.sonar.server.computation.component.Component; import org.sonar.server.computation.component.DumbComponent; import org.sonar.server.computation.period.Period; -import org.sonar.server.computation.period.PeriodFinder; import org.sonar.server.computation.period.PeriodsHolderImpl; import org.sonar.server.db.DbClient; import org.sonar.test.DbTests; @@ -67,6 +66,9 @@ public class FeedPeriodsStepTest extends BaseStepTest { @Rule public BatchReportReaderRule reportReader = new BatchReportReaderRule(); + @Rule + public LogTester logTester = new LogTester(); + PeriodsHolderImpl periodsHolder = new PeriodsHolderImpl(); DbClient dbClient; @@ -101,7 +103,7 @@ public class FeedPeriodsStepTest extends BaseStepTest { Component project = new DumbComponent(Component.Type.PROJECT, 1, "ABCD", PROJECT_KEY); treeRootHolder.setRoot(project); - sut = new FeedPeriodsStep(dbClient, settings, treeRootHolder, new PeriodFinder(dbClient), reportReader, periodsHolder); + sut = new FeedPeriodsStep(dbClient, settings, treeRootHolder, reportReader, periodsHolder); } @After @@ -118,23 +120,20 @@ public class FeedPeriodsStepTest extends BaseStepTest { } @Test - public void get_one_period() throws Exception { + public void feed_one_period() throws Exception { dbTester.prepareDbUnit(getClass(), "shared.xml"); String textDate = "2008-11-22"; - Date date = DATE_FORMAT.parse(textDate); settings.setProperty("sonar.timemachine.period1", textDate); sut.execute(); List periods = periodsHolder.getPeriods(); assertThat(periods).hasSize(1); - Period period = periods.get(0); + Period period = periods.get(0); assertThat(period.getMode()).isEqualTo(CoreProperties.TIMEMACHINE_MODE_DATE); assertThat(period.getModeParameter()).isEqualTo(textDate); assertThat(period.getSnapshotDate()).isEqualTo(1227358680000L); - assertThat(period.getTargetDate()).isEqualTo(date.getTime()); - assertThat(period.getProjectSnapshot().getId()).isEqualTo(1003L); } @Test @@ -158,7 +157,214 @@ public class FeedPeriodsStepTest extends BaseStepTest { } @Test - public void get_five_different_periods() throws Exception { + public void ignore_unprocessed_snapshots() throws Exception { + dbTester.prepareDbUnit(getClass(), "unprocessed_snapshots.xml"); + + settings.setProperty("sonar.timemachine.period1", "100"); + + sut.execute(); + assertThat(periodsHolder.getPeriods()).isEmpty(); + } + + @Test + public void feed_period_by_date() throws Exception { + dbTester.prepareDbUnit(getClass(), "shared.xml"); + + String textDate = "2008-11-22"; + settings.setProperty("sonar.timemachine.period1", textDate); + + sut.execute(); + List periods = periodsHolder.getPeriods(); + assertThat(periods).hasSize(1); + + Period period = periods.get(0); + // Return analysis from given date 2008-11-22 + assertThat(period.getMode()).isEqualTo(CoreProperties.TIMEMACHINE_MODE_DATE); + assertThat(period.getModeParameter()).isEqualTo(textDate); + assertThat(period.getSnapshotDate()).isEqualTo(1227358680000L); + + assertThat(logTester.logs()).hasSize(1); + assertThat(logTester.logs().get(0)).startsWith("Compare to date 2008-11-22 (analysis of "); + } + + @Test + public void search_by_date_return_nearest_later_analysis() throws Exception { + dbTester.prepareDbUnit(getClass(), "shared.xml"); + String date = "2008-11-24"; + + settings.setProperty("sonar.timemachine.period1", date); + + sut.execute(); + List periods = periodsHolder.getPeriods(); + assertThat(periods).hasSize(1); + + // Analysis form 2008-11-29 + Period period = periods.get(0); + assertThat(period.getMode()).isEqualTo(CoreProperties.TIMEMACHINE_MODE_DATE); + assertThat(period.getModeParameter()).isEqualTo(date); + assertThat(period.getSnapshotDate()).isEqualTo(1227934800000L); + } + + @Test + public void no_period_by_date() throws Exception { + dbTester.prepareDbUnit(getClass(), "shared.xml"); + + // No analysis at and after this date + settings.setProperty("sonar.timemachine.period1", "2008-11-30"); + + sut.execute(); + assertThat(periodsHolder.getPeriods()).isEmpty(); + } + + @Test + public void feed_period_by_days() throws Exception { + dbTester.prepareDbUnit(getClass(), "shared.xml"); + + settings.setProperty("sonar.timemachine.period1", "10"); + + sut.execute(); + List periods = periodsHolder.getPeriods(); + assertThat(periods).hasSize(1); + + // return analysis from 2008-11-20 + Period period = periods.get(0); + assertThat(period.getMode()).isEqualTo(CoreProperties.TIMEMACHINE_MODE_DAYS); + assertThat(period.getModeParameter()).isEqualTo("10"); + assertThat(period.getSnapshotDate()).isEqualTo(1227157200000L); + + assertThat(logTester.logs()).hasSize(1); + assertThat(logTester.logs().get(0)).startsWith("Compare over 10 days (2008-11-20, analysis of "); + } + + @Test + public void no_period_by_days() throws Exception { + dbTester.prepareDbUnit(getClass(), "empty.xml"); + + settings.setProperty("sonar.timemachine.period1", "0"); + + sut.execute(); + assertThat(periodsHolder.getPeriods()).isEmpty(); + } + + @Test + public void feed_period_by_previous_analysis() throws Exception { + dbTester.prepareDbUnit(getClass(), "shared.xml"); + + settings.setProperty("sonar.timemachine.period1", "previous_analysis"); + + sut.execute(); + List periods = periodsHolder.getPeriods(); + assertThat(periods).hasSize(1); + + // return analysis from 2008-11-29 + Period period = periods.get(0); + assertThat(period.getMode()).isEqualTo(CoreProperties.TIMEMACHINE_MODE_PREVIOUS_ANALYSIS); + assertThat(period.getModeParameter()).isNotNull(); + assertThat(period.getSnapshotDate()).isEqualTo(1227934800000L); + + assertThat(logTester.logs()).hasSize(1); + assertThat(logTester.logs().get(0)).startsWith("Compare to previous analysis ("); + } + + @Test + public void no_period_by_previous_analysis() throws Exception { + dbTester.prepareDbUnit(getClass(), "empty.xml"); + + settings.setProperty("sonar.timemachine.period1", "previous_analysis"); + + sut.execute(); + assertThat(periodsHolder.getPeriods()).isEmpty(); + } + + @Test + public void feed_period_by_previous_version() throws Exception { + dbTester.prepareDbUnit(getClass(), "shared.xml"); + + settings.setProperty("sonar.timemachine.period1", "previous_version"); + + sut.execute(); + List periods = periodsHolder.getPeriods(); + assertThat(periods).hasSize(1); + + // Analysis form 2008-11-12 + Period period = periods.get(0); + assertThat(period.getMode()).isEqualTo(CoreProperties.TIMEMACHINE_MODE_PREVIOUS_VERSION); + assertThat(period.getModeParameter()).isEqualTo("1.0"); + assertThat(period.getSnapshotDate()).isEqualTo(1226494680000L); + + assertThat(logTester.logs()).hasSize(1); + assertThat(logTester.logs().get(0)).startsWith("Compare to previous version ("); + } + + @Test + public void feed_period_by_previous_version_wit_previous_version_deleted() throws Exception { + dbTester.prepareDbUnit(getClass(), "previous_version_deleted.xml"); + + settings.setProperty("sonar.timemachine.period1", "previous_version"); + + sut.execute(); + List periods = periodsHolder.getPeriods(); + assertThat(periods).hasSize(1); + + // Analysis form 2008-11-11 + Period period = periods.get(0); + assertThat(period.getMode()).isEqualTo(CoreProperties.TIMEMACHINE_MODE_PREVIOUS_VERSION); + assertThat(period.getModeParameter()).isEqualTo("0.9"); + assertThat(period.getSnapshotDate()).isEqualTo(1226379600000L); + } + + @Test + public void no_period_by_previous_version() throws Exception { + dbTester.prepareDbUnit(getClass(), "empty.xml"); + + settings.setProperty("sonar.timemachine.period1", "previous_version"); + + sut.execute(); + assertThat(periodsHolder.getPeriods()).isEmpty(); + } + + @Test + public void no_period_by_previous_version_when_no_event_version() throws Exception { + dbTester.prepareDbUnit(getClass(), "no_previous_version.xml"); + + settings.setProperty("sonar.timemachine.period1", "previous_version"); + + sut.execute(); + assertThat(periodsHolder.getPeriods()).isEmpty(); + } + + @Test + public void feed_period_by_version() throws Exception { + dbTester.prepareDbUnit(getClass(), "shared.xml"); + + settings.setProperty("sonar.timemachine.period1", "0.9"); + + sut.execute(); + List periods = periodsHolder.getPeriods(); + assertThat(periods).hasSize(1); + + // Analysis form 2008-11-11 + Period period = periods.get(0); + assertThat(period.getMode()).isEqualTo(CoreProperties.TIMEMACHINE_MODE_VERSION); + assertThat(period.getModeParameter()).isEqualTo("0.9"); + assertThat(period.getSnapshotDate()).isEqualTo(1226379600000L); + + assertThat(logTester.logs()).hasSize(1); + assertThat(logTester.logs().get(0)).startsWith("Compare to version (0.9) ("); + } + + @Test + public void no_period_by_version() throws Exception { + dbTester.prepareDbUnit(getClass(), "empty.xml"); + + settings.setProperty("sonar.timemachine.period1", "0.8"); + + sut.execute(); + assertThat(periodsHolder.getPeriods()).isEmpty(); + } + + @Test + public void feed_five_different_periods() throws Exception { dbTester.prepareDbUnit(getClass(), "shared.xml"); settings.setProperty("sonar.timemachine.period1", "2008-11-22"); // Analysis from 2008-11-22 should be returned @@ -181,23 +387,23 @@ public class FeedPeriodsStepTest extends BaseStepTest { assertThat(periods.get(0).getMode()).isEqualTo(CoreProperties.TIMEMACHINE_MODE_DATE); assertThat(periods.get(0).getIndex()).isEqualTo(1); - assertThat(periods.get(0).getProjectSnapshot().getId()).isEqualTo(1003L); + assertThat(periods.get(0).getSnapshotDate()).isEqualTo(1227358680000L); assertThat(periods.get(1).getMode()).isEqualTo(CoreProperties.TIMEMACHINE_MODE_DAYS); assertThat(periods.get(1).getIndex()).isEqualTo(2); - assertThat(periods.get(1).getProjectSnapshot().getId()).isEqualTo(1002L); + assertThat(periods.get(1).getSnapshotDate()).isEqualTo(1227157200000L); assertThat(periods.get(2).getMode()).isEqualTo(CoreProperties.TIMEMACHINE_MODE_PREVIOUS_ANALYSIS); assertThat(periods.get(2).getIndex()).isEqualTo(3); - assertThat(periods.get(2).getProjectSnapshot().getId()).isEqualTo(1004L); + assertThat(periods.get(2).getSnapshotDate()).isEqualTo(1227934800000L); assertThat(periods.get(3).getMode()).isEqualTo(CoreProperties.TIMEMACHINE_MODE_PREVIOUS_VERSION); assertThat(periods.get(3).getIndex()).isEqualTo(4); - assertThat(periods.get(3).getProjectSnapshot().getId()).isEqualTo(1001L); + assertThat(periods.get(3).getSnapshotDate()).isEqualTo(1226494680000L); assertThat(periods.get(4).getMode()).isEqualTo(CoreProperties.TIMEMACHINE_MODE_VERSION); assertThat(periods.get(4).getIndex()).isEqualTo(5); - assertThat(periods.get(4).getProjectSnapshot().getId()).isEqualTo(1000L); + assertThat(periods.get(4).getSnapshotDate()).isEqualTo(1226379600000L); } @Test diff --git a/server/sonar-server/src/test/resources/org/sonar/server/computation/period/PeriodFinderTest/previous_version_deleted.xml b/server/sonar-server/src/test/resources/org/sonar/server/computation/period/PeriodFinderTest/previous_version_deleted.xml deleted file mode 100644 index 0e7ea64c198..00000000000 --- a/server/sonar-server/src/test/resources/org/sonar/server/computation/period/PeriodFinderTest/previous_version_deleted.xml +++ /dev/null @@ -1,46 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - diff --git a/server/sonar-server/src/test/resources/org/sonar/server/computation/step/FeedPeriodsStepTest/empty.xml b/server/sonar-server/src/test/resources/org/sonar/server/computation/step/FeedPeriodsStepTest/empty.xml new file mode 100644 index 00000000000..9a36ce9a899 --- /dev/null +++ b/server/sonar-server/src/test/resources/org/sonar/server/computation/step/FeedPeriodsStepTest/empty.xml @@ -0,0 +1,7 @@ + + + + + diff --git a/server/sonar-server/src/test/resources/org/sonar/server/computation/period/PeriodFinderTest/find_by_date.xml b/server/sonar-server/src/test/resources/org/sonar/server/computation/step/FeedPeriodsStepTest/find_by_date.xml similarity index 100% rename from server/sonar-server/src/test/resources/org/sonar/server/computation/period/PeriodFinderTest/find_by_date.xml rename to server/sonar-server/src/test/resources/org/sonar/server/computation/step/FeedPeriodsStepTest/find_by_date.xml diff --git a/server/sonar-server/src/test/resources/org/sonar/server/computation/period/PeriodFinderTest/find_by_days.xml b/server/sonar-server/src/test/resources/org/sonar/server/computation/step/FeedPeriodsStepTest/find_by_days.xml similarity index 100% rename from server/sonar-server/src/test/resources/org/sonar/server/computation/period/PeriodFinderTest/find_by_days.xml rename to server/sonar-server/src/test/resources/org/sonar/server/computation/step/FeedPeriodsStepTest/find_by_days.xml diff --git a/server/sonar-server/src/test/resources/org/sonar/server/computation/period/PeriodFinderTest/find_by_previous_analysis.xml b/server/sonar-server/src/test/resources/org/sonar/server/computation/step/FeedPeriodsStepTest/find_by_previous_analysis.xml similarity index 100% rename from server/sonar-server/src/test/resources/org/sonar/server/computation/period/PeriodFinderTest/find_by_previous_analysis.xml rename to server/sonar-server/src/test/resources/org/sonar/server/computation/step/FeedPeriodsStepTest/find_by_previous_analysis.xml diff --git a/server/sonar-server/src/test/resources/org/sonar/server/computation/period/PeriodFinderTest/find_by_previous_version.xml b/server/sonar-server/src/test/resources/org/sonar/server/computation/step/FeedPeriodsStepTest/find_by_previous_version.xml similarity index 100% rename from server/sonar-server/src/test/resources/org/sonar/server/computation/period/PeriodFinderTest/find_by_previous_version.xml rename to server/sonar-server/src/test/resources/org/sonar/server/computation/step/FeedPeriodsStepTest/find_by_previous_version.xml diff --git a/server/sonar-server/src/test/resources/org/sonar/server/computation/period/PeriodFinderTest/find_by_version.xml b/server/sonar-server/src/test/resources/org/sonar/server/computation/step/FeedPeriodsStepTest/find_by_version.xml similarity index 100% rename from server/sonar-server/src/test/resources/org/sonar/server/computation/period/PeriodFinderTest/find_by_version.xml rename to server/sonar-server/src/test/resources/org/sonar/server/computation/step/FeedPeriodsStepTest/find_by_version.xml diff --git a/server/sonar-server/src/test/resources/org/sonar/server/computation/period/PeriodFinderTest/no_previous_version.xml b/server/sonar-server/src/test/resources/org/sonar/server/computation/step/FeedPeriodsStepTest/no_previous_version.xml similarity index 100% rename from server/sonar-server/src/test/resources/org/sonar/server/computation/period/PeriodFinderTest/no_previous_version.xml rename to server/sonar-server/src/test/resources/org/sonar/server/computation/step/FeedPeriodsStepTest/no_previous_version.xml diff --git a/server/sonar-server/src/test/resources/org/sonar/server/computation/step/FeedPeriodsStepTest/previous_version_deleted.xml b/server/sonar-server/src/test/resources/org/sonar/server/computation/step/FeedPeriodsStepTest/previous_version_deleted.xml new file mode 100644 index 00000000000..2c000e2c4a6 --- /dev/null +++ b/server/sonar-server/src/test/resources/org/sonar/server/computation/step/FeedPeriodsStepTest/previous_version_deleted.xml @@ -0,0 +1,39 @@ + + + + + + + + + + + + + + + + + + + + + + + diff --git a/server/sonar-server/src/test/resources/org/sonar/server/computation/step/FeedPeriodsStepTest/unprocessed_snapshots.xml b/server/sonar-server/src/test/resources/org/sonar/server/computation/step/FeedPeriodsStepTest/unprocessed_snapshots.xml new file mode 100644 index 00000000000..848506944cc --- /dev/null +++ b/server/sonar-server/src/test/resources/org/sonar/server/computation/step/FeedPeriodsStepTest/unprocessed_snapshots.xml @@ -0,0 +1,15 @@ + + + + + + + + -- 2.39.5