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 f0d174649ad64663f164a63ac9ba8f9907a4b3cf
Author: Otavio Rodolfo Piske <angusyo...@gmail.com>
AuthorDate: Tue May 21 13:30:18 2024 +0200

    CAMEL-20785: cleanup setting up a breakpoint debugger
---
 .../apache/camel/test/junit5/CamelTestSupport.java | 50 +++++-----------
 .../apache/camel/test/junit5/DebugBreakpoint.java  | 67 ++++++++++++++++++++++
 .../test/junit5/util/CamelContextTestHelper.java   | 23 ++++++++
 .../test/junit5/patterns/DebugJUnit5Test.java      | 16 ++----
 .../patterns/DebugNoLazyTypeConverterTest.java     | 14 +----
 .../camel/test/junit5/patterns/DebugTest.java      | 14 +----
 .../test/junit5/patterns/TestDebugBreakpoint.java  | 46 +++++++++++++++
 .../camel/test/patterns/DebugSpringTest.java       | 27 ++++++---
 8 files changed, 180 insertions(+), 77 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 2ef7f8fdc98..e624b3a0b27 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
@@ -29,7 +29,6 @@ import org.apache.camel.Exchange;
 import org.apache.camel.Expression;
 import org.apache.camel.FluentProducerTemplate;
 import org.apache.camel.Message;
-import org.apache.camel.NamedNode;
 import org.apache.camel.NoSuchEndpointException;
 import org.apache.camel.Predicate;
 import org.apache.camel.Processor;
@@ -38,14 +37,12 @@ import org.apache.camel.RouteConfigurationsBuilder;
 import org.apache.camel.RoutesBuilder;
 import org.apache.camel.RuntimeCamelException;
 import org.apache.camel.Service;
-import org.apache.camel.ServiceStatus;
 import org.apache.camel.builder.AdviceWith;
 import org.apache.camel.builder.AdviceWithRouteBuilder;
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.component.mock.InterceptSendToMockEndpointStrategy;
 import org.apache.camel.component.mock.MockEndpoint;
 import org.apache.camel.impl.DefaultCamelContext;
-import org.apache.camel.impl.debugger.DefaultDebugger;
 import org.apache.camel.model.Model;
 import org.apache.camel.model.ModelCamelContext;
 import org.apache.camel.model.ProcessorDefinition;
@@ -53,7 +50,6 @@ import org.apache.camel.spi.CamelBeanPostProcessor;
 import org.apache.camel.spi.Language;
 import org.apache.camel.spi.PropertiesComponent;
 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.junit5.util.CamelContextTestHelper;
@@ -110,7 +106,6 @@ public abstract class CamelTestSupport
     @RegisterExtension
     protected CamelTestSupport camelTestSupportExtension = this;
     private boolean useRouteBuilder = true;
-    private final DebugBreakpoint breakpoint = new DebugBreakpoint();
     private final StopWatch watch = new StopWatch();
     private final Map<String, String> fromEndpoints = new HashMap<>();
     private static final ThreadLocal<AtomicInteger> TESTS = new 
ThreadLocal<>();
@@ -119,6 +114,7 @@ public abstract class CamelTestSupport
     private boolean isCreateCamelContextPerClass = false;
 
     private ExtensionContext.Store globalStore;
+    private DebugBreakpoint breakpoint;
 
     @Override
     public void afterTestExecution(ExtensionContext context) throws Exception {
@@ -448,7 +444,7 @@ public abstract class CamelTestSupport
 
         // set debugger if enabled
         if (isUseDebugger()) {
-            setupDebugger();
+            CamelContextTestHelper.setupDebugger(context, createBreakpoint());
         }
 
         setupTemplates();
@@ -558,18 +554,6 @@ public abstract class CamelTestSupport
         }
     }
 
