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/15 16:35:33 UTC

svn commit: r1070940 - in /sling/trunk/testing/sample: ./ src/ src/test/ src/test/java/ src/test/java/org/ src/test/java/org/apache/ src/test/java/org/apache/sling/ src/test/java/org/apache/sling/junit/ src/test/java/org/apache/sling/junit/sample/ src/...

Author: bdelacretaz
Date: Tue Feb 15 15:35:32 2011
New Revision: 1070940

URL: http://svn.apache.org/viewvc?rev=1070940&view=rev
Log:
SLING-1981 - sample testing module, work in progress. Uses Stanbol snapshots from http://svn.apache.org/repos/asf/incubator/stanbol/trunk/commons

Added:
    sling/trunk/testing/sample/   (with props)
    sling/trunk/testing/sample/pom.xml   (with props)
    sling/trunk/testing/sample/src/
    sling/trunk/testing/sample/src/test/
    sling/trunk/testing/sample/src/test/java/
    sling/trunk/testing/sample/src/test/java/org/
    sling/trunk/testing/sample/src/test/java/org/apache/
    sling/trunk/testing/sample/src/test/java/org/apache/sling/
    sling/trunk/testing/sample/src/test/java/org/apache/sling/junit/
    sling/trunk/testing/sample/src/test/java/org/apache/sling/junit/sample/
    sling/trunk/testing/sample/src/test/java/org/apache/sling/junit/sample/SlingTestBase.java   (with props)
    sling/trunk/testing/sample/src/test/java/org/apache/sling/junit/sample/httptests/
    sling/trunk/testing/sample/src/test/java/org/apache/sling/junit/sample/httptests/OsgiConsoleTest.java   (with props)

Propchange: sling/trunk/testing/sample/
------------------------------------------------------------------------------
--- svn:ignore (added)
+++ svn:ignore Tue Feb 15 15:35:32 2011
@@ -0,0 +1,13 @@
+target
+bin
+*.iml
+*.ipr
+*.iws
+.settings
+.project
+.classpath
+.externalToolBuilders
+maven-eclipse.xml
+sling
+jackrabbit
+derby.log

