|
| 1 | +/* |
| 2 | + * Copyright 2023 Ihor Herasymenko. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.github.iherasymenko.jlink.test; |
| 17 | + |
| 18 | +import com.github.iherasymenko.jlink.test.fixtures.Text; |
| 19 | +import org.assertj.core.api.InstanceOfAssertFactories; |
| 20 | +import org.gradle.testkit.runner.BuildResult; |
| 21 | +import org.junit.jupiter.api.Test; |
| 22 | + |
| 23 | +import java.io.IOException; |
| 24 | + |
| 25 | +import static org.assertj.core.api.Assertions.assertThat; |
| 26 | + |
| 27 | +class IncludeLocalesPluginFunctionalTest extends AbstractTestBase { |
| 28 | + |
| 29 | + @Test |
| 30 | + void can_include_locales() throws IOException { |
| 31 | + build.buildFile = """ |
| 32 | + plugins { |
| 33 | + id 'java' |
| 34 | + id 'com.github.iherasymenko.jlink' |
| 35 | + } |
| 36 | + |
| 37 | + group = 'com.example' |
| 38 | + version = '0.0.1-SNAPSHOT' |
| 39 | +
|
| 40 | + java { |
| 41 | + toolchain { |
| 42 | + languageVersion = JavaLanguageVersion.of(System.getenv().getOrDefault('TESTING_AGAINST_JDK', '21')) |
| 43 | + vendor = JvmVendorSpec.AZUL |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + jlinkApplication { |
| 48 | + mainClass = 'com.example.demo.DemoApplication' |
| 49 | + mainModule = 'demo.main' |
| 50 | + includeLocales = ['en-CA'] |
| 51 | + } |
| 52 | + """; |
| 53 | + build.settingsFile = """ |
| 54 | + rootProject.name = 'demo' |
| 55 | + dependencyResolutionManagement { |
| 56 | + repositories { |
| 57 | + mavenCentral() |
| 58 | + } |
| 59 | + } |
| 60 | + """; |
| 61 | + build.mainClass = """ |
| 62 | + package com.example.demo; |
| 63 | + |
| 64 | + import java.util.Locale; |
| 65 | + |
| 66 | + public class DemoApplication { |
| 67 | + public static void main(String[] args) { |
| 68 | + for (Locale locale : Locale.getAvailableLocales()) { |
| 69 | + System.out.println(locale); |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + """; |
| 74 | + build.moduleInfo = """ |
| 75 | + module demo.main { |
| 76 | + requires jdk.localedata; |
| 77 | + } |
| 78 | + """; |
| 79 | + |
| 80 | + BuildResult buildResult = build.runner("imageRun").build(); |
| 81 | + |
| 82 | + String[] taskOutput = Text.linesBetweenTags(buildResult.getOutput(), "> Task :imageRun", "BUILD SUCCESSFUL"); |
| 83 | + assertThat(taskOutput) |
| 84 | + .hasSizeLessThan(10) |
| 85 | + .contains("en", "en_CA", "en_US", "en_US_POSIX"); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + void many_locales_are_available_by_default() throws IOException { |
| 90 | + build.buildFile = """ |
| 91 | + plugins { |
| 92 | + id 'java' |
| 93 | + id 'com.github.iherasymenko.jlink' |
| 94 | + } |
| 95 | + |
| 96 | + group = 'com.example' |
| 97 | + version = '0.0.1-SNAPSHOT' |
| 98 | +
|
| 99 | + java { |
| 100 | + toolchain { |
| 101 | + languageVersion = JavaLanguageVersion.of(System.getenv().getOrDefault('TESTING_AGAINST_JDK', '21')) |
| 102 | + vendor = JvmVendorSpec.AZUL |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + jlinkApplication { |
| 107 | + mainClass = 'com.example.demo.DemoApplication' |
| 108 | + mainModule = 'demo.main' |
| 109 | + } |
| 110 | + """; |
| 111 | + build.settingsFile = """ |
| 112 | + rootProject.name = 'demo' |
| 113 | + dependencyResolutionManagement { |
| 114 | + repositories { |
| 115 | + mavenCentral() |
| 116 | + } |
| 117 | + } |
| 118 | + """; |
| 119 | + build.mainClass = """ |
| 120 | + package com.example.demo; |
| 121 | + |
| 122 | + import java.util.Arrays; |
| 123 | + import java.util.Locale; |
| 124 | + import java.util.Comparator; |
| 125 | + |
| 126 | + public class DemoApplication { |
| 127 | + public static void main(String[] args) { |
| 128 | + Locale[] availableLocales = Locale.getAvailableLocales(); |
| 129 | + Arrays.sort(availableLocales, Comparator.comparing(Locale::toString)); |
| 130 | + for (Locale locale : availableLocales) { |
| 131 | + System.out.println(locale); |
| 132 | + } |
| 133 | + } |
| 134 | + } |
| 135 | + """; |
| 136 | + build.moduleInfo = """ |
| 137 | + module demo.main { |
| 138 | + requires jdk.localedata; |
| 139 | + } |
| 140 | + """; |
| 141 | + |
| 142 | + BuildResult buildResult = build.runner("imageRun").build(); |
| 143 | + |
| 144 | + String[] taskOutput = Text.linesBetweenTags(buildResult.getOutput(), "> Task :imageRun", "BUILD SUCCESSFUL"); |
| 145 | + assertThat(taskOutput) |
| 146 | + .hasSizeGreaterThan(100) |
| 147 | + .contains("uk_UA", "en_CA", "fr_CA"); |
| 148 | + } |
| 149 | + |
| 150 | +} |
0 commit comments