CrankstartBootstrap and test

git-svn-id: https://svn.apache.org/repos/asf/sling/trunk@1589362 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/pom.xml b/pom.xml
index a91b9f2..a61a847 100644
--- a/pom.xml
+++ b/pom.xml
@@ -121,5 +121,23 @@
             <version>${pax.url.version}</version>
             <scope>provided</scope>
         </dependency>
+        <dependency>
+            <groupId>junit</groupId>
+            <artifactId>junit</artifactId>
+            <version>4.11</version>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.sling</groupId>
+            <artifactId>org.apache.sling.commons.testing</artifactId>
+            <version>2.0.17-SNAPSHOT</version>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.httpcomponents</groupId>
+            <artifactId>httpclient</artifactId>
+            <version>4.1</version>
+            <scope>test</scope>
+        </dependency>
       </dependencies>
 </project>
diff --git a/src/main/java/org/apache/sling/crankstart/launcher/CrankstartBootstrap.java b/src/main/java/org/apache/sling/crankstart/launcher/CrankstartBootstrap.java
new file mode 100644
index 0000000..68ccd9d
--- /dev/null
+++ b/src/main/java/org/apache/sling/crankstart/launcher/CrankstartBootstrap.java
@@ -0,0 +1,106 @@
+/*
+ * 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.crankstart.launcher;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileReader;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.io.Reader;
+import java.net.URL;
+import java.net.URLClassLoader;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.Callable;
+
+import org.apache.sling.crankstart.api.CrankstartConstants;
+
+/** Execute a crankstart file */
+public class CrankstartBootstrap {
+    public static final String CLASSPATH_PREFIX = "classpath ";
+    private final String crankFile;
+    private final File tempFile;
+    
+    public CrankstartBootstrap(String filename) {
+        tempFile = null;
+        crankFile = filename;
+    }
+    
+    public CrankstartBootstrap(Reader r) throws IOException {
+        tempFile = File.createTempFile("CRANKSTART", "crank.txt");
+        tempFile.deleteOnExit();
+        crankFile = tempFile.getAbsolutePath();
+        
+        final FileWriter w = new FileWriter(tempFile);
+        final char [] buf = new char[4096];
+        int len = 0;
+        try {
+            while( (len = r.read(buf, 0, buf.length)) > 0) {
+                w.write(buf, 0, len);
+            }
+        } finally {
+            w.flush();
+            w.close();
+        }
+    }
+    
+    private void cleanup() {
+        if(tempFile != null) {
+            tempFile.delete();
+        }
+    }
+
+    
+    public void start() throws Exception {
+        System.setProperty(CrankstartConstants.CRANKSTART_INPUT_FILENAME, crankFile);
+        System.setProperty( "java.protocol.handler.pkgs", "org.ops4j.pax.url" );
+        final URL [] launcherClasspath = getClasspath(crankFile);
+        
+        final URLClassLoader launcherClassloader = new URLClassLoader(launcherClasspath, null);
+        
+        try {
+            final String callableClass = "org.apache.sling.crankstart.core.CrankstartFileProcessor";
+            
+            @SuppressWarnings("unchecked")
+            final Callable<Object> c = (Callable<Object>)launcherClassloader.loadClass(callableClass).newInstance();
+            c.call();
+        } finally {
+            launcherClassloader.close();
+            cleanup();
+        }
+    }
+    
+    private static URL[] getClasspath(String filename) throws IOException {
+        final List<URL> urls = new ArrayList<URL>();
+        final Reader input = new FileReader(new File(filename));
+        final BufferedReader r = new BufferedReader(input);
+        try {
+            String line = null;
+            while((line = r.readLine()) != null) {
+                if(line.length() == 0 || line.startsWith("#")) {
+                    // ignore comments and blank lines
+                } else if(line.startsWith(CLASSPATH_PREFIX)){
+                    urls.add(new URL(line.substring(CLASSPATH_PREFIX.length()).trim()));
+                }
+            }
+            return urls.toArray(new URL[] {});
+        } finally {
+            r.close();
+        }
+    }
+}
\ No newline at end of file
diff --git a/src/main/java/org/apache/sling/crankstart/launcher/Main.java b/src/main/java/org/apache/sling/crankstart/launcher/Main.java
index 225d859..8daf01f 100644
--- a/src/main/java/org/apache/sling/crankstart/launcher/Main.java
+++ b/src/main/java/org/apache/sling/crankstart/launcher/Main.java
@@ -16,19 +16,6 @@
  */
 package org.apache.sling.crankstart.launcher;
 
-import java.io.BufferedReader;
-import java.io.File;
-import java.io.FileReader;
-import java.io.IOException;
-import java.io.Reader;
-import java.net.URL;
-import java.net.URLClassLoader;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.concurrent.Callable;
-
-import org.apache.sling.crankstart.api.CrankstartConstants;
-
 /** Execute a crankstart file */
 public class Main {
     public static final String CLASSPATH_PREFIX = "classpath ";
@@ -41,34 +28,6 @@
         } else {
             crankFile = args[0];
         }
