SLING-4728 - use a JUnit ClassRule for integration tests

git-svn-id: https://svn.apache.org/repos/asf/sling/trunk@1722129 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/pom.xml b/pom.xml
index c53b43a..18e3ff6 100644
--- a/pom.xml
+++ b/pom.xml
@@ -154,8 +154,8 @@
         <dependency>
             <groupId>junit</groupId>
             <artifactId>junit</artifactId>
-            <version>4.11</version>
-            <scope>test</scope>
+            <version>4.12</version>
+            <scope>provided</scope>
         </dependency>
         <dependency>
             <groupId>org.apache.sling</groupId>
@@ -193,7 +193,7 @@
             <groupId>org.apache.httpcomponents</groupId>
             <artifactId>httpclient</artifactId>
             <version>4.1</version>
-            <scope>test</scope>
+            <scope>provided</scope>
         </dependency>
         <dependency>
             <groupId>commons-io</groupId>
diff --git a/src/test/java/org/apache/sling/crankstart/launcher/CrankstartSetup.java b/src/main/java/org/apache/sling/crankstart/junit/CrankstartSetup.java
similarity index 88%
rename from src/test/java/org/apache/sling/crankstart/launcher/CrankstartSetup.java
rename to src/main/java/org/apache/sling/crankstart/junit/CrankstartSetup.java
index 7548be7..295cb80 100644
--- a/src/test/java/org/apache/sling/crankstart/launcher/CrankstartSetup.java
+++ b/src/main/java/org/apache/sling/crankstart/junit/CrankstartSetup.java
@@ -1,4 +1,4 @@
-package org.apache.sling.crankstart.launcher;
+package org.apache.sling.crankstart.junit;
 
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.fail;
@@ -16,11 +16,13 @@
 import org.apache.http.client.methods.HttpGet;
 import org.apache.http.client.methods.HttpUriRequest;
 import org.apache.http.impl.client.DefaultHttpClient;
