Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/util/Locale/LocaleProviders.java
38813 views
/*1* Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/22import java.text.*;23import java.text.spi.*;24import java.util.*;25import java.util.spi.*;26import sun.util.locale.provider.LocaleProviderAdapter;2728public class LocaleProviders {2930public static void main(String[] args) {31String methodName = args[0];3233switch (methodName) {34case "getPlatformLocale":35if (args[1].equals("format")) {36getPlatformLocale(Locale.Category.FORMAT);37} else {38getPlatformLocale(Locale.Category.DISPLAY);39}40break;4142case "adapterTest":43adapterTest(args[1], args[2], (args.length >= 4 ? args[3] : ""));44break;4546case "bug7198834Test":47bug7198834Test();48break;4950case "tzNameTest":51tzNameTest(args[1]);52break;5354case "bug8001440Test":55bug8001440Test();56break;5758case "bug8010666Test":59bug8010666Test();60break;6162case "bug8013086Test":63bug8013086Test(args[1], args[2]);64break;6566case "bug8013903Test":67bug8013903Test();68break;6970default:71throw new RuntimeException("Test method '"+methodName+"' not found.");72}73}7475static void getPlatformLocale(Locale.Category cat) {76Locale defloc = Locale.getDefault(cat);77System.out.printf("%s,%s\n", defloc.getLanguage(), defloc.getCountry());78}7980static void adapterTest(String expected, String lang, String ctry) {81Locale testLocale = new Locale(lang, ctry);82LocaleProviderAdapter ldaExpected =83LocaleProviderAdapter.forType(LocaleProviderAdapter.Type.valueOf(expected));84if (!ldaExpected.getDateFormatProvider().isSupportedLocale(testLocale)) {85System.out.println("test locale: "+testLocale+" is not supported by the expected provider: "+ldaExpected+". Ignoring the test.");86return;87}88String preference = System.getProperty("java.locale.providers", "");89LocaleProviderAdapter lda = LocaleProviderAdapter.getAdapter(DateFormatProvider.class, testLocale);90LocaleProviderAdapter.Type type = lda.getAdapterType();91System.out.printf("testLocale: %s, got: %s, expected: %s\n", testLocale, type, expected);92if (!type.toString().equals(expected)) {93throw new RuntimeException("Returned locale data adapter is not correct.");94}95}9697static void bug7198834Test() {98LocaleProviderAdapter lda = LocaleProviderAdapter.getAdapter(DateFormatProvider.class, Locale.US);99LocaleProviderAdapter.Type type = lda.getAdapterType();100if (type == LocaleProviderAdapter.Type.HOST && System.getProperty("os.name").startsWith("Windows")) {101DateFormat df = DateFormat.getDateInstance(DateFormat.FULL, Locale.US);102String date = df.format(new Date());103if (date.charAt(date.length()-1) == ' ') {104throw new RuntimeException("Windows Host Locale Provider returns a trailing space.");105}106} else {107System.out.println("Windows HOST locale adapter not found. Ignoring this test.");108}109}110111static void tzNameTest(String id) {112TimeZone tz = TimeZone.getTimeZone(id);113String tzName = tz.getDisplayName(false, TimeZone.SHORT, Locale.US);114if (tzName.startsWith("GMT")) {115throw new RuntimeException("JRE's localized time zone name for "+id+" could not be retrieved. Returned name was: "+tzName);116}117}118119static void bug8001440Test() {120Locale locale = Locale.forLanguageTag("th-TH-u-nu-hoge");121NumberFormat nf = NumberFormat.getInstance(locale);122String nu = nf.format(1234560);123}124125// This test assumes Windows localized language/country display names.126static void bug8010666Test() {127if (System.getProperty("os.name").startsWith("Windows")) {128NumberFormat nf = NumberFormat.getInstance(Locale.US);129try {130double ver = nf.parse(System.getProperty("os.version"))131.doubleValue();132System.out.printf("Windows version: %.1f\n", ver);133if (ver >= 6.0) {134LocaleProviderAdapter lda =135LocaleProviderAdapter.getAdapter(136LocaleNameProvider.class, Locale.ENGLISH);137LocaleProviderAdapter.Type type = lda.getAdapterType();138if (type == LocaleProviderAdapter.Type.HOST) {139LocaleNameProvider lnp = lda.getLocaleNameProvider();140Locale mkmk = Locale.forLanguageTag("mk-MK");141String result = mkmk.getDisplayLanguage(Locale.ENGLISH);142String hostResult =143lnp.getDisplayLanguage(mkmk.getLanguage(),144Locale.ENGLISH);145System.out.printf(" Display language name for" +146" (mk_MK): result(HOST): \"%s\", returned: \"%s\"\n",147hostResult, result);148if (result == null ||149hostResult != null &&150!result.equals(hostResult)) {151throw new RuntimeException("Display language name" +152" mismatch for \"mk\". Returned name was" +153" \"" + result + "\", result(HOST): \"" +154hostResult + "\"");155}156result = Locale.US.getDisplayLanguage(Locale.ENGLISH);157hostResult =158lnp.getDisplayLanguage(Locale.US.getLanguage(),159Locale.ENGLISH);160System.out.printf(" Display language name for" +161" (en_US): result(HOST): \"%s\", returned: \"%s\"\n",162hostResult, result);163if (result == null ||164hostResult != null &&165!result.equals(hostResult)) {166throw new RuntimeException("Display language name" +167" mismatch for \"en\". Returned name was" +168" \"" + result + "\", result(HOST): \"" +169hostResult + "\"");170}171if (ver >= 6.1) {172result = Locale.US.getDisplayCountry(Locale.ENGLISH);173hostResult = lnp.getDisplayCountry(174Locale.US.getCountry(), Locale.ENGLISH);175System.out.printf(" Display country name for" +176" (en_US): result(HOST): \"%s\", returned: \"%s\"\n",177hostResult, result);178if (result == null ||179hostResult != null &&180!result.equals(hostResult)) {181throw new RuntimeException("Display country name" +182" mismatch for \"US\". Returned name was" +183" \"" + result + "\", result(HOST): \"" +184hostResult + "\"");185}186}187} else {188throw new RuntimeException("Windows Host" +189" LocaleProviderAdapter was not selected for" +190" English locale.");191}192}193} catch (ParseException pe) {194throw new RuntimeException("Parsing Windows version failed: "+pe.toString());195}196}197}198199static void bug8013086Test(String lang, String ctry) {200try {201// Throws a NullPointerException if the test fails.202System.out.println(new SimpleDateFormat("z", new Locale(lang, ctry)).parse("UTC"));203} catch (ParseException pe) {204// ParseException is fine in this test, as it's not "UTC"205}206}207208static void bug8013903Test() {209if (System.getProperty("os.name").startsWith("Windows")) {210Date sampleDate = new Date(0x10000000000L);211String fallbackResult = "Heisei 16.Nov.03 (Wed) AM 11:53:47";212String jreResult = "\u5e73\u6210 16.11.03 (\u6c34) \u5348\u524d 11:53:47";213Locale l = new Locale("ja", "JP", "JP");214SimpleDateFormat sdf = new SimpleDateFormat("GGGG yyyy.MMM.dd '('E')' a hh:mm:ss", l);215sdf.setTimeZone(TimeZone.getTimeZone("America/Los_Angeles"));216String result = sdf.format(sampleDate);217System.out.println(result);218if (LocaleProviderAdapter.getAdapterPreference()219.contains(LocaleProviderAdapter.Type.JRE)) {220if (!jreResult.equals(result)) {221throw new RuntimeException("Format failed. result: \"" +222result + "\", expected: \"" + jreResult);223}224} else {225// should be FALLBACK, as Windows HOST does not return226// display names227if (!fallbackResult.equals(result)) {228throw new RuntimeException("Format failed. result: \"" +229result + "\", expected: \"" + fallbackResult);230}231}232}233}234}235236237