]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-11280 add TreeRootHolder#isEmpty
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Wed, 10 Oct 2018 12:52:12 +0000 (14:52 +0200)
committerSonarTech <sonartech@sonarsource.com>
Thu, 11 Oct 2018 18:20:54 +0000 (20:20 +0200)
and fix interface MutableTreeRootHolder

server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/component/MutableTreeRootHolder.java
server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/component/TreeRootHolder.java
server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/component/TreeRootHolderImpl.java
server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/BuildComponentTreeStep.java
server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/component/MutableTreeRootHolderRule.java
server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/component/TreeRootHolderImplTest.java
server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/component/TreeRootHolderRule.java
server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/source/PersistFileSourcesStepTest.java

index e63bf33c176fc9e54fd7c6810729d495e82211b5..5e93c462ea21bcbc68764c779e287d430b37f28e 100644 (file)
@@ -27,13 +27,6 @@ public interface MutableTreeRootHolder extends TreeRootHolder {
    * @throws NullPointerException if {@code newRoot} is {@code null}
    * @throws IllegalStateException if root {@link Component} has already been set
    */
-  MutableTreeRootHolder setRoot(Component newRoot);
+  MutableTreeRootHolder setRoots(Component root, Component reportRoot);
 
-  /**
-   * Sets the root of the components that were in the scanner report in the TreeRootHolder.
-   *
-   * @throws NullPointerException if {@code newRoot} is {@code null}
-   * @throws IllegalStateException if extended tree root {@link Component} has already been set
-   */
-  MutableTreeRootHolder setReportTreeRoot(Component newRoot);
 }
