This is an automated email from the ASF dual-hosted git repository.
davsclaus 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 bc096b3de141 CAMEL-27341: camel-jbang - camel dependency list and
export work with the test plugin in a directory with Citrus tests (#30553)
bc096b3de141 is described below
commit bc096b3de1417bda120da515b8c5b87dc472952d
Author: Claus Ibsen <[email protected]>
AuthorDate: Sat Nov 21 12:52:34 2026 +0100
CAMEL-27341: camel-jbang - camel dependency list and export work with the
test plugin in a directory with Citrus tests (#30553)
The Citrus code analyzer reads its catalog with the thread context class
loader, which during camel export and camel dependency list is the class loader
of the CLI and cannot see the citrus-jbang JAR of the plugin.
TestPluginExporter now sets the context class loader to the analyzer's while it
scans the test files, and restores it after. A test file that fails to scan is
logged and skipped, so the command goes on without its test dependencies.
Signed-off-by: Claus Ibsen <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[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");