+import org.apache.sling.crankstart.launcher.Launcher;
+import org.junit.rules.ExternalResource;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 /** Setup a Crankstart-launched instance for our tests */ 
-public class CrankstartSetup {
+public class CrankstartSetup extends ExternalResource {
     
     private final Logger log = LoggerFactory.getLogger(getClass());
     private final int port = getAvailablePort();
@@ -30,18 +32,17 @@
     
     private static List<CrankstartSetup> toCleanup = new ArrayList<CrankstartSetup>();
     
-    public static final String [] MODEL_PATHS = {
-        "/crankstart-model.txt",
-        "/provisioning-model/base.txt",
-        "/provisioning-model/sling-extensions.txt",
-        "/provisioning-model/start-level-99.txt",
-        "/provisioning-model/crankstart-tests.txt"
-    };
+    private String [] modelPaths;
     
     @Override
     public String toString() {
         return getClass().getSimpleName() + ", port " + port + ", OSGi storage " + storagePath;
     }
+    
+    public CrankstartSetup withModels(String ... modelPaths) {
+        this.modelPaths = modelPaths;
+        return this;
+    }
             
     private static int getAvailablePort() {
         int result = -1;
@@ -73,11 +74,16 @@
         }
     }
     
-    String getBaseUrl() {
+    public String getBaseUrl() {
         return baseUrl;
     }
+    
+    @Override
+    protected void before() throws Throwable {
+        setup();
+    }
      
-    synchronized void setup() throws Exception {
+    public synchronized void setup() throws Exception {
         if(crankstartThread != null) {
             return;
         }
@@ -105,7 +111,7 @@
         }
         
         final Launcher launcher = new Launcher();
-        for(String path : MODEL_PATHS) {
+        for(String path : modelPaths) {
             mergeModelResource(launcher, path);
         }
         launcher.computeEffectiveModel();
diff --git a/src/test/java/org/apache/sling/crankstart/launcher/BasicLauncherIT.java b/src/test/java/org/apache/sling/crankstart/launcher/BasicLauncherIT.java
index 167accd..691853e 100644
--- a/src/test/java/org/apache/sling/crankstart/launcher/BasicLauncherIT.java
+++ b/src/test/java/org/apache/sling/crankstart/launcher/BasicLauncherIT.java
@@ -14,9 +14,11 @@
 import org.apache.sling.commons.json.JSONObject;
 import org.apache.sling.commons.testing.junit.Retry;
 import org.apache.sling.commons.testing.junit.RetryRule;
+import org.apache.sling.crankstart.junit.CrankstartSetup;
 import org.apache.sling.testing.tools.osgi.WebconsoleClient;
 import org.junit.Before;
 import org.junit.BeforeClass;
+import org.junit.ClassRule;
 import org.junit.Rule;
 import org.junit.Test;
 
@@ -26,7 +28,9 @@
  */
 public class BasicLauncherIT {
     
-    private static CrankstartSetup C;
+    @ClassRule
+    public static CrankstartSetup C = new CrankstartSetup().withModels(U.DEFAULT_MODELS);
+    
     private DefaultHttpClient client;
     private static WebconsoleClient osgiConsole;
     
@@ -35,8 +39,6 @@
     
     @BeforeClass
     public static void setupClass() throws Exception {
-        C = new CrankstartSetup();
-        C.setup();
         osgiConsole = new WebconsoleClient(C.getBaseUrl(), U.ADMIN, U.ADMIN);
     }
     
diff --git a/src/test/java/org/apache/sling/crankstart/launcher/RunModeAIT.java b/src/test/java/org/apache/sling/crankstart/launcher/RunModeAIT.java
index 99ed68b..1c741b3 100644
--- a/src/test/java/org/apache/sling/crankstart/launcher/RunModeAIT.java
+++ b/src/test/java/org/apache/sling/crankstart/launcher/RunModeAIT.java
@@ -7,18 +7,22 @@
 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.crankstart.junit.CrankstartSetup;
 import org.apache.sling.testing.tools.osgi.WebconsoleClient;
 import org.junit.AfterClass;
 import org.junit.Before;
 import org.junit.BeforeClass;
+import org.junit.ClassRule;
 import org.junit.Rule;
 import org.junit.Test;
 
 /** Test our run modes support */ 
 public class RunModeAIT {
     
-    private static CrankstartSetup C = new CrankstartSetup();
-    private static WebconsoleClient osgiConsole;
+    @ClassRule
+    public static CrankstartSetup C = new CrankstartSetup().withModels(U.DEFAULT_MODELS);
+    
+    private WebconsoleClient osgiConsole;
     private DefaultHttpClient client;
     private static final String RUN_MODES = "foo,bar,A";
     
@@ -28,12 +32,11 @@
     @BeforeClass
     public static void setupClass() throws Exception {
         System.setProperty(RunModeFilter.SLING_RUN_MODES, RUN_MODES);
-        C.setup();
-        osgiConsole = new WebconsoleClient(C.getBaseUrl(), U.ADMIN, U.ADMIN);
     }
     
     @Before
     public void setup() throws IOException {
+        osgiConsole = new WebconsoleClient(C.getBaseUrl(), U.ADMIN, U.ADMIN);
         client = new DefaultHttpClient();
     }
     
diff --git a/src/test/java/org/apache/sling/crankstart/launcher/RunModeBIT.java b/src/test/java/org/apache/sling/crankstart/launcher/RunModeBIT.java
index bad8b2f..f36e09f 100644
--- a/src/test/java/org/apache/sling/crankstart/launcher/RunModeBIT.java
+++ b/src/test/java/org/apache/sling/crankstart/launcher/RunModeBIT.java
@@ -7,18 +7,22 @@
 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.crankstart.junit.CrankstartSetup;
 import org.apache.sling.testing.tools.osgi.WebconsoleClient;
 import org.junit.AfterClass;
 import org.junit.Before;
 import org.junit.BeforeClass;
+import org.junit.ClassRule;
 import org.junit.Rule;
 import org.junit.Test;
 
 /** Test our run modes support */ 
 public class RunModeBIT {
     
-    private static CrankstartSetup C = new CrankstartSetup();
-    private static WebconsoleClient osgiConsole;
+    @ClassRule
+    public static CrankstartSetup C = new CrankstartSetup().withModels(U.DEFAULT_MODELS);
+    
+    private WebconsoleClient osgiConsole;
     private DefaultHttpClient client;
     private static final String RUN_MODES = "bala,B,laika,another";
     
@@ -28,12 +32,11 @@
     @BeforeClass
     public static void setupClass() throws Exception {
         System.setProperty(RunModeFilter.SLING_RUN_MODES, RUN_MODES);
-        C.setup();
-        osgiConsole = new WebconsoleClient(C.getBaseUrl(), U.ADMIN, U.ADMIN);
     }
     
     @Before
     public void setup() throws IOException {
+        osgiConsole = new WebconsoleClient(C.getBaseUrl(), U.ADMIN, U.ADMIN);
         client = new DefaultHttpClient();
     }
     
diff --git a/src/test/java/org/apache/sling/crankstart/launcher/U.java b/src/test/java/org/apache/sling/crankstart/launcher/U.java
index 626acc2..e20fa00 100644
--- a/src/test/java/org/apache/sling/crankstart/launcher/U.java
+++ b/src/test/java/org/apache/sling/crankstart/launcher/U.java
@@ -16,6 +16,7 @@
 import org.apache.http.util.EntityUtils;
 import org.apache.sling.commons.json.JSONException;
 import org.apache.sling.commons.json.JSONObject;
+import org.apache.sling.crankstart.junit.CrankstartSetup;
 import org.apache.sling.testing.tools.http.RequestBuilder;
 import org.apache.sling.testing.tools.http.RequestExecutor;
 
@@ -28,6 +29,14 @@
     public static final int STD_INTERVAL = 250;
     public static final String SLING_API_BUNDLE = "org.apache.sling.api";
     
+    static final String [] DEFAULT_MODELS = {
+        "/crankstart-model.txt",
+        "/provisioning-model/base.txt",
+        "/provisioning-model/sling-extensions.txt",
+        "/provisioning-model/start-level-99.txt",
+        "/provisioning-model/crankstart-tests.txt"
+    };
+
     static void setAdminCredentials(DefaultHttpClient c) {
         c.getCredentialsProvider().setCredentials(
                 AuthScope.ANY,