You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by en...@apache.org on 2021/06/20 23:13:14 UTC

[sling-org-apache-sling-testing-resourceresolver-mock] branch master updated: SLING-10508 customize how MockResource objects are created

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

enorman pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-testing-resourceresolver-mock.git


The following commit(s) were added to refs/heads/master by this push:
     new e8c792f  SLING-10508 customize how MockResource objects are created
e8c792f is described below

commit e8c792fe218858077b5809bda70c537cb42c67a4
Author: Eric Norman <en...@apache.org>
AuthorDate: Sun Jun 20 16:12:50 2021 -0700

    SLING-10508 customize how MockResource objects are created
    
    by the MockResourceResolver
---
 .../DefaultMockResourceFactory.java                | 37 ++++++++++++++++++++++
 .../resourceresolver/MockResourceFactory.java      | 34 ++++++++++++++++++++
 .../resourceresolver/MockResourceResolver.java     | 17 ++++++----
 .../MockResourceResolverFactoryOptions.java        | 14 ++++++++
 .../testing/resourceresolver/package-info.java     |  2 +-
 5 files changed, 97 insertions(+), 7 deletions(-)

diff --git a/src/main/java/org/apache/sling/testing/resourceresolver/DefaultMockResourceFactory.java b/src/main/java/org/apache/sling/testing/resourceresolver/DefaultMockResourceFactory.java
new file mode 100644
index 0000000..af167f2
--- /dev/null
+++ b/src/main/java/org/apache/sling/testing/resourceresolver/DefaultMockResourceFactory.java
@@ -0,0 +1,37 @@
+/*
+ * 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 java.util.Map;
+
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.api.resource.ResourceResolver;
+
+/**
+ * Default factory implementation that always returns a MockResource
+ * object
+ */
+public class DefaultMockResourceFactory implements MockResourceFactory {
+
+    @Override
+    public Resource newMockResource(final String path, 
+            final Map<String, Object> properties, 
+            final ResourceResolver resolver) {
+        return new MockResource(path, properties, resolver);
+    }
+
+}
diff --git a/src/main/java/org/apache/sling/testing/resourceresolver/MockResourceFactory.java b/src/main/java/org/apache/sling/testing/resourceresolver/MockResourceFactory.java
new file mode 100644
index 0000000..3885b7d
--- /dev/null
+++ b/src/main/java/org/apache/sling/testing/resourceresolver/MockResourceFactory.java
@@ -0,0 +1,34 @@
+/*
+ * 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 java.util.Map;
+
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.api.resource.ResourceResolver;
+
+/**
+ * Implement this interface to customize how MockResource objects
+ * are created by the MockResourceResolver
+ */
+public interface MockResourceFactory {
+
+    public Resource newMockResource(final String path, 
+            final Map<String, Object> properties,
+            final ResourceResolver resolver);
+
+}
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 7b33891..e09f529 100644
--- a/src/main/java/org/apache/sling/testing/resourceresolver/MockResourceResolver.java
+++ b/src/main/java/org/apache/sling/testing/resourceresolver/MockResourceResolver.java
@@ -181,14 +181,12 @@ public class MockResourceResolver extends SlingAdaptable implements ResourceReso
             }
             final Map<String, Object> tempProps = this.temporaryResources.get(normalizedPath);
             if ( tempProps != null ) {
-                final Resource rsrc = new MockResource(normalizedPath, tempProps, this);
-                return rsrc;
+                return newMockResource(normalizedPath, tempProps, this);
             }
             synchronized ( this.resources ) {
                 final Map<String, Object> props = this.resources.get(normalizedPath);
                 if ( props != null ) {
-                    final Resource rsrc = new MockResource(normalizedPath, props, this);
-                    return rsrc;
+                    return newMockResource(normalizedPath, props, this);
                 }
             }
         } else {
@@ -244,11 +242,18 @@ public class MockResourceResolver extends SlingAdaptable implements ResourceReso
         }
         final List<Resource> children = new ArrayList<Resource>();
         for(final Map.Entry<String, Map<String, Object>> e : candidates.entrySet()) {
-            children.add(new MockResource(e.getKey(), e.getValue(), this));
+            children.add(newMockResource(e.getKey(), e.getValue(), this));
         }
         return children.iterator();
     }
 
+    private Resource newMockResource(final String path, 
+            final Map<String, Object> properties,
+            final ResourceResolver resolver) {
+        return this.options.getMockResourceFactory()
+                    .newMockResource(path, properties, resolver);
+    }
+
     @Override
     public @NotNull Iterable<Resource> getChildren(final @NotNull Resource parent) {
         return new Iterable<Resource>() {
@@ -322,7 +327,7 @@ public class MockResourceResolver extends SlingAdaptable implements ResourceReso
             properties = new HashMap<String, Object>();
         }
 
-        Resource mockResource = new MockResource(path, properties, this);
+        Resource mockResource = newMockResource(path, properties, this);
         this.temporaryResources.put(path, ResourceUtil.getValueMap(mockResource));
         return mockResource;
     }
diff --git a/src/main/java/org/apache/sling/testing/resourceresolver/MockResourceResolverFactoryOptions.java b/src/main/java/org/apache/sling/testing/resourceresolver/MockResourceResolverFactoryOptions.java
index cde8efc..64705e6 100644
--- a/src/main/java/org/apache/sling/testing/resourceresolver/MockResourceResolverFactoryOptions.java
+++ b/src/main/java/org/apache/sling/testing/resourceresolver/MockResourceResolverFactoryOptions.java
@@ -27,6 +27,8 @@ import org.osgi.service.event.EventAdmin;
  */
 public class MockResourceResolverFactoryOptions {
 
+    private MockResourceFactory mockResourceFactory;
+
     private EventAdmin eventAdmin;
 
     private String[] searchPaths = new String[] {"/apps/", "/libs/"};
@@ -63,4 +65,16 @@ public class MockResourceResolverFactoryOptions {
         return this;
     }
 
+    public @NotNull MockResourceFactory getMockResourceFactory() {
+        if (mockResourceFactory == null) {
+            mockResourceFactory = new DefaultMockResourceFactory();
+        }
+        return mockResourceFactory;
+    }
+    
+    public @NotNull MockResourceResolverFactoryOptions setMockResourceFactory(MockResourceFactory factory) {
+        this.mockResourceFactory = factory;
+        return this;
+    }
+
 }
diff --git a/src/main/java/org/apache/sling/testing/resourceresolver/package-info.java b/src/main/java/org/apache/sling/testing/resourceresolver/package-info.java
index d735a2f..48d8cf1 100644
--- a/src/main/java/org/apache/sling/testing/resourceresolver/package-info.java
+++ b/src/main/java/org/apache/sling/testing/resourceresolver/package-info.java
@@ -19,5 +19,5 @@
 /**
  * Apache Sling Testing Resource Resolver Mock
  */
-@org.osgi.annotation.versioning.Version("2.0.0")
+@org.osgi.annotation.versioning.Version("2.1.0")
 package org.apache.sling.testing.resourceresolver;