You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by fm...@apache.org on 2008/11/09 17:49:54 UTC

svn commit: r712512 - in /incubator/sling/trunk/scripting/javascript/src: main/java/org/apache/sling/scripting/javascript/wrapper/ScriptableResource.java test/java/org/apache/sling/scripting/wrapper/ScriptableResourceTest.java

Author: fmeschbe
Date: Sun Nov  9 08:49:54 2008
New Revision: 712512

URL: http://svn.apache.org/viewvc?rev=712512&view=rev
Log:
SLING-724 Implemented missing method mappings, added JavaDoc to mapped methods
and deprecated wrong mappings. Added unit tests for the mappings to ensure they
return values as expected.
Finally removed the "jsGet_javascriptWrapperClass" method, which was just
provided for testing and has no place in the wrapped class.

Modified:
    incubator/sling/trunk/scripting/javascript/src/main/java/org/apache/sling/scripting/javascript/wrapper/ScriptableResource.java
    incubator/sling/trunk/scripting/javascript/src/test/java/org/apache/sling/scripting/wrapper/ScriptableResourceTest.java

Modified: incubator/sling/trunk/scripting/javascript/src/main/java/org/apache/sling/scripting/javascript/wrapper/ScriptableResource.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/scripting/javascript/src/main/java/org/apache/sling/scripting/javascript/wrapper/ScriptableResource.java?rev=712512&r1=712511&r2=712512&view=diff
==============================================================================
--- incubator/sling/trunk/scripting/javascript/src/main/java/org/apache/sling/scripting/javascript/wrapper/ScriptableResource.java (original)
+++ incubator/sling/trunk/scripting/javascript/src/main/java/org/apache/sling/scripting/javascript/wrapper/ScriptableResource.java Sun Nov  9 08:49:54 2008
@@ -31,10 +31,12 @@
  * data [Item] getItem(); [Item] item [String] getResourceType(); [String] type
  * [String] getPath(); [String] path
  */
