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

joerghoh pushed a commit to branch master
in repository 
https://gitbox.apache.org/repos/asf/sling-org-apache-sling-servlets-resolver.git


The following commit(s) were added to refs/heads/master by this push:
     new c1e47b0  SLING-11558 part1: remove the LocationIterator (#31)
c1e47b0 is described below

commit c1e47b02350c477a3b9781602ea7d72fd9a467ee
Author: Jörg Hoh <[email protected]>
AuthorDate: Sat Jun 10 13:17:00 2023 +0200

    SLING-11558 part1: remove the LocationIterator (#31)
    
    * convert the iterator-based approach into a collection-based approach.
    * convert the unit-tests 1:1 and add a few more to improve test coverage
    
    ---------
    
    Co-authored-by: angela <[email protected]>
---
 .../internal/helper/AbstractResourceCollector.java |  16 +-
 ...ocationIterator.java => LocationCollector.java} | 201 ++++-----
 ...teratorTest.java => LocationCollectorTest.java} | 500 +++++++++------------
 3 files changed, 306 insertions(+), 411 deletions(-)

diff --git 
a/src/main/java/org/apache/sling/servlets/resolver/internal/helper/AbstractResourceCollector.java
 
b/src/main/java/org/apache/sling/servlets/resolver/internal/helper/AbstractResourceCollector.java
index 03c0535..9a09dbd 100644
--- 
a/src/main/java/org/apache/sling/servlets/resolver/internal/helper/AbstractResourceCollector.java
+++ 
b/src/main/java/org/apache/sling/servlets/resolver/internal/helper/AbstractResourceCollector.java
@@ -20,7 +20,6 @@ package org.apache.sling.servlets.resolver.internal.helper;
 
 import java.util.ArrayList;
 import java.util.Collection;
-import java.util.Iterator;
 import java.util.List;
 import java.util.Set;
 import java.util.SortedSet;
@@ -95,11 +94,10 @@ public abstract class AbstractResourceCollector {
             }
             return o1.compareTo(o2);
         });
-        final Iterator<String> locations = new LocationIterator(resourceType, 
resourceSuperType,
-                                                                
baseResourceType, resolver);
-        while (locations.hasNext()) {
-            final String location = locations.next();
-
+        
+        
+        List<String> locations = LocationCollector.getLocations(resourceType, 
resourceSuperType, baseResourceType, resolver);
+        locations.forEach(location -> {
             // get the location resource, use a synthetic resource if there
             // is no real location. There may still be children at this
             // location
@@ -111,7 +109,7 @@ public abstract class AbstractResourceCollector {
             }
             final Resource locationRes = getResource(resolver, path);
             getWeightedResources(resources, locationRes);
-        }
+        });
 
         List<Resource> result = new ArrayList<>(resources.size());
         result.addAll(resources);
