Path: blob/master/test/jdk/java/util/PluggableLocale/CurrencyNameProviderTest.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 7199750 8000997 8062588 821040626* @summary CurrencyNameProvider 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 CurrencyNameProviderTest34*/3536import java.text.DecimalFormat;37import java.text.DecimalFormatSymbols;38import java.text.ParseException;39import java.util.ArrayList;40import java.util.Arrays;41import java.util.Currency;42import java.util.List;43import java.util.Locale;44import java.util.MissingResourceException;4546import com.bar.CurrencyNameProviderImpl;47import com.bar.CurrencyNameProviderImpl2;4849import sun.util.locale.provider.LocaleProviderAdapter;50import sun.util.locale.provider.ResourceBundleBasedAdapter;51import sun.util.resources.OpenListResourceBundle;5253public class CurrencyNameProviderTest extends ProviderTest {5455public static void main(String[] s) {56Locale reservedLocale = Locale.getDefault();57try {58new CurrencyNameProviderTest();59} finally {60// restore the reserved locale61Locale.setDefault(reservedLocale);62}63}6465CurrencyNameProviderTest() {66test1();67test2();68}6970void test1() {71CurrencyNameProviderImpl cnp = new CurrencyNameProviderImpl();72CurrencyNameProviderImpl2 cnp2 = new CurrencyNameProviderImpl2();73Locale[] availloc = Locale.getAvailableLocales();74Locale[] testloc = availloc.clone();75List<Locale> jreimplloc = Arrays.asList(LocaleProviderAdapter.forJRE().getCurrencyNameProvider().getAvailableLocales());76List<Locale> providerloc = new ArrayList<Locale>();77providerloc.addAll(Arrays.asList(cnp.getAvailableLocales()));78providerloc.addAll(Arrays.asList(cnp2.getAvailableLocales()));7980for (Locale target: availloc) {81// pure JRE implementation82OpenListResourceBundle rb = ((ResourceBundleBasedAdapter)LocaleProviderAdapter.forJRE()).getLocaleData().getCurrencyNames(target);83boolean jreSupportsTarget = jreimplloc.contains(target);8485for (Locale test: testloc) {86// get a Currency instance87Currency c = null;88try {89c = Currency.getInstance(test);90} catch (IllegalArgumentException iae) {}9192if (c == null) {93continue;94}9596// the localized symbol for the target locale97String currencyresult = c.getSymbol(target);9899// the localized name for the target locale100String nameresult = c.getDisplayName(target);101102// provider's name (if any)103String providerscurrency = null;104String providersname = null;105if (providerloc.contains(target)) {106if (cnp.isSupportedLocale(target)) {107providerscurrency = cnp.getSymbol(c.getCurrencyCode(), target);108providersname = cnp.getDisplayName(c.getCurrencyCode(), target);109} else {110providerscurrency = cnp2.getSymbol(c.getCurrencyCode(), target);111providersname = cnp2.getDisplayName(c.getCurrencyCode(), target);112}113}114115// JRE's name116String jrescurrency = null;117String jresname = null;118String key = c.getCurrencyCode();119String nameKey = key.toLowerCase(Locale.ROOT);120if (jreSupportsTarget) {121try {122jrescurrency = rb.getString(key);123} catch (MissingResourceException mre) {}124try {125jresname = rb.getString(nameKey);126} catch (MissingResourceException mre) {}127}128129checkValidity(target, jrescurrency, providerscurrency, currencyresult,130jreSupportsTarget && jrescurrency != null);131checkValidity(target, jresname, providersname, nameresult,132jreSupportsTarget && jresname != null);133}134}135}136137138final String pattern = "###,###\u00A4";139final String YEN_IN_OSAKA = "100,000\u5186\u3084\u3002";140final String YEN_IN_KYOTO = "100,000\u5186\u3069\u3059\u3002";141final String YEN_IN_TOKYO= "100,000JPY-tokyo";142final Locale OSAKA = new Locale("ja", "JP", "osaka");143final Locale KYOTO = new Locale("ja", "JP", "kyoto");144final Locale TOKYO = new Locale("ja", "JP", "tokyo");145Integer i = new Integer(100000);146String formatted;147DecimalFormat df;148149void test2() {150Locale defloc = Locale.getDefault();151152try {153df = new DecimalFormat(pattern, DecimalFormatSymbols.getInstance(OSAKA));154System.out.println(formatted = df.format(i));155if(!formatted.equals(YEN_IN_OSAKA)) {156throw new RuntimeException("formatted currency names mismatch. " +157"Should match with " + YEN_IN_OSAKA);158}159160df.parse(YEN_IN_OSAKA);161162Locale.setDefault(KYOTO);163df = new DecimalFormat(pattern, DecimalFormatSymbols.getInstance());164System.out.println(formatted = df.format(i));165if(!formatted.equals(YEN_IN_KYOTO)) {166throw new RuntimeException("formatted currency names mismatch. " +167"Should match with " + YEN_IN_KYOTO);168}169170df.parse(YEN_IN_KYOTO);171172Locale.setDefault(TOKYO);173df = new DecimalFormat(pattern, DecimalFormatSymbols.getInstance());174System.out.println(formatted = df.format(i));175if(!formatted.equals(YEN_IN_TOKYO)) {176throw new RuntimeException("formatted currency names mismatch. " +177"Should match with " + YEN_IN_TOKYO);178}179180df.parse(YEN_IN_TOKYO);181} catch (ParseException pe) {182throw new RuntimeException("parse error occured" + pe);183} finally {184Locale.setDefault(defloc);185}186}187}188189