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 75ff061a6dba466085c40e1e865bf5f20331bfee Author: Otavio Rodolfo Piske <angusyo...@gmail.com> AuthorDate: Tue May 14 17:22:09 2024 +0200 (chores) camel-test-junit5: decouple the logging from the base class --- .../apache/camel/test/junit5/CamelTestSupport.java | 46 +++++------------- .../camel/test/junit5/util/ExtensionHelper.java | 32 +++++++++++++ .../junit5/util/RouteCoverageDumperExtension.java | 54 ++++++++++++++++++++++ 3 files changed, 97 insertions(+), 35 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 c35fc26ce9b..79bc99244fa 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 @@ -16,7 +16,6 @@ */ package org.apache.camel.test.junit5; -import java.lang.annotation.Annotation; import java.net.URISyntaxException; import java.util.HashMap; import java.util.Map; @@ -40,8 +39,6 @@ import org.apache.camel.RoutesBuilder; import org.apache.camel.RuntimeCamelException; import org.apache.camel.Service; import org.apache.camel.ServiceStatus; -import org.apache.camel.api.management.ManagedCamelContext; -import org.apache.camel.api.management.mbean.ManagedCamelContextMBean; import org.apache.camel.builder.AdviceWith; import org.apache.camel.builder.AdviceWithRouteBuilder; import org.apache.camel.builder.RouteBuilder; @@ -55,16 +52,14 @@ import org.apache.camel.model.ProcessorDefinition; import org.apache.camel.spi.CamelBeanPostProcessor; import org.apache.camel.spi.Language; import org.apache.camel.spi.PropertiesComponent; -import org.apache.camel.spi.PropertiesSource; import org.apache.camel.spi.Registry; import org.apache.camel.support.BreakpointSupport; import org.apache.camel.support.EndpointHelper; import org.apache.camel.support.PluginHelper; -import org.apache.camel.test.CamelRouteCoverageDumper; import org.apache.camel.test.junit5.util.ExtensionHelper; +import org.apache.camel.test.junit5.util.RouteCoverageDumperExtension; import org.apache.camel.util.StopWatch; import org.apache.camel.util.StringHelper; -import org.apache.camel.util.TimeUtils; import org.apache.camel.util.URISupport; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; @@ -81,6 +76,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import static org.apache.camel.test.junit5.TestSupport.isCamelDebugPresent; +import static org.apache.camel.test.junit5.util.ExtensionHelper.testStartHeader; import static org.junit.jupiter.api.Assertions.assertNotNull; /** @@ -103,7 +99,7 @@ public abstract class CamelTestSupport private static final ThreadLocal<FluentProducerTemplate> THREAD_FLUENT_TEMPLATE = new ThreadLocal<>(); private static final ThreadLocal<ConsumerTemplate> THREAD_CONSUMER = new ThreadLocal<>(); private static final ThreadLocal<Service> THREAD_SERVICE = new ThreadLocal<>(); - public static final String SEPARATOR = "********************************************************************************"; + protected Properties extra; protected volatile ModelCamelContext context; protected volatile ProducerTemplate template; @@ -120,7 +116,7 @@ public abstract class CamelTestSupport private static final ThreadLocal<CamelTestSupport> INSTANCE = new ThreadLocal<>(); private String currentTestName; private boolean isCreateCamelContextPerClass = false; - private final CamelRouteCoverageDumper routeCoverageDumper = new CamelRouteCoverageDumper(); + private ExtensionContext.Store globalStore; @Override @@ -133,6 +129,7 @@ public abstract class CamelTestSupport watch.restart(); } + @Deprecated(since = "4.7.0") public long timeTaken() { return watch.taken(); } @@ -344,9 +341,7 @@ public abstract class CamelTestSupport @BeforeEach public void setUp() throws Exception { - LOG.info(SEPARATOR); - LOG.info("Testing: {} ({})", currentTestName, getClass().getName()); - LOG.info(SEPARATOR); + testStartHeader(getClass(), currentTestName); doSpringBootCheck(); doQuarkusCheck(); @@ -365,6 +360,8 @@ public abstract class CamelTestSupport watch.restart(); } + + private void createCamelContextPerClass() throws Exception { INSTANCE.set(this); AtomicInteger v = TESTS.get(); @@ -590,15 +587,11 @@ public abstract class CamelTestSupport public void tearDown() throws Exception { long time = watch.taken(); - LOG.info(SEPARATOR); - LOG.info("Testing done: {} ({})", currentTestName, getClass().getName()); - LOG.info("Took: {} ({} millis)", TimeUtils.printDuration(time, true), time); - - // if we should dump route stats, then write that to a file if (isRouteCoverageEnabled()) { - dumpRouteCoverage(); + ExtensionHelper.testEndFooter(getClass(), currentTestName, time, new RouteCoverageDumperExtension(context)); + } else { + ExtensionHelper.testEndFooter(getClass(), currentTestName, time); } - LOG.info(SEPARATOR); if (isCreateCamelContextPerClass()) { // will tear down test specially in afterAll callback @@ -613,23 +606,6 @@ public abstract class CamelTestSupport } - private void dumpRouteCoverage() throws Exception { - String className = this.getClass().getSimpleName(); - String dir = "target/camel-route-coverage"; - String name = className + "-" + StringHelper.before(currentTestName, "(") + ".xml"; - - ManagedCamelContext mc - = context != null ? context.getCamelContextExtension().getContextPlugin(ManagedCamelContext.class) : null; - ManagedCamelContextMBean managedCamelContext = mc != null ? mc.getManagedCamelContext() : null; - if (managedCamelContext == null) { - LOG.warn("Cannot dump route coverage to file as JMX is not enabled. " - + "Add camel-management JAR as dependency and/or override useJmx() method to enable JMX in the unit test classes."); - } else { - routeCoverageDumper.dump(managedCamelContext, context, dir, name, getClass().getName(), currentTestName, - timeTaken()); - } - } - void tearDownCreateCamelContextPerClass() throws Exception { LOG.debug("tearDownCreateCamelContextPerClass()"); TESTS.remove(); diff --git a/components/camel-test/camel-test-junit5/src/main/java/org/apache/camel/test/junit5/util/ExtensionHelper.java b/components/camel-test/camel-test-junit5/src/main/java/org/apache/camel/test/junit5/util/ExtensionHelper.java index 64f356b78ac..9b8bddfe0f9 100644 --- a/components/camel-test/camel-test-junit5/src/main/java/org/apache/camel/test/junit5/util/ExtensionHelper.java +++ b/components/camel-test/camel-test-junit5/src/main/java/org/apache/camel/test/junit5/util/ExtensionHelper.java @@ -19,7 +19,13 @@ package org.apache.camel.test.junit5.util; import java.lang.annotation.Annotation; +import org.apache.camel.util.TimeUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + public final class ExtensionHelper { + private static final Logger LOG = LoggerFactory.getLogger(ExtensionHelper.class); + public static final String SEPARATOR = "*".repeat(80); /** * Does the test class have any of the following annotations on the class-level? @@ -36,4 +42,30 @@ public final class ExtensionHelper { } return false; } + + public static void testStartHeader(Class<?> testClass, String currentTestName) { + LOG.info(SEPARATOR); + LOG.info("Testing: {} ({})", currentTestName, testClass.getName()); + LOG.info(SEPARATOR); + } + + public static void testEndFooter(Class<?> testClass, String currentTestName, long time) { + LOG.info(SEPARATOR); + LOG.info("Testing done: {} ({})", currentTestName, testClass.getName()); + LOG.info("Took: {} ({} millis)", TimeUtils.printDuration(time, true), time); + LOG.info(SEPARATOR); + } + + public static void testEndFooter(Class<?> testClass, String currentTestName, long time, RouteCoverageDumperExtension routeCoverageWrapper) + throws Exception { + LOG.info(SEPARATOR); + LOG.info("Testing done: {} ({})", currentTestName, testClass.getName()); + LOG.info("Took: {} ({} millis)", TimeUtils.printDuration(time, true), time); + + if (routeCoverageWrapper != null) { + routeCoverageWrapper.dumpRouteCoverage(testClass, currentTestName, time); + } + + LOG.info(SEPARATOR); + } } diff --git a/components/camel-test/camel-test-junit5/src/main/java/org/apache/camel/test/junit5/util/RouteCoverageDumperExtension.java b/components/camel-test/camel-test-junit5/src/main/java/org/apache/camel/test/junit5/util/RouteCoverageDumperExtension.java new file mode 100644 index 00000000000..6d0ac31a8d5 --- /dev/null +++ b/components/camel-test/camel-test-junit5/src/main/java/org/apache/camel/test/junit5/util/RouteCoverageDumperExtension.java @@ -0,0 +1,54 @@ +/* + * 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.util; + +import org.apache.camel.api.management.ManagedCamelContext; +import org.apache.camel.api.management.mbean.ManagedCamelContextMBean; +import org.apache.camel.model.ModelCamelContext; +import org.apache.camel.test.CamelRouteCoverageDumper; +import org.apache.camel.util.StringHelper; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class RouteCoverageDumperExtension { + private static final Logger LOG = LoggerFactory.getLogger(RouteCoverageDumperExtension.class); + private final ModelCamelContext context; + + public RouteCoverageDumperExtension(ModelCamelContext context) { + this.context = context; + } + + public void dumpRouteCoverage(Class<?> testClass, String currentTestName, long taken) throws Exception { + String className = testClass.getSimpleName(); + String dir = "target/camel-route-coverage"; + String name = className + "-" + StringHelper.before(currentTestName, "(") + ".xml"; + + CamelRouteCoverageDumper routeCoverageDumper = new CamelRouteCoverageDumper(); + + ManagedCamelContext mc + = context != null ? context.getCamelContextExtension().getContextPlugin(ManagedCamelContext.class) : null; + ManagedCamelContextMBean managedCamelContext = mc != null ? mc.getManagedCamelContext() : null; + if (managedCamelContext == null) { + LOG.warn("Cannot dump route coverage to file as JMX is not enabled. " + + "Add camel-management JAR as dependency and/or override useJmx() method to enable JMX in the unit test classes."); + } else { + routeCoverageDumper.dump(managedCamelContext, context, dir, name, getClass().getName(), currentTestName, + taken); + } + } +}