Skip to content

Commit

Permalink
Add ability to control which classes are cloned
Browse files Browse the repository at this point in the history
This can interfere with other JUnit exists, such
as Pact.

Relates to quarkusio#9677
  • Loading branch information
stuartwdouglas committed May 19, 2021
1 parent 55bfab4 commit 7657571
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Object> beforeClassCallbacks;
Expand Down Expand Up @@ -343,6 +345,9 @@ private ExtensionState doJavaStart(ExtensionContext context, Class<? extends Qua
populateCallbacks(startupAction.getClassLoader());

runningQuarkusApplication = startupAction.run();
String patternString = runningQuarkusApplication.getConfigValue("quarkus.test.class-clone-pattern", String.class)
.orElse("java.*");
clonePattern = Pattern.compile(patternString);
TracingHandler.quarkusStarted();

//now we have full config reset the hang timer
Expand Down Expand Up @@ -951,7 +956,12 @@ private Object runExtensionMethod(ReflectiveInvocationContext<Method> invocation
List<Object> originalArguments = invocationContext.getArguments();
List<Object> 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]));
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 7657571

Please sign in to comment.