incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cziege...@apache.org
Subject svn commit: r795007 - in /sling/trunk/bundles/api/src: main/java/org/apache/sling/api/resource/ResourceUtil.java test/java/org/apache/sling/api/resource/ResourceUtilTest.java
Date Fri, 17 Jul 2009 08:33:26 GMT
Author: cziegeler
Date: Fri Jul 17 08:33:26 2009
New Revision: 795007

URL: http://svn.apache.org/viewvc?rev=795007&view=rev
Log:
Add helper method to check if the resource is of a specific resource type (which checks the
resource type hierarchy)

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=795007&r1=795006&r2=795007&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
Fri Jul 17 08:33:26 2009
@@ -373,4 +373,28 @@
         }
         return resourceSuperType;
     }
+
+    /**
+     * Check if the resource is of the given type.
+     * This method first checks the resource type of the resource, then
+     * its super resource type and continues to go up the resource super
+     * type hierarchy.
+     * @since 2.0.6
+     */
+    public static boolean isA(final Resource resource, String resourceType) {
+        if ( resource == null || resourceType == null ) {
+            return false;
+        }
+        if ( resourceType.equals(resource.getResourceType()) ) {
+            return true;
+        }
+        String superType = findResourceSuperType(resource);
+        while ( superType != null ) {
+            if ( resourceType.equals(superType) ) {
+                return true;
+            }
+            superType = getResourceSuperType(resource.getResourceResolver(), 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=795007&r1=795006&r2=795007&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
Fri Jul 17 08:33:26 2009
@@ -19,8 +19,10 @@
 package org.apache.sling.api.resource;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 
 import java.util.HashMap;
@@ -275,4 +277,36 @@
         assertEquals("t:c", ResourceUtil.getResourceSuperType(r.getResourceResolver(), r.getResourceType()));
         assertNull(ResourceUtil.getResourceSuperType(r2.getResourceResolver(), r2.getResourceType()));
     }
+
+    @Test public void test_isA() {
+        // the resource resolver
+        final ResourceResolver resolver = this.context.mock(ResourceResolver.class);
+        // the resource to test
+        final Resource r = this.context.mock(Resource.class);
+        final Resource typeResource = this.context.mock(Resource.class);
+        this.context.checking(new Expectations() {{
+            allowing(r).getResourceType(); will(returnValue("a:b"));
+            allowing(r).getResourceSuperType(); will(returnValue("d:e"));
+            allowing(r).getResourceResolver(); will(returnValue(resolver));
+
+            allowing(typeResource).getResourceType();
+            will(returnValue("x:y"));
+            allowing(typeResource).getResourceSuperType();
+            will(returnValue("t:c"));
+
+            allowing(resolver).getResource("/a");
+            will(returnValue(r));
+            allowing(resolver).getResource("a/b");
+            will(returnValue(null));
+            allowing(resolver).getResource("t/c");
+            will(returnValue(null));
+            allowing(resolver).getResource("d/e");
+            will(returnValue(typeResource));
+        }});
+        assertTrue(ResourceUtil.isA(r, "a:b"));
+        assertTrue(ResourceUtil.isA(r, "d:e"));
+        assertFalse(ResourceUtil.isA(r, "x:y"));
+        assertTrue(ResourceUtil.isA(r, "t:c"));
+        assertFalse(ResourceUtil.isA(r, "h:p"));
+    }
 }



Mime
View raw message