Added: sling/trunk/testing/sample/pom.xml
URL: http://svn.apache.org/viewvc/sling/trunk/testing/sample/pom.xml?rev=1070940&view=auto
==============================================================================
--- sling/trunk/testing/sample/pom.xml (added)
+++ sling/trunk/testing/sample/pom.xml Tue Feb 15 15:35:32 2011
@@ -0,0 +1,173 @@
+<?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.testing.sample</artifactId>
+    <version>0.1.1-SNAPSHOT</version>
+    <packaging>jar</packaging>
+
+    <name>Apache Sling Testing Sample</name>
+    <description>
+        Sample project that demonstrates the various tools and techniques
+        available to test Sling applications: plain build-time JUnit tests,
+        HTTP-based integration tests, server-side JUnit tests injected via
+        bundles and server-side scriptable tests.
+    </description>
+    
+    <properties>
+        <!-- Set this to run the server on a specific port -->
+        <http.port></http.port>
+        
+        <!-- Set this to run tests against an existing server instance -->
+        <test.server.url></test.server.url>
+        
+        <!-- Options for the VM that executes our runnable jar -->
+        <jar.executor.vm.options>-Xmx512m</jar.executor.vm.options>
+        
+        <!-- Set this to true to keep the runnable jar running - useful to debug tests -->
+        <keepJarRunning>false</keepJarRunning>
+    </properties>
+    
+    <scm>
+        <connection>scm:svn:http://svn.apache.org/repos/asf/sling/trunk/testing/junit/sample</connection>
+        <developerConnection> scm:svn:https://svn.apache.org/repos/asf/sling/trunk/testing/junit/sample</developerConnection>
+        <url>http://svn.apache.org/viewvc/sling/trunk/testing/junit/sample</url>
+    </scm>
+
+    <build>
+        <plugins>
+           <plugin>
+                <artifactId>maven-clean-plugin</artifactId>
+                <version>2.2</version>
+                <configuration>
+                    <filesets>
+                        <fileset>
+                            <directory>${basedir}</directory>
+                            <includes>
+                                <!-- sling folder is the workdir of the executable jar that we test -->
+                                <include>sling</include>
+                            </includes>
+                        </fileset>
+                    </filesets>
+                </configuration>
+            </plugin>
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-dependency-plugin</artifactId>
+                <executions>
+                    <execution>
+                        <id>copy-runnable-jar</id>
+                        <goals>
+                            <goal>copy-dependencies</goal>
+                        </goals>
+                            <phase>process-resources</phase>
+                        <configuration>
+                            <includeArtifactIds>org.apache.sling.launchpad</includeArtifactIds>
+                            <excludeTransitive>true</excludeTransitive>
+                            <overWriteReleases>false</overWriteReleases>
+                            <overWriteSnapshots>false</overWriteSnapshots>
+                        </configuration>
+                    </execution>
+                </executions>
+            </plugin>
+            <plugin>
+                <!-- Find free ports to run our server -->
+                <groupId>org.codehaus.mojo</groupId>
+                <artifactId>build-helper-maven-plugin</artifactId>
+                <executions>
+                    <execution>
+                        <id>reserve-server-port</id>
+                        <goals>
+                            <goal>reserve-network-port</goal>
+                        </goals>
+                        <phase>process-resources</phase>
+                        <configuration>
+                            <portNames>
+                                <portName>http.port</portName>
+                            </portNames>
+                        </configuration>
+                    </execution>
+                </executions>
+            </plugin>
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-surefire-plugin</artifactId>
+                <version>2.5</version>
+                <configuration>
+                    <systemPropertyVariables>
+                        <test.server.url>${test.server.url}</test.server.url>
+                        <jar.executor.server.port>${http.port}</jar.executor.server.port>
+                        <jar.executor.vm.options>${jar.executor.vm.options}</jar.executor.vm.options>
+                        <jar.executor.jar.folder>${project.basedir}/target/dependency</jar.executor.jar.folder>
+                        <jar.executor.jar.name.regexp>org.apache.sling.launchpad.*jar$</jar.executor.jar.name.regexp>
+                        <keepJarRunning>${keepJarRunning}</keepJarRunning>
+                        <server.ready.timeout.seconds>60</server.ready.timeout.seconds>
+                        <server.ready.path.1>/:script src="system/sling.js"</server.ready.path.1>
+                        <server.ready.path.2>/.explorer.html:href="/libs/sling/explorer/css/explorer.css"</server.ready.path.2>
+                        <server.ready.path.3>/sling-test/sling/sling-test.html:Sling client library tests</server.ready.path.3>
+                    </systemPropertyVariables>
+                </configuration>
+            </plugin>
+         </plugins>
+    </build>
+
+    <dependencies>
+        <dependency>
+            <groupId>org.apache.stanbol</groupId>
+            <artifactId>org.apache.stanbol.commons.testing.jarexec</artifactId>
+            <version>0.9-SNAPSHOT</version>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.stanbol</groupId>
+            <artifactId>org.apache.stanbol.commons.testing.http</artifactId>
+            <version>0.9-SNAPSHOT</version>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.sling</groupId>
+            <artifactId>org.apache.sling.launchpad</artifactId>
+            <classifier>standalone</classifier>
+            <version>6-SNAPSHOT</version>
+        </dependency>
+        <dependency>
+            <groupId>org.slf4j</groupId>
+            <artifactId>slf4j-api</artifactId>
+            <version>1.5.11</version>
+        </dependency>
+        <dependency>
+            <groupId>org.slf4j</groupId>
+            <artifactId>slf4j-simple</artifactId>
+            <version>1.5.11</version>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>junit</groupId>
+            <artifactId>junit</artifactId>
+            <version>4.8.2</version>
+            <scope>test</scope>
+        </dependency>
+    </dependencies>
+</project>

