You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by bd...@apache.org on 2008/02/08 18:38:55 UTC

svn commit: r619952 - in /incubator/sling/trunk: launchpad/launchpad-webapp/src/test/java/org/apache/sling/launchpad/webapp/integrationtest/ launchpad/launchpad-webapp/src/test/resources/integration-test/ scripting/javascript/src/main/java/org/apache/s...

Author: bdelacretaz
Date: Fri Feb  8 09:38:51 2008
New Revision: 619952

URL: http://svn.apache.org/viewvc?rev=619952&view=rev
Log:
SLING-240 - Improve iteration of child Resources in ScriptableResource, work in progress

Added:
    incubator/sling/trunk/launchpad/launchpad-webapp/src/test/java/org/apache/sling/launchpad/webapp/integrationtest/JavascriptWrappersTest.java   (with props)
    incubator/sling/trunk/launchpad/launchpad-webapp/src/test/resources/integration-test/dump-resource.js   (with props)
Modified:
    incubator/sling/trunk/launchpad/launchpad-webapp/src/test/java/org/apache/sling/launchpad/webapp/integrationtest/HttpTestBase.java
    incubator/sling/trunk/scripting/javascript/src/main/java/org/apache/sling/scripting/javascript/wrapper/ScriptableResource.java

Modified: incubator/sling/trunk/launchpad/launchpad-webapp/src/test/java/org/apache/sling/launchpad/webapp/integrationtest/HttpTestBase.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/launchpad/launchpad-webapp/src/test/java/org/apache/sling/launchpad/webapp/integrationtest/HttpTestBase.java?rev=619952&r1=619951&r2=619952&view=diff
==============================================================================
--- incubator/sling/trunk/launchpad/launchpad-webapp/src/test/java/org/apache/sling/launchpad/webapp/integrationtest/HttpTestBase.java (original)
+++ incubator/sling/trunk/launchpad/launchpad-webapp/src/test/java/org/apache/sling/launchpad/webapp/integrationtest/HttpTestBase.java Fri Feb  8 09:38:51 2008
@@ -90,6 +90,10 @@
             scriptPath = "/apps/" + (resourceType == null ? "nt/unstructured" : resourceType);
             testClient.mkdirs(WEBDAV_BASE_URL, scriptPath);
         }
+        
+        void delete() throws IOException {
+            testClient.delete(nodeUrl);
+        }
     };
 
     @Override

Added: incubator/sling/trunk/launchpad/launchpad-webapp/src/test/java/org/apache/sling/launchpad/webapp/integrationtest/JavascriptWrappersTest.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/launchpad/launchpad-webapp/src/test/java/org/apache/sling/launchpad/webapp/integrationtest/JavascriptWrappersTest.java?rev=619952&view=auto
==============================================================================
--- incubator/sling/trunk/launchpad/launchpad-webapp/src/test/java/org/apache/sling/launchpad/webapp/integrationtest/JavascriptWrappersTest.java (added)
+++ incubator/sling/trunk/launchpad/launchpad-webapp/src/test/java/org/apache/sling/launchpad/webapp/integrationtest/JavascriptWrappersTest.java Fri Feb  8 09:38:51 2008
@@ -0,0 +1,74 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sling.launchpad.webapp.integrationtest;
+
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+
+/** Test Scriptable objects */
+public class JavascriptWrappersTest extends HttpTestBase {
+    private TestNode testRootNode;
+    private String scriptPath;
+    private String basePath;
+    
+    private void createNodes(TestNode n, String prefix, int levels) throws Exception {
+        String url = n.nodeUrl;
+        while(levels >= 1) {
+            url += "/" + prefix + levels; 
+            testClient.createNode(url, null);
+            levels--;
+        }
+    }
+    
+    @Override
+    protected void setUp() throws Exception {
+        super.setUp();
+        
+        basePath = "/" + getClass().getSimpleName() + "_" + System.currentTimeMillis();
+        
+        final Map<String, String> props = new HashMap<String, String>();
+        props.put(SLING_RESOURCE_TYPE, getClass().getSimpleName());
+        props.put("title", "testnode");
+        
+        testRootNode = new TestNode(HTTP_BASE_URL + basePath, props);
+        createNodes(testRootNode, "a", 3);
+        createNodes(testRootNode, "b", 1);
+        createNodes(testRootNode, "c", 2);
+    }
+    
+    public void testRecursiveDump() throws IOException {
+        final String toDelete = uploadTestScript(testRootNode.scriptPath, "dump-resource.js", "html.js");
+        try {
+            final String content = getContent(testRootNode.nodeUrl + ".html", CONTENT_TYPE_HTML);
+            
+            final String expected = 
+                "1 " + basePath + "/testnode\n"
+                + "2 " + basePath + "/testnode/a3\n"
+                + "3 " + basePath + "/testnode/a3/a2\n"
+                + "4 " + basePath + "/testnode/a3/a2/a1\n"
+                + "2 " + basePath + "/testnode/b1\n"
+                + "2 " + basePath + "/testnode/c2\n"
+                + "3 " + basePath + "/testnode/c2/c1\n"
+                ;
+            assertEquals(expected, content);
+        } finally {
+            testClient.delete(toDelete);
+        }
+        
+    }
+}

Propchange: incubator/sling/trunk/launchpad/launchpad-webapp/src/test/java/org/apache/sling/launchpad/webapp/integrationtest/JavascriptWrappersTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/sling/trunk/launchpad/launchpad-webapp/src/test/java/org/apache/sling/launchpad/webapp/integrationtest/JavascriptWrappersTest.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev URL

Added: incubator/sling/trunk/launchpad/launchpad-webapp/src/test/resources/integration-test/dump-resource.js
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/launchpad/launchpad-webapp/src/test/resources/integration-test/dump-resource.js?rev=619952&view=auto
==============================================================================
--- incubator/sling/trunk/launchpad/launchpad-webapp/src/test/resources/integration-test/dump-resource.js (added)
+++ incubator/sling/trunk/launchpad/launchpad-webapp/src/test/resources/integration-test/dump-resource.js Fri Feb  8 09:38:51 2008
@@ -0,0 +1,14 @@
+// recursive dump of a Resource in server-side javascript
+
+function dumpResource(r, level) {
+	out.print(level + " " + r + '\n');
+	
+	// TODO for now, "children" returns a Java
+	// iterator, need a javascript wrapper
+	var iterator = r.children;
+	while(iterator.hasNext()) {
+		dumpResource(iterator.next(), level + 1);
+	}
+}
+
+dumpResource(resource, 1);

Propchange: incubator/sling/trunk/launchpad/launchpad-webapp/src/test/resources/integration-test/dump-resource.js
------------------------------------------------------------------------------
    svn:eol-style = native

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=619952&r1=619951&r2=619952&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 Fri Feb  8 09:38:51 2008
@@ -16,6 +16,8 @@
  */
 package org.apache.sling.scripting.javascript.wrapper;
 
+import java.util.Iterator;
+
 import javax.jcr.Node;
 
 import org.apache.sling.api.resource.Resource;
@@ -98,6 +100,14 @@
 
     public Object jsGet_meta() {
         return resource.getResourceMetadata();
+    }
+    
+    // TODO a wrapper would be more convenient than an Iterator,
+    // but in my tests ScriptableItemMap didn't seem to allow
+    // proper wrapping of its elements: javascript constructor
+    // not found when scope = ScriptableItemMap
+    public Iterator<Resource> jsGet_children() {
+        return resource.getResourceProvider().listChildren(resource);
     }
 
     @Override