sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sseif...@apache.org
Subject svn commit: r1660995 - in /sling/trunk/testing/mocks/sling-mock/src: main/java/org/apache/sling/testing/mock/sling/ main/java/org/apache/sling/testing/mock/sling/context/ test/java/org/apache/sling/testing/mock/sling/resource/
Date Thu, 19 Feb 2015 20:20:59 GMT
Author: sseifert
Date: Thu Feb 19 20:20:58 2015
New Revision: 1660995

URL: http://svn.apache.org/r1660995
Log:
SLING-4437 MockJcrResourceResolverFactory should allow to register services dynamically

Modified:
    sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/MockJcrResourceResolverFactory.java
    sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/MockSling.java
    sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/context/ContextResourceResolverFactory.java
    sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/context/SlingContextImpl.java
    sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/package-info.java
    sling/trunk/testing/mocks/sling-mock/src/test/java/org/apache/sling/testing/mock/sling/resource/AbstractMultipleResourceResolverTest.java

Modified: sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/MockJcrResourceResolverFactory.java
URL: http://svn.apache.org/viewvc/sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/MockJcrResourceResolverFactory.java?rev=1660995&r1=1660994&r2=1660995&view=diff
==============================================================================
--- sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/MockJcrResourceResolverFactory.java
(original)
+++ sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/MockJcrResourceResolverFactory.java
Thu Feb 19 20:20:58 2015
@@ -19,6 +19,7 @@
 package org.apache.sling.testing.mock.sling;
 
 import java.util.Dictionary;
+import java.util.Enumeration;
 import java.util.HashMap;
 import java.util.Hashtable;
 import java.util.Map;
@@ -36,7 +37,6 @@ import org.apache.sling.resourceresolver
 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.component.ComponentContext;
 
 import com.google.common.collect.ImmutableMap;
