incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bdelacre...@apache.org
Subject svn commit: r1076217 - in /sling/trunk/testing: junit/core/ junit/core/src/main/java/org/apache/sling/junit/ samples/integration-tests/src/test/java/org/apache/sling/testing/samples/testtools/ samples/integration-tests/src/test/java/org/apache/sling/te...
Date Wed, 02 Mar 2011 13:43:59 GMT
Author: bdelacretaz
Date: Wed Mar  2 13:43:58 2011
New Revision: 1076217

URL: http://svn.apache.org/viewvc?rev=1076217&view=rev
Log:
SLING-1981 - move TimeoutsProvider and SlingTestBase to tools module, and improve SlingTestBase's
server startup mechanism

Added:
    sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools/sling/SlingTestBase.java
      - copied, changed from r1076194, 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/sling/TimeoutsProvider.java
      - copied, changed from r1076194, sling/trunk/testing/junit/core/src/main/java/org/apache/sling/junit/TimeoutsProvider.java
Removed:
    sling/trunk/testing/junit/core/src/main/java/org/apache/sling/junit/TimeoutsProvider.java
    sling/trunk/testing/samples/integration-tests/src/test/java/org/apache/sling/testing/samples/testtools/SlingTestBase.java
Modified:
    sling/trunk/testing/junit/core/pom.xml
    sling/trunk/testing/samples/integration-tests/src/test/java/org/apache/sling/testing/samples/testtools/http/OsgiConsoleTest.java
    sling/trunk/testing/samples/integration-tests/src/test/java/org/apache/sling/testing/samples/testtools/serverside/FailingTestsTest.java
    sling/trunk/testing/samples/integration-tests/src/test/java/org/apache/sling/testing/samples/testtools/serverside/JSONResponseTest.java
    sling/trunk/testing/samples/integration-tests/src/test/java/org/apache/sling/testing/samples/testtools/serverside/ServerSideSampleTest.java
    sling/trunk/testing/samples/integration-tests/src/test/java/org/apache/sling/testing/samples/testtools/serverside/ServerSideTestsBase.java
    sling/trunk/testing/samples/integration-tests/src/test/java/org/apache/sling/testing/samples/testtools/serverside/SingleServerSideMethodTest.java
    sling/trunk/testing/samples/integration-tests/src/test/java/org/apache/sling/testing/samples/testtools/serverside/scriptable/ScriptableTestsTest.java

Modified: sling/trunk/testing/junit/core/pom.xml
URL: http://svn.apache.org/viewvc/sling/trunk/testing/junit/core/pom.xml?rev=1076217&r1=1076216&r2=1076217&view=diff
==============================================================================
--- sling/trunk/testing/junit/core/pom.xml (original)
+++ sling/trunk/testing/junit/core/pom.xml Wed Mar  2 13:43:58 2011
@@ -31,7 +31,7 @@
     <packaging>bundle</packaging>
 
     <name>Apache Sling JUnit Core</name>
-    <description>Runs JUnit tests in an OSGi framework</description>
+    <description>Runs JUnit tests in an OSGi framework and provides the JUnit libraries</description>
     
     <properties>
         <junit.version>4.8.2</junit.version>

Modified: sling/trunk/testing/samples/integration-tests/src/test/java/org/apache/sling/testing/samples/testtools/http/OsgiConsoleTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/testing/samples/integration-tests/src/test/java/org/apache/sling/testing/samples/testtools/http/OsgiConsoleTest.java?rev=1076217&r1=1076216&r2=1076217&view=diff
==============================================================================
--- sling/trunk/testing/samples/integration-tests/src/test/java/org/apache/sling/testing/samples/testtools/http/OsgiConsoleTest.java
(original)
+++ sling/trunk/testing/samples/integration-tests/src/test/java/org/apache/sling/testing/samples/testtools/http/OsgiConsoleTest.java
Wed Mar  2 13:43:58 2011
@@ -16,7 +16,7 @@
  */
 package org.apache.sling.testing.samples.testtools.http;
 
