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.3.0 in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-testing-sling-mock.git
commit 9e35c02473fb955c026bcbb04ffde639b4f6d95e Author: Stefan Seifert <[email protected]> AuthorDate: Thu Feb 19 17:42:27 2015 +0000 SLING-4434 refactor integration with sling models: remove MockModelAdapterFactory and directly integrate the original ModelAdapterFactory using the new osgi mock dynamic service reference feature git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/testing/mocks/sling-mock@1660951 13f79535-47bb-0310-9956-ffa450edef68 --- pom.xml | 2 +- .../testing/mock/sling/MockAdapterManager.java | 12 +-- .../ModelAdapterFactoryUtil.java} | 100 +++------------------ .../mock/sling/context/SlingContextImpl.java | 29 +++--- .../testing/mock/sling/context/package-info.java | 2 +- .../testing/mock/sling/junit/package-info.java | 2 +- .../testing/mock/sling/services/package-info.java | 2 +- .../ModelAdapterFactoryUtilTest.java} | 43 +++------ .../mock/sling/context/SlingContextImplTest.java | 4 +- 9 files changed, 47 insertions(+), 149 deletions(-) diff --git a/pom.xml b/pom.xml index 64c4222..84d3d7b 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.1.0</version> + <version>1.1.1-SNAPSHOT</version> <scope>compile</scope> </dependency> <dependency> diff --git a/src/main/java/org/apache/sling/testing/mock/sling/MockAdapterManager.java b/src/main/java/org/apache/sling/testing/mock/sling/MockAdapterManager.java index a75d131..7b9eb5e 100644 --- a/src/main/java/org/apache/sling/testing/mock/sling/MockAdapterManager.java +++ b/src/main/java/org/apache/sling/testing/mock/sling/MockAdapterManager.java @@ -46,11 +46,13 @@ class MockAdapterManager implements AdapterManager { if (this.bundleContext != null) { try { ServiceReference[] references = bundleContext.getServiceReferences(AdapterFactory.class.getName(), null); - for (ServiceReference serviceReference : references) { - AdapterFactory adapterFactory = (AdapterFactory) bundleContext.getService(serviceReference); - AdapterType instance = adapterFactory.getAdapter(adaptable, type); - if (instance != null) { - return instance; + if (references != null) { + for (ServiceReference serviceReference : references) { + AdapterFactory adapterFactory = (AdapterFactory) bundleContext.getService(serviceReference); + AdapterType instance = adapterFactory.getAdapter(adaptable, type); + if (instance != null) { + return instance; + } } } } catch (InvalidSyntaxException ex) { diff --git a/src/main/java/org/apache/sling/testing/mock/sling/services/MockModelAdapterFactory.java b/src/main/java/org/apache/sling/testing/mock/sling/context/ModelAdapterFactoryUtil.java similarity index 58% rename from src/main/java/org/apache/sling/testing/mock/sling/services/MockModelAdapterFactory.java rename to src/main/java/org/apache/sling/testing/mock/sling/context/ModelAdapterFactoryUtil.java index 149ff27..43a4af4 100644 --- a/src/main/java/org/apache/sling/testing/mock/sling/services/MockModelAdapterFactory.java +++ b/src/main/java/org/apache/sling/testing/mock/sling/context/ModelAdapterFactoryUtil.java @@ -16,7 +16,7 @@ * specific language governing permissions and limitations * under the License. */ -package org.apache.sling.testing.mock.sling.services; +package org.apache.sling.testing.mock.sling.context; import java.io.IOException; import java.io.InputStream; @@ -24,104 +24,28 @@ import java.net.MalformedURLException; import java.net.URL; import java.util.Dictionary; import java.util.Enumeration; -import java.util.HashMap; import java.util.Hashtable; import java.util.Map; import java.util.Set; import java.util.Vector; -import org.apache.felix.scr.annotations.Component; -import org.apache.felix.scr.annotations.Service; -import org.apache.sling.api.adapter.AdapterFactory; import org.apache.sling.models.annotations.Model; -import org.apache.sling.models.impl.ModelAdapterFactory; -import org.apache.sling.models.spi.ImplementationPicker; -import org.apache.sling.models.spi.Injector; -import org.apache.sling.models.spi.injectorspecific.InjectAnnotationProcessorFactory; import org.apache.sling.testing.mock.osgi.MockOsgi; import org.osgi.framework.Bundle; import org.osgi.framework.BundleContext; import org.osgi.framework.BundleEvent; import org.osgi.framework.BundleException; -import org.osgi.framework.ServiceEvent; -import org.osgi.framework.ServiceListener; import org.osgi.framework.ServiceReference; import org.osgi.framework.Version; -import org.osgi.service.component.ComponentContext; import org.reflections.Reflections; /** - * Mock {@link ModelAdapterFactory} implementation. + * Helper methos for simulating sling models bundle events. */ -@Component(inherit = false) -@Service(AdapterFactory.class) -public final class MockModelAdapterFactory extends ModelAdapterFactory { - - private final BundleContext bundleContext; - - /** - * @param componentContext OSGi component context - */ - public MockModelAdapterFactory(ComponentContext componentContext) { - bundleContext = componentContext.getBundleContext(); - - // register service listener to collect injectors - // this allows detecting injectors even if they are registered after this bundle - // (which is otherwise currently not supported in the osgi mock environment) - bundleContext.addServiceListener(new InjectorServiceListener()); - - // activate service in simulated OSGi environment - activate(componentContext); - } - - /** - * Constructor with default component context - */ - public MockModelAdapterFactory() { - this(MockOsgi.newComponentContext()); - } - - private class InjectorServiceListener implements ServiceListener { - - @Override - public void serviceChanged(ServiceEvent event) { - Object service = bundleContext.getService(event.getServiceReference()); - if (service instanceof Injector) { - if (event.getType() == ServiceEvent.REGISTERED) { - bindInjector((Injector) service, getServiceProperties(event.getServiceReference())); - } else if (event.getType() == ServiceEvent.UNREGISTERING) { - unbindInjector((Injector) service, getServiceProperties(event.getServiceReference())); - } - } - if (service instanceof InjectAnnotationProcessorFactory) { - if (event.getType() == ServiceEvent.REGISTERED) { - bindInjectAnnotationProcessorFactory((InjectAnnotationProcessorFactory) service, - getServiceProperties(event.getServiceReference())); - } else if (event.getType() == ServiceEvent.UNREGISTERING) { - unbindInjectAnnotationProcessorFactory((InjectAnnotationProcessorFactory) service, - getServiceProperties(event.getServiceReference())); - } - } - if (service instanceof ImplementationPicker) { - if (event.getType() == ServiceEvent.REGISTERED) { - bindImplementationPicker((ImplementationPicker) service, - getServiceProperties(event.getServiceReference())); - } else if (event.getType() == ServiceEvent.UNREGISTERING) { - unbindImplementationPicker((ImplementationPicker) service, - getServiceProperties(event.getServiceReference())); - } - } - } - - private Map<String, Object> getServiceProperties(ServiceReference reference) { - Map<String, Object> props = new HashMap<String, Object>(); - String[] propertyKeys = reference.getPropertyKeys(); - for (String key : propertyKeys) { - props.put(key, reference.getProperty(key)); - } - return props; - } - +final class ModelAdapterFactoryUtil { + + private ModelAdapterFactoryUtil() { + // static methods only } /** @@ -129,21 +53,23 @@ public final class MockModelAdapterFactory extends ModelAdapterFactory { * register all classes with @Model annotation. * @param packageName Java package name */ - public void addModelsForPackage(String packageName) { - Bundle bundle = new ModelsPackageBundle(packageName, Bundle.ACTIVE); + public static void addModelsForPackage(String packageName, BundleContext bundleContext) { + Bundle bundle = new ModelsPackageBundle(packageName, Bundle.ACTIVE, bundleContext); BundleEvent event = new BundleEvent(BundleEvent.STARTED, bundle); - MockOsgi.sendBundleEvent(this.bundleContext, event); + MockOsgi.sendBundleEvent(bundleContext, event); } @SuppressWarnings("unused") - private class ModelsPackageBundle implements Bundle { + private static class ModelsPackageBundle implements Bundle { private final String packageName; private final int state; + private final BundleContext bundleContext; - public ModelsPackageBundle(String packageName, int state) { + public ModelsPackageBundle(String packageName, int state, BundleContext bundleContext) { this.packageName = packageName; this.state = state; + this.bundleContext = bundleContext; } @Override 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 00d4f2e..39ff90b 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 @@ -23,7 +23,6 @@ import java.util.Set; import javax.jcr.RepositoryException; import javax.jcr.Session; -import org.apache.sling.api.adapter.AdapterFactory; import org.apache.sling.api.resource.LoginException; import org.apache.sling.api.resource.Resource; import org.apache.sling.api.resource.ResourceResolver; @@ -32,6 +31,7 @@ import org.apache.sling.api.scripting.SlingBindings; import org.apache.sling.api.scripting.SlingScriptHelper; import org.apache.sling.commons.mime.MimeTypeService; import org.apache.sling.models.impl.FirstImplementationPicker; +import org.apache.sling.models.impl.ModelAdapterFactory; import org.apache.sling.models.impl.injectors.BindingsInjector; import org.apache.sling.models.impl.injectors.ChildResourceInjector; import org.apache.sling.models.impl.injectors.OSGiServiceInjector; @@ -41,7 +41,6 @@ import org.apache.sling.models.impl.injectors.SelfInjector; import org.apache.sling.models.impl.injectors.SlingObjectInjector; 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.context.OsgiContextImpl; import org.apache.sling.testing.mock.sling.MockSling; @@ -49,7 +48,6 @@ import org.apache.sling.testing.mock.sling.ResourceResolverType; import org.apache.sling.testing.mock.sling.builder.ContentBuilder; import org.apache.sling.testing.mock.sling.loader.ContentLoader; import org.apache.sling.testing.mock.sling.services.MockMimeTypeService; -import org.apache.sling.testing.mock.sling.services.MockModelAdapterFactory; 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; @@ -72,7 +70,6 @@ public class SlingContextImpl extends OsgiContextImpl { static final Set<String> DEFAULT_RUN_MODES = ImmutableSet.<String> builder().add("publish").build(); protected ResourceResolverFactory resourceResolverFactory; - protected MockModelAdapterFactory modelAdapterFactory; protected ResourceResolverType resourceResolverType; protected ResourceResolver resourceResolver; protected MockSlingHttpServletRequest request; @@ -115,20 +112,17 @@ public class SlingContextImpl extends OsgiContextImpl { registerService(ResourceResolverFactory.class, this.resourceResolverFactory); // adapter factories - modelAdapterFactory = new MockModelAdapterFactory(componentContext()); - registerService(AdapterFactory.class, modelAdapterFactory); + registerInjectActivateService(new ModelAdapterFactory()); // sling models injectors - registerService(Injector.class, new BindingsInjector()); - registerService(Injector.class, new ChildResourceInjector()); - OSGiServiceInjector osgiServiceInjector = new OSGiServiceInjector(); - osgiServiceInjector.activate(componentContext()); - registerService(Injector.class, osgiServiceInjector); - registerService(Injector.class, new RequestAttributeInjector()); - registerService(Injector.class, new ResourcePathInjector()); - registerService(Injector.class, new SelfInjector()); - registerService(Injector.class, new SlingObjectInjector()); - registerService(Injector.class, new ValueMapInjector()); + registerInjectActivateService(new BindingsInjector()); + registerInjectActivateService(new ChildResourceInjector()); + registerInjectActivateService(new OSGiServiceInjector()); + registerInjectActivateService(new RequestAttributeInjector()); + registerInjectActivateService(new ResourcePathInjector()); + registerInjectActivateService(new SelfInjector()); + registerInjectActivateService(new SlingObjectInjector()); + registerInjectActivateService(new ValueMapInjector()); // sling models implementation pickers registerService(ImplementationPicker.class, new FirstImplementationPicker()); @@ -156,7 +150,6 @@ public class SlingContextImpl extends OsgiContextImpl { } } - this.modelAdapterFactory = null; this.componentContext = null; this.resourceResolver = null; this.request = null; @@ -296,7 +289,7 @@ public class SlingContextImpl extends OsgiContextImpl { * @param packageName Java package name */ public final void addModelsForPackage(String packageName) { - this.modelAdapterFactory.addModelsForPackage(packageName); + ModelAdapterFactoryUtil.addModelsForPackage(packageName, bundleContext()); } /** 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 cd3a4a5..da3413d 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]("2.0") [email protected]("3.0") package org.apache.sling.testing.mock.sling.context; 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 09b2c65..f60a75d 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]("2.0") [email protected]("3.0") package org.apache.sling.testing.mock.sling.junit; diff --git a/src/main/java/org/apache/sling/testing/mock/sling/services/package-info.java b/src/main/java/org/apache/sling/testing/mock/sling/services/package-info.java index 83ecb71..e919159 100644 --- a/src/main/java/org/apache/sling/testing/mock/sling/services/package-info.java +++ b/src/main/java/org/apache/sling/testing/mock/sling/services/package-info.java @@ -19,5 +19,5 @@ /** * Mocks for selected Sling services. */ [email protected]("1.0") [email protected]("2.0") package org.apache.sling.testing.mock.sling.services; diff --git a/src/test/java/org/apache/sling/testing/mock/sling/services/MockModelAdapterFactoryTest.java b/src/test/java/org/apache/sling/testing/mock/sling/context/ModelAdapterFactoryUtilTest.java similarity index 66% rename from src/test/java/org/apache/sling/testing/mock/sling/services/MockModelAdapterFactoryTest.java rename to src/test/java/org/apache/sling/testing/mock/sling/context/ModelAdapterFactoryUtilTest.java index a4138c3..f150f07 100644 --- a/src/test/java/org/apache/sling/testing/mock/sling/services/MockModelAdapterFactoryTest.java +++ b/src/test/java/org/apache/sling/testing/mock/sling/context/ModelAdapterFactoryUtilTest.java @@ -16,7 +16,7 @@ * specific language governing permissions and limitations * under the License. */ -package org.apache.sling.testing.mock.sling.services; +package org.apache.sling.testing.mock.sling.context; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; @@ -25,50 +25,27 @@ import static org.junit.Assert.assertNull; import javax.inject.Inject; import org.apache.sling.api.SlingHttpServletRequest; -import org.apache.sling.api.adapter.AdapterFactory; import org.apache.sling.api.resource.ResourceResolver; import org.apache.sling.commons.mime.MimeTypeService; import org.apache.sling.models.annotations.Model; -import org.apache.sling.models.impl.FirstImplementationPicker; -import org.apache.sling.models.impl.injectors.OSGiServiceInjector; -import org.apache.sling.models.impl.injectors.RequestAttributeInjector; -import org.apache.sling.models.spi.ImplementationPicker; -import org.apache.sling.models.spi.Injector; -import org.apache.sling.testing.mock.osgi.MockOsgi; import org.apache.sling.testing.mock.sling.MockSling; +import org.apache.sling.testing.mock.sling.junit.SlingContext; +import org.apache.sling.testing.mock.sling.services.MockMimeTypeService; import org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest; import org.junit.After; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; -import org.osgi.framework.BundleContext; -import org.osgi.service.component.ComponentContext; -public class MockModelAdapterFactoryTest { - - private ComponentContext componentContext; - private BundleContext bundleContext; +public class ModelAdapterFactoryUtilTest { + @Rule + public SlingContext context = new SlingContext(); + @Before public void setUp() throws Exception { - componentContext = MockOsgi.newComponentContext(); - bundleContext = componentContext.getBundleContext(); - MockSling.setAdapterManagerBundleContext(bundleContext); - - // register sling models adapter factory - MockModelAdapterFactory mockModelAdapterFactory = new MockModelAdapterFactory(componentContext); - bundleContext.registerService(AdapterFactory.class.getName(), mockModelAdapterFactory, null); - - // register some injectors - bundleContext.registerService(Injector.class.getName(), new RequestAttributeInjector(), null); - OSGiServiceInjector osgiServiceInjector = new OSGiServiceInjector(); - osgiServiceInjector.activate(componentContext); - bundleContext.registerService(Injector.class.getName(), osgiServiceInjector, null); - - // register implementation pickers - bundleContext.registerService(ImplementationPicker.class.getName(), new FirstImplementationPicker(), null); - // scan for @Model classes - mockModelAdapterFactory.addModelsForPackage("org.apache.sling.testing.mock.sling.services"); + context.addModelsForPackage("org.apache.sling.testing.mock.sling.context"); } @After @@ -87,7 +64,7 @@ public class MockModelAdapterFactoryTest { @Test public void testOsgiService() { - bundleContext.registerService(MimeTypeService.class.getName(), new MockMimeTypeService(), null); + context.registerService(MimeTypeService.class, new MockMimeTypeService(), null); ResourceResolver resolver = MockSling.newResourceResolver(); OsgiServiceModel model = resolver.adaptTo(OsgiServiceModel.class); 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 817c502..b917a14 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 @@ -55,6 +55,8 @@ public class SlingContextImplTest { this.context.setResourceResolverType(ResourceResolverType.RESOURCERESOLVER_MOCK); this.context.setUp(); + context.addModelsForPackage("org.apache.sling.testing.mock.sling.context"); + ContentLoader contentLoader = this.context.load(); contentLoader.json("/json-import-samples/content.json", "/content/sample/en"); } @@ -129,8 +131,6 @@ public class SlingContextImplTest { @Test public void testAdaptToInterface() { - context.addModelsForPackage("org.apache.sling.testing.mock.sling.context"); - MockSlingHttpServletRequest request = new MockSlingHttpServletRequest(); request.setAttribute("prop1", "myValue"); ServiceInterface model = request.adaptTo(ServiceInterface.class); -- To stop receiving notification emails like this one, please contact "[email protected]" <[email protected]>.
