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..0125dfec8755b --- /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); + } + } + } +} diff --git a/components/camel-test/camel-test-junit5/src/test/java/org/apache/camel/test/junit5/CamelTestSupportTest.java b/components/camel-test/camel-test-junit5/src/test/java/org/apache/camel/test/junit5/CamelTestSupportTest.java deleted file mode 100644 index 4c8453f400d11..0000000000000 --- a/components/camel-test/camel-test-junit5/src/test/java/org/apache/camel/test/junit5/CamelTestSupportTest.java +++ /dev/null @@ -1,75 +0,0 @@ -/* - * 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.NoSuchEndpointException; -import org.apache.camel.builder.RouteBuilder; -import org.apache.camel.component.mock.MockEndpoint; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; - -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertThrows; - -public class CamelTestSupportTest extends CamelTestSupport { - - @Override - @BeforeEach - public void setUp() throws Exception { - replaceRouteFromWith("routeId", "direct:start"); - super.setUp(); - } - - @Test - public void replacesFromEndpoint() throws Exception { - getMockEndpoint("mock:result").expectedBodiesReceived("Hello World"); - - template.sendBody("direct:start", "Hello World"); - - assertMockEndpointsSatisfied(); - } - - @Test - public void exceptionThrownWhenEndpointNotFoundAndNoCreate() { - assertThrows(NoSuchEndpointException.class, () -> { - getMockEndpoint("mock:bogus", false); - }); - } - - @Test - public void exceptionThrownWhenEndpointNotAMockEndpoint() { - assertThrows(NoSuchEndpointException.class, () -> { - getMockEndpoint("direct:something", false); - }); - } - - @Test - public void autoCreateNonExisting() { - MockEndpoint mock = getMockEndpoint("mock:bogus2", true); - assertNotNull(mock); - } - - @Override - protected RouteBuilder createRouteBuilder() { - return new RouteBuilder() { - @Override - public void configure() { - from("direct:something").id("routeId").to("mock:result"); - } - }; - } -}