-import org.apache.sling.testing.samples.testtools.SlingTestBase;
+import org.apache.sling.testing.tools.sling.SlingTestBase;
 import org.junit.Test;
 
 /** Simple HTTP test example, checks the validity of some
@@ -41,8 +41,8 @@ public class OsgiConsoleTest extends Sli
         
         for(String subpath : subpaths) {
             final String path = "/system/console/" + subpath;
-            executor.execute(
-                    builder.buildGetRequest(path)
+            getRequestExecutor().execute(
+                    getRequestBuilder().buildGetRequest(path)
                     .withCredentials("admin", "admin")
             ).assertStatus(200);
         }

Modified: sling/trunk/testing/samples/integration-tests/src/test/java/org/apache/sling/testing/samples/testtools/serverside/FailingTestsTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/testing/samples/integration-tests/src/test/java/org/apache/sling/testing/samples/testtools/serverside/FailingTestsTest.java?rev=1076217&r1=1076216&r2=1076217&view=diff
==============================================================================
--- sling/trunk/testing/samples/integration-tests/src/test/java/org/apache/sling/testing/samples/testtools/serverside/FailingTestsTest.java
(original)
+++ sling/trunk/testing/samples/integration-tests/src/test/java/org/apache/sling/testing/samples/testtools/serverside/FailingTestsTest.java
Wed Mar  2 13:43:58 2011
@@ -58,11 +58,11 @@ public class FailingTestsTest extends Se
     public void testFailures() throws Exception{
         
         // Execute tests from the failingtests bundle and verify response
-        final Request r = builder.buildPostRequest(JUNIT_SERVLET_PATH 
+        final Request r = getRequestBuilder().buildPostRequest(JUNIT_SERVLET_PATH 
                 + "/org.apache.sling.testing.samples.failingtests.json");
-        executor.execute(r).assertStatus(200);
+        getRequestExecutor().execute(r).assertStatus(200);
         
-        final JSONArray json = new JSONArray(new JSONTokener((executor.getContent())));
+        final JSONArray json = new JSONArray(new JSONTokener((getRequestExecutor().getContent())));
         
         assertEquals(
                 "initializationError(org.apache.sling.testing.samples.failingtests.EmptyTest):
No runnable methods",

Modified: sling/trunk/testing/samples/integration-tests/src/test/java/org/apache/sling/testing/samples/testtools/serverside/JSONResponseTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/testing/samples/integration-tests/src/test/java/org/apache/sling/testing/samples/testtools/serverside/JSONResponseTest.java?rev=1076217&r1=1076216&r2=1076217&view=diff
==============================================================================
--- sling/trunk/testing/samples/integration-tests/src/test/java/org/apache/sling/testing/samples/testtools/serverside/JSONResponseTest.java
(original)
+++ sling/trunk/testing/samples/integration-tests/src/test/java/org/apache/sling/testing/samples/testtools/serverside/JSONResponseTest.java
Wed Mar  2 13:43:58 2011
@@ -25,9 +25,9 @@ import org.apache.sling.commons.json.JSO
 import org.apache.sling.commons.json.JSONException;
 import org.apache.sling.commons.json.JSONObject;
 import org.apache.sling.commons.json.JSONTokener;
-import org.apache.sling.junit.TimeoutsProvider;
 import org.apache.sling.testing.tools.http.Request;
 import org.apache.sling.testing.tools.retry.RetryLoop;
+import org.apache.sling.testing.tools.sling.TimeoutsProvider;
 import org.junit.Test;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -80,15 +80,15 @@ public class JSONResponseTest extends Se
     }
 
     private void testJsonListOfTests() throws Exception {
-        Request r = builder.buildGetRequest(JUNIT_SERVLET_PATH + "/.json");
+        Request r = getRequestBuilder().buildGetRequest(JUNIT_SERVLET_PATH + "/.json");
         
         // Get list of tests in JSON format
-        executor.execute(r)
+        getRequestExecutor().execute(r)
         .assertStatus(200)
         .assertContentType("application/json");
         
         // Parse JSON response for more precise testing
-        final JSONArray json = new JSONArray(new JSONTokener((executor.getContent())));
+        final JSONArray json = new JSONArray(new JSONTokener((getRequestExecutor().getContent())));
         
         // Verify that some test names are present in the response
         final List<String> expectedTestNames = Arrays.asList(new String []{

Modified: sling/trunk/testing/samples/integration-tests/src/test/java/org/apache/sling/testing/samples/testtools/serverside/ServerSideSampleTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/testing/samples/integration-tests/src/test/java/org/apache/sling/testing/samples/testtools/serverside/ServerSideSampleTest.java?rev=1076217&r1=1076216&r2=1076217&view=diff
==============================================================================
--- sling/trunk/testing/samples/integration-tests/src/test/java/org/apache/sling/testing/samples/testtools/serverside/ServerSideSampleTest.java
(original)
+++ sling/trunk/testing/samples/integration-tests/src/test/java/org/apache/sling/testing/samples/testtools/serverside/ServerSideSampleTest.java
Wed Mar  2 13:43:58 2011
@@ -20,7 +20,7 @@ import org.apache.sling.junit.remote.tes
 import org.apache.sling.junit.remote.testrunner.SlingRemoteTestRunner;
 import org.junit.runner.RunWith;
 
-/** Verify that getExpectedNumberOfTests() < 0 means "don't care" */
+/** Run some server-side tests */
 @RunWith(SlingRemoteTestRunner.class)
 public class ServerSideSampleTest extends ServerSideTestsBase implements SlingRemoteTestParameters
{
     
@@ -32,7 +32,7 @@ public class ServerSideSampleTest extend
     }
     
     public String getJunitServletUrl() {
-        return serverBaseUrl + JUNIT_SERVLET_PATH;
+        return getServerBaseUrl() + JUNIT_SERVLET_PATH;
     }
 
     public String getTestClassesSelector() {

Modified: sling/trunk/testing/samples/integration-tests/src/test/java/org/apache/sling/testing/samples/testtools/serverside/ServerSideTestsBase.java
URL: http://svn.apache.org/viewvc/sling/trunk/testing/samples/integration-tests/src/test/java/org/apache/sling/testing/samples/testtools/serverside/ServerSideTestsBase.java?rev=1076217&r1=1076216&r2=1076217&view=diff
==============================================================================
--- sling/trunk/testing/samples/integration-tests/src/test/java/org/apache/sling/testing/samples/testtools/serverside/ServerSideTestsBase.java
(original)
+++ sling/trunk/testing/samples/integration-tests/src/test/java/org/apache/sling/testing/samples/testtools/serverside/ServerSideTestsBase.java
Wed Mar  2 13:43:58 2011
@@ -16,12 +16,13 @@
  */
 package org.apache.sling.testing.samples.testtools.serverside;
 
-import org.apache.sling.junit.TimeoutsProvider;
-import org.apache.sling.testing.samples.testtools.SlingTestBase;
+import static org.junit.Assert.fail;
+
 import org.apache.sling.testing.tools.retry.RetryLoop;
+import org.apache.sling.testing.tools.sling.SlingTestBase;
+import org.apache.sling.testing.tools.sling.TimeoutsProvider;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
-import static org.junit.Assert.fail;
 
 /** Base class for tests that require the server-side test bundles
  *  to be active.
@@ -34,7 +35,9 @@ public class ServerSideTestsBase extends
     private final Logger log = LoggerFactory.getLogger(getClass());
     public static final int JUNIT_SERVLET_TIMEOUT_SECONDS = TimeoutsProvider.getInstance().getTimeout(60);
 
-    protected ServerSideTestsBase() {
+    @Override
+    protected void onServerReady(boolean serverStartedByThisClass) throws Exception {
+        super.onServerReady(serverStartedByThisClass);
         try {
             checkJunitServletPresent();
         } catch(Exception e) {
@@ -59,8 +62,8 @@ public class ServerSideTestsBase extends
             }
 
             public boolean isTrue() throws Exception {
-                executor.execute(
-                        builder.buildGetRequest(JUNIT_SERVLET_PATH))
+                getRequestExecutor().execute(
+                        getRequestBuilder().buildGetRequest(JUNIT_SERVLET_PATH))
                 .assertStatus(expectedStatus);
                 return true;
             }

Modified: sling/trunk/testing/samples/integration-tests/src/test/java/org/apache/sling/testing/samples/testtools/serverside/SingleServerSideMethodTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/testing/samples/integration-tests/src/test/java/org/apache/sling/testing/samples/testtools/serverside/SingleServerSideMethodTest.java?rev=1076217&r1=1076216&r2=1076217&view=diff
==============================================================================
--- sling/trunk/testing/samples/integration-tests/src/test/java/org/apache/sling/testing/samples/testtools/serverside/SingleServerSideMethodTest.java
(original)
+++ sling/trunk/testing/samples/integration-tests/src/test/java/org/apache/sling/testing/samples/testtools/serverside/SingleServerSideMethodTest.java
Wed Mar  2 13:43:58 2011
@@ -33,7 +33,7 @@ public class SingleServerSideMethodTest 
     }
 
     public String getJunitServletUrl() {
-        return serverBaseUrl + JUNIT_SERVLET_PATH;
+        return getServerBaseUrl() + JUNIT_SERVLET_PATH;
     }
 
     public String getTestClassesSelector() {

Modified: 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=1076217&r1=1076216&r2=1076217&view=diff
==============================================================================
--- sling/trunk/testing/samples/integration-tests/src/test/java/org/apache/sling/testing/samples/testtools/serverside/scriptable/ScriptableTestsTest.java
(original)
+++ sling/trunk/testing/samples/integration-tests/src/test/java/org/apache/sling/testing/samples/testtools/serverside/scriptable/ScriptableTestsTest.java
Wed Mar  2 13:43:58 2011
@@ -58,8 +58,8 @@ public class ScriptableTestsTest extends
     
     @Before
     public void setupContent() throws Exception {
-        slingClient = new SlingClient(serverBaseUrl, ADMIN, ADMIN);
-        testClient = new RemoteTestHttpClient(serverBaseUrl + JUNIT_SERVLET_PATH, true);
+        slingClient = new SlingClient(getServerBaseUrl(), ADMIN, ADMIN);
+        testClient = new RemoteTestHttpClient(getServerBaseUrl() + JUNIT_SERVLET_PATH, true);
         
         cleanupContent();
 

Copied: sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools/sling/SlingTestBase.java
(from r1076194, 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/tools/src/main/java/org/apache/sling/testing/tools/sling/SlingTestBase.java?p2=sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools/sling/SlingTestBase.java&p1=sling/trunk/testing/samples/integration-tests/src/test/java/org/apache/sling/testing/samples/testtools/SlingTestBase.java&r1=1076194&r2=1076217&rev=1076217&view=diff
==============================================================================
--- sling/trunk/testing/samples/integration-tests/src/test/java/org/apache/sling/testing/samples/testtools/SlingTestBase.java
(original)
+++ sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools/sling/SlingTestBase.java
Wed Mar  2 13:43:58 2011
@@ -14,7 +14,7 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package org.apache.sling.testing.samples.testtools;
+package org.apache.sling.testing.tools.sling;
 
 import static org.junit.Assert.fail;
 
@@ -24,8 +24,8 @@ import java.util.Collections;
 import java.util.List;
 import java.util.TreeSet;
 
+import org.apache.http.client.HttpClient;
 import org.apache.http.impl.client.DefaultHttpClient;
-import org.apache.sling.junit.TimeoutsProvider;
 import org.apache.sling.testing.tools.http.RequestBuilder;
 import org.apache.sling.testing.tools.http.RequestExecutor;
 import org.apache.sling.testing.tools.jarexec.JarExecutor;
@@ -45,29 +45,52 @@ public class SlingTestBase {
     public static final String BUNDLE_TO_INSTALL_PREFIX = "sling.additional.bundle";
     public static final String ADMIN = "admin";
     
-    protected static String serverBaseUrl;
-    protected static RequestBuilder builder;
-    protected static DefaultHttpClient httpClient = new DefaultHttpClient();
-    protected static RequestExecutor executor = new RequestExecutor(httpClient);
-    protected static WebconsoleClient webconsoleClient;
+    private static final boolean keepJarRunning = "true".equals(System.getProperty(KEEP_JAR_RUNNING_PROP));
+    private static String serverBaseUrl;
+    private static RequestBuilder builder;
+    private static DefaultHttpClient httpClient = new DefaultHttpClient();
+    private static RequestExecutor executor = new RequestExecutor(httpClient);
+    private static WebconsoleClient webconsoleClient;
     private static boolean serverStarted;
     private static boolean serverStartedByThisClass;
     private static boolean serverReady;
     private static boolean serverReadyTestFailed;
     private static final Logger log = LoggerFactory.getLogger(SlingTestBase.class);
-    
-    protected SlingTestBase() {
+
+    /** Start the server, if not done yet */
+    private void startServer() {
         try {
             startRunnableJar();
+            builder = new RequestBuilder(serverBaseUrl);
+            webconsoleClient = new WebconsoleClient(serverBaseUrl, ADMIN, ADMIN);
+            builder = new RequestBuilder(serverBaseUrl);
             waitForServerReady();
+            onServerReady(serverStartedByThisClass);
             blockIfRequested();
         } catch(Exception e) {
-            throw new IllegalStateException("Exception in SlingTestBase constructor", e);
+            log.error("Exception in maybeStartServer()", e);
+            fail("maybeStartServer() failed: " + e);
+        }
+    }
+    
+    /** Start server if needed, and return a RequestBuilder that points to it */
+    protected RequestBuilder getRequestBuilder() {
+        if(builder == null) {
+            startServer();
         }
+        return builder;
+    }
+
+    /** Start server if needed, and return its base URL */
+    protected String getServerBaseUrl() {
+        if(serverBaseUrl == null) {
+            startServer();
+        }
+        return serverBaseUrl;
     }
 
     /** Start the configured runnable jar and initialize our http client */
-    private synchronized void startRunnableJar() throws Exception {
+    protected synchronized void startRunnableJar() throws Exception {
         if(serverStarted) {
             return;
         }
@@ -85,15 +108,13 @@ public class SlingTestBase {
         }
         
         serverStarted = true;
-        builder = new RequestBuilder(serverBaseUrl);
-        webconsoleClient = new WebconsoleClient(serverBaseUrl, ADMIN, ADMIN);
     }
     
     /** Optionally block here so that the runnable jar stays up - we can 
      *  then run tests against it from another VM.
      */
-    private void blockIfRequested() {
-        if ("true".equals(System.getProperty(KEEP_JAR_RUNNING_PROP))) {
+    protected void blockIfRequested() {
+        if (keepJarRunning) {
             log.info(KEEP_JAR_RUNNING_PROP + " set to true - entering infinite loop"
                      + " so that runnable jar stays up. Kill this process to exit.");
             synchronized (this) {
@@ -107,7 +128,7 @@ public class SlingTestBase {
     }
     
     /** Check a number of server URLs for readyness */
-    private void waitForServerReady() throws Exception {
+    protected void waitForServerReady() throws Exception {
         if(serverReady) {
             return;
         }
@@ -165,9 +186,7 @@ public class SlingTestBase {
             Thread.sleep(TimeoutsProvider.getInstance().getTimeout(1000L));
         }
         
-        if(serverReady) {
-            onServerReady();
-        } else {
+        if(!serverReady) {
             serverReadyTestFailed = true;
             final String msg = "Server not ready after " + timeoutSec + " seconds, giving
up";
             log.info(msg);
@@ -178,8 +197,11 @@ public class SlingTestBase {
     /** Called once when the server is found to be ready, can be used for additional
      *  server setup (extra bundles etc.). If overridden, must be called by overriding
      *  method. 
+     *  
+     *  @param serverStartedByThisClass true if we started the server, in which case
+     *      additional setup might be needed
      */
-    protected void onServerReady() throws Exception {
+    protected void onServerReady(boolean serverStartedByThisClass) throws Exception {
         if(serverStartedByThisClass) {
             installExtraBundles();
         } else {
@@ -241,4 +263,19 @@ public class SlingTestBase {
     protected boolean isServerStartedByThisClass() {
         return serverStartedByThisClass;
     }
+    
+    protected HttpClient getHttpClient() {
+        return httpClient;
+    }
+    
+    protected RequestExecutor getRequestExecutor() {
+        return executor;
+    }
+    
+    protected WebconsoleClient getWebconsoleClient() {
+        if(webconsoleClient == null) {
+            startServer();
+        }
+        return webconsoleClient;
+    }
 }
\ No newline at end of file

Copied: sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools/sling/TimeoutsProvider.java
(from r1076194, sling/trunk/testing/junit/core/src/main/java/org/apache/sling/junit/TimeoutsProvider.java)
URL: http://svn.apache.org/viewvc/sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools/sling/TimeoutsProvider.java?p2=sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools/sling/TimeoutsProvider.java&p1=sling/trunk/testing/junit/core/src/main/java/org/apache/sling/junit/TimeoutsProvider.java&r1=1076194&r2=1076217&rev=1076217&view=diff
==============================================================================
--- sling/trunk/testing/junit/core/src/main/java/org/apache/sling/junit/TimeoutsProvider.java
(original)
+++ sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools/sling/TimeoutsProvider.java
Wed Mar  2 13:43:58 2011
@@ -14,7 +14,7 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package org.apache.sling.junit;
+package org.apache.sling.testing.tools.sling;
 
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;



Mime
View raw message