incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bdelacre...@apache.org
Subject svn commit: r1075851 - in /sling/trunk/testing: samples/integration-tests/src/test/java/org/apache/sling/testing/samples/testtools/ samples/integration-tests/src/test/java/org/apache/sling/testing/samples/testtools/serverside/scriptable/ tools/src/main...
Date Tue, 01 Mar 2011 15:47:45 GMT
Author: bdelacretaz
Date: Tue Mar  1 15:47:45 2011
New Revision: 1075851

URL: http://svn.apache.org/viewvc?rev=1075851&view=rev
Log:
SLING-1963 - test server-side scriptable tests

Added:
    sling/trunk/testing/samples/integration-tests/src/test/java/org/apache/sling/testing/samples/testtools/serverside/scriptable/
    sling/trunk/testing/samples/integration-tests/src/test/java/org/apache/sling/testing/samples/testtools/serverside/scriptable/ScriptableTestsTest.java
  (with props)
    sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools/sling/
    sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools/sling/SlingClient.java
  (with props)
Modified:
    sling/trunk/testing/samples/integration-tests/src/test/java/org/apache/sling/testing/samples/testtools/SlingTestBase.java
    sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools/osgi/WebconsoleClient.java

Modified: sling/trunk/testing/samples/integration-tests/src/test/java/org/apache/sling/testing/samples/testtools/SlingTestBase.java
URL: http://svn.apache.org/viewvc/sling/trunk/testing/samples/integration-tests/src/test/java/org/apache/sling/testing/samples/testtools/SlingTestBase.java?rev=1075851&r1=1075850&r2=1075851&view=diff
==============================================================================
--- sling/trunk/testing/samples/integration-tests/src/test/java/org/apache/sling/testing/samples/testtools/SlingTestBase.java
(original)
+++ sling/trunk/testing/samples/integration-tests/src/test/java/org/apache/sling/testing/samples/testtools/SlingTestBase.java
Tue Mar  1 15:47:45 2011
@@ -86,7 +86,7 @@ public class SlingTestBase {
         
         serverStarted = true;
         builder = new RequestBuilder(serverBaseUrl);
-        webconsoleClient = new WebconsoleClient(executor, builder, ADMIN, ADMIN);
+        webconsoleClient = new WebconsoleClient(serverBaseUrl, ADMIN, ADMIN);
     }
     
     /** Optionally block here so that the runnable jar stays up - we can 

Added: sling/trunk/testing/samples/integration-tests/src/test/java/org/apache/sling/testing/samples/testtools/serverside/scriptable/ScriptableTestsTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/testing/samples/integration-tests/src/test/java/org/apache/sling/testing/samples/testtools/serverside/scriptable/ScriptableTestsTest.java?rev=1075851&view=auto
==============================================================================
--- sling/trunk/testing/samples/integration-tests/src/test/java/org/apache/sling/testing/samples/testtools/serverside/scriptable/ScriptableTestsTest.java
(added)
+++ sling/trunk/testing/samples/integration-tests/src/test/java/org/apache/sling/testing/samples/testtools/serverside/scriptable/ScriptableTestsTest.java
Tue Mar  1 15:47:45 2011
@@ -0,0 +1,124 @@
+/*
+ * 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.testing.samples.testtools.serverside.scriptable;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.fail;
+
+import java.io.ByteArrayInputStream;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+
+import org.apache.sling.commons.json.JSONArray;
+import org.apache.sling.commons.json.JSONObject;
+import org.apache.sling.commons.json.JSONTokener;
+import org.apache.sling.junit.remote.httpclient.RemoteTestHttpClient;
+import org.apache.sling.testing.samples.testtools.serverside.ServerSideTestsBase;
+import org.apache.sling.testing.tools.http.RequestExecutor;
+import org.apache.sling.testing.tools.sling.SlingClient;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+import org.junit.runners.Parameterized.Parameters;
+
+/** Test server-side scriptable tests by creating test nodes and
+ *  scripts, for both success and failure cases */
+@RunWith(Parameterized.class)
+public class ScriptableTestsTest extends ServerSideTestsBase {
+    public static final String RESOURCE_TYPE = "testing/ScriptableTests";
+    public static final String TEST_NODES_BASE = "/apps/" + RESOURCE_TYPE;
+    public static final String TEST_NODE_PATH = TEST_NODES_BASE + "/testnode";
+    
+    private SlingClient slingClient;
+    private RemoteTestHttpClient testClient;
+    private final String testScript;
+    private final int failureCount;
+
+    public ScriptableTestsTest(String testScript, Integer failureCount) {
+        this.testScript = testScript;
+        this.failureCount = failureCount.intValue();
+    }
+    
+    @Before
+    public void setupContent() throws Exception {
+        slingClient = new SlingClient(serverBaseUrl, ADMIN, ADMIN);
+        testClient = new RemoteTestHttpClient(serverBaseUrl + JUNIT_SERVLET_PATH, true);
+        
+        cleanupContent();
+
+        // A test node has the sling:Test mixin and points
+        // to a test script
+        slingClient.createNode(TEST_NODE_PATH, 
+                "sling:resourceType", RESOURCE_TYPE,
+                "jcr:mixinTypes", "sling:Test");
+        
+        // Test script will be requested with .test.txt selector and extension
+        slingClient.upload(TEST_NODES_BASE + "/test.txt.esp", 
+                new ByteArrayInputStream(testScript.getBytes()), -1, true);
+    }
+    
+    @After
+    public void cleanupContent() throws Exception {
+        if(slingClient.exists(TEST_NODES_BASE)) {
+            slingClient.delete(TEST_NODES_BASE);
+        }
+    }
+    
+    @Test
+    public void testScriptableTest() throws Exception {
+        
+        // Execute all available scriptable tests and count failures 
+        final RequestExecutor executor = testClient.runTests(
+                "org.apache.sling.junit.scriptable.ScriptableTestsProvider",
+                null,
+                "json"
+                );
+        executor.assertContentType("application/json");
+        final JSONArray json = new JSONArray(new JSONTokener((executor.getContent())));
+        
+        int testsCount = 0;
+        final List<String> failures = new ArrayList<String>();
+        for(int i = 0 ; i < json.length(); i++) {
+            final JSONObject obj = json.getJSONObject(i);
+            if("test".equals(obj.getString("INFO_TYPE"))) {
+                testsCount++;
+                if(obj.has("failure")) {
+                    failures.add(obj.get("failure").toString());
+                }
+            }
+        }
+        
+        final int expectedTests = 1;
+        assertEquals("Expecting " + expectedTests + " scriptable tests", expectedTests, testsCount);
+        
+        if(failures.size() != failureCount) {
+            fail("Expected " + failureCount + " failing tests but got " + failures.size()
+ ": " + failures);
+        }
+    }
+    
+    /** Test both pass and fail cases */ 
+    @Parameters
+    public static Collection<Object[]> data() {
+        Collection<Object[]> data = new ArrayList<Object[]>();
+        data.add(new Object[] { "TEST_PASSED", 0});
+        data.add(new Object[] { "TEST_FAILED", 1});
+        return data;
+    }
+}
\ No newline at end of file

