tkobayas commented on code in PR #6587:
URL:
https://github.com/apache/incubator-kie-drools/pull/6587#discussion_r3233072223
##########
kie-memory-compiler/src/test/java/org/kie/memorycompiler/jdknative/NativeJavaCompilerTest.java:
##########
@@ -40,15 +55,152 @@ public void simulateJreWithException() {
assertThatExceptionOfType(KieMemoryCompilerException.class).isThrownBy(() ->
compiler.compile(null, null, null, null, null));
}
-
+
+ @Test
+ public void emptySourcesShouldSucceed() {
+ NativeJavaCompiler compiler = new NativeJavaCompiler();
+ CompilationResult result = compiler.compile(new String[0],
null, null, null, compiler.createDefaultSettings());
+ assertThat(result.getErrors()).isEmpty();
+ }
+
+ /**
+ * Tests that NativeJavaCompiler can resolve classes from
directory-based
+ * classpath entries (not JARs). This exercises the processDirectory()
method
+ * which was added to fix kjar builds where kie-maven-plugin compiles
DRL
+ * referencing Java classes from target/classes.
+ *
+ * The test compiles source that references CompilationResult (a class
from
+ * this module's target/classes directory) using a URLClassLoader that
includes
+ * target/classes as a directory URL — without setting any classpath on
+ * JavaCompilerSettings (simulating the classic DRL compilation path).
+ */
+ @Test
+ public void compileWithDirectoryClasspath() throws Exception {
+ // target/classes contains compiled classes from this module as
a directory (not a JAR)
+ File targetClasses = new File("target/classes");
+ assertThat(targetClasses).isDirectory();
+
+ // Create a classloader with target/classes as a directory URL
+ try (URLClassLoader classLoader = new URLClassLoader(
+ new URL[]{targetClasses.toURI().toURL()},
+ ClassLoader.getPlatformClassLoader()
+ )) {
+ // Source that references CompilationResult from the
directory-based classpath
+ String source =
+ "package
org.kie.memorycompiler.test;\n" +
+ "import
org.kie.memorycompiler.CompilationResult;\n" +
+ "public class DirectoryClasspathTest
{\n" +
+ " public boolean
hasErrors(CompilationResult result) {\n" +
+ " return
result.getErrors().length > 0;\n" +
+ " }\n" +
+ "}";
+
+ Map<String, byte[]> compiled =
KieMemoryCompiler.compileNoLoad(
+
Map.of("org.kie.memorycompiler.test.DirectoryClasspathTest", source),
+ classLoader
+ );
+
+
assertThat(compiled).containsKey("org.kie.memorycompiler.test.DirectoryClasspathTest");
+
assertThat(compiled.get("org.kie.memorycompiler.test.DirectoryClasspathTest")).isNotEmpty();
+ }
+ }
+
+ /**
+ * Tests that a directory classpath entry whose path contains
URL-encoded characters
+ * (a space here, encoded as %20) is correctly recognized as a
directory rather than
+ * falling through to JAR / VFS handling. The previous code used
URL#getFile() which
+ * leaves the path URL-encoded, so File.isDirectory() returned false on
disk paths
+ * with spaces or non-ASCII characters.
+ */
+ @Test
+ public void compileWithEncodedDirectoryClasspath() throws Exception {
+ Path sourceClass =
Paths.get("target/classes/org/kie/memorycompiler/CompilationResult.class");
+ assertThat(sourceClass).exists();
+
+ Path tempRoot = Paths.get("target", "encoded url classpath");
+ Path tempPackage = tempRoot.resolve("org/kie/memorycompiler");
+ Files.createDirectories(tempPackage);
+ Files.copy(sourceClass,
tempPackage.resolve("CompilationResult.class"),
StandardCopyOption.REPLACE_EXISTING);
Review Comment:
done
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]