]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-52_1/main/tests/localespi/src/com/ibm/icu/dev/test/localespi/LocaleNameTest.java
Upgrade ICU4J.
[Dictionary.git] / jars / icu4j-52_1 / main / tests / localespi / src / com / ibm / icu / dev / test / localespi / LocaleNameTest.java
1 /*
2  *******************************************************************************
3  * Copyright (C) 2008-2012, International Business Machines Corporation and    *
4  * others. All Rights Reserved.                                                *
5  *******************************************************************************
6  */
7 package com.ibm.icu.dev.test.localespi;
8
9 import java.lang.reflect.Method;
10 import java.util.HashSet;
11 import java.util.Locale;
12 import java.util.Set;
13
14 import com.ibm.icu.dev.test.TestFmwk;
15 import com.ibm.icu.util.ULocale;
16
17 public class LocaleNameTest extends TestFmwk {
18     public static void main(String[] args) throws Exception {
19         new LocaleNameTest().run(args);
20     }
21
22     private static final Method GETDISPLAYSCRIPT_METHOD;
23
24     static {
25         Method mGetDisplayScript = null;
26         try {
27             mGetDisplayScript = Locale.class.getMethod("getDisplayScript", new Class[] {Locale.class});
28         } catch (Exception e) {
29             // fall through
30         }
31         GETDISPLAYSCRIPT_METHOD = mGetDisplayScript;
32     }
33
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);
40                 continue;
41             }
42
43             ULocale inULocale = ULocale.forLocale(inLocale);
44             Locale inLocaleICU = TestUtil.toICUExtendedLocale(inLocale);
45             for (Locale forLocale : locales) {
46                 if (forLocale.getLanguage().length() == 0) {
47                     continue;
48                 }
49                 icuid.setLength(0);
50                 icuid.append(forLocale.getLanguage());
51                 String country = forLocale.getCountry();
52                 String variant = forLocale.getVariant();
53                 if (country.length() != 0) {
54                     icuid.append("_");
55                     icuid.append(country);
56                 }
57                 if (variant.length() != 0) {
58                     if (country.length() == 0) {
59                         icuid.append("_");
60                     }
61                     icuid.append("_");
62                     icuid.append(variant);
63                 }
64                 ULocale forULocale = new ULocale(icuid.toString());
65                 String icuname = ULocale.getDisplayLanguage(forULocale.getLanguage(), inULocale);
66                 if (icuname.equals(forULocale.getLanguage()) || icuname.length() == 0) {
67                     continue;
68                 }
69
70                 String name = forLocale.getDisplayLanguage(inLocale);
71
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);
77                     }
78                 } else {
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);
82                     }
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);
88                     }
89                 }
90             }
91         }
92     }
93
94     public void TestScriptNames() {
95         if (GETDISPLAYSCRIPT_METHOD == null) {
96             logln("INFO: Locale#getDisplayScript(Locale) is not available.");
97             return;
98         }
99
100         Locale[] locales = Locale.getAvailableLocales();
101         for (Locale inLocale : locales) {
102             if (TestUtil.isProblematicIBMLocale(inLocale)) {
103                 logln("Skipped " + inLocale);
104                 continue;
105             }
106
107             ULocale inULocale = ULocale.forLocale(inLocale);
108             Locale inLocaleICU = TestUtil.toICUExtendedLocale(inLocale);
109             for (ULocale forULocale : ULocale.getAvailableLocales()) {
110                 if (forULocale.getScript().length() == 0) {
111                     continue;
112                 }
113                 Locale forLocale = forULocale.toLocale();
114                 String icuname = forULocale.getDisplayScript(inULocale);
115                 if (icuname.equals(forULocale.getScript()) || icuname.length() == 0) {
116                     continue;
117                 }
118
119                 String name = null;
120                 try {
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());
124                     continue;
125                 }
126
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);
132                     }
133                 } else {
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);
138                     }
139                     // Try explicit ICU locale (xx_yy_ICU)
140                     try {
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());
144                         continue;
145                     }
146                     if (!name.equals(icuname)) {
147                         errln("FAIL: Script name by ICU is " + icuname + ", but got " + name
148                                 + " for locale " + forLocale + " in locale " + inLocaleICU);
149                     }
150                 }
151             }
152         }
153     }
154
155     public void TestCountryNames() {
156         Locale[] locales = Locale.getAvailableLocales();
157         for (Locale inLocale : locales) {
158             if (TestUtil.isProblematicIBMLocale(inLocale)) {
159                 logln("Skipped " + inLocale);
160                 continue;
161             }
162
163             ULocale inULocale = ULocale.forLocale(inLocale);
164             Locale inLocaleICU = TestUtil.toICUExtendedLocale(inLocale);
165             for (Locale forLocale : locales) {
166                 if (forLocale.getCountry().length() == 0) {
167                     continue;
168                 }
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) {
173                     continue;
174                 }
175
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);
182                     }
183                 } else {
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);
188                     }
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);
194                     }
195                 }
196             }
197         }
198     }
199
200     public void TestVariantNames() {
201         Set<Locale> locales = new HashSet<Locale>();
202         for (Locale l : Locale.getAvailableLocales()) {
203             locales.add(l);
204         }
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"),
211         };
212         for (Locale l : additionalLocales) {
213             locales.add(l);
214         }
215
216         for (Locale inLocale : locales) {
217             if (TestUtil.isProblematicIBMLocale(inLocale)) {
218                 logln("Skipped " + inLocale);
219                 continue;
220             }
221
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) {
227                     continue;
228                 }
229                 // Note: JDK resolves a display name for each variant subtag
230                 String[] locVarSubtags = locVar.split("_");
231
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
235                         continue;
236                     }
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) {
241                         continue;
242                     }
243
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.
248                         continue;
249                     }
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);
255                         }
256                     } else {
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);
260                         }
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);
266                         }
267                     }
268                 }
269             }
270         }
271     }
272 }