Propchange: sling/trunk/testing/samples/integration-tests/src/test/java/org/apache/sling/testing/samples/testtools/serverside/scriptable/ScriptableTestsTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sling/trunk/testing/samples/integration-tests/src/test/java/org/apache/sling/testing/samples/testtools/serverside/scriptable/ScriptableTestsTest.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev URL

Modified: sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools/osgi/WebconsoleClient.java
URL: http://svn.apache.org/viewvc/sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools/osgi/WebconsoleClient.java?rev=1075851&r1=1075850&r2=1075851&view=diff
==============================================================================
--- sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools/osgi/WebconsoleClient.java
(original)
+++ sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools/osgi/WebconsoleClient.java
Tue Mar  1 15:47:45 2011
@@ -21,6 +21,7 @@ import java.io.File;
 import org.apache.http.entity.mime.MultipartEntity;
 import org.apache.http.entity.mime.content.FileBody;
 import org.apache.http.entity.mime.content.StringBody;
+import org.apache.http.impl.client.DefaultHttpClient;
 import org.apache.sling.testing.tools.http.RequestBuilder;
 import org.apache.sling.testing.tools.http.RequestExecutor;
 import org.slf4j.Logger;
@@ -34,9 +35,9 @@ public class WebconsoleClient {
     private final String username;
     private final String password;
     
-    public WebconsoleClient(RequestExecutor executor, RequestBuilder builder, String username,
String password) {
-        this.executor = executor;
-        this.builder = builder;
+    public WebconsoleClient(String slingServerUrl, String username, String password) {
+        this.builder = new RequestBuilder(slingServerUrl);
+        this.executor = new RequestExecutor(new DefaultHttpClient());
         this.username = username;
         this.password = password;
     }

Added: sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools/sling/SlingClient.java
URL: http://svn.apache.org/viewvc/sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools/sling/SlingClient.java?rev=1075851&view=auto
==============================================================================
--- sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools/sling/SlingClient.java
(added)
+++ sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools/sling/SlingClient.java
Tue Mar  1 15:47:45 2011
@@ -0,0 +1,207 @@
+/*
+ * 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.testing.tools.sling;
+
+import static org.junit.Assert.assertNotNull;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.UnsupportedEncodingException;
+import java.net.URI;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Stack;
+
+import org.apache.http.Header;
+import org.apache.http.HttpEntity;
+import org.apache.http.HttpResponse;
+import org.apache.http.client.methods.HttpDelete;
+import org.apache.http.client.methods.HttpPut;
+import org.apache.http.client.methods.HttpRequestBase;
+import org.apache.http.entity.InputStreamEntity;
+import org.apache.http.entity.mime.MultipartEntity;
+import org.apache.http.entity.mime.content.StringBody;
+import org.apache.http.impl.client.DefaultHttpClient;
+import org.apache.sling.testing.tools.http.RequestBuilder;
+import org.apache.sling.testing.tools.http.RequestExecutor;
+
+/** Simple Sling client, created for integration
+ *  tests but should be general purpose */
+public class SlingClient {
+    public static final String LOCATION_HEADER = "Location";
+    private final RequestExecutor executor;
+    private final RequestBuilder builder;
+    private final String slingServerUrl;
+    private final String username;
+    private final String password;
+    
+    static class HttpAnyMethod extends HttpRequestBase {
+        private final URI uri;
+        private final String method;
+        
+        HttpAnyMethod(String method, String uriString) {
+            this.uri = URI.create(uriString);
+            this.method = method;
+        }
+
+        @Override
+        public String getMethod() {
+            return method;
+        }
+        
+        @Override
+        public URI getURI() {
+            return uri;
+        }
+    };
+    
+    public SlingClient(String slingServerUrl, String username, String password) {
+        this.slingServerUrl = slingServerUrl;
+        this.username = username;
+        this.password = password;
+        builder = new RequestBuilder(slingServerUrl);
+        executor = new RequestExecutor(new DefaultHttpClient());
+    }
+    
+    /** Create a node at specified path, with optional properties
+     *  specified as a list of String arguments, odd values are keys
+     *  and even arguments are values.
+     */
+    public String createNode(String path, String...properties) throws IOException {
+        Map<String, Object> props = null;
+        if(properties != null && properties.length > 0) {
+            props = new HashMap<String, Object>();
+            if(properties.length % 2 != 0) {
+                throw new IllegalArgumentException("Odd number of properties is invalid:"
+ properties.length);
+            }
+            for(int i=0 ; i<properties.length; i+=2) {
+                props.put(properties[i], properties[i+1]);
+            }
+        }
+        return createNode(path, props);
+    }
+    
+    /** Create a node at specified path, with optional properties
+     * @param path Used in POST request to Sling server
+     * @param properties If not null, properties are added to the created node
+     * @return The actual path of the node that was created
+     */
+    public String createNode(String path, Map<String, Object> properties) throws UnsupportedEncodingException,
IOException {
+        String actualPath = null;
+        
+        final MultipartEntity entity = new MultipartEntity();
+        
+        // Add Sling POST options 
+        entity.addPart(":redirect",new StringBody("*"));
+        entity.addPart(":displayExtension",new StringBody(""));
+        
+        // Add user properties
+        if(properties != null) {
+            for(Map.Entry<String, Object> e : properties.entrySet()) {
+                entity.addPart(e.getKey(), new StringBody(e.getValue().toString()));
+            }
+        }
+        
+        final HttpResponse response = 
+            executor.execute(
+                builder.buildPostRequest(path)
+                .withEntity(entity)
+                .withCredentials(username, password)
+            )
+            .assertStatus(302)
+            .getResponse();
+        
+        final Header location = response.getFirstHeader(LOCATION_HEADER);
+        assertNotNull("Expecting " + LOCATION_HEADER + " in response", location);
+        actualPath = location.getValue().substring(slingServerUrl.length());
+        return actualPath;
+    }
+    
+    /** Delete supplied path */
+    public void delete(String path) throws IOException {
+        executor.execute(
+        builder.buildOtherRequest(
+            new HttpDelete(builder.buildUrl(path)))
+            .withCredentials(username, password)
+        )
+        .assertStatus(204);
+    }
+    
+    /** Upload using a PUT request.
+     *  @param path the path of the uploaded file
+     *  @param data the content
+     *  @param length Use -1 if unknown
+     *  @param createFolders if true, intermediate folders are created via mkdirs
+     */
+    public void upload(String path, InputStream data, int length, boolean createFolders)
throws IOException {
+        final HttpEntity e = new InputStreamEntity(data, length);
+        if(createFolders) {
+            mkdirs(getParentPath(path));
+        }
+        executor.execute(
+        builder.buildOtherRequest(
+            new HttpPut(builder.buildUrl(path))).withEntity(e)
+            .withCredentials(username, password)
+        )
+        .assertStatus(201);
+    }
+    
+    /** Create path and all its parent folders, using MKCOL */
+    public void mkdirs(String path) throws IOException {
+        // Call mkdir on all parent path, starting at the topmost one
+        final Stack<String> parents = new Stack<String>();
+        path = getParentPath(path);
+        while(path.length() > 0) {
+            parents.push(path);
+            path = getParentPath(path);
+        }
+        
+        while(!parents.isEmpty()) {
+            mkdir(parents.pop());
+        }
+    }
+    
+    /** Create path using MKCOL */
+    public void mkdir(String path) throws IOException {
+        if(!exists(path)) {
+            executor.execute(
+                builder.buildOtherRequest(
+                    new HttpAnyMethod("MKCOL", builder.buildUrl(path)))
+                    .withCredentials(username, password)
+            )
+            .assertStatus(201); 
+        }
+    }
+    
+    public boolean exists(String path) throws IOException {
+        final int status = executor.execute(builder.buildGetRequest(path + ".json"))
+        .getResponse().getStatusLine().getStatusCode();
+        return status == 200;
+    }
+    
+    /** Return parent path: whatever comes before the last / in path, empty
+     *  string if no / in path.
+     */
+    protected String getParentPath(String path) {
+        final int pos = path.lastIndexOf('/');
+        if(pos > 0) {
+            return path.substring(0, pos);
+        } else {
+            return "";
+        }
+    }
+}

Propchange: sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools/sling/SlingClient.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools/sling/SlingClient.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev URL



Mime
View raw message