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:20:05 UTC

[sling-org-apache-sling-testing-resourceresolver-mock] 10/12: Refactoring and javadoc updates

This is an automated email from the ASF dual-hosted git repository.

rombert pushed a commit to annotated tag org.apache.sling.testing.resourceresolver-mock-0.2.0
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-testing-resourceresolver-mock.git

commit 812de294d0be80e69c0d3a39bc651428201268cd
Author: Carsten Ziegeler <cz...@apache.org>
AuthorDate: Thu Jan 30 14:24:23 2014 +0000

    Refactoring and javadoc updates
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/testing/resourceresolver-mock@1562837 13f79535-47bb-0310-9956-ffa450edef68
---
 .../resourceresolver/MockResourceResolver.java     | 17 ++++---
 .../MockResourceResolverFactory.java               | 32 ++++++++++---
 .../MockResourceResolverFactoryOptions.java        | 52 ++++++++++++++++++++++
 3 files changed, 85 insertions(+), 16 deletions(-)

diff --git a/src/main/java/org/apache/sling/testing/resourceresolver/MockResourceResolver.java b/src/main/java/org/apache/sling/testing/resourceresolver/MockResourceResolver.java
index 6dfe885..a28b62b 100644
--- a/src/main/java/org/apache/sling/testing/resourceresolver/MockResourceResolver.java
+++ b/src/main/java/org/apache/sling/testing/resourceresolver/MockResourceResolver.java
@@ -36,7 +36,6 @@ import org.apache.sling.api.resource.PersistenceException;
 import org.apache.sling.api.resource.Resource;
 import org.apache.sling.api.resource.ResourceResolver;
 import org.osgi.service.event.Event;
