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 427c19faa5d2e5a3534f244e30355ec7134422e5 Author: Otavio Rodolfo Piske <angusyo...@gmail.com> AuthorDate: Tue May 14 17:42:22 2024 +0200 (chores) camel-test-junit5: decouple supported/unsupported check --- .../apache/camel/test/junit5/CamelTestSupport.java | 18 +++++----- .../camel/test/junit5/util/ExtensionHelper.java | 41 ++++++++++++++++++++-- 2 files changed, 48 insertions(+), 11 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 79bc99244fa..bd54b3196d7 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 @@ -343,8 +343,7 @@ public abstract class CamelTestSupport public void setUp() throws Exception { testStartHeader(getClass(), currentTestName); - doSpringBootCheck(); - doQuarkusCheck(); + ExtensionHelper.hasUnsupported(getClass()); if (isCreateCamelContextPerClass()) { createCamelContextPerClass(); @@ -360,8 +359,6 @@ public abstract class CamelTestSupport watch.restart(); } - - private void createCamelContextPerClass() throws Exception { INSTANCE.set(this); AtomicInteger v = TESTS.get(); @@ -372,7 +369,6 @@ public abstract class CamelTestSupport if (v.getAndIncrement() == 0) { LOG.debug("Setup CamelContext before running first test"); // test is per class, so only setup once (the first time) - doSpringBootCheck(); setupResources(); doPreSetup(); doSetUp(); @@ -401,10 +397,12 @@ public abstract class CamelTestSupport /** * Detects if this is a Spring-Boot test and throws an exception, as these base classes is not intended for testing - * Camel on Spring Boot. + * Camel on Spring Boot. Use ExtensionHelper.hasClassAnnotation instead */ + @Deprecated(since = "4.7.0") protected void doSpringBootCheck() { - boolean springBoot = ExtensionHelper.hasClassAnnotation(getClass(),"org.springframework.boot.test.context.SpringBootTest"); + boolean springBoot + = ExtensionHelper.hasClassAnnotation(getClass(), "org.springframework.boot.test.context.SpringBootTest"); if (springBoot) { throw new RuntimeException( "Spring Boot detected: The CamelTestSupport/CamelSpringTestSupport class is not intended for Camel testing with Spring Boot."); @@ -413,10 +411,12 @@ public abstract class CamelTestSupport /** * Detects if this is a Camel-quarkus test and throw an exception, as these base classes is not intended for testing - * Camel onQuarkus. + * Camel onQuarkus. Use ExtensionHelper.hasClassAnnotation instead. */ + @Deprecated(since = "4.7.0") protected void doQuarkusCheck() { - boolean quarkus = ExtensionHelper.hasClassAnnotation(getClass(), "io.quarkus.test.junit.QuarkusTest", "org.apache.camel.quarkus.test.CamelQuarkusTest"); + boolean quarkus = ExtensionHelper.hasClassAnnotation(getClass(), "io.quarkus.test.junit.QuarkusTest", + "org.apache.camel.quarkus.test.CamelQuarkusTest"); if (quarkus) { throw new RuntimeException( "Quarkus detected: The CamelTestSupport/CamelSpringTestSupport class is not intended for Camel testing with Quarkus."); 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 9b8bddfe0f9..f1d3a2b1762 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 @@ -18,6 +18,7 @@ package org.apache.camel.test.junit5.util; import java.lang.annotation.Annotation; +import java.util.function.Consumer; import org.apache.camel.util.TimeUtils; import org.slf4j.Logger; @@ -26,11 +27,46 @@ import org.slf4j.LoggerFactory; public final class ExtensionHelper { private static final Logger LOG = LoggerFactory.getLogger(ExtensionHelper.class); public static final String SEPARATOR = "*".repeat(80); + private static final String SPRING_BOOT_TEST = "org.springframework.boot.test.context.SpringBootTest"; + private static final String QUARKUS_TEST = "io.quarkus.test.junit.QuarkusTest"; + private static final String CAMEL_QUARKUS_TEST = "org.apache.camel.quarkus.test.CamelQuarkusTest"; + + private static void throwUnsupportedClassException(String name) { + switch (name) { + case SPRING_BOOT_TEST: + throw new RuntimeException( + "Spring Boot detected: The CamelTestSupport/CamelSpringTestSupport class is not intended for Camel testing with Spring Boot."); + case QUARKUS_TEST: + case CAMEL_QUARKUS_TEST: { + throw new RuntimeException( + "Quarkus detected: The CamelTestSupport/CamelSpringTestSupport class is not intended for Camel testing with Quarkus."); + } + } + + throw new RuntimeException( + "Unspecified class detected: The " + name + " class is not intended for Camel testing"); + } + + public static boolean hasUnsupported(Class<?> clazz) { + hasClassAnnotation(clazz, ExtensionHelper::throwUnsupportedClassException, SPRING_BOOT_TEST, QUARKUS_TEST, + CAMEL_QUARKUS_TEST); + return true; + } /** * Does the test class have any of the following annotations on the class-level? - * @return true if has or false otherwise */ + public static void hasClassAnnotation(Class<?> clazz, Consumer<String> classConsumer, String... names) { + for (String name : names) { + for (Annotation ann : clazz.getAnnotations()) { + String annName = ann.annotationType().getName(); + if (annName.equals(name)) { + classConsumer.accept(name); + } + } + } + } + public static boolean hasClassAnnotation(Class<?> clazz, String... names) { for (String name : names) { for (Annotation ann : clazz.getAnnotations()) { @@ -56,7 +92,8 @@ public final class ExtensionHelper { LOG.info(SEPARATOR); } - public static void testEndFooter(Class<?> testClass, String currentTestName, long time, RouteCoverageDumperExtension routeCoverageWrapper) + public static void testEndFooter( + Class<?> testClass, String currentTestName, long time, RouteCoverageDumperExtension routeCoverageWrapper) throws Exception { LOG.info(SEPARATOR); LOG.info("Testing done: {} ({})", currentTestName, testClass.getName());