]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-6589 move ComponentTreeBuilders to test where it is actually used
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Thu, 28 May 2015 14:49:37 +0000 (16:49 +0200)
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Mon, 1 Jun 2015 15:08:29 +0000 (17:08 +0200)
server/sonar-server/src/main/java/org/sonar/server/computation/component/ComponentImpl.java
server/sonar-server/src/main/java/org/sonar/server/computation/component/ComponentTreeBuilder.java [deleted file]
server/sonar-server/src/main/java/org/sonar/server/computation/component/ComponentTreeBuilders.java [deleted file]
server/sonar-server/src/test/java/org/sonar/server/computation/component/ComponentImplTest.java [new file with mode: 0644]
server/sonar-server/src/test/java/org/sonar/server/computation/component/ComponentTreeBuilder.java [new file with mode: 0644]
server/sonar-server/src/test/java/org/sonar/server/computation/step/PopulateComponentsUuidAndKeyStepTest.java

index 318a8b83b47f88a3733730597ce53ddc0cbd65b5..f5f1d5ba0bbb609d4d4cbc3d851d52ec4f2ef840 100644 (file)
@@ -44,7 +44,7 @@ public class ComponentImpl implements Component {
     this.children = children == null ? Collections.<Component>emptyList() : copyOf(filter(children, notNull()));
   }
 
