Path: blob/master/test/jdk/java/util/PluggableLocale/LocaleNameProviderTest.java
41149 views
/*1* Copyright (c) 2007, 2018, 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*/2223/*24* @test25* @bug 4052440 8000273 8062588 821040626* @summary LocaleNameProvider tests27* @library providersrc/foobarutils28* providersrc/barprovider29* @modules java.base/sun.util.locale.provider30* java.base/sun.util.resources31* @build com.foobar.Utils32* com.bar.*33* @run main/othervm -Djava.locale.providers=JRE,SPI LocaleNameProviderTest34*/3536import java.util.Arrays;37import java.util.List;38import java.util.Locale;39import java.util.MissingResourceException;4041import com.bar.LocaleNameProviderImpl;4243import sun.util.locale.provider.LocaleProviderAdapter;44import sun.util.locale.provider.ResourceBundleBasedAdapter;45import sun.util.resources.OpenListResourceBundle;4647public class LocaleNameProviderTest extends ProviderTest {4849public static void main(String[] s) {50new LocaleNameProviderTest();51}5253LocaleNameProviderTest() {54checkAvailLocValidityTest();55variantFallbackTest();56}5758void checkAvailLocValidityTest() {59LocaleNameProviderImpl lnp = new LocaleNameProviderImpl();60Locale[] availloc = Locale.getAvailableLocales();61Locale[] testloc = availloc.clone();62List<Locale> jreimplloc = Arrays.asList(LocaleProviderAdapter.forJRE().getLocaleNameProvider().getAvailableLocales());63List<Locale> providerloc = Arrays.asList(lnp.getAvailableLocales());6465for (Locale target: availloc) {66// pure JRE implementation67OpenListResourceBundle rb = ((ResourceBundleBasedAdapter)LocaleProviderAdapter.forJRE()).getLocaleData().getLocaleNames(target);68boolean jreSupportsTarget = jreimplloc.contains(target);6970for (Locale test: testloc) {71// codes72String lang = test.getLanguage();73String ctry = test.getCountry();74String vrnt = test.getVariant();7576// the localized name77String langresult = test.getDisplayLanguage(target);78String ctryresult = test.getDisplayCountry(target);79String vrntresult = test.getDisplayVariant(target);8081// provider's name (if any)82String providerslang = null;83String providersctry = null;84String providersvrnt = null;85if (providerloc.contains(target)) {86providerslang = lnp.getDisplayLanguage(lang, target);87providersctry = lnp.getDisplayCountry(ctry, target);88providersvrnt = lnp.getDisplayVariant(vrnt, target);89}9091// JRE's name92String jreslang = null;93String jresctry = null;94String jresvrnt = null;95if (!lang.equals("")) {96try {97jreslang = rb.getString(lang);98} catch (MissingResourceException mre) {}99}100if (!ctry.equals("")) {101try {102jresctry = rb.getString(ctry);103} catch (MissingResourceException mre) {}104}105if (!vrnt.equals("")) {106try {107jresvrnt = rb.getString("%%"+vrnt);108} catch (MissingResourceException mre) {}109}110111System.out.print("For key: "+lang+" ");112checkValidity(target, jreslang, providerslang, langresult,113jreSupportsTarget && jreslang != null);114System.out.print("For key: "+ctry+" ");115checkValidity(target, jresctry, providersctry, ctryresult,116jreSupportsTarget && jresctry != null);117System.out.print("For key: "+vrnt+" ");118checkValidity(target, jresvrnt, providersvrnt, vrntresult,119jreSupportsTarget && jresvrnt != null);120}121}122}123124void variantFallbackTest() {125Locale YY = new Locale("yy", "YY", "YYYY");126Locale YY_suffix = new Locale("yy", "YY", "YYYY_suffix");127String retVrnt = null;128String message = "variantFallbackTest() succeeded.";129130131try {132YY.getDisplayVariant(YY_suffix);133message = "variantFallbackTest() failed. Either provider wasn't invoked, or invoked without suffix.";134} catch (RuntimeException re) {135retVrnt = re.getMessage();136if (YY_suffix.getVariant().equals(retVrnt)) {137System.out.println(message);138return;139}140message = "variantFallbackTest() failed. Returned variant: "+retVrnt;141}142143throw new RuntimeException(message);144}145}146147