From a4afdbc833f85376f5d05c4174effa61e4045911 Mon Sep 17 00:00:00 2001 From: JiriOndrusek Date: Fri, 17 Jun 2022 13:12:49 +0200 Subject: [PATCH] CAMEL-18201 Enhance CamelTestSupport to turn off context stopping --- .../camel/test/junit5/CamelTestSupport.java | 4 +- .../CamelTestSupporOneContextForAllTest.java | 129 ++++++++++++++++++ 2 files changed, 131 insertions(+), 2 deletions(-) create mode 100644 components/camel-test/camel-test-junit5/src/test/java/org/apache/camel/test/junit5/CamelTestSupporOneContextForAllTest.java 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 dfb8014f5a749..d3a13c8236fe6 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 @@ protected void doSpringBootCheck() { } } - 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 @@ protected void stopCamelContext() throws Exception { 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 0000000000000..3f8c0ffdbe0d3 --- /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 = ""; + + resultEndpoint.expectedBodiesReceived(expectedBody); + + template.sendBodyAndHeader(expectedBody, "foo", "bar"); + + resultEndpoint.assertIsSatisfied(); + resultEndpoint.reset(); + + } + + @Test + @Order(2) + public void stopNotEnabledTest() throws Exception { + String expectedBody = ""; + + 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); + } + } + } +}