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.0.0 in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-testing-sling-mock.git
commit 3239d982e81d3b20120ac88e09c8ee3188594150 Author: Stefan Seifert <[email protected]> AuthorDate: Fri Jul 8 15:45:02 2016 +0000 SLING-5770 add support for custom resource resolver factory activator configuration via SlingContextBuilder add support for additional callbacks before setup and after teardown git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/testing/mocks/sling-mock@1751927 13f79535-47bb-0310-9956-ffa450edef68 --- .../mock/sling/context/SlingContextImpl.java | 44 +++++++- .../testing/mock/sling/context/package-info.java | 2 +- .../testing/mock/sling/junit/SlingContext.java | 111 +++++++++++++------- .../mock/sling/junit/SlingContextBuilder.java | 115 +++++++++++++++++++++ .../testing/mock/sling/junit/package-info.java | 2 +- .../testing/mock/sling/junit/SlingContextTest.java | 42 +++++++- 6 files changed, 268 insertions(+), 48 deletions(-) diff --git a/src/main/java/org/apache/sling/testing/mock/sling/context/SlingContextImpl.java b/src/main/java/org/apache/sling/testing/mock/sling/context/SlingContextImpl.java index 66d6085..aceb506 100644 --- a/src/main/java/org/apache/sling/testing/mock/sling/context/SlingContextImpl.java +++ b/src/main/java/org/apache/sling/testing/mock/sling/context/SlingContextImpl.java @@ -18,6 +18,10 @@ */ package org.apache.sling.testing.mock.sling.context; +import java.io.IOException; +import java.util.Dictionary; +import java.util.Hashtable; +import java.util.Map; import java.util.Set; import javax.jcr.RepositoryException; @@ -55,13 +59,17 @@ import org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest; import org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletResponse; import org.osgi.framework.Constants; import org.osgi.framework.ServiceReference; - -import aQute.bnd.annotation.ConsumerType; +import org.osgi.service.cm.Configuration; +import org.osgi.service.cm.ConfigurationAdmin; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import com.google.common.base.Function; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; +import aQute.bnd.annotation.ConsumerType; + /** * Defines Sling context objects with lazy initialization. Should not be used * directly but via the {@link org.apache.sling.testing.mock.sling.junit.SlingContext} JUnit @@ -73,6 +81,10 @@ public class SlingContextImpl extends OsgiContextImpl { // default to publish instance run mode static final Set<String> DEFAULT_RUN_MODES = ImmutableSet.<String> builder().add("publish").build(); + private static final String RESOURCERESOLVERFACTORYACTIVATOR_PID = "org.apache.sling.jcr.resource.internal.JcrResourceResolverFactoryImpl"; + + private static final Logger log = LoggerFactory.getLogger(SlingContextImpl.class); + protected ResourceResolverFactory resourceResolverFactory; protected ResourceResolverType resourceResolverType; protected ResourceResolver resourceResolver; @@ -82,6 +94,8 @@ public class SlingContextImpl extends OsgiContextImpl { protected ContentLoader contentLoader; protected ContentBuilder contentBuilder; protected UniqueRoot uniqueRoot; + + private Map<String, Object> resourceResolverFactoryActivatorProps; /** * @param resourceResolverType Resource resolver type @@ -90,12 +104,38 @@ public class SlingContextImpl extends OsgiContextImpl { this.resourceResolverType = resourceResolverType; } + protected void setResourceResolverFactoryActivatorProps(Map<String, Object> props) { + this.resourceResolverFactoryActivatorProps = props; + } + /** * Setup actions before test method execution */ protected void setUp() { super.setUp(); MockSling.setAdapterManagerBundleContext(bundleContext()); + + if (this.resourceResolverFactoryActivatorProps != null) { + // use OSGi ConfigurationAdmin to pass over customized configuration to Resource Resolver Factory Activator service + ConfigurationAdmin configAdmin = getService(ConfigurationAdmin.class); + if (configAdmin == null) { + log.warn("ConfigAdmin not found in osgi-mock context - please make sure osgi-mock 1.7.0 or higher is used."); + } + else { + try { + Configuration resourceResolverFactoryActivatorConfig = configAdmin.getConfiguration(RESOURCERESOLVERFACTORYACTIVATOR_PID); + Dictionary<String, Object> props = new Hashtable<String, Object>(); + for (Map.Entry<String, Object> item : this.resourceResolverFactoryActivatorProps.entrySet()) { + props.put(item.getKey(), item.getValue()); + } + resourceResolverFactoryActivatorConfig.update(props); + } + catch (IOException ex) { + throw new RuntimeException(ex); + } + } + } + this.resourceResolverFactory = newResourceResolverFactory(); registerDefaultServices(); } 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 827160f..e440a9e 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.1") [email protected]("3.2") package org.apache.sling.testing.mock.sling.context; 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 2b8381b..7f7625a 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 @@ -18,6 +18,8 @@ */ package org.apache.sling.testing.mock.sling.junit; +import java.util.Map; + import org.apache.sling.testing.mock.sling.ResourceResolverType; import org.apache.sling.testing.mock.sling.context.SlingContextImpl; import org.junit.rules.ExternalResource; @@ -31,8 +33,10 @@ import org.junit.runners.model.Statement; */ public final class SlingContext extends SlingContextImpl implements TestRule { - private final SlingContextCallback setUpCallback; - private final SlingContextCallback tearDownCallback; + private final SlingContextCallback beforeSetUpCallback; + private final SlingContextCallback afterSetUpCallback; + private final SlingContextCallback beforeTearDownCallback; + private final SlingContextCallback afterTearDownCallback; private final TestRule delegate; /** @@ -54,54 +58,61 @@ public final class SlingContext extends SlingContextImpl implements TestRule { /** * Initialize Sling context with default resource resolver type: * {@link org.apache.sling.testing.mock.sling.MockSling#DEFAULT_RESOURCERESOLVER_TYPE}. - * @param setUpCallback Allows the application to register an own callback - * function that is called after 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. */ - public SlingContext(final SlingContextCallback setUpCallback) { - this(setUpCallback, null, null); + public SlingContext(final SlingContextCallback afterSetUpCallback) { + this(afterSetUpCallback, null, null); } /** * Initialize Sling context with resource resolver type. - * @param setUpCallback Allows the application to register an own callback - * function that is called after 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 resourceResolverType Resource resolver type. */ - public SlingContext(final SlingContextCallback setUpCallback, final ResourceResolverType resourceResolverType) { - this(setUpCallback, null, resourceResolverType); + public SlingContext(final SlingContextCallback afterSetUpCallback, final ResourceResolverType resourceResolverType) { + this(afterSetUpCallback, null, resourceResolverType); } /** * Initialize Sling context with default resource resolver type: * {@link org.apache.sling.testing.mock.sling.MockSling#DEFAULT_RESOURCERESOLVER_TYPE}. - * @param setUpCallback Allows the application to register an own callback - * function that is called after the built-in setup rules are - * executed. - * @param tearDownCallback 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. + * @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); + } + + /** + * Initialize Sling context with resource resolver 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 setUpCallback, final SlingContextCallback tearDownCallback) { - this(setUpCallback, tearDownCallback, null); + public SlingContext(final SlingContextCallback afterSetUpCallback, final SlingContextCallback beforeTearDownCallback, + final ResourceResolverType resourceResolverType) { + this(null, afterSetUpCallback, beforeTearDownCallback, null, null, resourceResolverType); } /** * Initialize Sling context with resource resolver type. - * @param setUpCallback Allows the application to register an own callback - * function that is called after the built-in setup rules are - * executed. - * @param tearDownCallback Allows the application to register an own - * callback function that is called before the built-in teardown - * 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. + * @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 resourceResolverFactoryActivatorProps Allows to override OSGi configuration parameters for the Resource Resolver Factory Activator service. * @param resourceResolverType Resource resolver type. */ - public SlingContext(final SlingContextCallback setUpCallback, final SlingContextCallback tearDownCallback, + SlingContext(final SlingContextCallback beforeSetUpCallback, final SlingContextCallback afterSetUpCallback, + final SlingContextCallback beforeTearDownCallback, final SlingContextCallback afterTearDownCallback, + final Map<String, Object> resourceResolverFactoryActivatorProps, final ResourceResolverType resourceResolverType) { - this.setUpCallback = setUpCallback; - this.tearDownCallback = tearDownCallback; + this.beforeSetUpCallback = beforeSetUpCallback; + this.afterSetUpCallback = afterSetUpCallback; + this.beforeTearDownCallback = beforeTearDownCallback; + this.afterTearDownCallback = afterTearDownCallback; + setResourceResolverFactoryActivatorProps(resourceResolverFactoryActivatorProps); // set resource resolver type in parent context setResourceResolverType(resourceResolverType); @@ -110,14 +121,16 @@ public final class SlingContext extends SlingContextImpl implements TestRule { this.delegate = new ExternalResource() { @Override protected void before() { + SlingContext.this.executeBeforeSetUpCallback(); SlingContext.this.setUp(); - SlingContext.this.executeSetUpCallback(); + SlingContext.this.executeAfterSetUpCallback(); } @Override protected void after() { - SlingContext.this.executeTearDownCallback(); + SlingContext.this.executeBeforeTearDownCallback(); SlingContext.this.tearDown(); + SlingContext.this.executeAfterTearDownCallback(); } }; } @@ -127,22 +140,42 @@ public final class SlingContext extends SlingContextImpl implements TestRule { return this.delegate.apply(base, description); } - private void executeSetUpCallback() { - if (this.setUpCallback != null) { + private void executeBeforeSetUpCallback() { + if (this.beforeSetUpCallback != null) { + try { + this.beforeSetUpCallback.execute(this); + } catch (Throwable ex) { + throw new RuntimeException("Before setup failed: " + ex.getMessage(), ex); + } + } + } + + private void executeAfterSetUpCallback() { + if (this.afterSetUpCallback != null) { + try { + this.afterSetUpCallback.execute(this); + } catch (Throwable ex) { + throw new RuntimeException("After setup failed: " + ex.getMessage(), ex); + } + } + } + + private void executeBeforeTearDownCallback() { + if (this.beforeTearDownCallback != null) { try { - this.setUpCallback.execute(this); + this.beforeTearDownCallback.execute(this); } catch (Throwable ex) { - throw new RuntimeException("Setup failed: " + ex.getMessage(), ex); + throw new RuntimeException("Before teardown failed: " + ex.getMessage(), ex); } } } - private void executeTearDownCallback() { - if (this.tearDownCallback != null) { + private void executeAfterTearDownCallback() { + if (this.afterTearDownCallback != null) { try { - this.tearDownCallback.execute(this); + this.afterTearDownCallback.execute(this); } catch (Throwable ex) { - throw new RuntimeException("Teardown failed: " + ex.getMessage(), 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 new file mode 100644 index 0000000..62080fa --- /dev/null +++ b/src/main/java/org/apache/sling/testing/mock/sling/junit/SlingContextBuilder.java @@ -0,0 +1,115 @@ +/* + * 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 java.util.Map; + +import org.apache.sling.testing.mock.sling.ResourceResolverType; + +/** + * Builder class for creating {@link SlingContext} instances with different sets of parameters. + */ +public final class SlingContextBuilder { + + private ResourceResolverType resourceResolverType; + private SlingContextCallback beforeSetUpCallback; + private SlingContextCallback afterSetUpCallback; + private SlingContextCallback beforeTearDownCallback; + private SlingContextCallback afterTearDownCallback; + private Map<String, Object> resourceResolverFactoryActivatorProps; + + /** + * Create builder with default resource resolver type. + */ + public SlingContextBuilder() {} + + /** + * Create builder with given resource resolver type. + * @param resourceResolverType Resource resolver type. + */ + public SlingContextBuilder(ResourceResolverType resourceResolverType) { + this.resourceResolverType(resourceResolverType); + } + + /** + * @param resourceResolverType Resource resolver type. + * @return this + */ + public SlingContextBuilder resourceResolverType(ResourceResolverType resourceResolverType) { + this.resourceResolverType = resourceResolverType; + return this; + } + + /** + * @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(SlingContextCallback beforeSetUpCallback) { + this.beforeSetUpCallback = beforeSetUpCallback; + return this; + } + + /** + * @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(SlingContextCallback afterSetUpCallback) { + this.afterSetUpCallback = 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. + * @return this + */ + public SlingContextBuilder beforeTearDown(SlingContextCallback beforeTearDownCallback) { + this.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(SlingContextCallback afterTearDownCallback) { + this.afterTearDownCallback = afterTearDownCallback; + return this; + } + + /** + * Allows to override OSGi configuration parameters for the Resource Resolver Factory Activator service. + * @param props Configuration properties + * @return this + */ + public SlingContextBuilder resourceResolverFactoryActivatorProps(Map<String, Object> props) { + this.resourceResolverFactoryActivatorProps = props; + return this; + } + + /** + * @return Build {@link SlingContext} instance. + */ + public SlingContext build() { + return new SlingContext(this.beforeSetUpCallback, this.afterSetUpCallback, + this.beforeTearDownCallback, this.afterTearDownCallback, + this.resourceResolverFactoryActivatorProps, + this.resourceResolverType); + } + +} 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 b7888e3..d7acc84 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.1") [email protected]("3.2") 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 2a25e7c..39bad27 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 @@ -43,17 +43,29 @@ import com.google.common.collect.ImmutableMap; @RunWith(MockitoJUnitRunner.class) public class SlingContextTest { - private final SlingContextCallback contextSetup = mock(SlingContextCallback.class); - private final SlingContextCallback contextTeardown = mock(SlingContextCallback.class); + private final SlingContextCallback contextBeforeSetup = mock(SlingContextCallback.class); + private final SlingContextCallback contextAfterSetup = mock(SlingContextCallback.class); + private final SlingContextCallback contextBeforeTeardown = mock(SlingContextCallback.class); + private final SlingContextCallback contextAfterTeardown = mock(SlingContextCallback.class); // Run all unit tests for each resource resolver types listed here @Rule - public SlingContext context = new SlingContext(contextSetup, contextTeardown, - ResourceResolverType.RESOURCERESOLVER_MOCK); + public SlingContext context = new SlingContextBuilder(ResourceResolverType.JCR_MOCK) + .beforeSetUp(contextBeforeSetup) + .afterSetUp(contextAfterSetup) + .beforeTearDown(contextBeforeTeardown) + .afterTearDown(contextAfterTeardown) + .resourceResolverFactoryActivatorProps(ImmutableMap.<String, Object>of("resource.resolver.searchpath", new String[] { + "/apps", + "/libs", + "/testpath", + })) + .build(); @Before public void setUp() throws IOException, PersistenceException { - verify(contextSetup).execute(context); + verify(contextBeforeSetup).execute(context); + verify(contextAfterSetup).execute(context); } @Test @@ -61,6 +73,26 @@ public class SlingContextTest { assertNotNull(context.request()); } + /** + * Test if custom searchpath /testpath added in this SlingContext is handled correctly. + */ + @Test + public void testResourceResolverFactoryActivatorProps() { + context.create().resource("/apps/node1"); + + context.create().resource("/libs/node1"); + context.create().resource("/libs/node2"); + + context.create().resource("/testpath/node1"); + context.create().resource("/testpath/node2"); + context.create().resource("/testpath/node3"); + + assertEquals("/apps/node1", context.resourceResolver().getResource("node1").getPath()); + assertEquals("/libs/node2", context.resourceResolver().getResource("node2").getPath()); + assertEquals("/testpath/node3", context.resourceResolver().getResource("node3").getPath()); + assertNull(context.resourceResolver().getResource("node4")); + } + @Test public void testRegisterAdapter() { -- To stop receiving notification emails like this one, please contact "[email protected]" <[email protected]>.