@@ -160,10 +158,6 @@ public abstract class AbstractResourceCollector {
         Resource res = resolver.getResource(path);
 
         if (res == null) {
-            if (!path.startsWith("/")) {
-                path = "/".concat(path);
-            }
-
             res = new SyntheticResource(resolver, path, "$synthetic$");
         }
 
diff --git 
a/src/main/java/org/apache/sling/servlets/resolver/internal/helper/LocationIterator.java
 
b/src/main/java/org/apache/sling/servlets/resolver/internal/helper/LocationCollector.java
similarity index 52%
rename from 
src/main/java/org/apache/sling/servlets/resolver/internal/helper/LocationIterator.java
rename to 
src/main/java/org/apache/sling/servlets/resolver/internal/helper/LocationCollector.java
index f7fdc3c..60b3ff3 100644
--- 
a/src/main/java/org/apache/sling/servlets/resolver/internal/helper/LocationIterator.java
+++ 
b/src/main/java/org/apache/sling/servlets/resolver/internal/helper/LocationCollector.java
@@ -18,18 +18,21 @@
  */
 package org.apache.sling.servlets.resolver.internal.helper;
 
+import java.util.ArrayList;
 import java.util.HashSet;
-import java.util.Iterator;
-import java.util.NoSuchElementException;
+import java.util.List;
 import java.util.Set;
 
+import org.apache.commons.lang3.StringUtils;
 import org.apache.sling.api.resource.Resource;
 import org.apache.sling.api.resource.ResourceResolver;
 import org.apache.sling.api.resource.ResourceUtil;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
 import org.slf4j.LoggerFactory;
 
 /**
- * The <code>LocationIterator</code> provides access to an ordered collection
+ * The <code>LocationCollector</code> provides access to an ordered collection
  * of absolute paths containing potential request handling. The primary order 
of
  * the collection is the resource type hierarchy with the base resource type at
  * the top. The secondary order is the search path retrieved from the resource
@@ -47,132 +50,97 @@ import org.slf4j.LoggerFactory;
  * <li><code>/libs/default</code></li>
  * </ol>
  */
-public class LocationIterator implements Iterator<String> {
 
-    // The resource resolver used to find resource super types of
-    // resource types
-    private final ResourceResolver resolver;
+class LocationCollector {
 
-    // The base resource type to be used as a final entry if there is
-    // no more resource super type. This is kind of the java.lang.Object
-    // the resource type hierarchy.
-    private final String baseResourceType;
+    static @NotNull List<String> getLocations(String resourceType, String 
resourceSuperType, String baseResourceType,
+                                                     ResourceResolver 
resolver) {
+        LocationCollector collector = new LocationCollector(resourceType, 
resourceSuperType, baseResourceType, resolver);
+        return collector.getResolvedLocations();
+    }
 
     // The search path of the resource resolver
     private final String[] searchPath;
 
-    // counter into the search path array
-    private int pathCounter;
-
-    // The resource type to use for the next iteration loop
-    private String resourceType;
-
-    // The first resource type to use
-    private final String firstResourceType;
-
-    // The first resource super type to use
-    private final String firstResourceSuperType;
-
-    // The current relative path generated from the resource type
-    private String relPath;
-
-    // the next absolute path to return from next(). This is null
-    // if there is no more location to return
-    private String nextLocation;
+    private final ResourceResolver resolver;
+    private final String baseResourceType;
+    private final String resourceType;
+    private final String resourceSuperType;
 
     /** Set of used resource types to detect a circular resource type 
hierarchy. */
     private final Set<String> usedResourceTypes = new HashSet<>();
+    
+    private final List<String> result = new ArrayList<>();
 
-    /**
-     * Creates an instance of this iterator starting with a location built from
-     * the resource type of the <code>resource</code> and ending with the
-     * given <code>baseResourceType</code>.
-     *
-     * @param resourceType the initial resource type.
-     * @param resourceSuperType the initial resource super type.
-     * @param baseResourceType The base resource type.
-     * @param resolver The resource resolver
-     */
-    public LocationIterator(String resourceType, String resourceSuperType, 
String baseResourceType,
-            ResourceResolver resolver) {
-        this.resolver = resolver;
+    private LocationCollector(String resourceType, String resourceSuperType, 
String baseResourceType,
+                              ResourceResolver resolver) {
+
+        this.resourceType = resourceType;
+        this.resourceSuperType = resourceSuperType;
         this.baseResourceType = baseResourceType;
+        this.resolver = resolver;
 
         String[] tmpPath = resolver.getSearchPath();
         if (tmpPath.length == 0) {
             tmpPath = new String[] { "/" };
         }
         searchPath = tmpPath;
-
-        this.firstResourceType = resourceType;
-        this.firstResourceSuperType = resourceSuperType;
-        // we start with the first resource type
-        this.resourceType = firstResourceType;
-
         this.usedResourceTypes.add(this.resourceType);
-
-        nextLocation = seek();
+        collectPaths();
     }
 
-    /**
-     * Returns <code>true</code> if there is another entry
-     */
-    public boolean hasNext() {
-        return nextLocation != null;
+    private @NotNull List<String> getResolvedLocations() {
+        return result;
     }
 
     /**
-     * Returns the next entry of this iterator.
-     *
-     * @throws NoSuchElementException if {@link #hasNext()} returns
-     *             <code>false</code>.
+     * Collect all resource types
      */
-    public String next() {
-        if (!hasNext()) {
-            throw new NoSuchElementException();
+    private void collectPaths() {
+        
+        String rt = this.resourceType;
+        do {
+            String superType = handleResourceType(rt);
+            rt = superType;
+        } while (rt != null);
+        
+        // add default resourceTypes
+        final String defaultResourceTypeSuffix;
+        boolean blankResourceType = StringUtils.isBlank(resourceType);
+        if (blankResourceType) {
+            defaultResourceTypeSuffix = "";
+        } else {
+            defaultResourceTypeSuffix = this.baseResourceType;
+        }
+        for (String spath : searchPath) {
+            result.add(spath + defaultResourceTypeSuffix);
         }
-
-        String result = nextLocation;
-        nextLocation = seek();
-        return result;
     }
-
+    
     /**
-     * @throws UnsupportedOperationException
+     * Add all necessary path entries to the result list, and return the 
resourceSuperType
+     * for the given resourceType
+     * @param resourceType the resourceType
+     * @return the resourceSuperType or null if the given resourceType does 
not have a resourceSuperType
      */
-    @Override
-    public void remove() {
-        throw new UnsupportedOperationException();
-    }
-
-    private String seek() {
-
-        if (relPath == null) {
-
-            if (resourceType == null) {
-                return null;
-            }
-
-            String typePath = ResourceUtil.resourceTypeToPath(resourceType);
-            if (typePath.startsWith("/")) {
-                resourceType = getResourceSuperType(resourceType);
-                return typePath;
+    private @Nullable String handleResourceType (@NotNull String resourceType) 
{
+        boolean isBlank = StringUtils.isBlank(resourceType);
+        boolean isAbsoluteResourceType = resourceType.startsWith("/");
+        String rst = null;
+        if (!isBlank) {
+            if (isAbsoluteResourceType) {
+                result.add(ResourceUtil.resourceTypeToPath(resourceType));
+            } else {
+                for (String spath : searchPath) {
+                    result.add(spath + 
ResourceUtil.resourceTypeToPath(resourceType));
+                }
+                
             }
-
-            relPath = typePath;
-        }
-
-        String result = searchPath[pathCounter].concat(relPath);
-
-        pathCounter++;
-        if (pathCounter >= searchPath.length) {
-            relPath = null;
-            resourceType = getResourceSuperType(resourceType);
-            pathCounter = 0;
+            rst = getResourceSuperType(resourceType);
         }
-
-        return result;
+        return rst;
     }
+    
 
     /**
      * Returns the resource super type of the given resource type:
@@ -186,7 +154,7 @@ public class LocationIterator implements Iterator<String> {
      * <li>Otherwise the base resource type is returned.</li>
      * </ol>
      */
-    private String getResourceSuperType(String resourceType) {
+    private @Nullable String getResourceSuperType(@NotNull String 
resourceType) {
 
         // if the current resource type is the default value, there are no more
         if (resourceType.equals(baseResourceType)) {
@@ -195,9 +163,9 @@ public class LocationIterator implements Iterator<String> {
 
         // get the super type of the current resource type
         String superType;
-        if (resourceType.equals(this.firstResourceType)
-                && this.firstResourceSuperType != null ) {
-            superType = this.firstResourceSuperType;
+        if (resourceType.equals(this.resourceType)
+                && this.resourceSuperType != null ) {
+            superType = this.resourceSuperType;
         } else {
             superType = getResourceSuperType(resolver, resourceType);
         }
@@ -211,42 +179,33 @@ public class LocationIterator implements Iterator<String> 
{
                 this.usedResourceTypes.add(superType);
             }
         }
-        // use default resource type if there is no super type any more
-        if (superType == null) {
-            superType = baseResourceType;
-        }
-
         return superType;
     }
 
     // this method is largely duplicated from ResourceUtil
-    private String getResourceSuperType(final ResourceResolver 
resourceResolver,
-                                        final String resourceType) {
+    private @Nullable String getResourceSuperType(final @NotNull 
ResourceResolver resourceResolver,
+                                                  final @NotNull String 
resourceType) {
         // normalize resource type to a path string
         final String rtPath = ResourceUtil.resourceTypeToPath(resourceType);
         // get the resource type resource and check its super type
-        String resourceSuperType = null;
+        String rst = null;
         // if the path is absolute, use it directly
-        if ( rtPath.startsWith("/") ) {
-            final String candidatePath = rtPath;
-
-            final Resource rtResource = 
resourceResolver.getResource(candidatePath);
-            if ( rtResource != null ) {
-                resourceSuperType = rtResource.getResourceSuperType();
+        if (rtPath.startsWith("/")) {
+            final Resource rtResource = resourceResolver.getResource(rtPath);
+            if (rtResource != null) {
+                rst = rtResource.getResourceSuperType();
             }
-
         } else {
             // if the path is relative we use the search paths
             for (final String path : this.searchPath) {
                 final String candidatePath = path + rtPath;
                 final Resource rtResource = 
resourceResolver.getResource(candidatePath);
-                if ( rtResource != null && rtResource.getResourceSuperType() 
!= null ) {
-                    resourceSuperType = rtResource.getResourceSuperType();
+                if (rtResource != null && rtResource.getResourceSuperType() != 
null) {
+                    rst = rtResource.getResourceSuperType();
                     break;
                 }
             }
         }
-        return resourceSuperType;
+        return rst;
     }
-
 }
diff --git 
a/src/test/java/org/apache/sling/servlets/resolver/internal/helper/LocationIteratorTest.java
 
b/src/test/java/org/apache/sling/servlets/resolver/internal/helper/LocationCollectorTest.java
similarity index 54%
rename from 
src/test/java/org/apache/sling/servlets/resolver/internal/helper/LocationIteratorTest.java
rename to 
src/test/java/org/apache/sling/servlets/resolver/internal/helper/LocationCollectorTest.java
index 1a43385..f640360 100644
--- 
a/src/test/java/org/apache/sling/servlets/resolver/internal/helper/LocationIteratorTest.java
+++ 
b/src/test/java/org/apache/sling/servlets/resolver/internal/helper/LocationCollectorTest.java
@@ -20,51 +20,49 @@ package org.apache.sling.servlets.resolver.internal.helper;
 
 import static 
org.apache.sling.api.servlets.ServletResolverConstants.DEFAULT_RESOURCE_TYPE;
 
+import java.util.Arrays;
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 
+import static org.hamcrest.CoreMatchers.*;
+import static org.hamcrest.MatcherAssert.assertThat;
+
 import org.apache.sling.api.resource.PersistenceException;
 import org.apache.sling.api.resource.Resource;
 import org.apache.sling.api.resource.ResourceResolver;
 import org.apache.sling.api.resource.ResourceUtil;
 
-public class LocationIteratorTest extends HelperTestBase {
+public class LocationCollectorTest extends HelperTestBase {
 
-    private LocationIterator getLocationIterator(final String resourceType,
+    List<String> getLocations(final String resourceType,
             final String resourceSuperType) {
-       return this.getLocationIterator(resourceType, resourceSuperType, 
DEFAULT_RESOURCE_TYPE);
+        return getLocations(resourceType, resourceSuperType, 
DEFAULT_RESOURCE_TYPE);
     }
-
-    private LocationIterator getLocationIterator(final String resourceType,
+    
+    List<String> getLocations( final String resourceType,
             final String resourceSuperType,
             final String baseResourceType) {
-        final LocationIterator li = new LocationIterator(resourceType,
+        return LocationCollector.getLocations(resourceType,
                 resourceSuperType,
                 baseResourceType,
                 this.resourceResolver);
-        return li;
     }
-
+    
     public void testSearchPathEmpty() {
         // expect path gets { "/" }
         resourceResolverOptions.setSearchPaths(null);
 
         final Resource r = request.getResource();
-        LocationIterator li = getLocationIterator(r.getResourceType(),
+        List<String> loc = getLocations(r.getResourceType(),
                 r.getResourceSuperType());
-
-        // 1. /foo/bar
-        assertTrue(li.hasNext());
-        assertEquals("/" + resourceTypePath, li.next());
-
-        // 2. /sling/servlet/default
-        assertTrue(li.hasNext());
-        assertEquals("/" + DEFAULT_RESOURCE_TYPE, li.next());
-
-        // 3. finished
-        assertFalse(li.hasNext());
+        
+        List<String> expected = Arrays.asList(
+                       "/" + resourceTypePath, // /foo/bar
+                "/" + DEFAULT_RESOURCE_TYPE); // /sling/servlet/default
+        assertThat(loc,is(expected));
     }
-
+    
     public void testSearchPath1Element() {
         String root0 = "/apps/";
         resourceResolverOptions.setSearchPaths(new String[] {
@@ -72,21 +70,15 @@ public class LocationIteratorTest extends HelperTestBase {
         });
 
         final Resource r = request.getResource();
-        LocationIterator li = getLocationIterator(r.getResourceType(),
+        List<String> loc = getLocations(r.getResourceType(),
                 r.getResourceSuperType());
-
-        // 1. /apps/foo/bar
-        assertTrue(li.hasNext());
-        assertEquals(root0 + resourceTypePath, li.next());
-
-        // 2. /apps/sling/servlet/default
-        assertTrue(li.hasNext());
-        assertEquals(root0 + DEFAULT_RESOURCE_TYPE, li.next());
-
-        // 3. finished
-        assertFalse(li.hasNext());
+        
+        List<String> expected = Arrays.asList(
+                       root0 + resourceTypePath, // /apps/foo/bar
+                root0 + DEFAULT_RESOURCE_TYPE); // /apps/sling/servlet/default
+        assertThat(loc,is(expected));
     }
-
+    
     public void testSearchPath2Elements() {
         String root0 = "/apps/";
         String root1 = "/libs/";
@@ -96,29 +88,17 @@ public class LocationIteratorTest extends HelperTestBase {
         });
 
         final Resource r = request.getResource();
-        LocationIterator li = getLocationIterator(r.getResourceType(),
+        List<String> loc = getLocations(r.getResourceType(),
                 r.getResourceSuperType());
-
-        // 1. /apps/foo/bar
-        assertTrue(li.hasNext());
-        assertEquals(root0 + resourceTypePath, li.next());
-
-        // 2. /libs/foo/bar
-        assertTrue(li.hasNext());
-        assertEquals(root1 + resourceTypePath, li.next());
-
-        // 3. /apps/sling/servlet/default
-        assertTrue(li.hasNext());
-        assertEquals(root0 + DEFAULT_RESOURCE_TYPE, li.next());
-
-        // 4. /libs/sling/servlet/default
-        assertTrue(li.hasNext());
-        assertEquals(root1 + DEFAULT_RESOURCE_TYPE, li.next());
-
-        // 5. finished
-        assertFalse(li.hasNext());
+        
+        List<String> expected = Arrays.asList(
+                       root0 + resourceTypePath, // /apps/foo/bar
+                root1 + resourceTypePath, // /libs/foo/bar
+                root0 + DEFAULT_RESOURCE_TYPE, // /apps/sling/servlet/default
+                root1 + DEFAULT_RESOURCE_TYPE); // /libs/sling/servlet/default
+        assertThat(loc,is(expected));
     }
-
+    
     /**
      * Replace a resource with a different type
      * 
@@ -150,21 +130,15 @@ public class LocationIteratorTest extends HelperTestBase {
         replaceResource(resourceType, null);
 
         final Resource r = request.getResource();
-        LocationIterator li = getLocationIterator(r.getResourceType(),
+        List<String> loc = getLocations(r.getResourceType(),
                 r.getResourceSuperType());
-
-        // 1. /foo/bar
-        assertTrue(li.hasNext());
-        assertEquals(resourceTypePath, li.next());
-
-        // 2. /sling/servlet/default
-        assertTrue(li.hasNext());
-        assertEquals("/" + DEFAULT_RESOURCE_TYPE, li.next());
-
-        // 3. finished
-        assertFalse(li.hasNext());
+        
+        List<String> expected = Arrays.asList(
+                       resourceTypePath, // /foo/bar
+                "/" + DEFAULT_RESOURCE_TYPE); // /sling/servlet/default
+        assertThat(loc,is(expected));
     }
-
+    
     public void testSearchPath1ElementAbsoluteType() {
         String root0 = "/apps/";
         resourceResolverOptions.setSearchPaths(new String[] {
@@ -177,21 +151,16 @@ public class LocationIteratorTest extends HelperTestBase {
         replaceResource(resourceType, null);
 
         final Resource r = request.getResource();
-        LocationIterator li = getLocationIterator(r.getResourceType(),
+        List<String> loc = getLocations(r.getResourceType(),
                 r.getResourceSuperType());
-
-        // 1. /foo/bar
-        assertTrue(li.hasNext());
-        assertEquals(resourceTypePath, li.next());
-
-        // 2. /apps/sling/servlet/default
-        assertTrue(li.hasNext());
-        assertEquals(root0 + DEFAULT_RESOURCE_TYPE, li.next());
-
-        // 3. finished
-        assertFalse(li.hasNext());
+        
+        
+        List<String> expected = Arrays.asList(
+                       resourceTypePath, // /foo/bar
+                root0 + DEFAULT_RESOURCE_TYPE); // /apps/sling/servlet/default
+        assertThat(loc,is(expected));
     }
-
+    
     public void testSearchPath2ElementsAbsoluteType() {
         String root0 = "/apps/";
         String root1 = "/libs/";
@@ -206,25 +175,16 @@ public class LocationIteratorTest extends HelperTestBase {
         replaceResource(resourceType, null);
 
         final Resource r = request.getResource();
-        LocationIterator li = getLocationIterator(r.getResourceType(),
+        List<String> loc = getLocations(r.getResourceType(),
                 r.getResourceSuperType());
-
-        // 1. /foo/bar
-        assertTrue(li.hasNext());
-        assertEquals(resourceTypePath, li.next());
-
-        // 2. /apps/sling/servlet/default
-        assertTrue(li.hasNext());
-        assertEquals(root0 + DEFAULT_RESOURCE_TYPE, li.next());
-
-        // 3. /libs/sling/servlet/default
-        assertTrue(li.hasNext());
-        assertEquals(root1 + DEFAULT_RESOURCE_TYPE, li.next());
-
-        // 4. finished
-        assertFalse(li.hasNext());
+        
+        List<String> expected = Arrays.asList(
+                       resourceTypePath, // /foo/bar
+                root0 + DEFAULT_RESOURCE_TYPE, // /apps/sling/servlet/default
+                root1 + DEFAULT_RESOURCE_TYPE); // /libs/sling/servlet/default
+        assertThat(loc,is(expected));
     }
-
+    
     public void testSearchPathEmptyWithSuper() {
         // expect path gets { "/" }
         resourceResolverOptions.setSearchPaths(null);
@@ -235,25 +195,16 @@ public class LocationIteratorTest extends HelperTestBase {
         replaceResource(null, resourceSuperType);
 
         final Resource r = request.getResource();
-        LocationIterator li = getLocationIterator(r.getResourceType(),
+        List<String> loc = getLocations(r.getResourceType(),
                 r.getResourceSuperType());
-
-        // 1. /foo/bar
-        assertTrue(li.hasNext());
-        assertEquals("/" + resourceTypePath, li.next());
-
-        // 2. /foo/superBar
-        assertTrue(li.hasNext());
-        assertEquals("/" + resourceSuperTypePath, li.next());
-
-        // 3. /sling/servlet/default
-        assertTrue(li.hasNext());
-        assertEquals("/" + DEFAULT_RESOURCE_TYPE, li.next());
-
-        // 4. finished
-        assertFalse(li.hasNext());
+        
+        List<String> expected = Arrays.asList(
+                       "/" + resourceTypePath, // /foo/bar
+                "/" + resourceSuperTypePath, // /foo/superBar
+                "/" + DEFAULT_RESOURCE_TYPE); // /sling/servlet/default
+        assertThat(loc,is(expected));
     }
-
+    
     public void testSearchPath1ElementWithSuper() {
         String root0 = "/apps/";
         resourceResolverOptions.setSearchPaths(new String[] {
@@ -266,25 +217,16 @@ public class LocationIteratorTest extends HelperTestBase {
         replaceResource(null, resourceSuperType);
 
         final Resource r = request.getResource();
-        LocationIterator li = getLocationIterator(r.getResourceType(),
+        List<String> loc = getLocations(r.getResourceType(),
                 r.getResourceSuperType());
-
-        // 1. /apps/foo/bar
-        assertTrue(li.hasNext());
-        assertEquals(root0 + resourceTypePath, li.next());
-
-        // 2. /apps/foo/superBar
-        assertTrue(li.hasNext());
-        assertEquals(root0 + resourceSuperTypePath, li.next());
-
-        // 3. /apps/sling/servlet/default
-        assertTrue(li.hasNext());
-        assertEquals(root0 + DEFAULT_RESOURCE_TYPE, li.next());
-
-        // 4. finished
-        assertFalse(li.hasNext());
+        
+        List<String> expected = Arrays.asList(
+                       root0 + resourceTypePath, // /apps/foo/bar
+                root0 + resourceSuperTypePath, // /apps/foo/superBar
+                root0 + DEFAULT_RESOURCE_TYPE); // /apps/sling/servlet/default
+        assertThat(loc,is(expected));
     }
-
+    
     public void testSearchPath2ElementsWithSuper() {
         String root0 = "/apps/";
         String root1 = "/libs/";
@@ -299,37 +241,19 @@ public class LocationIteratorTest extends HelperTestBase {
         replaceResource(null, resourceSuperType);
 
         final Resource r = request.getResource();
-        LocationIterator li = getLocationIterator(r.getResourceType(),
+        List<String> loc = getLocations(r.getResourceType(),
                 r.getResourceSuperType());
-
-        // 1. /apps/foo/bar
-        assertTrue(li.hasNext());
-        assertEquals(root0 + resourceTypePath, li.next());
-
-        // 2. /libs/foo/bar
-        assertTrue(li.hasNext());
-        assertEquals(root1 + resourceTypePath, li.next());
-
-        // 3. /apps/foo/superBar
-        assertTrue(li.hasNext());
-        assertEquals(root0 + resourceSuperTypePath, li.next());
-
-        // 4. /libs/foo/superBar
-        assertTrue(li.hasNext());
-        assertEquals(root1 + resourceSuperTypePath, li.next());
-
-        // 5. /apps/sling/servlet/default
-        assertTrue(li.hasNext());
-        assertEquals(root0 + DEFAULT_RESOURCE_TYPE, li.next());
-
-        // 6. /libs/sling/servlet/default
-        assertTrue(li.hasNext());
-        assertEquals(root1 + DEFAULT_RESOURCE_TYPE, li.next());
-
-        // 7. finished
-        assertFalse(li.hasNext());
+        
+        List<String> expected = Arrays.asList(
+                       root0 + resourceTypePath, // /apps/foo/bar
+                root1 + resourceTypePath, // /libs/foo/bar
+                root0 + resourceSuperTypePath, // /apps/foo/superBar
+                root1 + resourceSuperTypePath, // /libs/foo/superBar
+                root0 + DEFAULT_RESOURCE_TYPE, // /apps/sling/servlet/default
+                root1 + DEFAULT_RESOURCE_TYPE); // /libs/sling/servlet/default
+        assertThat(loc,is(expected));
     }
-
+    
     public void testSearchPathEmptyAbsoluteTypeWithSuper() {
         // expect path gets { "/" }
         resourceResolverOptions.setSearchPaths(null);
@@ -344,25 +268,16 @@ public class LocationIteratorTest extends HelperTestBase {
         replaceResource(resourceType, resourceSuperType);
 
         final Resource r = request.getResource();
-        LocationIterator li = getLocationIterator(r.getResourceType(),
+        List<String> loc = getLocations(r.getResourceType(),
                 r.getResourceSuperType());
-
-        // 1. /foo/bar
-        assertTrue(li.hasNext());
-        assertEquals(resourceTypePath, li.next());
-
-        // 2. /foo/superBar
-        assertTrue(li.hasNext());
-        assertEquals("/" + resourceSuperTypePath, li.next());
-
-        // 3. /sling/servlet/default
-        assertTrue(li.hasNext());
-        assertEquals("/" + DEFAULT_RESOURCE_TYPE, li.next());
-
-        // 4. finished
-        assertFalse(li.hasNext());
+        
+        List<String> expected = Arrays.asList(
+                       resourceTypePath, // /foo/bar
+                "/" + resourceSuperTypePath, // /foo/superBar
+                "/" + DEFAULT_RESOURCE_TYPE); // /sling/servlet/default
+        assertThat(loc,is(expected));
     }
-
+    
     public void testSearchPath1ElementAbsoluteTypeWithSuper() {
         String root0 = "/apps/";
         resourceResolverOptions.setSearchPaths(new String[] {
@@ -379,25 +294,16 @@ public class LocationIteratorTest extends HelperTestBase {
         replaceResource(resourceType, resourceSuperType);
 
         final Resource r = request.getResource();
-        LocationIterator li = getLocationIterator(r.getResourceType(),
+        List<String> loc = getLocations(r.getResourceType(),
                 r.getResourceSuperType());
-
-        // 1. /foo/bar
-        assertTrue(li.hasNext());
-        assertEquals(resourceTypePath, li.next());
-
-        // 2. /apps/foo/superBar
-        assertTrue(li.hasNext());
-        assertEquals(root0 + resourceSuperTypePath, li.next());
-
-        // 3. /apps/sling/servlet/default
-        assertTrue(li.hasNext());
-        assertEquals(root0 + DEFAULT_RESOURCE_TYPE, li.next());
-
-        // 4. finished
-        assertFalse(li.hasNext());
+        
+        List<String> expected = Arrays.asList(
+                       resourceTypePath, // /foo/bar
+                root0 + resourceSuperTypePath, // /apps/foo/superBar
+                root0 + DEFAULT_RESOURCE_TYPE); // /apps/sling/servlet/default
+        assertThat(loc,is(expected));
     }
-
+    
     public void testSearchPath2ElementsAbsoluteTypeWithSuper() {
         String root0 = "/apps/";
         String root1 = "/libs/";
@@ -416,33 +322,18 @@ public class LocationIteratorTest extends HelperTestBase {
         replaceResource(resourceType, resourceSuperType);
 
         final Resource r = request.getResource();
-        LocationIterator li = getLocationIterator(r.getResourceType(),
+        List<String> loc = getLocations(r.getResourceType(),
                 r.getResourceSuperType());
-
-        // 1. /foo/bar
-        assertTrue(li.hasNext());
-        assertEquals(resourceTypePath, li.next());
-
-        // 2. /apps/foo/superBar
-        assertTrue(li.hasNext());
-        assertEquals(root0 + resourceSuperTypePath, li.next());
-
-        // 3. /libs/foo/superBar
-        assertTrue(li.hasNext());
-        assertEquals(root1 + resourceSuperTypePath, li.next());
-
-        // 4. /apps/sling/servlet/default
-        assertTrue(li.hasNext());
-        assertEquals(root0 + DEFAULT_RESOURCE_TYPE, li.next());
-
-        // 5. /libs/sling/servlet/default
-        assertTrue(li.hasNext());
-        assertEquals(root1 + DEFAULT_RESOURCE_TYPE, li.next());
-
-        // 6. finished
-        assertFalse(li.hasNext());
+        
+        List<String> expected = Arrays.asList(
+                       resourceTypePath, // /foo/bar
+                root0 + resourceSuperTypePath, // /apps/foo/superBar
+                root1 + resourceSuperTypePath, // /libs/foo/superBar
+                root0 + DEFAULT_RESOURCE_TYPE, // /apps/sling/servlet/default
+                root1 + DEFAULT_RESOURCE_TYPE); // /libs/sling/servlet/default
+        assertThat(loc,is(expected));
     }
-
+    
     public void testScriptNameWithoutResourceType() {
         String root0 = "/apps/";
         String root1 = "/libs/";
@@ -450,16 +341,13 @@ public class LocationIteratorTest extends HelperTestBase {
                 root0,
                 root1
         });
-        LocationIterator li = getLocationIterator("",
-                null,
-                "");
-        assertTrue(li.hasNext());
-        assertEquals("/apps/", li.next());
-        assertTrue(li.hasNext());
-        assertEquals("/libs/", li.next());
-        assertFalse(li.hasNext());
+        List<String> loc = getLocations("",
+                null,"");
+        
+        List<String> expected = Arrays.asList("/apps/","/libs/");
+        assertThat(loc,is(expected));
     }
-
+    
     public void testScriptNameWithResourceType() {
         String root0 = "/apps/";
         String root1 = "/libs/";
@@ -467,19 +355,16 @@ public class LocationIteratorTest extends HelperTestBase {
                 root0,
                 root1
         });
-        LocationIterator li = getLocationIterator("a/b",
-                null);
-        assertTrue(li.hasNext());
-        assertEquals(root0 + "a/b", li.next());
-        assertTrue(li.hasNext());
-        assertEquals(root1 + "a/b", li.next());
-        assertTrue(li.hasNext());
-        assertEquals(root0 + DEFAULT_RESOURCE_TYPE, li.next());
-        assertTrue(li.hasNext());
-        assertEquals(root1 + DEFAULT_RESOURCE_TYPE, li.next());
-        assertFalse(li.hasNext());
+        List<String> loc = getLocations("a/b", null);
+        
+        List<String> expected = Arrays.asList(
+                       root0 + "a/b",
+                root1 + "a/b",
+                root0 + DEFAULT_RESOURCE_TYPE,
+                root1 + DEFAULT_RESOURCE_TYPE);
+        assertThat(loc,is(expected));
     }
-
+    
     public void testScriptNameWithResourceTypeAndSuperType() {
         String root0 = "/apps/";
         String root1 = "/libs/";
@@ -487,23 +372,19 @@ public class LocationIteratorTest extends HelperTestBase {
                 root0,
                 root1
         });
-        LocationIterator li = getLocationIterator("a/b",
-                "c/d");
-        assertTrue(li.hasNext());
-        assertEquals(root0 + "a/b", li.next());
-        assertTrue(li.hasNext());
-        assertEquals(root1 + "a/b", li.next());
-        assertTrue(li.hasNext());
-        assertEquals(root0 + "c/d", li.next());
-        assertTrue(li.hasNext());
-        assertEquals(root1 + "c/d", li.next());
-        assertTrue(li.hasNext());
-        assertEquals(root0 + DEFAULT_RESOURCE_TYPE, li.next());
-        assertTrue(li.hasNext());
-        assertEquals(root1 + DEFAULT_RESOURCE_TYPE, li.next());
-        assertFalse(li.hasNext());
+        
+        List<String> loc = getLocations("a/b", "c/d");
+
+        List<String> expected = Arrays.asList(
+                       root0 + "a/b",
+                root1 + "a/b",
+                root0 + "c/d",
+                root1 + "c/d",
+                root0 + DEFAULT_RESOURCE_TYPE,
+                root1 + DEFAULT_RESOURCE_TYPE);
+        assertThat(loc,is(expected));
     }
-
+    
     public void testCircularResourceTypeHierarchy() {
         final String root1 = "/libs/";
         resourceResolverOptions.setSearchPaths(new String[] {
@@ -538,27 +419,88 @@ public class LocationIteratorTest extends HelperTestBase {
         } catch (PersistenceException e) {
             fail("Did not expect a persistence exception: " + e.getMessage());
         }
-
-        LocationIterator li = getLocationIterator(resourceType,
-                resourceSuperType);
-
-        // 1. /libs/foo/bar
-        assertTrue(li.hasNext());
-        assertEquals(root1 + resourceType, li.next());
-
-        // 1. /libs/foo/check1
-        assertTrue(li.hasNext());
-        assertEquals(root1 + resourceSuperType, li.next());
-
-        // 3. /libs/foo/check2
-        assertTrue(li.hasNext());
-        assertEquals(root1 + resourceSuperType2, li.next());
-
-        // 4. /libs/sling/servlet/default
-        assertTrue(li.hasNext());
-        assertEquals(root1 + DEFAULT_RESOURCE_TYPE, li.next());
-
-        // 5. finished
-        assertFalse(li.hasNext());
+        
+        List<String> loc = getLocations(resourceType, resourceSuperType);
+        
+        List<String> expected = Arrays.asList(
+                       root1 + resourceType, // /libs/foo/bar
+                root1 + resourceSuperType, // /libs/foo/check1
+                root1 + resourceSuperType2, // /libs/foo/check2
+                root1 + DEFAULT_RESOURCE_TYPE); // /libs/sling/servlet/default
+        assertThat(loc,is(expected));
+    }
+    
+    
+    public void testResolveDefaultResourceType() {
+       
+       List<String> loc = getLocations(DEFAULT_RESOURCE_TYPE, 
resourceSuperType);
+       
+       List<String> expected = Arrays.asList(
+                       "/apps/sling/servlet/default",
+                       "/libs/sling/servlet/default",
+                       "/apps/sling/servlet/default",
+                       "/libs/sling/servlet/default"
+                       );
+       assertThat(loc,is(expected));
+    }
+ 
+    
+    public void testAbsoluteResourceSuperType() throws Exception {
+        final String root = "/apps/";
+        resourceResolverOptions.setSearchPaths(new String[] {
+                root
+        });
+        
+        String resourceType="a/b";
+        String resourceTypePath= root + resourceType;
+        
+        String resourceSuperType= "/apps/c/d";
+        String resourceSuperTypePath = resourceSuperType;
+        
+        Map<String, Object> resourceTypeProps = new HashMap<>();
+        resourceTypeProps.put(ResourceResolver.PROPERTY_RESOURCE_TYPE, 
resourceType);
+        resourceTypeProps.put("sling:resourceSuperType", resourceSuperType);
+        
+               
resourceResolver.create(getOrCreateParentResource(resourceResolver, 
resourceTypePath),
+                               ResourceUtil.getName(resourceTypePath), 
resourceTypeProps);
+               
resourceResolver.create(getOrCreateParentResource(resourceResolver, 
resourceSuperTypePath),
+                               ResourceUtil.getName(resourceSuperTypePath), 
null);
+        
+        
+       List<String> loc = getLocations(resourceType, resourceSuperType);
+       
+       List<String> expected = Arrays.asList(
+                       resourceTypePath,                       // /apps/a/b
+                       resourceSuperTypePath,                  // /apps/c/d
+                       root + DEFAULT_RESOURCE_TYPE    // 
/apps/sling/servlet/default
+                       );
+       assertThat(loc,is(expected));
+    }
+    
+    
+    public void testNoSuperType() throws Exception {
+        final String root = "/apps/";
+        resourceResolverOptions.setSearchPaths(new String[] {
+                root
+        });
+        
+        String resourceType="a/b";
+        String resourceTypePath= root + resourceType;
+        
+        Map<String, Object> resourceTypeProps = new HashMap<>();
+        resourceTypeProps.put(ResourceResolver.PROPERTY_RESOURCE_TYPE, 
resourceType);
+        
+               
resourceResolver.create(getOrCreateParentResource(resourceResolver, 
resourceTypePath),
+                               ResourceUtil.getName(resourceTypePath), 
resourceTypeProps);
+        
+        
+       List<String> loc = getLocations(resourceType, resourceSuperType);
+       
+       List<String> expected = Arrays.asList(
+                       resourceTypePath,                       // /apps/a/b
+                       root + DEFAULT_RESOURCE_TYPE    // 
/apps/sling/servlet/default
+                       );
+       assertThat(loc,is(expected));
     }
+    
 }


Reply via email to