]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-6993 Load report metadata in a dedicated step
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Tue, 10 Nov 2015 16:49:59 +0000 (17:49 +0100)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Thu, 12 Nov 2015 10:01:29 +0000 (11:01 +0100)
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/step/BuildComponentTreeStep.java
server/sonar-server/src/main/java/org/sonar/server/computation/step/LoadReportAnalysisMetadataHolderStep.java [new file with mode: 0644]
server/sonar-server/src/main/java/org/sonar/server/computation/step/ReportComputationSteps.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/LoadReportAnalysisMetadataHolderStepTest.java [new file with mode: 0644]

index 6cd49d3bd14becdf39d4a62e84ddbd550fe3ecf3..0c1001e4e990d26b4b40ec0daaf8c7666a715166 100644 (file)
@@ -40,10 +40,11 @@ public class AnalysisMetadataHolderImpl implements MutableAnalysisMetadataHolder
   private InitializedProperty<Integer> rootComponentRef = new InitializedProperty<>();
 
   @Override
-  public void setAnalysisDate(Date date) {
+  public MutableAnalysisMetadataHolder setAnalysisDate(Date date) {
     checkNotNull(date, "Date must not be null");
     checkState(!analysisDate.isInitialized(), "Analysis date has already been set");
     this.analysisDate.setProperty(date.getTime());
+    return this;
   }
 
   @Override
@@ -58,9 +59,10 @@ public class AnalysisMetadataHolderImpl implements MutableAnalysisMetadataHolder
   }
 
   @Override
-  public void setBaseProjectSnapshot(@Nullable Snapshot baseProjectSnapshot) {
+  public MutableAnalysisMetadataHolder setBaseProjectSnapshot(@Nullable Snapshot baseProjectSnapshot) {
     checkState(!this.baseProjectSnapshot.isInitialized(), "Base project snapshot has already been set");
     this.baseProjectSnapshot.setProperty(baseProjectSnapshot);
+    return this;
   }
 
   @Override
@@ -71,9 +73,10 @@ public class AnalysisMetadataHolderImpl implements MutableAnalysisMetadataHolder
   }
 
   @Override
-  public void setCrossProjectDuplicationEnabled(boolean isCrossProjectDuplicationEnabled) {
+  public MutableAnalysisMetadataHolder setCrossProjectDuplicationEnabled(boolean isCrossProjectDuplicationEnabled) {
     checkState(!this.crossProjectDuplicationEnabled.isInitialized(), "Cross project duplication flag has already been set");
     this.crossProjectDuplicationEnabled.setProperty(isCrossProjectDuplicationEnabled);
+    return this;
   }
 
   @Override
@@ -83,9 +86,10 @@ public class AnalysisMetadataHolderImpl implements MutableAnalysisMetadataHolder
   }
 
   @Override
-  public void setBranch(@Nullable String branch) {
+  public MutableAnalysisMetadataHolder setBranch(@Nullable String branch) {
     checkState(!this.branch.isInitialized(), "Branch has already been set");
     this.branch.setProperty(branch);
+    return this;
   }
 
   @Override
@@ -95,9 +99,10 @@ public class AnalysisMetadataHolderImpl implements MutableAnalysisMetadataHolder
   }
 
   @Override
