You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by bd...@apache.org on 2008/01/15 15:40:19 UTC

svn commit: r612127 - /incubator/sling/trunk/usling/usling-webapp/src/test/java/org/apache/sling/usling/webapp/integrationtest/UslingHttpTestBase.java

Author: bdelacretaz
Date: Tue Jan 15 06:40:18 2008
New Revision: 612127

URL: http://svn.apache.org/viewvc?rev=612127&view=rev
Log:
SLING-149 - merge usling into Sling - UslingHttpTestBase makes test HTTP requests to server to wait for Sling services to startup

Modified:
    incubator/sling/trunk/usling/usling-webapp/src/test/java/org/apache/sling/usling/webapp/integrationtest/UslingHttpTestBase.java

Modified: incubator/sling/trunk/usling/usling-webapp/src/test/java/org/apache/sling/usling/webapp/integrationtest/UslingHttpTestBase.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/usling/usling-webapp/src/test/java/org/apache/sling/usling/webapp/integrationtest/UslingHttpTestBase.java?rev=612127&r1=612126&r2=612127&view=diff
==============================================================================
--- incubator/sling/trunk/usling/usling-webapp/src/test/java/org/apache/sling/usling/webapp/integrationtest/UslingHttpTestBase.java (original)
+++ incubator/sling/trunk/usling/usling-webapp/src/test/java/org/apache/sling/usling/webapp/integrationtest/UslingHttpTestBase.java Tue Jan 15 06:40:18 2008
@@ -22,7 +22,11 @@
 import java.io.StringWriter;
 import java.net.MalformedURLException;
 import java.net.URL;
+import java.util.HashMap;
+import java.util.HashSet;
 import java.util.List;
+import java.util.Map;
+import java.util.Set;
 
 import junit.framework.TestCase;
 
@@ -57,7 +61,8 @@
     protected UslingIntegrationTestClient testClient;
     protected HttpClient httpClient;
     
-    private static long startupTime = System.currentTimeMillis(); 
+    private static boolean slingStartupOk; 
+    private static final long startupTime = System.currentTimeMillis();
     
     @Override
     protected void setUp() throws Exception {
@@ -83,14 +88,72 @@
         waitForSlingStartup();
     }
     
+    /** On the server side, initialization of Sling bundles is done 
+     *  asynchronously once the webapp is started. This method checks
+     *  that everything's ready on the server side, by calling an URL
+     *  that requires the UjaxPostServlet and the JCR repository to
+     *  work correctly.
+     */
     protected void waitForSlingStartup() throws Exception {
-        // TODO we'll need a better way to make sure integration tests
-        // wait for all bundles to be started!!!
-        final long delta = System.currentTimeMillis() - startupTime;
-        final long minWait = 5000L;
-        if(minWait > delta) {
-            Thread.sleep(minWait - delta);
+        // Use a static flag to make sure this runs only once in our test suite
+        if(slingStartupOk) {
+            return;
         }
+        
+        System.err.println("Checking if the required Sling services are started...");
+        
+        // Try creating a node on server, every 500msec, until ok, with timeout
+        final Set exceptionMessages = new HashSet();
+        final long maxMsecToWait = 10000L;
+        while(!slingStartupOk && (System.currentTimeMillis() < startupTime + maxMsecToWait) ) {
+            try {
+                slingStartupOk = slingServerReady();
+            } catch(Exception e) {
+                exceptionMessages.add(e.toString());
+                Thread.sleep(500L);
+            }
+        }
+        
+        if(!slingStartupOk) {
+            fail(
+                    "Server does not seem to be ready, after " + maxMsecToWait + " msec"
+                    + ", got the following Exceptions: " + exceptionMessages
+            );
+        }
+        
+        System.err.println("Sling services seem to be started, continuing with integration tests.");
+    }
+    
+    /** Return true if able to create and retrieve a node on server */
+    protected boolean slingServerReady() throws Exception {
+        // create a node on the server
+        final String time = String.valueOf(System.currentTimeMillis());
+        final String url = HTTP_BASE_URL + "/WaitForSlingStartup_" + time;
+        
+        // add some properties to the node
+        final Map<String,String> props = new HashMap<String,String>();
+        props.put("time", time);
+        
+        // POST, get URL of created node and get content 
+        final String urlOfNewNode = testClient.createNode(url, props, null, true);
+        final GetMethod get = new GetMethod(urlOfNewNode);
+        final int status = httpClient.executeMethod(get);
+        if(status!=200) {
+            throw new IOException("Expected status 200 but got " + status + " for URL=" + urlOfNewNode);
+        }
+        
+        final Header h = get.getResponseHeader("Content-Type");
+        final String contentType = h==null ? "" : h.getValue();
+        if(!contentType.startsWith("text/plain")) {
+            throw new IOException("Expected Content-Type=text/plain but got '" + contentType + "' for URL=" + urlOfNewNode);
+        }
+        
+        final String content = get.getResponseBodyAsString();
+        if(!content.contains(time)) {
+            throw new IOException("Content does not contain '" + time + "' (" + content + ")");
+        }
+        
+        return true;
     }
 
     /** Verify that given URL returns expectedStatusCode