-    private void setupDebugger() {
-        if (context.getStatus().equals(ServiceStatus.Started)) {
-            LOG.info("Cannot setting the Debugger to the starting 
CamelContext, stop the CamelContext now.");
-            // we need to stop the context first to setup the debugger
-            context.stop();
-        }
-        context.setDebugging(true);
-        context.setDebugger(new DefaultDebugger());
-        context.getDebugger().addBreakpoint(breakpoint);
-        // when stopping CamelContext it will automatically remove the 
breakpoint
-    }
-
     private void replaceFromEndpoints() throws Exception {
         for (final Map.Entry<String, String> entry : fromEndpoints.entrySet()) 
{
             AdviceWith.adviceWith(context.getRouteDefinition(entry.getKey()), 
context, new AdviceWithRouteBuilder() {
@@ -1004,36 +988,32 @@ public abstract class CamelTestSupport
     }
 
     /**
-     * Single step debugs and Camel invokes this method before entering the 
given processor
+     * Single step debugs and Camel invokes this method before entering the 
given processor. This method is NOOP.
+     *
+     * @deprecated Use {@link #createBreakpoint} and return a new instance of 
{@link DebugBreakpoint}
      */
+    @Deprecated(since = "4.7.0")
     protected void debugBefore(
             Exchange exchange, Processor processor, ProcessorDefinition<?> 
definition, String id, String label) {
     }
 
     /**
-     * Single step debugs and Camel invokes this method after processing the 
given processor
+     * Single step debugs and Camel invokes this method after processing the 
given processor. This method is NOOP.
+     *
+     * @deprecated Use {@link #createBreakpoint} and return a new instance of 
{@link DebugBreakpoint}
      */
+    @Deprecated(since = "4.7.0")
     protected void debugAfter(
             Exchange exchange, Processor processor, ProcessorDefinition<?> 
definition, String id, String label,
             long timeTaken) {
     }
 
     /**
-     * To easily debug by overriding the <tt>debugBefore</tt> and 
<tt>debugAfter</tt> methods.
+     * Provides a debug breakpoint to be executed before and/or after entering 
processors
+     *
+     * @return a new debug breakpoint
      */
-    private class DebugBreakpoint extends BreakpointSupport {
-
-        @Override
-        public void beforeProcess(Exchange exchange, Processor processor, 
NamedNode definition) {
-            CamelTestSupport.this.debugBefore(exchange, processor, 
(ProcessorDefinition<?>) definition, definition.getId(),
-                    definition.getLabel());
-        }
-
-        @Override
-        public void afterProcess(Exchange exchange, Processor processor, 
NamedNode definition, long timeTaken) {
-            CamelTestSupport.this.debugAfter(exchange, processor, 
(ProcessorDefinition<?>) definition, definition.getId(),
-                    definition.getLabel(), timeTaken);
-        }
+    protected DebugBreakpoint createBreakpoint() {
+        return null;
     }
-
 }
diff --git 
a/components/camel-test/camel-test-junit5/src/main/java/org/apache/camel/test/junit5/DebugBreakpoint.java
 
b/components/camel-test/camel-test-junit5/src/main/java/org/apache/camel/test/junit5/DebugBreakpoint.java
new file mode 100644
index 00000000000..d32fa0e73d7
--- /dev/null
+++ 
b/components/camel-test/camel-test-junit5/src/main/java/org/apache/camel/test/junit5/DebugBreakpoint.java
@@ -0,0 +1,67 @@
+/*
+ * 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.apache.camel.Exchange;
+import org.apache.camel.NamedNode;
+import org.apache.camel.Processor;
+import org.apache.camel.model.ProcessorDefinition;
+import org.apache.camel.support.BreakpointSupport;
+
+/**
+ * To easily debug by overriding the <tt>debugBefore</tt> and 
<tt>debugAfter</tt> methods.
+ */
+public abstract class DebugBreakpoint extends BreakpointSupport {
+
+    @Override
+    public void beforeProcess(Exchange exchange, Processor processor, 
NamedNode definition) {
+        debugBefore(exchange, processor, (ProcessorDefinition<?>) definition, 
definition.getId(),
+                definition.getLabel());
+    }
+
+    @Override
+    public void afterProcess(Exchange exchange, Processor processor, NamedNode 
definition, long timeTaken) {
+        debugAfter(exchange, processor, (ProcessorDefinition<?>) definition, 
definition.getId(),
+                definition.getLabel(), timeTaken);
+    }
+
+    /**
+     * Single step debugs and Camel invokes this method before entering the 
given processor
+     *
+     * @param exchange   the {@link Exchange}
+     * @param processor  the {@link Processor} which was processed
+     * @param definition the {@link ProcessorDefinition} definition of the 
processor
+     * @param id         the definition ID
+     * @param label      the definition label
+     */
+    protected abstract void debugBefore(
+            Exchange exchange, Processor processor, ProcessorDefinition<?> 
definition, String id, String label);
+
+    /**
+     * Single step debugs and Camel invokes this method after processing the 
given processor
+     *
+     * @param exchange   the {@link Exchange}
+     * @param processor  the {@link Processor} which was processed
+     * @param definition the {@link ProcessorDefinition} definition of the 
processor
+     * @param id         the definition ID
+     * @param label      the definition label
+     */
+    protected abstract void debugAfter(
+            Exchange exchange, Processor processor, ProcessorDefinition<?> 
definition, String id, String label,
+            long timeTaken);
+}
diff --git 
a/components/camel-test/camel-test-junit5/src/main/java/org/apache/camel/test/junit5/util/CamelContextTestHelper.java
 
b/components/camel-test/camel-test-junit5/src/main/java/org/apache/camel/test/junit5/util/CamelContextTestHelper.java
index f9e42e6c57c..09fb59e7341 100644
--- 
a/components/camel-test/camel-test-junit5/src/main/java/org/apache/camel/test/junit5/util/CamelContextTestHelper.java
+++ 
b/components/camel-test/camel-test-junit5/src/main/java/org/apache/camel/test/junit5/util/CamelContextTestHelper.java
@@ -18,10 +18,16 @@
 package org.apache.camel.test.junit5.util;
 
 import org.apache.camel.CamelContext;
+import org.apache.camel.ServiceStatus;
 import org.apache.camel.impl.DefaultCamelContext;
+import org.apache.camel.impl.debugger.DefaultDebugger;
+import org.apache.camel.spi.Breakpoint;
 import org.apache.camel.spi.Registry;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 public final class CamelContextTestHelper {
+    private static final Logger LOG = 
LoggerFactory.getLogger(CamelContextTestHelper.class);
 
     public static CamelContext createCamelContext(Registry registry) throws 
Exception {
         CamelContext retContext;
@@ -33,4 +39,21 @@ public final class CamelContextTestHelper {
 
         return retContext;
     }
+
+    public static void setupDebugger(CamelContext context, Breakpoint 
breakpoint) {
+        assert context != null : "You cannot set a debugger on a null context";
+        assert breakpoint != null : "You cannot set a debugger using a null 
debug breakpoint";
+
+        if (context.getStatus().equals(ServiceStatus.Started)) {
+            LOG.info("Cannot setting the Debugger to the starting 
CamelContext, stop the CamelContext now.");
+            // we need to stop the context first to setup the debugger
+            context.stop();
+        }
+        context.setDebugging(true);
+        final DefaultDebugger defaultDebugger = new DefaultDebugger();
+        context.setDebugger(defaultDebugger);
+
+        defaultDebugger.addBreakpoint(breakpoint);
+        // when stopping CamelContext it will automatically remove the 
breakpoint
+    }
 }
diff --git 
a/components/camel-test/camel-test-junit5/src/test/java/org/apache/camel/test/junit5/patterns/DebugJUnit5Test.java
 
b/components/camel-test/camel-test-junit5/src/test/java/org/apache/camel/test/junit5/patterns/DebugJUnit5Test.java
index 510d8f20094..b108106c4be 100644
--- 
a/components/camel-test/camel-test-junit5/src/test/java/org/apache/camel/test/junit5/patterns/DebugJUnit5Test.java
+++ 
b/components/camel-test/camel-test-junit5/src/test/java/org/apache/camel/test/junit5/patterns/DebugJUnit5Test.java
@@ -16,12 +16,10 @@
  */
 package org.apache.camel.test.junit5.patterns;
 
-import org.apache.camel.Exchange;
-import org.apache.camel.Processor;
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.component.mock.MockEndpoint;
-import org.apache.camel.model.ProcessorDefinition;
 import org.apache.camel.test.junit5.CamelTestSupport;
+import org.apache.camel.test.junit5.DebugBreakpoint;
 import org.junit.jupiter.api.Test;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -36,17 +34,11 @@ public class DebugJUnit5Test extends CamelTestSupport {
         // must enable debugger
         return true;
     }
+    // END SNIPPET: e1
 
-    @Override
-    protected void debugBefore(
-            Exchange exchange, Processor processor, ProcessorDefinition<?> 
definition, String id, String shortName) {
-        // this method is invoked before we are about to enter the given
-        // processor
-        // from your Java editor you can add a breakpoint in the code line
-        // below
-        LOG.info("Before {} with body {}", definition, 
exchange.getIn().getBody());
+    protected DebugBreakpoint createBreakpoint() {
+        return new TestDebugBreakpoint();
     }
-    // END SNIPPET: e1
 
     @Test
     public void testDebugger() throws Exception {
diff --git 
a/components/camel-test/camel-test-junit5/src/test/java/org/apache/camel/test/junit5/patterns/DebugNoLazyTypeConverterTest.java
 
b/components/camel-test/camel-test-junit5/src/test/java/org/apache/camel/test/junit5/patterns/DebugNoLazyTypeConverterTest.java
index 7d00bf5c690..746fd44dbfe 100644
--- 
a/components/camel-test/camel-test-junit5/src/test/java/org/apache/camel/test/junit5/patterns/DebugNoLazyTypeConverterTest.java
+++ 
b/components/camel-test/camel-test-junit5/src/test/java/org/apache/camel/test/junit5/patterns/DebugNoLazyTypeConverterTest.java
@@ -16,12 +16,10 @@
  */
 package org.apache.camel.test.junit5.patterns;
 
-import org.apache.camel.Exchange;
-import org.apache.camel.Processor;
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.component.mock.MockEndpoint;
-import org.apache.camel.model.ProcessorDefinition;
 import org.apache.camel.test.junit5.CamelTestSupport;
+import org.apache.camel.test.junit5.DebugBreakpoint;
 import org.junit.jupiter.api.Test;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -37,14 +35,8 @@ public class DebugNoLazyTypeConverterTest extends 
CamelTestSupport {
         return true;
     }
 
-    @Override
-    protected void debugBefore(
-            Exchange exchange, Processor processor, ProcessorDefinition<?> 
definition, String id, String shortName) {
-        // this method is invoked before we are about to enter the given
-        // processor
-        // from your Java editor you can add a breakpoint in the code line
-        // below
-        LOG.info("Before {} with body {}", definition, 
exchange.getIn().getBody());
+    protected DebugBreakpoint createBreakpoint() {
+        return new TestDebugBreakpoint();
     }
     // END SNIPPET: e1
 
diff --git 
a/components/camel-test/camel-test-junit5/src/test/java/org/apache/camel/test/junit5/patterns/DebugTest.java
 
b/components/camel-test/camel-test-junit5/src/test/java/org/apache/camel/test/junit5/patterns/DebugTest.java
index bd28f4ad521..d6b0a249bc3 100644
--- 
a/components/camel-test/camel-test-junit5/src/test/java/org/apache/camel/test/junit5/patterns/DebugTest.java
+++ 
b/components/camel-test/camel-test-junit5/src/test/java/org/apache/camel/test/junit5/patterns/DebugTest.java
@@ -16,12 +16,10 @@
  */
 package org.apache.camel.test.junit5.patterns;
 
-import org.apache.camel.Exchange;
-import org.apache.camel.Processor;
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.component.mock.MockEndpoint;
-import org.apache.camel.model.ProcessorDefinition;
 import org.apache.camel.test.junit5.CamelTestSupport;
+import org.apache.camel.test.junit5.DebugBreakpoint;
 import org.junit.jupiter.api.Test;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -37,14 +35,8 @@ public class DebugTest extends CamelTestSupport {
         return true;
     }
 
-    @Override
-    protected void debugBefore(
-            Exchange exchange, Processor processor, ProcessorDefinition<?> 
definition, String id, String shortName) {
-        // this method is invoked before we are about to enter the given
-        // processor
-        // from your Java editor you can add a breakpoint in the code line
-        // below
-        LOG.info("Before {} with body {}", definition, 
exchange.getIn().getBody());
+    protected DebugBreakpoint createBreakpoint() {
+        return new TestDebugBreakpoint();
     }
     // END SNIPPET: e1
 
diff --git 
a/components/camel-test/camel-test-junit5/src/test/java/org/apache/camel/test/junit5/patterns/TestDebugBreakpoint.java
 
b/components/camel-test/camel-test-junit5/src/test/java/org/apache/camel/test/junit5/patterns/TestDebugBreakpoint.java
new file mode 100644
index 00000000000..fe876baa0ab
--- /dev/null
+++ 
b/components/camel-test/camel-test-junit5/src/test/java/org/apache/camel/test/junit5/patterns/TestDebugBreakpoint.java
@@ -0,0 +1,46 @@
+/*
+ * 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.patterns;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+import org.apache.camel.model.ProcessorDefinition;
+import org.apache.camel.test.junit5.DebugBreakpoint;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class TestDebugBreakpoint extends DebugBreakpoint {
+    private static final Logger LOG = 
LoggerFactory.getLogger(TestDebugBreakpoint.class);
+
+    @Override
+    protected void debugBefore(
+            Exchange exchange, Processor processor, ProcessorDefinition<?> 
definition, String id, String label) {
+        // this method is invoked before we are about to enter the given
+        // processor
+        // from your Java editor you can add a breakpoint in the code line
+        // below
+        LOG.info("Before {} with body {}", definition, 
exchange.getIn().getBody());
+    }
+
+    @Override
+    protected void debugAfter(
+            Exchange exchange, Processor processor, ProcessorDefinition<?> 
definition, String id, String label,
+            long timeTaken) {
+
+    }
+}
diff --git 
a/components/camel-test/camel-test-spring-junit5/src/test/java/org/apache/camel/test/patterns/DebugSpringTest.java
 
b/components/camel-test/camel-test-spring-junit5/src/test/java/org/apache/camel/test/patterns/DebugSpringTest.java
index abc7a2feeb4..cdef8b67248 100644
--- 
a/components/camel-test/camel-test-spring-junit5/src/test/java/org/apache/camel/test/patterns/DebugSpringTest.java
+++ 
b/components/camel-test/camel-test-spring-junit5/src/test/java/org/apache/camel/test/patterns/DebugSpringTest.java
@@ -21,6 +21,7 @@ import org.apache.camel.Processor;
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.component.mock.MockEndpoint;
 import org.apache.camel.model.ProcessorDefinition;
+import org.apache.camel.test.junit5.DebugBreakpoint;
 import org.apache.camel.test.spring.junit5.CamelSpringTestSupport;
 import org.junit.jupiter.api.Test;
 import org.slf4j.Logger;
@@ -41,14 +42,24 @@ public class DebugSpringTest extends CamelSpringTestSupport 
{
         return true;
     }
 
-    @Override
-    protected void debugBefore(
-            Exchange exchange, Processor processor,
-            ProcessorDefinition<?> definition, String id, String shortName) {
-        // this method is invoked before we are about to enter the given 
processor
-        // from your Java editor you can add a breakpoint in the code line 
below
-        LOG.info("Before {} with body {}", definition, 
exchange.getIn().getBody());
-        debugged = true;
+    protected DebugBreakpoint createBreakpoint() {
+        return new DebugBreakpoint() {
+            @Override
+            protected void debugBefore(
+                    Exchange exchange, Processor processor, 
ProcessorDefinition<?> definition, String id, String label) {
+                // this method is invoked before we are about to enter the 
given processor
+                // from your Java editor you can add a breakpoint in the code 
line below
+                LOG.info("Before {} with body {}", definition, 
exchange.getIn().getBody());
+                debugged = true;
+            }
+
+            @Override
+            protected void debugAfter(
+                    Exchange exchange, Processor processor, 
ProcessorDefinition<?> definition, String id, String label,
+                    long timeTaken) {
+
+            }
+        };
     }
 
     @Test

Reply via email to