< prev index next >

test/jdk/java/util/Locale/LocaleProviders.java

Print this page
rev 55815 : [mq]: 8228465


  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 


  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             case "bug8220227Test":
  76                 bug8220227Test();
  77                 break;
  78 




  79             default:
  80                 throw new RuntimeException("Test method '"+methodName+"' not found.");
  81         }
  82     }
  83 
  84     static void getPlatformLocale(Locale.Category cat) {
  85         Locale defloc = Locale.getDefault(cat);
  86         System.out.printf("%s,%s\n", defloc.getLanguage(), defloc.getCountry());
  87     }
  88 
  89     static void adapterTest(String expected, String lang, String ctry) {
  90         Locale testLocale = new Locale(lang, ctry);
  91         LocaleProviderAdapter ldaExpected =
  92             LocaleProviderAdapter.forType(LocaleProviderAdapter.Type.valueOf(expected));
  93         if (!ldaExpected.getDateFormatProvider().isSupportedLocale(testLocale)) {
  94             System.out.println("test locale: "+testLocale+" is not supported by the expected provider: "+ldaExpected+". Ignoring the test.");
  95             return;
  96         }
  97         String preference = System.getProperty("java.locale.providers", "");
  98         LocaleProviderAdapter lda = LocaleProviderAdapter.getAdapter(DateFormatProvider.class, testLocale);
  99         LocaleProviderAdapter.Type type = lda.getAdapterType();
 100         System.out.printf("testLocale: %s, got: %s, expected: %s\n", testLocale, type, expected);
 101         if (!type.toString().equals(expected)) {
 102             throw new RuntimeException("Returned locale data adapter is not correct.");
 103         }
 104     }
 105 
 106     static void bug7198834Test() {
 107         LocaleProviderAdapter lda = LocaleProviderAdapter.getAdapter(DateFormatProvider.class, Locale.US);
 108         LocaleProviderAdapter.Type type = lda.getAdapterType();
 109         if (type == LocaleProviderAdapter.Type.HOST && System.getProperty("os.name").startsWith("Windows")) {
 110             DateFormat df = DateFormat.getDateInstance(DateFormat.FULL, Locale.US);
 111             String date = df.format(new Date());
 112             if (date.charAt(date.length()-1) == ' ') {
 113                 throw new RuntimeException("Windows Host Locale Provider returns a trailing space.");
 114             }
 115         } else {
 116             System.out.println("Windows HOST locale adapter not found. Ignoring this test.");
 117         }
 118     }
 119 
 120     static void tzNameTest(String id) {
 121         TimeZone tz = TimeZone.getTimeZone(id);
 122         String tzName = tz.getDisplayName(false, TimeZone.SHORT, Locale.US);
 123         if (tzName.startsWith("GMT")) {
 124             throw new RuntimeException("JRE's localized time zone name for "+id+" could not be retrieved. Returned name was: "+tzName);
 125         }
 126     }
 127 
 128     static void bug8001440Test() {
 129         Locale locale = Locale.forLanguageTag("th-TH-u-nu-hoge");
 130         NumberFormat nf = NumberFormat.getInstance(locale);
 131         String nu = nf.format(1234560);
 132     }
 133 
 134     // This test assumes Windows localized language/country display names.
 135     static void bug8010666Test() {
 136         if (System.getProperty("os.name").startsWith("Windows")) {
 137             NumberFormat nf = NumberFormat.getInstance(Locale.US);
 138             try {
 139                 double ver = nf.parse(System.getProperty("os.version"))
 140                                .doubleValue();
 141                 System.out.printf("Windows version: %.1f\n", ver);
 142                 if (ver >= 6.0) {
 143                     LocaleProviderAdapter lda =
 144                         LocaleProviderAdapter.getAdapter(
 145                             LocaleNameProvider.class, Locale.ENGLISH);
 146                     LocaleProviderAdapter.Type type = lda.getAdapterType();
 147                     if (type == LocaleProviderAdapter.Type.HOST) {
 148                         LocaleNameProvider lnp = lda.getLocaleNameProvider();
 149                         Locale mkmk = Locale.forLanguageTag("mk-MK");
 150                         String result = mkmk.getDisplayLanguage(Locale.ENGLISH);
 151                         String hostResult =
 152                             lnp.getDisplayLanguage(mkmk.getLanguage(),
 153                                                    Locale.ENGLISH);
 154                         System.out.printf("  Display language name for" +
 155                             " (mk_MK): result(HOST): \"%s\", returned: \"%s\"\n",
 156                             hostResult, result);


 198                             " LocaleProviderAdapter was not selected for" +
 199                             " English locale.");
 200                     }
 201                 }
 202             } catch (ParseException pe) {
 203                 throw new RuntimeException("Parsing Windows version failed: "+pe.toString());
 204             }
 205         }
 206     }
 207 
 208     static void bug8013086Test(String lang, String ctry) {
 209         try {
 210             // Throws a NullPointerException if the test fails.
 211             System.out.println(new SimpleDateFormat("z", new Locale(lang, ctry)).parse("UTC"));
 212         } catch (ParseException pe) {
 213             // ParseException is fine in this test, as it's not "UTC"
 214 }
 215     }
 216 
 217     static void bug8013903Test() {
 218         if (System.getProperty("os.name").startsWith("Windows")) {
 219             Date sampleDate = new Date(0x10000000000L);
 220             String hostResult = "\u5e73\u6210 16.11.03 (Wed) AM 11:53:47";
 221             String jreResult = "\u5e73\u6210 16.11.03 (\u6c34) \u5348\u524d 11:53:47";
 222             Locale l = new Locale("ja", "JP", "JP");
 223             SimpleDateFormat sdf = new SimpleDateFormat("GGGG yyyy.MMM.dd '('E')' a hh:mm:ss", l);
 224             sdf.setTimeZone(TimeZone.getTimeZone("America/Los_Angeles"));
 225             String result = sdf.format(sampleDate);
 226             System.out.println(result);
 227             if (LocaleProviderAdapter.getAdapterPreference()
 228                 .contains(LocaleProviderAdapter.Type.JRE)) {
 229                 if (!jreResult.equals(result)) {
 230                     throw new RuntimeException("Format failed. result: \"" +
 231                         result + "\", expected: \"" + jreResult);
 232                 }
 233             } else {
 234                 // Windows display names. Subject to change if Windows changes its format.
 235                 if (!hostResult.equals(result)) {
 236                     throw new RuntimeException("Format failed. result: \"" +
 237                         result + "\", expected: \"" + hostResult);
 238                 }
 239             }
 240         }
 241     }
 242 
 243     static void bug8027289Test(String expectedCodePoint) {
 244         if (System.getProperty("os.name").startsWith("Windows")) {
 245             char[] expectedSymbol = Character.toChars(Integer.valueOf(expectedCodePoint, 16));
 246             NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.CHINA);
 247             char formatted = nf.format(7000).charAt(0);
 248             System.out.println("returned: " + formatted + ", expected: " + expectedSymbol[0]);
 249             if (formatted != expectedSymbol[0]) {
 250                 throw new RuntimeException(
 251                         "Unexpected Chinese currency symbol. returned: "
 252                                 + formatted + ", expected: " + expectedSymbol[0]);
 253             }
 254         }
 255     }
 256 
 257     static void bug8220227Test() {
 258         if (System.getProperty("os.name").startsWith("Windows")) {
 259             Locale l = new Locale("xx","XX");
 260             String country = l.getDisplayCountry();
 261             if (country.endsWith("(XX)")) {
 262                 throw new RuntimeException(
 263                         "Unexpected Region name: " + country);
















 264             }
 265         }
 266     }
 267 }


  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     private static final boolean IS_WINDOWS = System.getProperty("os.name").startsWith("Windows");
  32 
  33     public static void main(String[] args) {
  34         String methodName = args[0];
  35 
  36         switch (methodName) {
  37             case "getPlatformLocale":
  38                 if (args[1].equals("format")) {
  39                     getPlatformLocale(Locale.Category.FORMAT);
  40                 } else {
  41                     getPlatformLocale(Locale.Category.DISPLAY);
  42                 }
  43                 break;
  44 
  45             case "adapterTest":
  46                 adapterTest(args[1], args[2], (args.length >= 4 ? args[3] : ""));
  47                 break;
  48 
  49             case "bug7198834Test":
  50                 bug7198834Test();
  51                 break;
  52 


  61             case "bug8010666Test":
  62                 bug8010666Test();
  63                 break;
  64 
  65             case "bug8013086Test":
  66                 bug8013086Test(args[1], args[2]);
  67                 break;
  68 
  69             case "bug8013903Test":
  70                 bug8013903Test();
  71                 break;
  72 
  73             case "bug8027289Test":
  74                 bug8027289Test(args[1]);
  75                 break;
  76 
  77             case "bug8220227Test":
  78                 bug8220227Test();
  79                 break;
  80 
  81             case "bug8228465Test":
  82                 bug8228465Test();
  83                 break;
  84 
  85             default:
  86                 throw new RuntimeException("Test method '"+methodName+"' not found.");
  87         }
  88     }
  89 
  90     static void getPlatformLocale(Locale.Category cat) {
  91         Locale defloc = Locale.getDefault(cat);
  92         System.out.printf("%s,%s\n", defloc.getLanguage(), defloc.getCountry());
  93     }
  94 
  95     static void adapterTest(String expected, String lang, String ctry) {
  96         Locale testLocale = new Locale(lang, ctry);
  97         LocaleProviderAdapter ldaExpected =
  98             LocaleProviderAdapter.forType(LocaleProviderAdapter.Type.valueOf(expected));
  99         if (!ldaExpected.getDateFormatProvider().isSupportedLocale(testLocale)) {
 100             System.out.println("test locale: "+testLocale+" is not supported by the expected provider: "+ldaExpected+". Ignoring the test.");
 101             return;
 102         }
 103         String preference = System.getProperty("java.locale.providers", "");
 104         LocaleProviderAdapter lda = LocaleProviderAdapter.getAdapter(DateFormatProvider.class, testLocale);
 105         LocaleProviderAdapter.Type type = lda.getAdapterType();
 106         System.out.printf("testLocale: %s, got: %s, expected: %s\n", testLocale, type, expected);
 107         if (!type.toString().equals(expected)) {
 108             throw new RuntimeException("Returned locale data adapter is not correct.");
 109         }
 110     }
 111 
 112     static void bug7198834Test() {
 113         LocaleProviderAdapter lda = LocaleProviderAdapter.getAdapter(DateFormatProvider.class, Locale.US);
 114         LocaleProviderAdapter.Type type = lda.getAdapterType();
 115         if (type == LocaleProviderAdapter.Type.HOST && IS_WINDOWS) {
 116             DateFormat df = DateFormat.getDateInstance(DateFormat.FULL, Locale.US);
 117             String date = df.format(new Date());
 118             if (date.charAt(date.length()-1) == ' ') {
 119                 throw new RuntimeException("Windows Host Locale Provider returns a trailing space.");
 120             }
 121         } else {
 122             System.out.println("Windows HOST locale adapter not found. Ignoring this test.");
 123         }
 124     }
 125 
 126     static void tzNameTest(String id) {
 127         TimeZone tz = TimeZone.getTimeZone(id);
 128         String tzName = tz.getDisplayName(false, TimeZone.SHORT, Locale.US);
 129         if (tzName.startsWith("GMT")) {
 130             throw new RuntimeException("JRE's localized time zone name for "+id+" could not be retrieved. Returned name was: "+tzName);
 131         }
 132     }
 133 
 134     static void bug8001440Test() {
 135         Locale locale = Locale.forLanguageTag("th-TH-u-nu-hoge");
 136         NumberFormat nf = NumberFormat.getInstance(locale);
 137         String nu = nf.format(1234560);
 138     }
 139 
 140     // This test assumes Windows localized language/country display names.
 141     static void bug8010666Test() {
 142         if (IS_WINDOWS) {
 143             NumberFormat nf = NumberFormat.getInstance(Locale.US);
 144             try {
 145                 double ver = nf.parse(System.getProperty("os.version"))
 146                                .doubleValue();
 147                 System.out.printf("Windows version: %.1f\n", ver);
 148                 if (ver >= 6.0) {
 149                     LocaleProviderAdapter lda =
 150                         LocaleProviderAdapter.getAdapter(
 151                             LocaleNameProvider.class, Locale.ENGLISH);
 152                     LocaleProviderAdapter.Type type = lda.getAdapterType();
 153                     if (type == LocaleProviderAdapter.Type.HOST) {
 154                         LocaleNameProvider lnp = lda.getLocaleNameProvider();
 155                         Locale mkmk = Locale.forLanguageTag("mk-MK");
 156                         String result = mkmk.getDisplayLanguage(Locale.ENGLISH);
 157                         String hostResult =
 158                             lnp.getDisplayLanguage(mkmk.getLanguage(),
 159                                                    Locale.ENGLISH);
 160                         System.out.printf("  Display language name for" +
 161                             " (mk_MK): result(HOST): \"%s\", returned: \"%s\"\n",
 162                             hostResult, result);


 204                             " LocaleProviderAdapter was not selected for" +
 205                             " English locale.");
 206                     }
 207                 }
 208             } catch (ParseException pe) {
 209                 throw new RuntimeException("Parsing Windows version failed: "+pe.toString());
 210             }
 211         }
 212     }
 213 
 214     static void bug8013086Test(String lang, String ctry) {
 215         try {
 216             // Throws a NullPointerException if the test fails.
 217             System.out.println(new SimpleDateFormat("z", new Locale(lang, ctry)).parse("UTC"));
 218         } catch (ParseException pe) {
 219             // ParseException is fine in this test, as it's not "UTC"
 220 }
 221     }
 222 
 223     static void bug8013903Test() {
 224         if (IS_WINDOWS) {
 225             Date sampleDate = new Date(0x10000000000L);
 226             String hostResult = "\u5e73\u6210 16.11.03 (Wed) AM 11:53:47";
 227             String jreResult = "\u5e73\u6210 16.11.03 (\u6c34) \u5348\u524d 11:53:47";
 228             Locale l = new Locale("ja", "JP", "JP");
 229             SimpleDateFormat sdf = new SimpleDateFormat("GGGG yyyy.MMM.dd '('E')' a hh:mm:ss", l);
 230             sdf.setTimeZone(TimeZone.getTimeZone("America/Los_Angeles"));
 231             String result = sdf.format(sampleDate);
 232             System.out.println(result);
 233             if (LocaleProviderAdapter.getAdapterPreference()
 234                 .contains(LocaleProviderAdapter.Type.JRE)) {
 235                 if (!jreResult.equals(result)) {
 236                     throw new RuntimeException("Format failed. result: \"" +
 237                         result + "\", expected: \"" + jreResult);
 238                 }
 239             } else {
 240                 // Windows display names. Subject to change if Windows changes its format.
 241                 if (!hostResult.equals(result)) {
 242                     throw new RuntimeException("Format failed. result: \"" +
 243                         result + "\", expected: \"" + hostResult);
 244                 }
 245             }
 246         }
 247     }
 248 
 249     static void bug8027289Test(String expectedCodePoint) {
 250         if (IS_WINDOWS) {
 251             char[] expectedSymbol = Character.toChars(Integer.valueOf(expectedCodePoint, 16));
 252             NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.CHINA);
 253             char formatted = nf.format(7000).charAt(0);
 254             System.out.println("returned: " + formatted + ", expected: " + expectedSymbol[0]);
 255             if (formatted != expectedSymbol[0]) {
 256                 throw new RuntimeException(
 257                         "Unexpected Chinese currency symbol. returned: "
 258                                 + formatted + ", expected: " + expectedSymbol[0]);
 259             }
 260         }
 261     }
 262 
 263     static void bug8220227Test() {
 264         if (IS_WINDOWS) {
 265             Locale l = new Locale("xx","XX");
 266             String country = l.getDisplayCountry();
 267             if (country.endsWith("(XX)")) {
 268                 throw new RuntimeException(
 269                         "Unexpected Region name: " + country);
 270             }
 271         }
 272     }
 273 
 274     static void bug8228465Test() {
 275         LocaleProviderAdapter lda = LocaleProviderAdapter.getAdapter(CalendarNameProvider.class, Locale.US);
 276         LocaleProviderAdapter.Type type = lda.getAdapterType();
 277         if (type == LocaleProviderAdapter.Type.HOST && IS_WINDOWS) {
 278             var names =  new GregorianCalendar()
 279                 .getDisplayNames(Calendar.ERA, Calendar.SHORT_FORMAT, Locale.US);
 280             if (!names.keySet().contains("AD") ||
 281                 names.get("AD").intValue() != 1) {
 282                     throw new RuntimeException(
 283                             "Short Era name for 'AD' is missing or incorrect");
 284             } else {
 285                 System.out.println("bug8228465Test succeeded.");
 286             }
 287         }
 288     }
 289 }
< prev index next >