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 246df70494a CAMEL-20838: implement logging via extension
246df70494a is described below
commit 246df70494ac14c166e508e9686340271f4bcc3f
Author: Otavio Rodolfo Piske <[email protected]>
AuthorDate: Wed Jun 19 12:09:58 2024 +0200
CAMEL-20838: implement logging via extension
---
.../apache/camel/test/junit5/CamelTestSupport.java | 13 ++--
.../camel/test/junit5/TestLoggerExtension.java | 82 ++++++++++++++++++++++
2 files changed, 89 insertions(+), 6 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 a2b8c2e7ed6..b628fec7340 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
@@ -37,6 +37,7 @@ import
org.apache.camel.test.junit5.util.RouteCoverageDumperExtension;
import org.apache.camel.util.StopWatch;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.TestInstance.Lifecycle;
import org.junit.jupiter.api.extension.AfterAllCallback;
import org.junit.jupiter.api.extension.AfterEachCallback;
@@ -49,7 +50,6 @@ import org.junit.jupiter.api.extension.RegisterExtension;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import static
org.apache.camel.test.junit5.util.ExtensionHelper.testStartHeader;
import static org.junit.jupiter.api.Assertions.assertNotNull;
/**
@@ -62,6 +62,10 @@ public abstract class CamelTestSupport extends
AbstractTestSupport
AfterTestExecutionCallback {
private static final Logger LOG =
LoggerFactory.getLogger(CamelTestSupport.class);
+ @RegisterExtension
+ @Order(10)
+ protected TestLoggerExtension testLoggerExtension = new
TestLoggerExtension();
+
@RegisterExtension
protected CamelTestSupport camelTestSupportExtension = this;
private final StopWatch watch = new StopWatch();
@@ -165,8 +169,6 @@ public abstract class CamelTestSupport extends
AbstractTestSupport
@Deprecated(since = "4.7.0")
@BeforeEach
public void setUp() throws Exception {
- testStartHeader(getClass(), currentTestName);
-
unsupportedCheck();
setupResources();
@@ -254,9 +256,8 @@ public abstract class CamelTestSupport extends
AbstractTestSupport
long time = watch.taken();
if (isRouteCoverageEnabled()) {
- ExtensionHelper.testEndFooter(getClass(), currentTestName, time,
new RouteCoverageDumperExtension(context));
- } else {
- ExtensionHelper.testEndFooter(getClass(), currentTestName, time);
+ final RouteCoverageDumperExtension routeCoverageWrapper = new
RouteCoverageDumperExtension(context);
+ routeCoverageWrapper.dumpRouteCoverage(getClass(),
currentTestName, time);
}
if (testConfigurationBuilder.isCreateCamelContextPerClass()) {
diff --git
a/components/camel-test/camel-test-junit5/src/main/java/org/apache/camel/test/junit5/TestLoggerExtension.java
b/components/camel-test/camel-test-junit5/src/main/java/org/apache/camel/test/junit5/TestLoggerExtension.java
new file mode 100644
index 00000000000..9a8332b45d5
--- /dev/null
+++
b/components/camel-test/camel-test-junit5/src/main/java/org/apache/camel/test/junit5/TestLoggerExtension.java
@@ -0,0 +1,82 @@
+/*
+ * 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 java.lang.reflect.Method;
+
+import org.apache.camel.util.StopWatch;
+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;
+import org.junit.jupiter.api.extension.ExtensionContext.Namespace;
+
+import static org.apache.camel.test.junit5.util.ExtensionHelper.testEndFooter;
+import static
org.apache.camel.test.junit5.util.ExtensionHelper.testStartHeader;
+
+public class TestLoggerExtension
+ implements BeforeEachCallback, AfterEachCallback,
BeforeTestExecutionCallback,
+ AfterTestExecutionCallback {
+ private static final String START_TIME = "start time";
+ private String currentTestName;
+
+ @Override
+ public void afterEach(ExtensionContext context) throws Exception {
+ final Class<?> testClass =
context.getTestClass().orElse(this.getClass());
+ StopWatch durationTracker = getStore(context,
testClass).remove(START_TIME, StopWatch.class);
+
+ Method testMethod = context.getRequiredTestMethod();
+ if (durationTracker != null) {
+ testEndFooter(testClass, testMethod.getName(),
durationTracker.taken());
+ } else {
+ /*
+ * Extensions may be called twice for Nested methods.
+ * As such, we ignore the last one as it's related to the
+ * top-most class: https://github.com/junit-team/junit5/issues/2421
+ */
+ testEndFooter(testClass, testMethod.getName(), 0);
+ }
+
+ }
+
+ @Override
+ public void beforeEach(ExtensionContext context) throws Exception {
+ currentTestName = context.getDisplayName();
+ }
+
+ @Override
+ public void afterTestExecution(ExtensionContext context) {
+
+ }
+
+ @Override
+ public void beforeTestExecution(ExtensionContext context) {
+ currentTestName = context.getDisplayName();
+
+ final Class<?> testClass =
context.getTestClass().orElse(this.getClass());
+ testStartHeader(testClass, currentTestName);
+ final ExtensionContext.Store store = getStore(context, testClass);
+
+ store.getOrComputeIfAbsent(START_TIME, key -> new StopWatch(),
Object.class);
+ }
+
+ private ExtensionContext.Store getStore(ExtensionContext context, Class<?>
testClass) {
+ return context.getStore(Namespace.create(testClass,
context.getRequiredTestMethod()));
+ }
+}