You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by cz...@apache.org on 2009/06/24 13:42:27 UTC
svn commit: r787983 - in /sling/trunk/bundles/api: ./
src/main/java/org/apache/sling/api/
src/main/java/org/apache/sling/api/resource/
src/main/java/org/apache/sling/api/scripting/
src/test/java/org/apache/sling/api/resource/
Author: cziegeler
Date: Wed Jun 24 11:42:27 2009
New Revision: 787983
URL: http://svn.apache.org/viewvc?rev=787983&view=rev
Log:
SLING-1020 : Add new implementations of getResourceSuperType, resourceTypeToPath from JcrResourceUtil to ResourceUtil and add test cases.
Modified:
sling/trunk/bundles/api/pom.xml
sling/trunk/bundles/api/src/main/java/org/apache/sling/api/SlingConstants.java
sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/ResourceUtil.java
sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/ValueMap.java
sling/trunk/bundles/api/src/main/java/org/apache/sling/api/scripting/SlingScriptHelper.java
sling/trunk/bundles/api/src/test/java/org/apache/sling/api/resource/ResourceUtilTest.java
Modified: sling/trunk/bundles/api/pom.xml
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/api/pom.xml?rev=787983&r1=787982&r2=787983&view=diff
==============================================================================
--- sling/trunk/bundles/api/pom.xml (original)
+++ sling/trunk/bundles/api/pom.xml Wed Jun 24 11:42:27 2009
@@ -59,11 +59,15 @@
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
-
+ <!-- Testing -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
+ <dependency>
+ <groupId>org.jmock</groupId>
+ <artifactId>jmock-junit4</artifactId>
+ </dependency>
</dependencies>
<build>
Modified: sling/trunk/bundles/api/src/main/java/org/apache/sling/api/SlingConstants.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/api/src/main/java/org/apache/sling/api/SlingConstants.java?rev=787983&r1=787982&r2=787983&view=diff
==============================================================================
--- sling/trunk/bundles/api/src/main/java/org/apache/sling/api/SlingConstants.java (original)
+++ sling/trunk/bundles/api/src/main/java/org/apache/sling/api/SlingConstants.java Wed Jun 24 11:42:27 2009
@@ -145,5 +145,4 @@
* The type of the attribute value is <code>java.lang.Integer</code>.
*/
public static final String ERROR_STATUS = "javax.servlet.error.status_code";
-
}
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=787983&r1=787982&r2=787983&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 Jun 24 11:42:27 2009
@@ -291,21 +291,81 @@
// adapt to ValueMap if resource is not null
ValueMap valueMap = (res != null)?
res.adaptTo(ValueMap.class) : null;
-
+
// if no resource or no ValueMap adapter, check Map
if (valueMap == null) {
-
+
Map map = (res != null) ? res.adaptTo(Map.class) : null;
// if not even adapting to map, assume an empty map
if (map == null) {
map = new HashMap<String, Object>();
}
-
+
// .. and decorate the plain map
valueMap = new ValueMapDecorator(map);
}
-
+
return valueMap;
}
+ /**
+ * Helper method, which returns the given resource type as returned from the
+ * {@link org.apache.sling.api.resource.Resource#getResourceType()} as a
+ * relative path.
+ *
+ * @param type The resource type to be converted into a path
+ * @return The resource type as a path.
+ * @since 2.0.6
+ */
+ public static String resourceTypeToPath(final String type) {
+ return type.replaceAll("\\:", "/");
+ }
+
+ /**
+ * Returns the super type of the given resource type.
+ * This method converts the resource type to a resource path
+ * by calling {@link #resourceTypeToPath(String)} and uses
+ * the <code>resourceResolver</code> to get the corresponding
+ * resource. If the resource exists, the {@link Resource#getResourceSuperType()}
+ * metod is called.
+ *
+ * @param resourceResolver The <code>ResourceResolver</code> used to
+ * access the resource whose path (relative or absolute) is given
+ * by the <code>resourceType</code> parameter.
+ * @param resourceType The resource type whose super type is to be returned.
+ * This type is turned into a path by calling the
+ * {@link #resourceTypeToPath(String)} method before trying to
+ * get the resource through the <code>resourceResolver</code>.
+ * @return the super type of the <code>resourceType</code> or
+ * <code>null</code> if the resource type does not exists
+ * or returns <code>null</code> for its super type.
+ * @since 2.0.6
+ */
+ public static String getResourceSuperType(final ResourceResolver resourceResolver,
+ final String resourceType) {
+ // normalize resource type to a path string
+ final String rtPath = resourceTypeToPath(resourceType);
+ // get the resource type resource
+ final Resource rtResource = resourceResolver.getResource(rtPath);
+ return (rtResource == null ? null : rtResource.getResourceSuperType());
+ }
+
+ /**
+ * Returns the resource super type of the resource type of the given resource.
+ * This method simply calls {@link #getResourceSuperType(ResourceResolver, String)}
+ * with the resource type of the <code>resource</code>
+ *
+ * @param resource The <code>Resource</code> whose resource super type is
+ * requested.
+ * @return The resource super type or <code>null</code> if the algorithm
+ * described above does not yield a resource super type.
+ * @since 2.0.6
+ */
+ public static String getResourceSuperType(final Resource resource) {
+ if ( resource == null ) {
+ return null;
+ }
+ return getResourceSuperType(resource.getResourceResolver(),
+ resource.getResourceType());
+ }
}
Modified: sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/ValueMap.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/ValueMap.java?rev=787983&r1=787982&r2=787983&view=diff
==============================================================================
--- sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/ValueMap.java (original)
+++ sling/trunk/bundles/api/src/main/java/org/apache/sling/api/resource/ValueMap.java Wed Jun 24 11:42:27 2009
@@ -32,14 +32,14 @@
public interface ValueMap extends Map<String, Object> {
/**
- * Empty value map
+ * Empty immutable value map.
*/
final ValueMap EMPTY = new ValueMapDecorator(
Collections.<String, Object> emptyMap());
/**
* Get a named property and convert it into the given type.
- *
+ *
* @param name The name of the property
* @param type The class of the type
* @return Return named value converted to type T or <code>null</code> if
@@ -49,7 +49,7 @@
/**
* Get a named property and convert it into the given type.
- *
+ *
* @param name The name of the property
* @param defaultValue The default value to use if the named property does
* not exist or cannot be converted to the requested type. The
Modified: sling/trunk/bundles/api/src/main/java/org/apache/sling/api/scripting/SlingScriptHelper.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/api/src/main/java/org/apache/sling/api/scripting/SlingScriptHelper.java?rev=787983&r1=787982&r2=787983&view=diff
==============================================================================
--- sling/trunk/bundles/api/src/main/java/org/apache/sling/api/scripting/SlingScriptHelper.java (original)
+++ sling/trunk/bundles/api/src/main/java/org/apache/sling/api/scripting/SlingScriptHelper.java Wed Jun 24 11:42:27 2009
@@ -49,10 +49,10 @@
/**
* Same as {@link #include(String,RequestDispatcherOptions)}, but using
* empty options.
- *
- * @throws SlingIOException Wrapping a <code>IOException</code> thrown
+ *
+ * @throws org.apache.sling.api.SlingIOException Wrapping a <code>IOException</code> thrown
* while handling the include.
- * @throws SlingServletException Wrapping a <code>ServletException</code>
+ * @throws org.apache.sling.api.SlingServletException Wrapping a <code>ServletException</code>
* thrown while handling the include.
*/
void include(String path);
@@ -61,7 +61,7 @@
* Helper method to include the result of processing the request for the
* given <code>path</code> and <code>requestDispatcherOptions</code>.
* This method is intended to be implemented as follows:
- *
+ *
* <pre>
* RequestDispatcher dispatcher = getRequest().getRequestDispatcher(path,
* "option:xyz");
@@ -69,19 +69,19 @@
* dispatcher.include(getRequest(), getResponse());
* }
* </pre>
- *
+ *
* <p>
* This method creates a <code>RequestDispatcherOptions</code> object by
* calling the
* {@link RequestDispatcherOptions#RequestDispatcherOptions(String)}
* constructor.
- *
+ *
* @param path The path to the resource to include.
* @param requestDispatcherOptions influence the rendering of the included
* Resource
- * @throws SlingIOException Wrapping a <code>IOException</code> thrown
+ * @throws org.apache.sling.api.SlingIOException Wrapping a <code>IOException</code> thrown
* while handling the include.
- * @throws SlingServletException Wrapping a <code>ServletException</code>
+ * @throws org.apache.sling.api.SlingServletException Wrapping a <code>ServletException</code>
* thrown while handling the include.
* @see RequestDispatcherOptions#RequestDispatcherOptions(String)
* @see #include(String, RequestDispatcherOptions)
@@ -92,7 +92,7 @@
* Helper method to include the result of processing the request for the
* given <code>path</code> and <code>options</code>. This method is
* intended to be implemented as follows:
- *
+ *
* <pre>
* RequestDispatcherOptions opts = new RequestDispatcherOptions();
* opts.put("option", "xyz");
@@ -101,12 +101,12 @@
* dispatcher.include(getRequest(), getResponse());
* }
* </pre>
- *
+ *
* @param path The path to the resource to include.
* @param options influence the rendering of the included Resource
- * @throws SlingIOException Wrapping a <code>IOException</code> thrown
+ * @throws org.apache.sling.api.SlingIOException Wrapping a <code>IOException</code> thrown
* while handling the include.
- * @throws SlingServletException Wrapping a <code>ServletException</code>
+ * @throws org.apache.sling.api.SlingServletException Wrapping a <code>ServletException</code>
* thrown while handling the include.
* @see RequestDispatcherOptions
* @see #include(String, String)
@@ -116,10 +116,10 @@
/**
* Same as {@link #forward(String,RequestDispatcherOptions)}, but using
* empty options.
- *
- * @throws SlingIOException Wrapping a <code>IOException</code> thrown
+ *
+ * @throws org.apache.sling.api.SlingIOException Wrapping a <code>IOException</code> thrown
* while handling the forward.
- * @throws SlingServletException Wrapping a <code>ServletException</code>
+ * @throws org.apache.sling.api.SlingServletException Wrapping a <code>ServletException</code>
* thrown while handling the forward.
*/
void forward(String path);
@@ -128,7 +128,7 @@
* Helper method to forward the request to a Servlet or script for the given
* <code>path</code> and <code>requestDispatcherOptions</code>. This method
* is intended to be implemented as follows:
- *
+ *
* <pre>
* RequestDispatcher dispatcher = getRequest().getRequestDispatcher(path,
* "option:xyz");
@@ -136,19 +136,19 @@
* dispatcher.forward(getRequest(), getResponse());
* }
* </pre>
- *
+ *
* <p>
* This method creates a <code>RequestDispatcherOptions</code> object by
* calling the
* {@link RequestDispatcherOptions#RequestDispatcherOptions(String)}
* constructor.
- *
+ *
* @param path The path to the resource to forward to.
* @param requestDispatcherOptions influence the rendering of the forwarded
* Resource
- * @throws SlingIOException Wrapping a <code>IOException</code> thrown
+ * @throws org.apache.sling.api.SlingIOException Wrapping a <code>IOException</code> thrown
* while handling the forward.
- * @throws SlingServletException Wrapping a <code>ServletException</code>
+ * @throws org.apache.sling.api.SlingServletException Wrapping a <code>ServletException</code>
* thrown while handling the forward.
* @see RequestDispatcherOptions#RequestDispatcherOptions(String)
* @see #forward(String, RequestDispatcherOptions)
@@ -159,7 +159,7 @@
* Helper method to forward the request to a Servlet or script for the given
* <code>path</code> and <code>options</code>. This method is intended
* to be implemented as follows:
- *
+ *
* <pre>
* RequestDispatcherOptions opts = new RequestDispatcherOptions();
* opts.put("option", "xyz");
@@ -168,12 +168,12 @@
* dispatcher.forward(getRequest(), getResponse());
* }
* </pre>
- *
+ *
* @param path The path to the resource to forward the request to.
* @param options influence the rendering of the forwarded Resource
- * @throws SlingIOException Wrapping a <code>IOException</code> thrown
+ * @throws org.apache.sling.api.SlingIOException Wrapping a <code>IOException</code> thrown
* while handling the forward.
- * @throws SlingServletException Wrapping a <code>ServletException</code>
+ * @throws org.apache.sling.api.SlingServletException Wrapping a <code>ServletException</code>
* thrown while handling the forward.
* @throws IllegalStateException If the respoonse has already been committed
* @see RequestDispatcherOptions
@@ -182,7 +182,7 @@
/**
* Lookup a single service
- *
+ *
* @param serviceType The type (interface) of the service.
* @return The service instance, or null if the service is not available.
*/
@@ -190,7 +190,7 @@
/**
* Lookup one or several services
- *
+ *
* @param serviceType The type (interface) of the service.
* @param filter An optional filter (LDAP-like, see OSGi spec)
* @return The services object or null.
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=787983&r1=787982&r2=787983&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 Jun 24 11:42:27 2009
@@ -18,16 +18,28 @@
*/
package org.apache.sling.api.resource;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.fail;
+
import java.util.HashMap;
import java.util.Map;
-import junit.framework.TestCase;
-
import org.apache.sling.api.wrappers.ValueMapDecorator;
+import org.jmock.Expectations;
+import org.jmock.Mockery;
+import org.jmock.integration.junit4.JMock;
+import org.jmock.integration.junit4.JUnit4Mockery;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+@RunWith(JMock.class)
+public class ResourceUtilTest {
-public class ResourceUtilTest extends TestCase {
+ protected final Mockery context = new JUnit4Mockery();
- public void testResolveRelativeSegments() {
+ @Test public void testResolveRelativeSegments() {
assertEquals("/", ResourceUtil.normalize("/"));
assertEquals("/", ResourceUtil.normalize("///"));
@@ -107,7 +119,7 @@
}
}
- public void testResolveRelativeSegmentsRelative() {
+ @Test public void testResolveRelativeSegmentsRelative() {
assertEquals("a/b", ResourceUtil.normalize("a/b"));
assertEquals("a", ResourceUtil.normalize("a/b/.."));
@@ -123,7 +135,7 @@
assertEquals("", ResourceUtil.normalize(""));
}
- public void testGetParent() {
+ @Test public void testGetParent() {
assertNull(ResourceUtil.getParent("/"));
assertNull(ResourceUtil.getParent("/.."));
@@ -135,7 +147,7 @@
assertNull(ResourceUtil.getParent("/b/.."));
}
- public void testGetName() {
+ @Test public void testGetName() {
assertEquals("", ResourceUtil.getName("/"));
assertEquals("", ResourceUtil.getName("/a/.."));
@@ -152,22 +164,22 @@
assertEquals("b", ResourceUtil.getName("/b/c/.."));
assertEquals("", ResourceUtil.getName("/b/c/../.."));
}
-
- public void test_getValueMap_null_resource() {
+
+ @Test public void test_getValueMap_null_resource() {
final ValueMap valueMap = ResourceUtil.getValueMap(null);
assertNotNull(valueMap);
assertEquals(0, valueMap.size());
-
+
final Object replaced = valueMap.put("sample", 1);
assertNull(replaced);
-
+
assertEquals(1, valueMap.size());
assertEquals(1, valueMap.get("sample"));
assertEquals(Integer.valueOf(1), valueMap.get("sample", Integer.class));
assertEquals("1", valueMap.get("sample", String.class));
}
-
- public void test_getValueMap_direct() {
+
+ @Test public void test_getValueMap_direct() {
final ValueMap valueMap = new ValueMapDecorator(new HashMap<String, Object>());
valueMap.put("sample", true);
final Resource resource = new SyntheticResource(null, "/", "sample") {
@@ -177,11 +189,11 @@
if (type == ValueMap.class) {
return (Type) valueMap;
}
-
+
return super.adaptTo(type);
}
};
-
+
final ValueMap adapted = ResourceUtil.getValueMap(resource);
assertEquals(valueMap, adapted);
assertNotNull(adapted);
@@ -191,8 +203,8 @@
assertEquals(Boolean.valueOf(true), adapted.get("sample", Boolean.class));
assertEquals(Boolean.TRUE.toString(), adapted.get("sample", String.class));
}
-
- public void test_getValueMap_decorated_map() {
+
+ @Test public void test_getValueMap_decorated_map() {
final Map<String, Object> map = new HashMap<String, Object>();
map.put("sample", true);
final Resource resource = new SyntheticResource(null, "/", "sample") {
@@ -202,11 +214,11 @@
if (type == Map.class) {
return (Type) map;
}
-
+
return super.adaptTo(type);
}
};
-
+
final ValueMap adapted = ResourceUtil.getValueMap(resource);
assertNotNull(adapted);
assertEquals(1, adapted.size());
@@ -216,17 +228,50 @@
assertEquals(Boolean.TRUE.toString(), adapted.get("sample", String.class));
}
- public void test_getValueMap_no_adapter() {
+ @Test public void test_getValueMap_no_adapter() {
final ValueMap valueMap = ResourceUtil.getValueMap(null);
assertNotNull(valueMap);
assertEquals(0, valueMap.size());
-
+
final Object replaced = valueMap.put("sample", 1);
assertNull(replaced);
-
+
assertEquals(1, valueMap.size());
assertEquals(1, valueMap.get("sample"));
assertEquals(Integer.valueOf(1), valueMap.get("sample", Integer.class));
assertEquals("1", valueMap.get("sample", String.class));
}
+
+ @Test public void test_resourceTypeToPath() {
+ assertEquals("a/b", ResourceUtil.resourceTypeToPath("a:b"));
+ }
+
+ @Test public void test_getResourceSuperType() {
+ // 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 r2 = 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).getResourceResolver(); will(returnValue(resolver));
+
+ allowing(r2).getResourceType(); will(returnValue("a:c"));
+ allowing(r2).getResourceResolver(); will(returnValue(resolver));
+
+ allowing(typeResource).getResourceSuperType();
+ will(returnValue("t:c"));
+
+ allowing(resolver).getResource("/a");
+ will(returnValue(r));
+ allowing(resolver).getResource("a/b");
+ will(returnValue(typeResource));
+ allowing(resolver).getResource("a/c");
+ will(returnValue(null));
+ }});
+ assertEquals("t:c", ResourceUtil.getResourceSuperType(r));
+ assertNull(ResourceUtil.getResourceSuperType(null));
+ assertNull(ResourceUtil.getResourceSuperType(r2));
+ }
}