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.1.0 in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-testing-sling-mock.git
commit cb5f2377fd889cc8fbe3e3970ee7c4d75904bf0a Author: Stefan Seifert <[email protected]> AuthorDate: Fri Nov 14 09:55:05 2014 +0000 SLING-4162 Introduce "OsgiContext" junit rule for OSGi and OsgiContextImpl git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/testing/mocks/sling-mock@1639589 13f79535-47bb-0310-9956-ffa450edef68 --- pom.xml | 2 +- .../mock/sling/context/SlingContextImpl.java | 122 ++------------------- .../testing/mock/sling/context/package-info.java | 2 +- .../testing/mock/sling/junit/SlingContext.java | 6 +- .../testing/mock/sling/junit/package-info.java | 2 +- .../mock/sling/context/SlingContextImplTest.java | 44 -------- .../testing/mock/sling/junit/SlingContextTest.java | 10 -- 7 files changed, 14 insertions(+), 174 deletions(-) diff --git a/pom.xml b/pom.xml index 66a28ce..c3621a9 100644 --- a/pom.xml +++ b/pom.xml @@ -49,7 +49,7 @@ <dependency> <groupId>org.apache.sling</groupId> <artifactId>org.apache.sling.testing.osgi-mock</artifactId> - <version>1.0.0</version> + <version>1.0.1-SNAPSHOT</version> <scope>compile</scope> </dependency> <dependency> 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 229b1e1..976c4e0 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,9 +18,6 @@ */ package org.apache.sling.testing.mock.sling.context; -import java.util.Dictionary; -import java.util.Hashtable; -import java.util.Map; import java.util.Set; import javax.jcr.RepositoryException; @@ -47,7 +44,7 @@ import org.apache.sling.models.impl.injectors.ValueMapInjector; import org.apache.sling.models.spi.ImplementationPicker; import org.apache.sling.models.spi.Injector; import org.apache.sling.settings.SlingSettingsService; -import org.apache.sling.testing.mock.osgi.MockOsgi; +import org.apache.sling.testing.mock.osgi.context.OsgiContextImpl; import org.apache.sling.testing.mock.sling.MockSling; import org.apache.sling.testing.mock.sling.ResourceResolverType; import org.apache.sling.testing.mock.sling.builder.ContentBuilder; @@ -58,11 +55,10 @@ import org.apache.sling.testing.mock.sling.services.MockSlingSettingService; import org.apache.sling.testing.mock.sling.servlet.MockRequestPathInfo; import org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest; import org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletResponse; -import org.osgi.framework.BundleContext; import org.osgi.framework.ServiceReference; -import org.osgi.service.component.ComponentContext; -import com.google.common.collect.ImmutableMap; +import aQute.bnd.annotation.ConsumerType; + import com.google.common.collect.ImmutableSet; /** @@ -70,7 +66,8 @@ import com.google.common.collect.ImmutableSet; * directly but via the {@link org.apache.sling.testing.mock.sling.junit.SlingContext} JUnit * rule. */ -public class SlingContextImpl { +@ConsumerType +public class SlingContextImpl extends OsgiContextImpl { // default to publish instance run mode static final Set<String> DEFAULT_RUN_MODES = ImmutableSet.<String> builder().add("publish").build(); @@ -78,7 +75,6 @@ public class SlingContextImpl { protected ResourceResolverFactory resourceResolverFactory; protected MockModelAdapterFactory modelAdapterFactory; protected ResourceResolverType resourceResolverType; - protected ComponentContext componentContext; protected ResourceResolver resourceResolver; protected MockSlingHttpServletRequest request; protected MockSlingHttpServletResponse response; @@ -97,6 +93,7 @@ public class SlingContextImpl { * Setup actions before test method execution */ protected void setUp() { + super.setUp(); MockSling.setAdapterManagerBundleContext(bundleContext()); this.resourceResolverFactory = newResourceResolverFactory(); registerDefaultServices(); @@ -171,6 +168,8 @@ public class SlingContextImpl { this.contentBuilder = null; MockSling.clearAdapterManagerBundleContext(); + + super.tearDown(); } /** @@ -181,23 +180,6 @@ public class SlingContextImpl { } /** - * @return OSGi component context - */ - public final ComponentContext componentContext() { - if (this.componentContext == null) { - this.componentContext = MockOsgi.newComponentContext(); - } - return this.componentContext; - } - - /** - * @return OSGi Bundle context - */ - public final BundleContext bundleContext() { - return componentContext().getBundleContext(); - } - - /** * @return Resource resolver */ public final ResourceResolver resourceResolver() { @@ -277,94 +259,6 @@ public class SlingContextImpl { } /** - * Registers a service in the mocked OSGi environment. - * @param <T> Service type - * @param service Service instance - * @return Registered service instance - */ - public final <T> T registerService(final T service) { - return registerService(null, service, null); - } - - /** - * Registers a service in the mocked OSGi environment. - * @param <T> Service type - * @param serviceClass Service class - * @param service Service instance - * @return Registered service instance - */ - public final <T> T registerService(final Class<T> serviceClass, final T service) { - return registerService(serviceClass, service, null); - } - - /** - * Registers a service in the mocked OSGi environment. - * @param <T> Service type - * @param serviceClass Service class - * @param service Service instance - * @param properties Service properties (optional) - * @return Registered service instance - */ - public final <T> T registerService(final Class<T> serviceClass, final T service, final Map<String, Object> properties) { - Dictionary<String, Object> serviceProperties = null; - if (properties != null) { - serviceProperties = new Hashtable<String, Object>(properties); - } - bundleContext().registerService(serviceClass != null ? serviceClass.getName() : null, service, - serviceProperties); - return service; - } - - /** - * Injects dependencies, activates and registers a service in the mocked - * OSGi environment. - * @param <T> Service type - * @param service Service instance - * @return Registered service instance - */ - public final <T> T registerInjectActivateService(final T service) { - return registerInjectActivateService(service, ImmutableMap.<String, Object> of()); - } - - /** - * Injects dependencies, activates and registers a service in the mocked - * OSGi environment. - * @param <T> Service type - * @param service Service instance - * @param properties Service properties (optional) - * @return Registered service instance - */ - public final <T> T registerInjectActivateService(final T service, final Map<String, Object> properties) { - MockOsgi.injectServices(service, bundleContext()); - MockOsgi.activate(service, bundleContext(), properties); - registerService(null, service, null); - return service; - } - - /** - * Lookup a single service - * @param <ServiceType> Service type - * @param serviceType The type (interface) of the service. - * @return The service instance, or null if the service is not available. - */ - public final <ServiceType> ServiceType getService(final Class<ServiceType> serviceType) { - return slingScriptHelper().getService(serviceType); - } - - /** - * Lookup one or several services - * @param <ServiceType> Service type - * @param serviceType The type (interface) of the service. - * @param filter An optional filter (LDAP-like, see OSGi spec) - * @return The services object or null. - * @throws org.apache.sling.api.scripting.InvalidServiceFilterSyntaxException If the <code>filter</code> - * string is not a valid OSGi service filter string. - */ - public final <ServiceType> ServiceType[] getServices(final Class<ServiceType> serviceType, final String filter) { - return slingScriptHelper().getServices(serviceType, filter); - } - - /** * @return Current resource */ public final Resource currentResource() { 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 ca62fc4..cd3a4a5 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]("1.0") [email protected]("2.0") 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 56413d3..5e0b48c 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 @@ -33,7 +33,6 @@ public final class SlingContext extends SlingContextImpl implements TestRule { private final SlingContextCallback setUpCallback; private final SlingContextCallback tearDownCallback; - private final ResourceResolverType resourceResolverType; private final TestRule delegate; /** @@ -103,10 +102,11 @@ public final class SlingContext extends SlingContextImpl implements TestRule { this.setUpCallback = setUpCallback; this.tearDownCallback = tearDownCallback; - this.resourceResolverType = resourceResolverType; - // user default rule that directly executes each test method once + // set resource resolver type in parent context setResourceResolverType(this.resourceResolverType); + + // wrap {@link ExternalResource} rule executes each test method once this.delegate = new ExternalResource() { @Override protected void before() { 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 4700e5a..09b2c65 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]("1.0") [email protected]("2.0") package org.apache.sling.testing.mock.sling.junit; diff --git a/src/test/java/org/apache/sling/testing/mock/sling/context/SlingContextImplTest.java b/src/test/java/org/apache/sling/testing/mock/sling/context/SlingContextImplTest.java index 2c7aed5..817c502 100644 --- a/src/test/java/org/apache/sling/testing/mock/sling/context/SlingContextImplTest.java +++ b/src/test/java/org/apache/sling/testing/mock/sling/context/SlingContextImplTest.java @@ -24,9 +24,6 @@ import static org.junit.Assert.assertNull; import static org.junit.Assert.assertSame; import static org.junit.Assert.assertTrue; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Map; import java.util.Set; import javax.inject.Inject; @@ -47,7 +44,6 @@ import org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest; import org.junit.After; import org.junit.Before; import org.junit.Test; -import org.osgi.framework.ServiceReference; public class SlingContextImplTest { @@ -89,41 +85,6 @@ public class SlingContextImplTest { } @Test - public void testRegisterService() { - Set<String> myService = new HashSet<String>(); - context.registerService(Set.class, myService); - - Set<?> serviceResult = context.getService(Set.class); - assertSame(myService, serviceResult); - } - - @Test - public void testRegisterServiceWithProperties() { - Map<String, Object> props = new HashMap<String, Object>(); - props.put("prop1", "value1"); - - Set<String> myService = new HashSet<String>(); - context.registerService(Set.class, myService, props); - - ServiceReference serviceReference = context.bundleContext().getServiceReference(Set.class.getName()); - Object serviceResult = context.bundleContext().getService(serviceReference); - assertSame(myService, serviceResult); - assertEquals("value1", serviceReference.getProperty("prop1")); - } - - @Test - public void testRegisterMultipleServices() { - Set<String> myService1 = new HashSet<String>(); - context.registerService(Set.class, myService1); - Set<String> myService2 = new HashSet<String>(); - context.registerService(Set.class, myService2); - - Set[] serviceResults = context.getServices(Set.class, null); - assertSame(myService1, serviceResults[0]); - assertSame(myService2, serviceResults[1]); - } - - @Test public void testSetCurrentResource() { context.currentResource("/content/sample/en/jcr:content/par/colctrl"); assertEquals("/content/sample/en/jcr:content/par/colctrl", context.currentResource().getPath()); @@ -178,11 +139,6 @@ public class SlingContextImplTest { } @Test - public void testRegisterInjectActivate() { - context.registerInjectActivateService(new Object()); - } - - @Test public void testRunModes() { SlingSettingsService slingSettings = context.getService(SlingSettingsService.class); assertEquals(SlingContextImpl.DEFAULT_RUN_MODES, slingSettings.getRunModes()); 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 24c72dd..8af0b55 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 @@ -20,14 +20,12 @@ package org.apache.sling.testing.mock.sling.junit; import static org.junit.Assert.assertNotNull; import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.reset; import static org.mockito.Mockito.verify; import java.io.IOException; import org.apache.sling.api.resource.PersistenceException; import org.apache.sling.testing.mock.sling.ResourceResolverType; -import org.junit.After; import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -55,12 +53,4 @@ public class SlingContextTest { assertNotNull(context.request()); } - @After - public void tearDown() { - // reset required because mockito gets puzzled with the parameterized - // JUnit rule - // TODO: better solution? - reset(contextSetup); - } - } -- To stop receiving notification emails like this one, please contact "[email protected]" <[email protected]>.
