1 /*
   2  * Copyright (c) 2012, 2019, 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 sun.util.locale.provider.LocaleProviderAdapter;
  28 
  29 public class LocaleProviders {
  30 
  31     public static void main(String[] args) {
  32         String methodName = args[0];
  33 
  34         switch (methodName) {
  35             case "getPlatformLocale":
  36                 if (args[1].equals("format")) {
  37                     getPlatformLocale(Locale.Category.FORMAT);
  38                 } else {
  39                     getPlatformLocale(Locale.Category.DISPLAY);
  40                 }
  41                 break;
  42 
  43             case "adapterTest":
  44                 adapterTest(args[1], args[2], (args.length >= 4 ? args[3] : ""));
  45                 break;
  46 
  47             case "bug7198834Test":
  48                 bug7198834Test();
  49                 break;
  50 
  51             case "tzNameTest":
  52                 tzNameTest(args[1]);
  53                 break;
  54 
  55             case "bug8001440Test":
  56                 bug8001440Test();
  57                 break;
  58 
  59             case "bug8010666Test":
  60                 bug8010666Test();
  61                 break;
  62 
  63             case "bug8013086Test":
  64                 bug8013086Test(args[1], args[2]);
  65                 break;
  66 
  67             case "bug8013903Test":
  68                 bug8013903Test();
  69                 break;
  70 
  71             case "bug8027289Test":
  72                 bug8027289Test(args[1]);
  73                 break;
  74 
  75             default:
  76                 throw new RuntimeException("Test method '"+methodName+"' not found.");
  77         }
  78     }
  79 
  80     static void getPlatformLocale(Locale.Category cat) {
  81         Locale defloc = Locale.getDefault(cat);
  82         System.out.printf("%s,%s\n", defloc.getLanguage(), defloc.getCountry());
  83     }
  84 
  85     static void adapterTest(String expected, String lang, String ctry) {
  86         Locale testLocale = new Locale(lang, ctry);
  87         LocaleProviderAdapter ldaExpected =
  88             LocaleProviderAdapter.forType(LocaleProviderAdapter.Type.valueOf(expected));
  89         if (!ldaExpected.getDateFormatProvider().isSupportedLocale(testLocale)) {
  90             System.out.println("test locale: "+testLocale+" is not supported by the expected provider: "+ldaExpected+". Ignoring the test.");
  91             return;
  92         }
  93         String preference = System.getProperty("java.locale.providers", "");
  94         LocaleProviderAdapter lda = LocaleProviderAdapter.getAdapter(DateFormatProvider.class, testLocale);
  95         LocaleProviderAdapter.Type type = lda.getAdapterType();
  96         System.out.printf("testLocale: %s, got: %s, expected: %s\n", testLocale, type, expected);
  97         if (!type.toString().equals(expected)) {
  98             throw new RuntimeException("Returned locale data adapter is not correct.");
  99         }
 100     }
 101 
 102     static void bug7198834Test() {
 103         LocaleProviderAdapter lda = LocaleProviderAdapter.getAdapter(DateFormatProvider.class, Locale.US);
 104         LocaleProviderAdapter.Type type = lda.getAdapterType();
 105         if (type == LocaleProviderAdapter.Type.HOST && System.getProperty("os.name").startsWith("Windows")) {
 106             DateFormat df = DateFormat.getDateInstance(DateFormat.FULL, Locale.US);
 107             String date = df.format(new Date());
 108             if (date.charAt(date.length()-1) == ' ') {
 109                 throw new RuntimeException("Windows Host Locale Provider returns a trailing space.");
 110             }
 111         } else {
 112             System.out.println("Windows HOST locale adapter not found. Ignoring this test.");
 113         }
 114     }
 115 
 116     static void tzNameTest(String id) {
 117         TimeZone tz = TimeZone.getTimeZone(id);
 118         String tzName = tz.getDisplayName(false, TimeZone.SHORT, Locale.US);
 119         if (tzName.startsWith("GMT")) {
 120             throw new RuntimeException("JRE's localized time zone name for "+id+" could not be retrieved. Returned name was: "+tzName);
 121         }
 122     }
 123 
 124     static void bug8001440Test() {
 125         Locale locale = Locale.forLanguageTag("th-TH-u-nu-hoge");
 126         NumberFormat nf = NumberFormat.getInstance(locale);
 127         String nu = nf.format(1234560);
 128     }
 129 
 130     // This test assumes Windows localized language/country display names.
 131     static void bug8010666Test() {
 132         if (System.getProperty("os.name").startsWith("Windows")) {
 133             NumberFormat nf = NumberFormat.getInstance(Locale.US);
 134             try {
 135                 double ver = nf.parse(System.getProperty("os.version"))
 136                                .doubleValue();
 137                 System.out.printf("Windows version: %.1f\n", ver);
 138                 if (ver >= 6.0) {
 139                     LocaleProviderAdapter lda =
 140                         LocaleProviderAdapter.getAdapter(
 141                             LocaleNameProvider.class, Locale.ENGLISH);
 142                     LocaleProviderAdapter.Type type = lda.getAdapterType();
 143                     if (type == LocaleProviderAdapter.Type.HOST) {
 144                         LocaleNameProvider lnp = lda.getLocaleNameProvider();
 145                         Locale mkmk = Locale.forLanguageTag("mk-MK");
 146                         String result = mkmk.getDisplayLanguage(Locale.ENGLISH);
 147                         String hostResult =
 148                             lnp.getDisplayLanguage(mkmk.getLanguage(),
 149                                                    Locale.ENGLISH);
 150                         System.out.printf("  Display language name for" +
 151                             " (mk_MK): result(HOST): \"%s\", returned: \"%s\"\n",
 152                             hostResult, result);
 153                         if (result == null ||
 154                             hostResult != null &&
 155                             !result.equals(hostResult)) {
 156                             throw new RuntimeException("Display language name" +
 157                                 " mismatch for \"mk\". Returned name was" +
 158                                 " \"" + result + "\", result(HOST): \"" +
 159                                 hostResult + "\"");
 160                         }
 161                         result = Locale.US.getDisplayLanguage(Locale.ENGLISH);
 162                         hostResult =
 163                             lnp.getDisplayLanguage(Locale.US.getLanguage(),
 164                                                    Locale.ENGLISH);
 165                         System.out.printf("  Display language name for" +
 166                             " (en_US): result(HOST): \"%s\", returned: \"%s\"\n",
 167                             hostResult, result);
 168                         if (result == null ||
 169                             hostResult != null &&
 170                             !result.equals(hostResult)) {
 171                             throw new RuntimeException("Display language name" +
 172                                 " mismatch for \"en\". Returned name was" +
 173                                 " \"" + result + "\", result(HOST): \"" +
 174                                 hostResult + "\"");
 175                         }
 176                         if (ver >= 6.1) {
 177                             result = Locale.US.getDisplayCountry(Locale.ENGLISH);
 178                             hostResult = lnp.getDisplayCountry(
 179                                 Locale.US.getCountry(), Locale.ENGLISH);
 180                             System.out.printf("  Display country name for" +
 181                                 " (en_US): result(HOST): \"%s\", returned: \"%s\"\n",
 182                                 hostResult, result);
 183                             if (result == null ||
 184                                 hostResult != null &&
 185                                 !result.equals(hostResult)) {
 186                                 throw new RuntimeException("Display country name" +
 187                                     " mismatch for \"US\". Returned name was" +
 188                                     " \"" + result + "\", result(HOST): \"" +
 189                                     hostResult + "\"");
 190                             }
 191                         }
 192                     } else {
 193                         throw new RuntimeException("Windows Host" +
 194                             " LocaleProviderAdapter was not selected for" +
 195                             " English locale.");
 196                     }
 197                 }
 198             } catch (ParseException pe) {
 199                 throw new RuntimeException("Parsing Windows version failed: "+pe.toString());
 200             }
 201         }
 202     }
 203 
 204     static void bug8013086Test(String lang, String ctry) {
 205         try {
 206             // Throws a NullPointerException if the test fails.
 207             System.out.println(new SimpleDateFormat("z", new Locale(lang, ctry)).parse("UTC"));
 208         } catch (ParseException pe) {
 209             // ParseException is fine in this test, as it's not "UTC"
 210 }
 211     }
 212 
 213     static void bug8013903Test() {
 214         if (System.getProperty("os.name").startsWith("Windows")) {
 215             Date sampleDate = new Date(0x10000000000L);
 216             String hostResult = "\u5e73\u6210 16.11.03 (Wed) AM 11:53:47";
 217             String jreResult = "\u5e73\u6210 16.11.03 (\u6c34) \u5348\u524d 11:53:47";
 218             Locale l = new Locale("ja", "JP", "JP");
 219             SimpleDateFormat sdf = new SimpleDateFormat("GGGG yyyy.MMM.dd '('E')' a hh:mm:ss", l);
 220             sdf.setTimeZone(TimeZone.getTimeZone("America/Los_Angeles"));
 221             String result = sdf.format(sampleDate);
 222             System.out.println(result);
 223             if (LocaleProviderAdapter.getAdapterPreference()
 224                 .contains(LocaleProviderAdapter.Type.JRE)) {
 225                 if (!jreResult.equals(result)) {
 226                     throw new RuntimeException("Format failed. result: \"" +
 227                         result + "\", expected: \"" + jreResult);
 228                 }
 229             } else {
 230                 // Windows display names. Subject to change if Windows changes its format.
 231                 if (!hostResult.equals(result)) {
 232                     throw new RuntimeException("Format failed. result: \"" +
 233                         result + "\", expected: \"" + hostResult);
 234                 }
 235             }
 236         }
 237     }
 238 
 239     static void bug8027289Test(String expectedCodePoint) {
 240         if (System.getProperty("os.name").startsWith("Windows")) {
 241             char[] expectedSymbol = Character.toChars(Integer.valueOf(expectedCodePoint, 16));
 242             NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.CHINA);
 243             char formatted = nf.format(7000).charAt(0);
 244             System.out.println("returned: " + formatted + ", expected: " + expectedSymbol[0]);
 245             if (formatted != expectedSymbol[0]) {
 246                 throw new RuntimeException(
 247                         "Unexpected Chinese currency symbol. returned: "
 248                                 + formatted + ", expected: " + expectedSymbol[0]);
 249             }
 250         }
 251     }
 252 }