From: Julien Lancelot Date: Thu, 30 May 2013 09:06:08 +0000 (+0200) Subject: SONAR-4334 Add date of the latest snapshot in the error message displayed to user X-Git-Tag: 3.6~188 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=f0ab4532da5784d44f110240aadbf049cf588bcb;p=sonarqube.git SONAR-4334 Add date of the latest snapshot in the error message displayed to user --- diff --git a/sonar-batch/src/main/java/org/sonar/batch/ProjectConfigurator.java b/sonar-batch/src/main/java/org/sonar/batch/ProjectConfigurator.java index d5caa5bde19..40d12a7fd28 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/ProjectConfigurator.java +++ b/sonar-batch/src/main/java/org/sonar/batch/ProjectConfigurator.java @@ -30,6 +30,7 @@ import org.sonar.api.database.DatabaseSession; import org.sonar.api.database.model.ResourceModel; import org.sonar.api.database.model.Snapshot; import org.sonar.api.resources.Project; +import org.sonar.api.utils.DateUtils; import org.sonar.api.utils.SonarException; import java.util.Date; @@ -67,7 +68,7 @@ public class ProjectConfigurator implements BatchComponent { public ProjectConfigurator configure(Project project) { Date analysisDate = loadAnalysisDate(); - checkCurrentAnalyisIsTheLatestOne(project, analysisDate); + checkCurrentAnalyisIsTheLatestOne(project.getKey(), analysisDate); project .setConfiguration(new PropertiesConfiguration()) // will be populated by ProjectSettings @@ -77,22 +78,19 @@ public class ProjectConfigurator implements BatchComponent { return this; } - private void checkCurrentAnalyisIsTheLatestOne(Project project, Date analysisDate) { - if (!isLatestAnalysis(project.getKey(), analysisDate)) { - throw new IllegalArgumentException( - "The value '" + settings.getString(CoreProperties.PROJECT_DATE_PROPERTY) + "' of the sonar.projectDate property " + - "can't be older than the date of last known quality snapshot " + - "on this project. This property must only be used to rebuild the past in a chronological order."); - } - } - - boolean isLatestAnalysis(String projectKey, Date analysisDate) { + private void checkCurrentAnalyisIsTheLatestOne(String projectKey, Date analysisDate) { ResourceModel persistedProject = databaseSession.getSingleResult(ResourceModel.class, "key", projectKey, "enabled", true); if (persistedProject != null) { Snapshot lastSnapshot = databaseSession.getSingleResult(Snapshot.class, "resourceId", persistedProject.getId(), "last", true); - return lastSnapshot == null || lastSnapshot.getCreatedAt().before(analysisDate); + Date lastSnapshotCreationDate = lastSnapshot.getCreatedAt(); + if (lastSnapshot != null && !lastSnapshotCreationDate.before(analysisDate)) { + throw new IllegalArgumentException( + "'sonar.projectDate' property cannot be older than the date of the last known quality snapshot on this project. Value: '"+ settings.getString(CoreProperties.PROJECT_DATE_PROPERTY) + "'. " + + "Latest quality snapshot: '"+ DateUtils.formatDate(lastSnapshotCreationDate) +"'. This property may only be used to rebuild the past in a chronological order." + ); + } } - return true; + } Date loadAnalysisDate() { diff --git a/sonar-batch/src/test/java/org/sonar/batch/ProjectConfiguratorTest.java b/sonar-batch/src/test/java/org/sonar/batch/ProjectConfiguratorTest.java index ce13c0b7177..9ac9408c8bd 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/ProjectConfiguratorTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/ProjectConfiguratorTest.java @@ -154,8 +154,8 @@ public class ProjectConfiguratorTest extends AbstractDbUnitTestCase { new ProjectConfigurator(getSession(), configuration).configure(project); fail(); } catch (Exception e) { - assertThat(e).isInstanceOf(IllegalArgumentException.class).hasMessage("The value '2005-12-25' of the sonar.projectDate property can't be older than the date " + - "of last known quality snapshot on this project. This property must only be used to rebuild the past in a chronological order."); + assertThat(e).isInstanceOf(IllegalArgumentException.class).hasMessage("'sonar.projectDate' property cannot be older than the date of the last known quality snapshot on this project. " + + "Value: '2005-12-25'. Latest quality snapshot: '2010-12-02'. This property may only be used to rebuild the past in a chronological order."); } }