2 *******************************************************************************
3 * Copyright (C) 2008-2012, International Business Machines Corporation and *
4 * others. All Rights Reserved. *
5 *******************************************************************************
7 package com.ibm.icu.dev.test.localespi;
9 import java.lang.reflect.Method;
10 import java.util.Collections;
11 import java.util.Currency;
12 import java.util.HashSet;
13 import java.util.Locale;
16 import com.ibm.icu.dev.test.TestFmwk;
18 @SuppressWarnings("unchecked")
19 public class CurrencyNameTest extends TestFmwk {
20 public static void main(String[] args) throws Exception {
21 new CurrencyNameTest().run(args);
24 private static final Set<Currency> AVAILABLE_CURRENCIES;
25 private static final Method GETDISPLAYNAME_METHOD;
28 Method mGetDisplayName = null;
29 Set<Currency> currencies = null;
31 mGetDisplayName = Currency.class.getMethod("getDisplayName", new Class[] {Locale.class});
32 Method mGetAvailableCurrencies = Currency.class.getMethod("getAvailableCurrencies", (Class[]) null);
33 currencies = (Set<Currency>)mGetAvailableCurrencies.invoke(null, (Object[]) null);
34 } catch (Exception e) {
38 if (currencies == null) {
39 // Make a set of unique currencies
40 currencies = new HashSet<Currency>();
41 for (Locale l : Locale.getAvailableLocales()) {
42 if (l.getCountry().length() == 0) {
46 Currency currency = Currency.getInstance(l);
47 if (currency != null) {
48 currencies.add(currency);
50 } catch (IllegalArgumentException iae) {
55 GETDISPLAYNAME_METHOD = mGetDisplayName;
56 AVAILABLE_CURRENCIES = Collections.unmodifiableSet(currencies);
59 public void TestCurrencySymbols() {
60 for (Currency currency : AVAILABLE_CURRENCIES) {
61 String currencyCode = currency.getCurrencyCode();
62 com.ibm.icu.util.Currency currencyIcu = com.ibm.icu.util.Currency.getInstance(currencyCode);
63 for (Locale loc : Locale.getAvailableLocales()) {
64 if (TestUtil.isProblematicIBMLocale(loc)) {
65 logln("Skipped " + loc);
69 String curSymbol = currency.getSymbol(loc);
70 String curSymbolIcu = currencyIcu.getSymbol(loc);
72 if (curSymbolIcu.equals(currencyCode)) {
74 if (!curSymbol.equals(currencyCode)) {
75 logln("INFO: JDK has currency symbol " + curSymbol + " for locale " +
76 loc + ", but ICU does not");
81 if (TestUtil.isICUExtendedLocale(loc)) {
82 if (!curSymbol.equals(curSymbolIcu)) {
83 errln("FAIL: Currency symbol for " + currencyCode + " by ICU is " + curSymbolIcu
84 + ", but got " + curSymbol + " in locale " + loc);
87 if (!curSymbol.equals(curSymbolIcu)) {
88 logln("INFO: Currency symbol for " + currencyCode + " by ICU is " + curSymbolIcu
89 + ", but " + curSymbol + " by JDK in locale " + loc);
91 // Try explicit ICU locale (xx_yy_ICU)
92 Locale locIcu = TestUtil.toICUExtendedLocale(loc);
93 curSymbol = currency.getSymbol(locIcu);
94 if (!curSymbol.equals(curSymbolIcu)) {
95 errln("FAIL: Currency symbol for " + currencyCode + " by ICU is " + curSymbolIcu
96 + ", but got " + curSymbol + " in locale " + locIcu);
103 public void TestCurrencyDisplayNames() {
104 if (GETDISPLAYNAME_METHOD == null) {
105 logln("INFO: Currency#getDisplayName(String,Locale) is not available.");
109 for (Currency currency : AVAILABLE_CURRENCIES) {
110 String currencyCode = currency.getCurrencyCode();
111 com.ibm.icu.util.Currency currencyIcu = com.ibm.icu.util.Currency.getInstance(currencyCode);
112 for (Locale loc : Locale.getAvailableLocales()) {
113 if (TestUtil.isProblematicIBMLocale(loc)) {
114 logln("Skipped " + loc);
118 String curName = null;
120 curName = (String)GETDISPLAYNAME_METHOD.invoke(currency, new Object[] {loc});
121 } catch (Exception e) {
122 errln("FAIL: JDK Currency#getDisplayName(\"" + currency + "\", \"" + loc + "\") throws exception: " + e.getMessage());
126 String curNameIcu = currencyIcu.getDisplayName(loc);
128 if (curNameIcu.equals(currencyCode)) {
130 if (!curName.equals(currencyCode)) {
131 logln("INFO: JDK has currency display name " + curName + " for locale " +
132 loc + ", but ICU does not");
137 if (TestUtil.isICUExtendedLocale(loc)) {
138 if (!curName.equals(curNameIcu)) {
139 errln("FAIL: Currency display name for " + currencyCode + " by ICU is " + curNameIcu
140 + ", but got " + curName + " in locale " + loc);
143 if (!curName.equals(curNameIcu)) {
144 logln("INFO: Currency display name for " + currencyCode + " by ICU is " + curNameIcu
145 + ", but " + curName + " by JDK in locale " + loc);
147 // Try explicit ICU locale (xx_yy_ICU)
148 Locale locIcu = TestUtil.toICUExtendedLocale(loc);
150 curName = (String)GETDISPLAYNAME_METHOD.invoke(currency, new Object[] {locIcu});
151 } catch (Exception e) {
152 errln("FAIL: JDK Currency#getDisplayName(\"" + currency + "\", \"" + locIcu + "\") throws exception: " + e.getMessage());
155 if (!curName.equals(curNameIcu)) {
156 errln("FAIL: Currency display name for " + currencyCode + " by ICU is " + curNameIcu
157 + ", but got " + curName + " in locale " + locIcu);