2 **********************************************************************
\r
3 * Copyright (c) 2002-2009, International Business Machines
\r
4 * Corporation and others. All Rights Reserved.
\r
5 **********************************************************************
\r
7 * Created: December 18 2002
\r
9 **********************************************************************
\r
12 package com.ibm.icu.dev.test.util;
\r
13 import java.util.Date;
\r
14 import java.util.HashSet;
\r
15 import java.util.Locale;
\r
17 import com.ibm.icu.dev.test.TestFmwk;
\r
18 import com.ibm.icu.impl.Utility;
\r
19 import com.ibm.icu.text.DecimalFormatSymbols;
\r
20 import com.ibm.icu.util.Currency;
\r
21 import com.ibm.icu.util.ULocale;
\r
25 * @summary General test of Currency
\r
27 public class CurrencyTest extends TestFmwk {
\r
29 public static void main(String[] args) throws Exception {
\r
30 new CurrencyTest().run(args);
\r
34 * Test of basic API.
\r
36 public void TestAPI() {
\r
37 Currency usd = Currency.getInstance("USD");
\r
38 /*int hash = */usd.hashCode();
\r
39 Currency jpy = Currency.getInstance("JPY");
\r
40 if (usd.equals(jpy)) {
\r
41 errln("FAIL: USD == JPY");
\r
43 if (usd.equals("abc")) {
\r
44 errln("FAIL: USD == (String)");
\r
46 if (usd.equals(null)) {
\r
47 errln("FAIL: USD == (null)");
\r
49 if (!usd.equals(usd)) {
\r
50 errln("FAIL: USD != USD");
\r
54 Currency nullCurrency = Currency.getInstance((String)null);
\r
55 errln("FAIL: Expected getInstance(null) to throw "
\r
56 + "a NullPointerException, but returned " + nullCurrency);
\r
57 } catch (NullPointerException npe) {
\r
58 logln("PASS: getInstance(null) threw a NullPointerException");
\r
62 Currency bogusCurrency = Currency.getInstance("BOGUS");
\r
63 errln("FAIL: Expected getInstance(\"BOGUS\") to throw "
\r
64 + "an IllegalArgumentException, but returned " + bogusCurrency);
\r
65 } catch (IllegalArgumentException iae) {
\r
66 logln("PASS: getInstance(\"BOGUS\") threw an IllegalArgumentException");
\r
69 Locale[] avail = Currency.getAvailableLocales();
\r
71 errln("FAIL: getAvailableLocales returned null");
\r
75 usd.getName(ULocale.US, 5, new boolean[1]);
\r
76 errln("expected getName with invalid type parameter to throw exception");
\r
78 catch (Exception e) {
\r
79 logln("PASS: getName failed as expected");
\r
84 * Test registration.
\r
86 public void TestRegistration() {
\r
87 final Currency jpy = Currency.getInstance("JPY");
\r
88 final Currency usd = Currency.getInstance(Locale.US);
\r
91 Currency.unregister(null); // should fail, coverage
\r
92 errln("expected unregister of null to throw exception");
\r
94 catch (Exception e) {
\r
95 logln("PASS: unregister of null failed as expected");
\r
98 if (Currency.unregister("")) { // coverage
\r
99 errln("unregister before register erroneously succeeded");
\r
102 ULocale fu_FU = new ULocale("fu_FU");
\r
104 Object key1 = Currency.registerInstance(jpy, ULocale.US);
\r
105 Object key2 = Currency.registerInstance(jpy, fu_FU);
\r
107 Currency nus = Currency.getInstance(Locale.US);
\r
108 if (!nus.equals(jpy)) {
\r
109 errln("expected " + jpy + " but got: " + nus);
\r
112 // converage, make sure default factory works
\r
113 Currency nus1 = Currency.getInstance(Locale.JAPAN);
\r
114 if (!nus1.equals(jpy)) {
\r
115 errln("expected " + jpy + " but got: " + nus1);
\r
118 ULocale[] locales = Currency.getAvailableULocales();
\r
119 boolean found = false;
\r
120 for (int i = 0; i < locales.length; ++i) {
\r
121 if (locales[i].equals(fu_FU)) {
\r
127 errln("did not find locale" + fu_FU + " in currency locales");
\r
130 if (!Currency.unregister(key1)) {
\r
131 errln("unable to unregister currency using key1");
\r
133 if (!Currency.unregister(key2)) {
\r
134 errln("unable to unregister currency using key2");
\r
137 Currency nus2 = Currency.getInstance(Locale.US);
\r
138 if (!nus2.equals(usd)) {
\r
139 errln("expected " + usd + " but got: " + nus2);
\r
142 locales = Currency.getAvailableULocales();
\r
144 for (int i = 0; i < locales.length; ++i) {
\r
145 if (locales[i].equals(fu_FU)) {
\r
151 errln("found locale" + fu_FU + " in currency locales after unregister");
\r
158 public void TestNames() {
\r
159 // Do a basic check of getName()
\r
160 // USD { "US$", "US Dollar" } // 04/04/1792-
\r
161 ULocale en = ULocale.ENGLISH;
\r
162 boolean[] isChoiceFormat = new boolean[1];
\r
163 Currency usd = Currency.getInstance("USD");
\r
164 // Warning: HARD-CODED LOCALE DATA in this test. If it fails, CHECK
\r
165 // THE LOCALE DATA before diving into the code.
\r
167 assertEquals("USD.getName(SYMBOL_NAME)",
\r
169 usd.getName(en, Currency.SYMBOL_NAME, isChoiceFormat));
\r
170 assertEquals("USD.getName(LONG_NAME)",
\r
172 usd.getName(en, Currency.LONG_NAME, isChoiceFormat));
\r
174 // TODO add more tests later
\r
177 public void TestCoverage() {
\r
178 Currency usd = Currency.getInstance("USD");
\r
180 assertEquals("USD.getSymbol()",
\r
184 assertEquals("USD.getLocale()",
\r
186 usd.getLocale(null));
\r
189 public void TestCurrencyKeyword() {
\r
190 ULocale locale = new ULocale("th_TH@collation=traditional;currency=QQQ");
\r
191 Currency currency = Currency.getInstance(locale);
\r
192 String result = currency.getCurrencyCode();
\r
193 if (!"QQQ".equals(result)) {
\r
194 errln("got unexpected currency: " + result);
\r
198 public void TestAvailableCurrencyCodes()
\r
203 // Cycle through historical currencies
\r
204 currency = Currency.getAvailableCurrencyCodes(new ULocale("eo_AM"), new Date(-630720000000L)); // pre 1961
\r
205 if (currency != null)
\r
207 errln("FAIL: didn't return null for eo_AM");
\r
210 currency = Currency.getAvailableCurrencyCodes(new ULocale("eo_AM"), new Date(0L)); // 1970
\r
211 if (currency.length != 1)
\r
213 errln("FAIL: didn't return 1 for eo_AM returned: " + currency.length);
\r
215 if (!"SUR".equals(currency[0]))
\r
217 errln("didn't return SUR for eo_AM returned: " + currency[0]);
\r
220 currency = Currency.getAvailableCurrencyCodes(new ULocale("eo_AM"), new Date(693792000000L)); // 1992
\r
221 if (currency.length != 1)
\r
223 errln("FAIL: didn't return 1 for eo_AM returned: " + currency.length);
\r
225 if (!"RUR".equals(currency[0]))
\r
227 errln("didn't return RUR for eo_AM returned: " + currency[0]);
\r
230 currency = Currency.getAvailableCurrencyCodes(new ULocale("eo_AM"), new Date(977616000000L)); // post 1993
\r
231 if (currency.length != 1)
\r
233 errln("FAIL: didn't return 1 for eo_AM returned: " + currency.length);
\r
235 if (!"AMD".equals(currency[0]))
\r
237 errln("didn't return AMD for eo_AM returned: " + currency[0]);
\r
240 // Locale AD has multiple currencies at once
\r
241 currency = Currency.getAvailableCurrencyCodes(new ULocale("eo_AD"), new Date(977616000000L)); // 2001
\r
242 if (currency.length != 4)
\r
244 errln("FAIL: didn't return 4 for eo_AD returned: " + currency.length);
\r
246 if (!"EUR".equals(currency[0]))
\r
248 errln("didn't return EUR for eo_AD returned: " + currency[0]);
\r
250 if (!"ESP".equals(currency[1]))
\r
252 errln("didn't return ESP for eo_AD returned: " + currency[1]);
\r
254 if (!"FRF".equals(currency[2]))
\r
256 errln("didn't return FRF for eo_AD returned: " + currency[2]);
\r
258 if (!"ADP".equals(currency[3]))
\r
260 errln("didn't return ADP for eo_AD returned: " + currency[3]);
\r
263 currency = Currency.getAvailableCurrencyCodes(new ULocale("eo_AD"), new Date(0L)); // 1970
\r
264 if (currency.length != 3)
\r
266 errln("FAIL: didn't return 3 for eo_AD returned: " + currency.length);
\r
268 if (!"ESP".equals(currency[0]))
\r
270 errln("didn't return ESP for eo_AD returned: " + currency[0]);
\r
272 if (!"FRF".equals(currency[1]))
\r
274 errln("didn't return FRF for eo_AD returned: " + currency[1]);
\r
276 if (!"ADP".equals(currency[2]))
\r
278 errln("didn't return ADP for eo_AD returned: " + currency[2]);
\r
281 currency = Currency.getAvailableCurrencyCodes(new ULocale("eo_AD"), new Date(-630720000000L)); // 1950
\r
282 if (currency.length != 2)
\r
284 errln("FAIL: didn't return 2 for eo_AD returned: " + currency.length);
\r
286 if (!"ESP".equals(currency[0]))
\r
288 errln("didn't return ESP for eo_AD returned: " + currency[0]);
\r
290 if (!"ADP".equals(currency[1]))
\r
292 errln("didn't return ADP for eo_AD returned: " + currency[1]);
\r
295 currency = Currency.getAvailableCurrencyCodes(new ULocale("eo_AD"), new Date(-2207520000000L)); // 1900
\r
296 if (currency.length != 1)
\r
298 errln("FAIL: didn't return 1 for eo_AD returned: " + currency.length);
\r
300 if (!"ESP".equals(currency[0]))
\r
302 errln("didn't return ESP for eo_AD returned: " + currency[0]);
\r
305 // Locale UA has gap between years 1994 - 1996
\r
306 currency = Currency.getAvailableCurrencyCodes(new ULocale("eo_UA"), new Date(788400000000L));
\r
307 if (currency != null)
\r
309 errln("FAIL: didn't return null for eo_UA");
\r
312 // Test for bogus locale
\r
313 currency = Currency.getAvailableCurrencyCodes(new ULocale("eo_QQ"), new Date(0L));
\r
314 if (currency != null)
\r
316 errln("FAIL: didn't return null for eo_QQ");
\r
319 // Cycle through historical currencies
\r
320 currency = Currency.getAvailableCurrencyCodes(new ULocale("eo_AO"), new Date(977616000000L)); // 2001
\r
321 if (currency.length != 1)
\r
323 errln("FAIL: didn't return 1 for eo_AO returned: " + currency.length);
\r
325 if (!"AOA".equals(currency[0]))
\r
327 errln("didn't return AOA for eo_AO returned: " + currency[0]);
\r
330 currency = Currency.getAvailableCurrencyCodes(new ULocale("eo_AO"), new Date(819936000000L)); // 1996
\r
331 if (currency.length != 2)
\r
333 errln("FAIL: didn't return 2 for eo_AO returned: " + currency.length);
\r
335 if (!"AOR".equals(currency[0]))
\r
337 errln("didn't return AOR for eo_AO returned: " + currency[0]);
\r
339 if (!"AON".equals(currency[1]))
\r
341 errln("didn't return AON for eo_AO returned: " + currency[1]);
\r
344 currency = Currency.getAvailableCurrencyCodes(new ULocale("eo_AO"), new Date(662256000000L)); //
\r
345 if (currency.length != 2)
\r
347 errln("FAIL: didn't return 2 for eo_AO returned: " + currency.length);
\r
349 if (!"AON".equals(currency[0]))
\r
351 errln("didn't return AON for eo_AO returned: " + currency[0]);
\r
353 if (!"AOK".equals(currency[1]))
\r
355 errln("didn't return AOK for eo_AO returned: " + currency[1]);
\r
358 currency = Currency.getAvailableCurrencyCodes(new ULocale("eo_AO"), new Date(315360000000L)); // 1980
\r
359 if (currency.length != 1)
\r
361 errln("FAIL: didn't return 1 for eo_AO returned: " + currency.length);
\r
363 if (!"AOK".equals(currency[0]))
\r
365 errln("didn't return AOK for eo_AO returned: " + currency[0]);
\r
368 currency = Currency.getAvailableCurrencyCodes(new ULocale("eo_AO"), new Date(0L)); // 1970
\r
369 if (currency != null)
\r
371 errln("FAIL: didn't return null for eo_AO");
\r
374 // Test with currency keyword override
\r
375 currency = Currency.getAvailableCurrencyCodes(new ULocale("eo_DE@currency=DEM"), new Date(977616000000L)); // 2001
\r
376 if (currency.length != 2)
\r
378 errln("FAIL: didn't return 2 for eo_DE@currency=DEM returned: " + currency.length);
\r
380 if (!"EUR".equals(currency[0]))
\r
382 errln("didn't return EUR for eo_DE@currency=DEM returned: " + currency[0]);
\r
384 if (!"DEM".equals(currency[1]))
\r
386 errln("didn't return DEM for eo_DE@currency=DEM returned: " + currency[1]);
\r
389 // Test Euro Support
\r
390 currency = Currency.getAvailableCurrencyCodes(new ULocale("en_US"), new Date(System.currentTimeMillis()));
\r
391 if (!"USD".equals(currency[0]))
\r
393 errln("didn't return USD for en_US returned: " + currency[0]);
\r
396 currency = Currency.getAvailableCurrencyCodes(new ULocale("en_US_PREEURO"), new Date(System.currentTimeMillis()));
\r
397 if (!"USD".equals(currency[0]))
\r
399 errln("didn't return USD for en_US_PREEURO returned: " + currency[0]);
\r
402 currency = Currency.getAvailableCurrencyCodes(new ULocale("en_US_Q"), new Date(System.currentTimeMillis()));
\r
403 if (!"USD".equals(currency[0]))
\r
405 errln("didn't return USD for en_US_Q returned: " + currency[0]);
\r
410 public void TestDeprecatedCurrencyFormat() {
\r
412 Locale locale = new Locale("sr", "QQ");
\r
413 DecimalFormatSymbols icuSymbols = new
\r
414 com.ibm.icu.text.DecimalFormatSymbols(locale);
\r
415 String symbol = icuSymbols.getCurrencySymbol();
\r
416 Currency currency = icuSymbols.getCurrency();
\r
417 String expectCur = null;
\r
418 String expectSym = "\u00A4";
\r
419 if(!symbol.toString().equals(expectSym) || currency != null) {
\r
420 errln("for " + locale + " expected " + expectSym+"/"+expectCur + " but got " + symbol+"/"+currency);
\r
422 logln("for " + locale + " expected " + expectSym+"/"+expectCur + " and got " + symbol+"/"+currency);
\r
426 public void TestGetKeywordValues(){
\r
428 final String[][] PREFERRED = {
\r
438 {"ar_PS", "JOD", "ILS"},
\r
439 {"en@currency=CAD", "USD"},
\r
440 {"fr@currency=ZZZ", "EUR"},
\r
441 {"de_DE@currency=DEM", "EUR"},
\r
444 String[] ALL = Currency.getKeywordValuesForLocale("currency", ULocale.getDefault(), false);
\r
445 HashSet ALLSET = new HashSet();
\r
446 for (int i = 0; i < ALL.length; i++) {
\r
447 ALLSET.add(ALL[i]);
\r
450 for (int i = 0; i < PREFERRED.length; i++) {
\r
451 ULocale loc = new ULocale(PREFERRED[i][0]);
\r
452 String[] expected = new String[PREFERRED[i].length - 1];
\r
453 System.arraycopy(PREFERRED[i], 1, expected, 0, expected.length);
\r
455 String[] pref = Currency.getKeywordValuesForLocale("currency", loc, true);
\r
456 boolean matchPref = false;
\r
457 if (pref.length == expected.length) {
\r
459 for (int j = 0; j < pref.length; j++) {
\r
460 if (!pref[j].equals(expected[j])) {
\r
466 errln("FAIL: Preferred values for locale " + loc
\r
467 + " got:" + Utility.arrayToString(pref) + " expected:" + Utility.arrayToString(expected));
\r
470 String[] all = Currency.getKeywordValuesForLocale("currency", loc, false);
\r
471 boolean matchAll = false;
\r
472 if (all.length == ALLSET.size()) {
\r
474 for (int j = 0; j < all.length; j++) {
\r
475 if (!ALLSET.contains(all[j])) {
\r
482 errln("FAIL: All values for locale " + loc
\r
483 + " got:" + Utility.arrayToString(all));
\r