This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch CAMEL-27341-test-plugin-citrus
in repository https://gitbox.apache.org/repos/asf/camel.git

commit 4e2b6aff474a5098b8686fdf75e0df27e6b203da
Author: Claus Ibsen <[email protected]>
AuthorDate: Sat Nov 21 12:35:46 2026 +0100

    CAMEL-27341: camel-jbang - the test plugin scans Citrus tests with its own 
class loader, so camel dependency list and export work in a directory with 
Citrus tests
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
    Signed-off-by: Claus Ibsen <[email protected]>
---
 .../core/commands/test/TestPluginExporter.java     | 14 ++++++++-
 .../jbang/core/commands/test/TestPluginTest.java   | 34 ++++++++++++++++++++++
 2 files changed, 47 insertions(+), 1 deletion(-)

diff --git 
a/dsl/camel-jbang/camel-jbang-plugin-test/src/main/java/org/apache/camel/dsl/jbang/core/commands/test/TestPluginExporter.java
 
b/dsl/camel-jbang/camel-jbang-plugin-test/src/main/java/org/apache/camel/dsl/jbang/core/commands/test/TestPluginExporter.java
index 101399265b0c..93b3f3a552c2 100644
--- 
a/dsl/camel-jbang/camel-jbang-plugin-test/src/main/java/org/apache/camel/dsl/jbang/core/commands/test/TestPluginExporter.java
+++ 
b/dsl/camel-jbang/camel-jbang-plugin-test/src/main/java/org/apache/camel/dsl/jbang/core/commands/test/TestPluginExporter.java
@@ -202,11 +202,19 @@ public class TestPluginExporter implements PluginExporter 
{
     /**
      * Scans Citrus test files in the given directory using the Citrus code 
analyzer to detect required modules and
      * dependencies. Supports YAML, XML and Groovy test sources.
+     * <p/>
+     * The analyzer reads its catalog (citrus-catalog/citrus/*.json in the 
citrus-jbang JAR) with the thread context
+     * class loader. During camel export or camel dependency list that is the 
class loader of the CLI, which cannot see
+     * the JARs of this plugin, so it is set to the class loader of the 
analyzer while scanning. A test file that cannot
+     * be scanned is skipped, so the command goes on without its test 
dependencies.
      */
     private Set<String> detectDependenciesFromTestSources(Path testDir) {
         Set<String> deps = new HashSet<>();
-        DelegatingCodeAnalyzer analyzer = new DelegatingCodeAnalyzer();
+        Thread thread = Thread.currentThread();
+        ClassLoader tccl = thread.getContextClassLoader();
         try (Stream<Path> paths = Files.list(testDir)) {
+            thread.setContextClassLoader(CodeAnalyzer.class.getClassLoader());
+            DelegatingCodeAnalyzer analyzer = new DelegatingCodeAnalyzer();
             paths.filter(this::isTestSource)
                     .filter(this::isAnalyzerSupported)
                     .forEach(path -> {
@@ -220,10 +228,14 @@ public class TestPluginExporter implements PluginExporter 
{
                             }
                         } catch (IOException e) {
                             LOG.debug("Failed to scan {} for dependencies: 
{}", path, e.getMessage());
+                        } catch (RuntimeException e) {
+                            LOG.warn("Cannot detect the test dependencies of 
{}: {}", path, e.getMessage());
                         }
                     });
         } catch (IOException e) {
             LOG.debug("Failed to list test directory {}: {}", testDir, 
e.getMessage());
+        } finally {
+            thread.setContextClassLoader(tccl);
         }
 
         return deps;
diff --git 
a/dsl/camel-jbang/camel-jbang-plugin-test/src/test/java/org/apache/camel/dsl/jbang/core/commands/test/TestPluginTest.java
 
b/dsl/camel-jbang/camel-jbang-plugin-test/src/test/java/org/apache/camel/dsl/jbang/core/commands/test/TestPluginTest.java
index 4f82fc55bd3c..e2b04efd98c9 100644
--- 
a/dsl/camel-jbang/camel-jbang-plugin-test/src/test/java/org/apache/camel/dsl/jbang/core/commands/test/TestPluginTest.java
+++ 
b/dsl/camel-jbang/camel-jbang-plugin-test/src/test/java/org/apache/camel/dsl/jbang/core/commands/test/TestPluginTest.java
@@ -18,6 +18,8 @@
 package org.apache.camel.dsl.jbang.core.commands.test;
 
 import java.io.IOException;
+import java.net.URL;
+import java.net.URLClassLoader;
 import java.nio.file.Files;
 import java.nio.file.Path;
 import java.util.Set;
@@ -225,6 +227,38 @@ class TestPluginTest {
         }
     }
 
+    @Test
+    public void 
shouldDetectDependenciesWhenTheContextClassLoaderCannotSeeCitrus() throws 
IOException {
+        // camel export and camel dependency list run with the class loader of 
the CLI as the context class loader,
+        // which cannot see the citrus-jbang JAR of the plugin, where the code 
analyzer has its catalog (CAMEL-27341)
+        Path testDir = Path.of(".").resolve("test");
+        Files.createDirectories(testDir);
+        Path yamlFile = testDir.resolve("cli-loader.citrus.it.yaml");
+        Thread thread = Thread.currentThread();
+        ClassLoader tccl = thread.getContextClassLoader();
+        try (URLClassLoader cli = new URLClassLoader(new URL[0], null)) {
+            Files.writeString(yamlFile, """
+                    name: cli-loader
+                    actions:
+                      - send:
+                          endpoint: "kafka:myTopic"
+                    """);
+            
Assertions.assertNull(cli.getResource("citrus-catalog/citrus/citrus-catalog-aggregate-endpoints.json"),
+                    "The class loader of the test must not see the catalog of 
the code analyzer");
+
+            thread.setContextClassLoader(cli);
+            Set<String> deps = new 
TestPluginExporter().getDependencies(RuntimeType.quarkus);
+
+            
Assertions.assertTrue(deps.contains("mvn@test:org.citrusframework:citrus-kafka:${citrus.version}"),
+                    "Should auto-detect citrus-kafka from kafka endpoint");
+            Assertions.assertSame(cli, thread.getContextClassLoader(), "Should 
restore the context class loader");
+        } finally {
+            thread.setContextClassLoader(tccl);
+            Files.deleteIfExists(yamlFile);
+            Files.deleteIfExists(testDir);
+        }
+    }
+
     @Test
     public void shouldNotAddExtraDepsWithoutMatchingActions() throws 
IOException {
         Path testDir = Path.of(".").resolve("test");

Reply via email to