2 *******************************************************************************
3 * Copyright (C) 2008-2012, International Business Machines Corporation and *
4 * others. All Rights Reserved. *
5 *******************************************************************************
7 package com.ibm.icu.dev.test.localespi;
9 import java.lang.reflect.Method;
10 import java.util.HashSet;
11 import java.util.Locale;
14 import com.ibm.icu.dev.test.TestFmwk;
15 import com.ibm.icu.util.ULocale;
17 public class LocaleNameTest extends TestFmwk {
18 public static void main(String[] args) throws Exception {
19 new LocaleNameTest().run(args);
22 private static final Method GETDISPLAYSCRIPT_METHOD;
25 Method mGetDisplayScript = null;
27 mGetDisplayScript = Locale.class.getMethod("getDisplayScript", new Class[] {Locale.class});
28 } catch (Exception e) {
31 GETDISPLAYSCRIPT_METHOD = mGetDisplayScript;
34 public void TestLanguageNames() {
35 Locale[] locales = Locale.getAvailableLocales();
36 StringBuffer icuid = new StringBuffer();
37 for (Locale inLocale : locales) {
38 if (TestUtil.isProblematicIBMLocale(inLocale)) {
39 logln("Skipped " + inLocale);
43 ULocale inULocale = ULocale.forLocale(inLocale);
44 Locale inLocaleICU = TestUtil.toICUExtendedLocale(inLocale);
45 for (Locale forLocale : locales) {
46 if (forLocale.getLanguage().length() == 0) {
50 icuid.append(forLocale.getLanguage());
51 String country = forLocale.getCountry();
52 String variant = forLocale.getVariant();
53 if (country.length() != 0) {
55 icuid.append(country);
57 if (variant.length() != 0) {
58 if (country.length() == 0) {
62 icuid.append(variant);
64 ULocale forULocale = new ULocale(icuid.toString());
65 String icuname = ULocale.getDisplayLanguage(forULocale.getLanguage(), inULocale);
66 if (icuname.equals(forULocale.getLanguage()) || icuname.length() == 0) {
70 String name = forLocale.getDisplayLanguage(inLocale);
72 if (TestUtil.isICUExtendedLocale(inLocale)) {
73 // The name should be taken from ICU
74 if (!name.equals(icuname)) {
75 errln("FAIL: Language name by ICU is " + icuname + ", but got " + name
76 + " for locale " + forLocale + " in locale " + inLocale);
79 if (!name.equals(icuname)) {
80 logln("INFO: Language name by JDK is " + name + ", but " + icuname +
81 " by ICU, for locale " + forLocale + " in locale " + inLocale);
83 // Try explicit ICU locale (xx_yy_ICU)
84 name = forLocale.getDisplayLanguage(inLocaleICU);
85 if (!name.equals(icuname)) {
86 errln("FAIL: Language name by ICU is " + icuname + ", but got " + name
87 + " for locale " + forLocale + " in locale " + inLocaleICU);
94 public void TestScriptNames() {
95 if (GETDISPLAYSCRIPT_METHOD == null) {
96 logln("INFO: Locale#getDisplayScript(Locale) is not available.");
100 Locale[] locales = Locale.getAvailableLocales();
101 for (Locale inLocale : locales) {
102 if (TestUtil.isProblematicIBMLocale(inLocale)) {
103 logln("Skipped " + inLocale);
107 ULocale inULocale = ULocale.forLocale(inLocale);
108 Locale inLocaleICU = TestUtil.toICUExtendedLocale(inLocale);
109 for (ULocale forULocale : ULocale.getAvailableLocales()) {
110 if (forULocale.getScript().length() == 0) {
113 Locale forLocale = forULocale.toLocale();
114 String icuname = forULocale.getDisplayScript(inULocale);
115 if (icuname.equals(forULocale.getScript()) || icuname.length() == 0) {
121 name = (String)GETDISPLAYSCRIPT_METHOD.invoke(forLocale, new Object[] {inLocale});
122 } catch (Exception e) {
123 errln("FAIL: JDK Locale#getDisplayScript(\"" + inLocale + "\") throws exception: " + e.getMessage());
127 if (TestUtil.isICUExtendedLocale(inLocale)) {
128 // The name should be taken from ICU
129 if (!name.equals(icuname)) {
130 errln("FAIL: Script name by ICU is " + icuname + ", but got " + name
131 + " for locale " + forLocale + " in locale " + inLocale);
134 // The name might be taken from JDK
135 if (!name.equals(icuname)) {
136 logln("INFO: Script name by JDK is " + name + ", but " + icuname +
137 " in ICU, for locale " + forLocale + " in locale " + inLocale);
139 // Try explicit ICU locale (xx_yy_ICU)
141 name = (String)GETDISPLAYSCRIPT_METHOD.invoke(forLocale, new Object[] {inLocaleICU});
142 } catch (Exception e) {
143 errln("FAIL: JDK Locale#getDisplayScript(\"" + inLocaleICU + "\") throws exception: " + e.getMessage());
146 if (!name.equals(icuname)) {
147 errln("FAIL: Script name by ICU is " + icuname + ", but got " + name
148 + " for locale " + forLocale + " in locale " + inLocaleICU);
155 public void TestCountryNames() {
156 Locale[] locales = Locale.getAvailableLocales();
157 for (Locale inLocale : locales) {
158 if (TestUtil.isProblematicIBMLocale(inLocale)) {
159 logln("Skipped " + inLocale);
163 ULocale inULocale = ULocale.forLocale(inLocale);
164 Locale inLocaleICU = TestUtil.toICUExtendedLocale(inLocale);
165 for (Locale forLocale : locales) {
166 if (forLocale.getCountry().length() == 0) {
169 // ULocale#forLocale preserves country always
170 ULocale forULocale = ULocale.forLocale(forLocale);
171 String icuname = forULocale.getDisplayCountry(inULocale);
172 if (icuname.equals(forULocale.getCountry()) || icuname.length() == 0) {
176 String name = forLocale.getDisplayCountry(inLocale);
177 if (TestUtil.isICUExtendedLocale(inLocale)) {
178 // The name should be taken from ICU
179 if (!name.equals(icuname)) {
180 errln("FAIL: Country name by ICU is " + icuname + ", but got " + name
181 + " for locale " + forLocale + " in locale " + inLocale);
184 // The name might be taken from JDK
185 if (!name.equals(icuname)) {
186 logln("INFO: Country name by JDK is " + name + ", but " + icuname +
187 " in ICU, for locale " + forLocale + " in locale " + inLocale);
189 // Try explicit ICU locale (xx_yy_ICU)
190 name = forLocale.getDisplayCountry(inLocaleICU);
191 if (!name.equals(icuname)) {
192 errln("FAIL: Country name by ICU is " + icuname + ", but got " + name
193 + " for locale " + forLocale + " in locale " + inLocaleICU);
200 public void TestVariantNames() {
201 Set<Locale> locales = new HashSet<Locale>();
202 for (Locale l : Locale.getAvailableLocales()) {
205 // Add some locales with variant
206 final Locale[] additionalLocales = {
207 new Locale("fr", "FR", "1694acad"),
208 new Locale("de", "DE", "1901"),
209 new Locale("en", "US", "boont"),
210 new Locale("el", "GR", "monoton"),
212 for (Locale l : additionalLocales) {
216 for (Locale inLocale : locales) {
217 if (TestUtil.isProblematicIBMLocale(inLocale)) {
218 logln("Skipped " + inLocale);
222 ULocale inULocale = ULocale.forLocale(inLocale);
223 Locale inLocaleICU = TestUtil.toICUExtendedLocale(inLocale);
224 for (Locale forLocale : locales) {
225 String locVar = forLocale.getVariant();
226 if (locVar.length() == 0) {
229 // Note: JDK resolves a display name for each variant subtag
230 String[] locVarSubtags = locVar.split("_");
232 for (String locSingleVar : locVarSubtags) {
233 if (locSingleVar.equals(TestUtil.ICU_VARIANT)
234 || locSingleVar.equals("Cyrl") || locSingleVar.equals("Latn")) { // IBM Java 6 has locales with 'variant' Cryl/Latn
237 Locale forLocaleSingleVar = new Locale(forLocale.getLanguage(), forLocale.getCountry(), locSingleVar);
238 ULocale forULocaleSingleVar = new ULocale("und_ZZ_" + locSingleVar);
239 String icuname = forULocaleSingleVar.getDisplayVariant(inULocale);
240 if (icuname.equals(locSingleVar) || icuname.length() == 0) {
244 String name = forLocaleSingleVar.getDisplayVariant(inLocale);
245 if (name.equalsIgnoreCase(locSingleVar)) {
246 // ICU does not have any localized display name.
247 // Note: ICU turns variant to upper case string, while Java does not.
250 if (TestUtil.isICUExtendedLocale(inLocale)) {
251 // The name should be taken from ICU
252 if (!name.equals(icuname)) {
253 errln("FAIL: Variant name by ICU is " + icuname + ", but got " + name
254 + " for locale " + forLocaleSingleVar + " in locale " + inLocale);
257 if (!name.equals(icuname)) {
258 logln("INFO: Variant name by JDK is " + name + ", but " + icuname +
259 " in ICU, for locale " + forLocaleSingleVar + " in locale " + inLocale);
261 // Try explicit ICU locale (xx_yy_ICU)
262 name = forLocaleSingleVar.getDisplayVariant(inLocaleICU);
263 if (!name.equals(icuname)) {
264 errln("FAIL: Variant name by ICU is " + icuname + ", but got " + name
265 + " for locale " + forLocaleSingleVar + " in locale " + inLocaleICU);