1 /*
   2  * Copyright (c) 2012, 2020, 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 import java.text.*;
  24 import java.text.spi.*;
  25 import java.util.*;
  26 import java.util.spi.*;
  27 import java.util.stream.IntStream;
  28 import sun.util.locale.provider.LocaleProviderAdapter;
  29 
  30 public class LocaleProviders {
  31 
  32     private static final boolean IS_WINDOWS = System.getProperty("os.name").startsWith("Windows");
  33     private static final boolean IS_MAC = System.getProperty("os.name").startsWith("Mac");
  34 
  35     public static void main(String[] args) {
  36         String methodName = args[0];
  37 
  38         switch (methodName) {
  39             case "getPlatformLocale":
  40                 if (args[1].equals("format")) {
  41                     getPlatformLocale(Locale.Category.FORMAT);
  42                 } else {
  43                     getPlatformLocale(Locale.Category.DISPLAY);
  44                 }
  45                 break;
  46 
  47             case "adapterTest":
  48                 adapterTest(args[1], args[2], (args.length >= 4 ? args[3] : ""));
  49                 break;
  50 
  51             case "bug7198834Test":
  52                 bug7198834Test();
  53                 break;
  54 
  55             case "tzNameTest":
  56                 tzNameTest(args[1]);
  57                 break;
  58 
  59             case "bug8001440Test":
  60                 bug8001440Test();
  61                 break;
  62 
  63             case "bug8010666Test":
  64                 bug8010666Test();
  65                 break;
  66 
  67             case "bug8013086Test":
  68                 bug8013086Test(args[1], args[2]);
  69                 break;
  70 
  71             case "bug8013903Test":
  72                 bug8013903Test();
  73                 break;
  74 
  75             case "bug8027289Test":
  76                 bug8027289Test(args[1]);
  77                 break;
  78 
  79             case "bug8220227Test":
  80                 bug8220227Test();
  81                 break;
  82 
  83             case "bug8228465Test":
  84                 bug8228465Test();
  85                 break;
  86 
  87             case "bug8232871Test":
  88                 bug8232871Test();
  89                 break;
  90 
  91             case "bug8232860Test":
  92                 bug8232860Test();
  93                 break;
  94 
  95             default:
  96                 throw new RuntimeException("Test method '"+methodName+"' not found.");
  97         }
  98     }
  99 
 100     static void getPlatformLocale(Locale.Category cat) {
 101         Locale defloc = Locale.getDefault(cat);
 102         System.out.printf("%s,%s\n", defloc.getLanguage(), defloc.getCountry());
 103     }
 104 
 105     static void adapterTest(String expected, String lang, String ctry) {
 106         Locale testLocale = new Locale(lang, ctry);
 107         LocaleProviderAdapter ldaExpected =
 108             LocaleProviderAdapter.forType(LocaleProviderAdapter.Type.valueOf(expected));
 109         if (!ldaExpected.getDateFormatProvider().isSupportedLocale(testLocale)) {
 110             System.out.println("test locale: "+testLocale+" is not supported by the expected provider: "+ldaExpected+". Ignoring the test.");
 111             return;
 112         }
 113         String preference = System.getProperty("java.locale.providers", "");
 114         LocaleProviderAdapter lda = LocaleProviderAdapter.getAdapter(DateFormatProvider.class, testLocale);
 115         LocaleProviderAdapter.Type type = lda.getAdapterType();
 116         System.out.printf("testLocale: %s, got: %s, expected: %s\n", testLocale, type, expected);
 117         if (!type.toString().equals(expected)) {
 118             throw new RuntimeException("Returned locale data adapter is not correct.");
 119         }
 120     }
 121 
 122     static void bug7198834Test() {
 123         LocaleProviderAdapter lda = LocaleProviderAdapter.getAdapter(DateFormatProvider.class, Locale.US);
 124         LocaleProviderAdapter.Type type = lda.getAdapterType();
 125         if (type == LocaleProviderAdapter.Type.HOST && IS_WINDOWS) {
 126             DateFormat df = DateFormat.getDateInstance(DateFormat.FULL, Locale.US);
 127             String date = df.format(new Date());
 128             if (date.charAt(date.length()-1) == ' ') {
 129                 throw new RuntimeException("Windows Host Locale Provider returns a trailing space.");
 130             }
 131         } else {
 132             System.out.println("Windows HOST locale adapter not found. Ignoring this test.");
 133         }
 134     }
 135 
 136     static void tzNameTest(String id) {
 137         TimeZone tz = TimeZone.getTimeZone(id);
 138         String tzName = tz.getDisplayName(false, TimeZone.SHORT, Locale.US);
 139         if (tzName.startsWith("GMT")) {
 140             throw new RuntimeException("JRE's localized time zone name for "+id+" could not be retrieved. Returned name was: "+tzName);
 141         }
 142     }
 143 
 144     static void bug8001440Test() {
 145         Locale locale = Locale.forLanguageTag("th-TH-u-nu-hoge");
 146         NumberFormat nf = NumberFormat.getInstance(locale);
 147         String nu = nf.format(1234560);
 148     }
 149 
 150     // This test assumes Windows localized language/country display names.
 151     static void bug8010666Test() {
 152         if (IS_WINDOWS) {
 153             NumberFormat nf = NumberFormat.getInstance(Locale.US);
 154             try {
 155                 double ver = nf.parse(System.getProperty("os.version"))
 156                                .doubleValue();
 157                 System.out.printf("Windows version: %.1f\n", ver);
 158                 if (ver >= 6.0) {
 159                     LocaleProviderAdapter lda =
 160                         LocaleProviderAdapter.getAdapter(
 161                             LocaleNameProvider.class, Locale.ENGLISH);
 162                     LocaleProviderAdapter.Type type = lda.getAdapterType();
 163                     if (type == LocaleProviderAdapter.Type.HOST) {
 164                         LocaleNameProvider lnp = lda.getLocaleNameProvider();
 165                         Locale mkmk = Locale.forLanguageTag("mk-MK");
 166                         String result = mkmk.getDisplayLanguage(Locale.ENGLISH);
 167                         String hostResult =
 168                             lnp.getDisplayLanguage(mkmk.getLanguage(),
 169                                                    Locale.ENGLISH);
 170                         System.out.printf("  Display language name for" +
 171                             " (mk_MK): result(HOST): \"%s\", returned: \"%s\"\n",
 172                             hostResult, result);
 173                         if (result == null ||
 174                             hostResult != null &&
 175                             !result.equals(hostResult)) {
 176                             throw new RuntimeException("Display language name" +
 177                                 " mismatch for \"mk\". Returned name was" +
 178                                 " \"" + result + "\", result(HOST): \"" +
 179                                 hostResult + "\"");
 180                         }
 181                         result = Locale.US.getDisplayLanguage(Locale.ENGLISH);
 182                         hostResult =
 183                             lnp.getDisplayLanguage(Locale.US.getLanguage(),
 184                                                    Locale.ENGLISH);
 185                         System.out.printf("  Display language name for" +
 186                             " (en_US): result(HOST): \"%s\", returned: \"%s\"\n",
 187                             hostResult, result);
 188                         if (result == null ||
 189                             hostResult != null &&
 190                             !result.equals(hostResult)) {
 191                             throw new RuntimeException("Display language name" +
 192                                 " mismatch for \"en\". Returned name was" +
 193                                 " \"" + result + "\", result(HOST): \"" +
 194                                 hostResult + "\"");
 195                         }
 196                         if (ver >= 6.1) {
 197                             result = Locale.US.getDisplayCountry(Locale.ENGLISH);
 198                             hostResult = lnp.getDisplayCountry(
 199                                 Locale.US.getCountry(), Locale.ENGLISH);
 200                             System.out.printf("  Display country name for" +
 201                                 " (en_US): result(HOST): \"%s\", returned: \"%s\"\n",
 202                                 hostResult, result);
 203                             if (result == null ||
 204                                 hostResult != null &&
 205                                 !result.equals(hostResult)) {
 206                                 throw new RuntimeException("Display country name" +
 207                                     " mismatch for \"US\". Returned name was" +
 208                                     " \"" + result + "\", result(HOST): \"" +
 209                                     hostResult + "\"");
 210                             }
 211                         }
 212                     } else {
 213                         throw new RuntimeException("Windows Host" +
 214                             " LocaleProviderAdapter was not selected for" +
 215                             " English locale.");
 216                     }
 217                 }
 218             } catch (ParseException pe) {
 219                 throw new RuntimeException("Parsing Windows version failed: "+pe.toString());
 220             }
 221         }
 222     }
 223 
 224     static void bug8013086Test(String lang, String ctry) {
 225         try {
 226             // Throws a NullPointerException if the test fails.
 227             System.out.println(new SimpleDateFormat("z", new Locale(lang, ctry)).parse("UTC"));
 228         } catch (ParseException pe) {
 229             // ParseException is fine in this test, as it's not "UTC"
 230 }
 231     }
 232 
 233     static void bug8013903Test() {
 234         if (IS_WINDOWS) {
 235             Date sampleDate = new Date(0x10000000000L);
 236             String hostResult = "\u5e73\u6210 16.11.03 (Wed) AM 11:53:47";
 237             String jreResult = "\u5e73\u6210 16.11.03 (\u6c34) \u5348\u524d 11:53:47";
 238             Locale l = new Locale("ja", "JP", "JP");
 239             SimpleDateFormat sdf = new SimpleDateFormat("GGGG yyyy.MMM.dd '('E')' a hh:mm:ss", l);
 240             sdf.setTimeZone(TimeZone.getTimeZone("America/Los_Angeles"));
 241             String result = sdf.format(sampleDate);
 242             System.out.println(result);
 243             if (LocaleProviderAdapter.getAdapterPreference()
 244                 .contains(LocaleProviderAdapter.Type.JRE)) {
 245                 if (!jreResult.equals(result)) {
 246                     throw new RuntimeException("Format failed. result: \"" +
 247                         result + "\", expected: \"" + jreResult);
 248                 }
 249             } else {
 250                 // Windows display names. Subject to change if Windows changes its format.
 251                 if (!hostResult.equals(result)) {
 252                     throw new RuntimeException("Format failed. result: \"" +
 253                         result + "\", expected: \"" + hostResult);
 254                 }
 255             }
 256         }
 257     }
 258 
 259     static void bug8027289Test(String expectedCodePoint) {
 260         if (IS_WINDOWS) {
 261             char[] expectedSymbol = Character.toChars(Integer.valueOf(expectedCodePoint, 16));
 262             NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.CHINA);
 263             char formatted = nf.format(7000).charAt(0);
 264             System.out.println("returned: " + formatted + ", expected: " + expectedSymbol[0]);
 265             if (formatted != expectedSymbol[0]) {
 266                 throw new RuntimeException(
 267                         "Unexpected Chinese currency symbol. returned: "
 268                                 + formatted + ", expected: " + expectedSymbol[0]);
 269             }
 270         }
 271     }
 272 
 273     static void bug8220227Test() {
 274         if (IS_WINDOWS) {
 275             Locale l = new Locale("xx","XX");
 276             String country = l.getDisplayCountry();
 277             if (country.endsWith("(XX)")) {
 278                 throw new RuntimeException(
 279                         "Unexpected Region name: " + country);
 280             }
 281         }
 282     }
 283 
 284     static void bug8228465Test() {
 285         LocaleProviderAdapter lda = LocaleProviderAdapter.getAdapter(CalendarNameProvider.class, Locale.US);
 286         LocaleProviderAdapter.Type type = lda.getAdapterType();
 287         if (type == LocaleProviderAdapter.Type.HOST && IS_WINDOWS) {
 288             var names =  new GregorianCalendar()
 289                 .getDisplayNames(Calendar.ERA, Calendar.SHORT_FORMAT, Locale.US);
 290             if (!names.keySet().contains("AD") ||
 291                 names.get("AD").intValue() != 1) {
 292                     throw new RuntimeException(
 293                             "Short Era name for 'AD' is missing or incorrect");
 294             } else {
 295                 System.out.println("bug8228465Test succeeded.");
 296             }
 297         }
 298     }
 299 
 300     static void bug8232871Test() {
 301         LocaleProviderAdapter lda = LocaleProviderAdapter.getAdapter(CalendarNameProvider.class, Locale.US);
 302         LocaleProviderAdapter.Type type = lda.getAdapterType();
 303         var lang = Locale.getDefault().getLanguage();
 304         var cal = Calendar.getInstance();
 305         var calType = cal.getCalendarType();
 306         var expected = "\u4ee4\u548c1\u5e745\u67081\u65e5 \u6c34\u66dc\u65e5 \u5348\u524d0:00:00 \u30a2\u30e1\u30ea\u30ab\u592a\u5e73\u6d0b\u590f\u6642\u9593";
 307 
 308         if (type == LocaleProviderAdapter.Type.HOST &&
 309             IS_MAC &&
 310             lang.equals("ja") &&
 311             calType.equals("japanese")) {
 312             cal.set(1, 4, 1, 0, 0, 0);
 313             cal.setTimeZone(TimeZone.getTimeZone("America/Los_Angeles"));
 314             DateFormat df = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL,
 315                             Locale.JAPAN);
 316             df.setCalendar(cal);
 317             var result = df.format(cal.getTime());
 318             if (result.equals(expected)) {
 319                 System.out.println("bug8232871Test succeeded.");
 320             } else {
 321                 throw new RuntimeException(
 322                             "Japanese calendar names mismatch. result: " +
 323                             result +
 324                             ", expected: " +
 325                             expected);
 326             }
 327         } else {
 328             System.out.println("Test ignored. Either :-\n" +
 329                 "OS is not macOS, or\n" +
 330                 "provider is not HOST: " + type + ", or\n" +
 331                 "Language is not Japanese: " + lang + ", or\n" +
 332                 "native calendar is not JapaneseCalendar: " + calType);
 333         }
 334     }
 335 
 336     static void bug8232860Test() {
 337         var inputList = List.of(123, 123.4);
 338         var nfExpectedList = List.of("123", "123.4");
 339         var ifExpectedList = List.of("123", "123");
 340 
 341         var defLoc = Locale.getDefault(Locale.Category.FORMAT);
 342         var type = LocaleProviderAdapter.getAdapter(CalendarNameProvider.class, Locale.US)
 343                                         .getAdapterType();
 344         if (defLoc.equals(Locale.US) &&
 345             type == LocaleProviderAdapter.Type.HOST &&
 346             (IS_WINDOWS || IS_MAC)) {
 347             final var numf = NumberFormat.getNumberInstance(Locale.US);
 348             final var intf = NumberFormat.getIntegerInstance(Locale.US);
 349 
 350             IntStream.range(0, inputList.size())
 351                 .forEach(i -> {
 352                     var input = inputList.get(i);
 353                     var nfExpected = nfExpectedList.get(i);
 354                     var result = numf.format(input);
 355                     if (!result.equals(nfExpected)) {
 356                         throw new RuntimeException("Incorrect number format. " +
 357                             "input: " + input + ", expected: " +
 358                             nfExpected + ", result: " + result);
 359                     }
 360 
 361                     var ifExpected = ifExpectedList.get(i);
 362                     result = intf.format(input);
 363                     if (!result.equals(ifExpected)) {
 364                         throw new RuntimeException("Incorrect integer format. " +
 365                             "input: " + input + ", expected: " +
 366                             ifExpected + ", result: " + result);
 367                     }
 368                 });
 369             System.out.println("bug8232860Test succeeded.");
 370         } else {
 371             System.out.println("Test ignored. Either :-\n" +
 372                 "Default format locale is not Locale.US: " + defLoc + ", or\n" +
 373                 "OS is neither macOS/Windows, or\n" +
 374                 "provider is not HOST: " + type);
 375         }
 376     }
 377 }