mirror of
https://github.com/unicode-org/icu.git
synced 2025-04-13 08:53:20 +00:00
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
This commit is contained in:
parent
1064832f90
commit
440d4c84c7
2 changed files with 120 additions and 27 deletions
|
@ -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();
|
||||
|
|
|
@ -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<Currency> currencies = new HashSet<Currency>();
|
||||
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<Currency> AVAILABLE_CURRENCIES;
|
||||
private static final Method GETDISPLAYNAME_METHOD;
|
||||
|
||||
static {
|
||||
Method mGetDisplayName = null;
|
||||
Set<Currency> currencies = null;
|
||||
try {
|
||||
mGetDisplayName = Currency.class.getMethod("getDisplayName", new Class[] {Locale.class});
|
||||
Method mGetAvailableCurrencies = Currency.class.getMethod("getAvailableCurrencies", (Class[]) null);
|
||||
currencies = (Set<Currency>)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<Currency>();
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue