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-1.9.0 in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-testing-sling-mock.git
commit 6e46477439d0e78689910cf2a23e44c40a78a43f 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/branches/testing/mocks/sling-mock-1.x@1772466 13f79535-47bb-0310-9956-ffa450edef68 --- .../context/ContextResourceResolverFactory.java | 3 + .../sling/context/ModelAdapterFactoryUtil.java | 3 + .../testing/mock/sling/context/UniqueRoot.java | 3 + .../testing/mock/sling/junit/CallbackParams.java | 43 --------- .../testing/mock/sling/junit/SlingContext.java | 105 ++++++--------------- .../mock/sling/junit/SlingContextBuilder.java | 75 ++++++++------- .../mock/sling/junit/SlingContextCallback.java | 7 +- .../testing/mock/sling/junit/SlingContextTest.java | 1 + 8 files changed, 84 insertions(+), 156 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 2448e07..64dcff8 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 @@ -26,9 +26,12 @@ import org.apache.sling.testing.mock.sling.MockSling; import org.apache.sling.testing.mock.sling.ResourceResolverType; import org.osgi.framework.BundleContext; +import aQute.bnd.annotation.ProviderType; + /** * 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 3cc6327..52568d3 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 @@ -39,9 +39,12 @@ import org.osgi.framework.ServiceReference; import org.osgi.framework.Version; import org.reflections.Reflections; +import aQute.bnd.annotation.ProviderType; + /** * 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..cc00d75 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 @@ -31,12 +31,15 @@ import org.slf4j.LoggerFactory; import com.google.common.collect.ImmutableMap; +import aQute.bnd.annotation.ConsumerType; + /** * Manages unique root paths in JCR repository. * This is important for resource resolver types like JCR_JACKRABBIT * 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/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 9201f79..52aa2ff 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,7 +20,9 @@ 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; @@ -28,14 +30,17 @@ import org.junit.rules.TestRule; import org.junit.runner.Description; import org.junit.runners.model.Statement; +import aQute.bnd.annotation.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 +48,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 +56,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 SlingContextCallback 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 SlingContextCallback 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 SlingContextCallback afterSetUpCallback, final SlingContextCallback 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 SlingContextCallback afterSetUpCallback, final SlingContextCallback 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 +124,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,60 +143,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 3fd870f..37780dd 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,21 @@ 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 aQute.bnd.annotation.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,61 +61,58 @@ 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. - * @return this - */ - public SlingContextBuilder setUp(ContextCallback... afterSetUpCallback) { - return afterSetUp(afterSetUpCallback); - } - - /** - * @param beforeSetUpCallback Allows the application to register an own callback function that is called before 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 beforeSetUp(ContextCallback... beforeSetUpCallback) { - callbackParams.beforeSetUpCallback = beforeSetUpCallback; - return this; + @SafeVarargs + public final <T extends OsgiContextImpl> SlingContextBuilder plugin(ContextPlugin<T>... plugin) { + plugins.addPlugin(plugin); + return this; } /** - * @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 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 afterSetUp(ContextCallback... afterSetUpCallback) { - callbackParams.afterSetUpCallback = afterSetUpCallback; - return this; + @SafeVarargs + public final <T extends OsgiContextImpl> SlingContextBuilder beforeSetUp(ContextCallback<T>... beforeSetUpCallback) { + plugins.addBeforeSetUpCallback(beforeSetUpCallback); + return this; } /** - * @param beforeTearDownCallback Allows the application to register an own callback function that is called before the - * built-in teardown rules are executed. + * @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 tearDown(ContextCallback... beforeTearDownCallback) { - return beforeTearDown(beforeTearDownCallback); + @SafeVarargs + public final <T extends OsgiContextImpl> SlingContextBuilder afterSetUp(ContextCallback<T>... afterSetUpCallback) { + plugins.addAfterSetUpCallback(afterSetUpCallback); + return this; } /** - * @param beforeTearDownCallback Allows the application to register an own callback function that is called before the - * built-in teardown rules are executed. + * @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 beforeTearDown(ContextCallback... beforeTearDownCallback) { - callbackParams.beforeTearDownCallback = beforeTearDownCallback; - return this; + @SafeVarargs + public final <T extends OsgiContextImpl> SlingContextBuilder beforeTearDown(ContextCallback<T>... beforeTearDownCallback) { + plugins.addBeforeTearDownCallback(beforeTearDownCallback); + return this; } /** - * @param afterTearDownCallback Allows the application to register an own callback function that is after before the - * built-in teardown rules are executed. + * @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; - return this; + @SafeVarargs + public final <T extends OsgiContextImpl> SlingContextBuilder afterTearDown(ContextCallback<T>... afterTearDownCallback) { + plugins.addAfterTearDownCallback(afterTearDownCallback); + return this; } /** @@ -126,7 +129,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..d4fc9c4 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,15 @@ */ 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 aQute.bnd.annotation.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 diff --git a/src/test/java/org/apache/sling/testing/mock/sling/junit/SlingContextTest.java b/src/test/java/org/apache/sling/testing/mock/sling/junit/SlingContextTest.java index d186c92..ead89a6 100644 --- a/src/test/java/org/apache/sling/testing/mock/sling/junit/SlingContextTest.java +++ b/src/test/java/org/apache/sling/testing/mock/sling/junit/SlingContextTest.java @@ -46,6 +46,7 @@ public class SlingContextTest { private final SlingContextCallback contextAfterTeardown = mock(SlingContextCallback.class); // Run all unit tests for each resource resolver types listed here + @SuppressWarnings("unchecked") @Rule public SlingContext context = new SlingContextBuilder(ResourceResolverType.JCR_MOCK) .beforeSetUp(contextBeforeSetup) -- To stop receiving notification emails like this one, please contact "[email protected]" <[email protected]>.
