]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-8093 PostTask API: don't fail when analysis date is not set
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Mon, 19 Sep 2016 09:57:21 +0000 (11:57 +0200)
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Mon, 19 Sep 2016 12:10:21 +0000 (14:10 +0200)
server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/analysis/AnalysisMetadataHolder.java
server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/analysis/AnalysisMetadataHolderImpl.java
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/analysis/AnalysisMetadataHolderImplTest.java
server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/analysis/AnalysisMetadataHolderRule.java
server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/analysis/MutableAnalysisMetadataHolderRule.java
server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/api/posttask/PostProjectAnalysisTasksExecutorTest.java

index 43d6c7a06d94edccf00634247c6120a13c52aefa..82c0ea9b4d7ff63c68b2e4624ff558e7ae93e9ec 100644 (file)
@@ -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}
    *
index 6ce238ab4d307178fa7d1fd05e318af73201b955..a8cd6c05c9b22432bb52cf273a55d2d4481766fd 100644 (file)
@@ -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;
index 10d5dda0a9732b158b9d242ab07c5f0e6f0ed1ce..563bacd42097c93a2bc1b7c1c260bb959020c6b5 100644 (file)
@@ -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
index a4a4e363d7d73873fe33fe1b82bde39af8b3ab87..008f2a48e147e9754adb452c58a22c9671ec27f5 100644 (file)
@@ -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();
index f12d92f4565e851fbde70f7373ff62dcac36ce3a..787f7f880ee5fb1cfd85d7dc613f7cf301466e62 100644 (file)
@@ -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;
index 17c5350ecb9c7d883d5ca3a37c7b317514941112..762aea97bb985aa62236a4bf7c6e9b8dbbd71c11 100644 (file)
@@ -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;
index 52237137b3fe32d5ed11bb0e5323c2be89af6d30..88b0e70e5aafd9934c6793485f0e02eb6af3d695 100644 (file)
@@ -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<PostProjectAnalysisTask.ProjectAnalysis> 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);