-        System.setProperty(CrankstartConstants.CRANKSTART_INPUT_FILENAME, crankFile);
-        System.setProperty( "java.protocol.handler.pkgs", "org.ops4j.pax.url" );
-        final URL [] launcherClasspath = getClasspath(crankFile);
-        
-        final URLClassLoader launcherClassloader = new URLClassLoader(launcherClasspath, null);
-        final String callableClass = "org.apache.sling.crankstart.core.CrankstartFileProcessor";
-        
-        @SuppressWarnings("unchecked")
-        final Callable<Object> c = (Callable<Object>)launcherClassloader.loadClass(callableClass).newInstance();
-        c.call();
-    }
-    
-    private static URL[] getClasspath(String filename) throws IOException {
-        final List<URL> urls = new ArrayList<URL>();
-        final Reader input = new FileReader(new File(filename));
-        final BufferedReader r = new BufferedReader(input);
-        try {
-            String line = null;
-            while((line = r.readLine()) != null) {
-                if(line.length() == 0 || line.startsWith("#")) {
-                    // ignore comments and blank lines
-                } else if(line.startsWith(CLASSPATH_PREFIX)){
-                    urls.add(new URL(line.substring(CLASSPATH_PREFIX.length()).trim()));
-                }
-            }
-            return urls.toArray(new URL[] {});
-        } finally {
-            r.close();
-        }
+        new CrankstartBootstrap(crankFile).start();
     }
 }
\ No newline at end of file
diff --git a/src/test/java/org/apache/sling/crankstart/launcher/CrankstartBootstrapTest.java b/src/test/java/org/apache/sling/crankstart/launcher/CrankstartBootstrapTest.java
new file mode 100644
index 0000000..c559290
--- /dev/null
+++ b/src/test/java/org/apache/sling/crankstart/launcher/CrankstartBootstrapTest.java
@@ -0,0 +1,106 @@
+package org.apache.sling.crankstart.launcher;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.fail;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.StringReader;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.Random;
+
+import org.apache.commons.httpclient.HttpClient;
+import org.apache.commons.httpclient.methods.GetMethod;
+import org.apache.sling.commons.testing.junit.Retry;
+import org.apache.sling.commons.testing.junit.RetryRule;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Rule;
+import org.junit.Test;
+
+/** Verify that we can start the Felix HTTP service
+ *  with a {@link CrankstartBootstrap}. 
+ */
+public class CrankstartBootstrapTest {
+    
+    private static final int port = Integer.valueOf(System.getProperty("test.http.port", "12345"));
+    private static final HttpClient client = new HttpClient();
+    private static Thread crankstartThread;
+    private static URL rootUrl = null;
+            
+    static {
+        try {
+            rootUrl = new URL("http://localhost:" + port + "/");
+        } catch(MalformedURLException mfe) {
+            fail(mfe.toString());
+        }
+    }
+    
+    @Rule
+    public final RetryRule retryRule = new RetryRule();
+    
+    private final static String CRANKSTART = 
+        "classpath mvn:org.apache.felix/org.apache.felix.framework/4.4.0\n"
+        + "classpath mvn:org.slf4j/slf4j-api/1.6.2\n"
+        + "classpath mvn:org.ops4j.pax.url/pax-url-aether/1.6.0\n"
+        + "classpath mvn:org.ops4j.pax.url/pax-url-commons/1.6.0\n"
+        + "classpath mvn:org.apache.sling/org.apache.sling.crankstart.core/0.0.1-SNAPSHOT\n"
+        + "classpath mvn:org.apache.sling/org.apache.sling.crankstart.api/0.0.1-SNAPSHOT\n"
+        + "osgi.property org.osgi.service.http.port " + port + "\n"
+        + "osgi.property org.osgi.framework.storage " + getOsgiStoragePath() + "\n"
+        + "start.framework\n"
+        + "bundle mvn:org.apache.felix/org.apache.felix.http.jetty/2.2.0\n"
+        + "bundle mvn:org.apache.sling/org.apache.sling.commons.log/2.1.2\n"
+        + "start.all.bundles\n"
+        + "log felix http service should come up at http://localhost:" + port + "\n"
+    ;
+    
+    @BeforeClass
+    public static void setup() {
+        final GetMethod get = new GetMethod(rootUrl.toExternalForm());
+        
+        try {
+            client.executeMethod(get);
+            fail("Expecting connection to " + port + " to fail before starting HTTP service");
+        } catch(IOException expected) {
+        }
+        
+        crankstartThread = new Thread() {
+            public void run() {
+                try {
+                    new CrankstartBootstrap(new StringReader(CRANKSTART)).start();
+                } catch(Exception e) {
+                    fail("CrankstartBootstrap exception:" + e);
+                }
+            }
+        };
+        crankstartThread.setDaemon(true);
+        crankstartThread.start();
+    }
+    
+    @AfterClass
+    public static void cleanup() throws InterruptedException {
+        crankstartThread.interrupt();
+        crankstartThread.join();
+    }
+    
+    @Test
+    @Retry(timeoutMsec=10000, intervalMsec=250)
+    public void testHttpResponse() throws Exception {
+        final GetMethod get = new GetMethod(rootUrl.toExternalForm());
+        client.executeMethod(get);
+        assertEquals("Expecting 404 at " + get.getURI(), 404, get.getStatusCode());
+    }
+    
+    private static String getOsgiStoragePath() {
+        final File tmpRoot = new File(System.getProperty("java.io.tmpdir"));
+        final Random random = new Random();
+        final File tmpFolder = new File(tmpRoot, System.currentTimeMillis() + "_" + random.nextInt());
+        if(!tmpFolder.mkdir()) {
+            fail("Failed to create " + tmpFolder.getAbsolutePath());
+        }
+        tmpFolder.deleteOnExit();
+        return tmpFolder.getAbsolutePath();
+    }
+}