From 440d4c84c725c29bc0dc0ec612d71600dbcbaf7e Mon Sep 17 00:00:00 2001 From: Yoshito Umaoka Date: Wed, 29 Feb 2012 23:14:37 +0000 Subject: [PATCH] ICU-9151 Implemented CurrencyNameProvider#getDisplayName in the locale SPI module. Also added corresponding test case. This change was agreed on ICU PMC 2012-02-29. X-SVN-Rev: 31548 --- .../javaspi/util/CurrencyNameProviderICU.java | 11 +- .../dev/test/localespi/CurrencyNameTest.java | 136 ++++++++++++++---- 2 files changed, 120 insertions(+), 27 deletions(-) diff --git a/icu4j/main/classes/localespi/src/com/ibm/icu/impl/javaspi/util/CurrencyNameProviderICU.java b/icu4j/main/classes/localespi/src/com/ibm/icu/impl/javaspi/util/CurrencyNameProviderICU.java index 423f3b8bc09..06aeaf0018b 100644 --- a/icu4j/main/classes/localespi/src/com/ibm/icu/impl/javaspi/util/CurrencyNameProviderICU.java +++ b/icu4j/main/classes/localespi/src/com/ibm/icu/impl/javaspi/util/CurrencyNameProviderICU.java @@ -1,6 +1,6 @@ /* ******************************************************************************* - * Copyright (C) 2008, International Business Machines Corporation and * + * Copyright (C) 2008-2012, International Business Machines Corporation and * * others. All Rights Reserved. * ******************************************************************************* */ @@ -24,6 +24,15 @@ public class CurrencyNameProviderICU extends CurrencyNameProvider { return sym; } + public String getDisplayName(String currencyCode, Locale locale) { + Currency cur = Currency.getInstance(currencyCode); + String name = cur.getDisplayName(locale); + if (name.length() == 0 || name.equals(currencyCode)) { + return null; + } + return name; + } + @Override public Locale[] getAvailableLocales() { return ICULocaleServiceProvider.getAvailableLocales(); diff --git a/icu4j/main/tests/localespi/src/com/ibm/icu/dev/test/localespi/CurrencyNameTest.java b/icu4j/main/tests/localespi/src/com/ibm/icu/dev/test/localespi/CurrencyNameTest.java index 1da80ebdb95..d93c94aa132 100644 --- a/icu4j/main/tests/localespi/src/com/ibm/icu/dev/test/localespi/CurrencyNameTest.java +++ b/icu4j/main/tests/localespi/src/com/ibm/icu/dev/test/localespi/CurrencyNameTest.java @@ -6,42 +6,60 @@ */ package com.ibm.icu.dev.test.localespi; +import java.lang.reflect.Method; +import java.util.Collections; import java.util.Currency; import java.util.HashSet; import java.util.Locale; +import java.util.Set; import com.ibm.icu.dev.test.TestFmwk; +@SuppressWarnings("unchecked") public class CurrencyNameTest extends TestFmwk { public static void main(String[] args) throws Exception { new CurrencyNameTest().run(args); } - public void TestCurrencySymbols() { - // Make a set of unique currencies - HashSet currencies = new HashSet(); - for (Locale l : Locale.getAvailableLocales()) { - if (l.getCountry().length() == 0) { - continue; - } - try { - Currency currency = Currency.getInstance(l); - if (currency != null) { - currencies.add(currency); - } - } catch (IllegalArgumentException iae) { - logln("INFO: java.util.Currency.getInstance(Locale) throws IllegalArgumentException for locale: " + l); - continue; - } + private static final Set AVAILABLE_CURRENCIES; + private static final Method GETDISPLAYNAME_METHOD; + + static { + Method mGetDisplayName = null; + Set currencies = null; + try { + mGetDisplayName = Currency.class.getMethod("getDisplayName", new Class[] {Locale.class}); + Method mGetAvailableCurrencies = Currency.class.getMethod("getAvailableCurrencies", (Class[]) null); + currencies = (Set)mGetAvailableCurrencies.invoke(null, (Object[]) null); + } catch (Exception e) { + // fall through } - for (Currency currency : currencies) { + if (currencies == null) { + // Make a set of unique currencies + currencies = new HashSet(); + for (Locale l : Locale.getAvailableLocales()) { + if (l.getCountry().length() == 0) { + continue; + } + try { + Currency currency = Currency.getInstance(l); + if (currency != null) { + currencies.add(currency); + } + } catch (IllegalArgumentException iae) { + // ignore + } + } + } + GETDISPLAYNAME_METHOD = mGetDisplayName; + AVAILABLE_CURRENCIES = Collections.unmodifiableSet(currencies); + } + + public void TestCurrencySymbols() { + for (Currency currency : AVAILABLE_CURRENCIES) { String currencyCode = currency.getCurrencyCode(); com.ibm.icu.util.Currency currencyIcu = com.ibm.icu.util.Currency.getInstance(currencyCode); - if (currencyIcu == null) { - logln("INFO: Currency code " + currencyCode + " is not supported by ICU"); - continue; - } for (Locale loc : Locale.getAvailableLocales()) { if (TestUtil.isProblematicIBMLocale(loc)) { logln("Skipped " + loc); @@ -67,11 +85,9 @@ public class CurrencyNameTest extends TestFmwk { boolean ignoreErrorForNow = TestUtil.hasScript(loc); if (TestUtil.isICUExtendedLocale(loc)) { - if (!curSymbol.equals(curSymbolIcu)) { - if (!curSymbol.equals(curSymbolIcu) && !ignoreErrorForNow) { - errln("FAIL: Currency symbol for " + currencyCode + " by ICU is " + curSymbolIcu - + ", but got " + curSymbol + " in locale " + loc); - } + if (!curSymbol.equals(curSymbolIcu) && !ignoreErrorForNow) { + errln("FAIL: Currency symbol for " + currencyCode + " by ICU is " + curSymbolIcu + + ", but got " + curSymbol + " in locale " + loc); } } else { if (!curSymbol.equals(curSymbolIcu)) { @@ -89,4 +105,72 @@ public class CurrencyNameTest extends TestFmwk { } } } + + public void TestCurrencyDisplayNames() { + if (GETDISPLAYNAME_METHOD == null) { + logln("INFO: Currency#getDisplayName(String,Locale) is not available."); + return; + } + + for (Currency currency : AVAILABLE_CURRENCIES) { + String currencyCode = currency.getCurrencyCode(); + com.ibm.icu.util.Currency currencyIcu = com.ibm.icu.util.Currency.getInstance(currencyCode); + for (Locale loc : Locale.getAvailableLocales()) { + if (TestUtil.isProblematicIBMLocale(loc)) { + logln("Skipped " + loc); + continue; + } + + String curName = null; + try { + curName = (String)GETDISPLAYNAME_METHOD.invoke(currency, new Object[] {loc}); + } catch (Exception e) { + errln("FAIL: JDK Currency#getDisplayName(\"" + currency + "\", \"" + loc + "\") throws exception: " + e.getMessage()); + continue; + } + + String curNameIcu = currencyIcu.getDisplayName(loc); + + if (curNameIcu.equals(currencyCode)) { + // No data in ICU + if (!curName.equals(currencyCode)) { + logln("INFO: JDK has currency display name " + curName + " for locale " + + loc + ", but ICU does not"); + } + continue; + } + + // Note: Short term workaround for Java locale with script. + // Java Locale with non-empty script cannot have variant "ICU" + // because it's not well-formed as BCP 47. Because we cannot + // build such Locale, we skip the check below for now. + boolean ignoreErrorForNow = TestUtil.hasScript(loc); + + if (TestUtil.isICUExtendedLocale(loc)) { + if (!curName.equals(curNameIcu) && !ignoreErrorForNow) { + errln("FAIL: Currency display name for " + currencyCode + " by ICU is " + curNameIcu + + ", but got " + curName + " in locale " + loc); + } + } else { + if (!curName.equals(curNameIcu)) { + logln("INFO: Currency display name for " + currencyCode + " by ICU is " + curNameIcu + + ", but " + curName + " by JDK in locale " + loc); + } + // Try explicit ICU locale (xx_yy_ICU) + Locale locIcu = TestUtil.toICUExtendedLocale(loc); + try { + curName = (String)GETDISPLAYNAME_METHOD.invoke(currency, new Object[] {locIcu}); + } catch (Exception e) { + errln("FAIL: JDK Currency#getDisplayName(\"" + currency + "\", \"" + locIcu + "\") throws exception: " + e.getMessage()); + continue; + } + if (!curName.equals(curNameIcu) && !ignoreErrorForNow) { + errln("FAIL: Currency display name for " + currencyCode + " by ICU is " + curNameIcu + + ", but got " + curName + " in locale " + locIcu); + } + } + } + } + } + }