]> source.dussan.org Git - sonarqube.git/commitdiff
Replace AnalysisMetadataHolder analysis date object by long
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Thu, 12 Nov 2015 09:23:06 +0000 (10:23 +0100)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Thu, 12 Nov 2015 10:01:31 +0000 (11:01 +0100)
18 files changed:
server/sonar-server/src/main/java/org/sonar/server/computation/analysis/AnalysisMetadataHolder.java
server/sonar-server/src/main/java/org/sonar/server/computation/analysis/AnalysisMetadataHolderImpl.java
server/sonar-server/src/main/java/org/sonar/server/computation/analysis/MutableAnalysisMetadataHolder.java
server/sonar-server/src/main/java/org/sonar/server/computation/issue/IssueLifecycle.java
server/sonar-server/src/main/java/org/sonar/server/computation/step/LoadPeriodsStep.java
server/sonar-server/src/main/java/org/sonar/server/computation/step/LoadReportAnalysisMetadataHolderStep.java
server/sonar-server/src/main/java/org/sonar/server/computation/step/PersistEventsStep.java
server/sonar-server/src/main/java/org/sonar/server/computation/step/PersistSnapshotsStep.java
server/sonar-server/src/main/java/org/sonar/server/computation/step/SendIssueNotificationsStep.java
server/sonar-server/src/main/java/org/sonar/server/computation/step/ValidateProjectStep.java
server/sonar-server/src/test/java/org/sonar/server/computation/analysis/AnalysisMetadataHolderImplTest.java
server/sonar-server/src/test/java/org/sonar/server/computation/analysis/AnalysisMetadataHolderRule.java
server/sonar-server/src/test/java/org/sonar/server/computation/analysis/MutableAnalysisMetadataHolderRule.java
server/sonar-server/src/test/java/org/sonar/server/computation/step/BuildComponentTreeStepTest.java
server/sonar-server/src/test/java/org/sonar/server/computation/step/LoadPeriodsStepTest.java
server/sonar-server/src/test/java/org/sonar/server/computation/step/PersistEventsStepTest.java
server/sonar-server/src/test/java/org/sonar/server/computation/step/ReportPersistSnapshotsStepTest.java
server/sonar-server/src/test/java/org/sonar/server/computation/step/ViewsPersistSnapshotsStepTest.java

index 595afb2817f40fc40228e01d5eb2fe5c323f027b..01f0aa12b7fb6b5f9affdaa6baedef3b063245b4 100644 (file)
@@ -19,7 +19,6 @@
  */
 package org.sonar.server.computation.analysis;
 
-import java.util.Date;
 import javax.annotation.CheckForNull;
 import org.sonar.server.computation.snapshot.Snapshot;
 
