Path: blob/jdk8u272-b10-aarch32-20201026/jdk/test/java/util/PluggableLocale/CurrencyNameProviderTest.java
48795 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 CurrencyNameProviderTest extends ProviderTest {3233public static void main(String[] s) {34Locale reservedLocale = Locale.getDefault();35try {36new CurrencyNameProviderTest();37} finally {38// restore the reserved locale39Locale.setDefault(reservedLocale);40}41}4243CurrencyNameProviderTest() {44test1();45test2();46}4748void test1() {49com.bar.CurrencyNameProviderImpl cnp = new com.bar.CurrencyNameProviderImpl();50com.bar.CurrencyNameProviderImpl2 cnp2 = new com.bar.CurrencyNameProviderImpl2();51Locale[] availloc = Locale.getAvailableLocales();52Locale[] testloc = availloc.clone();53List<Locale> jreimplloc = Arrays.asList(LocaleProviderAdapter.forJRE().getCurrencyNameProvider().getAvailableLocales());54List<Locale> providerloc = new ArrayList<Locale>();55providerloc.addAll(Arrays.asList(cnp.getAvailableLocales()));56providerloc.addAll(Arrays.asList(cnp2.getAvailableLocales()));5758for (Locale target: availloc) {59// pure JRE implementation60OpenListResourceBundle rb = ((ResourceBundleBasedAdapter)LocaleProviderAdapter.forJRE()).getLocaleData().getCurrencyNames(target);61boolean jreSupportsTarget = jreimplloc.contains(target);6263for (Locale test: testloc) {64// get a Currency instance65Currency c = null;66try {67c = Currency.getInstance(test);68} catch (IllegalArgumentException iae) {}6970if (c == null) {71continue;72}7374// the localized symbol for the target locale75String currencyresult = c.getSymbol(target);7677// the localized name for the target locale78String nameresult = c.getDisplayName(target);7980// provider's name (if any)81String providerscurrency = null;82String providersname = null;83if (providerloc.contains(target)) {84if (cnp.isSupportedLocale(target)) {85providerscurrency = cnp.getSymbol(c.getCurrencyCode(), target);86providersname = cnp.getDisplayName(c.getCurrencyCode(), target);87} else {88providerscurrency = cnp2.getSymbol(c.getCurrencyCode(), target);89providersname = cnp2.getDisplayName(c.getCurrencyCode(), target);90}91}9293// JRE's name94String jrescurrency = null;95String jresname = null;96String key = c.getCurrencyCode();97String nameKey = key.toLowerCase(Locale.ROOT);98if (jreSupportsTarget) {99try {100jrescurrency = rb.getString(key);101} catch (MissingResourceException mre) {}102try {103jresname = rb.getString(nameKey);104} catch (MissingResourceException mre) {}105}106107checkValidity(target, jrescurrency, providerscurrency, currencyresult,108jreSupportsTarget && jrescurrency != null);109checkValidity(target, jresname, providersname, nameresult,110jreSupportsTarget && jresname != null);111}112}113}114115116final String pattern = "###,###\u00A4";117final String YEN_IN_OSAKA = "100,000\u5186\u3084\u3002";118final String YEN_IN_KYOTO = "100,000\u5186\u3069\u3059\u3002";119final String YEN_IN_TOKYO= "100,000JPY-tokyo";120final Locale OSAKA = new Locale("ja", "JP", "osaka");121final Locale KYOTO = new Locale("ja", "JP", "kyoto");122final Locale TOKYO = new Locale("ja", "JP", "tokyo");123Integer i = new Integer(100000);124String formatted;125DecimalFormat df;126127void test2() {128Locale defloc = Locale.getDefault();129130try {131df = new DecimalFormat(pattern, DecimalFormatSymbols.getInstance(OSAKA));132System.out.println(formatted = df.format(i));133if(!formatted.equals(YEN_IN_OSAKA)) {134throw new RuntimeException("formatted currency names mismatch. " +135"Should match with " + YEN_IN_OSAKA);136}137138df.parse(YEN_IN_OSAKA);139140Locale.setDefault(KYOTO);141df = new DecimalFormat(pattern, DecimalFormatSymbols.getInstance());142System.out.println(formatted = df.format(i));143if(!formatted.equals(YEN_IN_KYOTO)) {144throw new RuntimeException("formatted currency names mismatch. " +145"Should match with " + YEN_IN_KYOTO);146}147148df.parse(YEN_IN_KYOTO);149150Locale.setDefault(TOKYO);151df = new DecimalFormat(pattern, DecimalFormatSymbols.getInstance());152System.out.println(formatted = df.format(i));153if(!formatted.equals(YEN_IN_TOKYO)) {154throw new RuntimeException("formatted currency names mismatch. " +155"Should match with " + YEN_IN_TOKYO);156}157158df.parse(YEN_IN_TOKYO);159} catch (ParseException pe) {160throw new RuntimeException("parse error occured" + pe);161} finally {162Locale.setDefault(defloc);163}164}165}166167168