*/
long getAnalysisDate();
+ /**
+ * Tell whether the analysisDate has been set.
+ */
+ boolean hasAnalysisDateBeenSet();
+
/**
* Convenience method equivalent to calling {@link #getBaseAnalysis() == null}
*
return this.analysisDate.getProperty();
}
+ @Override
+ public boolean hasAnalysisDateBeenSet() {
+ return analysisDate.isInitialized();
+ }
+
@Override
public boolean isFirstAnalysis() {
return getBaseAnalysis() == null;
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;
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.
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;
this.ceTask = ceTask;
this.reportReader = reportReader;
this.postProjectAnalysisTasks = postProjectAnalysisTasks == null ? NO_POST_PROJECT_ANALYSIS_TASKS : postProjectAnalysisTasks;
+ this.system2 = system2;
}
@Override
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
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;
}
@Override
public Date getDate() {
- return date;
+ return new Date(date);
}
@Override
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();
return this.analysisDate.getProperty();
}
+ @Override
+ public boolean hasAnalysisDateBeenSet() {
+ return analysisDate.isInitialized();
+ }
+
@Override
public boolean isFirstAnalysis() {
return getBaseAnalysis() == null;
return delegate.getAnalysisDate();
}
+ @Override
+ public boolean hasAnalysisDateBeenSet() {
+ return delegate.hasAnalysisDateBeenSet();
+ }
+
public MutableAnalysisMetadataHolderRule setAnalysisDate(long date) {
delegate.setAnalysisDate(date);
return this;
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;
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<PostProjectAnalysisTask.ProjectAnalysis> projectAnalysisArgumentCaptor = ArgumentCaptor.forClass(PostProjectAnalysisTask.ProjectAnalysis.class);
private CeTask ceTask = new CeTask.Builder()
.setType("type")
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 {
@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);
}
new PostProjectAnalysisTasksExecutor(
ceTask, analysisMetadataHolder, qualityGateHolder, qualityGateStatusHolder, reportReader,
- new PostProjectAnalysisTask[] {postProjectAnalysisTask1, postProjectAnalysisTask2})
+ system2, new PostProjectAnalysisTask[] {postProjectAnalysisTask1, postProjectAnalysisTask2})
.finished(allStepsExecuted);
inOrder.verify(postProjectAnalysisTask1).finished(projectAnalysisArgumentCaptor.capture());
@Test
public void analysisDate_comes_from_AnalysisMetadataHolder() {
+ analysisMetadataHolder.setAnalysisDate(8465132498L);
+
underTest.finished(true);
verify(postProjectAnalysisTask).finished(projectAnalysisArgumentCaptor.capture());
.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);