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 2011/02/02 18:01:52 UTC

svn commit: r1066530 - in /sling/whiteboard/bdelacretaz/junit: ./ core/ scriptable/ scriptable/src/ scriptable/src/main/ scriptable/src/main/java/ scriptable/src/main/java/org/ scriptable/src/main/java/org/apache/ scriptable/src/main/java/org/apache/sl...

Author: bdelacretaz
Date: Wed Feb  2 17:01:51 2011
New Revision: 1066530

URL: http://svn.apache.org/viewvc?rev=1066530&view=rev
Log:
SLING-1963 - scriptable tests support

Added:
    sling/whiteboard/bdelacretaz/junit/scriptable/   (with props)
    sling/whiteboard/bdelacretaz/junit/scriptable/pom.xml   (with props)
    sling/whiteboard/bdelacretaz/junit/scriptable/src/
    sling/whiteboard/bdelacretaz/junit/scriptable/src/main/
    sling/whiteboard/bdelacretaz/junit/scriptable/src/main/java/
    sling/whiteboard/bdelacretaz/junit/scriptable/src/main/java/org/
    sling/whiteboard/bdelacretaz/junit/scriptable/src/main/java/org/apache/
    sling/whiteboard/bdelacretaz/junit/scriptable/src/main/java/org/apache/sling/
    sling/whiteboard/bdelacretaz/junit/scriptable/src/main/java/org/apache/sling/junit/
    sling/whiteboard/bdelacretaz/junit/scriptable/src/main/java/org/apache/sling/junit/scriptable/
    sling/whiteboard/bdelacretaz/junit/scriptable/src/main/java/org/apache/sling/junit/scriptable/ExplainTests.java   (with props)
    sling/whiteboard/bdelacretaz/junit/scriptable/src/main/java/org/apache/sling/junit/scriptable/HttpRequest.java   (with props)
    sling/whiteboard/bdelacretaz/junit/scriptable/src/main/java/org/apache/sling/junit/scriptable/HttpResponse.java   (with props)
    sling/whiteboard/bdelacretaz/junit/scriptable/src/main/java/org/apache/sling/junit/scriptable/ScriptableTestsProvider.java   (with props)
    sling/whiteboard/bdelacretaz/junit/scriptable/src/main/java/org/apache/sling/junit/scriptable/TestAllPaths.java   (with props)
    sling/whiteboard/bdelacretaz/junit/scriptable/src/main/java/org/apache/sling/junit/scriptable/TestServletOutputStream.java   (with props)
    sling/whiteboard/bdelacretaz/junit/scriptable/src/main/resources/
    sling/whiteboard/bdelacretaz/junit/scriptable/src/main/resources/SLING-INF/
    sling/whiteboard/bdelacretaz/junit/scriptable/src/main/resources/SLING-INF/nodetypes/
    sling/whiteboard/bdelacretaz/junit/scriptable/src/main/resources/SLING-INF/nodetypes/slingtest.cnd   (with props)
Modified:
    sling/whiteboard/bdelacretaz/junit/core/pom.xml
    sling/whiteboard/bdelacretaz/junit/pom.xml
    sling/whiteboard/bdelacretaz/junit/testbundle/pom.xml

Modified: sling/whiteboard/bdelacretaz/junit/core/pom.xml
URL: http://svn.apache.org/viewvc/sling/whiteboard/bdelacretaz/junit/core/pom.xml?rev=1066530&r1=1066529&r2=1066530&view=diff
==============================================================================
--- sling/whiteboard/bdelacretaz/junit/core/pom.xml (original)
+++ sling/whiteboard/bdelacretaz/junit/core/pom.xml Wed Feb  2 17:01:51 2011
@@ -30,7 +30,7 @@
     <version>0.1.1-SNAPSHOT</version>
     <packaging>bundle</packaging>
 
-    <name>Apache Sling JUnit extension</name>
+    <name>Apache Sling JUnit Extension</name>
     <description>Runs JUnit tests in a Sling instance</description>
     
     <properties>

