Skip to content

Commit

Permalink
Make picocli tests definitely work in JVM mode
Browse files Browse the repository at this point in the history
  • Loading branch information
holly-cummins committed Dec 10, 2024
1 parent 25ec295 commit f597355
Show file tree
Hide file tree
Showing 5 changed files with 238 additions and 30 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package io.quarkus.deployment.dev.testing;

import java.util.function.Consumer;

import io.quarkus.bootstrap.app.CuratedApplication;

/**
* This class is a bit of a hack, it provides a way to pass in the current curratedApplication into the TestExtension
* TODO It is only needed for QuarkusMainTest, so we may be able to find a better way.
* For example, what about JUnit state?
*/
public class CurrentTestApplication implements Consumer<CuratedApplication> {
public static volatile CuratedApplication curatedApplication;

@Override
public void accept(CuratedApplication c) {
curatedApplication = c;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ public Class<?> loadClass(String name) throws ClassNotFoundException {
// Doing it just for the test loads too little, doing it for everything gives java.lang.ClassCircularityError: io/quarkus/runtime/configuration/QuarkusConfigFactory
// Anything loaded by JUnit will come through this classloader

if ((isQuarkusTest && !isIntegrationTest) || isMainTest) {
if (isQuarkusTest && !isIntegrationTest && !isMainTest) {
System.out.println("HOLLY attempting to load " + name);
QuarkusClassLoader runtimeClassLoader = getQuarkusClassLoader(key, fromCanary, profile);
Class thing = runtimeClassLoader.loadClass(name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ public Class<?> loadClass(String name) throws ClassNotFoundException {
protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
ensureOpen();

if (name.contains("acme")) {
if (name.contains("acme") || name.contains("Pico") || name.contains("Pico")) {
System.out.println("HOLLY loading " + name + " with " + this);
}

Expand Down Expand Up @@ -576,12 +576,12 @@ protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundE
ClassPathElement[] resource = state.loadableResources.get(resourceName);

if (resource != null) {
if (name.contains("acme")) {
if (name.contains("acme") || name.contains("Pico")) {
System.out.println("checking " + resource[0].getRoot() + " for " + resourceName);
}
ClassPathElement classPathElement = resource[0];
ClassPathResource classPathElementResource = classPathElement.getResource(resourceName);
if (name.contains("acme")) {
if (name.contains("acme") || name.contains("Pico")) {
System.out.println("did find it in " + classPathElementResource);
}
if (classPathElementResource != null) { //can happen if the class loader was closed
Expand All @@ -596,7 +596,7 @@ protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundE
}
}

if (name.contains("acme")) {
if (name.contains("acme") || name.contains("Pico")) {
System.out.println("HOLLY ok, problem " + this + " can't find " + name);
}
if (!parentFirst) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.junit.jupiter.api.extension.ExtensionContext;

import io.quarkus.bootstrap.BootstrapConstants;
import io.quarkus.bootstrap.BootstrapException;
import io.quarkus.bootstrap.app.AugmentAction;
import io.quarkus.bootstrap.app.CuratedApplication;
import io.quarkus.bootstrap.app.RunningQuarkusApplication;
Expand Down Expand Up @@ -59,8 +60,7 @@ protected PrepareResult createAugmentor(ExtensionContext context, Class<? extend

System.out.println(
"HOLLY about to cast " + requiredTestClass.getName() + " which has cl " + requiredTestClass.getClassLoader());
CuratedApplication curatedApplication = ((QuarkusClassLoader) requiredTestClass.getClassLoader())
.getCuratedApplication();
CuratedApplication curatedApplication = getCuratedApplication(requiredTestClass, context, shutdownTasks);
System.out.println("HOLLY " + requiredTestClass.getClassLoader() + " gives " + curatedApplication);

// TODO need to handle the gradle case - can we put it in that method?
Expand Down Expand Up @@ -94,6 +94,14 @@ protected PrepareResult createAugmentor(ExtensionContext context, Class<? extend
curatedApplication, testClassLocation);
}

protected CuratedApplication getCuratedApplication(Class<?> requiredTestClass, ExtensionContext context,
Collection<Runnable> shutdownTasks) throws BootstrapException, AppModelResolverException, IOException {
// TODO make this abstract, push this implementation down to QuarkusTestExtension, since that is the only place it will work
CuratedApplication curatedApplication = ((QuarkusClassLoader) requiredTestClass.getClassLoader())
.getCuratedApplication();
return curatedApplication;
}

protected static QuarkusTestProfile getQuarkusTestProfile(Class<? extends QuarkusTestProfile> profile,
Collection<Runnable> shutdownTasks, Map<String, String> additional)
throws InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
Expand Down Expand Up @@ -126,7 +134,7 @@ protected static QuarkusTestProfile getQuarkusTestProfile(Class<? extends Quarku
return profileInstance;
}

private ApplicationModel getGradleAppModelForIDE(Path projectRoot) throws IOException, AppModelResolverException {
ApplicationModel getGradleAppModelForIDE(Path projectRoot) throws IOException, AppModelResolverException {
return System.getProperty(BootstrapConstants.SERIALIZED_TEST_APP_MODEL) == null
? BuildToolHelper.enableGradleAppModelForTest(projectRoot)
: null;
Expand Down
Loading

0 comments on commit f597355

Please sign in to comment.