2 **********************************************************************
3 * Copyright (c) 2002-2011, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 **********************************************************************
7 * Created: December 18 2002
9 **********************************************************************
12 package com.ibm.icu.dev.test.util;
14 import java.text.DateFormat;
15 import java.text.SimpleDateFormat;
16 import java.util.Arrays;
17 import java.util.Date;
18 import java.util.HashSet;
19 import java.util.Locale;
22 import com.ibm.icu.dev.test.TestFmwk;
23 import com.ibm.icu.impl.CurrencyData;
24 import com.ibm.icu.text.CurrencyDisplayNames;
25 import com.ibm.icu.text.CurrencyMetaInfo;
26 import com.ibm.icu.text.DecimalFormatSymbols;
27 import com.ibm.icu.util.Currency;
28 import com.ibm.icu.util.ULocale;
32 * @summary General test of Currency
34 public class CurrencyTest extends TestFmwk {
36 public static void main(String[] args) throws Exception {
37 new CurrencyTest().run(args);
43 public void TestAPI() {
44 Currency usd = Currency.getInstance("USD");
45 /*int hash = */usd.hashCode();
46 Currency jpy = Currency.getInstance("JPY");
47 if (usd.equals(jpy)) {
48 errln("FAIL: USD == JPY");
50 if (usd.equals("abc")) {
51 errln("FAIL: USD == (String)");
53 if (usd.equals(null)) {
54 errln("FAIL: USD == (null)");
56 if (!usd.equals(usd)) {
57 errln("FAIL: USD != USD");
61 Currency nullCurrency = Currency.getInstance((String)null);
62 errln("FAIL: Expected getInstance(null) to throw "
63 + "a NullPointerException, but returned " + nullCurrency);
64 } catch (NullPointerException npe) {
65 logln("PASS: getInstance(null) threw a NullPointerException");
69 Currency bogusCurrency = Currency.getInstance("BOGUS");
70 errln("FAIL: Expected getInstance(\"BOGUS\") to throw "
71 + "an IllegalArgumentException, but returned " + bogusCurrency);
72 } catch (IllegalArgumentException iae) {
73 logln("PASS: getInstance(\"BOGUS\") threw an IllegalArgumentException");
76 Locale[] avail = Currency.getAvailableLocales();
78 errln("FAIL: getAvailableLocales returned null");
82 usd.getName(ULocale.US, 5, new boolean[1]);
83 errln("expected getName with invalid type parameter to throw exception");
86 logln("PASS: getName failed as expected");
93 public void TestRegistration() {
94 final Currency jpy = Currency.getInstance("JPY");
95 final Currency usd = Currency.getInstance(Locale.US);
98 Currency.unregister(null); // should fail, coverage
99 errln("expected unregister of null to throw exception");
101 catch (Exception e) {
102 logln("PASS: unregister of null failed as expected");
105 if (Currency.unregister("")) { // coverage
106 errln("unregister before register erroneously succeeded");
109 ULocale fu_FU = new ULocale("fu_FU");
111 Object key1 = Currency.registerInstance(jpy, ULocale.US);
112 Object key2 = Currency.registerInstance(jpy, fu_FU);
114 Currency nus = Currency.getInstance(Locale.US);
115 if (!nus.equals(jpy)) {
116 errln("expected " + jpy + " but got: " + nus);
119 // converage, make sure default factory works
120 Currency nus1 = Currency.getInstance(Locale.JAPAN);
121 if (!nus1.equals(jpy)) {
122 errln("expected " + jpy + " but got: " + nus1);
125 ULocale[] locales = Currency.getAvailableULocales();
126 boolean found = false;
127 for (int i = 0; i < locales.length; ++i) {
128 if (locales[i].equals(fu_FU)) {
134 errln("did not find locale" + fu_FU + " in currency locales");
137 if (!Currency.unregister(key1)) {
138 errln("unable to unregister currency using key1");
140 if (!Currency.unregister(key2)) {
141 errln("unable to unregister currency using key2");
144 Currency nus2 = Currency.getInstance(Locale.US);
145 if (!nus2.equals(usd)) {
146 errln("expected " + usd + " but got: " + nus2);
149 locales = Currency.getAvailableULocales();
151 for (int i = 0; i < locales.length; ++i) {
152 if (locales[i].equals(fu_FU)) {
158 errln("found locale" + fu_FU + " in currency locales after unregister");
161 Locale[] locs = Currency.getAvailableLocales();
163 for (int i = 0; i < locs.length; ++i) {
164 if (locs[i].equals(fu_FU)) {
170 errln("found locale" + fu_FU + " in currency locales after unregister");
177 public void TestNames() {
178 // Do a basic check of getName()
179 // USD { "US$", "US Dollar" } // 04/04/1792-
180 ULocale en = ULocale.ENGLISH;
181 boolean[] isChoiceFormat = new boolean[1];
182 Currency usd = Currency.getInstance("USD");
183 // Warning: HARD-CODED LOCALE DATA in this test. If it fails, CHECK
184 // THE LOCALE DATA before diving into the code.
186 assertEquals("USD.getName(SYMBOL_NAME)",
188 usd.getName(en, Currency.SYMBOL_NAME, isChoiceFormat));
189 assertEquals("USD.getName(LONG_NAME)",
191 usd.getName(en, Currency.LONG_NAME, isChoiceFormat));
193 // TODO add more tests later
196 public void TestCoverage() {
197 Currency usd = Currency.getInstance("USD");
199 assertEquals("USD.getSymbol()",
205 // Provide better code coverage for the CurrencyDisplayNames class
206 public void TestCurrencyDisplayNames() {
207 if (!CurrencyDisplayNames.hasData()) {
208 errln("hasData() should return true.");
212 // Provide better code coverage for the CurrencyData class
213 public void TestCurrencyData() {
214 CurrencyData.DefaultInfo info_fallback = (CurrencyData.DefaultInfo)CurrencyData.DefaultInfo.getWithFallback(true);
215 if (info_fallback == null) {
216 errln("getWithFallback() returned null.");
220 CurrencyData.DefaultInfo info_nofallback = (CurrencyData.DefaultInfo)CurrencyData.DefaultInfo.getWithFallback(false);
221 if (info_nofallback == null) {
222 errln("getWithFallback() returned null.");
226 if (!info_fallback.getName("isoCode").equals("isoCode") || info_nofallback.getName("isoCode") != null) {
227 errln("Error calling getName().");
231 if (!info_fallback.getPluralName("isoCode", "type").equals("isoCode") || info_nofallback.getPluralName("isoCode", "type") != null) {
232 errln("Error calling getPluralName().");
236 if (!info_fallback.getSymbol("isoCode").equals("isoCode") || info_nofallback.getSymbol("isoCode") != null) {
237 errln("Error calling getSymbol().");
241 if (!info_fallback.symbolMap().isEmpty()) {
242 errln("symbolMap() should return empty map.");
246 if (!info_fallback.nameMap().isEmpty()) {
247 errln("nameMap() should return empty map.");
251 if (!info_fallback.getUnitPatterns().isEmpty() || info_nofallback.getUnitPatterns() != null) {
252 errln("Error calling getUnitPatterns().");
256 if (!info_fallback.getSpacingInfo().equals((CurrencyData.CurrencySpacingInfo.DEFAULT)) ||
257 info_nofallback.getSpacingInfo() != null) {
258 errln("Error calling getSpacingInfo().");
262 if (info_fallback.getLocale() != ULocale.ROOT) {
263 errln("Error calling getLocale().");
267 if (info_fallback.getFormatInfo("isoCode") != null) {
268 errln("Error calling getFormatInfo().");
273 // Provide better code coverage for the CurrencyMetaInfo class
274 public void TestCurrencyMetaInfo() {
275 CurrencyMetaInfo metainfo = CurrencyMetaInfo.getInstance();
276 if (metainfo == null) {
277 errln("Unable to get CurrencyMetaInfo instance.");
281 if (!CurrencyMetaInfo.hasData()) {
282 errln("hasData() should note return false.");
286 CurrencyMetaInfo.CurrencyFilter filter;
287 CurrencyMetaInfo.CurrencyInfo info;
288 CurrencyMetaInfo.CurrencyDigits digits;
291 filter = CurrencyMetaInfo.CurrencyFilter.onCurrency("currency");
292 CurrencyMetaInfo.CurrencyFilter filter2 = CurrencyMetaInfo.CurrencyFilter.onCurrency("test");
293 if (filter == null) {
294 errln("Unable to create CurrencyFilter.");
298 if (filter.equals(new Object())) {
299 errln("filter should not equal to Object");
303 if (filter.equals(filter2)) {
304 errln("filter should not equal filter2");
308 if (filter.hashCode() == 0) {
309 errln("Error getting filter hashcode");
313 if (filter.toString() == null) {
314 errln("Error calling toString()");
320 info = new CurrencyMetaInfo.CurrencyInfo("region", "code", 0, 1, 1);
322 errln("Error creating CurrencyInfo.");
326 if (info.toString() == null) {
327 errln("Error calling toString()");
333 digits = metainfo.currencyDigits("isoCode");
334 if (digits == null) {
335 errln("Unable to get CurrencyDigits.");
339 if (digits.toString() == null) {
340 errln("Error calling toString()");
346 public void TestCurrencyKeyword() {
347 ULocale locale = new ULocale("th_TH@collation=traditional;currency=QQQ");
348 Currency currency = Currency.getInstance(locale);
349 String result = currency.getCurrencyCode();
350 if (!"QQQ".equals(result)) {
351 errln("got unexpected currency: " + result);
355 public void TestAvailableCurrencyCodes() {
357 { "eo_AM", "1950-01-05" },
358 { "eo_AM", "1969-12-31", "SUR" },
359 { "eo_AM", "1991-12-26", "RUR" },
360 { "eo_AM", "2000-12-23", "AMD" },
361 { "eo_AD", "2000-12-23", "EUR", "ESP", "FRF", "ADP" },
362 { "eo_AD", "1969-12-31", "ESP", "FRF", "ADP" },
363 { "eo_AD", "1950-01-05", "ESP", "ADP" },
364 { "eo_AD", "1900-01-17", "ESP" },
365 { "eo_UA", "1994-12-25" },
366 { "eo_QQ", "1969-12-31" },
367 { "eo_AO", "2000-12-23", "AOA" },
368 { "eo_AO", "1995-12-25", "AOR", "AON" },
369 { "eo_AO", "1990-12-26", "AON", "AOK" },
370 { "eo_AO", "1979-12-29", "AOK" },
371 { "eo_AO", "1969-12-31" },
372 { "eo_DE@currency=DEM", "2000-12-23", "EUR", "DEM" },
373 { "eo-DE-u-cu-dem", "2000-12-23", "EUR", "DEM" },
374 { "en_US", null, "USD" },
375 { "en_US_PREEURO", null, "USD" },
376 { "en_US_Q", null, "USD" },
379 DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
380 for (String[] test : tests) {
381 ULocale locale = new ULocale(test[0]);
382 String timeString = test[1];
384 if (timeString == null) {
386 timeString = "today";
389 date = fmt.parse(timeString);
390 } catch (Exception e) {
391 fail("could not parse date: " + timeString);
395 String[] expected = null;
396 if (test.length > 2) {
397 expected = new String[test.length - 2];
398 System.arraycopy(test, 2, expected, 0, expected.length);
400 String[] actual = Currency.getAvailableCurrencyCodes(locale, date);
402 // Order is not important as of 4.4. We never documented that it was.
403 Set<String> expectedSet = new HashSet<String>();
404 if (expected != null) {
405 expectedSet.addAll(Arrays.asList(expected));
407 Set<String> actualSet = new HashSet<String>();
408 if (actual != null) {
409 actualSet.addAll(Arrays.asList(actual));
411 assertEquals(locale + " on " + timeString, expectedSet, actualSet);
415 public void TestDeprecatedCurrencyFormat() {
417 Locale locale = new Locale("sr", "QQ");
418 DecimalFormatSymbols icuSymbols = new
419 com.ibm.icu.text.DecimalFormatSymbols(locale);
420 String symbol = icuSymbols.getCurrencySymbol();
421 Currency currency = icuSymbols.getCurrency();
422 String expectCur = null;
423 String expectSym = "\u00A4";
424 if(!symbol.toString().equals(expectSym) || currency != null) {
425 errln("for " + locale + " expected " + expectSym+"/"+expectCur + " but got " + symbol+"/"+currency);
427 logln("for " + locale + " expected " + expectSym+"/"+expectCur + " and got " + symbol+"/"+currency);
431 public void TestGetKeywordValues(){
433 final String[][] PREFERRED = {
444 {"ar_PS", "JOD", "ILS"},
445 {"en@currency=CAD", "USD"},
446 {"fr@currency=ZZZ", "EUR"},
447 {"de_DE@currency=DEM", "EUR"},
450 String[] ALL = Currency.getKeywordValuesForLocale("currency", ULocale.getDefault(), false);
451 HashSet ALLSET = new HashSet();
452 for (int i = 0; i < ALL.length; i++) {
456 for (int i = 0; i < PREFERRED.length; i++) {
457 ULocale loc = new ULocale(PREFERRED[i][0]);
458 String[] expected = new String[PREFERRED[i].length - 1];
459 System.arraycopy(PREFERRED[i], 1, expected, 0, expected.length);
460 String[] pref = Currency.getKeywordValuesForLocale("currency", loc, true);
461 assertEquals(loc.toString(), expected, pref);
463 String[] all = Currency.getKeywordValuesForLocale("currency", loc, false);
464 // The items in the two collections should match (ignore order,
465 // behavior change from 4.3.3)
466 Set<String> returnedSet = new HashSet<String>();
467 returnedSet.addAll(Arrays.asList(all));
468 assertEquals(loc.toString(), ALLSET, returnedSet);
472 public void TestIsAvailable() {
473 Date d1995 = new Date(788918400000L); // 1995-01-01 00:00 GMT
474 Date d2000 = new Date(946684800000L); // 2000-01-01 00:00 GMT
475 Date d2005 = new Date(1104537600000L); // 2005-01-01 00:00 GMT
477 assertTrue("USD all time", Currency.isAvailable("USD", null, null));
478 assertTrue("USD before 1995", Currency.isAvailable("USD", null, d1995));
479 assertTrue("USD 1995-2005", Currency.isAvailable("USD", d1995, d2005));
480 assertTrue("USD after 2005", Currency.isAvailable("USD", d2005, null));
481 assertTrue("USD on 2005-01-01", Currency.isAvailable("USD", d2005, d2005));
483 assertTrue("usd all time", Currency.isAvailable("usd", null, null));
485 assertTrue("DEM all time", Currency.isAvailable("DEM", null, null));
486 assertTrue("DEM before 1995", Currency.isAvailable("DEM", null, d1995));
487 assertTrue("DEM 1995-2000", Currency.isAvailable("DEM", d1995, d2000));
488 assertTrue("DEM 1995-2005", Currency.isAvailable("DEM", d1995, d2005));
489 assertFalse("DEM after 2005", Currency.isAvailable("DEM", d2005, null));
490 assertTrue("DEM on 2000-01-01", Currency.isAvailable("DEM", d2000, d2000));
491 assertFalse("DEM on 2005-01-01", Currency.isAvailable("DEM", d2005, d2005));
493 assertFalse("XXX unknown code", Currency.isAvailable("XXX", null, null));
495 assertFalse("USDOLLAR invalid code", Currency.isAvailable("USDOLLAR", null, null));
497 // illegal argument combination
499 Currency.isAvailable("USD", d2005, d1995);
500 errln("Expected IllegalArgumentException, because lower range is after upper range");
501 } catch (IllegalArgumentException e) {
502 logln("IllegalArgumentException, because lower range is after upper range");