Modified: sling/whiteboard/bdelacretaz/junit/pom.xml
URL: http://svn.apache.org/viewvc/sling/whiteboard/bdelacretaz/junit/pom.xml?rev=1066530&r1=1066529&r2=1066530&view=diff
==============================================================================
--- sling/whiteboard/bdelacretaz/junit/pom.xml (original)
+++ sling/whiteboard/bdelacretaz/junit/pom.xml Wed Feb  2 17:01:51 2011
@@ -43,6 +43,7 @@
 
     <modules>
         <module>core</module>
+        <module>scriptable</module>
         <module>testbundle</module>
     </modules>
 </project>

Propchange: sling/whiteboard/bdelacretaz/junit/scriptable/
------------------------------------------------------------------------------
--- svn:ignore (added)
+++ svn:ignore Wed Feb  2 17:01:51 2011
@@ -0,0 +1,14 @@
+target
+bin
+*.iml
+*.ipr
+*.iws
+.settings
+.project
+.classpath
+.externalToolBuilders
+maven-eclipse.xml
+
+
+
+

Added: sling/whiteboard/bdelacretaz/junit/scriptable/pom.xml
URL: http://svn.apache.org/viewvc/sling/whiteboard/bdelacretaz/junit/scriptable/pom.xml?rev=1066530&view=auto
==============================================================================
--- sling/whiteboard/bdelacretaz/junit/scriptable/pom.xml (added)
+++ sling/whiteboard/bdelacretaz/junit/scriptable/pom.xml Wed Feb  2 17:01:51 2011
@@ -0,0 +1,123 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+    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.
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+
+    <modelVersion>4.0.0</modelVersion>
+    <parent>
+        <groupId>org.apache.sling</groupId>
+        <artifactId>sling</artifactId>
+        <version>10</version>
+    </parent>
+
+    <artifactId>org.apache.sling.junit.scriptable</artifactId>
+    <version>0.1.1-SNAPSHOT</version>
+    <packaging>bundle</packaging>
+
+    <name>Apache Sling JUnit Scriptable Tests Provider</name>
+    <description>
+        Registers Sling test resources with the JUnit core 
+        so that they can be run as tests.
+    </description>
+    
+<!-- TODO
+    <scm>
+        <connection>scm:svn:http://svn.apache.org/repos/asf/sling/trunk/installer/providers/jcr</connection>
+        <developerConnection> scm:svn:https://svn.apache.org/repos/asf/sling/trunk/installer/providers/jcr</developerConnection>
+        <url>http://svn.apache.org/viewvc/sling/trunk/installer/providers/jcr/</url>
+    </scm>
+-->
+
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.apache.felix</groupId>
+                <artifactId>maven-scr-plugin</artifactId>
+            </plugin>
+            <plugin>
+                <groupId>org.apache.felix</groupId>
+                <artifactId>maven-bundle-plugin</artifactId>
+                <extensions>true</extensions>
+                <configuration>
+                    <instructions>
+                        <Private-Package>org.apache.sling.junit.scriptable.*</Private-Package>
+                        <Sling-Nodetypes>SLING-INF/nodetypes/slingtest.cnd</Sling-Nodetypes>
+                    </instructions>
+                </configuration>
+            </plugin>
+        </plugins>
+    </build>
+
+    <dependencies>
+        <dependency>
+            <groupId>org.osgi</groupId>
+            <artifactId>org.osgi.core</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.osgi</groupId>
+            <artifactId>org.osgi.compendium</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.felix</groupId>
+            <artifactId>org.apache.felix.scr.annotations</artifactId>
+            <version>1.4.0</version>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.sling</groupId>
+            <artifactId>org.apache.sling.api</artifactId>
+            <version>2.0.2-incubator</version>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.sling</groupId>
+            <artifactId>org.apache.sling.engine</artifactId>
+            <version>2.2.0</version>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.sling</groupId>
+            <artifactId>org.apache.sling.jcr.api</artifactId>
+            <version>2.0.2-incubator</version>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.sling</groupId>
+            <artifactId>org.apache.sling.jcr.resource</artifactId>
+            <version>2.0.6</version>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.sling</groupId>
+            <artifactId>org.apache.sling.junit.core</artifactId>
+            <version>0.1.1-SNAPSHOT</version>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>javax.servlet</groupId>
+            <artifactId>servlet-api</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.slf4j</groupId>
+            <artifactId>slf4j-api</artifactId>
+            <version>1.5.11</version>
+            <scope>provided</scope>
+        </dependency>
+    </dependencies>
+</project>