-public class ScriptableResource extends ScriptableObject implements SlingWrapper {
+public class ScriptableResource extends ScriptableObject implements
+        SlingWrapper {
 
     public static final String CLASSNAME = "Resource";
-    public static final Class<?> [] WRAPPED_CLASSES = { Resource.class };
+
+    public static final Class<?>[] WRAPPED_CLASSES = { Resource.class };
 
     private Resource resource;
 
@@ -49,51 +51,143 @@
         this.resource = (Resource) res;
     }
 
-    public Class<?> [] getWrappedClasses() {
-        return WRAPPED_CLASSES;
-    }
-
-    @Override
-    public String getClassName() {
-        return CLASSNAME;
+    /**
+     * Mapps getPath() method as path property.
+     */
+    public String jsGet_path() {
+        return this.jsFunction_getPath();
     }
 
-    public Object jsFunction_getObject() {
-        return toJS(resource.adaptTo(Object.class));
+    /**
+     * Mapps getPath() method as getPath() method.
+     */
+    public String jsFunction_getPath() {
+        return resource.getPath();
     }
 
-    public String jsFunction_getResourceType() {
-        return resource.getResourceType();
+    /**
+     * Maps getResourceType() to type property. This property is deprecated
+     * since it does not correctly map the getResourceType() method name to a
+     * property.
+     * 
+     * @deprecated since 2.1.0 because it maps the method name incorrectly.
+     */
+    public String jsGet_type() {
+        return this.jsFunction_getResourceType();
     }
 
-    public String jsGet_type() {
+    /**
+     * Maps getResourceType() to resourceType property.
+     */
+    public String jsGet_resourceType() {
         return this.jsFunction_getResourceType();
     }
 
-    public String jsFunction_getPath() {
-        return resource.getPath();
+    /**
+     * Maps getResourceType() to the getResourceType() method.
+     */
+    public String jsFunction_getResourceType() {
+        return resource.getResourceType();
     }
 
-    public String jsGet_path() {
-        return this.jsFunction_getPath();
+    /**
+     * Maps getResourceSuperType() to resourceSuperType property.
+     */
+    public String jsGet_resourceSuperType() {
+        return this.jsFunction_getResourceSuperType();
+    }
+
+    /**
+     * Maps getResourceSuperType() to the getResourceSuperType() method.
+     */
+    public String jsFunction_getResourceSuperType() {
+        return resource.getResourceSuperType();
+    }
+
+    /**
+     * Maps getResourceMetadata() to meta property. This property is deprecated
+     * since it does not correctly map the getResourceType() method name to a
+     * property.
+     * 
+     * @deprecated since 2.1.0 because it maps the method name incorrectly.
+     */
+    public Object jsGet_meta() {
+        return jsFunction_getResourceMetadata();
     }
 
+    /**
+     * Maps getResourceMetadata() to resourceMetadata property.
+     */
+    public Object jsGet_resourceMetadata() {
+        return jsFunction_getResourceMetadata();
+    }
+
+    /**
+     * Maps getResourceMetadata() to getMetadata() method. This method is
+     * deprecated since it has the wrong name to support the
+     * getResourceMetadata() method.
+     * 
+     * @deprecated since 2.1.0 because the method is named incorrectly.
+     */
     public Object jsFunction_getMetadata() {
-        return toJS(resource.getResourceMetadata());
+        return jsFunction_getResourceMetadata();
     }
 
-    public Object jsGet_meta() {
-        return jsFunction_getMetadata();
+    /**
+     * Maps getResourceMetadata() to getResourceMetadata method.
+     */
+    public Object jsFunction_getResourceMetadata() {
+        return toJS(resource.getResourceMetadata());
     }
 
+    /**
+     * Maps getResourceResolver() to resourceResolver property.
+     */
     public Object jsFunction_getResourceResolver() {
         return toJS(resource.getResourceResolver());
     }
 
+    /**
+     * Maps getResourceResolver() to getResourceResolver method.
+     */
     public Object jsGet_resourceResolver() {
         return jsFunction_getResourceResolver();
     }
 
+    /**
+     * Helper method to easily retrieve the default adapted object of the
+     * resource. In case of Object Content Mapping support, this method will
+     * return the correctly mapped content object for this resource.
+     * <p>
+     * Calling this method is equivalent to calling the adaptTo method with the
+     * argument "java.lang.Object".
+     */
+    public Object jsFunction_getObject() {
+        return toJS(resource.adaptTo(Object.class));
+    }
+
+    /**
+     * Implements the adaptTo() method for JavaScript scripts. This method takes
+     * either a java.lang.Class object or a String containing the fully
+     * qualified name of the class to adapt to.
+     * <p>
+     * Supporting String as an argument to this method allows for much easier
+     * use in JavaScript since instead of for example writing
+     * <i>"javax.jcr.Node"</i> instead of the much clumsier
+     * <i>Packages.javax.jcr.Node</i>.
+     * 
+     * @param cx The current Rhino context
+     * @param thisObj The ScriptableResource object in which the method is
+     *            called.
+     * @param args The argument vector. Only the first argument is used which is
+     *            expected to be a Class object or a String. If no argument is
+     *            supplied or it has the wrong type, this method just returns
+     *            <code>null</code>.
+     * @param funObj The object representing the JavaScript adaptTo function.
+     * @return The object to which the resource adapts or <code>null</code> if
+     *         the resource does not adapt to the required type or if the
+     *         argument is of the wrong type or missing.
+     */
     public static Object jsFunction_adaptTo(Context cx, Scriptable thisObj,
             Object[] args, Function funObj) {
 
@@ -133,10 +227,13 @@
         return Undefined.instance;
     }
 
-    public Class<?> jsGet_javascriptWrapperClass() {
-        return getClass();
+    // --------- ScriptableObject API
+
+    @Override
+    public String getClassName() {
+        return CLASSNAME;
     }
-    
+
     @SuppressWarnings("unchecked")
     @Override
     public Object getDefaultValue(Class typeHint) {
@@ -147,6 +244,12 @@
         this.resource = entry;
     }
 
+    // ---------- SlingWrapper API
+
+    public Class<?>[] getWrappedClasses() {
+        return WRAPPED_CLASSES;
+    }
+
     // ---------- Wrapper interface --------------------------------------------
 
     // returns the wrapped resource
@@ -154,7 +257,7 @@
         return resource;
     }
 
-    //---------- Internal helper ----------------------------------------------
+    // ---------- Internal helper ----------------------------------------------
 
     private Object toJS(Object javaObject) {
         if (javaObject == null) {

Modified: incubator/sling/trunk/scripting/javascript/src/test/java/org/apache/sling/scripting/wrapper/ScriptableResourceTest.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/scripting/javascript/src/test/java/org/apache/sling/scripting/wrapper/ScriptableResourceTest.java?rev=712512&r1=712511&r2=712512&view=diff
==============================================================================
--- incubator/sling/trunk/scripting/javascript/src/test/java/org/apache/sling/scripting/wrapper/ScriptableResourceTest.java (original)
+++ incubator/sling/trunk/scripting/javascript/src/test/java/org/apache/sling/scripting/wrapper/ScriptableResourceTest.java Sun Nov  9 08:49:54 2008
@@ -19,12 +19,16 @@
 package org.apache.sling.scripting.wrapper;
 
 import javax.jcr.Item;
+import javax.jcr.NamespaceException;
 import javax.jcr.Node;
 import javax.jcr.RepositoryException;
 
+import org.apache.sling.api.SlingConstants;
 import org.apache.sling.api.resource.Resource;
 import org.apache.sling.api.resource.ResourceMetadata;
 import org.apache.sling.api.resource.ResourceResolver;
+import org.apache.sling.commons.testing.sling.MockResourceResolver;
+import org.apache.sling.jcr.resource.JcrResourceConstants;
 import org.apache.sling.scripting.RepositoryScriptingTestBase;
 import org.apache.sling.scripting.javascript.internal.ScriptEngineHelper;
 import org.mozilla.javascript.Undefined;
@@ -34,11 +38,25 @@
 
     private Node node;
 
+    private static final ResourceResolver RESOURCE_RESOLVER = new MockResourceResolver();
+
+    private static final String RESOURCE_TYPE = "testWrappedResourceType";
+
+    private static final String RESOURCE_SUPER_TYPE = "testWrappedResourceSuperType";
+
     @Override
     protected void setUp() throws Exception {
         super.setUp();
 
         node = getNewNode();
+
+        try {
+            node.getSession().getWorkspace().getNamespaceRegistry().registerNamespace(
+                SlingConstants.NAMESPACE_PREFIX,
+                JcrResourceConstants.SLING_NAMESPACE_URI);
+        } catch (NamespaceException ne) {
+            // don't care, might happen if already registered
+        }
     }
 
     public void testDefaultValuePath() throws Exception {
@@ -52,6 +70,67 @@
         assertEquals(node.getPath(), script.eval("resource.getPath()", data));
     }
 
+    public void testResourceType() throws Exception {
+        // set resource and resource super type
+        node.setProperty(JcrResourceConstants.SLING_RESOURCE_TYPE_PROPERTY,
+            RESOURCE_TYPE);
+        node.setProperty(
+            JcrResourceConstants.SLING_RESOURCE_SUPER_TYPE_PROPERTY,
+            RESOURCE_SUPER_TYPE);
+
+        final ScriptEngineHelper.Data data = new ScriptEngineHelper.Data();
+        data.put("resource", new TestResource(node));
+
+        // the resourceType of the resource
+        assertEquals(RESOURCE_TYPE, script.eval("resource.type", data));
+        assertEquals(RESOURCE_TYPE, script.eval("resource.resourceType", data));
+        assertEquals(RESOURCE_TYPE, script.eval("resource.getResourceType()",
+            data));
+    }
+
+    public void testResourceSuperType() throws Exception {
+        // set resource and resource super type
+        node.setProperty(JcrResourceConstants.SLING_RESOURCE_TYPE_PROPERTY,
+            RESOURCE_TYPE);
+        node.setProperty(
+            JcrResourceConstants.SLING_RESOURCE_SUPER_TYPE_PROPERTY,
+            RESOURCE_SUPER_TYPE);
+
+        final ScriptEngineHelper.Data data = new ScriptEngineHelper.Data();
+        data.put("resource", new TestResource(node));
+
+        // the resourceType of the resource
+        assertEquals(RESOURCE_SUPER_TYPE, script.eval(
+            "resource.resourceSuperType", data));
+        assertEquals(RESOURCE_SUPER_TYPE, script.eval(
+            "resource.getResourceSuperType()", data));
+    }
+
+    public void testResourceMetadata() throws Exception {
+        final ScriptEngineHelper.Data data = new ScriptEngineHelper.Data();
+        data.put("resource", new TestResource(node));
+
+        // official API
+        assertResourceMetaData(script.eval("resource.resourceMetadata", data));
+        assertResourceMetaData(script.eval("resource.getResourceMetadata()",
+            data));
+
+        // deprecated mappings
+        assertResourceMetaData(script.eval("resource.meta", data));
+        assertResourceMetaData(script.eval("resource.getMetadata()", data));
+    }
+
+    public void testResourceResolver() throws Exception {
+        final ScriptEngineHelper.Data data = new ScriptEngineHelper.Data();
+        data.put("resource", new TestResource(node));
+
+        // official API
+        assertEquals(RESOURCE_RESOLVER, script.eval(
+            "resource.resourceResolver", data));
+        assertEquals(RESOURCE_RESOLVER, script.eval(
+            "resource.getResourceResolver()", data));
+    }
+
     public void testAdaptToNode() throws Exception {
         final ScriptEngineHelper.Data data = new ScriptEngineHelper.Data();
         data.put("resource", new TestResource(node));
@@ -77,16 +156,6 @@
         assertEquals(Undefined.instance, script.eval(
             "resource.adaptTo(Packages.java.util.Date)", data));
     }
-    
-    public void testResourceWrapperClass() throws Exception {
-        final ScriptEngineHelper.Data data = new ScriptEngineHelper.Data();
-        data.put("resource", new TestResource(node));
-        
-        assertEquals(
-                "org.apache.sling.scripting.javascript.wrapper.ScriptableResource", 
-                script.eval("resource.javascriptWrapperClass.getName()", data)
-        );
-    }
 
     private void assertEquals(Node expected, Object actual) {
         while (actual instanceof Wrapper) {
@@ -96,20 +165,26 @@
         super.assertEquals(expected, actual);
     }
 
+    private void assertResourceMetaData(Object metaData) throws Exception {
+        if (metaData instanceof ResourceMetadata) {
+            ResourceMetadata rm = (ResourceMetadata) metaData;
+            rm.getResolutionPath().equals(node.getPath());
+        } else {
+            fail("Expected ResourceMetadata, got " + metaData);
+        }
+    }
+
     private static class TestResource implements Resource {
 
         private final Node node;
 
         private final String path;
 
-        private final String resourceType;
-
         private final ResourceMetadata metadata;
 
         TestResource(Node node) throws RepositoryException {
             this.node = node;
             this.path = node.getPath();
-            this.resourceType = node.getPrimaryNodeType().getName();
             this.metadata = new ResourceMetadata();
             this.metadata.setResolutionPath(this.path);
         }
@@ -123,16 +198,15 @@
         }
 
         public ResourceResolver getResourceResolver() {
-            // none, don't care
-            return null;
+            return RESOURCE_RESOLVER;
         }
 
         public String getResourceType() {
-            return resourceType;
+            return RESOURCE_TYPE;
         }
 
         public String getResourceSuperType() {
-            return null;
+            return RESOURCE_SUPER_TYPE;
         }
 
         @SuppressWarnings("unchecked")