From af4f16c14e877f9bdc00eaf448221d1bf574b4f9 Mon Sep 17 00:00:00 2001 From: Foivos Zakkak Date: Mon, 23 Sep 2024 18:14:06 +0300 Subject: [PATCH] Adapt locales support for GraalVM >= 24.2 Starting with GraalVM for JDK 24 (24.2) native image will no longer set the locale default at build time. As a result, the default locale won't be included by default in the native image unless explicitly specified. See https://github.com/oracle/graal/pull/9694 --- .../quarkus/deployment/pkg/steps/GraalVM.java | 2 ++ .../pkg/steps/NativeImageBuildStep.java | 14 ++++++++++++- .../java/io/quarkus/locales/it/LocalesIT.java | 21 ++++++++++++++++++- .../src/test/resources/application.properties | 1 + .../io/quarkus/test/junit/GraalVMVersion.java | 4 +++- 5 files changed, 39 insertions(+), 3 deletions(-) diff --git a/core/deployment/src/main/java/io/quarkus/deployment/pkg/steps/GraalVM.java b/core/deployment/src/main/java/io/quarkus/deployment/pkg/steps/GraalVM.java index 2aa16d5165c0c7..317d90f485427b 100644 --- a/core/deployment/src/main/java/io/quarkus/deployment/pkg/steps/GraalVM.java +++ b/core/deployment/src/main/java/io/quarkus/deployment/pkg/steps/GraalVM.java @@ -193,6 +193,8 @@ public static final class Version implements Comparable { public static final Version VERSION_23_0_0 = new Version("GraalVM 23.0.0", "23.0.0", "17", Distribution.GRAALVM); public static final Version VERSION_23_1_0 = new Version("GraalVM 23.1.0", "23.1.0", "21", Distribution.GRAALVM); public static final Version VERSION_24_0_0 = new Version("GraalVM 24.0.0", "24.0.0", "22", Distribution.GRAALVM); + public static final Version VERSION_24_1_0 = new Version("GraalVM 24.1.0", "24.1.0", "23", Distribution.GRAALVM); + public static final Version VERSION_24_2_0 = new Version("GraalVM 24.2.0", "24.2.0", "24", Distribution.GRAALVM); /** * The minimum version of GraalVM supported by Quarkus. diff --git a/core/deployment/src/main/java/io/quarkus/deployment/pkg/steps/NativeImageBuildStep.java b/core/deployment/src/main/java/io/quarkus/deployment/pkg/steps/NativeImageBuildStep.java index cb844cd4e5887a..cc8bdc05641476 100644 --- a/core/deployment/src/main/java/io/quarkus/deployment/pkg/steps/NativeImageBuildStep.java +++ b/core/deployment/src/main/java/io/quarkus/deployment/pkg/steps/NativeImageBuildStep.java @@ -750,14 +750,19 @@ public NativeImageInvokerInfo build() { } final String userLanguage = LocaleProcessor.nativeImageUserLanguage(nativeConfig, localesBuildTimeConfig); + String userLocale = ""; if (!userLanguage.isEmpty()) { nativeImageArgs.add("-J-Duser.language=" + userLanguage); + userLocale = userLanguage; } final String userCountry = LocaleProcessor.nativeImageUserCountry(nativeConfig, localesBuildTimeConfig); if (!userCountry.isEmpty()) { nativeImageArgs.add("-J-Duser.country=" + userCountry); + if (!userLocale.isEmpty()) { + userLocale += "," + userCountry; + } } - final String includeLocales = LocaleProcessor.nativeImageIncludeLocales(nativeConfig, localesBuildTimeConfig); + String includeLocales = LocaleProcessor.nativeImageIncludeLocales(nativeConfig, localesBuildTimeConfig); if (!includeLocales.isEmpty()) { if ("all".equals(includeLocales)) { log.warn( @@ -765,8 +770,15 @@ public NativeImageInvokerInfo build() { "All JDK locales, languages, currencies, etc. will be included, inflating the size of the executable."); addExperimentalVMOption(nativeImageArgs, "-H:+IncludeAllLocales"); } else { + // Explicitly include user's locale (needed since https://github.com/oracle/graal/pull/9694) + if (!userLocale.isEmpty()) { + includeLocales += "," + userLocale; + } addExperimentalVMOption(nativeImageArgs, "-H:IncludeLocales=" + includeLocales); } + } else if (!userLocale.isEmpty()) { + // Explicitly include user's locale (needed since https://github.com/oracle/graal/pull/9694) + addExperimentalVMOption(nativeImageArgs, "-H:IncludeLocales=" + userLocale); } nativeImageArgs.add("-J-Dfile.encoding=" + nativeConfig.fileEncoding()); diff --git a/integration-tests/locales/some/src/test/java/io/quarkus/locales/it/LocalesIT.java b/integration-tests/locales/some/src/test/java/io/quarkus/locales/it/LocalesIT.java index 4b192ba4ca03af..82c2fdd3b7fb53 100644 --- a/integration-tests/locales/some/src/test/java/io/quarkus/locales/it/LocalesIT.java +++ b/integration-tests/locales/some/src/test/java/io/quarkus/locales/it/LocalesIT.java @@ -4,6 +4,9 @@ import static org.hamcrest.Matchers.equalToIgnoringCase; import static org.hamcrest.Matchers.is; +import io.quarkus.test.junit.DisableIfBuiltWithGraalVMNewerThan; +import io.quarkus.test.junit.DisableIfBuiltWithGraalVMOlderThan; +import io.quarkus.test.junit.GraalVMVersion; import org.apache.http.HttpStatus; import org.jboss.logging.Logger; import org.junit.jupiter.api.Test; @@ -81,7 +84,8 @@ public void testTimeZones(String zone, String language, String name) { } @Test - public void testDefaultLocale() { + @DisableIfBuiltWithGraalVMNewerThan(value = GraalVMVersion.GRAALVM_24_1_0) + public void testDefaultLocaleBefore24_2() { RestAssured.given().when() .get("/default/de-CH") .then() @@ -94,6 +98,21 @@ public void testDefaultLocale() { .log().all(); } + @Test + @DisableIfBuiltWithGraalVMOlderThan(value = GraalVMVersion.GRAALVM_24_2_0) + public void testDefaultLocaleAfter24_1() { + RestAssured.given().when() + .get("/default/de-CH") + .then() + .statusCode(HttpStatus.SC_OK) + /* + * "Schweiz" is the correct name for Switzerland in German. + * German is the default language as per the application.properties. + */ + .body(is("Schweiz")) + .log().all(); + } + @Test public void testMissingLocaleSorryItaly() { RestAssured.given().when() diff --git a/integration-tests/locales/some/src/test/resources/application.properties b/integration-tests/locales/some/src/test/resources/application.properties index 4d3a429e5788c6..4617c6481b6d1e 100644 --- a/integration-tests/locales/some/src/test/resources/application.properties +++ b/integration-tests/locales/some/src/test/resources/application.properties @@ -3,3 +3,4 @@ quarkus.locales=de,fr-FR,ja,uk-UA # used in your application properties. This test uses it only to verify compatibility. quarkus.native.user-language=cs quarkus.default-locale=en-US +quarkus.test.arg-line=-Duser.language=de \ No newline at end of file diff --git a/test-framework/junit5/src/main/java/io/quarkus/test/junit/GraalVMVersion.java b/test-framework/junit5/src/main/java/io/quarkus/test/junit/GraalVMVersion.java index df95a13f166b9b..3a82fe47a6ef19 100644 --- a/test-framework/junit5/src/main/java/io/quarkus/test/junit/GraalVMVersion.java +++ b/test-framework/junit5/src/main/java/io/quarkus/test/junit/GraalVMVersion.java @@ -4,7 +4,9 @@ public enum GraalVMVersion { GRAALVM_23_1_0(GraalVM.Version.VERSION_23_1_0), - GRAALVM_24_0_0(GraalVM.Version.VERSION_24_0_0); + GRAALVM_24_0_0(GraalVM.Version.VERSION_24_0_0), + GRAALVM_24_1_0(GraalVM.Version.VERSION_24_1_0), + GRAALVM_24_2_0(GraalVM.Version.VERSION_24_2_0); private final GraalVM.Version version;