Skip to content

Commit dcef943

Browse files
authored
Merge pull request quarkusio#47904 from holly-cummins/improve-clarity-of-profile-config-reading
Add comments and lightly refactor to improve clarity
2 parents 5d9e5a6 + 047a8ed commit dcef943

File tree

3 files changed

+22
-17
lines changed

3 files changed

+22
-17
lines changed

test-framework/junit5/src/main/java/io/quarkus/test/junit/AppMakerHelper.java

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,15 @@ static QuarkusTestProfile getQuarkusTestProfile(Class<? extends QuarkusTestProfi
9292
return profile == null ? null : new ClassCoercingTestProfile(profile.getConstructor().newInstance());
9393
}
9494

95-
static Runnable setExtraProperties(Class<?> profileClass, QuarkusTestProfile profileInstance) {
95+
/**
96+
* Reads properties from a profile, sets them as system properties, and returns a Runnable which can be invoked to remove
97+
* them back off of
98+
* system properties.
99+
*/
100+
static Runnable setExtraPropertiesRestorably(Class<?> profileClass, QuarkusTestProfile profileInstance) {
96101
final Map<String, String> additional = new HashMap<>();
97-
// TODO we make this twice, also in abstractjvmextension can we streamline that?
98-
// TODO We can't get rid of the one here because config needs to be set before augmentation, but maybe we can get rid of it on the test side?
102+
// We apply the profile config twice, once before augmentation, and once before app start
103+
// That's a bit awkward, but both augmentation and app start need to have the right config for their profile
99104
additional.putAll(profileInstance.getConfigOverrides());
100105
if (!profileInstance.getEnabledAlternatives().isEmpty()) {
101106
additional.put("quarkus.arc.selected-alternatives", profileInstance.getEnabledAlternatives()
@@ -124,7 +129,9 @@ static Runnable setExtraProperties(Class<?> profileClass, QuarkusTestProfile pro
124129
//we just use system properties for now
125130
//it's a lot simpler
126131
// TODO this is really ugly, set proper config on the app
127-
// Sadly, I don't think #42715 helps, because it kicks in after this code
132+
// TODO investigate whether we can use the config from https://github.com/quarkusio/quarkus/pull/42715 to avoid system properties
133+
// ... but be aware that this is called twice, and on the first pass through, the classloader might be an all-purpose runtime classloader, and would not be the actual test classloader
134+
// Setting config on the wrong classloader is worse than useless, so we'd need solid test coverage
128135
return RestorableSystemProperties.setProperties(additional)::close;
129136
}
130137

@@ -233,29 +240,25 @@ public static CuratedApplication makeCuratedApplication(Class<?> requiredTestCla
233240
// should have been freshly created
234241
// TODO maybe don't even accept one? is that comment right?
235242
public static StartupAction getStartupAction(Class<?> testClass, CuratedApplication curatedApplication, Class profile)
236-
throws AppModelResolverException, BootstrapException, IOException,
237-
InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
243+
throws InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
238244

239-
PrepareResult prepareResult = prepare(testClass,
240-
curatedApplication,
241-
profile);
245+
PrepareResult prepareResult = prepare(testClass, curatedApplication, profile);
242246

247+
// Before doing the augmentation, apply any extra config from the profile
243248
final Runnable configCleanup = prepareResult.profileInstance() == null ? null
244-
: setExtraProperties(profile, prepareResult.profileInstance());
249+
: setExtraPropertiesRestorably(profile, prepareResult.profileInstance());
245250

246251
try {
247-
248252
// To check changes here run integration-tests/elytron-resteasy-reactive and SharedProfileTestCase in integration-tests/main
249-
250253
return prepareResult.augmentAction().createInitialRuntimeApplication();
251-
} catch (Throwable e) {
254+
} catch (Exception e) {
252255
// Errors at this point just get reported as org.junit.platform.commons.JUnitException: TestEngine with ID 'junit-jupiter' failed to discover tests
253-
// Give a little help to debuggers
254-
// TODO how best to handle?
256+
// Even though a stack trace isn't ideal handling, we want to make sure people have something to try and debug if problems happen
255257
e.printStackTrace();
256258
throw e;
257259

258260
} finally {
261+
// We may by doing augmentations for other profiles now, so unset the config
259262
if (configCleanup != null) {
260263
configCleanup.run();
261264
}

test-framework/junit5/src/main/java/io/quarkus/test/junit/QuarkusMainTestExtension.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,8 @@ protected PrepareResult prepare(ExtensionContext context, Class<? extends Quarku
111111

112112
var result = AppMakerHelper.prepare(requiredTestClass, curatedApplication, profile);
113113
if (result.profileInstance() != null) {
114-
shutdownTasks.add(AppMakerHelper.setExtraProperties(profile, result.profileInstance()));
114+
Runnable configCleaner = AppMakerHelper.setExtraPropertiesRestorably(profile, result.profileInstance());
115+
shutdownTasks.add(configCleaner);
115116
}
116117
return result;
117118
}

test-framework/junit5/src/main/java/io/quarkus/test/junit/QuarkusTestExtension.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,8 @@ public Thread newThread(Runnable r) {
195195
System.clearProperty("test.url");
196196
QuarkusTestProfile profileInstance = AppMakerHelper.getQuarkusTestProfile(profile);
197197
if (profileInstance != null) {
198-
shutdownTasks.add(AppMakerHelper.setExtraProperties(profile, profileInstance));
198+
Runnable configCleaner = AppMakerHelper.setExtraPropertiesRestorably(profile, profileInstance);
199+
shutdownTasks.add(configCleaner);
199200
}
200201
StartupAction startupAction = getClassLoaderFromTestClass(requiredTestClass).getStartupAction();
201202

0 commit comments

Comments
 (0)