Propchange: sling/whiteboard/bdelacretaz/junit/scriptable/pom.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Added: sling/whiteboard/bdelacretaz/junit/scriptable/src/main/java/org/apache/sling/junit/scriptable/ExplainTests.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/bdelacretaz/junit/scriptable/src/main/java/org/apache/sling/junit/scriptable/ExplainTests.java?rev=1066530&view=auto
==============================================================================
--- sling/whiteboard/bdelacretaz/junit/scriptable/src/main/java/org/apache/sling/junit/scriptable/ExplainTests.java (added)
+++ sling/whiteboard/bdelacretaz/junit/scriptable/src/main/java/org/apache/sling/junit/scriptable/ExplainTests.java Wed Feb  2 17:01:51 2011
@@ -0,0 +1,38 @@
+/*
+ * 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.junit.scriptable;
+
+import static org.junit.Assert.fail;
+
+import org.junit.Test;
+
+/** Test that always fails and explains how to create
+ *  scriptable tests.
+ */
+public class ExplainTests {
+    
+    @Test
+    public void explain() {
+        fail(
+                "No scriptable tests found."
+                + " To create scriptable tests, create nodes with the sling:Test"
+                + " mixin, and setup Sling so that requesting them with .test.txt generates"
+                + " a text response containing just TEST_PASSED if the test is successful."
+                + " Empty lines and lines starting with # are ignored in the test output."
+                );
+    }
+}

Propchange: sling/whiteboard/bdelacretaz/junit/scriptable/src/main/java/org/apache/sling/junit/scriptable/ExplainTests.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sling/whiteboard/bdelacretaz/junit/scriptable/src/main/java/org/apache/sling/junit/scriptable/ExplainTests.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev URL

