Author: kwin
Date: Thu Sep 17 07:16:01 2015
New Revision: 1703505

URL: http://svn.apache.org/r1703505
Log:
SLING-4819 allow to undeploy bundles optionally after execution of IT

Added:
    
sling/trunk/testing/junit/remote/src/main/java/org/apache/sling/junit/remote/testrunner/package-info.java
Modified:
    
sling/trunk/testing/junit/remote/src/main/java/org/apache/sling/junit/remote/testrunner/SlingRemoteTestRunner.java
    sling/trunk/testing/samples/integration-tests/pom.xml
    
sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools/sling/BundlesInstaller.java
    
sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools/sling/SlingTestBase.java
    
sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools/sling/package-info.java

Modified: 
sling/trunk/testing/junit/remote/src/main/java/org/apache/sling/junit/remote/testrunner/SlingRemoteTestRunner.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/testing/junit/remote/src/main/java/org/apache/sling/junit/remote/testrunner/SlingRemoteTestRunner.java?rev=1703505&r1=1703504&r2=1703505&view=diff
==============================================================================
--- 
sling/trunk/testing/junit/remote/src/main/java/org/apache/sling/junit/remote/testrunner/SlingRemoteTestRunner.java
 (original)
+++ 
sling/trunk/testing/junit/remote/src/main/java/org/apache/sling/junit/remote/testrunner/SlingRemoteTestRunner.java
 Thu Sep 17 07:16:01 2015
@@ -26,12 +26,17 @@ import org.apache.sling.junit.remote.htt
 import org.apache.sling.testing.tools.http.RequestCustomizer;
 import org.apache.sling.testing.tools.http.RequestExecutor;
 import org.apache.sling.testing.tools.sling.SlingTestBase;
+import org.junit.After;
 import org.junit.internal.AssumptionViolatedException;
 import org.junit.internal.runners.model.EachTestNotifier;
+import org.junit.internal.runners.model.MultipleFailureException;
+import org.junit.internal.runners.statements.RunAfters;
 import org.junit.runner.Description;
 import org.junit.runner.notification.RunNotifier;
 import org.junit.runners.ParentRunner;
+import org.junit.runners.model.FrameworkMethod;
 import org.junit.runners.model.InitializationError;
+import org.junit.runners.model.Statement;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -163,4 +168,27 @@ public class SlingRemoteTestRunner exten
             eachNotifier.fireTestFinished();
         }
     }
+
+    /**
+     * Similar to {@link ParentRunner#classBlock} but will call the methods 
annotated with @After in addition.
+     */
+    @Override
+    protected Statement classBlock(RunNotifier notifier) {
+        Statement statement = childrenInvoker(notifier);
+        statement = withBeforeClasses(statement);
+        // call @After class in addition
+        statement = withAfter(statement);
+        statement = withAfterClasses(statement);
+        return statement;
+    }
+    
+    /**
+     * Returns a {@link Statement}: run all non-overridden {@code @After} 
methods on this class and superclasses after
+     * executing {@code statement}; all After methods are always executed: 
exceptions thrown by previous steps are
+     * combined, if necessary, with exceptions from After methods into a 
{@link MultipleFailureException}.
+     */
+    private Statement withAfter(Statement statement) {
+        List<FrameworkMethod> afters = 
getTestClass().getAnnotatedMethods(After.class);
+        return afters.isEmpty() ? statement : new RunAfters(statement, afters, 
testParameters);
+    }
 }

Added: 
sling/trunk/testing/junit/remote/src/main/java/org/apache/sling/junit/remote/testrunner/package-info.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/testing/junit/remote/src/main/java/org/apache/sling/junit/remote/testrunner/package-info.java?rev=1703505&view=auto
==============================================================================
--- 
sling/trunk/testing/junit/remote/src/main/java/org/apache/sling/junit/remote/testrunner/package-info.java
 (added)
+++ 
sling/trunk/testing/junit/remote/src/main/java/org/apache/sling/junit/remote/testrunner/package-info.java
 Thu Sep 17 07:16:01 2015
@@ -0,0 +1,24 @@
+/*
+ * 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.
+ */
+
+@Version("1.1.0")
+package org.apache.sling.junit.remote.testrunner;
+
+import aQute.bnd.annotation.Version;
+

Modified: sling/trunk/testing/samples/integration-tests/pom.xml
URL: 
http://svn.apache.org/viewvc/sling/trunk/testing/samples/integration-tests/pom.xml?rev=1703505&r1=1703504&r2=1703505&view=diff
==============================================================================
--- sling/trunk/testing/samples/integration-tests/pom.xml (original)
+++ sling/trunk/testing/samples/integration-tests/pom.xml Thu Sep 17 07:16:01 
2015
@@ -297,7 +297,7 @@
         <dependency>
             <groupId>org.apache.sling</groupId>
             <artifactId>org.apache.sling.junit.remote</artifactId>
