SLING-3235 - additional readyness checks to cope with async config install at startup

git-svn-id: https://svn.apache.org/repos/asf/sling/trunk@1542223 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/src/main/java/org/apache/sling/commons/testing/integration/HttpTestBase.java b/src/main/java/org/apache/sling/commons/testing/integration/HttpTestBase.java
index bfe7ebb..51ab614 100644
--- a/src/main/java/org/apache/sling/commons/testing/integration/HttpTestBase.java
+++ b/src/main/java/org/apache/sling/commons/testing/integration/HttpTestBase.java
@@ -24,6 +24,7 @@
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
+import java.util.regex.Pattern;
 
 import javax.servlet.http.HttpServletResponse;
 
@@ -57,6 +58,9 @@
     public static final String HTTP_BASE_URL = removePath(HTTP_URL);
     public static final String WEBDAV_BASE_URL = removeEndingSlash(System.getProperty("launchpad.webdav.server.url", HTTP_BASE_URL));
     public static final String SERVLET_CONTEXT = removeEndingSlash(System.getProperty("launchpad.servlet.context", getPath(HTTP_URL)));
+    
+    public static final String READY_URL_PROP_PREFIX = "launchpad.ready.";
+    public static final int MAX_READY_URL_INDEX = 50;
 
     /** base path for test files */
     public static final String TEST_PATH = "/launchpad-integration-tests";
@@ -313,7 +317,31 @@
                 throw new IOException("Allow header (" + h.getValue() + " does not contain PROPFIND, at URL=" + webDavUrl);
             }
         }
-
+        
+        // And check optional additional URLs for readyness
+        // Defined by system properties like
+        //  launchpad.ready.1 = GET:/tmp/someUrl:200:expectedRegexpInResponse
+        {
+            for(int i=0; i <= MAX_READY_URL_INDEX ; i++) {
+                final String propName = READY_URL_PROP_PREFIX + i;
+                final String readyDef = System.getProperty(propName, "");
+                final String [] parts = readyDef.split(":");
+                if(parts.length == 4) {
+                    final String info = propName + "=" + readyDef;
+                    final HttpAnyMethod m = new HttpAnyMethod(parts[0],HTTP_BASE_URL + parts[1]);
+                    final int expectedStatus = Integer.valueOf(parts[2]);
+                    final int status = httpClient.executeMethod(m);
+                    if(expectedStatus != status) {
+                        throw new IOException("Status " + status + " does not match expected value: " + info); 
+                    }
+                    final String content = m.getResponseBodyAsString();
+                    final Pattern p = Pattern.compile("(?s).*" + parts[3] + ".*");
+                    if(!p.matcher(content).matches()) {
+                        throw new IOException("Content does not match expected regexp:" + info  + ", content=" + content);
+                    }
+                }
+            }
+        }
 
         return true;
     }
@@ -518,4 +546,4 @@
         }
         return content.toString();
     }
-}
+}
\ No newline at end of file