Propchange: sling/trunk/testing/sample/pom.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Added: sling/trunk/testing/sample/src/test/java/org/apache/sling/junit/sample/SlingTestBase.java
URL: http://svn.apache.org/viewvc/sling/trunk/testing/sample/src/test/java/org/apache/sling/junit/sample/SlingTestBase.java?rev=1070940&view=auto
==============================================================================
--- sling/trunk/testing/sample/src/test/java/org/apache/sling/junit/sample/SlingTestBase.java (added)
+++ sling/trunk/testing/sample/src/test/java/org/apache/sling/junit/sample/SlingTestBase.java Tue Feb 15 15:35:32 2011
@@ -0,0 +1,142 @@
+/*
+ * 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.sample;
+
+import static org.junit.Assert.fail;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.TreeSet;
+
+import org.apache.http.impl.client.DefaultHttpClient;
+import org.apache.stanbol.commons.testing.http.RequestBuilder;
+import org.apache.stanbol.commons.testing.http.RequestExecutor;
+import org.apache.stanbol.commons.testing.jarexec.JarExecutor;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/** Base class for running tests against a Sling instance,
+ *  takes care of starting Sling and waiting for it to be ready.
+ */
+public class SlingTestBase {
+    public static final String TEST_SERVER_URL_PROP = "test.server.url";
+    public static final String SERVER_READY_TIMEOUT_PROP = "server.ready.timeout.seconds";
+    public static final String SERVER_READY_PROP_PREFIX = "server.ready.path";
+    public static final String KEEP_JAR_RUNNING_PROP = "keepJarRunning";
+    
+    protected static String serverBaseUrl;
+    protected static RequestBuilder builder;
+    protected static DefaultHttpClient httpClient = new DefaultHttpClient();
+    protected static RequestExecutor executor = new RequestExecutor(httpClient);
+    
+    private static boolean serverReady;
+    private static boolean serverReadyTestFailed;
+    private static final Logger log = LoggerFactory.getLogger(SlingTestBase.class);
+    
+    @BeforeClass
+    public static synchronized void startRunnableJar() throws Exception {
+        final String configuredUrl = System.getProperty(TEST_SERVER_URL_PROP);
+        if(configuredUrl != null) {
+            serverBaseUrl = configuredUrl;
+            log.info(TEST_SERVER_URL_PROP + " is set: not starting server jar (" + serverBaseUrl + ")");
+        } else {
+            final JarExecutor j = JarExecutor.getInstance(System.getProperties());
+            j.start();
+            serverBaseUrl = "http://localhost:" + j.getServerPort();
+            
+            // Optionally block here so that the runnable jar stays up - we can
+            // then run tests against it from another VM
+            if ("true".equals(System.getProperty(KEEP_JAR_RUNNING_PROP))) {
+                log.info(KEEP_JAR_RUNNING_PROP + " set to true - entering infinite loop"
+                         + " so that runnable jar stays up. Kill this process to exit.");
+                while (true) {
+                    Thread.sleep(1000L);
+                }
+            }
+        }
+        
+        builder = new RequestBuilder(serverBaseUrl);
+    }
+    
+    @Before
+    public void waitForServerReady() throws Exception {
+        if(serverReady) {
+            return;
+        }
+        if(serverReadyTestFailed) {
+            fail("Server is not ready according to previous tests");
+        }
+        
+        // Timeout for readiness test
+        final String sec = System.getProperty(SERVER_READY_TIMEOUT_PROP);
+        final int timeoutSec = sec == null ? 60 : Integer.valueOf(sec);
+        log.info("Will wait up to " + timeoutSec + " seconds for server to become ready");
+        final long endTime = System.currentTimeMillis() + timeoutSec * 1000L;
+
+        // Get the list of paths to test and expected content regexps
+        final List<String> testPaths = new ArrayList<String>();
+        final TreeSet<Object> propertyNames = new TreeSet<Object>();
+        propertyNames.addAll(System.getProperties().keySet());
+        for(Object o : propertyNames) {
+            final String key = (String)o;
+            if(key.startsWith(SERVER_READY_PROP_PREFIX)) {
+                testPaths.add(System.getProperty(key));
+            }
+        }
+        
+        // Consider the server ready if it responds to a GET on each of 
+        // our configured request paths with a 200 result and content
+        // that contains the pattern that's optionally supplied with the 
+        // path, separated by a colon
+        while(System.currentTimeMillis() < endTime) {
+            boolean errors = false;
+            for(String p : testPaths) {
+                final String [] s = p.split(":");
+                final String path = s[0];
+                final String pattern = (s.length > 0 ? s[1] : "");
+                try {
+                    executor.execute(builder.buildGetRequest(path))
+                    .assertStatus(200)
+                    .assertContentContains(pattern);
+                } catch(AssertionError ae) {
+                    errors = true;
+                    log.info("Request to {}{} failed, will retry ({})", 
+                            new Object[] { serverBaseUrl, path, ae});
+                } catch(Exception e) {
+                    errors = true;
+                    log.info("Request to {}{} failed, will retry ({})",
+                            new Object[] { serverBaseUrl, path, pattern, e });
+                }
+            }
+            
+            if(!errors) {
+                serverReady = true;
+                log.info("All {} paths return expected content, server ready", testPaths.size());
+                break;
+            }
+            Thread.sleep(1000L);
+        }
+        
+        if(!serverReady) {
+            serverReadyTestFailed = true;
+            final String msg = "Server not ready after " + timeoutSec + " seconds, giving up";
+            log.info(msg);
+            fail(msg);
+        }
+    }
+}
\ No newline at end of file

