You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by or...@apache.org on 2024/06/06 06:36:48 UTC
(camel) branch main updated: CAMEL-20785: provide a way to create context managers when needed
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
The following commit(s) were added to refs/heads/main by this push:
new 15771f1457b CAMEL-20785: provide a way to create context managers when needed
15771f1457b is described below
commit 15771f1457b3ea1cef292acb5cb5fc14442fc57d
Author: Otavio Rodolfo Piske <an...@gmail.com>
AuthorDate: Wed Jun 5 11:31:40 2024 +0200
CAMEL-20785: provide a way to create context managers when needed
---
.../apache/camel/test/junit5/CamelTestSupport.java | 15 +++++--
.../camel/test/junit5/ContextManagerFactory.java | 49 ++++++++++++++++++++++
2 files changed, 61 insertions(+), 3 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 d908b8a0428..4c3d098f6ba 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
@@ -94,8 +94,11 @@ public abstract class CamelTestSupport
private final CamelContextConfiguration camelContextConfiguration;
private CamelContextManager contextManager;
+ private final ContextManagerFactory contextManagerFactory;
+
+ protected CamelTestSupport(ContextManagerFactory contextManagerFactory) {
+ this.contextManagerFactory = contextManagerFactory;
- protected CamelTestSupport() {
testConfigurationBuilder = new TestExecutionConfiguration();
testConfigurationBuilder.withJMX(useJmx())
.withUseRouteBuilder(isUseRouteBuilder())
@@ -116,6 +119,10 @@ public abstract class CamelTestSupport
.withMockEndpointsAndSkip(isMockEndpointsAndSkip());
}
+ protected CamelTestSupport() {
+ this(new ContextManagerFactory());
+ }
+
@Override
public void afterTestExecution(ExtensionContext context) throws Exception {
watch.taken();
@@ -136,7 +143,8 @@ public abstract class CamelTestSupport
public void beforeEach(ExtensionContext context) throws Exception {
if (contextManager == null) {
LOG.trace("Creating a transient context manager for {}", context.getDisplayName());
- contextManager = new TransientCamelContextManager(testConfigurationBuilder, camelContextConfiguration);
+ contextManager = contextManagerFactory.createContextManager(ContextManagerFactory.Type.BEFORE_EACH,
+ testConfigurationBuilder, camelContextConfiguration);
}
currentTestName = context.getDisplayName();
@@ -156,7 +164,8 @@ public abstract class CamelTestSupport
if (perClassPresent) {
LOG.trace("Creating a legacy context manager for {}", context.getDisplayName());
testConfigurationBuilder.withCreateCamelContextPerClass(perClassPresent);
- contextManager = new LegacyCamelContextManager(testConfigurationBuilder, camelContextConfiguration);
+ contextManager = contextManagerFactory.createContextManager(ContextManagerFactory.Type.BEFORE_ALL,
+ testConfigurationBuilder, camelContextConfiguration);
}
ExtensionContext.Store globalStore = context.getStore(ExtensionContext.Namespace.GLOBAL);
diff --git a/components/camel-test/camel-test-junit5/src/main/java/org/apache/camel/test/junit5/ContextManagerFactory.java b/components/camel-test/camel-test-junit5/src/main/java/org/apache/camel/test/junit5/ContextManagerFactory.java
new file mode 100644
index 00000000000..8d22eef2894
--- /dev/null
+++ b/components/camel-test/camel-test-junit5/src/main/java/org/apache/camel/test/junit5/ContextManagerFactory.java
@@ -0,0 +1,49 @@
+/*
+ * 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;
+
+/**
+ * A factory class for creating context managers
+ */
+public class ContextManagerFactory {
+ /**
+ * Callback types
+ */
+ public enum Type {
+ BEFORE_ALL,
+ BEFORE_EACH,
+ }
+
+ /**
+ * Creates a new context manager
+ *
+ * @param type callback type that initiated the request
+ * @param testConfiguration the test configuration instance
+ * @param contextConfiguration the context configuration instance
+ * @return a new context manager instance
+ */
+ public CamelContextManager createContextManager(
+ Type type, TestExecutionConfiguration testConfiguration, CamelContextConfiguration contextConfiguration) {
+ return switch (type) {
+ case BEFORE_ALL:
+ yield new LegacyCamelContextManager(testConfiguration, contextConfiguration);
+ case BEFORE_EACH:
+ yield new TransientCamelContextManager(testConfiguration, contextConfiguration);
+ };
+ }
+}