Added: sling/whiteboard/bdelacretaz/junit/scriptable/src/main/java/org/apache/sling/junit/scriptable/HttpRequest.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/bdelacretaz/junit/scriptable/src/main/java/org/apache/sling/junit/scriptable/HttpRequest.java?rev=1066530&view=auto
==============================================================================
--- sling/whiteboard/bdelacretaz/junit/scriptable/src/main/java/org/apache/sling/junit/scriptable/HttpRequest.java (added)
+++ sling/whiteboard/bdelacretaz/junit/scriptable/src/main/java/org/apache/sling/junit/scriptable/HttpRequest.java Wed Feb  2 17:01:51 2011
@@ -0,0 +1,261 @@
+/*
+ * 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.junit.scriptable;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.UnsupportedEncodingException;
+import java.security.Principal;
+import java.util.Enumeration;
+import java.util.HashMap;
+import java.util.Locale;
+import java.util.Map;
+
+import javax.servlet.RequestDispatcher;
+import javax.servlet.ServletInputStream;
+import javax.servlet.http.Cookie;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpSession;
+
+/** Fake request used to acquire content from Sling */
+public class HttpRequest implements HttpServletRequest {
+
+    public static final String FAKE_URL_BASE = "http://example.com";
+    private final String path;
+    private final Map<String, Object> attributes;
+    
+    HttpRequest(String path) {
+        this.path = path;
+        attributes = new HashMap<String, Object>();
+    }
+    
+    public String getAuthType() {
+        return null;
+    }
+
+    public String getContextPath() {
+        return "";
+    }
+
+    public Cookie[] getCookies() {
+        return null;
+    }
+
+    public long getDateHeader(String name) {
+        return 0;
+    }
+
+    public String getHeader(String name) {
+        return null;
+    }
+
+    public Enumeration<?> getHeaderNames() {
+        return null;
+    }
+
+    public Enumeration<?> getHeaders(String name) {
+        return null;
+    }
+
+    public int getIntHeader(String name) {
+        return 0;
+    }
+
+    public String getMethod() {
+        return "GET";
+    }
+
+    public String getPathInfo() {
+        return null;
+    }
+
+    public String getPathTranslated() {
+        return null;
+    }
+
+    public String getQueryString() {
+        return null;
+    }
+
+    public String getRemoteUser() {
+        return null;
+    }
+
+    public String getRequestedSessionId() {
+        return null;
+    }
+
+    public String getRequestURI() {
+        return path;
+    }
+
+    public StringBuffer getRequestURL() {
+        return new StringBuffer(FAKE_URL_BASE + path);
+    }
+
+    public String getServletPath() {
+        return path; 
+    }
+
+    public HttpSession getSession() {
+        return null;
+    }
+
+    public HttpSession getSession(boolean create) {
+        return null;
+    }
+
+    public Principal getUserPrincipal() {
+        return null;
+    }
+
+    public boolean isRequestedSessionIdFromCookie() {
+        return false;
+    }
+
+    public boolean isRequestedSessionIdFromUrl() {
+        return false;
+    }
+
+    public boolean isRequestedSessionIdFromURL() {
+        return false;
+    }
+
+    public boolean isRequestedSessionIdValid() {
+        return false;
+    }
+
+    public boolean isUserInRole(String role) {
+        return false;
+    }
+
+    public Object getAttribute(String name) {
+        return attributes.get(name);
+    }
+
+    public Enumeration<?> getAttributeNames() {
+        return null;
+    }
+
+    public String getCharacterEncoding() {
+        return null;
+    }
+
+    public int getContentLength() {
+        return 0;
+    }
+
+    public String getContentType() {
+        return null;
+    }
+
+    public ServletInputStream getInputStream() throws IOException {
+        return null;
+    }
+
+    public String getLocalAddr() {
+        return null;
+    }
+
+    public Locale getLocale() {
+        return null;
+    }
+
+    public Enumeration<?> getLocales() {
+        return null;
+    }
+
+    public String getLocalName() {
+        return null;
+    }
+
+    public int getLocalPort() {
+        return 0;
+    }
+
+    public String getParameter(String name) {
+        return null;
+    }
+
+    public Map<?,?> getParameterMap() {
+        return null;
+    }
+
+    public Enumeration<?> getParameterNames() {
+        return null;
+    }
+
+    public String[] getParameterValues(String name) {
+        return null;
+    }
+
+    public String getProtocol() {
+        return null;
+    }
+
+    public BufferedReader getReader() throws IOException {
+        return null;
+    }
+
+    public String getRealPath(String path) {
+        return null;
+    }
+
+    public String getRemoteAddr() {
+        return null;
+    }
+
+    public String getRemoteHost() {
+        return null;
+    }
+
+    public int getRemotePort() {
+        return 0;
+    }
+
+    public RequestDispatcher getRequestDispatcher(String path) {
+        return null;
+    }
+
+    public String getScheme() {
+        return "http";
+    }
+
+    public String getServerName() {
+        return null;
+    }
+
+    public int getServerPort() {
+        return 0;
+    }
+
+    public boolean isSecure() {
+        return false;
+    }
+
+    public void removeAttribute(String name) {
+        attributes.remove(name);
+    }
+
+    public void setAttribute(String name, Object o) {
+        attributes.put(name, o);
+    }
+
+    public void setCharacterEncoding(String env)
+            throws UnsupportedEncodingException {
+    }
+}

Propchange: sling/whiteboard/bdelacretaz/junit/scriptable/src/main/java/org/apache/sling/junit/scriptable/HttpRequest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sling/whiteboard/bdelacretaz/junit/scriptable/src/main/java/org/apache/sling/junit/scriptable/HttpRequest.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev URL

