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/25 08:11:52 UTC

(camel) branch main updated: CAMEL-20838: use a JUnit extension for collecting the test name when necessary

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 a35c86036cb CAMEL-20838: use a JUnit extension for collecting the test name when necessary
a35c86036cb is described below

commit a35c86036cb836636e1013b10a9dc47f8c35839f
Author: Otavio Rodolfo Piske <an...@gmail.com>
AuthorDate: Wed Jun 19 14:25:49 2024 +0200

    CAMEL-20838: use a JUnit extension for collecting the test name when necessary
---
 .../component/avro/AvroConsumerTestSupport.java    | 12 ++++-
 .../component/avro/AvroProducerTestSupport.java    |  9 +++-
 .../camel/component/consul/ConsulTestSupport.java  |  8 ++-
 .../es/integration/ElasticsearchTestSupport.java   |  8 ++-
 .../file/watch/FileWatchComponentTestBase.java     | 20 ++++---
 .../component/nitrite/AbstractNitriteTest.java     |  8 ++-
 .../apache/camel/test/junit5/CamelTestSupport.java |  5 ++
 .../camel/test/junit5/TestNameExtension.java       | 63 ++++++++++++++++++++++
 8 files changed, 121 insertions(+), 12 deletions(-)

diff --git a/components/camel-avro-rpc/camel-avro-rpc-component/src/test/java/org/apache/camel/component/avro/AvroConsumerTestSupport.java b/components/camel-avro-rpc/camel-avro-rpc-component/src/test/java/org/apache/camel/component/avro/AvroConsumerTestSupport.java
index 3f8585680bf..cc8f7cb2dec 100644
--- a/components/camel-avro-rpc/camel-avro-rpc-component/src/test/java/org/apache/camel/component/avro/AvroConsumerTestSupport.java
+++ b/components/camel-avro-rpc/camel-avro-rpc-component/src/test/java/org/apache/camel/component/avro/AvroConsumerTestSupport.java
@@ -27,7 +27,10 @@ import org.apache.camel.avro.impl.KeyValueProtocolImpl;
 import org.apache.camel.avro.test.TestPojo;
 import org.apache.camel.avro.test.TestReflection;
 import org.apache.camel.avro.test.TestReflectionImpl;
