1 /*
   2  * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 import java.nio.file.Path;
  25 import java.util.Arrays;
  26 import java.util.ArrayList;
  27 import java.util.List;
  28 import java.util.Locale;
  29 import java.util.stream.Collectors;
  30 
  31 import jdk.tools.jlink.plugin.Plugin;
  32 import jdk.tools.jlink.plugin.PluginException;
  33 import jdk.tools.jlink.internal.PluginRepository;
  34 import jdk.tools.jlink.internal.TaskHelper;
  35 import jdk.tools.jlink.internal.plugins.PluginsResourceBundle;
  36 import tests.Helper;
  37 import tests.JImageGenerator;
  38 import tests.JImageValidator;
  39 import tests.Result;
  40 
  41 /*
  42  * @test
  43  * @bug 8152143 8152704 8155649
  44  * @summary IncludeLocalesPlugin tests
  45  * @author Naoto Sato
  46  * @library ../../lib
  47  * @modules java.base/jdk.internal.jimage
  48  *          jdk.jdeps/com.sun.tools.classfile
  49  *          jdk.jlink/jdk.tools.jlink.internal
  50  *          jdk.jlink/jdk.tools.jlink.internal.plugins
  51  *          jdk.jlink/jdk.tools.jmod
  52  *          jdk.jlink/jdk.tools.jimage
  53  *          jdk.compiler
  54  * @build tests.*
  55  * @build tools.jlink.plugins.GetAvailableLocales
  56  * @run main/othervm -Xmx1g IncludeLocalesPluginTest
  57  */
  58 public class IncludeLocalesPluginTest {
  59 
  60     private final static String moduleName = "IncludeLocalesTest";
  61     private static Helper helper;
  62     private final static int INCLUDE_LOCALES_OPTION = 0;
  63     private final static int ADDMODS_OPTION         = 1;
  64     private final static int EXPECTED_LOCATIONS     = 2;
  65     private final static int UNEXPECTED_PATHS       = 3;
  66     private final static int AVAILABLE_LOCALES      = 4;
  67     private final static int ERROR_MESSAGE          = 5;
  68 
  69     private static int errors;
  70 
  71     private final static Object[][] testData = {
  72         // without --include-locales option: should include all locales
  73         {
  74             "",
  75             "jdk.localedata",
  76             List.of(
  77                 "/jdk.localedata/sun/text/resources/ext/FormatData_en_GB.class",
  78                 "/jdk.localedata/sun/text/resources/ext/FormatData_ja.class",
  79                 "/jdk.localedata/sun/text/resources/ext/FormatData_th.class",
  80                 "/jdk.localedata/sun/text/resources/ext/FormatData_zh.class",
  81                 "/jdk.localedata/sun/text/resources/cldr/ext/FormatData_en_001.class",
  82                 "/jdk.localedata/sun/text/resources/cldr/ext/FormatData_ja.class",
  83                 "/jdk.localedata/sun/text/resources/cldr/ext/FormatData_th.class",
  84                 "/jdk.localedata/sun/text/resources/cldr/ext/FormatData_zh.class"),
  85             List.of(),
  86             Arrays.stream(Locale.getAvailableLocales())
  87                   // "(root)" for Locale.ROOT rather than ""
  88                   .map(loc -> loc.equals(Locale.ROOT) ? "(root)" : loc.toString())
  89                   .collect(Collectors.toList()),
  90             "",
  91         },
  92 
  93         // All English and Japanese locales
  94         {
  95             "--include-locales=en,ja",
  96             "jdk.localedata",
  97             List.of(
  98                 "/jdk.localedata/sun/text/resources/ext/FormatData_en_GB.class",
  99                 "/jdk.localedata/sun/text/resources/ext/FormatData_ja.class",
 100                 "/jdk.localedata/sun/text/resources/cldr/ext/FormatData_en_001.class",
 101                 "/jdk.localedata/sun/text/resources/cldr/ext/FormatData_ja.class"),
 102             List.of(
 103                 "/jdk.localedata/sun/text/resources/LineBreakIteratorData_th",
 104                 "/jdk.localedata/sun/text/resources/thai_dict",
 105                 "/jdk.localedata/sun/text/resources/WordBreakIteratorData_th",
 106                 "/jdk.localedata/sun/text/resources/ext/BreakIteratorInfo_th.class",
 107                 "/jdk.localedata/sun/text/resources/ext/BreakIteratorRules_th.class",
 108                 "/jdk.localedata/sun/text/resources/ext/FormatData_th.class",
 109                 "/jdk.localedata/sun/text/resources/ext/FormatData_zh.class",
 110                 "/jdk.localedata/sun/text/resources/cldr/ext/FormatData_th.class",
 111                 "/jdk.localedata/sun/text/resources/cldr/ext/FormatData_zh.class"),
 112             List.of(
 113                 "(root)", "en", "en_001", "en_150", "en_AG", "en_AI", "en_AS", "en_AT",
 114                 "en_AU", "en_BB", "en_BE", "en_BI", "en_BM", "en_BS", "en_BW", "en_BZ",
 115                 "en_CA", "en_CC", "en_CH", "en_CK", "en_CM", "en_CX", "en_CY", "en_DE",
 116                 "en_DG", "en_DK", "en_DM", "en_ER", "en_FI", "en_FJ", "en_FK", "en_FM",
 117                 "en_GB", "en_GD", "en_GG", "en_GH", "en_GI", "en_GM", "en_GU", "en_GY",
 118                 "en_HK", "en_IE", "en_IL", "en_IM", "en_IN", "en_IO", "en_JE", "en_JM",
 119                 "en_KE", "en_KI", "en_KN", "en_KY", "en_LC", "en_LR", "en_LS", "en_MG",
 120                 "en_MH", "en_MO", "en_MP", "en_MS", "en_MT", "en_MU", "en_MW", "en_MY",
 121                 "en_NA", "en_NF", "en_NG", "en_NL", "en_NR", "en_NU", "en_NZ", "en_PG",
 122                 "en_PH", "en_PK", "en_PN", "en_PR", "en_PW", "en_RW", "en_SB", "en_SC",
 123                 "en_SD", "en_SE", "en_SG", "en_SH", "en_SI", "en_SL", "en_SS", "en_SX",
 124                 "en_SZ", "en_TC", "en_TK", "en_TO", "en_TT", "en_TV", "en_TZ", "en_UG",
 125                 "en_UM", "en_US", "en_US_POSIX", "en_VC", "en_VG", "en_VI", "en_VU",
 126                 "en_WS", "en_ZA", "en_ZM", "en_ZW", "ja", "ja_JP",
 127                 "ja_JP_JP_#u-ca-japanese"),
 128             "",
 129         },
 130 
 131         // All locales in India
 132         {
 133             "--include-locales=*-IN",
 134             "jdk.localedata",
 135             List.of(
 136                 "/jdk.localedata/sun/text/resources/ext/FormatData_en_IN.class",
 137                 "/jdk.localedata/sun/text/resources/ext/FormatData_hi_IN.class",
 138                 "/jdk.localedata/sun/util/resources/cldr/ext/CalendarData_as_IN.class",
 139                 "/jdk.localedata/sun/text/resources/cldr/ext/FormatData_en_001.class",
 140                 "/jdk.localedata/sun/text/resources/cldr/ext/FormatData_en_IN.class",
 141                 "/jdk.localedata/sun/util/resources/cldr/ext/CalendarData_kok_IN.class",
 142                 "/jdk.localedata/sun/util/resources/cldr/ext/CalendarData_pa_Guru_IN.class"),
 143             List.of(
 144                 "/jdk.localedata/sun/text/resources/LineBreakIteratorData_th",
 145                 "/jdk.localedata/sun/text/resources/thai_dict",
 146                 "/jdk.localedata/sun/text/resources/WordBreakIteratorData_th",
 147                 "/jdk.localedata/sun/text/resources/ext/BreakIteratorInfo_th.class",
 148                 "/jdk.localedata/sun/text/resources/ext/BreakIteratorRules_th.class",
 149                 "/jdk.localedata/sun/text/resources/ext/FormatData_en_GB.class",
 150                 "/jdk.localedata/sun/text/resources/ext/FormatData_ja.class",
 151                 "/jdk.localedata/sun/text/resources/ext/FormatData_th.class",
 152                 "/jdk.localedata/sun/text/resources/ext/FormatData_zh.class",
 153                 "/jdk.localedata/sun/text/resources/cldr/ext/FormatData_ja.class",
 154                 "/jdk.localedata/sun/text/resources/cldr/ext/FormatData_th.class",
 155                 "/jdk.localedata/sun/text/resources/cldr/ext/FormatData_zh.class"),
 156             List.of(
 157                 "(root)", "as_IN", "bn_IN", "bo_IN", "brx_IN", "en", /* "en_001", */
 158                 "en_IN", "en_US", "en_US_POSIX", "gu_IN", "hi_IN", "kn_IN", "kok_IN",
 159                 "ks_IN", "ml_IN", "mr_IN", "ne_IN", "or_IN", "pa_IN", "pa_IN_#Guru",
 160                 "ta_IN", "te_IN", "ur_IN"),
 161             "",
 162         },
 163 
 164         // Thai
 165         {
 166             "--include-locales=th",
 167             "jdk.localedata",
 168             List.of(
 169                 "/jdk.localedata/sun/text/resources/LineBreakIteratorData_th",
 170                 "/jdk.localedata/sun/text/resources/thai_dict",
 171                 "/jdk.localedata/sun/text/resources/WordBreakIteratorData_th",
 172                 "/jdk.localedata/sun/text/resources/ext/BreakIteratorInfo_th.class",
 173                 "/jdk.localedata/sun/text/resources/ext/BreakIteratorRules_th.class",
 174                 "/jdk.localedata/sun/text/resources/ext/FormatData_th.class"),
 175             List.of(
 176                 "/jdk.localedata/sun/text/resources/ext/FormatData_en_GB.class",
 177                 "/jdk.localedata/sun/text/resources/ext/FormatData_ja.class",
 178                 "/jdk.localedata/sun/text/resources/ext/FormatData_zh.class",
 179                 "/jdk.localedata/sun/text/resources/cldr/ext/FormatData_en_001.class",
 180                 "/jdk.localedata/sun/text/resources/cldr/ext/FormatData_ja.class",
 181                 "/jdk.localedata/sun/text/resources/cldr/ext/FormatData_zh.class"),
 182             List.of(
 183                 "(root)", "en", "en_US", "en_US_POSIX", "th", "th_TH",
 184                 "th_TH_TH_#u-nu-thai"),
 185             "",
 186         },
 187 
 188         // Hong Kong
 189         {
 190             "--include-locales=zh-HK",
 191             "jdk.localedata",
 192             List.of(
 193                 "/jdk.localedata/sun/text/resources/ext/FormatData_zh.class",
 194                 "/jdk.localedata/sun/text/resources/ext/FormatData_zh_HK.class",
 195                 "/jdk.localedata/sun/text/resources/ext/FormatData_zh_TW.class",
 196                 "/jdk.localedata/sun/text/resources/cldr/ext/FormatData_zh.class"),
 197             List.of(
 198                 "/jdk.localedata/sun/text/resources/LineBreakIteratorData_th",
 199                 "/jdk.localedata/sun/text/resources/thai_dict",
 200                 "/jdk.localedata/sun/text/resources/WordBreakIteratorData_th",
 201                 "/jdk.localedata/sun/text/resources/ext/BreakIteratorInfo_th.class",
 202                 "/jdk.localedata/sun/text/resources/ext/BreakIteratorRules_th.class",
 203                 "/jdk.localedata/sun/text/resources/ext/FormatData_en_GB.class",
 204                 "/jdk.localedata/sun/text/resources/ext/FormatData_ja.class",
 205                 "/jdk.localedata/sun/text/resources/ext/FormatData_th.class",
 206                 "/jdk.localedata/sun/text/resources/cldr/ext/FormatData_en_001.class",
 207                 "/jdk.localedata/sun/text/resources/cldr/ext/FormatData_ja.class",
 208                 "/jdk.localedata/sun/text/resources/cldr/ext/FormatData_th.class"),
 209             List.of(
 210                 "(root)", "en", "en_US", "en_US_POSIX", "zh_HK", "zh_HK_#Hans",
 211                 "zh_HK_#Hant"),
 212             "",
 213         },
 214 
 215         // Norwegian
 216         {
 217             "--include-locales=nb,nn,no",
 218             "jdk.localedata",
 219             List.of(
 220                 "/jdk.localedata/sun/text/resources/ext/FormatData_no.class",
 221                 "/jdk.localedata/sun/text/resources/ext/FormatData_no_NO.class",
 222                 "/jdk.localedata/sun/text/resources/ext/FormatData_no_NO_NY.class",
 223                 "/jdk.localedata/sun/text/resources/cldr/ext/FormatData_nb.class",
 224                 "/jdk.localedata/sun/text/resources/cldr/ext/FormatData_nn.class"),
 225             List.of(
 226                 "/jdk.localedata/sun/text/resources/LineBreakIteratorData_th",
 227                 "/jdk.localedata/sun/text/resources/thai_dict",
 228                 "/jdk.localedata/sun/text/resources/WordBreakIteratorData_th",
 229                 "/jdk.localedata/sun/text/resources/ext/BreakIteratorInfo_th.class",
 230                 "/jdk.localedata/sun/text/resources/ext/BreakIteratorRules_th.class",
 231                 "/jdk.localedata/sun/text/resources/ext/FormatData_en_GB.class",
 232                 "/jdk.localedata/sun/text/resources/ext/FormatData_ja.class",
 233                 "/jdk.localedata/sun/text/resources/ext/FormatData_th.class",
 234                 "/jdk.localedata/sun/text/resources/cldr/ext/FormatData_en_001.class",
 235                 "/jdk.localedata/sun/text/resources/cldr/ext/FormatData_ja.class",
 236                 "/jdk.localedata/sun/text/resources/cldr/ext/FormatData_th.class"),
 237             List.of(
 238                 "(root)", "en", "en_US", "en_US_POSIX", "nb", "nb_NO", "nb_SJ", "nn",
 239                 "nn_NO", "no", "no_NO", "no_NO_NY"),
 240             "",
 241         },
 242 
 243         // Hebrew/Indonesian/Yiddish
 244         {
 245             "--include-locales=he,id,yi",
 246             "jdk.localedata",
 247             List.of(
 248                 "/jdk.localedata/sun/text/resources/ext/FormatData_in.class",
 249                 "/jdk.localedata/sun/text/resources/ext/FormatData_in_ID.class",
 250                 "/jdk.localedata/sun/text/resources/ext/FormatData_iw.class",
 251                 "/jdk.localedata/sun/text/resources/ext/FormatData_iw_IL.class",
 252                 "/jdk.localedata/sun/text/resources/cldr/ext/FormatData_in.class",
 253                 "/jdk.localedata/sun/text/resources/cldr/ext/FormatData_iw.class",
 254                 "/jdk.localedata/sun/text/resources/cldr/ext/FormatData_ji.class"),
 255             List.of(
 256                 "/jdk.localedata/sun/text/resources/LineBreakIteratorData_th",
 257                 "/jdk.localedata/sun/text/resources/thai_dict",
 258                 "/jdk.localedata/sun/text/resources/WordBreakIteratorData_th",
 259                 "/jdk.localedata/sun/text/resources/ext/BreakIteratorInfo_th.class",
 260                 "/jdk.localedata/sun/text/resources/ext/BreakIteratorRules_th.class",
 261                 "/jdk.localedata/sun/text/resources/ext/FormatData_en_GB.class",
 262                 "/jdk.localedata/sun/text/resources/ext/FormatData_ja.class",
 263                 "/jdk.localedata/sun/text/resources/ext/FormatData_th.class",
 264                 "/jdk.localedata/sun/text/resources/cldr/ext/FormatData_en_001.class",
 265                 "/jdk.localedata/sun/text/resources/cldr/ext/FormatData_ja.class",
 266                 "/jdk.localedata/sun/text/resources/cldr/ext/FormatData_th.class"),
 267             List.of(
 268                 "(root)", "en", "en_US", "en_US_POSIX", "in", "in_ID", "iw", "iw_IL",
 269                 "ji", "ji_001"),
 270             "",
 271         },
 272 
 273         // Error case: No matching locales
 274         {
 275             "--include-locales=xyz",
 276             "jdk.localedata",
 277             null,
 278             null,
 279             null,
 280             new PluginException(String.format(
 281                 PluginsResourceBundle.getMessage("include-locales.nomatchinglocales"), "xyz"))
 282                 .getMessage(),
 283         },
 284 
 285         // Error case: Invalid argument
 286         {
 287             "--include-locales=en,zh_HK",
 288             "jdk.localedata",
 289             null,
 290             null,
 291             null,
 292             new PluginException(String.format(
 293                 PluginsResourceBundle.getMessage("include-locales.invalidtag"), "zh_HK"))
 294                 .getMessage(),
 295         },
 296 
 297         // Error case: jdk.localedata is not added
 298         {
 299             "--include-locales=en-US",
 300             "java.base",
 301             null,
 302             null,
 303             null,
 304             new PluginException(
 305                 PluginsResourceBundle.getMessage("include-locales.localedatanotfound"))
 306                 .getMessage(),
 307         },
 308     };
 309 
 310     public static void main(String[] args) throws Exception {
 311         helper = Helper.newHelper();
 312         if (helper == null) {
 313             System.err.println("Test not run");
 314             return;
 315         }
 316         helper.generateDefaultModules();
 317 
 318         for (Object[] data : testData) {
 319             // create image for each test data
 320             System.out.println("Invoking jlink with \"" + data[INCLUDE_LOCALES_OPTION] + "\"");
 321             Result result = JImageGenerator.getJLinkTask()
 322                     .modulePath(helper.defaultModulePath())
 323                     .output(helper.createNewImageDir(moduleName))
 324                     .addMods((String) data[ADDMODS_OPTION])
 325                     .option((String) data[INCLUDE_LOCALES_OPTION])
 326                     .call();
 327 
 328             String errorMsg = (String) data[ERROR_MESSAGE];
 329             if (errorMsg.isEmpty()) {
 330                 Path image = result.assertSuccess();
 331 
 332                 // test locale data entries
 333                 testLocaleDataEntries(image,
 334                     (List<String>) data[EXPECTED_LOCATIONS],
 335                     (List<String>) data[UNEXPECTED_PATHS]);
 336 
 337                 // test available locales
 338                 testAvailableLocales(image, (List<String>) data[AVAILABLE_LOCALES]);
 339             } else {
 340                 result.assertFailure(new TaskHelper(TaskHelper.JLINK_BUNDLE)
 341                     .getMessage("error.prefix") + " " +errorMsg);
 342                 System.out.println("\tExpected failure: " + result.getMessage());
 343             }
 344         }
 345 
 346         if (errors > 0) {
 347             throw new RuntimeException("Test failed");
 348         }
 349     }
 350 
 351     private static void testLocaleDataEntries(Path image, List<String> expectedLocations,
 352                         List<String> unexpectedPaths) throws Exception {
 353         System.out.println("testLocaleDataEntries:");
 354         try {
 355             JImageValidator.validate(
 356                 image.resolve("lib").resolve("modules"),
 357                 expectedLocations, unexpectedPaths);
 358         } catch (Exception e) {
 359             System.out.println("\tFailed with: " + e);
 360             e.printStackTrace();
 361             errors++;
 362         }
 363     }
 364 
 365     private static void testAvailableLocales(Path image, List<String> availableLocales) throws Exception {
 366         System.out.println("testAvailableLocales:");
 367         Path launcher = image.resolve("bin/java" +
 368             (System.getProperty("os.name").startsWith("Windows") ? ".exe" : ""));
 369         List<String> args = new ArrayList<>(availableLocales.size() + 2);
 370         args.add(launcher.toString());
 371         args.add("GetAvailableLocales");
 372         args.addAll(availableLocales);
 373         Process proc = new ProcessBuilder(args).inheritIO().start();
 374 
 375         int len = Math.min(10, args.size());
 376         String command = args.subList(0, len).stream().collect(Collectors.joining(" "))
 377                          + (len < availableLocales.size() ? " ..." : "");
 378 
 379         int status = proc.waitFor();
 380         if (status == 0) {
 381             System.out.println("\tDone\t" + command);
 382         } else {
 383             System.out.println("\tExit " + status + "\t" + command);
 384             errors++;
 385         }
 386         System.out.println();
 387     }
 388 }