You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ss...@apache.org on 2015/05/17 15:22:45 UTC

svn commit: r1679854 - in /sling/trunk/testing/mocks/sling-mock/src: main/java/org/apache/sling/testing/mock/sling/ test/java/org/apache/sling/testing/mock/sling/context/ test/java/org/apache/sling/testing/mock/sling/jcrmock/context/ test/java/org/apac...

Author: sseifert
Date: Sun May 17 13:22:45 2015
New Revision: 1679854

URL: http://svn.apache.org/r1679854
Log:
SLING-4721 sling-mock: Fully support ResourceResolverFactoryActivator for jcr-mock

Added:
    sling/trunk/testing/mocks/sling-mock/src/test/java/org/apache/sling/testing/mock/sling/context/AbstractSlingContextImplTest.java
      - copied, changed from r1679609, sling/trunk/testing/mocks/sling-mock/src/test/java/org/apache/sling/testing/mock/sling/context/SlingContextImplTest.java
    sling/trunk/testing/mocks/sling-mock/src/test/java/org/apache/sling/testing/mock/sling/jcrmock/context/
    sling/trunk/testing/mocks/sling-mock/src/test/java/org/apache/sling/testing/mock/sling/jcrmock/context/SlingContextImplTest.java   (with props)
    sling/trunk/testing/mocks/sling-mock/src/test/java/org/apache/sling/testing/mock/sling/rrmock/context/
    sling/trunk/testing/mocks/sling-mock/src/test/java/org/apache/sling/testing/mock/sling/rrmock/context/SlingContextImplTest.java   (with props)
Removed:
    sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/MockResourceResolverFactoryActivator.java
    sling/trunk/testing/mocks/sling-mock/src/test/java/org/apache/sling/testing/mock/sling/context/SlingContextImplTest.java
Modified:
    sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/MockJcrResourceResolverFactory.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=1679854&r1=1679853&r2=1679854&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 Sun May 17 13:22:45 2015
@@ -19,8 +19,6 @@
 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;
 
@@ -35,12 +33,16 @@ import org.apache.sling.jcr.api.SlingRep
 import org.apache.sling.jcr.resource.internal.helper.jcr.JcrResourceProviderFactory;
 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.resourceresolver.impl.ResourceResolverImpl;
 import org.apache.sling.resourceresolver.impl.helper.ResourceResolverContext;
+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.component.ComponentContext;
+import org.osgi.service.event.EventAdmin;
 
 import com.google.common.collect.ImmutableMap;
 
@@ -66,7 +68,6 @@ class MockJcrResourceResolverFactory imp
         resourceProviderFactoryFactoryProps.put(Constants.SERVICE_DESCRIPTION, "sling-mock");
         resourceProviderFactoryFactoryProps.put("resource.resolver.manglenamespaces", true);
         resourceProviderFactoryFactoryProps.put("resource.resolver.searchpath", new String[] { "/apps", "/libs" });
-        ComponentContext resourceProviderComponentContext = MockOsgi.newComponentContext(bundleContext, resourceProviderFactoryFactoryProps);
 
         // setup mocked JCR environment
         if (bundleContext.getServiceReference(SlingRepository.class.getName()) == null) {
@@ -90,27 +91,46 @@ class MockJcrResourceResolverFactory imp
         Dictionary<Object, Object> resourceProviderProps = new Hashtable<Object, Object>();
         resourceProviderProps.put(ResourceProvider.ROOTS, new String[] { "/" });
         resourceProviderProps.put(QueriableResourceProvider.LANGUAGES, new String[] { Query.XPATH, Query.SQL, Query.JCR_SQL2 });
+        bundleContext.registerService(ResourceProvider.class.getName(), resourceProvider, resourceProviderProps);
 
         // setup real sling resource resolver implementation for use in mocked context
-        MockResourceResolverFactoryActivator activator = new MockResourceResolverFactoryActivator();
-        activator.bindResourceProvider(resourceProvider, toMap(resourceProviderProps));
-        activator.activate(resourceProviderComponentContext);
+        ensureResourceResolverFactoryActivatorDependencies();
+        ResourceResolverFactoryActivator activator = new ResourceResolverFactoryActivator();
+        MockOsgi.injectServices(activator, bundleContext);
+        MockOsgi.activate(activator, resourceProviderFactoryFactoryProps);
+        
         CommonResourceResolverFactoryImpl commonFactoryImpl = new CommonResourceResolverFactoryImpl(activator);
         ResourceResolverContext context = new ResourceResolverContext(true, authenticationInfo, new ResourceAccessSecurityTracker());
         ResourceResolverImpl resourceResolver = new ResourceResolverImpl(commonFactoryImpl, context);
         return resourceResolver;
     }
