You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ro...@apache.org on 2017/11/07 10:23:44 UTC

[sling-org-apache-sling-testing-sling-mock] 28/37: SLING-5088 rewrite setup code for resource resolver factory

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.6.0
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-testing-sling-mock.git

commit 81f87d01468c2517d2f02d372f92255cada97eaa
Author: Stefan Seifert <ss...@apache.org>
AuthorDate: Fri Oct 2 22:09:28 2015 +0000

    SLING-5088 rewrite setup code for resource resolver factory
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/testing/mocks/sling-mock@1706495 13f79535-47bb-0310-9956-ffa450edef68
---
 .../sling/AbstractMockResourceResolverFactory.java | 122 ------------------
 .../mock/sling/MockJcrResourceResolverFactory.java |  95 --------------
 .../sling/MockNoneResourceResolverFactory.java     |  34 ------
 .../sling/ResourceResolverFactoryInitializer.java  | 136 +++++++++++++++++++--
 4 files changed, 129 insertions(+), 258 deletions(-)

diff --git a/src/main/java/org/apache/sling/testing/mock/sling/AbstractMockResourceResolverFactory.java b/src/main/java/org/apache/sling/testing/mock/sling/AbstractMockResourceResolverFactory.java
deleted file mode 100644
index 7b47918..0000000
--- a/src/main/java/org/apache/sling/testing/mock/sling/AbstractMockResourceResolverFactory.java
+++ /dev/null
@@ -1,122 +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;
-
-import java.util.Dictionary;
-import java.util.Hashtable;
-import java.util.Map;
-
-import org.apache.sling.api.resource.LoginException;
-import org.apache.sling.api.resource.ResourceResolver;
-import org.apache.sling.api.resource.ResourceResolverFactory;
-import org.apache.sling.resourceresolver.impl.CommonResourceResolverFactoryImpl;
-import org.apache.sling.resourceresolver.impl.ResourceAccessSecurityTracker;
-import org.apache.sling.resourceresolver.impl.ResourceResolverFactoryActivator;
-import org.apache.sling.serviceusermapping.ServiceUserMapper;
-import org.apache.sling.serviceusermapping.impl.ServiceUserMapperImpl;
-import org.apache.sling.testing.mock.osgi.MockEventAdmin;
-import org.apache.sling.testing.mock.osgi.MockOsgi;
-import org.osgi.framework.BundleContext;
-import org.osgi.framework.Constants;
-import org.osgi.service.event.EventAdmin;
-
-/**
- * Mock {@link ResourceResolverFactory} implementation.
- * Uses real Sling ResourceResolverFactory in simulated OSGi environment. 
- */
-@Deprecated
-abstract class AbstractMockResourceResolverFactory implements ResourceResolverFactory {
-
-    protected final BundleContext bundleContext;
-
-    public AbstractMockResourceResolverFactory(BundleContext bundleContext) {
-        this.bundleContext = bundleContext;
-    }
-
-    protected ResourceResolver getResourceResolverInternal(Map<String, Object> authenticationInfo, boolean isAdmin) throws LoginException {
-        // setup real sling resource resolver implementation for use in mocked context
-        Dictionary<String, Object> resourceProviderFactoryFactoryProps = new Hashtable<String, Object>();
-        resourceProviderFactoryFactoryProps.put(Constants.SERVICE_VENDOR, "sling-mock");
-        resourceProviderFactoryFactoryProps.put(Constants.SERVICE_DESCRIPTION, "sling-mock");
-        resourceProviderFactoryFactoryProps.put("resource.resolver.manglenamespaces", true);
-        resourceProviderFactoryFactoryProps.put("resource.resolver.searchpath", new String[] { "/apps", "/libs" });
-
-        ensureResourceResolverFactoryActivatorDependencies();
-        ResourceResolverFactoryActivator activator = new ResourceResolverFactoryActivator();
-        MockOsgi.injectServices(activator, bundleContext);
-        MockOsgi.activate(activator, resourceProviderFactoryFactoryProps);
-        
-        CommonResourceResolverFactoryImpl commonFactoryImpl = new CommonResourceResolverFactoryImpl(activator);
-        if (isAdmin) {
-            return commonFactoryImpl.getAdministrativeResourceResolver(authenticationInfo);
-        }
-        else {
-            return commonFactoryImpl.getResourceResolver(authenticationInfo);
-        }
-    }
-    
-    /**
-     * Make sure all dependencies required by {@link ResourceResolverFactoryActivator} exist - if not register them.
-     */
-    private void ensureResourceResolverFactoryActivatorDependencies() {
-        if (bundleContext.getServiceReference(ServiceUserMapper.class.getName()) == null) {
-            ServiceUserMapper serviceUserMapper = new ServiceUserMapperImpl();
-            MockOsgi.injectServices(serviceUserMapper, bundleContext);
-            MockOsgi.activate(serviceUserMapper);
-            bundleContext.registerService(ServiceUserMapper.class.getName(), serviceUserMapper, null);
-        }
-
-        if (bundleContext.getServiceReference(ResourceAccessSecurityTracker.class.getName()) == null) {
-            ResourceAccessSecurityTracker resourceAccessSecurityTracker = new ResourceAccessSecurityTracker();
-            MockOsgi.injectServices(resourceAccessSecurityTracker, bundleContext);
-            MockOsgi.activate(resourceAccessSecurityTracker);
-            bundleContext.registerService(ResourceAccessSecurityTracker.class.getName(), resourceAccessSecurityTracker, null);
-        }
-
-        if (bundleContext.getServiceReference(EventAdmin.class.getName()) == null) {
-            EventAdmin eventAdmin = new MockEventAdmin();
-            MockOsgi.injectServices(eventAdmin, bundleContext);
-            MockOsgi.activate(eventAdmin);
-            bundleContext.registerService(EventAdmin.class.getName(), eventAdmin, null);
-        }
-    }
-
-    @Override
-    public ResourceResolver getResourceResolver(final Map<String, Object> authenticationInfo) throws LoginException {
-        return getResourceResolverInternal(authenticationInfo, false);
-    }
-
-    @Override
-    public ResourceResolver getAdministrativeResourceResolver(final Map<String, Object> authenticationInfo)
-            throws LoginException {
-        return getResourceResolverInternal(authenticationInfo, true);
-    }
-
-    // part of Sling API 2.7
-    public ResourceResolver getServiceResourceResolver(final Map<String, Object> authenticationInfo)
-            throws LoginException {
-        return getResourceResolverInternal(authenticationInfo, true);
-    }
-
-    // part of Sling API 2.8
-    public ResourceResolver getThreadResourceResolver() {
-        throw new UnsupportedOperationException();
-    }
-
-}
diff --git a/src/main/java/org/apache/sling/testing/mock/sling/MockJcrResourceResolverFactory.java b/src/main/java/org/apache/sling/testing/mock/sling/MockJcrResourceResolverFactory.java
deleted file mode 100644
index 0890cd0..0000000
--- a/src/main/java/org/apache/sling/testing/mock/sling/MockJcrResourceResolverFactory.java
+++ /dev/null
@@ -1,95 +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;
-
-import java.util.Dictionary;
-import java.util.Hashtable;
-import java.util.Map;
-
-import javax.jcr.query.Query;
-
-import org.apache.sling.api.resource.LoginException;
-import org.apache.sling.api.resource.QueriableResourceProvider;
-import org.apache.sling.api.resource.ResourceProvider;
-import org.apache.sling.api.resource.ResourceProviderFactory;
-import org.apache.sling.api.resource.ResourceResolver;
-import org.apache.sling.jcr.api.SlingRepository;
-import org.apache.sling.jcr.resource.internal.helper.jcr.JcrResourceProviderFactory;
-import org.apache.sling.testing.mock.osgi.MockOsgi;
-import org.osgi.framework.BundleContext;
-
-/**
- * Mock {@link ResourceResolverFactory} implementation.
- * Uses real Sling ResourceResolverFactory in simulated OSGi environment
- * with a mocked JCR repository implementation underneath.
- */
-@Deprecated
-class MockJcrResourceResolverFactory extends AbstractMockResourceResolverFactory {
-
-    private final SlingRepository slingRepository;
-
-    public MockJcrResourceResolverFactory(final SlingRepository repository, BundleContext bundleContext) {
-        super(bundleContext);
-        this.slingRepository = repository;
-    }
-
-    protected ResourceResolver getResourceResolverInternal(Map<String, Object> authenticationInfo, boolean isAdmin) throws LoginException {
-        // setup mocked JCR environment
-        if (bundleContext.getServiceReference(SlingRepository.class.getName()) == null) {
-            bundleContext.registerService(SlingRepository.class.getName(), this.slingRepository, null);
-        }
-        
-        // setup PathMapper which is a mandatory service for JcrProviderFactory (since org.apache.sling.jcr.resource 2.5.4)
-        // use reflection to not depend on it if running with older version of org.apache.sling.jcr.resource
-        registerServiceIfFoundInClasspath("org.apache.sling.jcr.resource.internal.helper.jcr.PathMapper");
-
-        // setup real sling JCR resource provider implementation for use in mocked context
-        JcrResourceProviderFactory jcrResourceProviderFactory = new JcrResourceProviderFactory();
-        Dictionary<String, Object> resourceProviderProps = new Hashtable<String, Object>();
-        resourceProviderProps.put(ResourceProvider.ROOTS, new String[] { "/" });
-        resourceProviderProps.put(QueriableResourceProvider.LANGUAGES, new String[] { Query.XPATH, Query.SQL, Query.JCR_SQL2 });
-        MockOsgi.injectServices(jcrResourceProviderFactory, bundleContext);
-        MockOsgi.activate(jcrResourceProviderFactory, bundleContext, resourceProviderProps);
-        bundleContext.registerService(ResourceProviderFactory.class.getName(), jcrResourceProviderFactory, resourceProviderProps);
-
-        return super.getResourceResolverInternal(authenticationInfo, isAdmin);
-    }
-    
-    private void registerServiceIfFoundInClasspath(String className) {
-        try {
-            Class pathMapperClass = Class.forName(className);
-            if (bundleContext.getServiceReference(className) == null) {
-                Object instance = pathMapperClass.newInstance();
-                MockOsgi.injectServices(instance, bundleContext);
-                MockOsgi.activate(instance);
-                bundleContext.registerService(className, instance, null);
-            }
-        }
-        catch (ClassNotFoundException ex) {
-            // skip service registration
-        }
-        catch (InstantiationException e) {
-            // skip service registration
-        }
-        catch (IllegalAccessException e) {
-            // skip service registration
-        }
-    }
-    
-}
diff --git a/src/main/java/org/apache/sling/testing/mock/sling/MockNoneResourceResolverFactory.java b/src/main/java/org/apache/sling/testing/mock/sling/MockNoneResourceResolverFactory.java
deleted file mode 100644
index e18b097..0000000
--- a/src/main/java/org/apache/sling/testing/mock/sling/MockNoneResourceResolverFactory.java
+++ /dev/null
@@ -1,34 +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;
-
-import org.osgi.framework.BundleContext;
-
-/**
- * Mock {@link ResourceResolverFactory} implementation.
- * Uses real Sling ResourceResolverFactory in simulated OSGi environment.
- * Resource Resolver factory has no ResourceProvider registered; you have to registere one yourself to do anything useful with it. 
- */
-@Deprecated
-class MockNoneResourceResolverFactory extends AbstractMockResourceResolverFactory {
-
-    public MockNoneResourceResolverFactory(BundleContext bundleContext) {
-        super(bundleContext);
-    }
-}
diff --git a/src/main/java/org/apache/sling/testing/mock/sling/ResourceResolverFactoryInitializer.java b/src/main/java/org/apache/sling/testing/mock/sling/ResourceResolverFactoryInitializer.java
index 99af56a..ae1f129 100644
--- a/src/main/java/org/apache/sling/testing/mock/sling/ResourceResolverFactoryInitializer.java
+++ b/src/main/java/org/apache/sling/testing/mock/sling/ResourceResolverFactoryInitializer.java
@@ -18,12 +18,31 @@
  */
 package org.apache.sling.testing.mock.sling;
 
