You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openwebbeans.apache.org by rm...@apache.org on 2017/04/09 11:35:03 UTC

svn commit: r1790741 - /openwebbeans/meecrowave/trunk/meecrowave-junit/src/main/java/org/apache/meecrowave/testing/MonoBase.java

Author: rmannibucau
Date: Sun Apr  9 11:35:03 2017
New Revision: 1790741

URL: http://svn.apache.org/viewvc?rev=1790741&view=rev
Log:
MEECROWAVE-24 adding lifecycle hooks for junit mono integratin

Modified:
    openwebbeans/meecrowave/trunk/meecrowave-junit/src/main/java/org/apache/meecrowave/testing/MonoBase.java

Modified: openwebbeans/meecrowave/trunk/meecrowave-junit/src/main/java/org/apache/meecrowave/testing/MonoBase.java
URL: http://svn.apache.org/viewvc/openwebbeans/meecrowave/trunk/meecrowave-junit/src/main/java/org/apache/meecrowave/testing/MonoBase.java?rev=1790741&r1=1790740&r2=1790741&view=diff
==============================================================================
--- openwebbeans/meecrowave/trunk/meecrowave-junit/src/main/java/org/apache/meecrowave/testing/MonoBase.java (original)
+++ openwebbeans/meecrowave/trunk/meecrowave-junit/src/main/java/org/apache/meecrowave/testing/MonoBase.java Sun Apr  9 11:35:03 2017
@@ -21,6 +21,7 @@ package org.apache.meecrowave.testing;
 import org.apache.meecrowave.Meecrowave;
 
 import java.io.File;
+import java.util.Comparator;
 import java.util.ServiceLoader;
 import java.util.concurrent.atomic.AtomicReference;
 import java.util.stream.StreamSupport;
@@ -36,16 +37,22 @@ public class MonoBase {
         final Meecrowave meecrowave = new Meecrowave(CONFIGURATION.get());
         if (CONTAINER.compareAndSet(null, meecrowave)) {
             final Configuration runnerConfig = StreamSupport.stream(ServiceLoader.load(Configuration.class).spliterator(), false)
-                    .findAny()
+                    .sorted(Comparator.comparingInt(Configuration::order))
+                    .findFirst()
                     .orElseGet(() -> new Configuration() {
                     });
 
+            runnerConfig.beforeStarts();
+
             final File war = runnerConfig.application();
             if (war == null) {
                 meecrowave.bake(runnerConfig.context());
             } else {
                 meecrowave.deployWebapp(runnerConfig.context(), runnerConfig.application());
             }
+
+            runnerConfig.afterStarts();
+
             Runtime.getRuntime().addShutdownHook(new Thread() {
                 {
                     setName("Meecrowave-mono-rue-stopping");
@@ -53,7 +60,15 @@ public class MonoBase {
 
                 @Override
                 public void run() {
-                    meecrowave.close();
+                    try {
+                        runnerConfig.beforeStops();
+                    } finally {
+                        try {
+                            meecrowave.close();
+                        } finally {
+                            runnerConfig.afterStops();
+                        }
+                    }
                 }
             });
         }
@@ -76,6 +91,10 @@ public class MonoBase {
     }
 
     public interface Configuration {
+        default int order() {
+            return 0;
+        }
+
         default String context() {
             return "";
         }
@@ -83,5 +102,21 @@ public class MonoBase {
         default File application() {
             return null;
         }
+
+        default void beforeStarts() {
+            // no-op
+        }
+
+        default void afterStarts() {
+            // no-op
+        }
+
+        default void beforeStops() {
+            // no-op
+        }
+
+        default void afterStops() {
+            // no-op
+        }
     }
 }