]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-52_1/main/tests/localespi/src/com/ibm/icu/dev/test/localespi/CurrencyNameTest.java
Clean up imports.
[Dictionary.git] / jars / icu4j-52_1 / main / tests / localespi / src / com / ibm / icu / dev / test / localespi / CurrencyNameTest.java
1 /*
2  *******************************************************************************
3  * Copyright (C) 2008-2012, International Business Machines Corporation and    *
4  * others. All Rights Reserved.                                                *
5  *******************************************************************************
6  */
7 package com.ibm.icu.dev.test.localespi;
8
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;
14 import java.util.Set;
15
16 import com.ibm.icu.dev.test.TestFmwk;
17
18 @SuppressWarnings("unchecked")
19 public class CurrencyNameTest extends TestFmwk {
20     public static void main(String[] args) throws Exception {
21         new CurrencyNameTest().run(args);
22     }
23
24     private static final Set<Currency> AVAILABLE_CURRENCIES;
25     private static final Method GETDISPLAYNAME_METHOD;
26
27     static {
28         Method mGetDisplayName = null;
29         Set<Currency> currencies = null;
30         try {
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) {
35             // fall through
36         }
37
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) {
43                     continue;
44                 }
45                 try {
46                     Currency currency = Currency.getInstance(l);
47                     if (currency != null) {
48                         currencies.add(currency);
49                     }
50                 } catch (IllegalArgumentException iae) {
51                     // ignore
52                 }
53             }
54         }
55         GETDISPLAYNAME_METHOD = mGetDisplayName;
56         AVAILABLE_CURRENCIES = Collections.unmodifiableSet(currencies);
57     }
58
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);
66                     continue;
67                 }
68
69                 String curSymbol = currency.getSymbol(loc);
70                 String curSymbolIcu = currencyIcu.getSymbol(loc);
71
72                 if (curSymbolIcu.equals(currencyCode)) {
73                     // No data in ICU
74                     if (!curSymbol.equals(currencyCode)) {
75                         logln("INFO: JDK has currency symbol " + curSymbol + " for locale " +
76                                 loc + ", but ICU does not");
77                     }
78                     continue;
79                 }
80
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);
85                     }
86                 } else {
87                     if (!curSymbol.equals(curSymbolIcu)) {
88                         logln("INFO: Currency symbol for " + currencyCode +  " by ICU is " + curSymbolIcu
89                                 + ", but " + curSymbol + " by JDK in locale " + loc);
90                     }
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);
97                     }
98                 }
99             }
100         }
101     }
102
103     public void TestCurrencyDisplayNames() {
104         if (GETDISPLAYNAME_METHOD == null) {
105             logln("INFO: Currency#getDisplayName(String,Locale) is not available.");
106             return;
107         }
108
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);
115                     continue;
116                 }
117
118                 String curName = null;
119                 try {
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());
123                     continue;
124                 }
125
126                 String curNameIcu = currencyIcu.getDisplayName(loc);
127
128                 if (curNameIcu.equals(currencyCode)) {
129                     // No data in ICU
130                     if (!curName.equals(currencyCode)) {
131                         logln("INFO: JDK has currency display name " + curName + " for locale " +
132                                 loc + ", but ICU does not");
133                     }
134                     continue;
135                 }
136
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);
141                     }
142                 } else {
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);
146                     }
147                     // Try explicit ICU locale (xx_yy_ICU)
148                     Locale locIcu = TestUtil.toICUExtendedLocale(loc);
149                     try {
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());
153                         continue;
154                     }
155                     if (!curName.equals(curNameIcu)) {
156                         errln("FAIL: Currency display name for " + currencyCode + " by ICU is " + curNameIcu
157                                 + ", but got " + curName + " in locale " + locIcu);
158                     }
159                 }
160             }
161         }
162     }
163
164 }