@@ -27,7 +26,7 @@ public interface AnalysisMetadataHolder {
   /**
    * @throws IllegalStateException if no analysis date has been set
    */
-  Date getAnalysisDate();
+  long getAnalysisDate();
 
   /**
    * Convenience method equivalent to calling {@link #getBaseProjectSnapshot() == null}
index db8e5da074c5a992ff8780a3320ad154f0422be7..6f688412ae788dd00c3e4537b00fb7249d0f685c 100644 (file)
  */
 package org.sonar.server.computation.analysis;
 
-import java.util.Date;
 import javax.annotation.CheckForNull;
 import javax.annotation.Nullable;
 import org.sonar.server.computation.snapshot.Snapshot;
 import org.sonar.server.computation.util.InitializedProperty;
 
-import static com.google.common.base.Preconditions.checkNotNull;
 import static com.google.common.base.Preconditions.checkState;
 
 public class AnalysisMetadataHolderImpl implements MutableAnalysisMetadataHolder {
@@ -41,17 +39,16 @@ public class AnalysisMetadataHolderImpl implements MutableAnalysisMetadataHolder
   private InitializedProperty<Integer> rootComponentRef = new InitializedProperty<>();
 
   @Override
-  public MutableAnalysisMetadataHolder setAnalysisDate(Date date) {
-    checkNotNull(date, "Date must not be null");
+  public MutableAnalysisMetadataHolder setAnalysisDate(long date) {
     checkState(!analysisDate.isInitialized(), "Analysis date has already been set");
-    this.analysisDate.setProperty(date.getTime());
+    this.analysisDate.setProperty(date);
     return this;
   }
 
   @Override
-  public Date getAnalysisDate() {
+  public long getAnalysisDate() {
     checkState(analysisDate.isInitialized(), "Analysis date has not been set");
-    return new Date(this.analysisDate.getProperty());
+    return this.analysisDate.getProperty();
   }
 
   @Override
index fe6c918101d8bc757b712ee5dcb110b2623261cb..dc2fdcf9480cac0f92b42adafdacb2e8125031de 100644 (file)
  */
 package org.sonar.server.computation.analysis;
 
-import java.util.Date;
 import javax.annotation.Nullable;
 import org.sonar.server.computation.snapshot.Snapshot;
 
 public interface MutableAnalysisMetadataHolder extends AnalysisMetadataHolder {
 
   /**
-   * @throws NullPointerException if specified date is {@code null}
    * @throws IllegalStateException if the analysis date has already been set
    */
-  MutableAnalysisMetadataHolder setAnalysisDate(Date date);
+  MutableAnalysisMetadataHolder setAnalysisDate(long date);
 
   /**
    * @throws IllegalStateException if baseProjectSnapshot has already been set
index a7f71a02c03f99f2523eb289f7c57852b8d905e3..aae5b65dede4b87403418f48e1b75b20cc64223c 100644 (file)
@@ -20,6 +20,7 @@
 package org.sonar.server.computation.issue;
 
 import com.google.common.annotations.VisibleForTesting;
+import java.util.Date;
 import javax.annotation.Nullable;
 import org.sonar.api.issue.Issue;
 import org.sonar.core.issue.DefaultIssue;
@@ -45,7 +46,7 @@ public class IssueLifecycle {
   private final DebtCalculator debtCalculator;
 
   public IssueLifecycle(AnalysisMetadataHolder analysisMetadataHolder, IssueWorkflow workflow, IssueUpdater updater, DebtCalculator debtCalculator) {
-    this(IssueChangeContext.createScan(analysisMetadataHolder.getAnalysisDate()), workflow, updater, debtCalculator);
+    this(IssueChangeContext.createScan(new Date(analysisMetadataHolder.getAnalysisDate())), workflow, updater, debtCalculator);
   }
 
   @VisibleForTesting
index 2bd49ca248a314cbe0574138b06fc64f40bb09a8..b332dc2c95fb32a0f0ee9f79a17a6297b1a96cb3 100644 (file)
@@ -30,7 +30,6 @@ import java.util.List;
 import javax.annotation.CheckForNull;
 import javax.annotation.Nullable;
 import org.apache.commons.lang.StringUtils;
-import org.sonar.api.CoreProperties;
 import org.sonar.api.config.Settings;
 import org.sonar.api.resources.Qualifiers;
 import org.sonar.api.utils.DateUtils;
@@ -69,7 +68,7 @@ import static org.sonar.server.computation.component.CrawlerDepthLimit.reportMax
  * Populates the {@link org.sonar.server.computation.period.PeriodsHolder}
  * <p/>
  * Here is how these periods are computed :
- * - Read the 5 period properties ${@link CoreProperties#TIMEMACHINE_PERIOD_PREFIX}
+ * - Read the 5 period properties ${@link org.sonar.core.config.CorePropertyDefinitions#TIMEMACHINE_PERIOD_PREFIX}
  * - Try to find the matching snapshots from the properties
  * - If a snapshot is found, a new period is added to the repository
  */
@@ -127,7 +126,7 @@ public class LoadPeriodsStep implements ComputationStep {
     }
 
     boolean isReportType = projectOrView.getType().isReportType();
-    PeriodResolver periodResolver = new PeriodResolver(session, projectDto.get().getId(), analysisMetadataHolder.getAnalysisDate().getTime(),
+    PeriodResolver periodResolver = new PeriodResolver(session, projectDto.get().getId(), analysisMetadataHolder.getAnalysisDate(),
       isReportType ? projectOrView.getReportAttributes().getVersion() : null,
       isReportType ? Qualifiers.PROJECT : Qualifiers.VIEW);
 
index 8e01a6ceb0cc7156153a640207ed251a89eaf65a..67fc6d98af27482a7ef987d49333e21fc851d9a5 100644 (file)
@@ -20,7 +20,6 @@
 
 package org.sonar.server.computation.step;
 
-import java.util.Date;
 import org.sonar.batch.protocol.output.BatchReport;
 import org.sonar.server.computation.analysis.MutableAnalysisMetadataHolder;
 import org.sonar.server.computation.batch.BatchReportReader;
@@ -43,7 +42,7 @@ public class LoadReportAnalysisMetadataHolderStep implements ComputationStep {
     BatchReport.Metadata reportMetadata = reportReader.readMetadata();
     mutableAnalysisMetadataHolder.setRootComponentRef(reportMetadata.getRootComponentRef());
     mutableAnalysisMetadataHolder.setBranch(reportMetadata.hasBranch() ? reportMetadata.getBranch() : null);
-    mutableAnalysisMetadataHolder.setAnalysisDate(new Date(reportMetadata.getAnalysisDate()));
+    mutableAnalysisMetadataHolder.setAnalysisDate(reportMetadata.getAnalysisDate());
     mutableAnalysisMetadataHolder.setCrossProjectDuplicationEnabled(reportMetadata.hasCrossProjectDuplicationActivated() && reportMetadata.getCrossProjectDuplicationActivated());
   }
 
index 4dd6a24f7d737b4ff590a4bf5191c312c993eaab..83c833af4e462d3bcd513d57ab036222868f52b0 100644 (file)
@@ -63,7 +63,7 @@ public class PersistEventsStep implements ComputationStep {
   public void execute() {
     final DbSession session = dbClient.openSession(false);
     try {
-      long analysisDate = analysisMetadataHolder.getAnalysisDate().getTime();
+      long analysisDate = analysisMetadataHolder.getAnalysisDate();
       new DepthTraversalTypeAwareCrawler(new PersistEventComponentVisitor(session, analysisDate))
         .visit(treeRootHolder.getRoot());
       session.commit();
index 2046ffc3a18bc67c2be4463cf61245477d2aa9d1..74985b3d6c937094e31cb736553f8a3a281300e0 100644 (file)
@@ -68,8 +68,8 @@ public class PersistSnapshotsStep implements ComputationStep {
     DbSession session = dbClient.openSession(false);
     try {
       new PathAwareCrawler<>(
-        new PersistSnapshotsPathAwareVisitor(session, analysisMetadataHolder.getAnalysisDate().getTime(), dbIdsRepository))
-          .visit(treeRootHolder.getRoot());
+        new PersistSnapshotsPathAwareVisitor(session, analysisMetadataHolder.getAnalysisDate(), dbIdsRepository))
+        .visit(treeRootHolder.getRoot());
       session.commit();
     } finally {
       dbClient.closeSession(session);
index 8d6cf9d7c85bfceab5ef49575ac7e2af9ccb911e..e5e4737a724c0ccee5bf23d35ad4fd95d343fdb2 100644 (file)
@@ -83,7 +83,7 @@ public class SendIssueNotificationsStep implements ComputationStep {
       issues.close();
     }
     if (newIssuesStats.hasIssues()) {
-      long analysisDate = analysisMetadataHolder.getAnalysisDate().getTime();
+      long analysisDate = analysisMetadataHolder.getAnalysisDate();
       sendNewIssuesNotification(newIssuesStats, project, analysisDate);
       sendNewIssuesNotificationToAssignees(newIssuesStats, project, analysisDate);
     }
index 22a931809ff1a76a4cafbd0c7689b14387e4076a..55b1f759a313e6e9aec24a67e926d8ff705f271e 100644 (file)
@@ -137,7 +137,7 @@ public class ValidateProjectStep implements ComputationStep {
     private void validateAnalysisDate(Optional<ComponentDto> baseProject) {
       if (baseProject.isPresent()) {
         SnapshotDto snapshotDto = dbClient.snapshotDao().selectLastSnapshotByComponentId(session, baseProject.get().getId());
-        long currentAnalysisDate = analysisMetadataHolder.getAnalysisDate().getTime();
+        long currentAnalysisDate = analysisMetadataHolder.getAnalysisDate();
         Long lastAnalysisDate = snapshotDto != null ? snapshotDto.getCreatedAt() : null;
         if (lastAnalysisDate != null && currentAnalysisDate <= snapshotDto.getCreatedAt()) {
           validationMessages.add(String.format("Date of analysis cannot be older than the date of the last known analysis on this project. Value: \"%s\". " +
index a204ab73c57adf441e6772fd9f181df0af69ea3d..cd84dd94084cf62ff6ca8496e0f0df35c84f08fe 100644 (file)
@@ -19,7 +19,6 @@
  */
 package org.sonar.server.computation.analysis;
 
-import java.util.Date;
 import org.junit.Rule;
 import org.junit.Test;
 import org.junit.rules.ExpectedException;
@@ -37,7 +36,7 @@ public class AnalysisMetadataHolderImplTest {
     .setCreatedAt(123456789L)
     .build();
 
-  static Date SOME_DATE = new Date();
+  static long SOME_DATE = 10000000L;
 
   @Test
   public void getAnalysisDate_returns_date_with_same_time_as_the_one_set_with_setAnalysisDate() throws InterruptedException {
@@ -45,11 +44,7 @@ public class AnalysisMetadataHolderImplTest {
 
     underTest.setAnalysisDate(SOME_DATE);
 
-    Thread.sleep(10);
-
-    Date analysisDate = underTest.getAnalysisDate();
-    assertThat(analysisDate.getTime()).isEqualTo(SOME_DATE.getTime());
-    assertThat(analysisDate).isNotSameAs(SOME_DATE);
+    assertThat(underTest.getAnalysisDate()).isEqualTo(SOME_DATE);
   }
 
   @Test
@@ -60,16 +55,6 @@ public class AnalysisMetadataHolderImplTest {
     new AnalysisMetadataHolderImpl().getAnalysisDate();
   }
 
-  @Test
-  public void setAnalysisDate_throws_NPE_when_date_is_null() {
-    AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();
-
-    expectedException.expect(NullPointerException.class);
-    expectedException.expectMessage("Date must not be null");
-
-    underTest.setAnalysisDate(null);
-  }
-
   @Test
   public void setAnalysisDate_throws_ISE_when_called_twice() {
     AnalysisMetadataHolderImpl underTest = new AnalysisMetadataHolderImpl();
index f4adc2669a578c65e2050cd9c755e78bf23180ea..e33e44e9f80b9b2eef5660945dc98c48c0154650 100644 (file)
@@ -48,10 +48,16 @@ public class AnalysisMetadataHolderRule extends ExternalResource implements Anal
     return this;
   }
 
+  public AnalysisMetadataHolderRule setAnalysisDate(long date) {
+    checkNotNull(date, "Date must not be null");
+    this.analysisDate.setProperty(date);
+    return this;
+  }
+
   @Override
-  public Date getAnalysisDate() {
+  public long getAnalysisDate() {
     checkState(analysisDate.isInitialized(), "Analysis date has not been set");
-    return new Date(this.analysisDate.getProperty());
+    return this.analysisDate.getProperty();
   }
 
   @Override
index 220e1b4ce26623a33c0d21b9c8f01ddf6563de8e..d821e6093e5d0d4d7489f26d027fd85cbc246f95 100644 (file)
@@ -19,7 +19,6 @@
  */
 package org.sonar.server.computation.analysis;
 
-import java.util.Date;
 import javax.annotation.CheckForNull;
 import javax.annotation.Nullable;
 import org.junit.rules.ExternalResource;
@@ -35,11 +34,11 @@ public class MutableAnalysisMetadataHolderRule extends ExternalResource implemen
   }
 
   @Override
-  public Date getAnalysisDate() {
+  public long getAnalysisDate() {
     return delegate.getAnalysisDate();
   }
 
-  public MutableAnalysisMetadataHolderRule setAnalysisDate(Date date) {
+  public MutableAnalysisMetadataHolderRule setAnalysisDate(long date) {
     delegate.setAnalysisDate(date);
     return this;
   }
index b3aae7b71c3069792405adfcfdd35e3828dc2049..9df06d01505cb202cb900b81eaf3d0399d692305 100644 (file)
@@ -22,7 +22,6 @@ package org.sonar.server.computation.step;
 import com.tngtech.java.junit.dataprovider.DataProvider;
 import com.tngtech.java.junit.dataprovider.DataProviderRunner;
 import com.tngtech.java.junit.dataprovider.UseDataProvider;
-import java.util.Date;
 import java.util.HashMap;
 import java.util.Map;
 import javax.annotation.Nullable;
@@ -75,7 +74,7 @@ public class BuildComponentTreeStepTest {
   static final String REPORT_DIR_KEY_2 = "src/main/java/dir2";
   static final String REPORT_FILE_KEY_2 = "src/main/java/dir2/File2.java";
 
-  static final Date ANALYSIS_DATE = new Date();
+  static final long ANALYSIS_DATE = 123456789L;
 
   @Rule
   public DbTester dbTester = DbTester.create(System2.INSTANCE);
index 362167168b9c355e669e8b7cd753c56cd416175f..f7b2c50edd244060794f04f3d3da5c2807693c51 100644 (file)
@@ -86,7 +86,7 @@ public class LoadPeriodsStepTest extends BaseStepTest {
 
   @Before
   public void setUp() throws Exception {
-    analysisMetadataHolder.setAnalysisDate(DATE_FORMAT.parse("2008-11-30"));
+    analysisMetadataHolder.setAnalysisDate(DATE_FORMAT.parse("2008-11-30").getTime());
 
     underTest = new LoadPeriodsStep(dbClient, settingsRepository, treeRootHolder, analysisMetadataHolder, periodsHolder);
   }
index 45baa3a534f51032475912a9343cb41b59bb4dba..94894126fca81bb29affd9bca53b678e2d8a766a 100644 (file)
@@ -66,7 +66,7 @@ public class PersistEventsStepTest extends BaseStepTest {
   @Before
   public void setup() {
     when(system2.now()).thenReturn(1225630680000L);
-    analysisMetadataHolder.setAnalysisDate(someDate);
+    analysisMetadataHolder.setAnalysisDate(someDate.getTime());
     step = new PersistEventsStep(dbTester.getDbClient(), system2, treeRootHolder, analysisMetadataHolder, eventRepository, dbIdsRepository);
     when(eventRepository.getEvents(any(Component.class))).thenReturn(Collections.<Event>emptyList());
   }
index ee109266c1ef50db8127e887e9d8184d54ec3d9f..272aa1d16e2748f8b55b313f683b249738c95ce5 100644 (file)
@@ -20,7 +20,6 @@
 
 package org.sonar.server.computation.step;
 
-import java.util.Date;
 import java.util.List;
 import org.junit.Before;
 import org.junit.Rule;
@@ -84,7 +83,7 @@ public class ReportPersistSnapshotsStepTest extends BaseStepTest {
   public void setup() {
     dbTester.truncateTables();
     analysisDate = DateUtils.parseDateQuietly("2015-06-01").getTime();
-    analysisMetadataHolder.setAnalysisDate(new Date(analysisDate));
+    analysisMetadataHolder.setAnalysisDate(analysisDate);
     dbIdsRepository = new DbIdsRepositoryImpl();
 
     now = DateUtils.parseDateQuietly("2015-06-02").getTime();
index 85c80cdc1ef707622762f0336d7fb32bb90aa5d3..3d6b1c6406bb11391cf46770dfd816530cba0f54 100644 (file)
@@ -20,7 +20,6 @@
 
 package org.sonar.server.computation.step;
 
-import java.util.Date;
 import java.util.List;
 import org.junit.Before;
 import org.junit.Rule;
@@ -90,7 +89,7 @@ public class ViewsPersistSnapshotsStepTest extends BaseStepTest {
   public void setup() {
     dbTester.truncateTables();
     analysisDate = DateUtils.parseDateQuietly("2015-06-01").getTime();
-    analysisMetadataHolder.setAnalysisDate(new Date(analysisDate));
+    analysisMetadataHolder.setAnalysisDate(analysisDate);
 
     now = DateUtils.parseDateQuietly("2015-06-02").getTime();