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

joerghoh pushed a commit to branch SLING-11982-avoid-repo-access
in repository 
https://gitbox.apache.org/repos/asf/sling-org-apache-sling-servlets-resolver.git

commit 2d99034e4497c83a8705d4f1200d664039fe3ef2
Author: Joerg Hoh <[email protected]>
AuthorDate: Sun Jul 23 12:16:24 2023 +0200

    added unit test coverage
---
 .../helper/NamedScriptResourceCollector.java       |  1 -
 .../internal/helper/ResourceCollector.java         |  1 -
 .../helper/AbstractResourceCollectorTest.java      | 83 ++++++++++++++++++++++
 3 files changed, 83 insertions(+), 2 deletions(-)

diff --git 
a/src/main/java/org/apache/sling/servlets/resolver/internal/helper/NamedScriptResourceCollector.java
 
b/src/main/java/org/apache/sling/servlets/resolver/internal/helper/NamedScriptResourceCollector.java
index 2df58e5..2bf2668 100644
--- 
a/src/main/java/org/apache/sling/servlets/resolver/internal/helper/NamedScriptResourceCollector.java
+++ 
b/src/main/java/org/apache/sling/servlets/resolver/internal/helper/NamedScriptResourceCollector.java
@@ -18,7 +18,6 @@
  */
 package org.apache.sling.servlets.resolver.internal.helper;
 
-import java.util.Iterator;
 import java.util.List;
 import java.util.Set;
 
diff --git 
a/src/main/java/org/apache/sling/servlets/resolver/internal/helper/ResourceCollector.java
 
b/src/main/java/org/apache/sling/servlets/resolver/internal/helper/ResourceCollector.java
index 0d436de..d159256 100644
--- 
a/src/main/java/org/apache/sling/servlets/resolver/internal/helper/ResourceCollector.java
+++ 
b/src/main/java/org/apache/sling/servlets/resolver/internal/helper/ResourceCollector.java
@@ -19,7 +19,6 @@
 package org.apache.sling.servlets.resolver.internal.helper;
 
 import java.util.Arrays;
-import java.util.Iterator;
 import java.util.List;
 import java.util.Set;
 
diff --git 
a/src/test/java/org/apache/sling/servlets/resolver/internal/helper/AbstractResourceCollectorTest.java
 
b/src/test/java/org/apache/sling/servlets/resolver/internal/helper/AbstractResourceCollectorTest.java
new file mode 100644
index 0000000..9e3cdd3
--- /dev/null
+++ 
b/src/test/java/org/apache/sling/servlets/resolver/internal/helper/AbstractResourceCollectorTest.java
@@ -0,0 +1,83 @@
+/*
+ * 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.servlets.resolver.internal.helper;
+
+import static org.junit.Assert.assertEquals;
+
+import java.util.List;
+
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.testing.mock.sling.junit.SlingContext;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.mockito.Mockito;
+
+public class AbstractResourceCollectorTest {
+
+    
+    @Rule
+    public SlingContext context = new SlingContext();
+    
+    
+    @Before
+    public void setup() {
+        context.create().resource("/parent");
+        context.create().resource("/parent/child1");
+        context.create().resource("/parent/child2");
+        context.create().resource("/parent/child3");
+    }
+    
+    @Test
+    public void testWithCachingEnabled() {
+        
+        Resource spy = 
Mockito.spy(context.resourceResolver().getResource("/parent"));
+        
+        List<Resource> children = 
AbstractResourceCollector.getChildrenList(spy, true);
+        assertEquals(3,children.size());
+        children = AbstractResourceCollector.getChildrenList(spy, true);
+        Mockito.verify(spy,Mockito.times(1)).listChildren();
+    }
+    
+    
+    @Test
+    public void testWithCachingDisabled() {
+        
+        Resource spy = 
Mockito.spy(context.resourceResolver().getResource("/parent"));
+        
+        List<Resource> children = 
AbstractResourceCollector.getChildrenList(spy, false);
+        assertEquals(3,children.size());
+        children = AbstractResourceCollector.getChildrenList(spy, false);
+        Mockito.verify(spy,Mockito.times(2)).listChildren();
+    }
+    
+    @Test
+    public void testWithCachingWithAlreadyUsedCacheKey() {
+        
+        Resource spy = 
Mockito.spy(context.resourceResolver().getResource("/parent"));
+        String payload = "some payload";
+        
context.resourceResolver().getPropertyMap().put(AbstractResourceCollector.CACHE_KEY_CHILDREN_LIST,
 payload);
+        
+        List<Resource> children = 
AbstractResourceCollector.getChildrenList(spy, true);
+        assertEquals(3,children.size());
+        children = AbstractResourceCollector.getChildrenList(spy, true);
+        Mockito.verify(spy,Mockito.times(2)).listChildren();
+        assertEquals(payload, 
context.resourceResolver().getPropertyMap().get(AbstractResourceCollector.CACHE_KEY_CHILDREN_LIST));
+    }
+}

Reply via email to