index 347c7facad60a429c16d3b8e4335348ef7f5f01f..81c994fa726c400fd43780416e828c37d4f0e6fe 100644 (file)
@@ -25,6 +25,11 @@ import java.util.Optional;
  * The tree of components defined in the scanner report.
  */
 public interface TreeRootHolder {
+  /**
+   * @return true if the holder is empty
+   */
+  boolean isEmpty();
+
   /**
    * The root of the tree, for example the project or the portfolio.
    * With branches, it will refer to the root component of the branch.
index 91f47b639f1c6219e4d0de8909dd10a5f49f6c24..a0156ee19643a45595673fb231569357cea8fc02 100644 (file)
@@ -39,9 +39,15 @@ public class TreeRootHolderImpl implements MutableTreeRootHolder {
   private Component extendedTreeRoot;
 
   @Override
-  public MutableTreeRootHolder setRoot(Component root) {
+  public boolean isEmpty() {
+    return this.root == null;
+  }
+
+  @Override
+  public MutableTreeRootHolder setRoots(Component root, Component reportRoot) {
     checkState(this.root == null, "root can not be set twice in holder");
     this.root = requireNonNull(root, "root can not be null");
+    this.extendedTreeRoot = requireNonNull(reportRoot, "extended tree root can not be null");
     return this;
   }
 
@@ -51,13 +57,6 @@ public class TreeRootHolderImpl implements MutableTreeRootHolder {
     return this.root;
   }
 
-  @Override
-  public MutableTreeRootHolder setReportTreeRoot(Component root) {
-    checkState(this.extendedTreeRoot == null, "extended tree root can not be set twice in holder");
-    this.extendedTreeRoot = requireNonNull(root, "extended tree root can not be null");
-    return this;
-  }
-
   @Override
   public Component getReportTreeRoot() {
     checkInitialized();
index 206f7d8a4c8fb62b9dc4433292d82d6ef0591836..14133d5e4026bfff8221f991f494d29c098ae146 100644 (file)
@@ -86,13 +86,12 @@ public class BuildComponentTreeStep implements ComputationStep {
       String relativePathFromScmRoot = reportReader.readMetadata().getRelativePathFromScmRoot();
 
       Component reportTreeRoot = builder.buildProject(reportProject, relativePathFromScmRoot);
-      treeRootHolder.setReportTreeRoot(reportTreeRoot);
 
       if (analysisMetadataHolder.isShortLivingBranch() || analysisMetadataHolder.isPullRequest()) {
         Component changedComponentTreeRoot = builder.buildChangedComponentTreeRoot(reportTreeRoot);
-        treeRootHolder.setRoot(changedComponentTreeRoot);
+        treeRootHolder.setRoots(changedComponentTreeRoot, reportTreeRoot);
       } else {
-        treeRootHolder.setRoot(reportTreeRoot);
+        treeRootHolder.setRoots(reportTreeRoot, reportTreeRoot);
       }
 
       analysisMetadataHolder.setBaseAnalysis(toAnalysis(baseAnalysis));
index 5b11abf35d84569990773e59205478866c125263..dfec2c51944af69ea1defe1c47d389f464515ce6 100644 (file)
@@ -21,14 +21,8 @@ package org.sonar.ce.task.projectanalysis.component;
 
 public class MutableTreeRootHolderRule extends TreeRootHolderRule implements MutableTreeRootHolder {
   @Override
-  public MutableTreeRootHolderRule setRoot(Component newRoot) {
-    delegate.setRoot(newRoot);
-    return this;
-  }
-
-  @Override
-  public MutableTreeRootHolder setReportTreeRoot(Component newRoot) {
-    delegate.setReportTreeRoot(newRoot);
+  public MutableTreeRootHolderRule setRoots(Component root, Component reportRoot) {
+    delegate.setRoots(root, reportRoot);
     return this;
   }
 }
index 18c313064642c3aa4b79e3a231b3ab0ef02e4653..8c4fed5cd2b512867764ec34c61b1a3bf50e06ea 100644 (file)
@@ -56,21 +56,29 @@ public class TreeRootHolderImplTest {
   private TreeRootHolderImpl underTest = new TreeRootHolderImpl();
 
   @Test
-  public void setRoot_throws_NPE_if_arg_is_null() {
+  public void setRoots_throws_NPE_if_root_is_null() {
     expectedException.expect(NullPointerException.class);
     expectedException.expectMessage("root can not be null");
 
-    underTest.setRoot(null);
+    underTest.setRoots(null, DUMB_PROJECT);
+  }
+
+  @Test
+  public void setRoots_throws_NPE_if_reportRoot_is_null() {
+    expectedException.expect(NullPointerException.class);
+    expectedException.expectMessage("root can not be null");
+
+    underTest.setRoots(DUMB_PROJECT, null);
   }
 
   @Test
   public void setRoot_throws_ISE_when_called_twice() {
-    underTest.setRoot(DUMB_PROJECT);
+    underTest.setRoots(DUMB_PROJECT, DUMB_PROJECT);
 
     expectedException.expect(IllegalStateException.class);
     expectedException.expectMessage("root can not be set twice in holder");
 
-    underTest.setRoot(DUMB_PROJECT);
+    underTest.setRoots(null, DUMB_PROJECT);
   }
 
   @Test
@@ -89,7 +97,7 @@ public class TreeRootHolderImplTest {
 
   @Test
   public void getComponentByRef_returns_any_report_component_in_the_tree() {
-    underTest.setRoot(SOME_REPORT_COMPONENT_TREE);
+    underTest.setRoots(SOME_REPORT_COMPONENT_TREE, DUMB_PROJECT);
 
     for (int i = 1; i <= 4; i++) {
       assertThat(underTest.getComponentByRef(i).getReportAttributes().getRef()).isEqualTo(i);
@@ -98,7 +106,7 @@ public class TreeRootHolderImplTest {
 
   @Test
   public void getOptionalComponentByRef_returns_any_report_component_in_the_tree() {
-    underTest.setRoot(SOME_REPORT_COMPONENT_TREE);
+    underTest.setRoots(SOME_REPORT_COMPONENT_TREE, DUMB_PROJECT);
 
     for (int i = 1; i <= 4; i++) {
       assertThat(underTest.getOptionalComponentByRef(i).get().getReportAttributes().getRef()).isEqualTo(i);
@@ -107,7 +115,7 @@ public class TreeRootHolderImplTest {
 
   @Test
   public void getComponentByRef_throws_IAE_if_holder_does_not_contain_specified_component() {
-    underTest.setRoot(SOME_REPORT_COMPONENT_TREE);
+    underTest.setRoots(SOME_REPORT_COMPONENT_TREE, DUMB_PROJECT);
 
     expectedException.expect(IllegalArgumentException.class);
     expectedException.expectMessage("Component with ref '6' can't be found");
@@ -117,14 +125,14 @@ public class TreeRootHolderImplTest {
 
   @Test
   public void getOptionalComponentByRef_returns_empty_if_holder_does_not_contain_specified_component() {
-    underTest.setRoot(SOME_REPORT_COMPONENT_TREE);
+    underTest.setRoots(SOME_REPORT_COMPONENT_TREE, DUMB_PROJECT);
 
     assertThat(underTest.getOptionalComponentByRef(6)).isEmpty();
   }
 
   @Test
   public void getComponentByRef_throws_IAE_if_holder_contains_View_tree() {
-    underTest.setRoot(SOME_VIEWS_COMPONENT_TREE);
+    underTest.setRoots(SOME_VIEWS_COMPONENT_TREE, DUMB_PROJECT);
 
     expectedException.expect(IllegalArgumentException.class);
     expectedException.expectMessage("Component with ref '1' can't be found");
@@ -133,9 +141,15 @@ public class TreeRootHolderImplTest {
   }
 
   @Test
-  public void verify_setRoot_getRoot() {
-    underTest.setRoot(DUMB_PROJECT);
-    assertThat(underTest.getRoot()).isSameAs(DUMB_PROJECT);
+  public void verify_setRoots_getRoot() {
+    underTest.setRoots(SOME_REPORT_COMPONENT_TREE, DUMB_PROJECT);
+    assertThat(underTest.getRoot()).isSameAs(SOME_REPORT_COMPONENT_TREE);
+  }
+
+  @Test
+  public void verify_setRoots_getReportRoot() {
+    underTest.setRoots(SOME_REPORT_COMPONENT_TREE, DUMB_PROJECT);
+    assertThat(underTest.getReportTreeRoot()).isSameAs(DUMB_PROJECT);
   }
 
   @Test
@@ -147,7 +161,7 @@ public class TreeRootHolderImplTest {
 
   @Test
   public void getSize_counts_number_of_components() {
-    underTest.setRoot(SOME_REPORT_COMPONENT_TREE);
+    underTest.setRoots(SOME_REPORT_COMPONENT_TREE, DUMB_PROJECT);
     assertThat(underTest.getSize()).isEqualTo(4);
   }
 
index e0175a0b4805de62f5bb8b1392d57d837989e07e..8faa00f1dbcc6e87de3c94b92b1b82467f9812d5 100644 (file)
@@ -30,13 +30,21 @@ public class TreeRootHolderRule extends ExternalResource implements TreeRootHold
     this.delegate = null;
   }
 
-  public TreeRootHolderRule setRoot(Component newRoot) {
+  public TreeRootHolderRule setRoot(Component root) {
+    return setRoots(root, root);
+  }
+
+  public TreeRootHolderRule setRoots(Component root, Component reportRoot) {
     delegate = new TreeRootHolderImpl();
-    delegate.setRoot(newRoot);
-    delegate.setReportTreeRoot(newRoot);
+    delegate.setRoots(root, reportRoot);
     return this;
   }
 
+  @Override
+  public boolean isEmpty() {
+    return delegate.isEmpty();
+  }
+
   @Override
   public Component getRoot() {
     return delegate.getRoot();
@@ -62,5 +70,4 @@ public class TreeRootHolderRule extends ExternalResource implements TreeRootHold
     return delegate.getSize();
   }
 
-
 }
index 2d5be9616fb9b540ab97a2df6ec54695323e0641..8a0f121abeba05b41c6fb952e2ac969962617121 100644 (file)
@@ -448,11 +448,12 @@ public class PersistFileSourcesStepTest extends BaseStepTest {
   }
 
   private void initBasicReport(int numberOfLines) {
-    treeRootHolder.setRoot(ReportComponent.builder(Component.Type.PROJECT, 1).setUuid(PROJECT_UUID).setKey(PROJECT_KEY).addChildren(
+    ReportComponent root = ReportComponent.builder(Component.Type.PROJECT, 1).setUuid(PROJECT_UUID).setKey(PROJECT_KEY).addChildren(
       ReportComponent.builder(Component.Type.MODULE, 2).setUuid("MODULE").setKey("MODULE_KEY").addChildren(
         ReportComponent.builder(Component.Type.FILE, FILE1_REF).setUuid(FILE1_UUID).setKey("MODULE_KEY:src/Foo.java")
           .setFileAttributes(new FileAttributes(false, null, numberOfLines)).build())
         .build())
-      .build());
+      .build();
+    treeRootHolder.setRoots(root, root);
   }
 }