You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by nf...@apache.org on 2023/05/29 17:04:24 UTC

[camel] 01/02: CAMEL-19400: camel-test-infra - Only initialize and shutdown for outer classes

This is an automated email from the ASF dual-hosted git repository.

nfilotto pushed a commit to branch CAMEL-19400/reduce-it-duration
in repository https://gitbox.apache.org/repos/asf/camel.git

commit ca9fbbb6698a5744b435cf5d76ce76b62a0bdec9
Author: Nicolas Filotto <nf...@talend.com>
AuthorDate: Mon May 29 19:02:58 2023 +0200

    CAMEL-19400: camel-test-infra - Only initialize and shutdown for outer classes
---
 .../infra/common/services/TestServiceUtil.java     | 31 ++++++++++++++++------
 1 file changed, 23 insertions(+), 8 deletions(-)

diff --git a/test-infra/camel-test-infra-common/src/test/java/org/apache/camel/test/infra/common/services/TestServiceUtil.java b/test-infra/camel-test-infra-common/src/test/java/org/apache/camel/test/infra/common/services/TestServiceUtil.java
index 95e8d32ff95..0a688ca8cbd 100644
--- a/test-infra/camel-test-infra-common/src/test/java/org/apache/camel/test/infra/common/services/TestServiceUtil.java
+++ b/test-infra/camel-test-infra-common/src/test/java/org/apache/camel/test/infra/common/services/TestServiceUtil.java
@@ -17,6 +17,7 @@
 
 package org.apache.camel.test.infra.common.services;
 
+import org.junit.jupiter.api.Nested;
 import org.junit.jupiter.api.extension.ExtensionContext;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -39,10 +40,12 @@ public final class TestServiceUtil {
      * @throws Exception        exception thrown while initializing (if any)
      */
     public static void tryInitialize(TestService service, ExtensionContext extensionContext) throws Exception {
-        try {
-            service.initialize();
-        } catch (Exception e) {
-            logAndRethrow(service, extensionContext, e);
+        if (isOuterTestClass(extensionContext)) {
+            try {
+                service.initialize();
+            } catch (Exception e) {
+                logAndRethrow(service, extensionContext, e);
+            }
         }
     }
 
@@ -54,10 +57,12 @@ public final class TestServiceUtil {
      * @throws Exception        exception thrown while initializing (if any)
      */
     public static void tryShutdown(TestService service, ExtensionContext extensionContext) throws Exception {
-        try {
-            service.shutdown();
-        } catch (Exception e) {
-            logAndRethrow(service, extensionContext, e);
+        if (isOuterTestClass(extensionContext)) {
+            try {
+                service.shutdown();
+            } catch (Exception e) {
+                logAndRethrow(service, extensionContext, e);
+            }
         }
     }
 
@@ -76,4 +81,14 @@ public final class TestServiceUtil {
                 extensionContext.getDisplayName(), o.getClass().getName());
         throw exception;
     }
+
+    /**
+     * Indicates whether the test class called is an outer class.
+     *
+     * @param  extensionContext JUnit's extension context
+     * @return                  {@code true} if the test class called is an outer class, {@code false} otherwise.
+     */
+    private static boolean isOuterTestClass(ExtensionContext extensionContext) {
+        return extensionContext.getTestClass().map(aClass -> aClass.getAnnotation(Nested.class) == null).orElse(true);
+    }
 }