@@ -49,15 +49,14 @@ import com.google.common.collect.Immutab
 class MockJcrResourceResolverFactory implements ResourceResolverFactory {
 
     private final SlingRepository slingRepository;
+    private final BundleContext bundleContext;
 
-    public MockJcrResourceResolverFactory(final SlingRepository repository) {
+    public MockJcrResourceResolverFactory(final SlingRepository repository, BundleContext
bundleContext) {
         this.slingRepository = repository;
+        this.bundleContext = bundleContext;
     }
 
     private ResourceResolver getResourceResolverInternal(Map<String, Object> authenticationInfo,
boolean isAdmin) throws LoginException {
-        // setup mock OSGi environment
-        BundleContext bundleContext = MockOsgi.newBundleContext();
-
         Dictionary<String, Object> resourceProviderFactoryFactoryProps = new Hashtable<String,
Object>();
         resourceProviderFactoryFactoryProps.put(Constants.SERVICE_VENDOR, "sling-mock");
         resourceProviderFactoryFactoryProps.put(Constants.SERVICE_DESCRIPTION, "sling-mock");
@@ -66,7 +65,9 @@ class MockJcrResourceResolverFactory imp
         ComponentContext resourceProviderComponentContext = MockOsgi.newComponentContext(bundleContext,
resourceProviderFactoryFactoryProps);
 
         // setup mocked JCR environment
-        bundleContext.registerService(SlingRepository.class.getName(), this.slingRepository,
null);
+        if (bundleContext.getServiceReference(SlingRepository.class.getName()) == null) {
+            bundleContext.registerService(SlingRepository.class.getName(), this.slingRepository,
null);
+        }
 
         // setup real sling JCR resource provider implementation for use in
         // mocked context
@@ -84,14 +85,10 @@ class MockJcrResourceResolverFactory imp
 
         Dictionary<Object, Object> resourceProviderProps = new Hashtable<Object,
Object>();
         resourceProviderProps.put(ResourceProvider.ROOTS, new String[] { "/" });
-        bundleContext.registerService(ResourceProvider.class.getName(), resourceProvider,
resourceProviderProps);
-        ServiceReference resourceProviderServiceReference = bundleContext.getServiceReference(ResourceProvider.class.getName());
 
-        // setup real sling resource resolver implementation for use in mocked
-        // context
+        // setup real sling resource resolver implementation for use in mocked context
         MockResourceResolverFactoryActivator activator = new MockResourceResolverFactoryActivator();
-        activator.bindResourceProvider(resourceProvider,
-                getServiceReferenceProperties(resourceProviderServiceReference));
+        activator.bindResourceProvider(resourceProvider, toMap(resourceProviderProps));
         activator.activate(resourceProviderComponentContext);
         CommonResourceResolverFactoryImpl commonFactoryImpl = new CommonResourceResolverFactoryImpl(activator);
         ResourceResolverContext context = new ResourceResolverContext(true, authenticationInfo,
new ResourceAccessSecurityTracker());
@@ -99,15 +96,16 @@ class MockJcrResourceResolverFactory imp
         return resourceResolver;
     }
 
-    private Map<String, Object> getServiceReferenceProperties(final ServiceReference
serviceReference) {
-        Map<String, Object> props = new HashMap<String, Object>();
-        String[] keys = serviceReference.getPropertyKeys();
-        for (String key : keys) {
-            props.put(key, serviceReference.getProperty(key));
+    private static Map<String, Object> toMap(Dictionary<Object, Object> dictionary)
{
+        Map<String,Object> map = new HashMap<String, Object>();
+        Enumeration<Object> keys = dictionary.keys();
+        while (keys.hasMoreElements()) {
+            String key = keys.nextElement().toString();
+            map.put(key, dictionary.get(key));
         }
-        return props;
+        return map;
     }
-
+    
     @Override
     public ResourceResolver getResourceResolver(final Map<String, Object> authenticationInfo)
throws LoginException {
         return getResourceResolverInternal(authenticationInfo, false);

Modified: sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/MockSling.java
URL: http://svn.apache.org/viewvc/sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/MockSling.java?rev=1660995&r1=1660994&r2=1660995&view=diff
==============================================================================
--- sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/MockSling.java
(original)
+++ sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/MockSling.java
Thu Feb 19 20:20:58 2015
@@ -58,6 +58,16 @@ public final class MockSling {
      * @return Resource resolver factory instance
      */
     public static ResourceResolverFactory newResourceResolverFactory(final ResourceResolverType
type) {
+        return newResourceResolverFactory(type, MockOsgi.newBundleContext());
+    }
+    
+    /**
+     * Creates new sling resource resolver factory instance.
+     * @param type Type of underlying repository.
+     * @return Resource resolver factory instance
+     */
+    public static ResourceResolverFactory newResourceResolverFactory(final ResourceResolverType
type,
+            final BundleContext bundleContext) {
         ResourceResolverTypeAdapter adapter = getResourceResolverTypeAdapter(type);
         ResourceResolverFactory factory = adapter.newResourceResolverFactory();
         if (factory == null) {
@@ -65,7 +75,7 @@ public final class MockSling {
             if (repository == null) {
                 throw new RuntimeException("Adapter neither provides resource resolver factory
nor sling repository.");
             }
-            factory = new MockJcrResourceResolverFactory(repository);
+            factory = new MockJcrResourceResolverFactory(repository, bundleContext);
         }
         return factory;
     }

Modified: sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/context/ContextResourceResolverFactory.java
URL: http://svn.apache.org/viewvc/sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/context/ContextResourceResolverFactory.java?rev=1660995&r1=1660994&r2=1660995&view=diff
==============================================================================
--- sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/context/ContextResourceResolverFactory.java
(original)
+++ sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/context/ContextResourceResolverFactory.java
Thu Feb 19 20:20:58 2015
@@ -27,6 +27,7 @@ import org.apache.sling.api.resource.Res
 import org.apache.sling.api.resource.ResourceResolverFactory;
 import org.apache.sling.testing.mock.sling.MockSling;
 import org.apache.sling.testing.mock.sling.ResourceResolverType;
+import org.osgi.framework.BundleContext;
 
 /**
  * Create resolve resolver instance and initialize it depending on it's type.
@@ -37,13 +38,14 @@ final class ContextResourceResolverFacto
         // static methods only
     }
 
-    public static ResourceResolverFactory get(final ResourceResolverType resourceResolverType)
{
+    public static ResourceResolverFactory get(final ResourceResolverType resourceResolverType,
+            final BundleContext bundleContext) {
         ResourceResolverType type = resourceResolverType;
         if (type == null) {
             type = MockSling.DEFAULT_RESOURCERESOLVER_TYPE;
         }
         try {
-            ResourceResolverFactory factory = MockSling.newResourceResolverFactory(type);
+            ResourceResolverFactory factory = MockSling.newResourceResolverFactory(type,
bundleContext);
 
             switch (type) {
             case JCR_MOCK:

Modified: sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/context/SlingContextImpl.java
URL: http://svn.apache.org/viewvc/sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/context/SlingContextImpl.java?rev=1660995&r1=1660994&r2=1660995&view=diff
==============================================================================
--- sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/context/SlingContextImpl.java
(original)
+++ sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/context/SlingContextImpl.java
Thu Feb 19 20:20:58 2015
@@ -100,7 +100,7 @@ public class SlingContextImpl extends Os
      * @return Resource resolver factory
      */
     protected ResourceResolverFactory newResourceResolverFactory() {
-        return ContextResourceResolverFactory.get(this.resourceResolverType);
+        return ContextResourceResolverFactory.get(this.resourceResolverType, bundleContext());
     }
 
     /**

Modified: sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/package-info.java
URL: http://svn.apache.org/viewvc/sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/package-info.java?rev=1660995&r1=1660994&r2=1660995&view=diff
==============================================================================
--- sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/package-info.java
(original)
+++ sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/package-info.java
Thu Feb 19 20:20:58 2015
@@ -19,5 +19,5 @@
 /**
  * Mock implementation of selected Sling APIs.
  */
-@aQute.bnd.annotation.Version("1.0")
+@aQute.bnd.annotation.Version("1.1")
 package org.apache.sling.testing.mock.sling;

Modified: sling/trunk/testing/mocks/sling-mock/src/test/java/org/apache/sling/testing/mock/sling/resource/AbstractMultipleResourceResolverTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/testing/mocks/sling-mock/src/test/java/org/apache/sling/testing/mock/sling/resource/AbstractMultipleResourceResolverTest.java?rev=1660995&r1=1660994&r2=1660995&view=diff
==============================================================================
--- sling/trunk/testing/mocks/sling-mock/src/test/java/org/apache/sling/testing/mock/sling/resource/AbstractMultipleResourceResolverTest.java
(original)
+++ sling/trunk/testing/mocks/sling-mock/src/test/java/org/apache/sling/testing/mock/sling/resource/AbstractMultipleResourceResolverTest.java
Thu Feb 19 20:20:58 2015
@@ -25,9 +25,11 @@ import static org.junit.Assert.assertNul
 import org.apache.sling.api.resource.Resource;
 import org.apache.sling.api.resource.ResourceResolver;
 import org.apache.sling.api.resource.ResourceResolverFactory;
+import org.apache.sling.testing.mock.osgi.MockOsgi;
 import org.apache.sling.testing.mock.sling.MockSling;
 import org.apache.sling.testing.mock.sling.ResourceResolverType;
 import org.junit.Test;
+import org.osgi.framework.BundleContext;
 
 import com.google.common.collect.ImmutableMap;
 
@@ -36,10 +38,11 @@ import com.google.common.collect.Immutab
  */
 public abstract class AbstractMultipleResourceResolverTest {
 
+    private final BundleContext bundleContext = MockOsgi.newBundleContext();
     protected abstract ResourceResolverType getResourceResolverType();
 
     protected ResourceResolverFactory newResourceResolerFactory() {
-        return MockSling.newResourceResolverFactory(getResourceResolverType());
+        return MockSling.newResourceResolverFactory(getResourceResolverType(), bundleContext);
     }
     
     @Test



Mime
View raw message