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 6728203be51b0370b3cca89b986ad3bd7aca284f
Author: Stefan Seifert <[email protected]>
AuthorDate: Sun May 17 13:22:45 2015 +0000

    SLING-4721 sling-mock: Fully support ResourceResolverFactoryActivator for 
jcr-mock
    
    git-svn-id: 
https://svn.apache.org/repos/asf/sling/trunk/testing/mocks/sling-mock@1679854 
13f79535-47bb-0310-9956-ffa450edef68
---
 .../mock/sling/MockJcrResourceResolverFactory.java | 50 ++++++++++++++-------
 .../MockResourceResolverFactoryActivator.java      | 52 ----------------------
 ...Test.java => AbstractSlingContextImplTest.java} |  6 ++-
 .../jcrmock/context/SlingContextImplTest.java      | 31 +++++++++++++
 .../sling/rrmock/context/SlingContextImplTest.java | 31 +++++++++++++
 5 files changed, 101 insertions(+), 69 deletions(-)

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
index 816f9a6..dbc54fd 100644
--- 
a/src/main/java/org/apache/sling/testing/mock/sling/MockJcrResourceResolverFactory.java
+++ 
b/src/main/java/org/apache/sling/testing/mock/sling/MockJcrResourceResolverFactory.java
@@ -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.SlingRepository;
 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 implements 
ResourceResolverFactory {
         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 implements 
ResourceResolverFactory {
         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);
diff --git 
a/src/main/java/org/apache/sling/testing/mock/sling/MockResourceResolverFactoryActivator.java
 
b/src/main/java/org/apache/sling/testing/mock/sling/MockResourceResolverFactoryActivator.java
deleted file mode 100644
index 12dc624..0000000
--- 
a/src/main/java/org/apache/sling/testing/mock/sling/MockResourceResolverFactoryActivator.java
+++ /dev/null
@@ -1,52 +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.Map;
-
-import org.apache.sling.api.resource.ResourceProvider;
-import org.apache.sling.api.resource.ResourceProviderFactory;
-import org.apache.sling.resourceresolver.impl.ResourceResolverFactoryActivator;
-import org.osgi.service.component.ComponentContext;
-
-/**
- * Overrides some behavior of {@link ResourceResolverFactoryActivator} to allow
- * usage in mocking.
- */
-class MockResourceResolverFactoryActivator extends 
ResourceResolverFactoryActivator {
-
-    // make public
-    @Override
-    public void activate(final ComponentContext componentContext) {
-        super.activate(componentContext);
-    }
-
-    // make public
-    @Override
-    public void bindResourceProviderFactory(final ResourceProviderFactory 
provider, final Map<String, Object> props) {
-        super.bindResourceProviderFactory(provider, props);
-    }
-
-    // make public
-    @Override
-    public void bindResourceProvider(final ResourceProvider provider, final 
Map<String, Object> props) {
-        super.bindResourceProvider(provider, props);
-    }
-
-}
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/AbstractSlingContextImplTest.java
similarity index 97%
rename from 
src/test/java/org/apache/sling/testing/mock/sling/context/SlingContextImplTest.java
rename to 
src/test/java/org/apache/sling/testing/mock/sling/context/AbstractSlingContextImplTest.java
index b917a14..ddfa08a 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/AbstractSlingContextImplTest.java
@@ -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());
diff --git 
a/src/test/java/org/apache/sling/testing/mock/sling/jcrmock/context/SlingContextImplTest.java
 
b/src/test/java/org/apache/sling/testing/mock/sling/jcrmock/context/SlingContextImplTest.java
new file mode 100644
index 0000000..73b951c
--- /dev/null
+++ 
b/src/test/java/org/apache/sling/testing/mock/sling/jcrmock/context/SlingContextImplTest.java
@@ -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;
+    }
+
+}
diff --git 
a/src/test/java/org/apache/sling/testing/mock/sling/rrmock/context/SlingContextImplTest.java
 
b/src/test/java/org/apache/sling/testing/mock/sling/rrmock/context/SlingContextImplTest.java
new file mode 100644
index 0000000..d1374e2
--- /dev/null
+++ 
b/src/test/java/org/apache/sling/testing/mock/sling/rrmock/context/SlingContextImplTest.java
@@ -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;
+    }
+
+}

-- 
To stop receiving notification emails like this one, please contact
"[email protected]" <[email protected]>.

Reply via email to