1 /*
   2  * Copyright (c) 2012, 2013, 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             default:
  64                 throw new RuntimeException("Test method '"+methodName+"' not found.");
  65         }
  66     }
  67 
  68     static void getPlatformLocale(Locale.Category cat) {
  69         Locale defloc = Locale.getDefault(cat);
  70         System.out.printf("%s,%s\n", defloc.getLanguage(), defloc.getCountry());
  71     }
  72 
  73     static void adapterTest(String expected, String lang, String ctry) {
  74         Locale testLocale = new Locale(lang, ctry);
  75         String preference = System.getProperty("java.locale.providers", "");
  76         LocaleProviderAdapter lda = LocaleProviderAdapter.getAdapter(DateFormatProvider.class, testLocale);
  77         LocaleProviderAdapter.Type type = lda.getAdapterType();
  78         System.out.printf("testLocale: %s, got: %s, expected: %s\n", testLocale, type, expected);
  79         if (!type.toString().equals(expected)) {
  80             throw new RuntimeException("Returned locale data adapter is not correct.");
  81         }
  82     }
  83 
  84     static void bug7198834Test() {
  85         LocaleProviderAdapter lda = LocaleProviderAdapter.getAdapter(DateFormatProvider.class, Locale.US);
  86         LocaleProviderAdapter.Type type = lda.getAdapterType();
  87         if (type == LocaleProviderAdapter.Type.HOST && System.getProperty("os.name").startsWith("Windows")) {
  88             DateFormat df = DateFormat.getDateInstance(DateFormat.FULL, Locale.US);
  89             String date = df.format(new Date());
  90             if (date.charAt(date.length()-1) == ' ') {
  91                 throw new RuntimeException("Windows Host Locale Provider returns a trailing space.");
  92             }
  93         } else {
  94             System.out.println("Windows HOST locale adapter not found. Ignoring this test.");
  95         }
  96     }
  97 
  98     static void tzNameTest(String id) {
  99         TimeZone tz = TimeZone.getTimeZone(id);
 100         String tzName = tz.getDisplayName(false, TimeZone.SHORT, Locale.US);
 101         if (tzName.startsWith("GMT")) {
 102             throw new RuntimeException("JRE's localized time zone name for "+id+" could not be retrieved. Returned name was: "+tzName);
 103         }
 104     }
 105 
 106     static void bug8001440Test() {
 107         Locale locale = Locale.forLanguageTag("th-TH-u-nu-hoge");
 108         NumberFormat nf = NumberFormat.getInstance(locale);
 109         String nu = nf.format(1234560);
 110     }
 111 
 112     // This test assumes Windows localized language/country display names.
 113     static void bug8010666Test() {
 114         if (System.getProperty("os.name").startsWith("Windows")) {
 115             NumberFormat nf = NumberFormat.getInstance(Locale.US);
 116             try {
 117                 double ver = nf.parse(System.getProperty("os.version")).doubleValue();
 118                 System.out.printf("Windows version: %.1f\n", ver);
 119                 if (ver >= 6.0) {
 120                     LocaleProviderAdapter lda = LocaleProviderAdapter.getAdapter(LocaleNameProvider.class, Locale.ENGLISH);
 121                     LocaleProviderAdapter.Type type = lda.getAdapterType();
 122                     if (type == LocaleProviderAdapter.Type.HOST) {
 123                         Locale mkmk = Locale.forLanguageTag("mk-MK");
 124                         String result = mkmk.getDisplayLanguage(Locale.ENGLISH);
 125                         if (!"Macedonian (FYROM)".equals(result)) {
 126                             throw new RuntimeException("Windows locale name provider did not return expected localized language name for \"mk\". Returned name was \"" + result + "\"");
 127                         }
 128                         result = Locale.US.getDisplayLanguage(Locale.ENGLISH);
 129                         if (!"English".equals(result)) {
 130                             throw new RuntimeException("Windows locale name provider did not return expected localized language name for \"en\". Returned name was \"" + result + "\"");
 131                         }
 132                         result = Locale.US.getDisplayCountry(Locale.ENGLISH);
 133                         if (ver >= 6.1 && !"United States".equals(result)) {
 134                             throw new RuntimeException("Windows locale name provider did not return expected localized country name for \"US\". Returned name was \"" + result + "\"");
 135                         }
 136                     } else {
 137                         throw new RuntimeException("Windows Host LocaleProviderAdapter was not selected for English locale.");
 138                     }
 139                 }
 140             } catch (ParseException pe) {
 141                 throw new RuntimeException("Parsing Windows version failed: "+pe.toString());
 142             }
 143         }
 144     }
 145 }