Author: bdelacretaz
Date: Fri Jun 12 11:33:15 2015
New Revision: 1685068

URL: http://svn.apache.org/r1685068
Log:
SLING-4728 - prepare for run mode tests

Added:
    
sling/trunk/contrib/crankstart/launcher/src/test/java/org/apache/sling/crankstart/launcher/RunModeATest.java
Modified:
    
sling/trunk/contrib/crankstart/launcher/src/test/java/org/apache/sling/crankstart/launcher/CrankstartBootstrapTest.java
    
sling/trunk/contrib/crankstart/launcher/src/test/java/org/apache/sling/crankstart/launcher/CrankstartSetup.java
    
sling/trunk/contrib/crankstart/launcher/src/test/java/org/apache/sling/crankstart/launcher/U.java
    
sling/trunk/contrib/crankstart/launcher/src/test/resources/provisioning-model/crankstart-tests.txt

Modified: 
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=1685068&r1=1685067&r2=1685068&view=diff
==============================================================================
--- 
sling/trunk/contrib/crankstart/launcher/src/test/java/org/apache/sling/crankstart/launcher/CrankstartBootstrapTest.java
 (original)
+++ 
sling/trunk/contrib/crankstart/launcher/src/test/java/org/apache/sling/crankstart/launcher/CrankstartBootstrapTest.java
 Fri Jun 12 11:33:15 2015
@@ -1,13 +1,11 @@
 package org.apache.sling.crankstart.launcher;
 
 import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 
 import java.io.IOException;
 
-import org.apache.commons.io.IOUtils;
 import org.apache.http.HttpResponse;
 import org.apache.http.client.methods.HttpGet;
 import org.apache.http.client.methods.HttpUriRequest;
@@ -18,6 +16,7 @@ import org.apache.sling.commons.testing.
 import org.apache.sling.commons.testing.junit.RetryRule;
 import org.apache.sling.testing.tools.osgi.WebconsoleClient;
 import org.junit.Before;
+import org.junit.BeforeClass;
 import org.junit.Rule;
 import org.junit.Test;
 
