SLING-8195 Stop mongod processes created during test execution master
authorOliver Lietz <olli@apache.org>
Thu, 27 Dec 2018 13:49:46 +0000 (14:49 +0100)
committerOliver Lietz <olli@apache.org>
Thu, 27 Dec 2018 13:49:46 +0000 (14:49 +0100)
pom.xml
src/test/java/org/apache/sling/karaf/tests/bootstrap/SlingQuickstartNosqlMongoIT.java
src/test/java/org/apache/sling/karaf/tests/bootstrap/SlingQuickstartOakMongoIT.java
src/test/java/org/apache/sling/karaf/tests/support/MongodProcessStopper.java [new file with mode: 0644]

diff --git a/pom.xml b/pom.xml
index c04c4d7..1840577 100644 (file)
--- a/pom.xml
+++ b/pom.xml
             </executions>
             <configuration>
               <redirectTestOutputToFile>true</redirectTestOutputToFile>
+              <properties>
+                <property>
+                  <name>listener</name>
+                  <value>org.apache.sling.karaf.tests.support.MongodProcessStopper</value>
+                </property>
+              </properties>
             </configuration>
           </plugin>
         </plugins>
index fc0d879..b865d4c 100644 (file)
@@ -32,7 +32,7 @@ import de.flapdoodle.embed.mongo.distribution.Version;
 import de.flapdoodle.embed.process.runtime.Network;
 import org.apache.sling.api.resource.ResourceProviderFactory;
 import org.apache.sling.karaf.testing.KarafTestSupport;
-import org.junit.AfterClass;
+import org.apache.sling.karaf.tests.support.MongodProcessStopper;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.ops4j.pax.exam.Configuration;
@@ -52,10 +52,6 @@ import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.editConfi
 @ExamReactorStrategy(PerClass.class)
 public class SlingQuickstartNosqlMongoIT extends KarafTestSupport {
 
-    private static MongodExecutable executable;
-
-    private static MongodProcess process;
-
     @Inject
     @Filter(timeout = 300000)
     public ResourceProviderFactory resourceProviderFactory;
@@ -64,15 +60,9 @@ public class SlingQuickstartNosqlMongoIT extends KarafTestSupport {
         final MongodStarter starter = MongodStarter.getDefaultInstance();
         final Net net = new Net(port, Network.localhostIsIPv6());
         final IMongodConfig mongodConfig = new MongodConfigBuilder().version(Version.Main.PRODUCTION).net(net).build();
-        executable = starter.prepare(mongodConfig);
-        process = executable.start();
-    }
-
-    @AfterClass // TODO does it work? (no - not supported by Pax Exam)
-    public static void stopMongo() throws Exception {
-        if (executable != null) {
-            executable.stop();
-        }
+        final MongodExecutable executable = starter.prepare(mongodConfig);
+        final MongodProcess process = executable.start();
+        MongodProcessStopper.add(process);
     }
 
     @Configuration
index 8c0d688..f014c37 100644 (file)
@@ -28,7 +28,7 @@ import de.flapdoodle.embed.mongo.config.MongodConfigBuilder;
 import de.flapdoodle.embed.mongo.config.Net;
 import de.flapdoodle.embed.mongo.distribution.Version;
 import de.flapdoodle.embed.process.runtime.Network;
-import org.junit.AfterClass;
+import org.apache.sling.karaf.tests.support.MongodProcessStopper;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.ops4j.pax.exam.Configuration;
@@ -48,23 +48,13 @@ import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.editConfi
 @ExamReactorStrategy(PerClass.class)
 public class SlingQuickstartOakMongoIT extends AbstractSlingQuickstartOakTestSupport {
 
-    private static MongodExecutable executable;
-
-    private static MongodProcess process;
-
     protected void startMongo(final int port) throws IOException {
         final MongodStarter starter = MongodStarter.getDefaultInstance();
         final Net net = new Net(port, Network.localhostIsIPv6());
         final IMongodConfig mongodConfig = new MongodConfigBuilder().version(Version.Main.PRODUCTION).net(net).build();
-        executable = starter.prepare(mongodConfig);
-        process = executable.start();
-    }
-
-    @AfterClass // TODO does it work? (no - not supported by Pax Exam)
-    public static void stopMongo() throws Exception {
-        if (executable != null) {
-            executable.stop();
-        }
+        final MongodExecutable executable = starter.prepare(mongodConfig);
+        final MongodProcess process = executable.start();
+        MongodProcessStopper.add(process);
     }
 
     @Configuration
diff --git a/src/test/java/org/apache/sling/karaf/tests/support/MongodProcessStopper.java b/src/test/java/org/apache/sling/karaf/tests/support/MongodProcessStopper.java
new file mode 100644 (file)
index 0000000..e29841a
--- /dev/null
@@ -0,0 +1,46 @@
+/*
+ * 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.karaf.tests.support;
+
+import java.util.HashSet;
+import java.util.Set;
+
+import de.flapdoodle.embed.mongo.MongodProcess;
+import org.junit.runner.Result;
+import org.junit.runner.notification.RunListener;
+
+public class MongodProcessStopper extends RunListener {
+
+    private static Set<MongodProcess> processes = new HashSet<>();
+
+    public MongodProcessStopper() {
+    }
+
+    public static void add(final MongodProcess process) {
+        processes.add(process);
+    }
+
+    @Override
+    public void testRunFinished(Result result) {
+        for (final MongodProcess process : processes) {
+            process.stop();
+        }
+    }
+
+}