incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bdelacre...@apache.org
Subject svn commit: r1589362 - in /sling/trunk/contrib/crankstart: ./ launcher/ launcher/src/main/java/org/apache/sling/crankstart/launcher/ launcher/src/test/ launcher/src/test/java/ launcher/src/test/java/org/ launcher/src/test/java/org/apache/ launcher/src/...
Date Wed, 23 Apr 2014 09:56:11 GMT
Author: bdelacretaz
Date: Wed Apr 23 09:56:11 2014
New Revision: 1589362

URL: http://svn.apache.org/r1589362
Log:
CrankstartBootstrap and test

Added:
    sling/trunk/contrib/crankstart/launcher/src/main/java/org/apache/sling/crankstart/launcher/CrankstartBootstrap.java
    sling/trunk/contrib/crankstart/launcher/src/test/
    sling/trunk/contrib/crankstart/launcher/src/test/java/
    sling/trunk/contrib/crankstart/launcher/src/test/java/org/
    sling/trunk/contrib/crankstart/launcher/src/test/java/org/apache/
    sling/trunk/contrib/crankstart/launcher/src/test/java/org/apache/sling/
    sling/trunk/contrib/crankstart/launcher/src/test/java/org/apache/sling/crankstart/
    sling/trunk/contrib/crankstart/launcher/src/test/java/org/apache/sling/crankstart/launcher/
    sling/trunk/contrib/crankstart/launcher/src/test/java/org/apache/sling/crankstart/launcher/CrankstartBootstrapTest.java
Modified:
    sling/trunk/contrib/crankstart/   (props changed)
    sling/trunk/contrib/crankstart/launcher/   (props changed)
    sling/trunk/contrib/crankstart/launcher/pom.xml
    sling/trunk/contrib/crankstart/launcher/src/main/java/org/apache/sling/crankstart/launcher/Main.java

Propchange: sling/trunk/contrib/crankstart/
------------------------------------------------------------------------------
--- svn:ignore (original)
+++ svn:ignore Wed Apr 23 09:56:11 2014
@@ -9,3 +9,5 @@ bin
 .externalToolBuilders
 maven-eclipse.xml
 felix-cache
+sling-crankstart
+derby.log

Propchange: sling/trunk/contrib/crankstart/launcher/
------------------------------------------------------------------------------
--- svn:ignore (original)
+++ svn:ignore Wed Apr 23 09:56:11 2014
@@ -9,4 +9,5 @@ bin
 .externalToolBuilders
 maven-eclipse.xml
 felix-cache
-
+sling-crankstart
+derby.log

Modified: sling/trunk/contrib/crankstart/launcher/pom.xml
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/crankstart/launcher/pom.xml?rev=1589362&r1=1589361&r2=1589362&view=diff
==============================================================================
--- sling/trunk/contrib/crankstart/launcher/pom.xml (original)
+++ sling/trunk/contrib/crankstart/launcher/pom.xml Wed Apr 23 09:56:11 2014
@@ -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>

Added: sling/trunk/contrib/crankstart/launcher/src/main/java/org/apache/sling/crankstart/launcher/CrankstartBootstrap.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/crankstart/launcher/src/main/java/org/apache/sling/crankstart/launcher/CrankstartBootstrap.java?rev=1589362&view=auto
==============================================================================
--- sling/trunk/contrib/crankstart/launcher/src/main/java/org/apache/sling/crankstart/launcher/CrankstartBootstrap.java
(added)
+++ sling/trunk/contrib/crankstart/launcher/src/main/java/org/apache/sling/crankstart/launcher/CrankstartBootstrap.java
Wed Apr 23 09:56:11 2014
@@ -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

Modified: sling/trunk/contrib/crankstart/launcher/src/main/java/org/apache/sling/crankstart/launcher/Main.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/crankstart/launcher/src/main/java/org/apache/sling/crankstart/launcher/Main.java?rev=1589362&r1=1589361&r2=1589362&view=diff
==============================================================================
--- sling/trunk/contrib/crankstart/launcher/src/main/java/org/apache/sling/crankstart/launcher/Main.java
(original)
+++ sling/trunk/contrib/crankstart/launcher/src/main/java/org/apache/sling/crankstart/launcher/Main.java
Wed Apr 23 09:56:11 2014
@@ -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 @@ public class Main {
         } 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

Added: sling/trunk/contrib/crankstart/launcher/src/test/java/org/apache/sling/crankstart/launcher/CrankstartBootstrapTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/crankstart/launcher/src/test/java/org/apache/sling/crankstart/launcher/CrankstartBootstrapTest.java?rev=1589362&view=auto
==============================================================================
--- sling/trunk/contrib/crankstart/launcher/src/test/java/org/apache/sling/crankstart/launcher/CrankstartBootstrapTest.java
(added)
+++ sling/trunk/contrib/crankstart/launcher/src/test/java/org/apache/sling/crankstart/launcher/CrankstartBootstrapTest.java
Wed Apr 23 09:56:11 2014
@@ -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();
+    }
+}



Mime
View raw message