Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/util/PluggableLocale/LocaleNameProviderTest.java
47209 views
/*1* Copyright (c) 2007, 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*/22/*23*24*/2526import java.text.*;27import java.util.*;28import sun.util.locale.provider.*;29import sun.util.resources.*;3031public class LocaleNameProviderTest extends ProviderTest {3233public static void main(String[] s) {34new LocaleNameProviderTest();35}3637LocaleNameProviderTest() {38checkAvailLocValidityTest();39variantFallbackTest();40}4142void checkAvailLocValidityTest() {43com.bar.LocaleNameProviderImpl lnp = new com.bar.LocaleNameProviderImpl();44Locale[] availloc = Locale.getAvailableLocales();45Locale[] testloc = availloc.clone();46List<Locale> jreimplloc = Arrays.asList(LocaleProviderAdapter.forJRE().getLocaleNameProvider().getAvailableLocales());47List<Locale> providerloc = Arrays.asList(lnp.getAvailableLocales());4849for (Locale target: availloc) {50// pure JRE implementation51OpenListResourceBundle rb = ((ResourceBundleBasedAdapter)LocaleProviderAdapter.forJRE()).getLocaleData().getLocaleNames(target);52boolean jreSupportsTarget = jreimplloc.contains(target);5354for (Locale test: testloc) {55// codes56String lang = test.getLanguage();57String ctry = test.getCountry();58String vrnt = test.getVariant();5960// the localized name61String langresult = test.getDisplayLanguage(target);62String ctryresult = test.getDisplayCountry(target);63String vrntresult = test.getDisplayVariant(target);6465// provider's name (if any)66String providerslang = null;67String providersctry = null;68String providersvrnt = null;69if (providerloc.contains(target)) {70providerslang = lnp.getDisplayLanguage(lang, target);71providersctry = lnp.getDisplayCountry(ctry, target);72providersvrnt = lnp.getDisplayVariant(vrnt, target);73}7475// JRE's name76String jreslang = null;77String jresctry = null;78String jresvrnt = null;79if (!lang.equals("")) {80try {81jreslang = rb.getString(lang);82} catch (MissingResourceException mre) {}83}84if (!ctry.equals("")) {85try {86jresctry = rb.getString(ctry);87} catch (MissingResourceException mre) {}88}89if (!vrnt.equals("")) {90try {91jresvrnt = rb.getString("%%"+vrnt);92} catch (MissingResourceException mre) {}93}9495System.out.print("For key: "+lang+" ");96checkValidity(target, jreslang, providerslang, langresult,97jreSupportsTarget && jreslang != null);98System.out.print("For key: "+ctry+" ");99checkValidity(target, jresctry, providersctry, ctryresult,100jreSupportsTarget && jresctry != null);101System.out.print("For key: "+vrnt+" ");102checkValidity(target, jresvrnt, providersvrnt, vrntresult,103jreSupportsTarget && jresvrnt != null);104}105}106}107108void variantFallbackTest() {109Locale YY = new Locale("yy", "YY", "YYYY");110Locale YY_suffix = new Locale("yy", "YY", "YYYY_suffix");111String retVrnt = null;112String message = "variantFallbackTest() succeeded.";113114115try {116YY.getDisplayVariant(YY_suffix);117message = "variantFallbackTest() failed. Either provider wasn't invoked, or invoked without suffix.";118} catch (RuntimeException re) {119retVrnt = re.getMessage();120if (YY_suffix.getVariant().equals(retVrnt)) {121System.out.println(message);122return;123}124message = "variantFallbackTest() failed. Returned variant: "+retVrnt;125}126127throw new RuntimeException(message);128}129}130131132