-  public void setRootComponentRef(int rootComponentRef) {
+  public MutableAnalysisMetadataHolder setRootComponentRef(int rootComponentRef) {
     checkState(!this.rootComponentRef.isInitialized(), "Root component ref has already been set");
     this.rootComponentRef.setProperty(rootComponentRef);
+    return this;
   }
 
   @Override
index eec006c0591a3c89b958cdf0dd7f7d1b48bd6310..fe6c918101d8bc757b712ee5dcb110b2623261cb 100644 (file)
@@ -29,26 +29,26 @@ public interface MutableAnalysisMetadataHolder extends AnalysisMetadataHolder {
    * @throws NullPointerException if specified date is {@code null}
    * @throws IllegalStateException if the analysis date has already been set
    */
-  void setAnalysisDate(Date date);
+  MutableAnalysisMetadataHolder setAnalysisDate(Date date);
 
   /**
    * @throws IllegalStateException if baseProjectSnapshot has already been set
    */
-  void setBaseProjectSnapshot(@Nullable Snapshot baseProjectSnapshot);
+  MutableAnalysisMetadataHolder setBaseProjectSnapshot(@Nullable Snapshot baseProjectSnapshot);
 
   /**
    * @throws IllegalStateException if cross project duplication flag has already been set
    */
-  void setCrossProjectDuplicationEnabled(boolean isCrossProjectDuplicationEnabled);
+  MutableAnalysisMetadataHolder setCrossProjectDuplicationEnabled(boolean isCrossProjectDuplicationEnabled);
 
   /**
    * @throws IllegalStateException if branch has already been set
    */
-  void setBranch(@Nullable String branch);
+  MutableAnalysisMetadataHolder setBranch(@Nullable String branch);
 
   /**
    * @throws IllegalStateException if root component ref has already been set
    */
-  void setRootComponentRef(int rootComponentRef);
+  MutableAnalysisMetadataHolder setRootComponentRef(int rootComponentRef);
 
 }
index ab51b2eea6c5a634f3420a7d6c9f9fe057654922..e5e33f0d403807a15cf2581bf4e8faa1ef00d42a 100644 (file)
@@ -21,7 +21,6 @@ package org.sonar.server.computation.step;
 
 import com.google.common.base.Function;
 import com.google.common.collect.Iterables;
-import java.util.Date;
 import javax.annotation.CheckForNull;
 import javax.annotation.Nonnull;
 import javax.annotation.Nullable;
@@ -61,10 +60,8 @@ public class BuildComponentTreeStep implements ComputationStep {
 
   @Override
   public void execute() {
-    analysisMetadataHolder.setAnalysisDate(new Date(reportReader.readMetadata().getAnalysisDate()));
-    BatchReport.Metadata reportMetadata = reportReader.readMetadata();
-    String branch = reportMetadata.hasBranch() ? reportMetadata.getBranch() : null;
-    BatchReport.Component reportProject = reportReader.readComponent(reportMetadata.getRootComponentRef());
+    String branch = analysisMetadataHolder.getBranch();
+    BatchReport.Component reportProject = reportReader.readComponent(analysisMetadataHolder.getRootComponentRef());
     UuidFactory uuidFactory = new UuidFactory(dbClient, moduleKey(reportProject, branch));
     Component project = new ComponentRootBuilder(reportProject, uuidFactory, branch).build();
     treeRootHolder.setRoot(project);
diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/step/LoadReportAnalysisMetadataHolderStep.java b/server/sonar-server/src/main/java/org/sonar/server/computation/step/LoadReportAnalysisMetadataHolderStep.java
new file mode 100644 (file)
index 0000000..8e01a6c
--- /dev/null
@@ -0,0 +1,54 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+
+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;
+
+/**
+ * Feed analysis metadata holder with metadata from the analysis report.
+ */
+public class LoadReportAnalysisMetadataHolderStep implements ComputationStep {
+
+  private final BatchReportReader reportReader;
+  private final MutableAnalysisMetadataHolder mutableAnalysisMetadataHolder;
+
+  public LoadReportAnalysisMetadataHolderStep(BatchReportReader reportReader, MutableAnalysisMetadataHolder mutableAnalysisMetadataHolder) {
+    this.reportReader = reportReader;
+    this.mutableAnalysisMetadataHolder = mutableAnalysisMetadataHolder;
+  }
+
+  @Override
+  public void execute() {
+    BatchReport.Metadata reportMetadata = reportReader.readMetadata();
+    mutableAnalysisMetadataHolder.setRootComponentRef(reportMetadata.getRootComponentRef());
+    mutableAnalysisMetadataHolder.setBranch(reportMetadata.hasBranch() ? reportMetadata.getBranch() : null);
+    mutableAnalysisMetadataHolder.setAnalysisDate(new Date(reportMetadata.getAnalysisDate()));
+    mutableAnalysisMetadataHolder.setCrossProjectDuplicationEnabled(reportMetadata.hasCrossProjectDuplicationActivated() && reportMetadata.getCrossProjectDuplicationActivated());
+  }
+
+  @Override
+  public String getDescription() {
+    return "Load analysis metadata";
+  }
+}
index 3750a67e129229505fffd53515e73be2d053615c..e298bf36e4cd2616b467c95646015a24b06f06b5 100644 (file)
@@ -42,6 +42,7 @@ public class ReportComputationSteps implements ComputationSteps {
       LogScannerContextStep.class,
 
       // Builds Component tree
+      LoadReportAnalysisMetadataHolderStep.class,
       BuildComponentTreeStep.class,
       ValidateProjectStep.class,
 
index 51a9fb5295b628f45be6e2f58d0a1516fa43623a..220e1b4ce26623a33c0d21b9c8f01ddf6563de8e 100644 (file)
@@ -30,7 +30,7 @@ public class MutableAnalysisMetadataHolderRule extends ExternalResource implemen
   private AnalysisMetadataHolderImpl delegate = new AnalysisMetadataHolderImpl();
 
   @Override
-  protected void before() throws Throwable {
+  protected void after() {
     delegate = new AnalysisMetadataHolderImpl();
   }
 
@@ -39,9 +39,9 @@ public class MutableAnalysisMetadataHolderRule extends ExternalResource implemen
     return delegate.getAnalysisDate();
   }
 
-  @Override
-  public void setAnalysisDate(Date date) {
+  public MutableAnalysisMetadataHolderRule setAnalysisDate(Date date) {
     delegate.setAnalysisDate(date);
+    return this;
   }
 
   @Override
@@ -50,8 +50,9 @@ public class MutableAnalysisMetadataHolderRule extends ExternalResource implemen
   }
 
   @Override
-  public void setBaseProjectSnapshot(@Nullable Snapshot baseProjectSnapshot) {
+  public MutableAnalysisMetadataHolderRule setBaseProjectSnapshot(@Nullable Snapshot baseProjectSnapshot) {
     delegate.setBaseProjectSnapshot(baseProjectSnapshot);
+    return this;
   }
 
   @Override
@@ -66,8 +67,9 @@ public class MutableAnalysisMetadataHolderRule extends ExternalResource implemen
   }
 
   @Override
-  public void setCrossProjectDuplicationEnabled(boolean isCrossProjectDuplicationEnabled) {
+  public MutableAnalysisMetadataHolderRule setCrossProjectDuplicationEnabled(boolean isCrossProjectDuplicationEnabled) {
     delegate.setCrossProjectDuplicationEnabled(isCrossProjectDuplicationEnabled);
+    return this;
   }
 
   @Override
@@ -76,13 +78,15 @@ public class MutableAnalysisMetadataHolderRule extends ExternalResource implemen
   }
 
   @Override
-  public void setBranch(@Nullable String branch) {
+  public MutableAnalysisMetadataHolderRule setBranch(@Nullable String branch) {
     delegate.setBranch(branch);
+    return this;
   }
 
   @Override
-  public void setRootComponentRef(int rootComponentRef) {
+  public MutableAnalysisMetadataHolderRule setRootComponentRef(int rootComponentRef) {
     delegate.setRootComponentRef(rootComponentRef);
+    return this;
   }
 
   @Override
index ce6e98476ffe69a53bb78747573f9e6d4d17c789..b3aae7b71c3069792405adfcfdd35e3828dc2049 100644 (file)
@@ -26,7 +26,6 @@ import java.util.Date;
 import java.util.HashMap;
 import java.util.Map;
 import javax.annotation.Nullable;
-import org.junit.Before;
 import org.junit.Rule;
 import org.junit.Test;
 import org.junit.experimental.categories.Category;
@@ -34,11 +33,12 @@ import org.junit.runner.RunWith;
 import org.sonar.api.utils.System2;
 import org.sonar.batch.protocol.Constants;
 import org.sonar.batch.protocol.output.BatchReport;
-import org.sonar.batch.protocol.output.BatchReport.Metadata;
 import org.sonar.db.DbClient;
 import org.sonar.db.DbTester;
 import org.sonar.db.component.ComponentDto;
 import org.sonar.db.component.SnapshotDto;
+import org.sonar.server.computation.analysis.AnalysisMetadataHolderImpl;
+import org.sonar.server.computation.analysis.MutableAnalysisMetadataHolder;
 import org.sonar.server.computation.analysis.MutableAnalysisMetadataHolderRule;
 import org.sonar.server.computation.batch.BatchReportReaderRule;
 import org.sonar.server.computation.component.Component;
@@ -75,6 +75,8 @@ 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();
+
   @Rule
   public DbTester dbTester = DbTester.create(System2.INSTANCE);
 
@@ -85,22 +87,15 @@ public class BuildComponentTreeStepTest {
   public MutableTreeRootHolderRule treeRootHolder = new MutableTreeRootHolderRule();
 
   @Rule
-  public MutableAnalysisMetadataHolderRule analysisMetadataHolder = new MutableAnalysisMetadataHolderRule();
-
-  Date someDate = new Date();
+  public MutableAnalysisMetadataHolderRule analysisMetadataHolder = new MutableAnalysisMetadataHolderRule()
+    .setRootComponentRef(ROOT_REF)
+    .setAnalysisDate(ANALYSIS_DATE)
+    .setBranch(null);
 
   DbClient dbClient = dbTester.getDbClient();
 
   BuildComponentTreeStep underTest = new BuildComponentTreeStep(dbClient, reportReader, treeRootHolder, analysisMetadataHolder);
 
-  @Before
-  public void setUp() {
-    reportReader.setMetadata(Metadata.newBuilder()
-      .setRootComponentRef(ROOT_REF)
-      .setAnalysisDate(someDate.getTime())
-      .build());
-  }
-
   @Test(expected = NullPointerException.class)
   public void fails_if_root_component_does_not_exist_in_reportReader() {
     underTest.execute();
@@ -130,8 +125,6 @@ public class BuildComponentTreeStepTest {
     assertThat(root.getType()).isEqualTo(Component.Type.valueOf(componentType.name()));
     assertThat(root.getReportAttributes().getRef()).isEqualTo(ROOT_REF);
     assertThat(root.getChildren()).isEmpty();
-
-    assertThat(analysisMetadataHolder.getAnalysisDate().getTime()).isEqualTo(someDate.getTime());
   }
 
   @Test
@@ -158,8 +151,6 @@ public class BuildComponentTreeStepTest {
     Component dir2 = module.getChildren().get(1);
     verifyComponent(dir2, Component.Type.DIRECTORY, DIR_REF_2, 1);
     verifyComponent(dir2.getChildren().iterator().next(), Component.Type.FILE, FILE_3_REF, 0);
-
-    assertThat(analysisMetadataHolder.getAnalysisDate().getTime()).isEqualTo(someDate.getTime());
   }
 
   @Test
@@ -199,11 +190,12 @@ public class BuildComponentTreeStepTest {
 
   @Test
   public void use_branch_to_generate_keys() {
-    reportReader.setMetadata(BatchReport.Metadata.newBuilder()
+    MutableAnalysisMetadataHolder analysisMetadataHolder = new AnalysisMetadataHolderImpl()
       .setRootComponentRef(ROOT_REF)
-      .setBranch("origin/master")
-      .setProjectKey("")
-      .build());
+      .setAnalysisDate(ANALYSIS_DATE)
+      .setBranch("origin/master");
+
+    BuildComponentTreeStep underTest = new BuildComponentTreeStep(dbClient, reportReader, treeRootHolder, analysisMetadataHolder);
 
     reportReader.putComponent(componentWithKey(ROOT_REF, PROJECT, REPORT_PROJECT_KEY, MODULE_REF));
     reportReader.putComponent(componentWithKey(MODULE_REF, MODULE, REPORT_MODULE_KEY, DIR_REF_1));
diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/step/LoadReportAnalysisMetadataHolderStepTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/step/LoadReportAnalysisMetadataHolderStepTest.java
new file mode 100644 (file)
index 0000000..4f40772
--- /dev/null
@@ -0,0 +1,127 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+
+package org.sonar.server.computation.step;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.sonar.batch.protocol.output.BatchReport;
+import org.sonar.server.computation.analysis.MutableAnalysisMetadataHolderRule;
+import org.sonar.server.computation.batch.BatchReportReaderRule;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class LoadReportAnalysisMetadataHolderStepTest {
+
+  static long ANALYSIS_DATE = 123456789L;
+
+  static String BRANCH = "origin/master";
+
+  @Rule
+  public BatchReportReaderRule reportReader = new BatchReportReaderRule();
+
+  @Rule
+  public MutableAnalysisMetadataHolderRule analysisMetadataHolder = new MutableAnalysisMetadataHolderRule();
+
+  ComputationStep underTest = new LoadReportAnalysisMetadataHolderStep(reportReader, analysisMetadataHolder);
+
+  @Test
+  public void set_root_component_ref() throws Exception {
+    reportReader.setMetadata(
+      BatchReport.Metadata.newBuilder()
+        .setRootComponentRef(1)
+        .build());
+
+    underTest.execute();
+
+    assertThat(analysisMetadataHolder.getRootComponentRef()).isEqualTo(1);
+  }
+
+  @Test
+  public void set_analysis_date() throws Exception {
+    reportReader.setMetadata(
+      BatchReport.Metadata.newBuilder()
+        .setAnalysisDate(ANALYSIS_DATE)
+        .build());
+
+    underTest.execute();
+
+    assertThat(analysisMetadataHolder.getAnalysisDate()).isEqualTo(ANALYSIS_DATE);
+  }
+
+  @Test
+  public void set_branch() throws Exception {
+    reportReader.setMetadata(
+      BatchReport.Metadata.newBuilder()
+        .setBranch(BRANCH)
+        .build());
+
+    underTest.execute();
+
+    assertThat(analysisMetadataHolder.getBranch()).isEqualTo(BRANCH);
+  }
+
+  @Test
+  public void set_null_branch_when_nothing_in_the_report() throws Exception {
+    reportReader.setMetadata(
+      BatchReport.Metadata.newBuilder()
+        .build());
+
+    underTest.execute();
+
+    assertThat(analysisMetadataHolder.getBranch()).isNull();
+  }
+
+  @Test
+  public void set_cross_project_duplication_to_true() throws Exception {
+    reportReader.setMetadata(
+      BatchReport.Metadata.newBuilder()
+        .setCrossProjectDuplicationActivated(true)
+        .build());
+
+    underTest.execute();
+
+    assertThat(analysisMetadataHolder.isCrossProjectDuplicationEnabled()).isEqualTo(true);
+  }
+
+  @Test
+  public void set_cross_project_duplication_to_false() throws Exception {
+    reportReader.setMetadata(
+      BatchReport.Metadata.newBuilder()
+        .setCrossProjectDuplicationActivated(false)
+        .build());
+
+    underTest.execute();
+
+    assertThat(analysisMetadataHolder.isCrossProjectDuplicationEnabled()).isEqualTo(false);
+  }
+
+  @Test
+  public void set_cross_project_duplication_to_false_when_nothing_in_the_report() throws Exception {
+    reportReader.setMetadata(
+      BatchReport.Metadata.newBuilder()
+        .build());
+
+    underTest.execute();
+
+    assertThat(analysisMetadataHolder.isCrossProjectDuplicationEnabled()).isEqualTo(false);
+  }
+
+}