Home | History | Annotate | Download | only in unittest
      1 package org.unicode.cldr.unittest;
      2 
      3 import java.util.Arrays;
      4 import java.util.Date;
      5 import java.util.HashMap;
      6 import java.util.List;
      7 import java.util.Map;
      8 import java.util.Set;
      9 import java.util.TreeSet;
     10 
     11 import org.unicode.cldr.util.CLDRConfig;
     12 import org.unicode.cldr.util.CLDRFile;
     13 import org.unicode.cldr.util.Factory;
     14 import org.unicode.cldr.util.LanguageTagParser;
     15 import org.unicode.cldr.util.SupplementalDataInfo;
     16 import org.unicode.cldr.util.SupplementalDataInfo.CurrencyDateInfo;
     17 
     18 import com.ibm.icu.dev.test.TestFmwk;
     19 
     20 public class TestLocalCurrency extends TestFmwk {
     21     static CLDRConfig testInfo = CLDRConfig.getInstance();
     22 
     23     public static void main(String[] args) {
     24         new TestLocalCurrency().run(args);
     25     }
     26 
     27     private int maxLocalizedSymbols(String region) {
     28         final List<String> regionsWithTwoCurrencySymbols = Arrays.asList("AZ",
     29             "BA", "CN", "DZ", "ET", "IQ", "IR", "LK", "KM", "MA", "MR", "MK", "PK",
     30             "RS", "SD", "SY", "TN", "UZ");
     31         if (regionsWithTwoCurrencySymbols.contains(region)) {
     32             return 2;
     33         }
     34         return 1;
     35     }
     36 
     37     public void TestConsistency() {
     38 
     39         LanguageTagParser ltp = new LanguageTagParser();
     40         SupplementalDataInfo supplementalDataInfo = testInfo
     41             .getSupplementalDataInfo();
     42         Map<String, String> localeToLocalCurrencySymbol = new HashMap<String, String>();
     43         Map<String, Set<String>> localizedCurrencySymbols = new HashMap<String, Set<String>>();
     44         Map<String, Set<String>> regionToLocales = new HashMap<String, Set<String>>();
     45 
     46         List<String> nonLocalizedOK = Arrays.asList("AZN", "CHF", "CVE", "GEL",
     47             "HRK", "HUF", "IQD", "IRR", "ISK", "KPW", "LTL", "MAD", "MDL", "RON", "RSD",
     48             "SDG", "THB", "TMT");
     49 
     50         Factory factory = testInfo.getCldrFactory();
     51         Date now = new Date();
     52         for (String locale : factory.getAvailable()) {
     53             ltp.set(locale);
     54             String region = ltp.getRegion();
     55             if (region == null || region.isEmpty() || region.length() != 2) {
     56                 continue;
     57             }
     58             CLDRFile localeData = testInfo.getCLDRFile(locale, true);
     59             String localCurrency = null;
     60             Set<CurrencyDateInfo> targetCurrencyInfo = supplementalDataInfo
     61                 .getCurrencyDateInfo(region);
     62 
     63             for (CurrencyDateInfo cdi : targetCurrencyInfo) {
     64                 if (cdi.getStart().before(now) && cdi.getEnd().after(now)
     65                     && cdi.isLegalTender()) {
     66                     localCurrency = cdi.getCurrency();
     67                     break;
     68                 }
     69             }
     70 
     71             if (localCurrency == null) {
     72                 errln("No current legal tender currency for locale: " + locale);
     73                 continue;
     74             } else {
     75                 logln("Testing currency: " + localCurrency + " for locale: "
     76                     + locale);
     77             }
     78 
     79             String checkPath = "//ldml/numbers/currencies/currency[@type=\""
     80                 + localCurrency + "\"]/symbol";
     81             String localCurrencySymbol = localeData.getWinningValue(checkPath);
     82             localeToLocalCurrencySymbol.put(locale, localCurrencySymbol);
     83 
     84             Set<String> localSymbols = localizedCurrencySymbols.get(region);
     85             if (localSymbols == null) {
     86                 localSymbols = new TreeSet<String>();
     87             }
     88 
     89             if (localCurrencySymbol.equals(localCurrency)
     90                 && !nonLocalizedOK.contains(localCurrency)) {
     91                     errln("Currency symbol " + localCurrencySymbol + " for locale "
     92                         + locale + " is not localized.");
     93             }
     94 
     95             localSymbols.add(localCurrencySymbol);
     96             localizedCurrencySymbols.put(region, localSymbols);
     97 
     98             Set<String> regionLocales = regionToLocales.get(region);
     99             if (regionLocales == null) {
    100                 regionLocales = new TreeSet<String>();
    101             }
    102 
    103             regionLocales.add(locale);
    104             regionToLocales.put(region, regionLocales);
    105 
    106         }
    107 
    108         for (String region : localizedCurrencySymbols.keySet()) {
    109             Set<String> symbols = localizedCurrencySymbols.get(region);
    110             if (symbols.size() > maxLocalizedSymbols(region)) {
    111                 StringBuffer errmsg = new StringBuffer();
    112                 errmsg.append("Too many localized currency symbols for region: "
    113                     + region + "\n");
    114                 for (String locale : regionToLocales.get(region)) {
    115                     errmsg.append("\t\tLocale: " + locale);
    116                     errmsg.append(" Symbol: "
    117                         + localeToLocalCurrencySymbol.get(locale));
    118                     errmsg.append('\n');
    119                 }
    120                 errln(errmsg.toString());
    121             }
    122         }
    123     }
    124 }
    125