+import org.apache.camel.test.junit5.TestNameExtension;
+import org.junit.jupiter.api.Order;
 import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.RegisterExtension;
 
 import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
 import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -55,6 +58,10 @@ public abstract class AvroConsumerTestSupport extends AvroTestSupport {
     KeyValueProtocolImpl keyValue = new KeyValueProtocolImpl();
     TestReflection testReflection = new TestReflectionImpl();
 
+    @RegisterExtension
+    @Order(10)
+    TestNameExtension testNameExtension = new TestNameExtension();
+
     protected abstract void initializeTranceiver() throws IOException;
 
     @Override
@@ -187,7 +194,7 @@ public abstract class AvroConsumerTestSupport extends AvroTestSupport {
     }
 
     protected ConsumerRouteType getRouteType() {
-        switch (getCurrentTestName()) {
+        switch (testNameExtension.getCurrentTestName()) {
             case "testInOut()":
             case "testInOnly()":
                 return ConsumerRouteType.specific;
@@ -204,7 +211,8 @@ public abstract class AvroConsumerTestSupport extends AvroTestSupport {
             case "testInOnlyToNotExistingRoute()":
                 return ConsumerRouteType.specificProcessorWrong;
             default:
-                throw new IllegalStateException(String.format("Test '%s' is not listed.", getCurrentTestName()));
+                throw new IllegalStateException(
+                        String.format("Test '%s' is not listed.", testNameExtension.getCurrentTestName()));
         }
     }
 }
diff --git a/components/camel-avro-rpc/camel-avro-rpc-component/src/test/java/org/apache/camel/component/avro/AvroProducerTestSupport.java b/components/camel-avro-rpc/camel-avro-rpc-component/src/test/java/org/apache/camel/component/avro/AvroProducerTestSupport.java
index 2645b9cbfd7..c08490fb209 100644
--- a/components/camel-avro-rpc/camel-avro-rpc-component/src/test/java/org/apache/camel/component/avro/AvroProducerTestSupport.java
+++ b/components/camel-avro-rpc/camel-avro-rpc-component/src/test/java/org/apache/camel/component/avro/AvroProducerTestSupport.java
@@ -24,7 +24,10 @@ import org.apache.camel.avro.generated.Value;
 import org.apache.camel.avro.impl.KeyValueProtocolImpl;
 import org.apache.camel.avro.test.TestReflectionImpl;
 import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.test.junit5.TestNameExtension;
+import org.junit.jupiter.api.Order;
 import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.RegisterExtension;
 
 import static org.junit.jupiter.api.Assertions.assertEquals;
 
@@ -35,6 +38,10 @@ public abstract class AvroProducerTestSupport extends AvroTestSupport {
     KeyValueProtocolImpl keyValue = new KeyValueProtocolImpl();
     TestReflectionImpl testReflection = new TestReflectionImpl();
 
+    @RegisterExtension
+    @Order(10)
+    TestNameExtension testNameExtension = new TestNameExtension();
+
     protected abstract void initializeServer() throws IOException, InterruptedException;
 
     @Override
@@ -139,7 +146,7 @@ public abstract class AvroProducerTestSupport extends AvroTestSupport {
     }
 
     protected ProducerRouteType getRouteType() {
-        if (getCurrentTestName().contains("Reflection")) {
+        if (testNameExtension.getCurrentTestName().contains("Reflection")) {
             return ProducerRouteType.reflect;
         }
 
diff --git a/components/camel-consul/src/test/java/org/apache/camel/component/consul/ConsulTestSupport.java b/components/camel-consul/src/test/java/org/apache/camel/component/consul/ConsulTestSupport.java
index 137412f9d0f..7b1c2cf3f8c 100644
--- a/components/camel-consul/src/test/java/org/apache/camel/component/consul/ConsulTestSupport.java
+++ b/components/camel-consul/src/test/java/org/apache/camel/component/consul/ConsulTestSupport.java
@@ -24,6 +24,8 @@ import org.apache.camel.BindToRegistry;
 import org.apache.camel.test.infra.consul.services.ConsulService;
 import org.apache.camel.test.infra.consul.services.ConsulServiceFactory;
 import org.apache.camel.test.junit5.CamelTestSupport;
+import org.apache.camel.test.junit5.TestNameExtension;
+import org.junit.jupiter.api.Order;
 import org.junit.jupiter.api.extension.RegisterExtension;
 import org.kiwiproject.consul.Consul;
 
@@ -31,6 +33,10 @@ public class ConsulTestSupport extends CamelTestSupport {
     @RegisterExtension
     public static ConsulService service = ConsulServiceFactory.createService();
 
+    @RegisterExtension
+    @Order(10)
+    TestNameExtension testNameExtension = new TestNameExtension();
+
     public static final String KV_PREFIX = "/camel";
 
     @BindToRegistry("consul")
@@ -60,6 +66,6 @@ public class ConsulTestSupport extends CamelTestSupport {
     }
 
     protected String generateKey() {
-        return KV_PREFIX + "/" + getCurrentTestName() + "/" + generateRandomString();
+        return KV_PREFIX + "/" + testNameExtension.getCurrentTestName() + "/" + generateRandomString();
     }
 }
diff --git a/components/camel-elasticsearch/src/test/java/org/apache/camel/component/es/integration/ElasticsearchTestSupport.java b/components/camel-elasticsearch/src/test/java/org/apache/camel/component/es/integration/ElasticsearchTestSupport.java
index fe354fc579d..183b2ed44ea 100644
--- a/components/camel-elasticsearch/src/test/java/org/apache/camel/component/es/integration/ElasticsearchTestSupport.java
+++ b/components/camel-elasticsearch/src/test/java/org/apache/camel/component/es/integration/ElasticsearchTestSupport.java
@@ -27,6 +27,7 @@ import org.apache.camel.component.es.ElasticsearchComponent;
 import org.apache.camel.test.infra.elasticsearch.services.ElasticSearchService;
 import org.apache.camel.test.infra.elasticsearch.services.ElasticSearchServiceFactory;
 import org.apache.camel.test.junit5.CamelTestSupport;
+import org.apache.camel.test.junit5.TestNameExtension;
 import org.apache.http.HttpHost;
 import org.apache.http.auth.AuthScope;
 import org.apache.http.auth.UsernamePasswordCredentials;
@@ -34,6 +35,7 @@ import org.apache.http.client.CredentialsProvider;
 import org.apache.http.impl.client.BasicCredentialsProvider;
 import org.elasticsearch.client.RestClient;
 import org.elasticsearch.client.RestClientBuilder;
+import org.junit.jupiter.api.Order;
 import org.junit.jupiter.api.TestInstance;
 import org.junit.jupiter.api.extension.RegisterExtension;
 import org.slf4j.Logger;
@@ -45,6 +47,10 @@ public class ElasticsearchTestSupport extends CamelTestSupport {
     @RegisterExtension
     protected static ElasticSearchService service = ElasticSearchServiceFactory.createSingletonService();
 
+    @RegisterExtension
+    @Order(10)
+    TestNameExtension testNameExtension = new TestNameExtension();
+
     protected static String clusterName = "docker-cluster";
     protected static RestClient restClient;
     protected static ElasticsearchClient client;
@@ -119,7 +125,7 @@ public class ElasticsearchTestSupport extends CamelTestSupport {
 
     String createPrefix() {
         // make use of the test method name to avoid collision
-        return getCurrentTestName().toLowerCase() + "-";
+        return testNameExtension.getCurrentTestName().toLowerCase() + "-";
     }
 
     RestClient getClient() {
diff --git a/components/camel-file-watch/src/test/java/org/apache/camel/component/file/watch/FileWatchComponentTestBase.java b/components/camel-file-watch/src/test/java/org/apache/camel/component/file/watch/FileWatchComponentTestBase.java
index fb6d9ce8ee4..93ecf80fc96 100644
--- a/components/camel-file-watch/src/test/java/org/apache/camel/component/file/watch/FileWatchComponentTestBase.java
+++ b/components/camel-file-watch/src/test/java/org/apache/camel/component/file/watch/FileWatchComponentTestBase.java
@@ -28,6 +28,10 @@ import java.util.List;
 import org.apache.camel.Exchange;
 import org.apache.camel.component.file.watch.constants.FileEventEnum;
 import org.apache.camel.test.junit5.CamelTestSupport;
+import org.apache.camel.test.junit5.TestNameExtension;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.Order;
+import org.junit.jupiter.api.extension.RegisterExtension;
 import org.junit.jupiter.api.io.TempDir;
 
 import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -36,6 +40,10 @@ import static org.springframework.test.util.AssertionErrors.assertTrue;
 
 public class FileWatchComponentTestBase extends CamelTestSupport {
 
+    @RegisterExtension
+    @Order(10)
+    TestNameExtension testNameExtension = new TestNameExtension();
+
     @TempDir
     public Path folder;
 
@@ -49,20 +57,20 @@ public class FileWatchComponentTestBase extends CamelTestSupport {
     @Override
     protected void doPreSetup() throws Exception {
         cleanTestDir(new File(testPath()));
-        new File(testPath()).mkdirs();
+        Files.createDirectories(Path.of(testPath()));
         for (int i = 0; i < 10; i++) {
-            File newFile = new File(testPath(), getCurrentTestName() + "-" + i);
+            File newFile = new File(testPath(), testNameExtension.getCurrentTestName() + "-" + i);
             assumeTrue(newFile.createNewFile());
             testFiles.add(newFile.toPath());
         }
     }
 
-    @Override
-    public void doPostTearDown() throws Exception {
+    @AfterEach
+    public void cleanupTestDir() throws Exception {
         cleanTestDir(new File(testPath()));
     }
 
-    private void cleanTestDir(File file) throws Exception {
+    private static void cleanTestDir(File file) throws Exception {
         if (file == null || !file.exists() || file.listFiles() == null) {
             return;
         }
@@ -85,7 +93,7 @@ public class FileWatchComponentTestBase extends CamelTestSupport {
         try {
             return folder.toRealPath()
                    + folder.getFileSystem().getSeparator()
-                   + getClass().getSimpleName() + "_" + getCurrentTestName()
+                   + getClass().getSimpleName() + "_" + testNameExtension.getCurrentTestName()
                    + folder.getFileSystem().getSeparator();
         } catch (IOException e) {
             throw new IOError(e);
diff --git a/components/camel-nitrite/src/test/java/org/apache/camel/component/nitrite/AbstractNitriteTest.java b/components/camel-nitrite/src/test/java/org/apache/camel/component/nitrite/AbstractNitriteTest.java
index a7530a467c1..1dbc9e823b8 100644
--- a/components/camel-nitrite/src/test/java/org/apache/camel/component/nitrite/AbstractNitriteTest.java
+++ b/components/camel-nitrite/src/test/java/org/apache/camel/component/nitrite/AbstractNitriteTest.java
@@ -21,9 +21,15 @@ import java.util.List;
 
 import org.apache.camel.Exchange;
 import org.apache.camel.test.junit5.CamelTestSupport;
+import org.apache.camel.test.junit5.TestNameExtension;
 import org.apache.camel.util.FileUtil;
+import org.junit.jupiter.api.Order;
+import org.junit.jupiter.api.extension.RegisterExtension;
 
 public abstract class AbstractNitriteTest extends CamelTestSupport {
+    @RegisterExtension
+    @Order(10)
+    TestNameExtension testNameExtension = new TestNameExtension();
 
     @Override
     protected void doPreSetup() throws Exception {
@@ -32,7 +38,7 @@ public abstract class AbstractNitriteTest extends CamelTestSupport {
     }
 
     protected String tempDb() {
-        return "target/" + getClass().getSimpleName() + "_" + getCurrentTestName() + ".db";
+        return "target/" + getClass().getSimpleName() + "_" + testNameExtension.getCurrentTestName() + ".db";
     }
 
     protected List<Exchange> sortByChangeTimestamp(List<Exchange> input) {
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 b628fec7340..ec4c7335369 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
@@ -69,6 +69,8 @@ public abstract class CamelTestSupport extends AbstractTestSupport
     @RegisterExtension
     protected CamelTestSupport camelTestSupportExtension = this;
     private final StopWatch watch = new StopWatch();
+
+    @Deprecated
     private String currentTestName;
 
     private CamelContextManager contextManager;
@@ -155,7 +157,10 @@ public abstract class CamelTestSupport extends AbstractTestSupport
 
     /**
      * Gets the name of the current test being executed.
+     *
+     * @deprecated Use JUnit's TestInfo class or the {@link TestNameExtension}
      */
+    @Deprecated(since = "4.7.0")
     public final String getCurrentTestName() {
         return currentTestName;
     }
diff --git a/components/camel-test/camel-test-junit5/src/main/java/org/apache/camel/test/junit5/TestNameExtension.java b/components/camel-test/camel-test-junit5/src/main/java/org/apache/camel/test/junit5/TestNameExtension.java
new file mode 100644
index 00000000000..e7f62e73960
--- /dev/null
+++ b/components/camel-test/camel-test-junit5/src/main/java/org/apache/camel/test/junit5/TestNameExtension.java
@@ -0,0 +1,63 @@
+/*
+ * 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.junit.jupiter.api.extension.AfterEachCallback;
+import org.junit.jupiter.api.extension.AfterTestExecutionCallback;
+import org.junit.jupiter.api.extension.BeforeEachCallback;
+import org.junit.jupiter.api.extension.BeforeTestExecutionCallback;
+import org.junit.jupiter.api.extension.ExtensionContext;
+
+/**
+ * Helper utility to get the test name for legacy test. Do not use. Prefer to use JUnit's TestInfo whenever possible.
+ */
+public class TestNameExtension
+        implements BeforeEachCallback, AfterEachCallback, BeforeTestExecutionCallback,
+        AfterTestExecutionCallback {
+    private static final String START_TIME = "start time";
+    private String currentTestName;
+
+    @Override
+    public void afterEach(ExtensionContext context) throws Exception {
+        currentTestName = context.getDisplayName();
+    }
+
+    @Override
+    public void beforeEach(ExtensionContext context) throws Exception {
+        currentTestName = context.getDisplayName();
+    }
+
+    @Override
+    public void afterTestExecution(ExtensionContext context) {
+        currentTestName = context.getDisplayName();
+    }
+
+    @Override
+    public void beforeTestExecution(ExtensionContext context) {
+        currentTestName = context.getDisplayName();
+    }
+
+    /**
+     * Ges the current test name
+     *
+     * @return the current test name
+     */
+    public String getCurrentTestName() {
+        return currentTestName;
+    }
+}