-  private static Type convertType(Constants.ComponentType type) {
+  public static Type convertType(Constants.ComponentType type) {
     switch (type) {
       case PROJECT:
         return Type.PROJECT;
diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/component/ComponentTreeBuilder.java b/server/sonar-server/src/main/java/org/sonar/server/computation/component/ComponentTreeBuilder.java
deleted file mode 100644 (file)
index 622185d..0000000
+++ /dev/null
@@ -1,27 +0,0 @@
-/*
- * 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.component;
-
-public interface ComponentTreeBuilder {
-  /**
-   * Builds the tree of components and returns the Component of the root of this tree.
-   */
-  Component build();
-}
diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/component/ComponentTreeBuilders.java b/server/sonar-server/src/main/java/org/sonar/server/computation/component/ComponentTreeBuilders.java
deleted file mode 100644 (file)
index bfa2b97..0000000
+++ /dev/null
@@ -1,85 +0,0 @@
-/*
- * 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.component;
-
-import com.google.common.base.Function;
-import com.google.common.collect.Iterables;
-import javax.annotation.Nonnull;
-import org.sonar.batch.protocol.output.BatchReport;
-import org.sonar.server.computation.batch.BatchReportReader;
-
-import static java.util.Objects.requireNonNull;
-
-public final class ComponentTreeBuilders {
-  public static ComponentTreeBuilder from(final BatchReportReader reportReader) {
-    // fail fast
-    requireNonNull(reportReader);
-
-    return new BatchReportComponentTreeBuilderImpl(reportReader);
-  }
-
-  public static ComponentTreeBuilder from(final Component root) {
-    // fail-fast
-    requireNonNull(root);
-    return new ComponentTreeBuilder() {
-      @Override
-      public Component build() {
-        return root;
-      }
-    };
-  }
-
-  public interface BatchReportComponentTreeBuilder extends ComponentTreeBuilder {
-
-  }
-
-  private static class BatchReportComponentTreeBuilderImpl implements BatchReportComponentTreeBuilder {
-    private final BatchReportReader reportReader;
-
-    public BatchReportComponentTreeBuilderImpl(BatchReportReader reportReader) {
-      this.reportReader = reportReader;
-    }
-
-    @Override
-    public Component build() {
-      return buildComponentRoot(reportReader);
-    }
-
-    private Component buildComponentRoot(BatchReportReader reportReader) {
-      int rootComponentRef = reportReader.readMetadata().getRootComponentRef();
-      BatchReport.Component component = reportReader.readComponent(rootComponentRef);
-      return new ComponentImpl(component, buildComponent(rootComponentRef));
-    }
-
-    private Iterable<Component> buildComponent(int componentRef) {
-      BatchReport.Component component = reportReader.readComponent(componentRef);
-      return Iterables.transform(
-          component.getChildRefList(),
-          new Function<Integer, Component>() {
-            @Override
-            public Component apply(@Nonnull Integer componentRef) {
-              BatchReport.Component component = reportReader.readComponent(componentRef);
-              return new ComponentImpl(component, buildComponent(componentRef));
-            }
-          }
-      );
-    }
-  }
-}
diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/component/ComponentImplTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/component/ComponentImplTest.java
new file mode 100644 (file)
index 0000000..d5ba594
--- /dev/null
@@ -0,0 +1,60 @@
+/*
+ * 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.component;
+
+import java.util.Collections;
+import org.junit.Test;
+import org.sonar.batch.protocol.Constants;
+import org.sonar.batch.protocol.output.BatchReport;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class ComponentImplTest {
+  private ComponentImpl component = new ComponentImpl(BatchReport.Component.newBuilder().build(), Collections.<Component>emptyList());
+
+  @Test(expected = UnsupportedOperationException.class)
+  public void getUuid_throws_UOE_if_uuid_has_not_been_set_yet() {
+    component.getUuid();
+  }
+
+  @Test(expected = UnsupportedOperationException.class)
+  public void getKey_throws_UOE_if_uuid_has_not_been_set_yet() {
+    component.getKey();
+  }
+
+  @Test
+  public void verify_setUuid() {
+    String uuid = "toto";
+    assertThat(component.setUuid(uuid).getUuid()).isEqualTo(uuid);
+  }
+
+  @Test
+  public void verify_setKey() {
+    String key = "toto";
+    assertThat(component.setKey(key).getKey()).isEqualTo(key);
+  }
+
+  @Test
+  public void convertType() {
+    for (Constants.ComponentType componentType : Constants.ComponentType.values()) {
+      assertThat(ComponentImpl.convertType(componentType)).isEqualTo(Component.Type.valueOf(componentType.name()));
+    }
+  }
+}
diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/component/ComponentTreeBuilder.java b/server/sonar-server/src/test/java/org/sonar/server/computation/component/ComponentTreeBuilder.java
new file mode 100644 (file)
index 0000000..060cb47
--- /dev/null
@@ -0,0 +1,55 @@
+/*
+ * 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.component;
+
+import com.google.common.base.Function;
+import com.google.common.collect.Iterables;
+import javax.annotation.Nonnull;
+import org.sonar.batch.protocol.output.BatchReport;
+import org.sonar.server.computation.batch.BatchReportReader;
+
+public final class ComponentTreeBuilder {
+  public static Component from(final BatchReportReader reportReader) {
+    return buildComponentRoot(reportReader);
+  }
+
+  private static Component buildComponentRoot(BatchReportReader reportReader) {
+    int rootComponentRef = reportReader.readMetadata().getRootComponentRef();
+    BatchReport.Component component = reportReader.readComponent(rootComponentRef);
+    return newComponent(reportReader, component);
+  }
+
+  private static Iterable<Component> buildChildren(final BatchReportReader reportReader, final BatchReport.Component component) {
+    return Iterables.transform(
+        component.getChildRefList(),
+        new Function<Integer, Component>() {
+          @Override
+          public Component apply(@Nonnull Integer componentRef) {
+            BatchReport.Component component = reportReader.readComponent(componentRef);
+            return newComponent(reportReader, component);
+          }
+        }
+    );
+  }
+
+  private static Component newComponent(BatchReportReader reportReader, BatchReport.Component component) {
+    return new ComponentImpl(component, buildChildren(reportReader, component));
+  }
+}
index 790b0d15562d7540f09bc36563e81a538e8db7f0..e436c90de9678d1e7ee713415c1f29d4550b33df 100644 (file)
@@ -39,7 +39,7 @@ import org.sonar.server.component.db.ComponentDao;
 import org.sonar.server.computation.batch.BatchReportReaderRule;
 import org.sonar.server.computation.batch.TreeRootHolderRule;
 import org.sonar.server.computation.component.Component;
-import org.sonar.server.computation.component.ComponentTreeBuilders;
+import org.sonar.server.computation.component.ComponentTreeBuilder;
 import org.sonar.server.db.DbClient;
 import org.sonar.test.DbTests;
 
@@ -111,7 +111,7 @@ public class PopulateComponentsUuidAndKeyStepTest extends BaseStepTest {
       .setPath("src/main/java/dir/Foo.java")
       .build());
 
-    treeRootHolder.setRoot(ComponentTreeBuilders.from(reportReader).build());
+    treeRootHolder.setRoot(ComponentTreeBuilder.from(reportReader));
     sut.execute();
 
     Map<Integer, Component> componentsByRef = getComponentsByRef(treeRootHolder.getRoot());
@@ -226,7 +226,7 @@ public class PopulateComponentsUuidAndKeyStepTest extends BaseStepTest {
       .setPath("src/main/java/dir/Foo.java")
       .build());
 
-    treeRootHolder.setRoot(ComponentTreeBuilders.from(reportReader).build());
+    treeRootHolder.setRoot(ComponentTreeBuilder.from(reportReader));
     sut.execute();
 
     Map<Integer, Component> componentsByRef = getComponentsByRef(treeRootHolder.getRoot());
@@ -269,7 +269,7 @@ public class PopulateComponentsUuidAndKeyStepTest extends BaseStepTest {
       .setPath("src/main/java/dir/Foo.java")
       .build());
 
-    treeRootHolder.setRoot(ComponentTreeBuilders.from(reportReader).build());
+    treeRootHolder.setRoot(ComponentTreeBuilder.from(reportReader));
     sut.execute();
 
     Map<Integer, Component> componentsByRef = getComponentsByRef(treeRootHolder.getRoot());