-            <version>1.0.10</version>
+            <version>1.0.11-SNAPSHOT</version>
             <scope>provided</scope>
         </dependency>
         <dependency>

Modified: 
sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools/sling/BundlesInstaller.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools/sling/BundlesInstaller.java?rev=1703505&r1=1703504&r2=1703505&view=diff
==============================================================================
--- 
sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools/sling/BundlesInstaller.java
 (original)
+++ 
sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools/sling/BundlesInstaller.java
 Thu Sep 17 07:16:01 2015
@@ -76,13 +76,13 @@ public class BundlesInstaller {
             final String bundleSymbolicName = getBundleSymbolicName(f);
             if (isInstalled(f)) {
                 if (f.getName().contains("SNAPSHOT")) {
-                    log.info("Reinstalling (due to SNAPSHOT version): 
"+bundleSymbolicName);
+                    log.info("Reinstalling (due to SNAPSHOT version): {}", 
bundleSymbolicName);
                     webconsoleClient.uninstallBundle(bundleSymbolicName, f);
                 } else if (!isInstalledWithSameVersion(f)) {
-                    log.info("Reinstalling (due to version mismatch): 
"+bundleSymbolicName);
+                    log.info("Reinstalling (due to version mismatch): {}", 
bundleSymbolicName);
                     webconsoleClient.uninstallBundle(bundleSymbolicName, f);
                 } else {
-                    log.info("Not reinstalling: "+bundleSymbolicName);
+                    log.info("Not reinstalling: {}", bundleSymbolicName);
                     continue;
                 }
             }
@@ -96,6 +96,24 @@ public class BundlesInstaller {
         log.info("{} additional bundles installed", toInstall.size());
     }
     
+    /** Uninstall a list of bundles supplied as Files */
+    public void uninstallBundles(List<File> toUninstall) throws Exception {
+        for(File f : toUninstall) {
+            final String bundleSymbolicName = getBundleSymbolicName(f);
+            if (isInstalled(f)) {
+                log.info("Uninstalling bundle: {}", bundleSymbolicName);
+                webconsoleClient.uninstallBundle(bundleSymbolicName, f);
+            } else {
+                log.info("Could not uninstall: {} as it never was installed", 
bundleSymbolicName);
+            }
+        }
+        
+        // ensure that bundles are re-wired esp. if an existing bundle was 
updated
+        webconsoleClient.refreshPackages();
+
+        log.info("{} additional bundles uninstalled", toUninstall.size());
+    }
+    
     /** Wait for all bundles specified in symbolicNames list to be installed 
in the
      *  remote web console.
      */

Modified: 
sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools/sling/SlingTestBase.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools/sling/SlingTestBase.java?rev=1703505&r1=1703504&r2=1703505&view=diff
==============================================================================
--- 
sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools/sling/SlingTestBase.java
 (original)
+++ 
sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools/sling/SlingTestBase.java
 Thu Sep 17 07:16:01 2015
@@ -33,6 +33,8 @@ import org.apache.sling.testing.tools.ht
 import org.apache.sling.testing.tools.jarexec.JarExecutor;
 import org.apache.sling.testing.tools.junit.TestDescriptionInterceptor;
 import org.apache.sling.testing.tools.osgi.WebconsoleClient;
+import org.junit.After;
+import org.junit.runners.ParentRunner;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -48,12 +50,14 @@ public class SlingTestBase implements Sl
     public static final String KEEP_JAR_RUNNING_PROP = "keepJarRunning";
     public static final String SERVER_HOSTNAME_PROP = "test.server.hostname";
     public static final String ADDITONAL_BUNDLES_PATH = 
"additional.bundles.path";
+    public static final String ADDITONAL_BUNDLES_UNINSTALL = 
"additional.bundles.uninstall";
     public static final String BUNDLE_TO_INSTALL_PREFIX = 
"sling.additional.bundle";
     public static final String START_BUNDLES_TIMEOUT_SECONDS = 
"start.bundles.timeout.seconds";
     public static final String BUNDLE_INSTALL_TIMEOUT_SECONDS = 
"bundle.install.timeout.seconds";
     public static final String ADMIN = "admin";
 
     private final boolean keepJarRunning;
+    private final boolean uninstallAdditionalBundles;
     private final String serverUsername;
     private final String serverPassword;
     private final SlingInstanceState slingTestState;
@@ -86,6 +90,7 @@ public class SlingTestBase implements Sl
         if(configuredUrl != null && configuredUrl.trim().length() > 0) {
             slingTestState.setServerBaseUrl(configuredUrl);
             slingTestState.setServerStarted(true);
+            uninstallAdditionalBundles = 
"true".equals(systemProperties.getProperty(ADDITONAL_BUNDLES_UNINSTALL));
         } else {
             synchronized(this.slingTestState) {
                 try {
@@ -102,6 +107,7 @@ public class SlingTestBase implements Sl
                 serverHost = "localhost";
             }
             slingTestState.setServerBaseUrl("http://"; + serverHost + ":" + 
slingTestState.getJarExecutor().getServerPort());
+            uninstallAdditionalBundles = false; // never undeploy additional 
bundles in case the server is provisioned here!
         }
 
         // Set configured username using "admin" as default credential
@@ -131,6 +137,17 @@ public class SlingTestBase implements Sl
         }
     }
 
+    /**
+     * Automatically by the {@link SlingRemoteTestRunner} since package 
version 1.1.0.
+     */
+    @After
+    public void uninstallAdditionalBundlesIfNecessary() {
+        if (uninstallAdditionalBundles) {
+            log.info("Uninstalling additional bundles...");
+            uninstallAdditionalBundles();
+        }
+    }
+
     /** Start the server, if not done yet */
     private void startServerIfNeeded() {
         try {
@@ -162,23 +179,13 @@ public class SlingTestBase implements Sl
         if(slingTestState.isInstallBundlesFailed()) {
             fail("Bundles could not be installed, cannot run tests");
         } else if(!slingTestState.isExtraBundlesInstalled()) {
-            final String paths = 
systemProperties.getProperty(ADDITONAL_BUNDLES_PATH);
-            if(paths == null) {
-                log.info("System property {} not set, additional bundles won't 
be installed",
-                        ADDITONAL_BUNDLES_PATH);
-            } else {
-                final List<File> toInstall = new ArrayList<File>();
+            final List<File> toInstall = getBundlesToInstall();
+            if (!toInstall.isEmpty()) {
                 try {
-                    // Paths can contain a comma-separated list
-                    final String [] allPaths = paths.split(",");
-                    for(String path : allPaths) {
-                        toInstall.addAll(getBundlesToInstall(path.trim()));
-                    }
-                    
                     // Install bundles, check that they are installed and 
start them all
                     bundlesInstaller.installBundles(toInstall, false);
                     final List<String> symbolicNames = new 
LinkedList<String>();
-                    for(File f : toInstall) {
+                    for (File f : toInstall) {
                         
symbolicNames.add(bundlesInstaller.getBundleSymbolicName(f));
                     }
                     bundlesInstaller.waitForBundlesInstalled(symbolicNames,
@@ -192,15 +199,26 @@ public class SlingTestBase implements Sl
                     log.info("Exception while installing additional bundles", 
e);
                     slingTestState.setInstallBundlesFailed(true);
                 }
-
                 if(slingTestState.isInstallBundlesFailed()) {
                     fail("Could not start all installed bundles:" + toInstall);
                 }
+            } else {
+                log.info("Not installing additional bundles, probably System 
property {} not set",
+                        ADDITONAL_BUNDLES_PATH);
             }
         }
 
         
slingTestState.setExtraBundlesInstalled(!slingTestState.isInstallBundlesFailed());
     }
+    
+    protected void uninstallAdditionalBundles() {
+        try {
+            // always uninstall independent of installation status
+            bundlesInstaller.uninstallBundles(getBundlesToInstall());
+        } catch (Exception e) {
+             log.info("Exception while uninstalling additional bundles", e);
+        }
+    }
 
     /** Start server if needed, and return a RequestBuilder that points to it 
*/
     public RequestBuilder getRequestBuilder() {
@@ -309,7 +327,26 @@ public class SlingTestBase implements Sl
         }
     }
 
-    /** Get the list of additional bundles to install, as specified by path 
parameter */
+    /**
+     * Get the list of additional bundles to install, as specified by the 
system property {@link #ADDITONAL_BUNDLES_PATH} 
+     * @return the list of {@link File}s pointing to the Bundle JARs or the 
empty list in case no additional bundles should be installed (never {@code 
null}).
+     */
+    protected List<File> getBundlesToInstall() {
+        final String paths = 
systemProperties.getProperty(ADDITONAL_BUNDLES_PATH);
+        if(paths == null) {
+            return Collections.emptyList();
+        } 
+        
+        final List<File> toInstall = new ArrayList<File>();
+        // Paths can contain a comma-separated list
+        final String [] allPaths = paths.split(",");
+        for(String path : allPaths) {
+            toInstall.addAll(getBundlesToInstall(path.trim()));
+        }
+        return toInstall;
+    }
+
+    /** Get the list of additional bundles to install, as specified by 
additionalBundlesPath parameter */
     protected List<File> getBundlesToInstall(String additionalBundlesPath) {
         final List<File> result = new LinkedList<File>();
         if(additionalBundlesPath == null) {

Modified: 
sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools/sling/package-info.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools/sling/package-info.java?rev=1703505&r1=1703504&r2=1703505&view=diff
==============================================================================
--- 
sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools/sling/package-info.java
 (original)
+++ 
sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools/sling/package-info.java
 Thu Sep 17 07:16:01 2015
@@ -17,7 +17,7 @@
  * under the License.
  */
 
-@Version("1.0.8")
+@Version("1.1.0")
 package org.apache.sling.testing.tools.sling;
 
 import aQute.bnd.annotation.Version;


Reply via email to