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

orpiske pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git

commit 11e1c4f941d1e585acb9c4c17761615900243298
Author: Otavio Rodolfo Piske <angusyo...@gmail.com>
AuthorDate: Wed May 22 13:39:27 2024 +0200

    CAMEL-20785: cleanup startCamelContext and make reusable
---
 .../apache/camel/test/junit5/CamelTestSupport.java | 13 +--------
 .../test/junit5/util/CamelContextTestHelper.java   | 32 ++++++++++++++++++++++
 2 files changed, 33 insertions(+), 12 deletions(-)

diff --git 
a/components/camel-test/camel-test-junit5/src/main/java/org/apache/camel/test/junit5/CamelTestSupport.java
 
b/components/camel-test/camel-test-junit5/src/main/java/org/apache/camel/test/junit5/CamelTestSupport.java
index f9471bfd455..e4ac89e944a 100644
--- 
a/components/camel-test/camel-test-junit5/src/main/java/org/apache/camel/test/junit5/CamelTestSupport.java
+++ 
b/components/camel-test/camel-test-junit5/src/main/java/org/apache/camel/test/junit5/CamelTestSupport.java
@@ -698,18 +698,7 @@ public abstract class CamelTestSupport
     }
 
     protected void startCamelContext() throws Exception {
-        if (camelContextService != null) {
-            camelContextService.start();
-        } else {
-            if (context instanceof DefaultCamelContext) {
-                DefaultCamelContext defaultCamelContext = 
(DefaultCamelContext) context;
-                if (!defaultCamelContext.isStarted()) {
-                    defaultCamelContext.start();
-                }
-            } else {
-                context.start();
-            }
-        }
+        CamelContextTestHelper.startCamelContextOrService(context, 
camelContextService);
     }
 
     protected CamelContext createCamelContext() throws Exception {
diff --git 
a/components/camel-test/camel-test-junit5/src/main/java/org/apache/camel/test/junit5/util/CamelContextTestHelper.java
 
b/components/camel-test/camel-test-junit5/src/main/java/org/apache/camel/test/junit5/util/CamelContextTestHelper.java
index d4e2008c60a..16a88ff0778 100644
--- 
a/components/camel-test/camel-test-junit5/src/main/java/org/apache/camel/test/junit5/util/CamelContextTestHelper.java
+++ 
b/components/camel-test/camel-test-junit5/src/main/java/org/apache/camel/test/junit5/util/CamelContextTestHelper.java
@@ -23,6 +23,7 @@ import org.apache.camel.CamelContext;
 import org.apache.camel.NoSuchEndpointException;
 import org.apache.camel.RouteConfigurationsBuilder;
 import org.apache.camel.RoutesBuilder;
+import org.apache.camel.Service;
 import org.apache.camel.ServiceStatus;
 import org.apache.camel.component.mock.InterceptSendToMockEndpointStrategy;
 import org.apache.camel.component.mock.MockEndpoint;
@@ -176,4 +177,35 @@ public final class CamelContextTestHelper {
             
context.getCamelContextExtension().getContextPlugin(Model.class).setRouteFilterPattern(include,
 exclude);
         }
     }
+
+    /**
+     * Start the given context
+     *
+     * @param  context   the context to start
+     * @throws Exception
+     */
+    public static void startCamelContext(CamelContext context) throws 
Exception {
+        if (context instanceof DefaultCamelContext defaultCamelContext) {
+            if (!defaultCamelContext.isStarted()) {
+                defaultCamelContext.start();
+            }
+        } else {
+            context.start();
+        }
+    }
+
+    /**
+     * Starts a CamelContext or a service managing the CamelContext. The 
service takes priority if provided.
+     *
+     * @param  context             the context to start
+     * @param  camelContextService the service managing the CamelContext
+     * @throws Exception
+     */
+    public static void startCamelContextOrService(CamelContext context, 
Service camelContextService) throws Exception {
+        if (camelContextService != null) {
+            camelContextService.start();
+        } else {
+            CamelContextTestHelper.startCamelContext(context);
+        }
+    }
 }

Reply via email to