From 6327a718ef8197caec2ea8ced400cd9dae79c2d1 Mon Sep 17 00:00:00 2001 From: =?utf8?q?S=C3=A9bastien=20Lesaint?= Date: Thu, 28 May 2015 16:49:37 +0200 Subject: [PATCH] SONAR-6589 move ComponentTreeBuilders to test where it is actually used --- .../computation/component/ComponentImpl.java | 2 +- .../component/ComponentTreeBuilder.java | 27 ------ .../component/ComponentTreeBuilders.java | 85 ------------------- .../component/ComponentImplTest.java | 60 +++++++++++++ .../component/ComponentTreeBuilder.java | 55 ++++++++++++ .../PopulateComponentsUuidAndKeyStepTest.java | 8 +- 6 files changed, 120 insertions(+), 117 deletions(-) delete mode 100644 server/sonar-server/src/main/java/org/sonar/server/computation/component/ComponentTreeBuilder.java delete mode 100644 server/sonar-server/src/main/java/org/sonar/server/computation/component/ComponentTreeBuilders.java create mode 100644 server/sonar-server/src/test/java/org/sonar/server/computation/component/ComponentImplTest.java create mode 100644 server/sonar-server/src/test/java/org/sonar/server/computation/component/ComponentTreeBuilder.java diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/component/ComponentImpl.java b/server/sonar-server/src/main/java/org/sonar/server/computation/component/ComponentImpl.java index 318a8b83b47..f5f1d5ba0bb 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/component/ComponentImpl.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/component/ComponentImpl.java @@ -44,7 +44,7 @@ public class ComponentImpl implements Component { this.children = children == null ? Collections.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 index 622185d5cc3..00000000000 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/component/ComponentTreeBuilder.java +++ /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 index bfa2b97bb83..00000000000 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/component/ComponentTreeBuilders.java +++ /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 buildComponent(int componentRef) { - BatchReport.Component component = reportReader.readComponent(componentRef); - return Iterables.transform( - component.getChildRefList(), - new Function() { - @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 index 00000000000..d5ba594bfa8 --- /dev/null +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/component/ComponentImplTest.java @@ -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.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 index 00000000000..060cb476949 --- /dev/null +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/component/ComponentTreeBuilder.java @@ -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 buildChildren(final BatchReportReader reportReader, final BatchReport.Component component) { + return Iterables.transform( + component.getChildRefList(), + new Function() { + @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)); + } +} diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/step/PopulateComponentsUuidAndKeyStepTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/step/PopulateComponentsUuidAndKeyStepTest.java index 790b0d15562..e436c90de96 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/step/PopulateComponentsUuidAndKeyStepTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/step/PopulateComponentsUuidAndKeyStepTest.java @@ -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 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 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 componentsByRef = getComponentsByRef(treeRootHolder.getRoot()); -- 2.39.5