You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2022/06/17 19:58:24 UTC

[camel] branch main updated: CAMEL-18201 Enhance CamelTestSupport to turn off context stopping (#7813)

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

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


The following commit(s) were added to refs/heads/main by this push:
     new 62fb557c3d9 CAMEL-18201 Enhance CamelTestSupport to turn off context stopping (#7813)
62fb557c3d9 is described below

commit 62fb557c3d95df0264ffd69ec8c050b761048120
Author: JiriOndrusek <on...@gmail.com>
AuthorDate: Fri Jun 17 21:58:18 2022 +0200

    CAMEL-18201 Enhance CamelTestSupport to turn off context stopping (#7813)
---
 .../apache/camel/test/junit5/CamelTestSupport.java |   4 +-
 .../CamelTestSupporOneContextForAllTest.java       | 129 +++++++++++++++++++++
 2 files changed, 131 insertions(+), 2 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 dfb8014f5a7..d3a13c8236f 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
@@ -413,7 +413,7 @@ public abstract class CamelTestSupport
         }
     }
 
-    private void doSetUp() throws Exception {
+    protected void doSetUp() throws Exception {
         LOG.debug("setUp test");
         // jmx is enabled if we have configured to use it, or if dump route
         // coverage is enabled (it requires JMX)
@@ -706,7 +706,7 @@ public abstract class CamelTestSupport
         doStopCamelContext(context, camelContextService);
     }
 
-    private static void doStopCamelContext(CamelContext context, Service camelContextService) {
+    protected void doStopCamelContext(CamelContext context, Service camelContextService) {
         if (camelContextService != null) {
             if (camelContextService == threadService.get()) {
                 threadService.remove();
diff --git a/components/camel-test/camel-test-junit5/src/test/java/org/apache/camel/test/junit5/CamelTestSupporOneContextForAllTest.java b/components/camel-test/camel-test-junit5/src/test/java/org/apache/camel/test/junit5/CamelTestSupporOneContextForAllTest.java
new file mode 100644
index 00000000000..3f8c0ffdbe0
--- /dev/null
+++ b/components/camel-test/camel-test-junit5/src/test/java/org/apache/camel/test/junit5/CamelTestSupporOneContextForAllTest.java
@@ -0,0 +1,129 @@
+/*
+ * 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.camel.test.junit5;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.EndpointInject;
+import org.apache.camel.Produce;
+import org.apache.camel.ProducerTemplate;
+import org.apache.camel.RoutesBuilder;
+import org.apache.camel.Service;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.impl.DefaultCamelContext;
+import org.apache.camel.spi.Registry;
+import org.apache.camel.support.DefaultRegistry;
+import org.apache.camel.test.junit5.patterns.CreateCamelContextPerTestTrueTest;
+import org.junit.jupiter.api.Order;
+import org.junit.jupiter.api.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class CamelTestSupporOneContextForAllTest extends CamelTestSupport {
+
+    private static final Logger LOG = LoggerFactory.getLogger(CreateCamelContextPerTestTrueTest.class);
+
+    private static final CamelContext CUSTOM_CONTEXT;
+
+    static {
+        CUSTOM_CONTEXT = new MockContext();
+    }
+
+    @EndpointInject("mock:result")
+    protected MockEndpoint resultEndpoint;
+
+    @Produce("direct:start")
+    protected ProducerTemplate template;
+
+    @Override
+    protected CamelContext createCamelContext() throws Exception {
+        return CUSTOM_CONTEXT;
+    }
+
+    @Override
+    protected void doStopCamelContext(CamelContext context, Service camelContextService) {
+        //don't stop
+    }
+
+    @Override
+    protected void doSetUp() throws Exception {
+        if (context == null) {
+            super.doSetUp();
+        }
+    }
+
+    @Test
+    @Order(1)
+    public void initContextTest() throws Exception {
+        String expectedBody = "<matched/>";
+
+        resultEndpoint.expectedBodiesReceived(expectedBody);
+
+        template.sendBodyAndHeader(expectedBody, "foo", "bar");
+
+        resultEndpoint.assertIsSatisfied();
+        resultEndpoint.reset();
+
+    }
+
+    @Test
+    @Order(2)
+    public void stopNotEnabledTest() throws Exception {
+        String expectedBody = "<matched/>";
+
+        resultEndpoint.expectedBodiesReceived(expectedBody);
+
+        template.sendBodyAndHeader(expectedBody, "foo", "bar");
+
+        resultEndpoint.assertIsSatisfied();
+        resultEndpoint.reset();
+
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() {
+        return new RouteBuilder() {
+            public void configure() {
+                from("direct:start").filter(header("foo").isEqualTo("bar")).to("mock:result");
+            }
+        };
+    }
+
+    private static class MockContext extends DefaultCamelContext {
+
+        boolean initialized;
+
+        @Override
+        protected Registry createRegistry() {
+            if (initialized) {
+                throw new UnsupportedOperationException();
+            }
+            if (!initialized) {
+                initialized = true;
+            }
+            return new DefaultRegistry();
+        }
+
+        @Override
+        public void addRoutes(RoutesBuilder builder) throws Exception {
+            //if routes are already added, do not add them again
+            if (getRoutes().isEmpty()) {
+                super.addRoutes(builder);
+            }
+        }
+    }
+}