From 4a1abd0095cca47012fbe6ba69ca90ffe8d77cda Mon Sep 17 00:00:00 2001 From: Roberto Cortez Date: Wed, 17 Jul 2024 17:54:49 +0100 Subject: [PATCH] QuarkusTestProfile overrides in a high ordinal application.properties --- .../AbstractJvmQuarkusTestExtension.java | 61 ++++++++++++++----- .../test/junit/QuarkusMainTestExtension.java | 5 +- .../test/junit/QuarkusTestExtension.java | 17 ++++-- 3 files changed, 60 insertions(+), 23 deletions(-) diff --git a/test-framework/junit5/src/main/java/io/quarkus/test/junit/AbstractJvmQuarkusTestExtension.java b/test-framework/junit5/src/main/java/io/quarkus/test/junit/AbstractJvmQuarkusTestExtension.java index 7ec294c594874..80727210d42c2 100644 --- a/test-framework/junit5/src/main/java/io/quarkus/test/junit/AbstractJvmQuarkusTestExtension.java +++ b/test-framework/junit5/src/main/java/io/quarkus/test/junit/AbstractJvmQuarkusTestExtension.java @@ -4,6 +4,7 @@ import static io.quarkus.test.common.PathTestHelper.getAppClassLocationForTestLocation; import static io.quarkus.test.common.PathTestHelper.getTestClassesLocation; +import java.io.FileOutputStream; import java.io.IOException; import java.lang.annotation.Annotation; import java.nio.file.Files; @@ -15,6 +16,7 @@ import java.util.HashMap; import java.util.Map; import java.util.Optional; +import java.util.Properties; import java.util.function.Consumer; import java.util.stream.Collectors; @@ -40,7 +42,6 @@ import io.quarkus.runtime.LaunchMode; import io.quarkus.test.common.PathTestHelper; import io.quarkus.test.common.QuarkusTestResource; -import io.quarkus.test.common.RestorableSystemProperties; import io.quarkus.test.common.TestClassIndexer; import io.quarkus.test.common.WithTestResource; @@ -147,31 +148,59 @@ protected PrepareResult createAugmentor(ExtensionContext context, Class additional = new HashMap<>(); QuarkusTestProfile profileInstance = null; if (profile != null) { profileInstance = profile.getConstructor().newInstance(); - additional.putAll(profileInstance.getConfigOverrides()); + + Map overrides = new HashMap<>(profileInstance.getConfigOverrides()); if (!profileInstance.getEnabledAlternatives().isEmpty()) { - additional.put("quarkus.arc.selected-alternatives", profileInstance.getEnabledAlternatives().stream() - .peek((c) -> { - if (!c.isAnnotationPresent(Alternative.class)) { - throw new RuntimeException( - "Enabled alternative " + c + " is not annotated with @Alternative"); - } - }) - .map(Class::getName).collect(Collectors.joining(","))); + overrides.put("quarkus.arc.selected-alternatives", + profileInstance.getEnabledAlternatives() + .stream() + .peek((c) -> { + if (!c.isAnnotationPresent(Alternative.class)) { + throw new RuntimeException( + "Enabled alternative " + c + " is not annotated with " + + "@Alternative"); + } + }) + .map(Class::getName) + .collect(Collectors.joining(","))); } if (profileInstance.disableApplicationLifecycleObservers()) { - additional.put("quarkus.arc.test.disable-application-lifecycle-observers", "true"); + overrides.put("quarkus.arc.test.disable-application-lifecycle-observers", "true"); } if (profileInstance.getConfigProfile() != null) { - additional.put(LaunchMode.TEST.getProfileKey(), profileInstance.getConfigProfile()); + overrides.put(LaunchMode.TEST.getProfileKey(), profileInstance.getConfigProfile()); + } + + // Creates a temporary application.properties file for the test with a high ordinal (build and runtime) + Path tempDirectory = Files.createTempDirectory(testClassLocation, requiredTestClass.getSimpleName()); + Path propertiesFile = tempDirectory.resolve("application.properties"); + Files.createFile(propertiesFile); + Properties properties = new Properties(); + // TODO - radcortez - This should be higher that system properties, but configuration like ports is being + // passed around using system properties, meaning that it cannot be overridden. We cannot use system + // properties to carry that information. See io.quarkus.vertx.http.runtime.PortSystemProperties + properties.put("config_ordinal", "399"); + properties.putAll(overrides); + try (FileOutputStream outputStream = new FileOutputStream(propertiesFile.toFile())) { + properties.store(outputStream, ""); } - //we just use system properties for now - //it's a lot simpler - shutdownTasks.add(RestorableSystemProperties.setProperties(additional)::close); + addToBuilderIfConditionMet.accept(tempDirectory); + + shutdownTasks.add(new Runnable() { + @Override + public void run() { + try { + Files.deleteIfExists(propertiesFile); + Files.deleteIfExists(tempDirectory); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + }); } CuratedApplication curatedApplication; diff --git a/test-framework/junit5/src/main/java/io/quarkus/test/junit/QuarkusMainTestExtension.java b/test-framework/junit5/src/main/java/io/quarkus/test/junit/QuarkusMainTestExtension.java index d793d8e74b6c4..744e35bc67d0a 100644 --- a/test-framework/junit5/src/main/java/io/quarkus/test/junit/QuarkusMainTestExtension.java +++ b/test-framework/junit5/src/main/java/io/quarkus/test/junit/QuarkusMainTestExtension.java @@ -77,9 +77,8 @@ private void ensurePrepared(ExtensionContext extensionContext, Class shutdownTasks = new LinkedBlockingDeque<>(); - PrepareResult result = createAugmentor(extensionContext, profile, shutdownTasks); - prepareResult = result; + LinkedBlockingDeque shutdownTasks = new LinkedBlockingDeque<>(); + prepareResult = createAugmentor(extensionContext, profile, shutdownTasks); } } 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 0f881f19022ce..915f33f98eda1 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 @@ -205,12 +205,12 @@ public Thread newThread(Runnable r) { } hangTimeout = new DurationConverter().convert(time); hangTaskKey = hangDetectionExecutor.schedule(hangDetectionTask, hangTimeout.toMillis(), TimeUnit.MILLISECONDS); - quarkusTestProfile = profile; + + LinkedBlockingDeque shutdownTasks = new LinkedBlockingDeque<>(); Class requiredTestClass = context.getRequiredTestClass(); Closeable testResourceManager = null; try { - final LinkedBlockingDeque shutdownTasks = new LinkedBlockingDeque<>(); PrepareResult result = createAugmentor(context, profile, shutdownTasks); AugmentAction augmentAction = result.augmentAction; QuarkusTestProfile profileInstance = result.profileInstance; @@ -298,8 +298,7 @@ public void close() throws IOException { } } }; - ExtensionState state = new ExtensionState(testResourceManager, shutdownTask); - return state; + return new ExtensionState(testResourceManager, shutdownTask); } catch (Throwable e) { if (!InitialConfigurator.DELAYED_HANDLER.isActivated()) { activateLogging(); @@ -315,6 +314,16 @@ public void close() throws IOException { effectiveException.addSuppressed(determineEffectiveException(ex)); } + while (!shutdownTasks.isEmpty()) { + shutdownTasks.pop().run(); + } + + try { + TestClassIndexer.removeIndex(requiredTestClass); + } catch (Exception ignored) { + + } + throw effectiveException; } finally { if (originalCl != null) {