@@ -26,25 +25,27 @@ import org.junit.Test;
  */
 public class CrankstartBootstrapTest {
     
-    private static final CrankstartSetup C = new CrankstartSetup();
-    public static final int LONG_TIMEOUT_SECONDS = 10;
-    public static final int LONG_TIMEOUT_MSEC = LONG_TIMEOUT_SECONDS * 1000;
-    public static final int STD_INTERVAL = 250;
+    private static CrankstartSetup C;
     private DefaultHttpClient client;
-    private WebconsoleClient osgiConsole;
+    private static WebconsoleClient osgiConsole;
     
     @Rule
     public final RetryRule retryRule = new RetryRule();
     
-    @Before
-    public void setupHttpClient() throws IOException {
+    @BeforeClass
+    public static void setupClass() throws IOException {
+        C = new CrankstartSetup();
         C.setup();
+        osgiConsole = new WebconsoleClient(C.getBaseUrl(), U.ADMIN, U.ADMIN);
+    }
+    
+    @Before
+    public void setup() throws IOException {
         client = new DefaultHttpClient();
-        osgiConsole = new WebconsoleClient(C.getBaseUrl(), "admin", "admin");
     }
     
     @Test
-    @Retry(timeoutMsec=CrankstartBootstrapTest.LONG_TIMEOUT_MSEC, 
intervalMsec=CrankstartBootstrapTest.STD_INTERVAL)
+    @Retry(timeoutMsec=U.LONG_TIMEOUT_MSEC, intervalMsec=U.STD_INTERVAL)
     public void testHttpRoot() throws Exception {
         final HttpUriRequest get = new HttpGet(C.getBaseUrl());
         HttpResponse response = null;
@@ -57,7 +58,7 @@ public class CrankstartBootstrapTest {
     }
     
     @Test
-    @Retry(timeoutMsec=CrankstartBootstrapTest.LONG_TIMEOUT_MSEC, 
intervalMsec=CrankstartBootstrapTest.STD_INTERVAL)
+    @Retry(timeoutMsec=U.LONG_TIMEOUT_MSEC, intervalMsec=U.STD_INTERVAL)
     public void testSingleConfigServlet() throws Exception {
         final HttpUriRequest get = new HttpGet(C.getBaseUrl() + "/single");
         HttpResponse response = null;
@@ -70,7 +71,7 @@ public class CrankstartBootstrapTest {
     }
     
     @Test
-    @Retry(timeoutMsec=CrankstartBootstrapTest.LONG_TIMEOUT_MSEC, 
intervalMsec=CrankstartBootstrapTest.STD_INTERVAL)
+    @Retry(timeoutMsec=U.LONG_TIMEOUT_MSEC, intervalMsec=U.STD_INTERVAL)
     public void testConfigFactoryServlet() throws Exception {
         final String [] paths = { "/foo", "/bar/test" };
         for(String path : paths) {
@@ -86,7 +87,7 @@ public class CrankstartBootstrapTest {
     }
     
     @Test
-    @Retry(timeoutMsec=CrankstartBootstrapTest.LONG_TIMEOUT_MSEC, 
intervalMsec=CrankstartBootstrapTest.STD_INTERVAL)
+    @Retry(timeoutMsec=U.LONG_TIMEOUT_MSEC, intervalMsec=U.STD_INTERVAL)
     public void testJUnitServlet() throws Exception {
         final String path = "/system/sling/junit";
         final HttpUriRequest get = new HttpGet(C.getBaseUrl() + path);
@@ -108,7 +109,7 @@ public class CrankstartBootstrapTest {
         
         for(String name : addBundles) {
             try {
-                osgiConsole.checkBundleInstalled(name, 
CrankstartBootstrapTest.LONG_TIMEOUT_SECONDS);
+                osgiConsole.checkBundleInstalled(name, U.LONG_TIMEOUT_SECONDS);
             } catch(AssertionError ae) {
                 fail("Expected bundle to be present:" + name);
             }
@@ -116,7 +117,7 @@ public class CrankstartBootstrapTest {
     }
     
     @Test
-    @Retry(timeoutMsec=CrankstartBootstrapTest.LONG_TIMEOUT_MSEC, 
intervalMsec=CrankstartBootstrapTest.STD_INTERVAL)
+    @Retry(timeoutMsec=U.LONG_TIMEOUT_MSEC, intervalMsec=U.STD_INTERVAL)
     public void testSpecificStartLevel() throws Exception {
         // This bundle should only be installed, as it's set to start level 99
         final String symbolicName = "org.apache.commons.collections";
@@ -142,39 +143,20 @@ public class CrankstartBootstrapTest {
     }
     
     @Test
-    @Retry(timeoutMsec=CrankstartBootstrapTest.LONG_TIMEOUT_MSEC, 
intervalMsec=CrankstartBootstrapTest.STD_INTERVAL)
+    @Retry(timeoutMsec=U.LONG_TIMEOUT_MSEC, intervalMsec=U.STD_INTERVAL)
     public void testEmptyConfig() throws Exception {
         U.setAdminCredentials(client);
-        assertHttpGet(
+        U.assertHttpGet(C, client,
             "/test/config/empty.config.should.work", 
             
"empty.config.should.work#service.pid=(String)empty.config.should.work##EOC#");
     }
         
     @Test
-    @Retry(timeoutMsec=CrankstartBootstrapTest.LONG_TIMEOUT_MSEC, 
intervalMsec=CrankstartBootstrapTest.STD_INTERVAL)
+    @Retry(timeoutMsec=U.LONG_TIMEOUT_MSEC, intervalMsec=U.STD_INTERVAL)
     public void testFelixFormatConfig() throws Exception {
         U.setAdminCredentials(client);
-        assertHttpGet(
+        U.assertHttpGet(C, client,
                 "/test/config/felix.format.test", 
                 "felix.format.test#array=(String[])[foo, 
bar.from.launcher.test]#mongouri=(String)mongodb://localhost:27017#service.pid=(String)felix.format.test#service.ranking.launcher.test=(Integer)54321##EOC#");
     }
-    
-    private void assertHttpGet(String path, String expectedContent) throws 
Exception {
-        final HttpUriRequest get = new HttpGet(C.getBaseUrl() + path);
-        HttpResponse response = null;
-        try {
-            response = client.execute(get);
-            assertEquals("Expecting 200 response at " + path, 200, 
response.getStatusLine().getStatusCode());
-            assertNotNull("Expecting response entity", response.getEntity());
-            String encoding = "UTF-8";
-            if(response.getEntity().getContentEncoding() != null) {
-                encoding = 
response.getEntity().getContentEncoding().getValue();
-            }
-            final String content = 
IOUtils.toString(response.getEntity().getContent(), encoding);
-            assertEquals(expectedContent, content);
-        } finally {
-            U.closeConnection(response);
-        }
-    }
-    
 }
\ No newline at end of file

Modified: 
sling/trunk/contrib/crankstart/launcher/src/test/java/org/apache/sling/crankstart/launcher/CrankstartSetup.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/contrib/crankstart/launcher/src/test/java/org/apache/sling/crankstart/launcher/CrankstartSetup.java?rev=1685068&r1=1685067&r2=1685068&view=diff
==============================================================================
--- 
sling/trunk/contrib/crankstart/launcher/src/test/java/org/apache/sling/crankstart/launcher/CrankstartSetup.java
 (original)
+++ 
sling/trunk/contrib/crankstart/launcher/src/test/java/org/apache/sling/crankstart/launcher/CrankstartSetup.java
 Fri Jun 12 11:33:15 2015
@@ -9,18 +9,26 @@ import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.io.Reader;
 import java.net.ServerSocket;
+import java.util.ArrayList;
+import java.util.List;
 import java.util.Random;
 
 import org.apache.http.client.methods.HttpGet;
 import org.apache.http.client.methods.HttpUriRequest;
 import org.apache.http.impl.client.DefaultHttpClient;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /** Setup a Crankstart-launched instance for our tests */ 
 public class CrankstartSetup {
     
-    private static final int port = getAvailablePort();
-    private static Thread crankstartThread;
-    private static final String baseUrl = "http://localhost:"; + port;
+    private final Logger log = LoggerFactory.getLogger(getClass());
+    private final int port = getAvailablePort();
+    private final String storagePath = getOsgiStoragePath(); 
+    private Thread crankstartThread;
+    private final String baseUrl = "http://localhost:"; + port;
+    
+    private static List<CrankstartSetup> toCleanup = new 
ArrayList<CrankstartSetup>();
     
     public static final String [] MODEL_PATHS = {
         "/crankstart-model.txt",
@@ -29,6 +37,11 @@ public class CrankstartSetup {
         "/provisioning-model/start-level-99.txt",
         "/provisioning-model/crankstart-tests.txt"
     };
+    
+    @Override
+    public String toString() {
+        return getClass().getSimpleName() + ", port " + port + ", OSGi storage 
" + storagePath;
+    }
             
     private static int getAvailablePort() {
         int result = -1;
@@ -68,9 +81,21 @@ public class CrankstartSetup {
             return;
         }
         
+        synchronized (toCleanup) {
+            if(!toCleanup.isEmpty()) {
+                log.info("Stopping other Crankstart instances before starting 
this one...");
+            }
+            for(CrankstartSetup s : toCleanup) {
+                s.stopCrankstartInstance();
+            }
+            toCleanup.clear();
+        }
+        
+        log.info("Starting {}", this);
+        
         final HttpUriRequest get = new HttpGet(baseUrl);
         System.setProperty("crankstart.model.http.port", String.valueOf(port));
-        System.setProperty("crankstart.model.osgi.storage.path", 
getOsgiStoragePath());
+        System.setProperty("crankstart.model.osgi.storage.path", storagePath);
         
         try {
             new DefaultHttpClient().execute(get);
@@ -87,6 +112,8 @@ public class CrankstartSetup {
             public void run() {
                 try {
                     launcher.launch();
+                } catch(InterruptedException e) {
+                    log.info("Launcher thread was interrupted, exiting");
                 } catch(Exception e) {
                     e.printStackTrace();
                     fail("Launcher exception:" + e);
@@ -99,15 +126,24 @@ public class CrankstartSetup {
         Runtime.getRuntime().addShutdownHook(new Thread() {
             @Override
             public void run() {
-                crankstartThread.interrupt();
-                try {
-                    crankstartThread.join();
-                } catch(InterruptedException ignore) {
-                }
+                stopCrankstartInstance();
             }
         });
     }
     
+    private void stopCrankstartInstance() {
+        log.info("Stopping {}", this);
+        if(crankstartThread == null) {
+            return;
+        }
+        crankstartThread.interrupt();
+        try {
+            crankstartThread.join();
+        } catch(InterruptedException ignore) {
+        }
+        crankstartThread = null;
+    }
+    
     private static String getOsgiStoragePath() {
         final File tmpRoot = new File(System.getProperty("java.io.tmpdir"));
         final Random random = new Random();

Added: 
sling/trunk/contrib/crankstart/launcher/src/test/java/org/apache/sling/crankstart/launcher/RunModeATest.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/contrib/crankstart/launcher/src/test/java/org/apache/sling/crankstart/launcher/RunModeATest.java?rev=1685068&view=auto
==============================================================================
--- 
sling/trunk/contrib/crankstart/launcher/src/test/java/org/apache/sling/crankstart/launcher/RunModeATest.java
 (added)
+++ 
sling/trunk/contrib/crankstart/launcher/src/test/java/org/apache/sling/crankstart/launcher/RunModeATest.java
 Fri Jun 12 11:33:15 2015
@@ -0,0 +1,61 @@
+package org.apache.sling.crankstart.launcher;
+
+import static org.junit.Assert.assertEquals;
+
+import java.io.IOException;
+
+import org.apache.http.impl.client.DefaultHttpClient;
+import org.apache.sling.commons.testing.junit.Retry;
+import org.apache.sling.commons.testing.junit.RetryRule;
+import org.apache.sling.testing.tools.osgi.WebconsoleClient;
+import org.junit.AfterClass;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Rule;
+import org.junit.Test;
+
+/** Test our run modes support */ 
+public class RunModeATest {
+    
+    private static CrankstartSetup C = new CrankstartSetup();
+    private static WebconsoleClient osgiConsole;
+    private DefaultHttpClient client;
+    private static final String RUN_MODES = "foo,bar,A";
+    
+    @Rule
+    public final RetryRule retryRule = new RetryRule();
+    
+    @BeforeClass
+    public static void setupClass() throws IOException {
+        System.setProperty(U.SLING_RUN_MODES, RUN_MODES);
+        C.setup();
+        osgiConsole = new WebconsoleClient(C.getBaseUrl(), U.ADMIN, U.ADMIN);
+    }
+    
+    @Before
+    public void setup() throws IOException {
+        client = new DefaultHttpClient();
+    }
+    
+    @AfterClass
+    public static void cleanupClass() {
+        System.clearProperty(U.SLING_RUN_MODES);
+    }
+    
+    @Test
+    @Retry(timeoutMsec=U.LONG_TIMEOUT_MSEC, intervalMsec=U.STD_INTERVAL)
+    public void testSlingApiVersionA() throws Exception {
+        assertEquals("2.9.0", 
osgiConsole.getBundleVersion(U.SLING_API_BUNDLE));
+    }
+    
+    @Test
+    @Retry(timeoutMsec=U.LONG_TIMEOUT_MSEC, intervalMsec=U.STD_INTERVAL)
+    public void testConfigA() throws Exception {
+        U.setAdminCredentials(client);
+        U.assertHttpGet(C, client,
+                "/test/config/runmode.test", 
+                "runmode.test#mode=(String)That's 
B#service.pid=(String)runmode.test##EOC#");
+                // TODO should be "runmode.test#mode=(String)This is A##EOC#");
+    }
+    
+}
\ No newline at end of file

Modified: 
sling/trunk/contrib/crankstart/launcher/src/test/java/org/apache/sling/crankstart/launcher/U.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/contrib/crankstart/launcher/src/test/java/org/apache/sling/crankstart/launcher/U.java?rev=1685068&r1=1685067&r2=1685068&view=diff
==============================================================================
--- 
sling/trunk/contrib/crankstart/launcher/src/test/java/org/apache/sling/crankstart/launcher/U.java
 (original)
+++ 
sling/trunk/contrib/crankstart/launcher/src/test/java/org/apache/sling/crankstart/launcher/U.java
 Fri Jun 12 11:33:15 2015
@@ -1,11 +1,17 @@
 package org.apache.sling.crankstart.launcher;
 
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+
 import java.io.IOException;
 
+import org.apache.commons.io.IOUtils;
 import org.apache.http.HttpResponse;
 import org.apache.http.auth.AuthScope;
 import org.apache.http.auth.UsernamePasswordCredentials;
 import org.apache.http.client.ClientProtocolException;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.client.methods.HttpUriRequest;
 import org.apache.http.impl.client.DefaultHttpClient;
 import org.apache.http.util.EntityUtils;
 import org.apache.sling.commons.json.JSONException;
@@ -13,10 +19,15 @@ import org.apache.sling.commons.json.JSO
 import org.apache.sling.testing.tools.http.RequestBuilder;
 import org.apache.sling.testing.tools.http.RequestExecutor;
 
-/** General testing utilities */ 
+/** General testing utilities and constants */ 
 public class U {
     
     public static final String ADMIN = "admin";
+    public static final int LONG_TIMEOUT_SECONDS = 10;
+    public static final int LONG_TIMEOUT_MSEC = LONG_TIMEOUT_SECONDS * 1000;
+    public static final int STD_INTERVAL = 250;
+    public static final String SLING_API_BUNDLE = "org.apache.sling.api";
+    public static final String SLING_RUN_MODES = "sling.run.modes";
     
     static void setAdminCredentials(DefaultHttpClient c) {
         c.getCredentialsProvider().setCredentials(
@@ -42,4 +53,22 @@ public class U {
         ).assertStatus(200)
         .getContent());
     }
+    
+    public static void assertHttpGet(CrankstartSetup C, DefaultHttpClient 
client, String path, String expectedContent) throws Exception {
+        final HttpUriRequest get = new HttpGet(C.getBaseUrl() + path);
+        HttpResponse response = null;
+        try {
+            response = client.execute(get);
+            assertEquals("Expecting 200 response at " + path, 200, 
response.getStatusLine().getStatusCode());
+            assertNotNull("Expecting response entity", response.getEntity());
+            String encoding = "UTF-8";
+            if(response.getEntity().getContentEncoding() != null) {
+                encoding = 
response.getEntity().getContentEncoding().getValue();
+            }
+            final String content = 
IOUtils.toString(response.getEntity().getContent(), encoding);
+            assertEquals(expectedContent, content);
+        } finally {
+            U.closeConnection(response);
+        }
+    }
 }
\ No newline at end of file

Modified: 
sling/trunk/contrib/crankstart/launcher/src/test/resources/provisioning-model/crankstart-tests.txt
URL: 
http://svn.apache.org/viewvc/sling/trunk/contrib/crankstart/launcher/src/test/resources/provisioning-model/crankstart-tests.txt?rev=1685068&r1=1685067&r2=1685068&view=diff
==============================================================================
--- 
sling/trunk/contrib/crankstart/launcher/src/test/resources/provisioning-model/crankstart-tests.txt
 (original)
+++ 
sling/trunk/contrib/crankstart/launcher/src/test/resources/provisioning-model/crankstart-tests.txt
 Fri Jun 12 11:33:15 2015
@@ -25,6 +25,20 @@
   org.apache.sling/org.apache.sling.commons.mime/2.1.8
   org.apache.sling/org.apache.sling.settings/1.3.6
 
+[artifacts runModes=A]
+  org.apache.sling/org.apache.sling.api/2.9.0
+
+[configurations runModes=A]
+  runmode.test
+    mode="This is A"
+
+[artifacts runModes=B]
+  org.apache.sling/org.apache.sling.api/2.0.6
+
+[configurations runModes=B]
+  runmode.test
+    mode="That's B"
+
 [configurations]
   org.apache.sling.crankstart.testservices.SingleConfigServlet
   # TODO should use a variable to verify that they work in configs


Reply via email to