Added: sling/whiteboard/bdelacretaz/junit/scriptable/src/main/java/org/apache/sling/junit/scriptable/HttpResponse.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/bdelacretaz/junit/scriptable/src/main/java/org/apache/sling/junit/scriptable/HttpResponse.java?rev=1066530&view=auto
==============================================================================
--- sling/whiteboard/bdelacretaz/junit/scriptable/src/main/java/org/apache/sling/junit/scriptable/HttpResponse.java (added)
+++ sling/whiteboard/bdelacretaz/junit/scriptable/src/main/java/org/apache/sling/junit/scriptable/HttpResponse.java Wed Feb  2 17:01:51 2011
@@ -0,0 +1,168 @@
+package org.apache.sling.junit.scriptable;
+
+import java.io.IOException;
+import java.io.OutputStreamWriter;
+import java.io.PrintWriter;
+import java.io.UnsupportedEncodingException;
+import java.util.Locale;
+
+import javax.servlet.ServletOutputStream;
+import javax.servlet.http.Cookie;
+import javax.servlet.http.HttpServletResponse;
+
+/** Fake response used to acquire content from Sling */
+public class HttpResponse implements HttpServletResponse {
+
+    private int status = 200;
+    private String message;
+    private String encoding = "UTF-8";
+    private String contentType;
+    private final TestServletOutputStream outputStream;
+    private final PrintWriter writer;
+    
+    HttpResponse() throws UnsupportedEncodingException {
+        outputStream = new TestServletOutputStream();
+        writer = new PrintWriter(new OutputStreamWriter(outputStream, encoding));
+    }
+    
+    public void addCookie(Cookie cookie) {
+    }
+
+    public void addDateHeader(String name, long date) {
+    }
+
+    public void addHeader(String name, String value) {
+    }
+
+    public void addIntHeader(String name, int value) {
+    }
+
+    public boolean containsHeader(String name) {
+        return false;
+    }
+
+    public String encodeRedirectUrl(String url) {
+        return null;
+    }
+
+    public String encodeRedirectURL(String url) {
+        return null;
+    }
+
+    public String encodeUrl(String url) {
+        return null;
+    }
+
+    public String encodeURL(String url) {
+        return null;
+    }
+
+    public void sendError(int sc, String msg) throws IOException {
+        status = sc;
+        message = msg;
+    }
+
+    public void sendError(int sc) throws IOException {
+        status = sc;
+    }
+
+    public void sendRedirect(String location) throws IOException {
+    }
+
+    public void setDateHeader(String name, long date) {
+    }
+
+    public void setHeader(String name, String value) {
+    }
+
+    public void setIntHeader(String name, int value) {
+    }
+
+    public void setStatus(int sc, String sm) {
+        status = sc;
+        message = sm;
+    }
+
+    public void setStatus(int sc) {
+        status = sc;
+    }
+
+    public void flushBuffer() throws IOException {
+    }
+
+    public int getBufferSize() {
+        return 0;
+    }
+
+    public String getCharacterEncoding() {
+        return encoding;
+    }
+
+    public String getContentType() {
+        return contentType;
+    }
+
+    public Locale getLocale() {
+        return null;
+    }
+
+    public ServletOutputStream getOutputStream() throws IOException {
+        return outputStream;
+    }
+
+    public PrintWriter getWriter() throws IOException {
+        return writer;
+    }
+
+    public boolean isCommitted() {
+        return false;
+    }
+
+    public void reset() {
+    }
+
+    public void resetBuffer() {
+    }
+
+    public void setBufferSize(int size) {
+    }
+
+    public void setCharacterEncoding(String charset) {
+        encoding = charset;
+    }
+
+    public void setContentLength(int len) {
+    }
+
+    public void setContentType(String type) {
+        contentType = type;
+    }
+
+    public void setLocale(Locale loc) {
+    }
+
+    public String getMessage() {
+        return message;
+    }
+
+    public void setMessage(String message) {
+        this.message = message;
+    }
+
+    public String getEncoding() {
+        return encoding;
+    }
+
+    public void setEncoding(String encoding) {
+        this.encoding = encoding;
+    }
+
+    public int getStatus() {
+        return status;
+    }
+    
+    public String getContent() {
+        writer.flush();
+        return outputStream.toString();
+    }
+}

