]> gitweb.fperrin.net Git - Dictionary.git/blobdiff - jars/icu4j-52_1/main/tests/core/src/com/ibm/icu/dev/test/util/CurrencyTest.java
Upgrade ICU4J.
[Dictionary.git] / jars / icu4j-52_1 / main / tests / core / src / com / ibm / icu / dev / test / util / CurrencyTest.java
similarity index 60%
rename from jars/icu4j-4_8_1_1/main/tests/core/src/com/ibm/icu/dev/test/util/CurrencyTest.java
rename to jars/icu4j-52_1/main/tests/core/src/com/ibm/icu/dev/test/util/CurrencyTest.java
index 1901cd01bc292279b6a83da58f051dc82486f586..6bfd090b00e416eb174595811fa48ce5e3253e6c 100644 (file)
@@ -1,6 +1,6 @@
 /*
  **********************************************************************
- * Copyright (c) 2002-2011, International Business Machines
+ * Copyright (c) 2002-2013, International Business Machines
  * Corporation and others.  All Rights Reserved.
  **********************************************************************
  * Author: Alan Liu
 
 package com.ibm.icu.dev.test.util;
 
-import java.text.DateFormat;
-import java.text.SimpleDateFormat;
 import java.util.Arrays;
 import java.util.Date;
 import java.util.HashSet;
+import java.util.List;
 import java.util.Locale;
 import java.util.Set;
 
@@ -23,8 +22,14 @@ import com.ibm.icu.dev.test.TestFmwk;
 import com.ibm.icu.impl.CurrencyData;
 import com.ibm.icu.text.CurrencyDisplayNames;
 import com.ibm.icu.text.CurrencyMetaInfo;
+import com.ibm.icu.text.CurrencyMetaInfo.CurrencyFilter;
+import com.ibm.icu.text.CurrencyMetaInfo.CurrencyInfo;
+import com.ibm.icu.text.DateFormat;
 import com.ibm.icu.text.DecimalFormatSymbols;
+import com.ibm.icu.text.SimpleDateFormat;
 import com.ibm.icu.util.Currency;
+import com.ibm.icu.util.GregorianCalendar;
+import com.ibm.icu.util.TimeZone;
 import com.ibm.icu.util.ULocale;
 
 /**
@@ -202,14 +207,63 @@ public class CurrencyTest extends TestFmwk {
         }
     }
     
-    // Provide better code coverage for the CurrencyDisplayNames class
+    // A real test of the CurrencyDisplayNames class.
     public void TestCurrencyDisplayNames() {
         if (!CurrencyDisplayNames.hasData()) {
             errln("hasData() should return true.");
         }
+        
+        // with substitute
+        CurrencyDisplayNames cdn = CurrencyDisplayNames.getInstance(ULocale.GERMANY);
+        assertEquals("de_USD_name", "US-Dollar", cdn.getName("USD"));
+        assertEquals("de_USD_symbol", "$", cdn.getSymbol("USD"));
+        assertEquals("de_USD_plural_other", "US-Dollar", cdn.getPluralName("USD", "other"));
+        // unknown plural category, substitute "other"
+        assertEquals("de_USD_plural_foo", "US-Dollar", cdn.getPluralName("USD", "foo"));
+        
+        cdn = CurrencyDisplayNames.getInstance(ULocale.forLanguageTag("en-US"));
+        assertEquals("en-US_USD_name", "US Dollar", cdn.getName("USD"));
+        assertEquals("en-US_USD_symbol", "$", cdn.getSymbol("USD"));
+        assertEquals("en-US_USD_plural_one", "US dollar", cdn.getPluralName("USD", "one"));
+        assertEquals("en-US_USD_plural_other", "US dollars", cdn.getPluralName("USD", "other"));
+        
+        assertEquals("en-US_FOO_name", "FOO", cdn.getName("FOO"));
+        assertEquals("en-US_FOO_symbol", "FOO", cdn.getSymbol("FOO"));
+        assertEquals("en-US_FOO_plural_other", "FOO", cdn.getPluralName("FOO", "other"));
+        
+        assertEquals("en-US bundle", "en", cdn.getULocale().toString());
+        
+        cdn = CurrencyDisplayNames.getInstance(ULocale.forLanguageTag("zz-Gggg-YY"));
+        assertEquals("bundle from current locale", "en", cdn.getULocale().toString());
+        
+        // with no substitute
+        cdn = CurrencyDisplayNames.getInstance(ULocale.GERMANY, true);
+        assertNotNull("have currency data for Germany", cdn);
+        
+        // known currency, behavior unchanged
+        assertEquals("de_USD_name", "US-Dollar", cdn.getName("USD"));
+        assertEquals("de_USD_symbol", "$", cdn.getSymbol("USD"));
+        assertEquals("de_USD_plural_other", "US-Dollar", cdn.getPluralName("USD", "other"));
+        
+        // known currency but unknown plural category
+        assertNull("de_USD_plural_foo", cdn.getPluralName("USD", "foo"));
+        
+        // unknown currency, get null
+        assertNull("de_FOO_name", cdn.getName("FOO"));
+        assertNull("de_FOO_symbol", cdn.getSymbol("FOO"));
+        assertNull("de_FOO_plural_other", cdn.getPluralName("FOO", "other"));
+        assertNull("de_FOO_plural_foo", cdn.getPluralName("FOO", "foo"));
+        
+        // unknown locale with no substitute
+        cdn = CurrencyDisplayNames.getInstance(ULocale.forLanguageTag("zz-Gggg-YY"), true);
+        String ln = "";
+        if (cdn != null) {
+            ln = " (" + cdn.getULocale().toString() + ")";
+        }
+        assertNull("no fallback from unknown locale" + ln , cdn);
     }
     
-    // Provide better code coverage for the CurrencyData class
+    // Coverage-only test of CurrencyData
     public void TestCurrencyData() {
         CurrencyData.DefaultInfo info_fallback = (CurrencyData.DefaultInfo)CurrencyData.DefaultInfo.getWithFallback(true);
         if (info_fallback == null) {
@@ -259,7 +313,7 @@ public class CurrencyTest extends TestFmwk {
             return;
         }
         
-        if (info_fallback.getLocale() != ULocale.ROOT) {
+        if (info_fallback.getULocale() != ULocale.ROOT) {
             errln("Error calling getLocale().");
             return;
         }
@@ -270,7 +324,109 @@ public class CurrencyTest extends TestFmwk {
         }
     }
     
-    // Provide better code coverage for the CurrencyMetaInfo class
+    // A real test of CurrencyMetaInfo.
+    public void testCurrencyMetaInfoRanges() {
+        CurrencyMetaInfo metainfo = CurrencyMetaInfo.getInstance(true);
+        assertNotNull("have metainfo", metainfo);
+    
+        CurrencyFilter filter = CurrencyFilter.onRegion("DE"); // must be capitalized
+        List<CurrencyInfo> currenciesInGermany = metainfo.currencyInfo(filter);
+        logln("currencies: " + currenciesInGermany.size());
+        DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS z");
+        fmt.setTimeZone(TimeZone.getTimeZone("GMT"));
+        Date demLastDate = new Date(Long.MAX_VALUE);
+        Date eurFirstDate = new Date(Long.MIN_VALUE);
+        for (CurrencyInfo info : currenciesInGermany) {
+            logln(info.toString());
+            logln("from: " + fmt.format(info.from)+ Long.toHexString(info.from));
+            logln("  to: " + fmt.format(info.to) + Long.toHexString(info.to));
+            if (info.code.equals("DEM")) {
+                demLastDate = new Date(info.to);
+            } else if (info.code.equals("EUR")) {
+                eurFirstDate = new Date(info.from);
+            }
+        }
+        
+        // the Euro and Deutschmark overlapped for several years
+        assertEquals("DEM available at last date", 2, metainfo.currencyInfo(filter.withDate(demLastDate)).size());
+        
+        // demLastDate + 1 millisecond is not the start of the last day, we consider it the next day, so...
+        Date demLastDatePlus1ms = new Date(demLastDate.getTime() + 1);
+        assertEquals("DEM not available after very start of last date", 1, metainfo.currencyInfo(filter.withDate(demLastDatePlus1ms)).size());
+        
+        // both available for start of euro
+        assertEquals("EUR available on start of first date", 2, metainfo.currencyInfo(filter.withDate(eurFirstDate)).size());
+        
+        // but not one millisecond before the start of the first day
+        Date eurFirstDateMinus1ms = new Date(eurFirstDate.getTime() - 1);
+        assertEquals("EUR not avilable before very start of first date", 1, metainfo.currencyInfo(filter.withDate(eurFirstDateMinus1ms)).size());
+
+        // end time is last millisecond of day
+        GregorianCalendar cal = new GregorianCalendar();
+        cal.setTimeZone(TimeZone.getTimeZone("GMT"));
+        cal.setTime(demLastDate);
+        assertEquals("hour is 23", 23, cal.get(GregorianCalendar.HOUR_OF_DAY));
+        assertEquals("minute is 59", 59, cal.get(GregorianCalendar.MINUTE));
+        assertEquals("second is 59", 59, cal.get(GregorianCalendar.SECOND));
+        assertEquals("millisecond is 999", 999, cal.get(GregorianCalendar.MILLISECOND));
+        
+        // start time is first millisecond of day
+        cal.setTime(eurFirstDate);
+        assertEquals("hour is 0", 0, cal.get(GregorianCalendar.HOUR_OF_DAY));
+        assertEquals("minute is 0", 0, cal.get(GregorianCalendar.MINUTE));
+        assertEquals("second is 0", 0, cal.get(GregorianCalendar.SECOND));
+        assertEquals("millisecond is 0", 0, cal.get(GregorianCalendar.MILLISECOND));
+    }
+    
+    public void testCurrencyMetaInfoRangesWithLongs() {
+        CurrencyMetaInfo metainfo = CurrencyMetaInfo.getInstance(true);
+        assertNotNull("have metainfo", metainfo);
+    
+        CurrencyFilter filter = CurrencyFilter.onRegion("DE"); // must be capitalized
+        List<CurrencyInfo> currenciesInGermany = metainfo.currencyInfo(filter);
+        logln("currencies: " + currenciesInGermany.size());
+        long demLastDate = Long.MAX_VALUE;
+        long eurFirstDate = Long.MIN_VALUE;
+        for (CurrencyInfo info : currenciesInGermany) {
+            logln(info.toString());
+            if (info.code.equals("DEM")) {
+                demLastDate = info.to;
+            } else if (info.code.equals("EUR")) {
+                eurFirstDate = info.from;
+            }
+        }
+        // the Euro and Deutschmark overlapped for several years
+        assertEquals("DEM available at last date", 2, metainfo.currencyInfo(filter.withDate(demLastDate)).size());
+        
+        // demLastDate + 1 millisecond is not the start of the last day, we consider it the next day, so...
+        long demLastDatePlus1ms = demLastDate + 1;
+        assertEquals("DEM not available after very start of last date", 1, metainfo.currencyInfo(filter.withDate(demLastDatePlus1ms)).size());
+        
+        // both available for start of euro
+        assertEquals("EUR available on start of first date", 2, metainfo.currencyInfo(filter.withDate(eurFirstDate)).size());
+        
+        // but not one millisecond before the start of the first day
+        long eurFirstDateMinus1ms = eurFirstDate - 1;
+        assertEquals("EUR not avilable before very start of first date", 1, metainfo.currencyInfo(filter.withDate(eurFirstDateMinus1ms)).size());
+    }
+    
+    public void TestWithTender() {
+        CurrencyMetaInfo metainfo = CurrencyMetaInfo.getInstance();
+        if (metainfo == null) {
+            errln("Unable to get CurrencyMetaInfo instance.");
+            return;
+        }
+        CurrencyMetaInfo.CurrencyFilter filter =
+                CurrencyMetaInfo.CurrencyFilter.onRegion("CH");
+        List<String> currencies = metainfo.currencies(filter);
+        assertTrue("More than one currency for switzerland", currencies.size() > 1);
+        assertEquals(
+                "With tender",
+                Arrays.asList(new String[] {"CHF"}),
+                metainfo.currencies(filter.withTender()));
+    }
+   
+    // Coverage-only test of the CurrencyMetaInfo class
     public void TestCurrencyMetaInfo() {
         CurrencyMetaInfo metainfo = CurrencyMetaInfo.getInstance();
         if (metainfo == null) {
@@ -317,7 +473,7 @@ public class CurrencyTest extends TestFmwk {
         }
             
         { // CurrencyInfo
-            info = new CurrencyMetaInfo.CurrencyInfo("region", "code", 0, 1, 1);
+            info = new CurrencyMetaInfo.CurrencyInfo("region", "code", 0, 1, 1, false);
             if (info == null) {
                 errln("Error creating CurrencyInfo.");
                 return;
@@ -441,7 +597,7 @@ public class CurrencyTest extends TestFmwk {
             {"de_DE",           "EUR"},
             {"de_ZZ",                },
             {"ar",              "EGP"},
-            {"ar_PS",           "JOD", "ILS"},
+            {"ar_PS",           "ILS", "JOD"},
             {"en@currency=CAD",     "USD"},
             {"fr@currency=ZZZ",     "EUR"},
             {"de_DE@currency=DEM",  "EUR"},
@@ -489,8 +645,9 @@ public class CurrencyTest extends TestFmwk {
         assertFalse("DEM after 2005", Currency.isAvailable("DEM", d2005, null));
         assertTrue("DEM on 2000-01-01", Currency.isAvailable("DEM", d2000, d2000));
         assertFalse("DEM on 2005-01-01", Currency.isAvailable("DEM", d2005, d2005));
+        assertTrue("CHE all the time", Currency.isAvailable("CHE", null, null));
 
-        assertFalse("XXX unknown code", Currency.isAvailable("XXX", null, null));
+        assertFalse("XXY unknown code", Currency.isAvailable("XXY", null, null));
 
         assertFalse("USDOLLAR invalid code", Currency.isAvailable("USDOLLAR", null, null));
 
@@ -502,4 +659,74 @@ public class CurrencyTest extends TestFmwk {
             logln("IllegalArgumentException, because lower range is after upper range");
         }
     }
+
+    /**
+     * Test case for getAvailableCurrencies()
+     */
+    public void TestGetAvailableCurrencies() {
+        Set<Currency> avail1 = Currency.getAvailableCurrencies();
+
+        // returned set must be modifiable - add one more currency
+        avail1.add(Currency.getInstance("ZZZ"));    // ZZZ is not defined by ISO 4217
+
+        Set<Currency> avail2 = Currency.getAvailableCurrencies();
+        assertTrue("avail1 does not contain all currencies in avail2", avail1.containsAll(avail2));
+        assertTrue("avail1 must have one more currency", (avail1.size() - avail2.size() == 1));
+    }
+
+    /**
+     * Test case for getNumericCode()
+     */
+    public void TestGetNumericCode() {
+        final Object[][] NUMCODE_TESTDATA = {
+            {"USD", 840},
+            {"Usd", 840},   /* mixed casing */
+            {"EUR", 978},
+            {"JPY", 392},
+            {"XFU", 0},     /* XFU: no numeric code */
+            {"ZZZ", 0},     /* ZZZ: undefined ISO currency code */
+        };
+
+        for (Object[] data : NUMCODE_TESTDATA) {
+            Currency cur = Currency.getInstance((String)data[0]);
+            int numCode = cur.getNumericCode();
+            int expected = ((Integer)data[1]).intValue();
+            if (numCode != expected) {
+                errln("FAIL: getNumericCode returned " + numCode + " for "
+                        + cur.getCurrencyCode() + " - expected: " + expected);
+            }
+        }
+    }
+
+    /**
+     * Test case for getDisplayName()
+     */
+    public void TestGetDisplayName() {
+        final String[][] DISPNAME_TESTDATA = {
+            {"USD", "US Dollar"},
+            {"EUR", "Euro"},
+            {"JPY", "Japanese Yen"},
+        };
+
+        Locale defLocale = Locale.getDefault();
+        Locale jaJP = new Locale("ja", "JP");
+        Locale root = new Locale("");
+
+        for (String[] data : DISPNAME_TESTDATA) {
+            Currency cur = Currency.getInstance(data[0]);
+            assertEquals("getDisplayName() for " + data[0], data[1], cur.getDisplayName());
+            assertEquals("getDisplayName() for " + data[0] + " in locale " + defLocale, data[1], cur.getDisplayName(defLocale));
+
+            // ICU has localized display name for ja
+            assertNotEquals("getDisplayName() for " + data[0] + " in locale " + jaJP, data[1], cur.getDisplayName(jaJP));
+
+            // root locale does not have any localized display names,
+            // so the currency code itself should be returned
+            assertEquals("getDisplayName() for " + data[0] + " in locale " + root, data[0], cur.getDisplayName(root));
+        }
+    }
+    
+    public void TestCurrencyInfoCtor() {
+        new CurrencyMetaInfo.CurrencyInfo("region", "code", 0, 0, 1);
+    }
 }