]> source.dussan.org Git - sonarqube.git/commitdiff
Replace usage of BatchReportReader.readMetadata() by AnalysisMetadataHolder
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Thu, 12 Nov 2015 09:13:49 +0000 (10:13 +0100)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Thu, 12 Nov 2015 10:01:31 +0000 (11:01 +0100)
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/step/SendIssueNotificationsStepTest.java
server/sonar-server/src/test/java/org/sonar/server/computation/step/ValidateProjectStepTest.java

index e84ae899a00cf2dadb03f3ae5f7ab6f0464137a6..8d6cf9d7c85bfceab5ef49575ac7e2af9ccb911e 100644 (file)
@@ -25,7 +25,7 @@ import java.util.Map;
 import java.util.Set;
 import org.sonar.core.issue.DefaultIssue;
 import org.sonar.core.util.CloseableIterator;
-import org.sonar.server.computation.batch.BatchReportReader;
+import org.sonar.server.computation.analysis.AnalysisMetadataHolder;
 import org.sonar.server.computation.component.Component;
 import org.sonar.server.computation.component.TreeRootHolder;
 import org.sonar.server.computation.issue.IssueCache;
@@ -52,17 +52,17 @@ public class SendIssueNotificationsStep implements ComputationStep {
   private final RuleRepository rules;
   private final TreeRootHolder treeRootHolder;
   private final NotificationService service;
-  private final BatchReportReader reportReader;
+  private final AnalysisMetadataHolder analysisMetadataHolder;
   private NewIssuesNotificationFactory newIssuesNotificationFactory;
 
   public SendIssueNotificationsStep(IssueCache issueCache, RuleRepository rules, TreeRootHolder treeRootHolder,
-    NotificationService service, BatchReportReader reportReader,
+    NotificationService service, AnalysisMetadataHolder analysisMetadataHolder,
     NewIssuesNotificationFactory newIssuesNotificationFactory) {
     this.issueCache = issueCache;
     this.rules = rules;
     this.treeRootHolder = treeRootHolder;
     this.service = service;
-    this.reportReader = reportReader;
+    this.analysisMetadataHolder = analysisMetadataHolder;
     this.newIssuesNotificationFactory = newIssuesNotificationFactory;
   }
 
@@ -83,7 +83,7 @@ public class SendIssueNotificationsStep implements ComputationStep {
       issues.close();
     }
     if (newIssuesStats.hasIssues()) {
-      long analysisDate = reportReader.readMetadata().getAnalysisDate();
+      long analysisDate = analysisMetadataHolder.getAnalysisDate().getTime();
       sendNewIssuesNotification(newIssuesStats, project, analysisDate);
       sendNewIssuesNotificationToAssignees(newIssuesStats, project, analysisDate);
     }
index 7177a62e46ea166333122a1970569a811ddddf50..22a931809ff1a76a4cafbd0c7689b14387e4076a 100644 (file)
@@ -29,13 +29,13 @@ import java.util.List;
 import java.util.Map;
 import javax.annotation.CheckForNull;
 import org.sonar.api.utils.MessageException;
-import org.sonar.batch.protocol.output.BatchReport;
 import org.sonar.core.component.ComponentKeys;
 import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
 import org.sonar.db.component.ComponentDao;
 import org.sonar.db.component.ComponentDto;
 import org.sonar.db.component.SnapshotDto;
+import org.sonar.server.computation.analysis.AnalysisMetadataHolder;
 import org.sonar.server.computation.batch.BatchReportReader;
 import org.sonar.server.computation.component.Component;
 import org.sonar.server.computation.component.ComponentVisitor;
@@ -64,11 +64,13 @@ public class ValidateProjectStep implements ComputationStep {
   private final DbClient dbClient;
   private final BatchReportReader reportReader;
   private final TreeRootHolder treeRootHolder;
+  private final AnalysisMetadataHolder analysisMetadataHolder;
 
-  public ValidateProjectStep(DbClient dbClient, BatchReportReader reportReader, TreeRootHolder treeRootHolder) {
+  public ValidateProjectStep(DbClient dbClient, BatchReportReader reportReader, TreeRootHolder treeRootHolder, AnalysisMetadataHolder analysisMetadataHolder) {
     this.dbClient = dbClient;
     this.reportReader = reportReader;
     this.treeRootHolder = treeRootHolder;
+    this.analysisMetadataHolder = analysisMetadataHolder;
   }
 
   @Override
@@ -135,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 = reportReader.readMetadata().getAnalysisDate();
+        long currentAnalysisDate = analysisMetadataHolder.getAnalysisDate().getTime();
         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\". " +
@@ -185,11 +187,10 @@ public class ValidateProjectStep implements ComputationStep {
 
     @CheckForNull
     private void validateBranch() {
-      BatchReport.Metadata metadata = reportReader.readMetadata();
-      if (!metadata.hasBranch()) {
+      String branch = analysisMetadataHolder.getBranch();
+      if (branch == null) {
         return;
       }
-      String branch = metadata.getBranch();
       if (!ComponentKeys.isValidBranch(branch)) {
         validationMessages.add(String.format("\"%s\" is not a valid branch name. "
           + "Allowed characters are alphanumeric, '-', '_', '.' and '/'.", branch));
index cd8512295df9ce259e53be3fe05e95e9af0a069b..e13692358ebf9ce7ae6e43a5c9446a6c3971127e 100644 (file)
@@ -28,9 +28,8 @@ import org.sonar.api.notifications.Notification;
 import org.sonar.api.rule.Severity;
 import org.sonar.api.utils.Duration;
 import org.sonar.api.utils.System2;
-import org.sonar.batch.protocol.output.BatchReport;
 import org.sonar.core.issue.DefaultIssue;
-import org.sonar.server.computation.batch.BatchReportReaderRule;
+import org.sonar.server.computation.analysis.AnalysisMetadataHolderRule;
 import org.sonar.server.computation.batch.TreeRootHolderRule;
 import org.sonar.server.computation.component.Component;
 import org.sonar.server.computation.component.Component.Type;
@@ -66,13 +65,14 @@ public class SendIssueNotificationsStepTest extends BaseStepTest {
 
   static final Component PROJECT = builder(Type.PROJECT, 1).setUuid(PROJECT_UUID).setKey(PROJECT_KEY).setName(PROJECT_NAME).build();
 
-  @Rule
-  public BatchReportReaderRule reportReader = new BatchReportReaderRule();
-
   @Rule
   public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule()
     .setRoot(PROJECT);
 
+  @Rule
+  public AnalysisMetadataHolderRule analysisMetadataHolder = new AnalysisMetadataHolderRule()
+    .setAnalysisDate(new Date(ANALYSE_DATE));
+
   @Rule
   public TemporaryFolder temp = new TemporaryFolder();
 
@@ -87,15 +87,11 @@ public class SendIssueNotificationsStepTest extends BaseStepTest {
   @Before
   public void setUp() throws Exception {
     issueCache = new IssueCache(temp.newFile(), System2.INSTANCE);
-    underTest = new SendIssueNotificationsStep(issueCache, mock(RuleRepository.class), treeRootHolder, notificationService, reportReader, newIssuesNotificationFactory);
+    underTest = new SendIssueNotificationsStep(issueCache, mock(RuleRepository.class), treeRootHolder, notificationService, analysisMetadataHolder,
+      newIssuesNotificationFactory);
 
     when(newIssuesNotificationFactory.newNewIssuesNotication()).thenReturn(newIssuesNotificationMock);
     when(newIssuesNotificationFactory.newMyNewIssuesNotification()).thenReturn(myNewIssuesNotificationMock);
-
-    reportReader.setMetadata(BatchReport.Metadata.newBuilder()
-      .setRootComponentRef(1)
-      .setAnalysisDate(ANALYSE_DATE)
-      .build());
   }
 
   @Test
index bc24a015cae76935fb0027b936a53bb6b3e02815..059ecab9c05647e7583b2a852487323e8e403b30 100644 (file)
 
 package org.sonar.server.computation.step;
 
+import java.util.Date;
 import org.junit.Before;
 import org.junit.Rule;
 import org.junit.Test;
 import org.junit.experimental.categories.Category;
 import org.junit.rules.ExpectedException;
+import org.sonar.api.utils.DateUtils;
 import org.sonar.api.utils.MessageException;
 import org.sonar.api.utils.System2;
 import org.sonar.batch.protocol.Constants;
@@ -34,6 +36,7 @@ import org.sonar.db.DbTester;
 import org.sonar.db.component.ComponentDto;
 import org.sonar.db.component.ComponentTesting;
 import org.sonar.db.component.SnapshotTesting;
+import org.sonar.server.computation.analysis.AnalysisMetadataHolderRule;
 import org.sonar.server.computation.batch.BatchReportReaderRule;
 import org.sonar.server.computation.batch.TreeRootHolderRule;
 import org.sonar.server.computation.component.Component;
@@ -43,9 +46,10 @@ import org.sonar.test.DbTests;
 @Category(DbTests.class)
 public class ValidateProjectStepTest {
 
-  private static long DEFAULT_ANALYSIS_TIME = 1433131200000L; // 2015-06-01
-  private static final String PROJECT_KEY = "PROJECT_KEY";
-  private static final String MODULE_KEY = "MODULE_KEY";
+  static long DEFAULT_ANALYSIS_TIME = 1433131200000L; // 2015-06-01
+  static final String PROJECT_KEY = "PROJECT_KEY";
+  static final String MODULE_KEY = "MODULE_KEY";
+  static final String DEFAULT_BRANCH = "origin/master";
 
   @Rule
   public DbTester dbTester = DbTester.create(System2.INSTANCE);
@@ -59,9 +63,14 @@ public class ValidateProjectStepTest {
   @Rule
   public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule();
 
+  @Rule
+  public AnalysisMetadataHolderRule analysisMetadataHolder = new AnalysisMetadataHolderRule()
+    .setAnalysisDate(new Date(DEFAULT_ANALYSIS_TIME))
+    .setBranch(DEFAULT_BRANCH);
+
   DbClient dbClient = dbTester.getDbClient();
 
-  ValidateProjectStep underTest = new ValidateProjectStep(dbClient, reportReader, treeRootHolder);
+  ValidateProjectStep underTest = new ValidateProjectStep(dbClient, reportReader, treeRootHolder, analysisMetadataHolder);
 
   @Before
   public void setUp() {
@@ -70,10 +79,7 @@ public class ValidateProjectStepTest {
 
   @Test
   public void not_fail_on_valid_branch() {
-    reportReader.setMetadata(BatchReport.Metadata.newBuilder()
-      .setAnalysisDate(DEFAULT_ANALYSIS_TIME)
-      .setBranch("origin/master")
-      .build());
+    analysisMetadataHolder.setBranch(DEFAULT_BRANCH);
     reportReader.putComponent(BatchReport.Component.newBuilder()
       .setRef(1)
       .setType(Constants.ComponentType.PROJECT)
@@ -90,10 +96,7 @@ public class ValidateProjectStepTest {
     thrown.expectMessage("Validation of project failed:\n" +
       "  o \"bran#ch\" is not a valid branch name. Allowed characters are alphanumeric, '-', '_', '.' and '/'.");
 
-    reportReader.setMetadata(BatchReport.Metadata.newBuilder()
-      .setAnalysisDate(DEFAULT_ANALYSIS_TIME)
-      .setBranch("bran#ch")
-      .build());
+    analysisMetadataHolder.setBranch("bran#ch");
     reportReader.putComponent(BatchReport.Component.newBuilder()
       .setRef(1)
       .setType(Constants.ComponentType.PROJECT)
@@ -113,7 +116,6 @@ public class ValidateProjectStepTest {
       "  o \"Project\\Key\" is not a valid project or module key. Allowed characters are alphanumeric, '-', '_', '.' and ':', with at least one non-digit.\n" +
       "  o \"Module$Key\" is not a valid project or module key. Allowed characters are alphanumeric, '-', '_', '.' and ':', with at least one non-digit");
 
-    reportReader.setMetadata(BatchReport.Metadata.newBuilder().setAnalysisDate(DEFAULT_ANALYSIS_TIME).build());
     reportReader.putComponent(BatchReport.Component.newBuilder()
       .setRef(1)
       .setType(Constants.ComponentType.PROJECT)
@@ -140,7 +142,6 @@ public class ValidateProjectStepTest {
       "If you really want to stop directly analysing project \"" + MODULE_KEY + "\", please first delete it from SonarQube and then relaunch the analysis of project \""
       + PROJECT_KEY + "\".");
 
-    reportReader.setMetadata(BatchReport.Metadata.newBuilder().setAnalysisDate(DEFAULT_ANALYSIS_TIME).build());
     reportReader.putComponent(BatchReport.Component.newBuilder()
       .setRef(1)
       .setType(Constants.ComponentType.PROJECT)
@@ -171,7 +172,6 @@ public class ValidateProjectStepTest {
     thrown.expectMessage("Validation of project failed:\n" +
       "  o Module \"" + MODULE_KEY + "\" is already part of project \"" + anotherProjectKey + "\"");
 
-    reportReader.setMetadata(BatchReport.Metadata.newBuilder().setAnalysisDate(DEFAULT_ANALYSIS_TIME).build());
     reportReader.putComponent(BatchReport.Component.newBuilder()
       .setRef(1)
       .setType(Constants.ComponentType.PROJECT)
@@ -207,7 +207,6 @@ public class ValidateProjectStepTest {
       "If you really want to stop directly analysing project \"" + anotherProjectKey + "\", please first delete it from SonarQube and then relaunch the analysis of project \""
       + PROJECT_KEY + "\".");
 
-    reportReader.setMetadata(BatchReport.Metadata.newBuilder().setAnalysisDate(DEFAULT_ANALYSIS_TIME).build());
     reportReader.putComponent(BatchReport.Component.newBuilder()
       .setRef(1)
       .setType(Constants.ComponentType.PROJECT)
@@ -235,9 +234,6 @@ public class ValidateProjectStepTest {
 
   @Test
   public void not_fail_if_analysis_date_is_after_last_analysis() {
-    reportReader.setMetadata(BatchReport.Metadata.newBuilder()
-      .setAnalysisDate(DEFAULT_ANALYSIS_TIME) // 2015-06-01
-      .build());
     reportReader.putComponent(BatchReport.Component.newBuilder()
       .setRef(1)
       .setType(Constants.ComponentType.PROJECT)
@@ -262,9 +258,8 @@ public class ValidateProjectStepTest {
     thrown.expectMessage("Date of analysis cannot be older than the date of the last known analysis on this project. Value: ");
     thrown.expectMessage("Latest analysis: ");
 
-    reportReader.setMetadata(BatchReport.Metadata.newBuilder()
-      .setAnalysisDate(1420088400000L) // 2015-01-01
-      .build());
+    analysisMetadataHolder.setAnalysisDate(DateUtils.parseDate("2015-01-01"));
+
     reportReader.putComponent(BatchReport.Component.newBuilder()
       .setRef(1)
       .setType(Constants.ComponentType.PROJECT)