Propchange: sling/whiteboard/bdelacretaz/junit/scriptable/src/main/java/org/apache/sling/junit/scriptable/HttpResponse.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sling/whiteboard/bdelacretaz/junit/scriptable/src/main/java/org/apache/sling/junit/scriptable/HttpResponse.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev URL

Added: sling/whiteboard/bdelacretaz/junit/scriptable/src/main/java/org/apache/sling/junit/scriptable/ScriptableTestsProvider.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/bdelacretaz/junit/scriptable/src/main/java/org/apache/sling/junit/scriptable/ScriptableTestsProvider.java?rev=1066530&view=auto
==============================================================================
--- sling/whiteboard/bdelacretaz/junit/scriptable/src/main/java/org/apache/sling/junit/scriptable/ScriptableTestsProvider.java (added)
+++ sling/whiteboard/bdelacretaz/junit/scriptable/src/main/java/org/apache/sling/junit/scriptable/ScriptableTestsProvider.java Wed Feb  2 17:01:51 2011
@@ -0,0 +1,129 @@
+/*
+ * 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.junit.scriptable;
+
+import java.util.LinkedList;
+import java.util.List;
+
+import javax.jcr.NodeIterator;
+import javax.jcr.RepositoryException;
+import javax.jcr.Session;
+import javax.jcr.query.Query;
+
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Reference;
+import org.apache.felix.scr.annotations.Service;
+import org.apache.sling.api.resource.ResourceResolver;
+import org.apache.sling.engine.SlingRequestProcessor;
+import org.apache.sling.jcr.api.SlingRepository;
+import org.apache.sling.jcr.resource.JcrResourceResolverFactory;
+import org.apache.sling.junit.TestsProvider;
+import org.osgi.framework.Constants;
+import org.osgi.service.component.ComponentContext;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/** TestsProvider that provides test classes for repository
+ *  nodes that have a sling:Test mixin.
+ */
+@Component
+@Service
+public class ScriptableTestsProvider implements TestsProvider {
+    private final Logger log = LoggerFactory.getLogger(getClass());
+    private String pid;
+    private static List<String> testPaths = new LinkedList<String>();
+    private Session session;
+    private ResourceResolver resolver;
+    
+    @Reference
+    private SlingRepository repository;
+    
+    @Reference
+    private SlingRequestProcessor requestProcessor;
+    
+    @Reference
+    private JcrResourceResolverFactory resolverFactory;
+    
+    protected void activate(ComponentContext ctx) throws Exception {
+        pid = (String)ctx.getProperties().get(Constants.SERVICE_PID);
+        session = repository.loginAdministrative(repository.getDefaultWorkspace());
+        resolver = resolverFactory.getResourceResolver(session);
+    }
+    
+    protected void deactivate(ComponentContext ctx) throws RepositoryException {
+        resolver = null;
+        if(session != null) {
+            session.logout();
+        }
+        session = null;
+    }
+    
+    public Class<?> createTestClass(String testName) throws ClassNotFoundException {
+        queryTestPaths();
+        
+        if(testPaths.size() == 0) {
+            return ExplainTests.class;
+        } else {
+            // TODO this would cause a mess if TestAllPaths is executed concurrently
+            // and these values change
+            TestAllPaths.testPaths = testPaths;
+            TestAllPaths.requestProcessor = requestProcessor;
+            TestAllPaths.resolver = resolver;
+            return TestAllPaths.class;
+        }
+    }
+
+    public String getServicePid() {
+        return pid;
+    }
+
+    public List<String> getTestNames() {
+        // We have a single test to run, would be better to have one
+        // test class per test resource but that looks harder. Maybe
+        // use the Sling compiler to generate test classes? 
+        final List<String> result = new LinkedList<String>();
+        result.add(getClass().getSimpleName() + "Tests");
+        return result;
+    }
+    
+    private List<String> queryTestPaths() {
+        final List<String> result = new LinkedList<String>();
+        
+        // TODO do we want to cache results, use observation, etc.
+        try {
+            final Query q = session.getWorkspace().getQueryManager().createQuery("//element(*, sling:Test)", Query.XPATH);
+            final NodeIterator it = q.execute().getNodes();
+            while(it.hasNext()) {
+                result.add(it.nextNode().getPath());
+            }
+        } catch(RepositoryException re) {
+            log.warn("RepositoryException in getTestNames()", re);
+        }
+
+        synchronized (testPaths) {
+            testPaths.clear();
+            testPaths.addAll(result);
+        }
+        
+        return testPaths;
+    }
+
+    public long lastModified() {
+        // TODO caching etc.
+        return System.currentTimeMillis();
+    }
+}

