From b7e86f982606ebb310530277d2f8359a4998d59a Mon Sep 17 00:00:00 2001 From: Roberto Cortez Date: Thu, 29 Aug 2024 13:07:22 +0100 Subject: [PATCH] Revert "QuarkusTestProfile overrides in a high ordinal application.properties" This reverts commit 4a1abd00 --- .../AbstractJvmQuarkusTestExtension.java | 69 +++++-------------- .../test/junit/QuarkusMainTestExtension.java | 5 +- .../test/junit/QuarkusTestExtension.java | 17 ++--- 3 files changed, 23 insertions(+), 68 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 ec8e3cb970ada..a7bf610f10f44 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,7 +4,6 @@ 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; @@ -16,7 +15,6 @@ 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; @@ -42,6 +40,7 @@ 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; @@ -148,67 +147,31 @@ protected PrepareResult createAugmentor(ExtensionContext context, Class additional = new HashMap<>(); QuarkusTestProfile profileInstance = null; if (profile != null) { profileInstance = profile.getConstructor().newInstance(); - - Map overrides = new HashMap<>(profileInstance.getConfigOverrides()); + additional.putAll(profileInstance.getConfigOverrides()); if (!profileInstance.getEnabledAlternatives().isEmpty()) { - 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(","))); + 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(","))); } if (profileInstance.disableApplicationLifecycleObservers()) { - overrides.put("quarkus.arc.test.disable-application-lifecycle-observers", "true"); + additional.put("quarkus.arc.test.disable-application-lifecycle-observers", "true"); } if (profileInstance.getConfigProfile() != null) { - overrides.put(LaunchMode.TEST.getProfileKey(), profileInstance.getConfigProfile()); - } - - // Creates a temporary application.properties file for the test with a high ordinal (build and runtime) - // Note that in the case of the Quarkus Platform, the testClassLocation is actually a jar so we can't - // create a temp directory in it. - Path tempDirectory; - if (Files.isDirectory(testClassLocation) && Files.isWritable(testClassLocation)) { - tempDirectory = Files.createTempDirectory(testClassLocation, requiredTestClass.getSimpleName()); - } else { - tempDirectory = Files.createTempDirectory(requiredTestClass.getSimpleName()); + additional.put(LaunchMode.TEST.getProfileKey(), profileInstance.getConfigProfile()); } - - 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, ""); - } - 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); - } - } - }); + //we just use system properties for now + //it's a lot simpler + shutdownTasks.add(RestorableSystemProperties.setProperties(additional)::close); } 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 744e35bc67d0a..d793d8e74b6c4 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,8 +77,9 @@ private void ensurePrepared(ExtensionContext extensionContext, Class shutdownTasks = new LinkedBlockingDeque<>(); - prepareResult = createAugmentor(extensionContext, profile, shutdownTasks); + final LinkedBlockingDeque shutdownTasks = new LinkedBlockingDeque<>(); + PrepareResult result = createAugmentor(extensionContext, profile, shutdownTasks); + prepareResult = result; } } 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 915f33f98eda1..0f881f19022ce 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<>(); + quarkusTestProfile = profile; 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,7 +298,8 @@ public void close() throws IOException { } } }; - return new ExtensionState(testResourceManager, shutdownTask); + ExtensionState state = new ExtensionState(testResourceManager, shutdownTask); + return state; } catch (Throwable e) { if (!InitialConfigurator.DELAYED_HANDLER.isActivated()) { activateLogging(); @@ -314,16 +315,6 @@ 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) {