Author: cziegeler
Date: Wed Oct 24 07:34:02 2012
New Revision: 1401572

URL: http://svn.apache.org/viewvc?rev=1401572&view=rev
Log:
SLING-2364 : ResourceUtil should provide a method to get the parent on an 
arbitrary level

Modified:
    
sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/ResourceUtil.java
    
sling/trunk/bundles/api/src/test/java/org/apache/sling/api/resource/ResourceUtilTest.java

Modified: 
sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/ResourceUtil.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/ResourceUtil.java?rev=1401572&r1=1401571&r2=1401572&view=diff
==============================================================================
--- 
sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/ResourceUtil.java
 (original)
+++ 
sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/ResourceUtil.java
 Wed Oct 24 07:34:02 2012
@@ -162,6 +162,40 @@ public class ResourceUtil {
     }
 
     /**
+     * Utility method returns the ancestor's path at the given 
<code>level</code>
+     * relative to <code>path</code>, which is normalized by {@link 
#normalize(String)}
+     * before resolving the ancestor.
+     * 
+     * <ul>
+     * <li><code>level</code> = 0 returns the <code>path</code>.</li>
+     * <li><code>level</code> = 1 returns the parent of <code>path</code>, if 
it exists, <code>null</code> otherwise.</li>
+     * <li><code>level</code> = 2 returns the grandparent of 
<code>path</code>, if it exists, <code>null</code> otherwise.</li>
+     * </ul>
+     * 
+     * @param path The path whose ancestor is to be returned.
+     * @param level The relative level of the ancestor, relative to 
<code>path</code>.
+     * @return <code>null</code> if <code>path</code> doesn't have an ancestor 
at the
+     *            specified <code>level</code>.
+     * @throws IllegalArgumentException If the path cannot be normalized by the
+     *             {@link #normalize(String)} method or if <code>level</code> 
< 0.
+     * @throws NullPointerException If <code>path</code> is <code>null</code>.
+     * @since 2.2
+     */
+    public static String getParent(final String path, final int level) {
+        if ( level < 0 ) {
+            throw new IllegalArgumentException("level must be non-negative");
+        }
+        String result = path;
+        for(int i=0; i<level; i++) {
+            result = getParent(result);
+            if ( result == null ) {
+                break;
+            }
+        }
+        return result;
+    }
+
+    /**
      * Utility method returns the parent resource of the resource.
      *
      * @throws NullPointerException If <code>rsrc</code> is <code>null</code>.
@@ -402,9 +436,9 @@ public class ResourceUtil {
             // if the path is relative we use the search paths
             for (final String searchPath : resourceResolver.getSearchPath()) {
                 final Resource rtResource = 
resourceResolver.getResource(searchPath
-                    + rtPath);
+                        + rtPath);
                 if (rtResource != null
-                    && rtResource.getResourceSuperType() != null) {
+                        && rtResource.getResourceSuperType() != null) {
                     resourceSuperType = rtResource.getResourceSuperType();
                     break;
                 }
@@ -429,7 +463,7 @@ public class ResourceUtil {
         String resourceSuperType = resource.getResourceSuperType();
         if (resourceSuperType == null) {
             resourceSuperType = getResourceSuperType(
-                resource.getResourceResolver(), resource.getResourceType());
+                    resource.getResourceResolver(), 
resource.getResourceType());
         }
         return resourceSuperType;
     }
@@ -483,7 +517,7 @@ public class ResourceUtil {
                 return true;
             }
             superType = getResourceSuperType(resource.getResourceResolver(),
-                superType);
+                    superType);
         }
 
         return false;

Modified: 
sling/trunk/bundles/api/src/test/java/org/apache/sling/api/resource/ResourceUtilTest.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/bundles/api/src/test/java/org/apache/sling/api/resource/ResourceUtilTest.java?rev=1401572&r1=1401571&r2=1401572&view=diff
==============================================================================
--- 
sling/trunk/bundles/api/src/test/java/org/apache/sling/api/resource/ResourceUtilTest.java
 (original)
+++ 
sling/trunk/bundles/api/src/test/java/org/apache/sling/api/resource/ResourceUtilTest.java
 Wed Oct 24 07:34:02 2012
@@ -151,7 +151,7 @@ public class ResourceUtilTest {
 
         assertNull(ResourceUtil.getParent("b"));
         assertNull(ResourceUtil.getParent("/b/.."));
-        
+
         assertEquals("security:/", ResourceUtil.getParent("security:/b"));
         assertEquals("security:/b", ResourceUtil.getParent("security:/b/c"));
         assertEquals("security:/b/c", 
ResourceUtil.getParent("security:/b/c/d"));
@@ -384,33 +384,92 @@ public class ResourceUtilTest {
     }
 
     @Test public void testIsStarResource() {
-               final Resource nonStar = context.mock(Resource.class, 
"nonStarResource");
-               final String starPath = "/foo/*";
-               final Resource star = context.mock(Resource.class, 
"starResource");
-               final String nonStarPath = "/foo/*";
+        final Resource nonStar = context.mock(Resource.class, 
"nonStarResource");
+        final String starPath = "/foo/*";
+        final Resource star = context.mock(Resource.class, "starResource");
+        final String nonStarPath = "/foo/*";
         this.context.checking(new Expectations() {{
-               allowing(star).getPath(); will(returnValue(starPath));
-               allowing(nonStar).getPath(); will(returnValue(nonStarPath));
+            allowing(star).getPath(); will(returnValue(starPath));
+            allowing(nonStar).getPath(); will(returnValue(nonStarPath));
         }});
 
-               assertTrue("expecting star==true for path" + starPath,
-                               ResourceUtil.isStarResource(star));
-               assertTrue("expecting star==false for path" + starPath,
-                               ResourceUtil.isStarResource(nonStar));
+        assertTrue("expecting star==true for path" + starPath,
+                ResourceUtil.isStarResource(star));
+        assertTrue("expecting star==false for path" + starPath,
+                ResourceUtil.isStarResource(nonStar));
     }
     @Test public void testIsSyntheticResource() {
-               final Resource synth = new SyntheticResource(null, "foo", 
"bar");
-               final Resource star = context.mock(Resource.class);
+        final Resource synth = new SyntheticResource(null, "foo", "bar");
+        final Resource star = context.mock(Resource.class);
         this.context.checking(new Expectations() {{
-               allowing(star).getPath(); will(returnValue("/foo/*"));
+            allowing(star).getPath(); will(returnValue("/foo/*"));
         }});
         final Resource wrapped = new ResourceWrapper(synth);
 
-               assertTrue("expecting synthetic==true for SyntheticResource",
-                               ResourceUtil.isSyntheticResource(synth));
-               assertFalse("expecting synthetic==false for star resource",
-                               ResourceUtil.isSyntheticResource(star));
-               assertTrue("expecting synthetic==true for wrapped Resource",
-                               ResourceUtil.isSyntheticResource(wrapped));
+        assertTrue("expecting synthetic==true for SyntheticResource",
+                ResourceUtil.isSyntheticResource(synth));
+        assertFalse("expecting synthetic==false for star resource",
+                ResourceUtil.isSyntheticResource(star));
+        assertTrue("expecting synthetic==true for wrapped Resource",
+                ResourceUtil.isSyntheticResource(wrapped));
+    }
+
+    @Test public void testGetParentLevel() throws Exception {
+        boolean caughtNullPointerException = false;
+        try {
+            ResourceUtil.getParent(null, 4);
+        } catch (NullPointerException e) {
+            // Expected exception
+            caughtNullPointerException = true;
+        } catch (Exception e) {
+            fail("Expected NullPointerException, but caught " +
+                    e.getClass().getName() + " instead.");
+        }
+        if (!caughtNullPointerException) {
+            fail("Expected NullPointerException, but no exception was 
thrown.");
+        }
+
+        boolean caughtIllegalArgumentException = false;
+        try {
+            ResourceUtil.getParent("/a/b", -2);
+        } catch (IllegalArgumentException e) {
+            // Expected exception
+            caughtIllegalArgumentException = true;
+        } catch (Exception e) {
+            fail("Expected IllegalArgumentException, but caught " +
+                    e.getClass().getName() + " instead.");
+        }
+        if (!caughtIllegalArgumentException) {
+            fail("Expected IllegalArgumentException, but no exception was 
thrown.");
+        }
+
+        assertNull(ResourceUtil.getParent("/a", 4));
+        assertNull(ResourceUtil.getParent("/", 1));
+        assertNull(ResourceUtil.getParent("b/c", 2));
+        assertNull(ResourceUtil.getParent("/b/..", 1));
+        assertNull(ResourceUtil.getParent("b", 1));
+        assertNull(ResourceUtil.getParent("", 3));
+        assertNull(ResourceUtil.getParent("/..", 1));
+        assertNull(ResourceUtil.getParent("security:/b", 2));
+        assertNull(ResourceUtil.getParent("/b///", 2));
+
+        assertEquals("", ResourceUtil.getParent("", 0));
+        assertEquals("b", ResourceUtil.getParent("b", 0));
+        assertEquals("/", ResourceUtil.getParent("/", 0));
+        assertEquals("/a/b", ResourceUtil.getParent("/a/b", 0));
+        assertEquals("security:/b", ResourceUtil.getParent("security:/b", 0));
+
+        assertEquals("/", ResourceUtil.getParent("/b", 1));
+        assertEquals("b", ResourceUtil.getParent("b/c", 1));
+        assertEquals("b/c", ResourceUtil.getParent("b/c/d", 1));
+        assertEquals("/b/c", ResourceUtil.getParent("/b/c/d", 1));
+        assertEquals("security:/", ResourceUtil.getParent("security:/b", 1));
+        assertEquals("security:/b", ResourceUtil.getParent("security:/b/c", 
1));
+        assertEquals("security:/b/c", 
ResourceUtil.getParent("security:/b/c/d", 1));
+
+        assertEquals("b", ResourceUtil.getParent("b/c/d", 2));
+        assertEquals("b/c", ResourceUtil.getParent("b/c/d/e", 2));
+        assertEquals("/", ResourceUtil.getParent("/b/c/d", 3));
+        assertEquals("/", ResourceUtil.getParent("/b///", 1));
     }
 }


Reply via email to