Propchange: sling/whiteboard/bdelacretaz/junit/scriptable/src/main/java/org/apache/sling/junit/scriptable/ScriptableTestsProvider.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sling/whiteboard/bdelacretaz/junit/scriptable/src/main/java/org/apache/sling/junit/scriptable/ScriptableTestsProvider.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev URL

Added: sling/whiteboard/bdelacretaz/junit/scriptable/src/main/java/org/apache/sling/junit/scriptable/TestAllPaths.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/bdelacretaz/junit/scriptable/src/main/java/org/apache/sling/junit/scriptable/TestAllPaths.java?rev=1066530&view=auto
==============================================================================
--- sling/whiteboard/bdelacretaz/junit/scriptable/src/main/java/org/apache/sling/junit/scriptable/TestAllPaths.java (added)
+++ sling/whiteboard/bdelacretaz/junit/scriptable/src/main/java/org/apache/sling/junit/scriptable/TestAllPaths.java Wed Feb  2 17:01:51 2011
@@ -0,0 +1,92 @@
+/*
+ * 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.junit.scriptable;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.fail;
+
+import java.io.BufferedReader;
+import java.io.StringReader;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+
+import org.apache.sling.api.resource.ResourceResolver;
+import org.apache.sling.engine.SlingRequestProcessor;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+import org.junit.runners.Parameterized.Parameters;
+
+/** The single test class that runs all our path-based tests */
+@RunWith(Parameterized.class)
+public class TestAllPaths {
+
+    private String path;
+    public static final String TEST_URL_SUFFIX = ".test.txt";
+    public static final String PASSED = "TEST_PASSED";
+    
+    // TODO can we do better than this to inject our environment here?
+    static List<String> testPaths;
+    static SlingRequestProcessor requestProcessor;
+    static ResourceResolver resolver;
+
+    public TestAllPaths(String path) {
+        this.path = path;
+    }
+
+    /** Let JUnit run this all on our paths */
+    @Parameters
+    public static Collection<Object[]> data() {
+        Collection<Object[]> data = new ArrayList<Object[]>();
+        for(String path : testPaths) {
+            data.add(new Object[] { path + TEST_URL_SUFFIX });
+        }
+        return data;
+    }
+
+    @Test
+    public void verifyContent() throws Exception {
+        
+        // Get content via internal Sling request
+        final HttpRequest req = new HttpRequest(path);
+        final HttpResponse resp = new HttpResponse();
+        requestProcessor.processRequest(req, resp, resolver);
+        final String content = resp.getContent();
+        assertEquals("Expecting HTTP status 200 for path " + path, 200, resp.getStatus());
+        
+        // Expect a single line of content with TEST_PASSED, ignoring
+        // empty lines and lines that start with #
+        final BufferedReader br = new BufferedReader(new StringReader(content));
+        String line = null;
+        int passedCount = 0;
+        while( (line = br.readLine()) != null) {
+            if(line.trim().length() == 0) {
+                // ignore
+            } else if(line.startsWith("#")) {
+                // ignore
+            } else if(line.trim().equals(PASSED) && passedCount == 0) {
+                passedCount++;
+            } else {
+                fail("Unexpected content at path " + path 
+                        + ", should be just " + PASSED + " (lines starting with # and empty lines are ignored)"
+                        + "\ncontent was:\n" + content + "\n");
+            }
+        }
+    }
+}
+

