Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to define system properties with @NativeImageTest #13998

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,9 @@
@ExtendWith({ DisabledOnNativeImageCondition.class, QuarkusTestExtension.class, NativeTestExtension.class })
@Retention(RetentionPolicy.RUNTIME)
public @interface NativeImageTest {
/**
* Allow to define system properties that will be passed to the native image that will be started by the tests.
* Properties defined in this field will have precedence over the ones defined in profiles shipped with the image.
*/
String[] systemProperties() default {};
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.io.Closeable;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
Expand Down Expand Up @@ -71,6 +72,11 @@ public void beforeAll(ExtensionContext extensionContext) throws Exception {
Map<String, String> systemProps = testResourceManager.start();
NativeImageLauncher launcher = new NativeImageLauncher(testClass);
launcher.addSystemProperties(systemProps);
systemProps = getAnnotationSystemProperties(testClass);
if (!systemProps.isEmpty()) {
launcher.addSystemProperties(systemProps);
}

try {
launcher.start();
} catch (IOException e) {
Expand Down Expand Up @@ -109,7 +115,31 @@ private void ensureNoInjectAnnotationIsUsed(Class<?> testClass) {
}
current = current.getSuperclass();
}
}

private Map<String, String> getAnnotationSystemProperties(Class<?> testClass) {
final Map<String, String> systemProps = new HashMap<>();
final NativeImageTest annotation = testClass.getAnnotation(NativeImageTest.class);

if (annotation != null && annotation.systemProperties().length > 0) {
for (String expression : annotation.systemProperties()) {
if (!expression.contains("=") || expression.startsWith("=")) {
throw new JUnitException("Invalid system property '" + expression
+ "' passed to @NativeImageTest: it must be 'key=value'");
}
final int equalSignIndex = expression.indexOf('=');
final String key = expression.substring(0, equalSignIndex);

final String value;
if (expression.endsWith("=")) {
value = "";
} else {
value = expression.substring(equalSignIndex + 1);
}
systemProps.put(key, value);
}
}
return systemProps;
}

/**
Expand Down