+    
+    /**
+     * 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);
+        }
 
-    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));
+        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);
         }
-        return map;
     }
-    
+
     @Override
     public ResourceResolver getResourceResolver(final Map<String, Object> authenticationInfo) throws LoginException {
         return getResourceResolverInternal(authenticationInfo, false);

Copied: sling/trunk/testing/mocks/sling-mock/src/test/java/org/apache/sling/testing/mock/sling/context/AbstractSlingContextImplTest.java (from r1679609, sling/trunk/testing/mocks/sling-mock/src/test/java/org/apache/sling/testing/mock/sling/context/SlingContextImplTest.java)
URL: http://svn.apache.org/viewvc/sling/trunk/testing/mocks/sling-mock/src/test/java/org/apache/sling/testing/mock/sling/context/AbstractSlingContextImplTest.java?p2=sling/trunk/testing/mocks/sling-mock/src/test/java/org/apache/sling/testing/mock/sling/context/AbstractSlingContextImplTest.java&p1=sling/trunk/testing/mocks/sling-mock/src/test/java/org/apache/sling/testing/mock/sling/context/SlingContextImplTest.java&r1=1679609&r2=1679854&rev=1679854&view=diff
==============================================================================
--- sling/trunk/testing/mocks/sling-mock/src/test/java/org/apache/sling/testing/mock/sling/context/SlingContextImplTest.java (original)
+++ sling/trunk/testing/mocks/sling-mock/src/test/java/org/apache/sling/testing/mock/sling/context/AbstractSlingContextImplTest.java Sun May 17 13:22:45 2015
@@ -45,14 +45,14 @@ import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
 
-public class SlingContextImplTest {
+public abstract class AbstractSlingContextImplTest {
 
     private SlingContextImpl context;
 
     @Before
     public void setUp() throws Exception {
         this.context = new SlingContextImpl();
-        this.context.setResourceResolverType(ResourceResolverType.RESOURCERESOLVER_MOCK);
+        this.context.setResourceResolverType(getResourceResolverType());
         this.context.setUp();
 
         context.addModelsForPackage("org.apache.sling.testing.mock.sling.context");
@@ -66,6 +66,8 @@ public class SlingContextImplTest {
         this.context.tearDown();
     }
     
+    protected abstract ResourceResolverType getResourceResolverType();
+    
     @Test
     public void testContextObjects() {
         assertNotNull(context.componentContext());

Added: sling/trunk/testing/mocks/sling-mock/src/test/java/org/apache/sling/testing/mock/sling/jcrmock/context/SlingContextImplTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/testing/mocks/sling-mock/src/test/java/org/apache/sling/testing/mock/sling/jcrmock/context/SlingContextImplTest.java?rev=1679854&view=auto
==============================================================================
--- sling/trunk/testing/mocks/sling-mock/src/test/java/org/apache/sling/testing/mock/sling/jcrmock/context/SlingContextImplTest.java (added)
+++ sling/trunk/testing/mocks/sling-mock/src/test/java/org/apache/sling/testing/mock/sling/jcrmock/context/SlingContextImplTest.java Sun May 17 13:22:45 2015
@@ -0,0 +1,31 @@
+/*
+ * 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.jcrmock.context;
+
+import org.apache.sling.testing.mock.sling.ResourceResolverType;
+import org.apache.sling.testing.mock.sling.context.AbstractSlingContextImplTest;
+
+public class SlingContextImplTest extends AbstractSlingContextImplTest {
+
+    @Override
+    protected ResourceResolverType getResourceResolverType() {
+        return ResourceResolverType.JCR_MOCK;
+    }
+
+}

Propchange: sling/trunk/testing/mocks/sling-mock/src/test/java/org/apache/sling/testing/mock/sling/jcrmock/context/SlingContextImplTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sling/trunk/testing/mocks/sling-mock/src/test/java/org/apache/sling/testing/mock/sling/jcrmock/context/SlingContextImplTest.java
------------------------------------------------------------------------------
--- svn:keywords (added)
+++ svn:keywords Sun May 17 13:22:45 2015
@@ -0,0 +1 @@
+LastChangedDate LastChangedRevision LastChangedBy HeadURL Id Author

Propchange: sling/trunk/testing/mocks/sling-mock/src/test/java/org/apache/sling/testing/mock/sling/jcrmock/context/SlingContextImplTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: sling/trunk/testing/mocks/sling-mock/src/test/java/org/apache/sling/testing/mock/sling/rrmock/context/SlingContextImplTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/testing/mocks/sling-mock/src/test/java/org/apache/sling/testing/mock/sling/rrmock/context/SlingContextImplTest.java?rev=1679854&view=auto
==============================================================================
--- sling/trunk/testing/mocks/sling-mock/src/test/java/org/apache/sling/testing/mock/sling/rrmock/context/SlingContextImplTest.java (added)
+++ sling/trunk/testing/mocks/sling-mock/src/test/java/org/apache/sling/testing/mock/sling/rrmock/context/SlingContextImplTest.java Sun May 17 13:22:45 2015
@@ -0,0 +1,31 @@
+/*
+ * 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.rrmock.context;
+
+import org.apache.sling.testing.mock.sling.ResourceResolverType;
+import org.apache.sling.testing.mock.sling.context.AbstractSlingContextImplTest;
+
+public class SlingContextImplTest extends AbstractSlingContextImplTest {
+
+    @Override
+    protected ResourceResolverType getResourceResolverType() {
+        return ResourceResolverType.RESOURCERESOLVER_MOCK;
+    }
+
+}

Propchange: sling/trunk/testing/mocks/sling-mock/src/test/java/org/apache/sling/testing/mock/sling/rrmock/context/SlingContextImplTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sling/trunk/testing/mocks/sling-mock/src/test/java/org/apache/sling/testing/mock/sling/rrmock/context/SlingContextImplTest.java
------------------------------------------------------------------------------
--- svn:keywords (added)
+++ svn:keywords Sun May 17 13:22:45 2015
@@ -0,0 +1 @@
+LastChangedDate LastChangedRevision LastChangedBy HeadURL Id Author

Propchange: sling/trunk/testing/mocks/sling-mock/src/test/java/org/apache/sling/testing/mock/sling/rrmock/context/SlingContextImplTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain