]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_8_1_1/main/tests/core/src/com/ibm/icu/dev/test/util/CurrencyTest.java
Added flags.
[Dictionary.git] / jars / icu4j-4_8_1_1 / main / tests / core / src / com / ibm / icu / dev / test / util / CurrencyTest.java
1 /*
2  **********************************************************************
3  * Copyright (c) 2002-2011, 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.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;
20 import java.util.Set;
21
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;
29
30 /**
31  * @test
32  * @summary General test of Currency
33  */
34 public class CurrencyTest extends TestFmwk {
35
36     public static void main(String[] args) throws Exception {
37         new CurrencyTest().run(args);
38     }
39
40     /**
41      * Test of basic API.
42      */
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");
49         }
50         if (usd.equals("abc")) {
51             errln("FAIL: USD == (String)");
52         }
53         if (usd.equals(null)) {
54             errln("FAIL: USD == (null)");
55         }
56         if (!usd.equals(usd)) {
57             errln("FAIL: USD != USD");
58         }
59
60         try {
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");
66         }
67
68         try {
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");
74         }
75
76         Locale[] avail = Currency.getAvailableLocales();
77         if(avail==null){
78             errln("FAIL: getAvailableLocales returned null");
79         }
80
81         try {
82             usd.getName(ULocale.US, 5, new boolean[1]);
83             errln("expected getName with invalid type parameter to throw exception");
84         }
85         catch (Exception e) {
86             logln("PASS: getName failed as expected");
87         }
88     }
89
90     /**
91      * Test registration.
92      */
93     public void TestRegistration() {
94         final Currency jpy = Currency.getInstance("JPY");
95         final Currency usd = Currency.getInstance(Locale.US);
96
97     try {
98       Currency.unregister(null); // should fail, coverage
99       errln("expected unregister of null to throw exception");
100     }
101     catch (Exception e) {
102         logln("PASS: unregister of null failed as expected");
103     }
104
105     if (Currency.unregister("")) { // coverage
106       errln("unregister before register erroneously succeeded");
107     }
108
109         ULocale fu_FU = new ULocale("fu_FU");
110
111         Object key1 = Currency.registerInstance(jpy, ULocale.US);
112         Object key2 = Currency.registerInstance(jpy, fu_FU);
113
114         Currency nus = Currency.getInstance(Locale.US);
115         if (!nus.equals(jpy)) {
116             errln("expected " + jpy + " but got: " + nus);
117         }
118
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);
123         }
124
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)) {
129                 found = true;
130                 break;
131             }
132         }
133         if (!found) {
134             errln("did not find locale" + fu_FU + " in currency locales");
135         }
136
137         if (!Currency.unregister(key1)) {
138             errln("unable to unregister currency using key1");
139         }
140         if (!Currency.unregister(key2)) {
141             errln("unable to unregister currency using key2");
142         }
143
144         Currency nus2 = Currency.getInstance(Locale.US);
145         if (!nus2.equals(usd)) {
146             errln("expected " + usd + " but got: " + nus2);
147         }
148
149         locales = Currency.getAvailableULocales();
150         found = false;
151         for (int i = 0; i < locales.length; ++i) {
152             if (locales[i].equals(fu_FU)) {
153                 found = true;
154                 break;
155             }
156         }
157         if (found) {
158             errln("found locale" + fu_FU + " in currency locales after unregister");
159         }
160         
161         Locale[] locs = Currency.getAvailableLocales();
162         found = false;
163         for (int i = 0; i < locs.length; ++i) {
164             if (locs[i].equals(fu_FU)) {
165                 found = true;
166                 break;
167             }
168         }
169         if (found) {
170             errln("found locale" + fu_FU + " in currency locales after unregister");
171         }
172     }
173
174     /**
175      * Test names.
176      */
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.
185         if (!noData()) {
186             assertEquals("USD.getName(SYMBOL_NAME)",
187                          "$",
188                          usd.getName(en, Currency.SYMBOL_NAME, isChoiceFormat));
189             assertEquals("USD.getName(LONG_NAME)",
190                          "US Dollar",
191                          usd.getName(en, Currency.LONG_NAME, isChoiceFormat));
192         }
193         // TODO add more tests later
194     }
195
196     public void TestCoverage() {
197         Currency usd = Currency.getInstance("USD");
198         if (!noData()) {
199         assertEquals("USD.getSymbol()",
200                 "$",
201                 usd.getSymbol());
202         }
203     }
204     
205     // Provide better code coverage for the CurrencyDisplayNames class
206     public void TestCurrencyDisplayNames() {
207         if (!CurrencyDisplayNames.hasData()) {
208             errln("hasData() should return true.");
209         }
210     }
211     
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.");
217             return;
218         }
219         
220         CurrencyData.DefaultInfo info_nofallback = (CurrencyData.DefaultInfo)CurrencyData.DefaultInfo.getWithFallback(false);
221         if (info_nofallback == null) {
222             errln("getWithFallback() returned null.");
223             return;
224         }
225         
226         if (!info_fallback.getName("isoCode").equals("isoCode") || info_nofallback.getName("isoCode") != null) {
227             errln("Error calling getName().");
228             return;
229         }
230         
231         if (!info_fallback.getPluralName("isoCode", "type").equals("isoCode") || info_nofallback.getPluralName("isoCode", "type") != null) {
232             errln("Error calling getPluralName().");
233             return;
234         }
235         
236         if (!info_fallback.getSymbol("isoCode").equals("isoCode") || info_nofallback.getSymbol("isoCode") != null) {
237             errln("Error calling getSymbol().");
238             return;
239         }
240         
241         if (!info_fallback.symbolMap().isEmpty()) {
242             errln("symbolMap() should return empty map.");
243             return;
244         }
245         
246         if (!info_fallback.nameMap().isEmpty()) {
247             errln("nameMap() should return empty map.");
248             return;
249         }
250         
251         if (!info_fallback.getUnitPatterns().isEmpty() || info_nofallback.getUnitPatterns() != null) {
252             errln("Error calling getUnitPatterns().");
253             return;
254         }
255         
256         if (!info_fallback.getSpacingInfo().equals((CurrencyData.CurrencySpacingInfo.DEFAULT)) ||
257                 info_nofallback.getSpacingInfo() != null) {
258             errln("Error calling getSpacingInfo().");
259             return;
260         }
261         
262         if (info_fallback.getLocale() != ULocale.ROOT) {
263             errln("Error calling getLocale().");
264             return;
265         }
266         
267         if (info_fallback.getFormatInfo("isoCode") != null) {
268             errln("Error calling getFormatInfo().");
269             return;
270         }
271     }
272     
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.");
278             return;
279         }
280         
281         if (!CurrencyMetaInfo.hasData()) {
282             errln("hasData() should note return false.");
283             return;
284         }
285         
286         CurrencyMetaInfo.CurrencyFilter filter;
287         CurrencyMetaInfo.CurrencyInfo info;
288         CurrencyMetaInfo.CurrencyDigits digits;
289         
290         { // CurrencyFilter
291             filter = CurrencyMetaInfo.CurrencyFilter.onCurrency("currency");
292             CurrencyMetaInfo.CurrencyFilter filter2 = CurrencyMetaInfo.CurrencyFilter.onCurrency("test");
293             if (filter == null) {
294                 errln("Unable to create CurrencyFilter.");
295                 return;
296             }
297             
298             if (filter.equals(new Object())) {
299                 errln("filter should not equal to Object");
300                 return;
301             }
302             
303             if (filter.equals(filter2)) {
304                 errln("filter should not equal filter2");
305                 return;
306             }
307             
308             if (filter.hashCode() == 0) {
309                 errln("Error getting filter hashcode");
310                 return;
311             }
312             
313             if (filter.toString() == null) {
314                 errln("Error calling toString()");
315                 return;
316             }
317         }
318             
319         { // CurrencyInfo
320             info = new CurrencyMetaInfo.CurrencyInfo("region", "code", 0, 1, 1);
321             if (info == null) {
322                 errln("Error creating CurrencyInfo.");
323                 return;
324             }
325             
326             if (info.toString() == null) {
327                 errln("Error calling toString()");
328                 return;
329             }
330         }
331         
332         { // CurrencyDigits
333             digits = metainfo.currencyDigits("isoCode");
334             if (digits == null) {
335                 errln("Unable to get CurrencyDigits.");
336                 return;
337             }
338             
339             if (digits.toString() == null) {
340                 errln("Error calling toString()");
341                 return;
342             }
343         }
344     }
345
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);
352         }
353     }
354
355     public void TestAvailableCurrencyCodes() {
356         String[][] tests = {
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" },
377         };
378         
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];
383             Date date;
384             if (timeString == null) {
385                 date = new Date();
386                 timeString = "today";
387             } else {
388                 try {
389                     date = fmt.parse(timeString);
390                 } catch (Exception e) {
391                     fail("could not parse date: " + timeString);
392                     continue;
393                 }
394             }
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);
399             }
400             String[] actual = Currency.getAvailableCurrencyCodes(locale, date);
401             
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));
406             }
407             Set<String> actualSet = new HashSet<String>();
408             if (actual != null) {
409                 actualSet.addAll(Arrays.asList(actual));
410             }
411             assertEquals(locale + " on " + timeString, expectedSet, actualSet);
412         }
413     }
414
415     public void TestDeprecatedCurrencyFormat() {
416         // bug 5952
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);
426         } else {
427             logln("for " + locale + " expected " + expectSym+"/"+expectCur + " and got " + symbol+"/"+currency);
428         }
429     }
430     
431     public void TestGetKeywordValues(){
432
433         final String[][] PREFERRED = {
434             {"root",                 },
435             {"und",                  },
436             {"und_ZZ",               },
437             {"en_US",           "USD"},
438             {"en_029",               },
439             {"en_TH",           "THB"},
440             {"de",              "EUR"},
441             {"de_DE",           "EUR"},
442             {"de_ZZ",                },
443             {"ar",              "EGP"},
444             {"ar_PS",           "JOD", "ILS"},
445             {"en@currency=CAD",     "USD"},
446             {"fr@currency=ZZZ",     "EUR"},
447             {"de_DE@currency=DEM",  "EUR"},
448         };
449
450         String[] ALL = Currency.getKeywordValuesForLocale("currency", ULocale.getDefault(), false);
451         HashSet ALLSET = new HashSet();
452         for (int i = 0; i < ALL.length; i++) {
453             ALLSET.add(ALL[i]);
454         }
455         
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);
462
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);
469         }
470     }
471
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
476
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));
482
483         assertTrue("usd all time", Currency.isAvailable("usd", null, null));
484
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));
492
493         assertFalse("XXX unknown code", Currency.isAvailable("XXX", null, null));
494
495         assertFalse("USDOLLAR invalid code", Currency.isAvailable("USDOLLAR", null, null));
496
497         // illegal argument combination
498         try {
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");
503         }
504     }
505 }