]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-52_1/main/tests/core/src/com/ibm/icu/dev/test/util/CurrencyTest.java
Added flags.
[Dictionary.git] / jars / icu4j-52_1 / main / tests / core / src / com / ibm / icu / dev / test / util / CurrencyTest.java
1 /*
2  **********************************************************************
3  * Copyright (c) 2002-2013, International Business Machines
4  * Corporation and others.  All Rights Reserved.
5  **********************************************************************
6  * Author: Alan Liu
7  * Created: December 18 2002
8  * Since: ICU 2.4
9  **********************************************************************
10  */
11
12 package com.ibm.icu.dev.test.util;
13
14 import java.util.Arrays;
15 import java.util.Date;
16 import java.util.HashSet;
17 import java.util.List;
18 import java.util.Locale;
19 import java.util.Set;
20
21 import com.ibm.icu.dev.test.TestFmwk;
22 import com.ibm.icu.impl.CurrencyData;
23 import com.ibm.icu.text.CurrencyDisplayNames;
24 import com.ibm.icu.text.CurrencyMetaInfo;
25 import com.ibm.icu.text.CurrencyMetaInfo.CurrencyFilter;
26 import com.ibm.icu.text.CurrencyMetaInfo.CurrencyInfo;
27 import com.ibm.icu.text.DateFormat;
28 import com.ibm.icu.text.DecimalFormatSymbols;
29 import com.ibm.icu.text.SimpleDateFormat;
30 import com.ibm.icu.util.Currency;
31 import com.ibm.icu.util.GregorianCalendar;
32 import com.ibm.icu.util.TimeZone;
33 import com.ibm.icu.util.ULocale;
34
35 /**
36  * @test
37  * @summary General test of Currency
38  */
39 public class CurrencyTest extends TestFmwk {
40
41     public static void main(String[] args) throws Exception {
42         new CurrencyTest().run(args);
43     }
44
45     /**
46      * Test of basic API.
47      */
48     public void TestAPI() {
49         Currency usd = Currency.getInstance("USD");
50         /*int hash = */usd.hashCode();
51         Currency jpy = Currency.getInstance("JPY");
52         if (usd.equals(jpy)) {
53             errln("FAIL: USD == JPY");
54         }
55         if (usd.equals("abc")) {
56             errln("FAIL: USD == (String)");
57         }
58         if (usd.equals(null)) {
59             errln("FAIL: USD == (null)");
60         }
61         if (!usd.equals(usd)) {
62             errln("FAIL: USD != USD");
63         }
64
65         try {
66             Currency nullCurrency = Currency.getInstance((String)null);
67             errln("FAIL: Expected getInstance(null) to throw "
68                     + "a NullPointerException, but returned " + nullCurrency);
69         } catch (NullPointerException npe) {
70             logln("PASS: getInstance(null) threw a NullPointerException");
71         }
72
73         try {
74             Currency bogusCurrency = Currency.getInstance("BOGUS");
75             errln("FAIL: Expected getInstance(\"BOGUS\") to throw "
76                     + "an IllegalArgumentException, but returned " + bogusCurrency);
77         } catch (IllegalArgumentException iae) {
78             logln("PASS: getInstance(\"BOGUS\") threw an IllegalArgumentException");
79         }
80
81         Locale[] avail = Currency.getAvailableLocales();
82         if(avail==null){
83             errln("FAIL: getAvailableLocales returned null");
84         }
85
86         try {
87             usd.getName(ULocale.US, 5, new boolean[1]);
88             errln("expected getName with invalid type parameter to throw exception");
89         }
90         catch (Exception e) {
91             logln("PASS: getName failed as expected");
92         }
93     }
94
95     /**
96      * Test registration.
97      */
98     public void TestRegistration() {
99         final Currency jpy = Currency.getInstance("JPY");
100         final Currency usd = Currency.getInstance(Locale.US);
101
102     try {
103       Currency.unregister(null); // should fail, coverage
104       errln("expected unregister of null to throw exception");
105     }
106     catch (Exception e) {
107         logln("PASS: unregister of null failed as expected");
108     }
109
110     if (Currency.unregister("")) { // coverage
111       errln("unregister before register erroneously succeeded");
112     }
113
114         ULocale fu_FU = new ULocale("fu_FU");
115
116         Object key1 = Currency.registerInstance(jpy, ULocale.US);
117         Object key2 = Currency.registerInstance(jpy, fu_FU);
118
119         Currency nus = Currency.getInstance(Locale.US);
120         if (!nus.equals(jpy)) {
121             errln("expected " + jpy + " but got: " + nus);
122         }
123
124         // converage, make sure default factory works
125         Currency nus1 = Currency.getInstance(Locale.JAPAN);
126         if (!nus1.equals(jpy)) {
127             errln("expected " + jpy + " but got: " + nus1);
128         }
129
130         ULocale[] locales = Currency.getAvailableULocales();
131         boolean found = false;
132         for (int i = 0; i < locales.length; ++i) {
133             if (locales[i].equals(fu_FU)) {
134                 found = true;
135                 break;
136             }
137         }
138         if (!found) {
139             errln("did not find locale" + fu_FU + " in currency locales");
140         }
141
142         if (!Currency.unregister(key1)) {
143             errln("unable to unregister currency using key1");
144         }
145         if (!Currency.unregister(key2)) {
146             errln("unable to unregister currency using key2");
147         }
148
149         Currency nus2 = Currency.getInstance(Locale.US);
150         if (!nus2.equals(usd)) {
151             errln("expected " + usd + " but got: " + nus2);
152         }
153
154         locales = Currency.getAvailableULocales();
155         found = false;
156         for (int i = 0; i < locales.length; ++i) {
157             if (locales[i].equals(fu_FU)) {
158                 found = true;
159                 break;
160             }
161         }
162         if (found) {
163             errln("found locale" + fu_FU + " in currency locales after unregister");
164         }
165         
166         Locale[] locs = Currency.getAvailableLocales();
167         found = false;
168         for (int i = 0; i < locs.length; ++i) {
169             if (locs[i].equals(fu_FU)) {
170                 found = true;
171                 break;
172             }
173         }
174         if (found) {
175             errln("found locale" + fu_FU + " in currency locales after unregister");
176         }
177     }
178
179     /**
180      * Test names.
181      */
182     public void TestNames() {
183         // Do a basic check of getName()
184         // USD { "US$", "US Dollar"            } // 04/04/1792-
185         ULocale en = ULocale.ENGLISH;
186         boolean[] isChoiceFormat = new boolean[1];
187         Currency usd = Currency.getInstance("USD");
188         // Warning: HARD-CODED LOCALE DATA in this test.  If it fails, CHECK
189         // THE LOCALE DATA before diving into the code.
190         if (!noData()) {
191             assertEquals("USD.getName(SYMBOL_NAME)",
192                          "$",
193                          usd.getName(en, Currency.SYMBOL_NAME, isChoiceFormat));
194             assertEquals("USD.getName(LONG_NAME)",
195                          "US Dollar",
196                          usd.getName(en, Currency.LONG_NAME, isChoiceFormat));
197         }
198         // TODO add more tests later
199     }
200
201     public void TestCoverage() {
202         Currency usd = Currency.getInstance("USD");
203         if (!noData()) {
204         assertEquals("USD.getSymbol()",
205                 "$",
206                 usd.getSymbol());
207         }
208     }
209     
210     // A real test of the CurrencyDisplayNames class.
211     public void TestCurrencyDisplayNames() {
212         if (!CurrencyDisplayNames.hasData()) {
213             errln("hasData() should return true.");
214         }
215         
216         // with substitute
217         CurrencyDisplayNames cdn = CurrencyDisplayNames.getInstance(ULocale.GERMANY);
218         assertEquals("de_USD_name", "US-Dollar", cdn.getName("USD"));
219         assertEquals("de_USD_symbol", "$", cdn.getSymbol("USD"));
220         assertEquals("de_USD_plural_other", "US-Dollar", cdn.getPluralName("USD", "other"));
221         // unknown plural category, substitute "other"
222         assertEquals("de_USD_plural_foo", "US-Dollar", cdn.getPluralName("USD", "foo"));
223         
224         cdn = CurrencyDisplayNames.getInstance(ULocale.forLanguageTag("en-US"));
225         assertEquals("en-US_USD_name", "US Dollar", cdn.getName("USD"));
226         assertEquals("en-US_USD_symbol", "$", cdn.getSymbol("USD"));
227         assertEquals("en-US_USD_plural_one", "US dollar", cdn.getPluralName("USD", "one"));
228         assertEquals("en-US_USD_plural_other", "US dollars", cdn.getPluralName("USD", "other"));
229         
230         assertEquals("en-US_FOO_name", "FOO", cdn.getName("FOO"));
231         assertEquals("en-US_FOO_symbol", "FOO", cdn.getSymbol("FOO"));
232         assertEquals("en-US_FOO_plural_other", "FOO", cdn.getPluralName("FOO", "other"));
233         
234         assertEquals("en-US bundle", "en", cdn.getULocale().toString());
235         
236         cdn = CurrencyDisplayNames.getInstance(ULocale.forLanguageTag("zz-Gggg-YY"));
237         assertEquals("bundle from current locale", "en", cdn.getULocale().toString());
238         
239         // with no substitute
240         cdn = CurrencyDisplayNames.getInstance(ULocale.GERMANY, true);
241         assertNotNull("have currency data for Germany", cdn);
242         
243         // known currency, behavior unchanged
244         assertEquals("de_USD_name", "US-Dollar", cdn.getName("USD"));
245         assertEquals("de_USD_symbol", "$", cdn.getSymbol("USD"));
246         assertEquals("de_USD_plural_other", "US-Dollar", cdn.getPluralName("USD", "other"));
247         
248         // known currency but unknown plural category
249         assertNull("de_USD_plural_foo", cdn.getPluralName("USD", "foo"));
250         
251         // unknown currency, get null
252         assertNull("de_FOO_name", cdn.getName("FOO"));
253         assertNull("de_FOO_symbol", cdn.getSymbol("FOO"));
254         assertNull("de_FOO_plural_other", cdn.getPluralName("FOO", "other"));
255         assertNull("de_FOO_plural_foo", cdn.getPluralName("FOO", "foo"));
256         
257         // unknown locale with no substitute
258         cdn = CurrencyDisplayNames.getInstance(ULocale.forLanguageTag("zz-Gggg-YY"), true);
259         String ln = "";
260         if (cdn != null) {
261             ln = " (" + cdn.getULocale().toString() + ")";
262         }
263         assertNull("no fallback from unknown locale" + ln , cdn);
264     }
265     
266     // Coverage-only test of CurrencyData
267     public void TestCurrencyData() {
268         CurrencyData.DefaultInfo info_fallback = (CurrencyData.DefaultInfo)CurrencyData.DefaultInfo.getWithFallback(true);
269         if (info_fallback == null) {
270             errln("getWithFallback() returned null.");
271             return;
272         }
273         
274         CurrencyData.DefaultInfo info_nofallback = (CurrencyData.DefaultInfo)CurrencyData.DefaultInfo.getWithFallback(false);
275         if (info_nofallback == null) {
276             errln("getWithFallback() returned null.");
277             return;
278         }
279         
280         if (!info_fallback.getName("isoCode").equals("isoCode") || info_nofallback.getName("isoCode") != null) {
281             errln("Error calling getName().");
282             return;
283         }
284         
285         if (!info_fallback.getPluralName("isoCode", "type").equals("isoCode") || info_nofallback.getPluralName("isoCode", "type") != null) {
286             errln("Error calling getPluralName().");
287             return;
288         }
289         
290         if (!info_fallback.getSymbol("isoCode").equals("isoCode") || info_nofallback.getSymbol("isoCode") != null) {
291             errln("Error calling getSymbol().");
292             return;
293         }
294         
295         if (!info_fallback.symbolMap().isEmpty()) {
296             errln("symbolMap() should return empty map.");
297             return;
298         }
299         
300         if (!info_fallback.nameMap().isEmpty()) {
301             errln("nameMap() should return empty map.");
302             return;
303         }
304         
305         if (!info_fallback.getUnitPatterns().isEmpty() || info_nofallback.getUnitPatterns() != null) {
306             errln("Error calling getUnitPatterns().");
307             return;
308         }
309         
310         if (!info_fallback.getSpacingInfo().equals((CurrencyData.CurrencySpacingInfo.DEFAULT)) ||
311                 info_nofallback.getSpacingInfo() != null) {
312             errln("Error calling getSpacingInfo().");
313             return;
314         }
315         
316         if (info_fallback.getULocale() != ULocale.ROOT) {
317             errln("Error calling getLocale().");
318             return;
319         }
320         
321         if (info_fallback.getFormatInfo("isoCode") != null) {
322             errln("Error calling getFormatInfo().");
323             return;
324         }
325     }
326     
327     // A real test of CurrencyMetaInfo.
328     public void testCurrencyMetaInfoRanges() {
329         CurrencyMetaInfo metainfo = CurrencyMetaInfo.getInstance(true);
330         assertNotNull("have metainfo", metainfo);
331     
332         CurrencyFilter filter = CurrencyFilter.onRegion("DE"); // must be capitalized
333         List<CurrencyInfo> currenciesInGermany = metainfo.currencyInfo(filter);
334         logln("currencies: " + currenciesInGermany.size());
335         DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS z");
336         fmt.setTimeZone(TimeZone.getTimeZone("GMT"));
337         Date demLastDate = new Date(Long.MAX_VALUE);
338         Date eurFirstDate = new Date(Long.MIN_VALUE);
339         for (CurrencyInfo info : currenciesInGermany) {
340             logln(info.toString());
341             logln("from: " + fmt.format(info.from)+ Long.toHexString(info.from));
342             logln("  to: " + fmt.format(info.to) + Long.toHexString(info.to));
343             if (info.code.equals("DEM")) {
344                 demLastDate = new Date(info.to);
345             } else if (info.code.equals("EUR")) {
346                 eurFirstDate = new Date(info.from);
347             }
348         }
349         
350         // the Euro and Deutschmark overlapped for several years
351         assertEquals("DEM available at last date", 2, metainfo.currencyInfo(filter.withDate(demLastDate)).size());
352         
353         // demLastDate + 1 millisecond is not the start of the last day, we consider it the next day, so...
354         Date demLastDatePlus1ms = new Date(demLastDate.getTime() + 1);
355         assertEquals("DEM not available after very start of last date", 1, metainfo.currencyInfo(filter.withDate(demLastDatePlus1ms)).size());
356         
357         // both available for start of euro
358         assertEquals("EUR available on start of first date", 2, metainfo.currencyInfo(filter.withDate(eurFirstDate)).size());
359         
360         // but not one millisecond before the start of the first day
361         Date eurFirstDateMinus1ms = new Date(eurFirstDate.getTime() - 1);
362         assertEquals("EUR not avilable before very start of first date", 1, metainfo.currencyInfo(filter.withDate(eurFirstDateMinus1ms)).size());
363
364         // end time is last millisecond of day
365         GregorianCalendar cal = new GregorianCalendar();
366         cal.setTimeZone(TimeZone.getTimeZone("GMT"));
367         cal.setTime(demLastDate);
368         assertEquals("hour is 23", 23, cal.get(GregorianCalendar.HOUR_OF_DAY));
369         assertEquals("minute is 59", 59, cal.get(GregorianCalendar.MINUTE));
370         assertEquals("second is 59", 59, cal.get(GregorianCalendar.SECOND));
371         assertEquals("millisecond is 999", 999, cal.get(GregorianCalendar.MILLISECOND));
372         
373         // start time is first millisecond of day
374         cal.setTime(eurFirstDate);
375         assertEquals("hour is 0", 0, cal.get(GregorianCalendar.HOUR_OF_DAY));
376         assertEquals("minute is 0", 0, cal.get(GregorianCalendar.MINUTE));
377         assertEquals("second is 0", 0, cal.get(GregorianCalendar.SECOND));
378         assertEquals("millisecond is 0", 0, cal.get(GregorianCalendar.MILLISECOND));
379     }
380     
381     public void testCurrencyMetaInfoRangesWithLongs() {
382         CurrencyMetaInfo metainfo = CurrencyMetaInfo.getInstance(true);
383         assertNotNull("have metainfo", metainfo);
384     
385         CurrencyFilter filter = CurrencyFilter.onRegion("DE"); // must be capitalized
386         List<CurrencyInfo> currenciesInGermany = metainfo.currencyInfo(filter);
387         logln("currencies: " + currenciesInGermany.size());
388         long demLastDate = Long.MAX_VALUE;
389         long eurFirstDate = Long.MIN_VALUE;
390         for (CurrencyInfo info : currenciesInGermany) {
391             logln(info.toString());
392             if (info.code.equals("DEM")) {
393                 demLastDate = info.to;
394             } else if (info.code.equals("EUR")) {
395                 eurFirstDate = info.from;
396             }
397         }
398         // the Euro and Deutschmark overlapped for several years
399         assertEquals("DEM available at last date", 2, metainfo.currencyInfo(filter.withDate(demLastDate)).size());
400         
401         // demLastDate + 1 millisecond is not the start of the last day, we consider it the next day, so...
402         long demLastDatePlus1ms = demLastDate + 1;
403         assertEquals("DEM not available after very start of last date", 1, metainfo.currencyInfo(filter.withDate(demLastDatePlus1ms)).size());
404         
405         // both available for start of euro
406         assertEquals("EUR available on start of first date", 2, metainfo.currencyInfo(filter.withDate(eurFirstDate)).size());
407         
408         // but not one millisecond before the start of the first day
409         long eurFirstDateMinus1ms = eurFirstDate - 1;
410         assertEquals("EUR not avilable before very start of first date", 1, metainfo.currencyInfo(filter.withDate(eurFirstDateMinus1ms)).size());
411     }
412     
413     public void TestWithTender() {
414         CurrencyMetaInfo metainfo = CurrencyMetaInfo.getInstance();
415         if (metainfo == null) {
416             errln("Unable to get CurrencyMetaInfo instance.");
417             return;
418         }
419         CurrencyMetaInfo.CurrencyFilter filter =
420                 CurrencyMetaInfo.CurrencyFilter.onRegion("CH");
421         List<String> currencies = metainfo.currencies(filter);
422         assertTrue("More than one currency for switzerland", currencies.size() > 1);
423         assertEquals(
424                 "With tender",
425                 Arrays.asList(new String[] {"CHF"}),
426                 metainfo.currencies(filter.withTender()));
427     }
428    
429     // Coverage-only test of the CurrencyMetaInfo class
430     public void TestCurrencyMetaInfo() {
431         CurrencyMetaInfo metainfo = CurrencyMetaInfo.getInstance();
432         if (metainfo == null) {
433             errln("Unable to get CurrencyMetaInfo instance.");
434             return;
435         }
436         
437         if (!CurrencyMetaInfo.hasData()) {
438             errln("hasData() should note return false.");
439             return;
440         }
441         
442         CurrencyMetaInfo.CurrencyFilter filter;
443         CurrencyMetaInfo.CurrencyInfo info;
444         CurrencyMetaInfo.CurrencyDigits digits;
445         
446         { // CurrencyFilter
447             filter = CurrencyMetaInfo.CurrencyFilter.onCurrency("currency");
448             CurrencyMetaInfo.CurrencyFilter filter2 = CurrencyMetaInfo.CurrencyFilter.onCurrency("test");
449             if (filter == null) {
450                 errln("Unable to create CurrencyFilter.");
451                 return;
452             }
453             
454             if (filter.equals(new Object())) {
455                 errln("filter should not equal to Object");
456                 return;
457             }
458             
459             if (filter.equals(filter2)) {
460                 errln("filter should not equal filter2");
461                 return;
462             }
463             
464             if (filter.hashCode() == 0) {
465                 errln("Error getting filter hashcode");
466                 return;
467             }
468             
469             if (filter.toString() == null) {
470                 errln("Error calling toString()");
471                 return;
472             }
473         }
474             
475         { // CurrencyInfo
476             info = new CurrencyMetaInfo.CurrencyInfo("region", "code", 0, 1, 1, false);
477             if (info == null) {
478                 errln("Error creating CurrencyInfo.");
479                 return;
480             }
481             
482             if (info.toString() == null) {
483                 errln("Error calling toString()");
484                 return;
485             }
486         }
487         
488         { // CurrencyDigits
489             digits = metainfo.currencyDigits("isoCode");
490             if (digits == null) {
491                 errln("Unable to get CurrencyDigits.");
492                 return;
493             }
494             
495             if (digits.toString() == null) {
496                 errln("Error calling toString()");
497                 return;
498             }
499         }
500     }
501
502     public void TestCurrencyKeyword() {
503         ULocale locale = new ULocale("th_TH@collation=traditional;currency=QQQ");
504         Currency currency = Currency.getInstance(locale);
505         String result = currency.getCurrencyCode();
506         if (!"QQQ".equals(result)) {
507             errln("got unexpected currency: " + result);
508         }
509     }
510
511     public void TestAvailableCurrencyCodes() {
512         String[][] tests = {
513             { "eo_AM", "1950-01-05" },
514             { "eo_AM", "1969-12-31", "SUR" },
515             { "eo_AM", "1991-12-26", "RUR" },
516             { "eo_AM", "2000-12-23", "AMD" },
517             { "eo_AD", "2000-12-23", "EUR", "ESP", "FRF", "ADP" },
518             { "eo_AD", "1969-12-31", "ESP", "FRF", "ADP" },
519             { "eo_AD", "1950-01-05", "ESP", "ADP" },
520             { "eo_AD", "1900-01-17", "ESP" },
521             { "eo_UA", "1994-12-25" },
522             { "eo_QQ", "1969-12-31" },
523             { "eo_AO", "2000-12-23", "AOA" },
524             { "eo_AO", "1995-12-25", "AOR", "AON" },
525             { "eo_AO", "1990-12-26", "AON", "AOK" },
526             { "eo_AO", "1979-12-29", "AOK" },
527             { "eo_AO", "1969-12-31" },
528             { "eo_DE@currency=DEM", "2000-12-23", "EUR", "DEM" },
529             { "eo-DE-u-cu-dem", "2000-12-23", "EUR", "DEM" },
530             { "en_US", null, "USD" },
531             { "en_US_PREEURO", null, "USD" },
532             { "en_US_Q", null, "USD" },
533         };
534         
535         DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
536         for (String[] test : tests) {
537             ULocale locale = new ULocale(test[0]);
538             String timeString = test[1];
539             Date date;
540             if (timeString == null) {
541                 date = new Date();
542                 timeString = "today";
543             } else {
544                 try {
545                     date = fmt.parse(timeString);
546                 } catch (Exception e) {
547                     fail("could not parse date: " + timeString);
548                     continue;
549                 }
550             }
551             String[] expected = null;
552             if (test.length > 2) {
553                 expected = new String[test.length - 2];
554                 System.arraycopy(test, 2, expected, 0, expected.length);
555             }
556             String[] actual = Currency.getAvailableCurrencyCodes(locale, date);
557             
558             // Order is not important as of 4.4.  We never documented that it was.
559             Set<String> expectedSet = new HashSet<String>();
560             if (expected != null) {
561                 expectedSet.addAll(Arrays.asList(expected));
562             }
563             Set<String> actualSet = new HashSet<String>();
564             if (actual != null) {
565                 actualSet.addAll(Arrays.asList(actual));
566             }
567             assertEquals(locale + " on " + timeString, expectedSet, actualSet);
568         }
569     }
570
571     public void TestDeprecatedCurrencyFormat() {
572         // bug 5952
573         Locale locale = new Locale("sr", "QQ");
574         DecimalFormatSymbols icuSymbols = new 
575         com.ibm.icu.text.DecimalFormatSymbols(locale);
576         String symbol = icuSymbols.getCurrencySymbol();
577         Currency currency = icuSymbols.getCurrency();
578         String expectCur = null;
579         String expectSym = "\u00A4";
580         if(!symbol.toString().equals(expectSym) || currency != null) {
581             errln("for " + locale + " expected " + expectSym+"/"+expectCur + " but got " + symbol+"/"+currency);
582         } else {
583             logln("for " + locale + " expected " + expectSym+"/"+expectCur + " and got " + symbol+"/"+currency);
584         }
585     }
586     
587     public void TestGetKeywordValues(){
588
589         final String[][] PREFERRED = {
590             {"root",                 },
591             {"und",                  },
592             {"und_ZZ",               },
593             {"en_US",           "USD"},
594             {"en_029",               },
595             {"en_TH",           "THB"},
596             {"de",              "EUR"},
597             {"de_DE",           "EUR"},
598             {"de_ZZ",                },
599             {"ar",              "EGP"},
600             {"ar_PS",           "ILS", "JOD"},
601             {"en@currency=CAD",     "USD"},
602             {"fr@currency=ZZZ",     "EUR"},
603             {"de_DE@currency=DEM",  "EUR"},
604         };
605
606         String[] ALL = Currency.getKeywordValuesForLocale("currency", ULocale.getDefault(), false);
607         HashSet ALLSET = new HashSet();
608         for (int i = 0; i < ALL.length; i++) {
609             ALLSET.add(ALL[i]);
610         }
611         
612         for (int i = 0; i < PREFERRED.length; i++) {
613             ULocale loc = new ULocale(PREFERRED[i][0]);
614             String[] expected = new String[PREFERRED[i].length - 1];
615             System.arraycopy(PREFERRED[i], 1, expected, 0, expected.length);
616             String[] pref = Currency.getKeywordValuesForLocale("currency", loc, true);
617             assertEquals(loc.toString(), expected, pref);
618
619             String[] all = Currency.getKeywordValuesForLocale("currency", loc, false);
620             // The items in the two collections should match (ignore order, 
621             // behavior change from 4.3.3)
622             Set<String> returnedSet = new HashSet<String>();
623             returnedSet.addAll(Arrays.asList(all));
624             assertEquals(loc.toString(), ALLSET, returnedSet);
625         }
626     }
627
628     public void TestIsAvailable() {
629         Date d1995 = new Date(788918400000L);   // 1995-01-01 00:00 GMT
630         Date d2000 = new Date(946684800000L);   // 2000-01-01 00:00 GMT
631         Date d2005 = new Date(1104537600000L);  // 2005-01-01 00:00 GMT
632
633         assertTrue("USD all time", Currency.isAvailable("USD", null, null));
634         assertTrue("USD before 1995", Currency.isAvailable("USD", null, d1995));
635         assertTrue("USD 1995-2005", Currency.isAvailable("USD", d1995, d2005));
636         assertTrue("USD after 2005", Currency.isAvailable("USD", d2005, null));
637         assertTrue("USD on 2005-01-01", Currency.isAvailable("USD", d2005, d2005));
638
639         assertTrue("usd all time", Currency.isAvailable("usd", null, null));
640
641         assertTrue("DEM all time", Currency.isAvailable("DEM", null, null));
642         assertTrue("DEM before 1995", Currency.isAvailable("DEM", null, d1995));
643         assertTrue("DEM 1995-2000", Currency.isAvailable("DEM", d1995, d2000));
644         assertTrue("DEM 1995-2005", Currency.isAvailable("DEM", d1995, d2005));
645         assertFalse("DEM after 2005", Currency.isAvailable("DEM", d2005, null));
646         assertTrue("DEM on 2000-01-01", Currency.isAvailable("DEM", d2000, d2000));
647         assertFalse("DEM on 2005-01-01", Currency.isAvailable("DEM", d2005, d2005));
648         assertTrue("CHE all the time", Currency.isAvailable("CHE", null, null));
649
650         assertFalse("XXY unknown code", Currency.isAvailable("XXY", null, null));
651
652         assertFalse("USDOLLAR invalid code", Currency.isAvailable("USDOLLAR", null, null));
653
654         // illegal argument combination
655         try {
656             Currency.isAvailable("USD", d2005, d1995);
657             errln("Expected IllegalArgumentException, because lower range is after upper range");
658         } catch (IllegalArgumentException e) {
659             logln("IllegalArgumentException, because lower range is after upper range");
660         }
661     }
662
663     /**
664      * Test case for getAvailableCurrencies()
665      */
666     public void TestGetAvailableCurrencies() {
667         Set<Currency> avail1 = Currency.getAvailableCurrencies();
668
669         // returned set must be modifiable - add one more currency
670         avail1.add(Currency.getInstance("ZZZ"));    // ZZZ is not defined by ISO 4217
671
672         Set<Currency> avail2 = Currency.getAvailableCurrencies();
673         assertTrue("avail1 does not contain all currencies in avail2", avail1.containsAll(avail2));
674         assertTrue("avail1 must have one more currency", (avail1.size() - avail2.size() == 1));
675     }
676
677     /**
678      * Test case for getNumericCode()
679      */
680     public void TestGetNumericCode() {
681         final Object[][] NUMCODE_TESTDATA = {
682             {"USD", 840},
683             {"Usd", 840},   /* mixed casing */
684             {"EUR", 978},
685             {"JPY", 392},
686             {"XFU", 0},     /* XFU: no numeric code */
687             {"ZZZ", 0},     /* ZZZ: undefined ISO currency code */
688         };
689
690         for (Object[] data : NUMCODE_TESTDATA) {
691             Currency cur = Currency.getInstance((String)data[0]);
692             int numCode = cur.getNumericCode();
693             int expected = ((Integer)data[1]).intValue();
694             if (numCode != expected) {
695                 errln("FAIL: getNumericCode returned " + numCode + " for "
696                         + cur.getCurrencyCode() + " - expected: " + expected);
697             }
698         }
699     }
700
701     /**
702      * Test case for getDisplayName()
703      */
704     public void TestGetDisplayName() {
705         final String[][] DISPNAME_TESTDATA = {
706             {"USD", "US Dollar"},
707             {"EUR", "Euro"},
708             {"JPY", "Japanese Yen"},
709         };
710
711         Locale defLocale = Locale.getDefault();
712         Locale jaJP = new Locale("ja", "JP");
713         Locale root = new Locale("");
714
715         for (String[] data : DISPNAME_TESTDATA) {
716             Currency cur = Currency.getInstance(data[0]);
717             assertEquals("getDisplayName() for " + data[0], data[1], cur.getDisplayName());
718             assertEquals("getDisplayName() for " + data[0] + " in locale " + defLocale, data[1], cur.getDisplayName(defLocale));
719
720             // ICU has localized display name for ja
721             assertNotEquals("getDisplayName() for " + data[0] + " in locale " + jaJP, data[1], cur.getDisplayName(jaJP));
722
723             // root locale does not have any localized display names,
724             // so the currency code itself should be returned
725             assertEquals("getDisplayName() for " + data[0] + " in locale " + root, data[0], cur.getDisplayName(root));
726         }
727     }
728     
729     public void TestCurrencyInfoCtor() {
730         new CurrencyMetaInfo.CurrencyInfo("region", "code", 0, 0, 1);
731     }
732 }