001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.harmony.luni.tests.java.util;
019:
020: import java.util.Arrays;
021: import java.util.HashSet;
022: import java.util.List;
023: import java.util.Locale;
024: import java.util.Set;
025:
026: public class LocaleTest extends junit.framework.TestCase {
027:
028: Locale testLocale;
029:
030: Locale l;
031:
032: Locale defaultLocale;
033:
034: /**
035: * @tests java.util.Locale#Locale(java.lang.String, java.lang.String)
036: */
037: public void test_ConstructorLjava_lang_StringLjava_lang_String() {
038: // Test for method java.util.Locale(java.lang.String, java.lang.String)
039: Locale x = new Locale("xx", "CV");
040: assertTrue("Failed to create Locale", x.getCountry().equals(
041: "CV")
042: && x.getVariant().equals(""));
043: }
044:
045: /**
046: * @tests java.util.Locale#Locale(java.lang.String, java.lang.String,
047: * java.lang.String)
048: */
049: public void test_ConstructorLjava_lang_StringLjava_lang_StringLjava_lang_String() {
050: // Test for method java.util.Locale(java.lang.String, java.lang.String,
051: // java.lang.String)
052: Locale x = new Locale("xx", "CV", "ZZ");
053: assertTrue("Failed to create Locale", x.getLanguage().equals(
054: "xx")
055: && (x.getCountry().equals("CV") && x.getVariant()
056: .equals("ZZ")));
057: try {
058: new Locale(null, "CV", "ZZ");
059: fail("expected NullPointerException with 1st parameter == null");
060: } catch (NullPointerException e) {
061: }
062:
063: try {
064: new Locale("xx", null, "ZZ");
065: fail("expected NullPointerException with 2nd parameter == null");
066: } catch (NullPointerException e) {
067: }
068:
069: try {
070: new Locale("xx", "CV", null);
071: fail("expected NullPointerException with 3rd parameter == null");
072: } catch (NullPointerException e) {
073: }
074: }
075:
076: /**
077: * @tests java.util.Locale#clone()
078: */
079: public void test_clone() {
080: // Test for method java.lang.Object java.util.Locale.clone()
081: assertTrue("Clone failed", l.clone().equals(l));
082: }
083:
084: /**
085: * @tests java.util.Locale#equals(java.lang.Object)
086: */
087: public void test_equalsLjava_lang_Object() {
088: // Test for method boolean java.util.Locale.equals(java.lang.Object)
089: Locale l2 = new Locale("en", "CA", "WIN32");
090: assertTrue("Same object returned false", testLocale
091: .equals(testLocale));
092: assertTrue("Same values returned false", testLocale.equals(l2));
093: assertTrue("Different locales returned true", !testLocale
094: .equals(l));
095:
096: }
097:
098: /**
099: * @tests java.util.Locale#getAvailableLocales()
100: */
101: public void test_getAvailableLocales() {
102: // Test for method java.util.Locale []
103: // java.util.Locale.getAvailableLocales()
104: // Assumes there will generally be about 100+ available locales...
105: Locale[] locales = Locale.getAvailableLocales();
106: assertTrue("Wrong number of locales: ", locales.length > 100);
107: // regression test for HARMONY-1514
108: // HashSet can filter duplicate locales
109: Set<Locale> localesSet = new HashSet<Locale>(Arrays
110: .asList(locales));
111: // Non-bug difference for HARMONY-5442
112: assertTrue(localesSet.size() <= locales.length);
113: }
114:
115: /**
116: * @tests java.util.Locale#getCountry()
117: */
118: public void test_getCountry() {
119: // Test for method java.lang.String java.util.Locale.getCountry()
120: assertTrue("Returned incorrect country: "
121: + testLocale.getCountry(), testLocale.getCountry()
122: .equals("CA"));
123: }
124:
125: /**
126: * @tests java.util.Locale#getDefault()
127: */
128: public void test_getDefault() {
129: // Test for method java.util.Locale java.util.Locale.getDefault()
130: assertTrue("returns copy", Locale.getDefault() == Locale
131: .getDefault());
132: Locale org = Locale.getDefault();
133: Locale.setDefault(l);
134: Locale x = Locale.getDefault();
135: Locale.setDefault(org);
136: assertEquals("Failed to get locale", "fr_CA_WIN32", x
137: .toString());
138: }
139:
140: /**
141: * @tests java.util.Locale#getDisplayCountry()
142: */
143: public void test_getDisplayCountry() {
144: // Test for method java.lang.String java.util.Locale.getDisplayCountry()
145: assertTrue("Returned incorrect country: "
146: + testLocale.getDisplayCountry(), testLocale
147: .getDisplayCountry().equals("Canada"));
148:
149: // Regression for Harmony-1146
150: // Non-bug difference for HARMONY-5442
151: Locale l_countryCD = new Locale("", "CD"); //$NON-NLS-1$ //$NON-NLS-2$
152: assertEquals("Congo - Kinshasa", //$NON-NLS-1$
153: l_countryCD.getDisplayCountry());
154: }
155:
156: /**
157: * @tests java.util.Locale#getDisplayCountry(java.util.Locale)
158: */
159: public void test_getDisplayCountryLjava_util_Locale() {
160: // Test for method java.lang.String
161: // java.util.Locale.getDisplayCountry(java.util.Locale)
162: assertEquals("Returned incorrect country", "Italie",
163: Locale.ITALY.getDisplayCountry(l));
164: }
165:
166: /**
167: * @tests java.util.Locale#getDisplayLanguage()
168: */
169: public void test_getDisplayLanguage() {
170: // Test for method java.lang.String
171: // java.util.Locale.getDisplayLanguage()
172: assertTrue("Returned incorrect language: "
173: + testLocale.getDisplayLanguage(), testLocale
174: .getDisplayLanguage().equals("English"));
175:
176: // Regression for Harmony-1146
177: Locale l_languageAE = new Locale("ae", ""); //$NON-NLS-1$ //$NON-NLS-2$
178: assertEquals("Avestan", l_languageAE.getDisplayLanguage()); //$NON-NLS-1$
179:
180: // Regression for HARMONY-4402
181: Locale defaultLocale = Locale.getDefault();
182: try {
183: Locale locale = new Locale("no", "NO");
184: Locale.setDefault(locale);
185: assertEquals("norsk", locale.getDisplayLanguage()); //$NON-NLS-1$
186: } finally {
187: Locale.setDefault(defaultLocale);
188: }
189: }
190:
191: /**
192: * @tests java.util.Locale#getDisplayLanguage(java.util.Locale)
193: */
194: public void test_getDisplayLanguageLjava_util_Locale() {
195: // Test for method java.lang.String
196: // java.util.Locale.getDisplayLanguage(java.util.Locale)
197: assertTrue("Returned incorrect language: "
198: + testLocale.getDisplayLanguage(l), testLocale
199: .getDisplayLanguage(l).equals("anglais"));
200: }
201:
202: /**
203: * @tests java.util.Locale#getDisplayName()
204: */
205: public void test_getDisplayName() {
206: // Test for method java.lang.String java.util.Locale.getDisplayName()
207: assertTrue("Returned incorrect name: "
208: + testLocale.getDisplayName(), testLocale
209: .getDisplayName().equals("English (Canada,WIN32)"));
210: }
211:
212: /**
213: * @tests java.util.Locale#getDisplayName(java.util.Locale)
214: */
215: public void test_getDisplayNameLjava_util_Locale() {
216: // Test for method java.lang.String
217: // java.util.Locale.getDisplayName(java.util.Locale)
218: assertTrue("Returned incorrect name: "
219: + testLocale.getDisplayName(l), testLocale
220: .getDisplayName(l).equals("anglais (Canada,WIN32)"));
221: }
222:
223: /**
224: * @tests java.util.Locale#getDisplayVariant()
225: */
226: public void test_getDisplayVariant() {
227: // Test for method java.lang.String java.util.Locale.getDisplayVariant()
228: assertTrue("Returned incorrect variant: "
229: + testLocale.getDisplayVariant(), testLocale
230: .getDisplayVariant().equals("WIN32"));
231: }
232:
233: /**
234: * @tests java.util.Locale#getDisplayVariant(java.util.Locale)
235: */
236: public void test_getDisplayVariantLjava_util_Locale() {
237: // Test for method java.lang.String
238: // java.util.Locale.getDisplayVariant(java.util.Locale)
239: assertTrue("Returned incorrect variant: "
240: + testLocale.getDisplayVariant(l), testLocale
241: .getDisplayVariant(l).equals("WIN32"));
242: }
243:
244: /**
245: * @tests java.util.Locale#getISO3Country()
246: */
247: public void test_getISO3Country() {
248: // Test for method java.lang.String java.util.Locale.getISO3Country()
249: assertTrue("Returned incorrect ISO3 country: "
250: + testLocale.getISO3Country(), testLocale
251: .getISO3Country().equals("CAN"));
252:
253: Locale l = new Locale("", "CD");
254: assertEquals("COD", l.getISO3Country());
255: }
256:
257: /**
258: * @tests java.util.Locale#getISO3Language()
259: */
260: public void test_getISO3Language() {
261: // Test for method java.lang.String java.util.Locale.getISO3Language()
262: assertTrue("Returned incorrect ISO3 language: "
263: + testLocale.getISO3Language(), testLocale
264: .getISO3Language().equals("eng"));
265:
266: Locale l = new Locale("ae");
267: assertEquals("ave", l.getISO3Language());
268:
269: // Regression for Harmony-1146
270:
271: // Non-bug difference for HARMONY-5442
272: Locale l_CountryCS = new Locale("", "CS"); //$NON-NLS-1$ //$NON-NLS-2$
273: assertEquals("", l_CountryCS.getISO3Country()); //$NON-NLS-1$
274:
275: // Regression for Harmony-1129
276: l = new Locale("ak", ""); //$NON-NLS-1$ //$NON-NLS-2$
277: assertEquals("aka", l.getISO3Language()); //$NON-NLS-1$
278: }
279:
280: /**
281: * @tests java.util.Locale#getISOCountries()
282: */
283: public void test_getISOCountries() {
284: // Test for method java.lang.String []
285: // java.util.Locale.getISOCountries()
286: // Assumes all countries are 2 digits, and that there will always be
287: // 230 countries on the list...
288: String[] isoCountries = Locale.getISOCountries();
289: int length = isoCountries.length;
290: int familiarCount = 0;
291: for (int i = 0; i < length; i++) {
292: if (isoCountries[i].length() != 2) {
293: fail("Wrong format for ISOCountries.");
294: }
295: if (isoCountries[i].equals("CA")
296: || isoCountries[i].equals("BB")
297: || isoCountries[i].equals("US")
298: || isoCountries[i].equals("KR"))
299: familiarCount++;
300: }
301: assertTrue("ISOCountries missing.", familiarCount == 4
302: && length > 230);
303: }
304:
305: /**
306: * @tests java.util.Locale#getISOLanguages()
307: */
308: public void test_getISOLanguages() {
309: // Test for method java.lang.String []
310: // java.util.Locale.getISOLanguages()
311: // Assumes always at least 131 ISOlanguages...
312: String[] isoLang = Locale.getISOLanguages();
313: int length = isoLang.length;
314:
315: // Non-bug difference for HARMONY-5442
316: assertTrue(isoLang[length / 2].length() == 3);
317: assertTrue(isoLang[length / 2].toLowerCase().equals(
318: isoLang[length / 2]));
319: assertTrue("Wrong number of ISOLanguages.", length > 130);
320: }
321:
322: /**
323: * @tests java.util.Locale#getLanguage()
324: */
325: public void test_getLanguage() {
326: // Test for method java.lang.String java.util.Locale.getLanguage()
327: assertTrue("Returned incorrect language: "
328: + testLocale.getLanguage(), testLocale.getLanguage()
329: .equals("en"));
330: }
331:
332: /**
333: * @tests java.util.Locale#getVariant()
334: */
335: public void test_getVariant() {
336: // Test for method java.lang.String java.util.Locale.getVariant()
337: assertTrue("Returned incorrect variant: "
338: + testLocale.getVariant(), testLocale.getVariant()
339: .equals("WIN32"));
340: }
341:
342: /**
343: * @tests java.util.Locale#setDefault(java.util.Locale)
344: */
345: public void test_setDefaultLjava_util_Locale() {
346: // Test for method void java.util.Locale.setDefault(java.util.Locale)
347:
348: Locale org = Locale.getDefault();
349: Locale.setDefault(l);
350: Locale x = Locale.getDefault();
351: Locale.setDefault(org);
352: assertEquals("Failed to set locale", "fr_CA_WIN32", x
353: .toString());
354:
355: Locale.setDefault(new Locale("tr", ""));
356: String res1 = "\u0069".toUpperCase();
357: String res2 = "\u0049".toLowerCase();
358: Locale.setDefault(org);
359: assertEquals("Wrong toUppercase conversion", "\u0130", res1);
360: assertEquals("Wrong toLowercase conversion", "\u0131", res2);
361: }
362:
363: /**
364: * @tests java.util.Locale#toString()
365: */
366: public void test_toString() {
367: // Test for method java.lang.String java.util.Locale.toString()
368: assertEquals("Returned incorrect string representation",
369: "en_CA_WIN32", testLocale.toString());
370:
371: Locale l = new Locale("en", "");
372: assertEquals("Wrong representation 1", "en", l.toString());
373: l = new Locale("", "CA");
374: assertEquals("Wrong representation 2", "_CA", l.toString());
375:
376: // Non-bug difference for HARMONY-5442
377: l = new Locale("", "CA", "var");
378: assertEquals("Wrong representation 2.5", "_CA_var", l
379: .toString());
380: l = new Locale("en", "", "WIN");
381: assertEquals("Wrong representation 4", "en__WIN", l.toString());
382: l = new Locale("en", "CA");
383: assertEquals("Wrong representation 6", "en_CA", l.toString());
384: l = new Locale("en", "CA", "VAR");
385: assertEquals("Wrong representation 7", "en_CA_VAR", l
386: .toString());
387:
388: l = new Locale("", "", "var");
389: assertEquals("Wrong representation 8", "", l.toString());
390:
391: }
392:
393: // Regression Test for HARMONY-2953
394: public void test_getISO() {
395: Locale locale = new Locale("an");
396: assertEquals("arg", locale.getISO3Language());
397:
398: locale = new Locale("PS");
399: assertEquals("pus", locale.getISO3Language());
400:
401: List<String> languages = Arrays
402: .asList(Locale.getISOLanguages());
403: assertTrue(languages.contains("ak"));
404:
405: // Non-bug difference for HARMONY-5442
406: List<String> countries = Arrays
407: .asList(Locale.getISOCountries());
408: assertFalse(countries.contains("CS"));
409: }
410:
411: /**
412: * Sets up the fixture, for example, open a network connection. This method
413: * is called before a test is executed.
414: */
415: protected void setUp() {
416: defaultLocale = Locale.getDefault();
417: Locale.setDefault(Locale.US);
418: testLocale = new Locale("en", "CA", "WIN32");
419: l = new Locale("fr", "CA", "WIN32");
420: }
421:
422: /**
423: * Tears down the fixture, for example, close a network connection. This
424: * method is called after a test is executed.
425: */
426: protected void tearDown() {
427: Locale.setDefault(defaultLocale);
428: }
429: }
|