+import java.util.Dictionary;
+import java.util.Hashtable;
+
 import javax.jcr.RepositoryException;
 import javax.jcr.Session;
+import javax.jcr.query.Query;
 
+import org.apache.sling.api.resource.QueriableResourceProvider;
+import org.apache.sling.api.resource.ResourceProvider;
+import org.apache.sling.api.resource.ResourceProviderFactory;
 import org.apache.sling.api.resource.ResourceResolverFactory;
 import org.apache.sling.jcr.api.SlingRepository;
+import org.apache.sling.jcr.resource.internal.helper.jcr.JcrResourceProviderFactory;
+import org.apache.sling.resourceresolver.impl.ResourceAccessSecurityTracker;
+import org.apache.sling.resourceresolver.impl.ResourceResolverFactoryActivator;
+import org.apache.sling.serviceusermapping.ServiceUserMapper;
+import org.apache.sling.serviceusermapping.impl.ServiceUserMapperImpl;
+import org.apache.sling.testing.mock.osgi.MockEventAdmin;
+import org.apache.sling.testing.mock.osgi.MockOsgi;
 import org.osgi.framework.BundleContext;
+import org.osgi.framework.Constants;
+import org.osgi.framework.ServiceReference;
+import org.osgi.service.event.EventAdmin;
+
+import sun.util.BuddhistCalendar;
 
 /**
  * Initializes Sling Resource Resolver factories with JCR-resource mapping.
@@ -41,15 +60,118 @@ class ResourceResolverFactoryInitializer {
      */
     public static ResourceResolverFactory setUp(SlingRepository slingRepository, 
             BundleContext bundleContext, NodeTypeMode nodeTypeMode) {
-        ResourceResolverFactory factory;
-        if (slingRepository == null) {
-            factory = new MockNoneResourceResolverFactory(bundleContext);
-        }
-        else {
+        
+        if (slingRepository != null) {
+            // register JCR node types found in classpath
             registerJcrNodeTypes(slingRepository, nodeTypeMode);
-            factory = new MockJcrResourceResolverFactory(slingRepository, bundleContext);
+            
+            // register sling repository as OSGi service
+            bundleContext.registerService(SlingRepository.class.getName(), slingRepository, null);
+            
+            // initialize JCR resource provider factory
+            ensureJcrResourceProviderFactoryDependencies(bundleContext);
+            initializeJcrResourceProviderFactory(bundleContext);
+        }
+        
+        // initialize resource resolver factory activator
+        ensureResourceResolverFactoryActivatorDependencies(bundleContext);
+        initializeResourceResolverFactoryActivator(bundleContext);
+
+        ServiceReference factoryRef = bundleContext.getServiceReference(ResourceResolverFactory.class.getName());
+        if (factoryRef == null) {
+            throw new IllegalStateException("Unable to get ResourceResolverFactory.");
+        }
+        return (ResourceResolverFactory)bundleContext.getService(factoryRef);
+    }
+    
+    /**
+     * Ensure dependencies for JcrResourceProviderFactory are present.
+     * @param bundleContext Bundle context
+     */
+    private static void ensureJcrResourceProviderFactoryDependencies(BundleContext bundleContext) {
+        // setup PathMapper which is a mandatory service for JcrProviderFactory (since org.apache.sling.jcr.resource 2.5.4)
+        // use reflection to not depend on it if running with older version of org.apache.sling.jcr.resource
+        registerServiceIfFoundInClasspath(bundleContext, "org.apache.sling.jcr.resource.internal.helper.jcr.PathMapper");
+    }
+ 
+    /**
+     * Initialize JCR resource provider factory.
+     * @param bundleContext Bundle context
+     */
+    @SuppressWarnings("deprecation")
+    private static void initializeJcrResourceProviderFactory(BundleContext bundleContext) {
+        Dictionary<String, Object> config = new Hashtable<String, Object>();
+        config.put(ResourceProvider.ROOTS, new String[] { "/" });
+        config.put(QueriableResourceProvider.LANGUAGES, new String[] { Query.XPATH, Query.SQL, Query.JCR_SQL2 });
+
+        JcrResourceProviderFactory factory = new JcrResourceProviderFactory();
+        MockOsgi.injectServices(factory, bundleContext);
+        MockOsgi.activate(factory, bundleContext, config);
+        bundleContext.registerService(ResourceProviderFactory.class.getName(), factory, config);
+    }
+    
+    /**
+     * Ensure dependencies for ResourceResolverFactoryActivator are present.
+     * @param bundleContext Bundle context
+     */
+    private static void ensureResourceResolverFactoryActivatorDependencies(BundleContext bundleContext) {
+        registerServiceIfNotPresent(bundleContext, ServiceUserMapper.class, new ServiceUserMapperImpl());
+        registerServiceIfNotPresent(bundleContext, ResourceAccessSecurityTracker.class, new ResourceAccessSecurityTracker());
+        registerServiceIfNotPresent(bundleContext, EventAdmin.class, new MockEventAdmin());
+    }
+ 
+    /**
+     * Initialize resource resolver factory activator.
+     * @param bundleContext Bundle context
+     */
+    private static void initializeResourceResolverFactoryActivator(BundleContext bundleContext) {
+        Dictionary<String, Object> config = new Hashtable<String, Object>();
+        config.put(Constants.SERVICE_VENDOR, "sling-mock");
+        config.put(Constants.SERVICE_DESCRIPTION, "sling-mock");
+        config.put("resource.resolver.manglenamespaces", true);
+        config.put("resource.resolver.searchpath", new String[] { "/apps", "/libs" });
+        config.put("resource.resolver.required.providers", new String[0]);
+
+        ResourceResolverFactoryActivator activator = new ResourceResolverFactoryActivator();
+        MockOsgi.injectServices(activator, bundleContext);
+        MockOsgi.activate(activator, bundleContext, config);
+        bundleContext.registerService(ResourceResolverFactoryActivator.class.getName(), activator, config);
+    }
+    
+    /**
+     * Registers a service if the service class is found in classpath,
+     * and if no service with this class is already registered.
+     * @param className Service class name
+     */
+    private static void registerServiceIfNotPresent(BundleContext bundleContext, Class<?> serviceClass, Object instance) {
+        if (bundleContext.getServiceReference(serviceClass.getName()) == null) {
+            Dictionary<String,Object> properties = new Hashtable<String, Object>();
+            MockOsgi.injectServices(instance, bundleContext);
+            MockOsgi.activate(instance, bundleContext, properties);
+            bundleContext.registerService(serviceClass.getName(), instance, properties);
+        }
+    }
+    
+    /**
+     * Registers a service if the service class is found in classpath,
+     * and if no service with this class is already registered.
+     * @param className Service class name
+     */
+    private static void registerServiceIfFoundInClasspath(BundleContext bundleContext, String className) {
+        try {
+            Class<?> serviceClass = Class.forName(className);
+            Object instance = serviceClass.newInstance();
+            registerServiceIfNotPresent(bundleContext, serviceClass, instance);
+        }
+        catch (ClassNotFoundException ex) {
+            // skip service registration
+        }
+        catch (InstantiationException e) {
+            // skip service registration
+        }
+        catch (IllegalAccessException e) {
+            // skip service registration
         }
-        return factory;
     }
     
     /**

-- 
To stop receiving notification emails like this one, please contact
"commits@sling.apache.org" <co...@sling.apache.org>.