From 7657571fdfc331b5a180bd76d4b0a88f1b9e8302 Mon Sep 17 00:00:00 2001 From: Stuart Douglas Date: Tue, 18 May 2021 15:50:45 +1000 Subject: [PATCH] Add ability to control which classes are cloned This can interfere with other JUnit exists, such as Pact. Relates to #9677 --- .../quarkus/deployment/dev/testing/TestConfig.java | 10 ++++++++++ .../io/quarkus/test/junit/QuarkusTestExtension.java | 13 ++++++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/core/deployment/src/main/java/io/quarkus/deployment/dev/testing/TestConfig.java b/core/deployment/src/main/java/io/quarkus/deployment/dev/testing/TestConfig.java index 81ae2dfaaf43a..1efdc5d142033 100644 --- a/core/deployment/src/main/java/io/quarkus/deployment/dev/testing/TestConfig.java +++ b/core/deployment/src/main/java/io/quarkus/deployment/dev/testing/TestConfig.java @@ -152,6 +152,16 @@ public class TestConfig { @ConfigItem(defaultValue = "all") TestType type; + /** + * If a class matches this pattern then it will be cloned into the Quarkus ClassLoader even if it + * is in a parent first artifact. + * + * This is important for collections which can contain objects from the Quarkus ClassLoader, but for + * most parent first classes it will just cause problems. + */ + @ConfigItem(defaultValue = "java.*") + String classClonePattern; + @ConfigGroup public static class Profile { diff --git a/test-framework/junit5/src/main/java/io/quarkus/test/junit/QuarkusTestExtension.java b/test-framework/junit5/src/main/java/io/quarkus/test/junit/QuarkusTestExtension.java index b9b04a6c6abcb..c20fb62bf041c 100644 --- a/test-framework/junit5/src/main/java/io/quarkus/test/junit/QuarkusTestExtension.java +++ b/test-framework/junit5/src/main/java/io/quarkus/test/junit/QuarkusTestExtension.java @@ -39,6 +39,7 @@ import java.util.function.Consumer; import java.util.function.Function; import java.util.function.Predicate; +import java.util.regex.Pattern; import java.util.stream.Collectors; import javax.enterprise.inject.Alternative; @@ -127,6 +128,7 @@ public class QuarkusTestExtension private static Object actualTestInstance; private static ClassLoader originalCl; private static RunningQuarkusApplication runningQuarkusApplication; + private static Pattern clonePattern; private static Throwable firstException; //if this is set then it will be thrown from the very first test that is run, the rest are aborted private static List beforeClassCallbacks; @@ -343,6 +345,9 @@ private ExtensionState doJavaStart(ExtensionContext context, Class invocation List originalArguments = invocationContext.getArguments(); List argumentsFromTccl = new ArrayList<>(); for (Object arg : originalArguments) { - argumentsFromTccl.add(deepClone.clone(arg)); + if (arg != null && (arg.getClass().getClassLoader() instanceof QuarkusClassLoader + || clonePattern.matcher(arg.getClass().getName()).matches())) { + argumentsFromTccl.add(deepClone.clone(arg)); + } else { + argumentsFromTccl.add(arg); + } } return newMethod.invoke(actualTestInstance, argumentsFromTccl.toArray(new Object[0])); @@ -1087,6 +1097,7 @@ public void close() { log.error("Failed to shutdown Quarkus", e); } finally { runningQuarkusApplication = null; + clonePattern = null; try { if (QuarkusTestExtension.this.originalCl != null) { setCCL(QuarkusTestExtension.this.originalCl);