Propchange: sling/trunk/testing/sample/src/test/java/org/apache/sling/junit/sample/SlingTestBase.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sling/trunk/testing/sample/src/test/java/org/apache/sling/junit/sample/SlingTestBase.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev URL

Added: sling/trunk/testing/sample/src/test/java/org/apache/sling/junit/sample/httptests/OsgiConsoleTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/testing/sample/src/test/java/org/apache/sling/junit/sample/httptests/OsgiConsoleTest.java?rev=1070940&view=auto
==============================================================================
--- sling/trunk/testing/sample/src/test/java/org/apache/sling/junit/sample/httptests/OsgiConsoleTest.java (added)
+++ sling/trunk/testing/sample/src/test/java/org/apache/sling/junit/sample/httptests/OsgiConsoleTest.java Tue Feb 15 15:35:32 2011
@@ -0,0 +1,50 @@
+/*
+ * 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.sample.httptests;
+
+import org.apache.sling.junit.sample.SlingTestBase;
+import org.junit.Test;
+
+/** Simple HTTP test example, checks the validity of some
+ *  OSGi webconsole URLs by GETting them and checking
+ *  status code 200.
+ */
+public class OsgiConsoleTest extends SlingTestBase {
+    
+    @Test
+    public void testSomeConsolePaths() throws Exception {
+        final String [] subpaths = {
+                "bundles",
+                "components",
+                "configMgr",
+                "config",
+                "licenses",
+                "logs",
+                "memoryusage",
+                "services",
+                "shell"
+        };
+        
+        for(String subpath : subpaths) {
+            final String path = "/system/console/" + subpath;
+            executor.execute(
+                    builder.buildGetRequest(path)
+                    .withCredentials("admin", "admin")
+            ).assertStatus(200);
+        }
+    }
+}

Propchange: sling/trunk/testing/sample/src/test/java/org/apache/sling/junit/sample/httptests/OsgiConsoleTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sling/trunk/testing/sample/src/test/java/org/apache/sling/junit/sample/httptests/OsgiConsoleTest.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev URL