This is an automated email from the ASF dual-hosted git repository. rombert pushed a commit to annotated tag org.apache.sling.testing.sling-mock-2.2.0 in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-testing-sling-mock.git
commit 99270aa95ed496a2ab46f5bf6f9b5cf32b713687 Author: Stefan Seifert <[email protected]> AuthorDate: Sat Dec 3 13:14:34 2016 +0000 SLING-6359 context plugin support git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/testing/mocks/sling-mock@1772466 13f79535-47bb-0310-9956-ffa450edef68 --- .../context/ContextResourceResolverFactory.java | 2 + .../sling/context/ModelAdapterFactoryUtil.java | 2 + .../testing/mock/sling/context/UniqueRoot.java | 2 + .../testing/mock/sling/context/package-info.java | 2 +- .../testing/mock/sling/junit/CallbackParams.java | 43 --------- .../testing/mock/sling/junit/SlingContext.java | 100 ++++++--------------- .../mock/sling/junit/SlingContextBuilder.java | 52 ++++++----- .../mock/sling/junit/SlingContextCallback.java | 6 +- 8 files changed, 70 insertions(+), 139 deletions(-) diff --git a/src/main/java/org/apache/sling/testing/mock/sling/context/ContextResourceResolverFactory.java b/src/main/java/org/apache/sling/testing/mock/sling/context/ContextResourceResolverFactory.java index 65cf6d2..3b61fdb 100644 --- a/src/main/java/org/apache/sling/testing/mock/sling/context/ContextResourceResolverFactory.java +++ b/src/main/java/org/apache/sling/testing/mock/sling/context/ContextResourceResolverFactory.java @@ -24,11 +24,13 @@ import org.apache.sling.api.resource.LoginException; import org.apache.sling.api.resource.ResourceResolverFactory; import org.apache.sling.testing.mock.sling.MockSling; import org.apache.sling.testing.mock.sling.ResourceResolverType; +import org.osgi.annotation.versioning.ProviderType; import org.osgi.framework.BundleContext; /** * Create resolve resolver instance and initialize it depending on it's type. */ +@ProviderType final class ContextResourceResolverFactory { private ContextResourceResolverFactory() { diff --git a/src/main/java/org/apache/sling/testing/mock/sling/context/ModelAdapterFactoryUtil.java b/src/main/java/org/apache/sling/testing/mock/sling/context/ModelAdapterFactoryUtil.java index e0044a4..478c101 100644 --- a/src/main/java/org/apache/sling/testing/mock/sling/context/ModelAdapterFactoryUtil.java +++ b/src/main/java/org/apache/sling/testing/mock/sling/context/ModelAdapterFactoryUtil.java @@ -34,6 +34,7 @@ import java.util.Vector; import org.apache.sling.models.annotations.Model; import org.apache.sling.testing.mock.osgi.MockOsgi; +import org.osgi.annotation.versioning.ProviderType; import org.osgi.framework.Bundle; import org.osgi.framework.BundleContext; import org.osgi.framework.BundleEvent; @@ -45,6 +46,7 @@ import org.reflections.Reflections; /** * Helper methos for simulating sling models bundle events. */ +@ProviderType final class ModelAdapterFactoryUtil { static { diff --git a/src/main/java/org/apache/sling/testing/mock/sling/context/UniqueRoot.java b/src/main/java/org/apache/sling/testing/mock/sling/context/UniqueRoot.java index 39bc2cc..7e897ef 100644 --- a/src/main/java/org/apache/sling/testing/mock/sling/context/UniqueRoot.java +++ b/src/main/java/org/apache/sling/testing/mock/sling/context/UniqueRoot.java @@ -26,6 +26,7 @@ import org.apache.jackrabbit.JcrConstants; import org.apache.sling.api.resource.PersistenceException; import org.apache.sling.api.resource.Resource; import org.apache.sling.api.resource.ResourceUtil; +import org.osgi.annotation.versioning.ConsumerType; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -37,6 +38,7 @@ import com.google.common.collect.ImmutableMap; * where the repository is not cleaned for each test run. This class provides * unique root paths for each run, and cleans them up when done. */ +@ConsumerType public class UniqueRoot { private final SlingContextImpl context; diff --git a/src/main/java/org/apache/sling/testing/mock/sling/context/package-info.java b/src/main/java/org/apache/sling/testing/mock/sling/context/package-info.java index d5b9338..cae884c 100644 --- a/src/main/java/org/apache/sling/testing/mock/sling/context/package-info.java +++ b/src/main/java/org/apache/sling/testing/mock/sling/context/package-info.java @@ -19,5 +19,5 @@ /** * Sling context implementation for unit tests. */ [email protected]("3.3") [email protected]("3.4") package org.apache.sling.testing.mock.sling.context; diff --git a/src/main/java/org/apache/sling/testing/mock/sling/junit/CallbackParams.java b/src/main/java/org/apache/sling/testing/mock/sling/junit/CallbackParams.java deleted file mode 100644 index 680ce04..0000000 --- a/src/main/java/org/apache/sling/testing/mock/sling/junit/CallbackParams.java +++ /dev/null @@ -1,43 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.sling.testing.mock.sling.junit; - -import org.apache.sling.testing.mock.osgi.junit.ContextCallback; - -final class CallbackParams { - - ContextCallback[] beforeSetUpCallback; - ContextCallback[] afterSetUpCallback; - ContextCallback[] beforeTearDownCallback; - ContextCallback[] afterTearDownCallback; - - CallbackParams() { - // no callbacks - } - - CallbackParams(ContextCallback afterSetUpCallback) { - this.afterSetUpCallback = new ContextCallback[] { afterSetUpCallback }; - } - - CallbackParams(ContextCallback afterSetUpCallback, ContextCallback beforeTearDownCallback) { - this.afterSetUpCallback = new ContextCallback[] { afterSetUpCallback }; - this.beforeTearDownCallback = new ContextCallback[] { beforeTearDownCallback }; - } - -} diff --git a/src/main/java/org/apache/sling/testing/mock/sling/junit/SlingContext.java b/src/main/java/org/apache/sling/testing/mock/sling/junit/SlingContext.java index 37cfdcf..bcd5da5 100644 --- a/src/main/java/org/apache/sling/testing/mock/sling/junit/SlingContext.java +++ b/src/main/java/org/apache/sling/testing/mock/sling/junit/SlingContext.java @@ -20,22 +20,26 @@ package org.apache.sling.testing.mock.sling.junit; import java.util.Map; -import org.apache.sling.testing.mock.osgi.junit.ContextCallback; +import org.apache.sling.testing.mock.osgi.context.ContextCallback; +import org.apache.sling.testing.mock.osgi.context.ContextPlugins; +import org.apache.sling.testing.mock.osgi.context.OsgiContextImpl; import org.apache.sling.testing.mock.sling.ResourceResolverType; import org.apache.sling.testing.mock.sling.context.SlingContextImpl; import org.junit.rules.ExternalResource; import org.junit.rules.TestRule; import org.junit.runner.Description; import org.junit.runners.model.Statement; +import org.osgi.annotation.versioning.ProviderType; /** * JUnit rule for setting up and tearing down Sling context objects for unit tests. * You can use {@link SlingContextBuilder} alternatively to the constructors on this class - it offers * more options and fine-grained control about setting up the test context. */ +@ProviderType public final class SlingContext extends SlingContextImpl implements TestRule { - private final CallbackParams callbackParams; + private final ContextPlugins plugins; private final TestRule delegate; /** @@ -43,7 +47,7 @@ public final class SlingContext extends SlingContextImpl implements TestRule { * {@link org.apache.sling.testing.mock.sling.MockSling#DEFAULT_RESOURCERESOLVER_TYPE}. */ public SlingContext() { - this(new CallbackParams(), null, null); + this(new ContextPlugins(), null, null); } /** @@ -51,59 +55,65 @@ public final class SlingContext extends SlingContextImpl implements TestRule { * @param resourceResolverType Resource resolver type. */ public SlingContext(final ResourceResolverType resourceResolverType) { - this(new CallbackParams(), null, resourceResolverType); + this(new ContextPlugins(), null, resourceResolverType); } /** * Initialize Sling context with default resource resolver type: * {@link org.apache.sling.testing.mock.sling.MockSling#DEFAULT_RESOURCERESOLVER_TYPE}. + * @param <T> context type * @param afterSetUpCallback Allows the application to register an own callback function that is called after the built-in setup rules are executed. */ - public SlingContext(final ContextCallback<?> afterSetUpCallback) { - this(new CallbackParams(afterSetUpCallback), null, null); + public <T extends OsgiContextImpl> SlingContext(final ContextCallback<T> afterSetUpCallback) { + this(new ContextPlugins(afterSetUpCallback), null, null); } /** * Initialize Sling context with resource resolver type. + * @param <T> context type * @param afterSetUpCallback Allows the application to register an own callback function that is called after the built-in setup rules are executed. * @param resourceResolverType Resource resolver type. */ - public SlingContext(final ContextCallback<?> afterSetUpCallback, final ResourceResolverType resourceResolverType) { - this(new CallbackParams(afterSetUpCallback), null, resourceResolverType); + public <T extends OsgiContextImpl> SlingContext(final ContextCallback<T> afterSetUpCallback, final ResourceResolverType resourceResolverType) { + this(new ContextPlugins(afterSetUpCallback), null, resourceResolverType); } /** * Initialize Sling context with default resource resolver type: * {@link org.apache.sling.testing.mock.sling.MockSling#DEFAULT_RESOURCERESOLVER_TYPE}. + * @param <U> context type + * @param <V> context type * @param afterSetUpCallback Allows the application to register an own callback function that is called after the built-in setup rules are executed. * @param beforeTearDownCallback Allows the application to register an own callback function that is called before the built-in teardown rules are executed. */ - public SlingContext(final ContextCallback<?> afterSetUpCallback, final ContextCallback<?> beforeTearDownCallback) { - this(new CallbackParams(afterSetUpCallback, beforeTearDownCallback), null, null); + public <U extends OsgiContextImpl, V extends OsgiContextImpl> SlingContext(final ContextCallback<U> afterSetUpCallback, final ContextCallback<V> beforeTearDownCallback) { + this(new ContextPlugins(afterSetUpCallback, beforeTearDownCallback), null, null); } /** * Initialize Sling context with resource resolver type. + * @param <U> context type + * @param <V> context type * @param afterSetUpCallback Allows the application to register an own callback function that is called after the built-in setup rules are executed. * @param beforeTearDownCallback Allows the application to register an own callback function that is called before the built-in teardown rules are executed. * @param resourceResolverType Resource resolver type. */ - public SlingContext(final ContextCallback<?> afterSetUpCallback, final ContextCallback<?> beforeTearDownCallback, + public <U extends OsgiContextImpl, V extends OsgiContextImpl> SlingContext(final ContextCallback<U> afterSetUpCallback, final ContextCallback<V> beforeTearDownCallback, final ResourceResolverType resourceResolverType) { - this(new CallbackParams(afterSetUpCallback, beforeTearDownCallback), null, resourceResolverType); + this(new ContextPlugins(afterSetUpCallback, beforeTearDownCallback), null, resourceResolverType); } /** * Initialize Sling context with resource resolver type. - * @param callbackParams Callback parameters + * @param contextPlugins Context plugins * @param resourceResolverFactoryActivatorProps Allows to override OSGi configuration parameters for the Resource Resolver Factory Activator service. * @param resourceResolverType Resource resolver type. */ - SlingContext(final CallbackParams callbackParams, + SlingContext(final ContextPlugins contextPlugins, final Map<String, Object> resourceResolverFactoryActivatorProps, final ResourceResolverType resourceResolverType) { - this.callbackParams = callbackParams; + this.plugins = contextPlugins; setResourceResolverFactoryActivatorProps(resourceResolverFactoryActivatorProps); // set resource resolver type in parent context @@ -113,16 +123,16 @@ public final class SlingContext extends SlingContextImpl implements TestRule { this.delegate = new ExternalResource() { @Override protected void before() { - SlingContext.this.executeBeforeSetUpCallback(); + plugins.executeBeforeSetUpCallback(SlingContext.this); SlingContext.this.setUp(); - SlingContext.this.executeAfterSetUpCallback(); + plugins.executeAfterSetUpCallback(SlingContext.this); } @Override protected void after() { - SlingContext.this.executeBeforeTearDownCallback(); + plugins.executeBeforeTearDownCallback(SlingContext.this); SlingContext.this.tearDown(); - SlingContext.this.executeAfterTearDownCallback(); + plugins.executeAfterTearDownCallback(SlingContext.this); } }; } @@ -132,56 +142,4 @@ public final class SlingContext extends SlingContextImpl implements TestRule { return this.delegate.apply(base, description); } - @SuppressWarnings("unchecked") - private void executeBeforeSetUpCallback() { - if (callbackParams.beforeSetUpCallback != null) { - try { - for (ContextCallback callback : callbackParams.beforeSetUpCallback) { - callback.execute(this); - } - } catch (Throwable ex) { - throw new RuntimeException("Before setup failed: " + ex.getMessage(), ex); - } - } - } - - @SuppressWarnings("unchecked") - private void executeAfterSetUpCallback() { - if (callbackParams.afterSetUpCallback != null) { - try { - for (ContextCallback callback : callbackParams.afterSetUpCallback) { - callback.execute(this); - } - } catch (Throwable ex) { - throw new RuntimeException("After setup failed: " + ex.getMessage(), ex); - } - } - } - - @SuppressWarnings("unchecked") - private void executeBeforeTearDownCallback() { - if (callbackParams.beforeTearDownCallback != null) { - try { - for (ContextCallback callback : callbackParams.beforeTearDownCallback) { - callback.execute(this); - } - } catch (Throwable ex) { - throw new RuntimeException("Before teardown failed: " + ex.getMessage(), ex); - } - } - } - - @SuppressWarnings("unchecked") - private void executeAfterTearDownCallback() { - if (callbackParams.afterTearDownCallback != null) { - try { - for (ContextCallback callback : callbackParams.afterTearDownCallback) { - callback.execute(this); - } - } catch (Throwable ex) { - throw new RuntimeException("After teardown failed: " + ex.getMessage(), ex); - } - } - } - } diff --git a/src/main/java/org/apache/sling/testing/mock/sling/junit/SlingContextBuilder.java b/src/main/java/org/apache/sling/testing/mock/sling/junit/SlingContextBuilder.java index 38adb2c..b9ca1c4 100644 --- a/src/main/java/org/apache/sling/testing/mock/sling/junit/SlingContextBuilder.java +++ b/src/main/java/org/apache/sling/testing/mock/sling/junit/SlingContextBuilder.java @@ -20,15 +20,20 @@ package org.apache.sling.testing.mock.sling.junit; import java.util.Map; -import org.apache.sling.testing.mock.osgi.junit.ContextCallback; +import org.apache.sling.testing.mock.osgi.context.ContextCallback; +import org.apache.sling.testing.mock.osgi.context.ContextPlugin; +import org.apache.sling.testing.mock.osgi.context.ContextPlugins; +import org.apache.sling.testing.mock.osgi.context.OsgiContextImpl; import org.apache.sling.testing.mock.sling.ResourceResolverType; +import org.osgi.annotation.versioning.ProviderType; /** * Builder class for creating {@link SlingContext} instances with different sets of parameters. */ +@ProviderType public final class SlingContextBuilder { - private final CallbackParams callbackParams = new CallbackParams(); + private final ContextPlugins plugins = new ContextPlugins(); private ResourceResolverType resourceResolverType; private Map<String, Object> resourceResolverFactoryActivatorProps; @@ -55,54 +60,57 @@ public final class SlingContextBuilder { } /** - * @param afterSetUpCallback Allows the application to register an own callback function that is called after the built-in setup rules are executed. + * @param <T> context type + * @param plugin Context plugin which listens to context lifecycle events. * @return this */ - public SlingContextBuilder setUp(ContextCallback... afterSetUpCallback) { - return afterSetUp(afterSetUpCallback); + @SafeVarargs + public final <T extends OsgiContextImpl> SlingContextBuilder plugin(ContextPlugin<T>... plugin) { + plugins.addPlugin(plugin); + return this; } /** + * @param <T> context type * @param beforeSetUpCallback Allows the application to register an own callback function that is called before the built-in setup rules are executed. * @return this */ - public SlingContextBuilder beforeSetUp(ContextCallback... beforeSetUpCallback) { - callbackParams.beforeSetUpCallback = beforeSetUpCallback; + @SafeVarargs + public final <T extends OsgiContextImpl> SlingContextBuilder beforeSetUp(ContextCallback<T>... beforeSetUpCallback) { + plugins.addBeforeSetUpCallback(beforeSetUpCallback); return this; } /** + * @param <T> context type * @param afterSetUpCallback Allows the application to register an own callback function that is called after the built-in setup rules are executed. * @return this */ - public SlingContextBuilder afterSetUp(ContextCallback... afterSetUpCallback) { - callbackParams.afterSetUpCallback = afterSetUpCallback; + @SafeVarargs + public final <T extends OsgiContextImpl> SlingContextBuilder afterSetUp(ContextCallback<T>... afterSetUpCallback) { + plugins.addAfterSetUpCallback(afterSetUpCallback); return this; } /** + * @param <T> context type * @param beforeTearDownCallback Allows the application to register an own callback function that is called before the built-in teardown rules are executed. * @return this */ - public SlingContextBuilder tearDown(ContextCallback... beforeTearDownCallback) { - return beforeTearDown(beforeTearDownCallback); - } - - /** - * @param beforeTearDownCallback Allows the application to register an own callback function that is called before the built-in teardown rules are executed. - * @return this - */ - public SlingContextBuilder beforeTearDown(ContextCallback... beforeTearDownCallback) { - callbackParams.beforeTearDownCallback = beforeTearDownCallback; + @SafeVarargs + public final <T extends OsgiContextImpl> SlingContextBuilder beforeTearDown(ContextCallback<T>... beforeTearDownCallback) { + plugins.addBeforeTearDownCallback(beforeTearDownCallback); return this; } /** + * @param <T> context type * @param afterTearDownCallback Allows the application to register an own callback function that is after before the built-in teardown rules are executed. * @return this */ - public SlingContextBuilder afterTearDown(ContextCallback... afterTearDownCallback) { - callbackParams.afterTearDownCallback = afterTearDownCallback; + @SafeVarargs + public final <T extends OsgiContextImpl> SlingContextBuilder afterTearDown(ContextCallback<T>... afterTearDownCallback) { + plugins.addAfterTearDownCallback(afterTearDownCallback); return this; } @@ -120,7 +128,7 @@ public final class SlingContextBuilder { * @return Build {@link SlingContext} instance. */ public SlingContext build() { - return new SlingContext(this.callbackParams, + return new SlingContext(this.plugins, this.resourceResolverFactoryActivatorProps, this.resourceResolverType); } diff --git a/src/main/java/org/apache/sling/testing/mock/sling/junit/SlingContextCallback.java b/src/main/java/org/apache/sling/testing/mock/sling/junit/SlingContextCallback.java index 4efdf44..79343c3 100644 --- a/src/main/java/org/apache/sling/testing/mock/sling/junit/SlingContextCallback.java +++ b/src/main/java/org/apache/sling/testing/mock/sling/junit/SlingContextCallback.java @@ -18,12 +18,14 @@ */ package org.apache.sling.testing.mock.sling.junit; -import org.apache.sling.testing.mock.osgi.junit.ContextCallback; +import org.apache.sling.testing.mock.osgi.context.ContextCallback; +import org.osgi.annotation.versioning.ConsumerType; /** - * Callback-interface for application-specific setup and teardown operations to + * Callback interface for application-specific setup and teardown operations to * customize the {@link SlingContext} JUnit rule. */ +@ConsumerType public interface SlingContextCallback extends ContextCallback<SlingContext> { // specialized version of ContextCallback -- To stop receiving notification emails like this one, please contact "[email protected]" <[email protected]>.
