From a15a1d788bc50a48ecda864102431d1ed1cc5a4f Mon Sep 17 00:00:00 2001 From: =?utf8?q?S=C3=A9bastien=20Lesaint?= Date: Mon, 19 Sep 2016 11:57:21 +0200 Subject: [PATCH] SONAR-8093 PostTask API: don't fail when analysis date is not set --- .../analysis/AnalysisMetadataHolder.java | 5 ++++ .../analysis/AnalysisMetadataHolderImpl.java | 5 ++++ .../PostProjectAnalysisTasksExecutor.java | 25 ++++++++++------- .../AnalysisMetadataHolderImplTest.java | 12 +++++++++ .../analysis/AnalysisMetadataHolderRule.java | 5 ++++ .../MutableAnalysisMetadataHolderRule.java | 5 ++++ .../PostProjectAnalysisTasksExecutorTest.java | 27 ++++++++++++++----- 7 files changed, 69 insertions(+), 15 deletions(-) diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/analysis/AnalysisMetadataHolder.java b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/analysis/AnalysisMetadataHolder.java index 43d6c7a06d9..82c0ea9b4d7 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/analysis/AnalysisMetadataHolder.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/analysis/AnalysisMetadataHolder.java @@ -36,6 +36,11 @@ public interface AnalysisMetadataHolder { */ long getAnalysisDate(); + /** + * Tell whether the analysisDate has been set. + */ + boolean hasAnalysisDateBeenSet(); + /** * Convenience method equivalent to calling {@link #getBaseAnalysis() == null} * diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/analysis/AnalysisMetadataHolderImpl.java b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/analysis/AnalysisMetadataHolderImpl.java index 6ce238ab4d3..a8cd6c05c9b 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/analysis/AnalysisMetadataHolderImpl.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/analysis/AnalysisMetadataHolderImpl.java @@ -70,6 +70,11 @@ public class AnalysisMetadataHolderImpl implements MutableAnalysisMetadataHolder return this.analysisDate.getProperty(); } + @Override + public boolean hasAnalysisDateBeenSet() { + return analysisDate.isInitialized(); + } + @Override public boolean isFirstAnalysis() { return getBaseAnalysis() == null; diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/api/posttask/PostProjectAnalysisTasksExecutor.java b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/api/posttask/PostProjectAnalysisTasksExecutor.java index 10d5dda0a97..563bacd4209 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/api/posttask/PostProjectAnalysisTasksExecutor.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/api/posttask/PostProjectAnalysisTasksExecutor.java @@ -31,6 +31,7 @@ import org.sonar.api.ce.posttask.PostProjectAnalysisTask; import org.sonar.api.ce.posttask.Project; import org.sonar.api.ce.posttask.QualityGate; import org.sonar.api.ce.posttask.ScannerContext; +import org.sonar.api.utils.System2; import org.sonar.server.computation.task.projectanalysis.analysis.AnalysisMetadataHolder; import org.sonar.server.computation.task.projectanalysis.batch.BatchReportReader; import org.sonar.server.computation.task.projectanalysis.qualitygate.Condition; @@ -58,6 +59,7 @@ public class PostProjectAnalysisTasksExecutor implements ComputationStepExecutor private final QualityGateStatusHolder qualityGateStatusHolder; private final PostProjectAnalysisTask[] postProjectAnalysisTasks; private final BatchReportReader reportReader; + private final System2 system2; /** * Constructor used by Pico when there is no {@link PostProjectAnalysisTask} in the container. @@ -65,13 +67,14 @@ public class PostProjectAnalysisTasksExecutor implements ComputationStepExecutor public PostProjectAnalysisTasksExecutor(org.sonar.ce.queue.CeTask ceTask, AnalysisMetadataHolder analysisMetadataHolder, QualityGateHolder qualityGateHolder, QualityGateStatusHolder qualityGateStatusHolder, - BatchReportReader reportReader) { - this(ceTask, analysisMetadataHolder, qualityGateHolder, qualityGateStatusHolder, reportReader, null); + BatchReportReader reportReader, System2 system2) { + this(ceTask, analysisMetadataHolder, qualityGateHolder, qualityGateStatusHolder, reportReader, system2, null); } public PostProjectAnalysisTasksExecutor(org.sonar.ce.queue.CeTask ceTask, AnalysisMetadataHolder analysisMetadataHolder, - QualityGateHolder qualityGateHolder, QualityGateStatusHolder qualityGateStatusHolder, BatchReportReader reportReader, + QualityGateHolder qualityGateHolder, QualityGateStatusHolder qualityGateStatusHolder, + BatchReportReader reportReader, System2 system2, @Nullable PostProjectAnalysisTask[] postProjectAnalysisTasks) { this.analysisMetadataHolder = analysisMetadataHolder; this.qualityGateHolder = qualityGateHolder; @@ -79,6 +82,7 @@ public class PostProjectAnalysisTasksExecutor implements ComputationStepExecutor this.ceTask = ceTask; this.reportReader = reportReader; this.postProjectAnalysisTasks = postProjectAnalysisTasks == null ? NO_POST_PROJECT_ANALYSIS_TASKS : postProjectAnalysisTasks; + this.system2 = system2; } @Override @@ -109,8 +113,11 @@ public class PostProjectAnalysisTasksExecutor implements ComputationStepExecutor ceTask.getComponentName()); } - private Date getAnalysisDate() { - return new Date(this.analysisMetadataHolder.getAnalysisDate()); + private long getAnalysisDate() { + if (this.analysisMetadataHolder.hasAnalysisDateBeenSet()) { + return this.analysisMetadataHolder.getAnalysisDate(); + } + return system2.now(); } @CheckForNull @@ -152,16 +159,16 @@ public class PostProjectAnalysisTasksExecutor implements ComputationStepExecutor private static class ProjectAnalysis implements PostProjectAnalysisTask.ProjectAnalysis { private final CeTask ceTask; private final Project project; - private final Date date; + private final long date; private final ScannerContext scannerContext; @CheckForNull private final QualityGate qualityGate; - private ProjectAnalysis(CeTask ceTask, Project project, Date date, + private ProjectAnalysis(CeTask ceTask, Project project, long date, ScannerContext scannerContext, @Nullable QualityGate qualityGate) { this.ceTask = requireNonNull(ceTask, "ceTask can not be null"); this.project = requireNonNull(project, "project can not be null"); - this.date = requireNonNull(date, "date can not be null"); + this.date = date; this.scannerContext = requireNonNull(scannerContext, "scannerContext can not be null"); this.qualityGate = qualityGate; } @@ -184,7 +191,7 @@ public class PostProjectAnalysisTasksExecutor implements ComputationStepExecutor @Override public Date getDate() { - return date; + return new Date(date); } @Override diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/analysis/AnalysisMetadataHolderImplTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/analysis/AnalysisMetadataHolderImplTest.java index a4a4e363d7d..008f2a48e14 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/analysis/AnalysisMetadataHolderImplTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/analysis/AnalysisMetadataHolderImplTest.java @@ -66,6 +66,18 @@ public class AnalysisMetadataHolderImplTest { underTest.setAnalysisDate(SOME_DATE); } + @Test + public void hasAnalysisDateBeenSet_returns_false_when_holder_is_not_initialized() { + assertThat(new AnalysisMetadataHolderImpl().hasAnalysisDateBeenSet()).isFalse(); + } + + @Test + public void hasAnalysisDateBeenSet_returns_true_when_holder_date_is_set() { + AnalysisMetadataHolderImpl holder = new AnalysisMetadataHolderImpl(); + holder.setAnalysisDate(46532); + assertThat(holder.hasAnalysisDateBeenSet()).isTrue(); + } + @Test public void isFirstAnalysis_return_true() throws Exception { AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl(); diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/analysis/AnalysisMetadataHolderRule.java b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/analysis/AnalysisMetadataHolderRule.java index f12d92f4565..787f7f880ee 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/analysis/AnalysisMetadataHolderRule.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/analysis/AnalysisMetadataHolderRule.java @@ -78,6 +78,11 @@ public class AnalysisMetadataHolderRule extends ExternalResource implements Muta return this.analysisDate.getProperty(); } + @Override + public boolean hasAnalysisDateBeenSet() { + return analysisDate.isInitialized(); + } + @Override public boolean isFirstAnalysis() { return getBaseAnalysis() == null; diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/analysis/MutableAnalysisMetadataHolderRule.java b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/analysis/MutableAnalysisMetadataHolderRule.java index 17c5350ecb9..762aea97bb9 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/analysis/MutableAnalysisMetadataHolderRule.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/analysis/MutableAnalysisMetadataHolderRule.java @@ -49,6 +49,11 @@ public class MutableAnalysisMetadataHolderRule extends ExternalResource implemen return delegate.getAnalysisDate(); } + @Override + public boolean hasAnalysisDateBeenSet() { + return delegate.hasAnalysisDateBeenSet(); + } + public MutableAnalysisMetadataHolderRule setAnalysisDate(long date) { delegate.setAnalysisDate(date); return this; diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/api/posttask/PostProjectAnalysisTasksExecutorTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/api/posttask/PostProjectAnalysisTasksExecutorTest.java index 52237137b3f..88b0e70e5aa 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/api/posttask/PostProjectAnalysisTasksExecutorTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/api/posttask/PostProjectAnalysisTasksExecutorTest.java @@ -33,6 +33,7 @@ import org.mockito.ArgumentCaptor; import org.mockito.InOrder; import org.sonar.api.ce.posttask.PostProjectAnalysisTask; import org.sonar.api.ce.posttask.Project; +import org.sonar.api.utils.System2; import org.sonar.ce.queue.CeTask; import org.sonar.scanner.protocol.output.ScannerReport; import org.sonar.server.computation.task.projectanalysis.analysis.AnalysisMetadataHolderRule; @@ -62,16 +63,15 @@ public class PostProjectAnalysisTasksExecutorTest { private static final Condition CONDITION_2 = createCondition("metric key 2"); @Rule - public AnalysisMetadataHolderRule analysisMetadataHolder = new AnalysisMetadataHolderRule() - .setAnalysisDate(8465132498L); + public AnalysisMetadataHolderRule analysisMetadataHolder = new AnalysisMetadataHolderRule(); @Rule public MutableQualityGateHolderRule qualityGateHolder = new MutableQualityGateHolderRule(); @Rule public MutableQualityGateStatusHolderRule qualityGateStatusHolder = new MutableQualityGateStatusHolderRule(); - @Rule public BatchReportReaderRule reportReader = new BatchReportReaderRule(); + private System2 system2 = mock(System2.class); private ArgumentCaptor projectAnalysisArgumentCaptor = ArgumentCaptor.forClass(PostProjectAnalysisTask.ProjectAnalysis.class); private CeTask ceTask = new CeTask.Builder() .setType("type") @@ -83,7 +83,8 @@ public class PostProjectAnalysisTasksExecutorTest { private PostProjectAnalysisTask postProjectAnalysisTask = mock(PostProjectAnalysisTask.class); private PostProjectAnalysisTasksExecutor underTest = new PostProjectAnalysisTasksExecutor( ceTask, analysisMetadataHolder, qualityGateHolder, qualityGateStatusHolder, - reportReader, new PostProjectAnalysisTask[] {postProjectAnalysisTask}); + reportReader, system2, + new PostProjectAnalysisTask[] {postProjectAnalysisTask}); @Before public void setUp() throws Exception { @@ -96,7 +97,7 @@ public class PostProjectAnalysisTasksExecutorTest { @Test @UseDataProvider("booleanValues") public void does_not_fail_when_there_is_no_PostProjectAnalysisTasksExecutor(boolean allStepsExecuted) { - new PostProjectAnalysisTasksExecutor(ceTask, analysisMetadataHolder, qualityGateHolder, qualityGateStatusHolder, reportReader) + new PostProjectAnalysisTasksExecutor(ceTask, analysisMetadataHolder, qualityGateHolder, qualityGateStatusHolder, reportReader, system2) .finished(allStepsExecuted); } @@ -109,7 +110,7 @@ public class PostProjectAnalysisTasksExecutorTest { new PostProjectAnalysisTasksExecutor( ceTask, analysisMetadataHolder, qualityGateHolder, qualityGateStatusHolder, reportReader, - new PostProjectAnalysisTask[] {postProjectAnalysisTask1, postProjectAnalysisTask2}) + system2, new PostProjectAnalysisTask[] {postProjectAnalysisTask1, postProjectAnalysisTask2}) .finished(allStepsExecuted); inOrder.verify(postProjectAnalysisTask1).finished(projectAnalysisArgumentCaptor.capture()); @@ -157,6 +158,8 @@ public class PostProjectAnalysisTasksExecutorTest { @Test public void analysisDate_comes_from_AnalysisMetadataHolder() { + analysisMetadataHolder.setAnalysisDate(8465132498L); + underTest.finished(true); verify(postProjectAnalysisTask).finished(projectAnalysisArgumentCaptor.capture()); @@ -165,6 +168,18 @@ public class PostProjectAnalysisTasksExecutorTest { .isEqualTo(new Date(analysisMetadataHolder.getAnalysisDate())); } + @Test + public void analysisDate_comes_from_system2_if_not_set_in_AnalysisMetadataHolder() { + long now = 1_999_663L; + when(system2.now()).thenReturn(now); + + underTest.finished(false); + + verify(postProjectAnalysisTask).finished(projectAnalysisArgumentCaptor.capture()); + + assertThat(projectAnalysisArgumentCaptor.getValue().getDate()).isEqualTo(new Date(now)); + } + @Test public void qualityGate_is_null_when_finished_method_argument_is_false() { underTest.finished(false); -- 2.39.5