Skip to content

Commit

Permalink
QuarkusTestProfile overrides in a high ordinal application.properties
Browse files Browse the repository at this point in the history
  • Loading branch information
radcortez committed Jul 17, 2024
1 parent 2d3f78b commit 4840e4c
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -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;

Expand Down Expand Up @@ -147,31 +148,56 @@ protected PrepareResult createAugmentor(ExtensionContext context, Class<? extend

// clear the test.url system property as the value leaks into the run when using different profiles
System.clearProperty("test.url");
Map<String, String> additional = new HashMap<>();

QuarkusTestProfile profileInstance = null;
if (profile != null) {
profileInstance = profile.getConstructor().newInstance();
additional.putAll(profileInstance.getConfigOverrides());

Map<String, String> 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();
properties.put("config_ordinal", Integer.MAX_VALUE - 100 + "");
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,8 @@ private void ensurePrepared(ExtensionContext extensionContext, Class<? extends Q
prepareResult = null;
}
if (prepareResult == null) {
final LinkedBlockingDeque<Runnable> shutdownTasks = new LinkedBlockingDeque<>();
PrepareResult result = createAugmentor(extensionContext, profile, shutdownTasks);
prepareResult = result;
LinkedBlockingDeque<Runnable> shutdownTasks = new LinkedBlockingDeque<>();
prepareResult = createAugmentor(extensionContext, profile, shutdownTasks);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Runnable> shutdownTasks = new LinkedBlockingDeque<>();
Class<?> requiredTestClass = context.getRequiredTestClass();
Closeable testResourceManager = null;
try {
final LinkedBlockingDeque<Runnable> shutdownTasks = new LinkedBlockingDeque<>();
PrepareResult result = createAugmentor(context, profile, shutdownTasks);
AugmentAction augmentAction = result.augmentAction;
QuarkusTestProfile profileInstance = result.profileInstance;
Expand Down Expand Up @@ -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();
Expand All @@ -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) {
Expand Down

0 comments on commit 4840e4c

Please sign in to comment.