-import org.osgi.service.event.EventAdmin;
 
 public class MockResourceResolver implements ResourceResolver {
 
@@ -46,11 +45,11 @@ public class MockResourceResolver implements ResourceResolver {
 
     private final Set<String> deletedResources = new HashSet<String>();
 
-    private final EventAdmin eventAdmin;
+    private final MockResourceResolverFactoryOptions options;
 
-    public MockResourceResolver(final EventAdmin eventAdmin,
+    public MockResourceResolver(final MockResourceResolverFactoryOptions options,
             final Map<String, Map<String, Object>> resources) {
-        this.eventAdmin = eventAdmin;
+        this.options = options;
         this.resources = resources;
     }
 
@@ -130,7 +129,7 @@ public class MockResourceResolver implements ResourceResolver {
 
     @Override
     public String[] getSearchPath() {
-        return new String[] {"/apps", "/libs"};
+        return this.options.getSearchPaths();
     }
 
     @Override
@@ -270,25 +269,25 @@ public class MockResourceResolver implements ResourceResolver {
     public void commit() throws PersistenceException {
         synchronized ( this.resources ) {
             for(final String path : this.deletedResources ) {
-                if ( this.resources.remove(path) != null && this.eventAdmin != null ) {
+                if ( this.resources.remove(path) != null && this.options.getEventAdmin() != null ) {
                     final Map<String, Object> props = new HashMap<String, Object>();
                     props.put(SlingConstants.PROPERTY_PATH, path);
                     final Event e = new Event(SlingConstants.TOPIC_RESOURCE_REMOVED, props);
-                    this.eventAdmin.sendEvent(e);
+                    this.options.getEventAdmin().sendEvent(e);
                 }
                 this.temporaryResources.remove(path);
             }
             for(final String path : this.temporaryResources.keySet() ) {
                 final boolean changed = this.resources.containsKey(path);
                 this.resources.put(path, this.temporaryResources.get(path));
-                if ( this.eventAdmin != null ) {
+                if ( this.options.getEventAdmin() != null ) {
                     final Map<String, Object> props = new HashMap<String, Object>();
                     props.put(SlingConstants.PROPERTY_PATH, path);
                     if ( this.resources.get(path).get(ResourceResolver.PROPERTY_RESOURCE_TYPE) != null ) {
                         props.put(SlingConstants.PROPERTY_RESOURCE_TYPE, this.resources.get(path).get(ResourceResolver.PROPERTY_RESOURCE_TYPE));
                     }
                     final Event e = new Event(changed ? SlingConstants.TOPIC_RESOURCE_CHANGED : SlingConstants.TOPIC_RESOURCE_ADDED, props);
-                    this.eventAdmin.sendEvent(e);
+                    this.options.getEventAdmin().sendEvent(e);
                 }
             }
         }
diff --git a/src/main/java/org/apache/sling/testing/resourceresolver/MockResourceResolverFactory.java b/src/main/java/org/apache/sling/testing/resourceresolver/MockResourceResolverFactory.java
index 2e7980b..086b6fc 100644
--- a/src/main/java/org/apache/sling/testing/resourceresolver/MockResourceResolverFactory.java
+++ b/src/main/java/org/apache/sling/testing/resourceresolver/MockResourceResolverFactory.java
@@ -27,36 +27,54 @@ import org.apache.sling.api.resource.ResourceResolver;
 import org.apache.sling.api.resource.ResourceResolverFactory;
 import org.osgi.service.event.EventAdmin;
 
+/**
+ * Simple resource resolver factory
+ */
 public class MockResourceResolverFactory implements ResourceResolverFactory {
 
+    /** We use a linked hash map to preserve creation order. */
     private final Map<String, Map<String, Object>> resources = new LinkedHashMap<String, Map<String, Object>>();
 
-    private final EventAdmin eventAdmin;
+    private final MockResourceResolverFactoryOptions options;
 
+    /**
+     * Create a new resource resolver factory
+     * @param eventAdmin All resource events are sent to this event admin
+     */
     public MockResourceResolverFactory(final EventAdmin eventAdmin) {
-        this.eventAdmin = eventAdmin;
-        resources.put("/", new HashMap<String, Object>());
+        this(new MockResourceResolverFactoryOptions().setEventAdmin(eventAdmin));
     }
 
+    /**
+     * Create a new resource resolver factory.
+     */
     public MockResourceResolverFactory() {
-        this(null);
+        this(new MockResourceResolverFactoryOptions());
+    }
+
+    /**
+     * Create a new resource resolver factory.
+     */
+    public MockResourceResolverFactory(final MockResourceResolverFactoryOptions options) {
+        this.options = options;
+        resources.put("/", new HashMap<String, Object>());
     }
 
     @Override
     public ResourceResolver getResourceResolver(
             final Map<String, Object> authenticationInfo) throws LoginException {
-        return new MockResourceResolver(this.eventAdmin, resources);
+        return new MockResourceResolver(options, resources);
     }
 
     @Override
     public ResourceResolver getAdministrativeResourceResolver(
             final Map<String, Object> authenticationInfo) throws LoginException {
-        return new MockResourceResolver(this.eventAdmin, resources);
+        return new MockResourceResolver(options, resources);
     }
 
     @Override
     public ResourceResolver getServiceResourceResolver(
             Map<String, Object> authenticationInfo) throws LoginException {
-        return new MockResourceResolver(this.eventAdmin, resources);
+        return new MockResourceResolver(options, resources);
     }
 }
diff --git a/src/main/java/org/apache/sling/testing/resourceresolver/MockResourceResolverFactoryOptions.java b/src/main/java/org/apache/sling/testing/resourceresolver/MockResourceResolverFactoryOptions.java
new file mode 100644
index 0000000..6572797
--- /dev/null
+++ b/src/main/java/org/apache/sling/testing/resourceresolver/MockResourceResolverFactoryOptions.java
@@ -0,0 +1,52 @@
+/*
+ * 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.resourceresolver;
+
+import org.osgi.service.event.EventAdmin;
+
+/**
+ * Options for the factory
+ */
+public class MockResourceResolverFactoryOptions {
+
+    private EventAdmin eventAdmin;
+
+    private String[] searchPaths = new String[] {"/apps", "/libs"};
+
+    public EventAdmin getEventAdmin() {
+        return eventAdmin;
+    }
+
+    public MockResourceResolverFactoryOptions setEventAdmin(EventAdmin eventAdmin) {
+        this.eventAdmin = eventAdmin;
+        return this;
+    }
+
+    public String[] getSearchPaths() {
+        return searchPaths;
+    }
+
+    public MockResourceResolverFactoryOptions setSearchPaths(String[] searchPaths) {
+        if ( searchPaths == null ) {
+            searchPaths = new String[] {};
+        }
+        this.searchPaths = searchPaths;
+        return this;
+    }
+}

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