]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-8094 add PostProjectAnalysisTask.ProjectAnalysis#getAnalysisDate
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Mon, 19 Sep 2016 11:51:59 +0000 (13:51 +0200)
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Mon, 19 Sep 2016 12:59:56 +0000 (14:59 +0200)
server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/api/posttask/PostProjectAnalysisTasksExecutor.java
server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/api/posttask/PostProjectAnalysisTasksExecutorTest.java
sonar-plugin-api/src/main/java/org/sonar/api/ce/posttask/PostProjectAnalysisTask.java
sonar-plugin-api/src/main/java/org/sonar/api/ce/posttask/PostProjectAnalysisTaskTester.java
sonar-plugin-api/src/test/java/org/sonar/api/ce/posttask/PostProjectAnalysisTaskTesterTest.java

index 563bacd42097c93a2bc1b7c1c260bb959020c6b5..1fed2f087ceb0603045ce3958a8dc62155668adf 100644 (file)
@@ -98,10 +98,12 @@ public class PostProjectAnalysisTasksExecutor implements ComputationStepExecutor
   }
 
   private ProjectAnalysis createProjectAnalysis(CeTask.Status status) {
+    Long analysisDate = getAnalysisDate();
     return new ProjectAnalysis(
       new CeTaskImpl(this.ceTask.getUuid(), status),
       createProject(this.ceTask),
-      getAnalysisDate(),
+      analysisDate,
+      analysisDate == null ? system2.now() : analysisDate,
       ScannerContextImpl.from(reportReader.readContextProperties()),
       status == SUCCESS ? createQualityGate(this.qualityGateHolder) : null);
   }
@@ -113,11 +115,12 @@ public class PostProjectAnalysisTasksExecutor implements ComputationStepExecutor
       ceTask.getComponentName());
   }
 
