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 9125ce1e9885d68fc7c3a0d1ff1a3d1e951fa72b Author: Stefan Seifert <[email protected]> AuthorDate: Sat Dec 3 10:01:00 2016 +0000 SLING-6359 osgi-mock, sling-mock: Make ContextCallback and ContextBuilder more flexible git-svn-id: https://svn.apache.org/repos/asf/sling/branches/testing/mocks/sling-mock-1.x@1772443 13f79535-47bb-0310-9956-ffa450edef68 --- ...ingContextCallback.java => CallbackParams.java} | 36 ++++---- .../testing/mock/sling/junit/SlingContext.java | 95 ++++++++++++---------- .../mock/sling/junit/SlingContextBuilder.java | 63 +++++++++----- .../mock/sling/junit/SlingContextCallback.java | 14 +--- .../testing/mock/sling/junit/package-info.java | 2 +- .../testing/mock/sling/junit/SlingContextTest.java | 5 +- 6 files changed, 117 insertions(+), 98 deletions(-) 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/CallbackParams.java similarity index 53% copy from src/main/java/org/apache/sling/testing/mock/sling/junit/SlingContextCallback.java copy to src/main/java/org/apache/sling/testing/mock/sling/junit/CallbackParams.java index 247185b..680ce04 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/CallbackParams.java @@ -18,22 +18,26 @@ */ package org.apache.sling.testing.mock.sling.junit; -import java.io.IOException; +import org.apache.sling.testing.mock.osgi.junit.ContextCallback; -import org.apache.sling.api.resource.PersistenceException; - -/** - * Callback-interface for application-specific setup and teardown operations to - * customize the {@link SlingContext} JUnit rule. - */ -public interface SlingContextCallback { - - /** - * Execute callback action - * @param context Sling context - * @throws IOException I/O exception - * @throws PersistenceException Persistence exception - */ - void execute(SlingContext context) throws IOException, PersistenceException; +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 9dcd712..9201f79 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,6 +20,7 @@ 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.sling.ResourceResolverType; import org.apache.sling.testing.mock.sling.context.SlingContextImpl; import org.junit.rules.ExternalResource; @@ -34,10 +35,7 @@ import org.junit.runners.model.Statement; */ public final class SlingContext extends SlingContextImpl implements TestRule { - private final SlingContextCallback beforeSetUpCallback; - private final SlingContextCallback afterSetUpCallback; - private final SlingContextCallback beforeTearDownCallback; - private final SlingContextCallback afterTearDownCallback; + private final CallbackParams callbackParams; private final TestRule delegate; /** @@ -45,7 +43,7 @@ public final class SlingContext extends SlingContextImpl implements TestRule { * {@link org.apache.sling.testing.mock.sling.MockSling#DEFAULT_RESOURCERESOLVER_TYPE}. */ public SlingContext() { - this(null, null, null); + this(new CallbackParams(), null, null); } /** @@ -53,7 +51,7 @@ public final class SlingContext extends SlingContextImpl implements TestRule { * @param resourceResolverType Resource resolver type. */ public SlingContext(final ResourceResolverType resourceResolverType) { - this(null, null, resourceResolverType); + this(new CallbackParams(), null, resourceResolverType); } /** @@ -62,7 +60,7 @@ public final class SlingContext extends SlingContextImpl implements TestRule { * @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(afterSetUpCallback, null, null); + this(new CallbackParams(afterSetUpCallback), null, null); } /** @@ -71,7 +69,7 @@ public final class SlingContext extends SlingContextImpl implements TestRule { * @param resourceResolverType Resource resolver type. */ public SlingContext(final SlingContextCallback afterSetUpCallback, final ResourceResolverType resourceResolverType) { - this(afterSetUpCallback, null, resourceResolverType); + this(new CallbackParams(afterSetUpCallback), null, resourceResolverType); } /** @@ -81,7 +79,7 @@ public final class SlingContext extends SlingContextImpl implements TestRule { * @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(afterSetUpCallback, beforeTearDownCallback, null); + this(new CallbackParams(afterSetUpCallback, beforeTearDownCallback), null, null); } /** @@ -92,27 +90,20 @@ public final class SlingContext extends SlingContextImpl implements TestRule { */ public SlingContext(final SlingContextCallback afterSetUpCallback, final SlingContextCallback beforeTearDownCallback, final ResourceResolverType resourceResolverType) { - this(null, afterSetUpCallback, beforeTearDownCallback, null, null, resourceResolverType); + this(new CallbackParams(afterSetUpCallback, beforeTearDownCallback), null, resourceResolverType); } /** * Initialize Sling context with resource resolver type. - * @param beforeSetUpCallback Allows the application to register an own callback function that is called before the built-in setup rules are executed. - * @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 afterTearDownCallback Allows the application to register an own callback function that is after before the built-in teardown rules are executed. + * @param callbackParams Callback parameters * @param resourceResolverFactoryActivatorProps Allows to override OSGi configuration parameters for the Resource Resolver Factory Activator service. * @param resourceResolverType Resource resolver type. */ - SlingContext(final SlingContextCallback beforeSetUpCallback, final SlingContextCallback afterSetUpCallback, - final SlingContextCallback beforeTearDownCallback, final SlingContextCallback afterTearDownCallback, + SlingContext(final CallbackParams callbackParams, final Map<String, Object> resourceResolverFactoryActivatorProps, final ResourceResolverType resourceResolverType) { - this.beforeSetUpCallback = beforeSetUpCallback; - this.afterSetUpCallback = afterSetUpCallback; - this.beforeTearDownCallback = beforeTearDownCallback; - this.afterTearDownCallback = afterTearDownCallback; + this.callbackParams = callbackParams; setResourceResolverFactoryActivatorProps(resourceResolverFactoryActivatorProps); // set resource resolver type in parent context @@ -141,44 +132,60 @@ public final class SlingContext extends SlingContextImpl implements TestRule { return this.delegate.apply(base, description); } + @SuppressWarnings("unchecked") private void executeBeforeSetUpCallback() { - if (this.beforeSetUpCallback != null) { - try { - this.beforeSetUpCallback.execute(this); - } catch (Throwable ex) { - throw new RuntimeException("Before setup failed: " + ex.getMessage(), ex); - } + 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 (this.afterSetUpCallback != null) { - try { - this.afterSetUpCallback.execute(this); - } catch (Throwable ex) { - throw new RuntimeException("After setup failed: " + ex.getMessage(), ex); - } + 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 (this.beforeTearDownCallback != null) { - try { - this.beforeTearDownCallback.execute(this); - } catch (Throwable ex) { - throw new RuntimeException("Before teardown failed: " + ex.getMessage(), ex); - } + 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 (this.afterTearDownCallback != null) { - try { - this.afterTearDownCallback.execute(this); - } catch (Throwable ex) { - throw new RuntimeException("After teardown failed: " + ex.getMessage(), ex); - } + 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 62080fa..3fd870f 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,6 +20,7 @@ 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.sling.ResourceResolverType; /** @@ -27,11 +28,8 @@ import org.apache.sling.testing.mock.sling.ResourceResolverType; */ public final class SlingContextBuilder { + private final CallbackParams callbackParams = new CallbackParams(); private ResourceResolverType resourceResolverType; - private SlingContextCallback beforeSetUpCallback; - private SlingContextCallback afterSetUpCallback; - private SlingContextCallback beforeTearDownCallback; - private SlingContextCallback afterTearDownCallback; private Map<String, Object> resourceResolverFactoryActivatorProps; /** @@ -57,39 +55,61 @@ public final class SlingContextBuilder { } /** - * @param beforeSetUpCallback Allows the application to register an own callback function that is called before the built-in setup rules are executed. + * @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 beforeSetUp(SlingContextCallback beforeSetUpCallback) { - this.beforeSetUpCallback = beforeSetUpCallback; - return this; + public SlingContextBuilder setUp(ContextCallback... afterSetUpCallback) { + return afterSetUp(afterSetUpCallback); } /** - * @param afterSetUpCallback Allows the application to register an own callback function that is called after the built-in setup rules are executed. + * @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(SlingContextCallback afterSetUpCallback) { - this.afterSetUpCallback = afterSetUpCallback; - return this; + public SlingContextBuilder beforeSetUp(ContextCallback... beforeSetUpCallback) { + callbackParams.beforeSetUpCallback = 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 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 beforeTearDown(SlingContextCallback beforeTearDownCallback) { - this.beforeTearDownCallback = beforeTearDownCallback; - return this; + public SlingContextBuilder afterSetUp(ContextCallback... afterSetUpCallback) { + callbackParams.afterSetUpCallback = afterSetUpCallback; + 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 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 afterTearDown(SlingContextCallback afterTearDownCallback) { - this.afterTearDownCallback = afterTearDownCallback; - 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; + return this; + } + + /** + * @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; } /** @@ -106,8 +126,7 @@ public final class SlingContextBuilder { * @return Build {@link SlingContext} instance. */ public SlingContext build() { - return new SlingContext(this.beforeSetUpCallback, this.afterSetUpCallback, - this.beforeTearDownCallback, this.afterTearDownCallback, + return new SlingContext(this.callbackParams, 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 247185b..4efdf44 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,22 +18,14 @@ */ package org.apache.sling.testing.mock.sling.junit; -import java.io.IOException; - -import org.apache.sling.api.resource.PersistenceException; +import org.apache.sling.testing.mock.osgi.junit.ContextCallback; /** * Callback-interface for application-specific setup and teardown operations to * customize the {@link SlingContext} JUnit rule. */ -public interface SlingContextCallback { +public interface SlingContextCallback extends ContextCallback<SlingContext> { - /** - * Execute callback action - * @param context Sling context - * @throws IOException I/O exception - * @throws PersistenceException Persistence exception - */ - void execute(SlingContext context) throws IOException, PersistenceException; + // specialized version of ContextCallback } diff --git a/src/main/java/org/apache/sling/testing/mock/sling/junit/package-info.java b/src/main/java/org/apache/sling/testing/mock/sling/junit/package-info.java index 72cb53a..f8109b3 100644 --- a/src/main/java/org/apache/sling/testing/mock/sling/junit/package-info.java +++ b/src/main/java/org/apache/sling/testing/mock/sling/junit/package-info.java @@ -19,5 +19,5 @@ /** * Rule for providing easy access to Sling context in JUnit tests. */ [email protected]("3.3") [email protected]("4.0") package org.apache.sling.testing.mock.sling.junit; 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 d99ed80..d186c92 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 @@ -24,9 +24,6 @@ import static org.junit.Assert.assertNull; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; -import java.io.IOException; - -import org.apache.sling.api.resource.PersistenceException; import org.apache.sling.api.resource.Resource; import org.apache.sling.api.resource.ResourceResolver; import org.apache.sling.api.resource.ResourceUtil; @@ -63,7 +60,7 @@ public class SlingContextTest { .build(); @Before - public void setUp() throws IOException, PersistenceException { + public void setUp() throws Exception { verify(contextBeforeSetup).execute(context); verify(contextAfterSetup).execute(context); } -- To stop receiving notification emails like this one, please contact "[email protected]" <[email protected]>.
