From: Sébastien Lesaint Date: Thu, 5 Nov 2015 14:44:11 +0000 (+0100) Subject: small refactoring of ComponentProvider X-Git-Tag: 5.3-RC1~355 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=48da48d8e13f59d8846b6affc99aa87171293a3e;p=sonarqube.git small refactoring of ComponentProvider change so that initialization state verification is responsability of ComponentProvider implementation, not user of ComponentProvider add NoComponentProvider implementation so that componentProvider property can be never null --- diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/component/AbstractComponentProvider.java b/server/sonar-server/src/test/java/org/sonar/server/computation/component/AbstractComponentProvider.java new file mode 100644 index 00000000000..c0ab6c4c4b8 --- /dev/null +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/component/AbstractComponentProvider.java @@ -0,0 +1,52 @@ +/* + * 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 static com.google.common.base.Preconditions.checkState; + +abstract class AbstractComponentProvider implements ComponentProvider { + private boolean initialized = false; + + @Override + public void ensureInitialized() { + if (!this.initialized) { + ensureInitializedImpl(); + this.initialized = true; + } + } + + protected abstract void ensureInitializedImpl(); + + @Override + public void reset() { + resetImpl(); + this.initialized = false; + } + + protected abstract void resetImpl(); + + @Override + public Component getByRef(int componentRef) { + checkState(this.initialized, "%s has not been initialized", getClass().getSimpleName()); + return getByRefImpl(componentRef); + } + + protected abstract Component getByRefImpl(int componentRef); +} diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/component/ComponentProvider.java b/server/sonar-server/src/test/java/org/sonar/server/computation/component/ComponentProvider.java index 9b6d032c23b..2258777d9b9 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/component/ComponentProvider.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/component/ComponentProvider.java @@ -20,12 +20,16 @@ package org.sonar.server.computation.component; public interface ComponentProvider { - void init(); + /** + * does nothing if already initialized + */ + void ensureInitialized(); void reset(); /** * @throws IllegalStateException if no component is found for the specified ref + * @throws IllegalStateException if provider has not been initialized */ Component getByRef(int componentRef); } diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/component/MutableDbIdsRepositoryRule.java b/server/sonar-server/src/test/java/org/sonar/server/computation/component/MutableDbIdsRepositoryRule.java index c07426f3f04..4fd4ff95ff3 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/component/MutableDbIdsRepositoryRule.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/component/MutableDbIdsRepositoryRule.java @@ -20,29 +20,24 @@ package org.sonar.server.computation.component; import com.google.common.base.Function; -import javax.annotation.CheckForNull; import javax.annotation.Nullable; import org.junit.rules.ExternalResource; import org.sonar.server.computation.batch.TreeRootHolderRule; -import static com.google.common.base.Preconditions.checkState; - /** * Implementation of {@link DbIdsRepository} as a JUnit {@link org.junit.Rule} which supports both * {@link ViewsComponent} and {@link ReportComponent}. */ public class MutableDbIdsRepositoryRule extends ExternalResource implements MutableDbIdsRepository { - @CheckForNull private final ComponentProvider componentProvider; - private boolean providerInitialized = false; private MutableDbIdsRepository delegate = newDelegate(); - private MutableDbIdsRepositoryRule(@Nullable ComponentProvider componentProvider) { + private MutableDbIdsRepositoryRule(ComponentProvider componentProvider) { this.componentProvider = componentProvider; } public static MutableDbIdsRepositoryRule standalone() { - return new MutableDbIdsRepositoryRule(null); + return new MutableDbIdsRepositoryRule(NoComponentProvider.INSTANCE); } public static MutableDbIdsRepositoryRule create(TreeRootHolderRule treeRootHolder) { @@ -66,29 +61,18 @@ public class MutableDbIdsRepositoryRule extends ExternalResource implements Muta @Override protected void before() throws Throwable { this.delegate = newDelegate(); - if (this.componentProvider != null) { - this.providerInitialized = false; - } } public DbIdsRepository setComponentId(int componentRef, long componentId) { - checkAndInitProvider(); + this.componentProvider.ensureInitialized(); return delegate.setComponentId(componentProvider.getByRef(componentRef), componentId); } public DbIdsRepository setSnapshotId(int componentRef, long snapshotId) { - checkAndInitProvider(); + this.componentProvider.ensureInitialized(); return delegate.setSnapshotId(componentProvider.getByRef(componentRef), snapshotId); } - private void checkAndInitProvider() { - checkState(this.componentProvider != null, "Can not use methods set taking a ref if no ComponentProvider has been set"); - if (!this.providerInitialized) { - this.componentProvider.init(); - this.providerInitialized = true; - } - } - @Override public DbIdsRepository setComponentId(Component component, long componentId) { return delegate.setComponentId(component, componentId); diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/component/NoComponentProvider.java b/server/sonar-server/src/test/java/org/sonar/server/computation/component/NoComponentProvider.java new file mode 100644 index 00000000000..bfdc74e5db6 --- /dev/null +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/component/NoComponentProvider.java @@ -0,0 +1,41 @@ +/* + * 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 enum NoComponentProvider implements ComponentProvider { + INSTANCE; + + private static final String ERROR_MSG = "Can not add a measure by Component ref if MeasureRepositoryRule has not been created for some Component provider"; + + @Override + public void ensureInitialized() { + throw new IllegalStateException(ERROR_MSG); + } + + @Override + public void reset() { + // do nothing + } + + @Override + public Component getByRef(int componentRef) { + throw new IllegalStateException(ERROR_MSG); + } +} diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/component/TreeComponentProvider.java b/server/sonar-server/src/test/java/org/sonar/server/computation/component/TreeComponentProvider.java index a58aef6bd39..93606568cce 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/component/TreeComponentProvider.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/component/TreeComponentProvider.java @@ -24,10 +24,21 @@ import java.util.Map; import static com.google.common.base.Preconditions.checkState; -public final class TreeComponentProvider implements ComponentProvider { +public final class TreeComponentProvider extends AbstractComponentProvider { + private final Component root; private final Map componentsByRef = new HashMap<>(); public TreeComponentProvider(Component root) { + this.root = root; + ensureInitialized(); + } + + private static String getRef(Component component) { + return component.getType().isReportType() ? String.valueOf(component.getReportAttributes().getRef()) : component.getKey(); + } + + @Override + protected void ensureInitializedImpl() { new DepthTraversalTypeAwareCrawler( new TypeAwareVisitorAdapter(CrawlerDepthLimit.LEAVES, ComponentVisitor.Order.PRE_ORDER) { @Override @@ -39,22 +50,13 @@ public final class TreeComponentProvider implements ComponentProvider { }).visit(root); } - private static String getRef(Component component) { - return component.getType().isReportType() ? String.valueOf(component.getReportAttributes().getRef()) : component.getKey(); - } - - @Override - public void init() { - // nothing to do, init done in constructor - } - @Override - public void reset() { + protected void resetImpl() { // we can not reset } @Override - public Component getByRef(int componentRef) { + protected Component getByRefImpl(int componentRef) { Component component = componentsByRef.get(String.valueOf(componentRef)); checkState(component != null, "Can not find Component for ref " + componentRef); return component; diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/component/TreeRootHolderComponentProvider.java b/server/sonar-server/src/test/java/org/sonar/server/computation/component/TreeRootHolderComponentProvider.java index 3e72a97d542..084d180601d 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/component/TreeRootHolderComponentProvider.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/component/TreeRootHolderComponentProvider.java @@ -19,7 +19,7 @@ */ package org.sonar.server.computation.component; -public final class TreeRootHolderComponentProvider implements ComponentProvider { +public final class TreeRootHolderComponentProvider extends AbstractComponentProvider { private final TreeRootHolder treeRootHolder; private TreeComponentProvider delegate; @@ -28,20 +28,20 @@ public final class TreeRootHolderComponentProvider implements ComponentProvider } @Override - public void init() { + protected void ensureInitializedImpl() { if (this.delegate == null) { this.delegate = new TreeComponentProvider(treeRootHolder.getRoot()); - this.delegate.init(); + this.delegate.ensureInitialized(); } } @Override - public void reset() { + protected void resetImpl() { this.delegate = null; } @Override - public Component getByRef(int componentRef) { + protected Component getByRefImpl(int componentRef) { return delegate.getByRef(componentRef); } } diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/measure/MeasureRepositoryRule.java b/server/sonar-server/src/test/java/org/sonar/server/computation/measure/MeasureRepositoryRule.java index 4aec1bce938..d435cc11e38 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/measure/MeasureRepositoryRule.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/measure/MeasureRepositoryRule.java @@ -34,6 +34,7 @@ import javax.annotation.Nullable; import org.junit.rules.ExternalResource; import org.sonar.server.computation.component.Component; import org.sonar.server.computation.component.ComponentProvider; +import org.sonar.server.computation.component.NoComponentProvider; import org.sonar.server.computation.component.TreeComponentProvider; import org.sonar.server.computation.component.TreeRootHolder; import org.sonar.server.computation.component.TreeRootHolderComponentProvider; @@ -53,7 +54,6 @@ import static java.util.Objects.requireNonNull; * providers. */ public class MeasureRepositoryRule extends ExternalResource implements MeasureRepository { - @CheckForNull private final ComponentProvider componentProvider; @CheckForNull private final MetricRepositoryRule metricRepositoryRule; @@ -68,22 +68,20 @@ public class MeasureRepositoryRule extends ExternalResource implements MeasureRe } }; - private MeasureRepositoryRule(@Nullable ComponentProvider componentProvider, @Nullable MetricRepositoryRule metricRepositoryRule) { + private MeasureRepositoryRule(ComponentProvider componentProvider, @Nullable MetricRepositoryRule metricRepositoryRule) { this.componentProvider = componentProvider; this.metricRepositoryRule = metricRepositoryRule; } @Override protected void after() { - if (componentProvider != null) { - componentProvider.reset(); - } + componentProvider.reset(); baseMeasures.clear(); rawMeasures.clear(); } public static MeasureRepositoryRule create() { - return new MeasureRepositoryRule(null, null); + return new MeasureRepositoryRule(NoComponentProvider.INSTANCE, null); } public static MeasureRepositoryRule create(TreeRootHolder treeRootHolder, MetricRepositoryRule metricRepositoryRule) { @@ -305,9 +303,8 @@ public class MeasureRepositoryRule extends ExternalResource implements MeasureRe } private void checkAndInitProvidersState() { - checkState(componentProvider != null, "Can not add a measure by Component ref if MeasureRepositoryRule has not been created for some Component provider"); checkState(metricRepositoryRule != null, "Can not add a measure by metric key if MeasureRepositoryRule has not been created for a MetricRepository"); - componentProvider.init(); + componentProvider.ensureInitialized(); } public boolean isEmpty() {