Propchange: sling/whiteboard/bdelacretaz/junit/scriptable/src/main/java/org/apache/sling/junit/scriptable/TestAllPaths.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sling/whiteboard/bdelacretaz/junit/scriptable/src/main/java/org/apache/sling/junit/scriptable/TestAllPaths.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev URL

Added: sling/whiteboard/bdelacretaz/junit/scriptable/src/main/java/org/apache/sling/junit/scriptable/TestServletOutputStream.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/bdelacretaz/junit/scriptable/src/main/java/org/apache/sling/junit/scriptable/TestServletOutputStream.java?rev=1066530&view=auto
==============================================================================
--- sling/whiteboard/bdelacretaz/junit/scriptable/src/main/java/org/apache/sling/junit/scriptable/TestServletOutputStream.java (added)
+++ sling/whiteboard/bdelacretaz/junit/scriptable/src/main/java/org/apache/sling/junit/scriptable/TestServletOutputStream.java Wed Feb  2 17:01:51 2011
@@ -0,0 +1,22 @@
+package org.apache.sling.junit.scriptable;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+
+import javax.servlet.ServletOutputStream;
+
+/** Output stream for our fake HTTP response class */
+public class TestServletOutputStream extends ServletOutputStream {
+
+    private final ByteArrayOutputStream bos = new ByteArrayOutputStream();
+    
+    @Override
+    public String toString() {
+        return bos.toString();
+    }
+    
+    @Override
+    public void write(int b) throws IOException {
+        bos.write(b);
+    }
+}

Propchange: sling/whiteboard/bdelacretaz/junit/scriptable/src/main/java/org/apache/sling/junit/scriptable/TestServletOutputStream.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sling/whiteboard/bdelacretaz/junit/scriptable/src/main/java/org/apache/sling/junit/scriptable/TestServletOutputStream.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev URL

Added: sling/whiteboard/bdelacretaz/junit/scriptable/src/main/resources/SLING-INF/nodetypes/slingtest.cnd
URL: http://svn.apache.org/viewvc/sling/whiteboard/bdelacretaz/junit/scriptable/src/main/resources/SLING-INF/nodetypes/slingtest.cnd?rev=1066530&view=auto
==============================================================================
--- sling/whiteboard/bdelacretaz/junit/scriptable/src/main/resources/SLING-INF/nodetypes/slingtest.cnd (added)
+++ sling/whiteboard/bdelacretaz/junit/scriptable/src/main/resources/SLING-INF/nodetypes/slingtest.cnd Wed Feb  2 17:01:51 2011
@@ -0,0 +1,25 @@
+//
+//  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.
+//
+
+<sling = 'http://sling.apache.org/jcr/sling/1.0'>
+
+//-----------------------------------------------------------------------------
+// Mixin marker node type for Sling Test nodes
+[sling:Test]
+    mixin
\ No newline at end of file

Propchange: sling/whiteboard/bdelacretaz/junit/scriptable/src/main/resources/SLING-INF/nodetypes/slingtest.cnd
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: sling/whiteboard/bdelacretaz/junit/testbundle/pom.xml
URL: http://svn.apache.org/viewvc/sling/whiteboard/bdelacretaz/junit/testbundle/pom.xml?rev=1066530&r1=1066529&r2=1066530&view=diff
==============================================================================
--- sling/whiteboard/bdelacretaz/junit/testbundle/pom.xml (original)
+++ sling/whiteboard/bdelacretaz/junit/testbundle/pom.xml Wed Feb  2 17:01:51 2011
@@ -30,7 +30,7 @@
     <version>0.1.1-SNAPSHOT</version>
     <packaging>bundle</packaging>
 
-    <name>Apache Sling JUnit extension test bundle</name>
+    <name>Apache Sling JUnit Extension Test Bundle</name>
     <description>Provides test classes to test the Sling JUnit extensions</description>
     
     <properties>