-  private long getAnalysisDate() {
+  @CheckForNull
+  private Long getAnalysisDate() {
     if (this.analysisMetadataHolder.hasAnalysisDateBeenSet()) {
       return this.analysisMetadataHolder.getAnalysisDate();
     }
-    return system2.now();
+    return null;
   }
 
   @CheckForNull
@@ -159,15 +162,19 @@ public class PostProjectAnalysisTasksExecutor implements ComputationStepExecutor
   private static class ProjectAnalysis implements PostProjectAnalysisTask.ProjectAnalysis {
     private final CeTask ceTask;
     private final Project project;
+    @CheckForNull
+    private final Long analysisDate;
     private final long date;
     private final ScannerContext scannerContext;
     @CheckForNull
     private final QualityGate qualityGate;
 
-    private ProjectAnalysis(CeTask ceTask, Project project, long date,
+    private ProjectAnalysis(CeTask ceTask, Project project,
+      @Nullable Long analysisDate, 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.analysisDate = analysisDate;
       this.date = date;
       this.scannerContext = requireNonNull(scannerContext, "scannerContext can not be null");
       this.qualityGate = qualityGate;
@@ -194,6 +201,14 @@ public class PostProjectAnalysisTasksExecutor implements ComputationStepExecutor
       return new Date(date);
     }
 
+    @Override
+    public java.util.Optional<Date> getAnalysisDate() {
+      if (analysisDate == null) {
+        return java.util.Optional.empty();
+      }
+      return java.util.Optional.of(new Date(analysisDate));
+    }
+
     @Override
     public ScannerContext getScannerContext() {
       return scannerContext;
@@ -204,7 +219,9 @@ public class PostProjectAnalysisTasksExecutor implements ComputationStepExecutor
       return "ProjectAnalysis{" +
         "ceTask=" + ceTask +
         ", project=" + project +
+        ", analysisDate=" + analysisDate +
         ", date=" + date +
+        ", scannerContext=" + scannerContext +
         ", qualityGate=" + qualityGate +
         '}';
     }
index 88b0e70e5aafd9934c6793485f0e02eb6af3d695..46134497eda4407622766a78b4b5e3bbdbfe81f3 100644 (file)
@@ -157,7 +157,7 @@ public class PostProjectAnalysisTasksExecutorTest {
   }
 
   @Test
-  public void analysisDate_comes_from_AnalysisMetadataHolder() {
+  public void date_comes_from_AnalysisMetadataHolder() {
     analysisMetadataHolder.setAnalysisDate(8465132498L);
 
     underTest.finished(true);
@@ -169,7 +169,7 @@ public class PostProjectAnalysisTasksExecutorTest {
   }
 
   @Test
-  public void analysisDate_comes_from_system2_if_not_set_in_AnalysisMetadataHolder() {
+  public void date_comes_from_system2_if_not_set_in_AnalysisMetadataHolder() {
     long now = 1_999_663L;
     when(system2.now()).thenReturn(now);
 
@@ -180,6 +180,27 @@ public class PostProjectAnalysisTasksExecutorTest {
     assertThat(projectAnalysisArgumentCaptor.getValue().getDate()).isEqualTo(new Date(now));
   }
 
+  @Test
+  public void analysisDate_comes_from_AnalysisMetadataHolder_when_set() {
+    analysisMetadataHolder.setAnalysisDate(8465132498L);
+
+    underTest.finished(true);
+
+    verify(postProjectAnalysisTask).finished(projectAnalysisArgumentCaptor.capture());
+
+    assertThat(projectAnalysisArgumentCaptor.getValue().getAnalysisDate())
+      .contains(new Date(analysisMetadataHolder.getAnalysisDate()));
+  }
+
+  @Test
+  public void analysisDate_is_empty_when_not_set_in_AnalysisMetadataHolder() {
+    underTest.finished(false);
+
+    verify(postProjectAnalysisTask).finished(projectAnalysisArgumentCaptor.capture());
+
+    assertThat(projectAnalysisArgumentCaptor.getValue().getAnalysisDate()).isEmpty();
+  }
+
   @Test
   public void qualityGate_is_null_when_finished_method_argument_is_false() {
     underTest.finished(false);
index b4ed6ea896385b254844a869a611705613bc97ad..5f196675f3829a57f97b46c81e87e7fa826456ac 100644 (file)
@@ -20,6 +20,7 @@
 package org.sonar.api.ce.posttask;
 
 import java.util.Date;
+import java.util.Optional;
 import javax.annotation.CheckForNull;
 import org.sonar.api.ExtensionPoint;
 import org.sonar.api.ce.ComputeEngineSide;
@@ -71,10 +72,22 @@ public interface PostProjectAnalysisTask {
      * Date of the analysis.
      * <p>
      * This date is the same as the date of the project analysis report and the snapshot.
-     * 
+     *
+     * @deprecated use {@link #getAnalysisDate()} instead. When {@link #getAnalysisDate()} returns
+     *             {@link Optional#empty() empty}, the current date will be returned.
      */
+    @Deprecated
     Date getDate();
 
+    /**
+     * Date of the analysis.
+     * <p>
+     * This date is the same as the date of the project analysis report and therefore as the analysis in DB. It can be
+     * missing when the status of the task is {@link org.sonar.api.ce.posttask.CeTask.Status#FAILED FAILED}.
+     * </p>
+     */
+    Optional<Date> getAnalysisDate();
+
     /**
      * Context as defined by scanner through {@link org.sonar.api.batch.sensor.SensorContext#addContextProperty(String, String)}.
      * It does not contain the settings used by scanner.
index 394ce62b7a43fbec4f2ffcfe41d26b1219bfc774..6c00a4d3bb1eecc1584a12e1123c0dbafbf78824 100644 (file)
@@ -25,6 +25,7 @@ import java.util.Date;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.Optional;
 import javax.annotation.CheckForNull;
 import javax.annotation.Nullable;
 
@@ -196,12 +197,18 @@ public class PostProjectAnalysisTaskTester {
           return date;
         }
 
+        @Override
+        public Optional<Date> getAnalysisDate() {
+          return Optional.of(date);
+        }
+
         @Override
         public String toString() {
           return "ProjectAnalysis{" +
             "ceTask=" + ceTask +
             ", project=" + project +
             ", date=" + date.getTime() +
+            ", analysisDate=" + date.getTime() +
             ", qualityGate=" + qualityGate +
             '}';
         }
index 3b1eddb7a744687a84f988ac9758ce6f827899f2..029ae39f766cf6cb4d7ab3a438423b97a3cd032a 100644 (file)
@@ -121,7 +121,7 @@ public class PostProjectAnalysisTaskTesterTest {
     underTest.execute();
 
     assertThat(captorPostProjectAnalysisTask.projectAnalysis.toString())
-      .isEqualTo("ProjectAnalysis{ceTask=CeTask, project=Project, date=846351351684351, qualityGate=QualityGate}");
+      .isEqualTo("ProjectAnalysis{ceTask=CeTask, project=Project, date=846351351684351, analysisDate=846351351684351, qualityGate=QualityGate}");
 
   }