LocaleTest.java revision 561ee011997c6c2f1befbfaa9d5f0a99771c1d63
1/*
2 *  Licensed to the Apache Software Foundation (ASF) under one or more
3 *  contributor license agreements.  See the NOTICE file distributed with
4 *  this work for additional information regarding copyright ownership.
5 *  The ASF licenses this file to You under the Apache License, Version 2.0
6 *  (the "License"); you may not use this file except in compliance with
7 *  the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  See the License for the specific language governing permissions and
15 *  limitations under the License.
16 */
17
18package org.apache.harmony.luni.tests.java.util;
19
20import java.util.Arrays;
21import java.util.HashSet;
22import java.util.List;
23import java.util.Locale;
24import java.util.Set;
25
26public class LocaleTest extends junit.framework.TestCase {
27
28	Locale testLocale;
29
30	Locale l;
31
32	Locale defaultLocale;
33
34	/**
35	 * @tests java.util.Locale#Locale(java.lang.String, java.lang.String)
36	 */
37	public void test_ConstructorLjava_lang_StringLjava_lang_String() {
38		// Test for method java.util.Locale(java.lang.String, java.lang.String)
39		Locale x = new Locale("xx", "CV");
40		assertTrue("Failed to create Locale", x.getCountry().equals("CV")
41				&& x.getVariant().equals(""));
42	}
43
44	/**
45	 * @tests java.util.Locale#Locale(java.lang.String, java.lang.String,
46	 *        java.lang.String)
47	 */
48	public void test_ConstructorLjava_lang_StringLjava_lang_StringLjava_lang_String() {
49		// Test for method java.util.Locale(java.lang.String, java.lang.String,
50		// java.lang.String)
51		Locale x = new Locale("xx", "CV", "ZZ");
52		assertTrue("Failed to create Locale", x.getLanguage().equals("xx")
53				&& (x.getCountry().equals("CV") && x.getVariant().equals("ZZ")));
54                try {
55                   new Locale(null, "CV", "ZZ");
56                   fail("expected NullPointerException with 1st parameter == null");
57                } catch(NullPointerException e) {
58                }
59
60                try {
61                   new Locale("xx", null, "ZZ");
62                   fail("expected NullPointerException with 2nd parameter == null");
63                } catch(NullPointerException e) {
64                }
65
66                try {
67                   new Locale("xx", "CV", null);
68                   fail("expected NullPointerException with 3rd parameter == null");
69                } catch(NullPointerException e) {
70                }
71	}
72
73	/**
74	 * @tests java.util.Locale#clone()
75	 */
76	public void test_clone() {
77		// Test for method java.lang.Object java.util.Locale.clone()
78		assertTrue("Clone failed", l.clone().equals(l));
79	}
80
81	/**
82	 * @tests java.util.Locale#equals(java.lang.Object)
83	 */
84	public void test_equalsLjava_lang_Object() {
85		// Test for method boolean java.util.Locale.equals(java.lang.Object)
86		Locale l2 = new Locale("en", "CA", "WIN32");
87		assertTrue("Same object returned false", testLocale.equals(testLocale));
88		assertTrue("Same values returned false", testLocale.equals(l2));
89		assertTrue("Different locales returned true", !testLocale.equals(l));
90
91	}
92
93	/**
94     * @tests java.util.Locale#getAvailableLocales()
95     */
96    public void test_getAvailableLocales() {
97        // Test for method java.util.Locale []
98        // java.util.Locale.getAvailableLocales()
99        // Assumes there will generally be about 100+ available locales...
100        Locale[] locales = Locale.getAvailableLocales();
101        assertTrue("Wrong number of locales: ", locales.length > 100);
102        // regression test for HARMONY-1514
103        // HashSet can filter duplicate locales
104        Set<Locale> localesSet = new HashSet<Locale>(Arrays.asList(locales));
105        // Non-bug difference for HARMONY-5442
106        assertTrue(localesSet.size() <= locales.length);
107    }
108
109	/**
110     * @tests java.util.Locale#getCountry()
111     */
112	public void test_getCountry() {
113		// Test for method java.lang.String java.util.Locale.getCountry()
114		assertTrue("Returned incorrect country: " + testLocale.getCountry(),
115				testLocale.getCountry().equals("CA"));
116	}
117
118	/**
119	 * @tests java.util.Locale#getDefault()
120	 */
121	public void test_getDefault() {
122		// Test for method java.util.Locale java.util.Locale.getDefault()
123		assertTrue("returns copy", Locale.getDefault() == Locale.getDefault());
124		Locale org = Locale.getDefault();
125		Locale.setDefault(l);
126		Locale x = Locale.getDefault();
127		Locale.setDefault(org);
128		assertEquals("Failed to get locale", "fr_CA_WIN32", x.toString());
129	}
130
131	/**
132	 * @tests java.util.Locale#getDisplayCountry()
133	 */
134	public void test_getDisplayCountry() {
135		// Test for method java.lang.String java.util.Locale.getDisplayCountry()
136		assertTrue("Returned incorrect country: "
137				+ testLocale.getDisplayCountry(), testLocale
138				.getDisplayCountry().equals("Canada"));
139
140        // Regression for Harmony-1146
141        // Non-bug difference for HARMONY-5442
142        Locale l_countryCD = new Locale("", "CD"); //$NON-NLS-1$ //$NON-NLS-2$
143                assertEquals("Congo - Kinshasa", //$NON-NLS-1$
144                        l_countryCD.getDisplayCountry());
145	}
146
147	/**
148	 * @tests java.util.Locale#getDisplayCountry(java.util.Locale)
149	 */
150	public void test_getDisplayCountryLjava_util_Locale() {
151		// Test for method java.lang.String
152		// java.util.Locale.getDisplayCountry(java.util.Locale)
153		assertEquals("Returned incorrect country", "Italie", Locale.ITALY
154				.getDisplayCountry(l));
155	}
156
157	/**
158	 * @tests java.util.Locale#getDisplayLanguage()
159	 */
160	public void test_getDisplayLanguage() {
161		// Test for method java.lang.String
162		// java.util.Locale.getDisplayLanguage()
163		assertTrue("Returned incorrect language: "
164				+ testLocale.getDisplayLanguage(), testLocale
165				.getDisplayLanguage().equals("English"));
166
167		// Regression for Harmony-1146
168        Locale l_languageAE = new Locale("ae", ""); //$NON-NLS-1$ //$NON-NLS-2$
169        assertEquals("Avestan", l_languageAE.getDisplayLanguage()); //$NON-NLS-1$
170
171        // Regression for HARMONY-4402
172        Locale defaultLocale = Locale.getDefault();
173        try {
174            Locale locale = new Locale("no", "NO");
175            Locale.setDefault(locale);
176            assertEquals("norsk", locale.getDisplayLanguage()); //$NON-NLS-1$
177        } finally {
178            Locale.setDefault(defaultLocale);
179        }
180	}
181
182	/**
183	 * @tests java.util.Locale#getDisplayLanguage(java.util.Locale)
184	 */
185	public void test_getDisplayLanguageLjava_util_Locale() {
186		// Test for method java.lang.String
187		// java.util.Locale.getDisplayLanguage(java.util.Locale)
188		assertTrue("Returned incorrect language: "
189				+ testLocale.getDisplayLanguage(l), testLocale
190				.getDisplayLanguage(l).equals("anglais"));
191	}
192
193	/**
194	 * @tests java.util.Locale#getDisplayName()
195	 */
196	public void test_getDisplayName() {
197		// Test for method java.lang.String java.util.Locale.getDisplayName()
198		assertTrue("Returned incorrect name: " + testLocale.getDisplayName(),
199				testLocale.getDisplayName().equals("English (Canada,WIN32)"));
200	}
201
202	/**
203	 * @tests java.util.Locale#getDisplayName(java.util.Locale)
204	 */
205	public void test_getDisplayNameLjava_util_Locale() {
206		// Test for method java.lang.String
207		// java.util.Locale.getDisplayName(java.util.Locale)
208		assertTrue("Returned incorrect name: " + testLocale.getDisplayName(l),
209				testLocale.getDisplayName(l).equals("anglais (Canada,WIN32)"));
210	}
211
212	/**
213	 * @tests java.util.Locale#getDisplayVariant()
214	 */
215	public void test_getDisplayVariant() {
216		// Test for method java.lang.String java.util.Locale.getDisplayVariant()
217		assertTrue("Returned incorrect variant: "
218				+ testLocale.getDisplayVariant(), testLocale
219				.getDisplayVariant().equals("WIN32"));
220	}
221
222	/**
223	 * @tests java.util.Locale#getDisplayVariant(java.util.Locale)
224	 */
225	public void test_getDisplayVariantLjava_util_Locale() {
226		// Test for method java.lang.String
227		// java.util.Locale.getDisplayVariant(java.util.Locale)
228		assertTrue("Returned incorrect variant: "
229				+ testLocale.getDisplayVariant(l), testLocale
230				.getDisplayVariant(l).equals("WIN32"));
231	}
232
233	/**
234	 * @tests java.util.Locale#getISO3Country()
235	 */
236	public void test_getISO3Country() {
237		// Test for method java.lang.String java.util.Locale.getISO3Country()
238		assertTrue("Returned incorrect ISO3 country: "
239				+ testLocale.getISO3Country(), testLocale.getISO3Country()
240				.equals("CAN"));
241
242        Locale l = new Locale("", "CD");
243        assertEquals("COD", l.getISO3Country());
244	}
245
246	/**
247	 * @tests java.util.Locale#getISO3Language()
248	 */
249	public void test_getISO3Language() {
250		// Test for method java.lang.String java.util.Locale.getISO3Language()
251		assertTrue("Returned incorrect ISO3 language: "
252				+ testLocale.getISO3Language(), testLocale.getISO3Language()
253				.equals("eng"));
254
255        Locale l = new Locale("ae");
256        assertEquals("ave", l.getISO3Language());
257
258        // Regression for Harmony-1146
259
260        // Non-bug difference for HARMONY-5442
261        Locale l_CountryCS = new Locale("", "CS"); //$NON-NLS-1$ //$NON-NLS-2$
262        assertEquals("SCG", l_CountryCS.getISO3Country()); //$NON-NLS-1$
263
264        // Regression for Harmony-1129
265        l = new Locale("ak", ""); //$NON-NLS-1$ //$NON-NLS-2$
266        assertEquals("aka", l.getISO3Language()); //$NON-NLS-1$
267	}
268
269	/**
270	 * @tests java.util.Locale#getISOCountries()
271	 */
272	public void test_getISOCountries() {
273		// Test for method java.lang.String []
274		// java.util.Locale.getISOCountries()
275		// Assumes all countries are 2 digits, and that there will always be
276		// 230 countries on the list...
277		String[] isoCountries = Locale.getISOCountries();
278		int length = isoCountries.length;
279		int familiarCount = 0;
280		for (int i = 0; i < length; i++) {
281			if (isoCountries[i].length() != 2) {
282				fail("Wrong format for ISOCountries.");
283			}
284			if (isoCountries[i].equals("CA") || isoCountries[i].equals("BB")
285					|| isoCountries[i].equals("US")
286					|| isoCountries[i].equals("KR"))
287				familiarCount++;
288		}
289		assertTrue("ISOCountries missing.", familiarCount == 4 && length > 230);
290	}
291
292	/**
293	 * @tests java.util.Locale#getISOLanguages()
294	 */
295	public void test_getISOLanguages() {
296		// Test for method java.lang.String []
297		// java.util.Locale.getISOLanguages()
298		// Assumes always at least 131 ISOlanguages...
299		String[] isoLang = Locale.getISOLanguages();
300		int length = isoLang.length;
301
302		// Non-bug difference for HARMONY-5442
303		assertTrue(isoLang[length / 2].length() == 3);
304		assertTrue(isoLang[length / 2].toLowerCase().equals(isoLang[length / 2]));
305		assertTrue("Wrong number of ISOLanguages.", length > 130);
306	}
307
308	/**
309	 * @tests java.util.Locale#getLanguage()
310	 */
311	public void test_getLanguage() {
312		// Test for method java.lang.String java.util.Locale.getLanguage()
313		assertTrue("Returned incorrect language: " + testLocale.getLanguage(),
314				testLocale.getLanguage().equals("en"));
315	}
316
317	/**
318	 * @tests java.util.Locale#getVariant()
319	 */
320	public void test_getVariant() {
321		// Test for method java.lang.String java.util.Locale.getVariant()
322		assertTrue("Returned incorrect variant: " + testLocale.getVariant(),
323				testLocale.getVariant().equals("WIN32"));
324	}
325
326	/**
327	 * @tests java.util.Locale#setDefault(java.util.Locale)
328	 */
329	public void test_setDefaultLjava_util_Locale() {
330		// Test for method void java.util.Locale.setDefault(java.util.Locale)
331
332		Locale org = Locale.getDefault();
333		Locale.setDefault(l);
334		Locale x = Locale.getDefault();
335		Locale.setDefault(org);
336		assertEquals("Failed to set locale", "fr_CA_WIN32", x.toString());
337
338		Locale.setDefault(new Locale("tr", ""));
339		String res1 = "\u0069".toUpperCase();
340		String res2 = "\u0049".toLowerCase();
341		Locale.setDefault(org);
342		assertEquals("Wrong toUppercase conversion", "\u0130", res1);
343		assertEquals("Wrong toLowercase conversion", "\u0131", res2);
344	}
345
346	/**
347	 * @tests java.util.Locale#toString()
348	 */
349	public void test_toString() {
350		// Test for method java.lang.String java.util.Locale.toString()
351		assertEquals("Returned incorrect string representation", "en_CA_WIN32", testLocale
352				.toString());
353
354		Locale l = new Locale("en", "");
355		assertEquals("Wrong representation 1", "en", l.toString());
356		l = new Locale("", "CA");
357		assertEquals("Wrong representation 2", "_CA", l.toString());
358
359		// Non-bug difference for HARMONY-5442
360		l = new Locale("", "CA", "var");
361		assertEquals("Wrong representation 2.5", "_CA_var", l.toString());
362		l = new Locale("en", "", "WIN");
363		assertEquals("Wrong representation 4", "en__WIN", l.toString());
364		l = new Locale("en", "CA");
365		assertEquals("Wrong representation 6", "en_CA", l.toString());
366		l = new Locale("en", "CA", "VAR");
367		assertEquals("Wrong representation 7", "en_CA_VAR", l.toString());
368
369        l = new Locale("", "", "var");
370        assertEquals("Wrong representation 8", "", l.toString());
371
372	}
373
374    // Regression Test for HARMONY-2953
375    public void test_getISO() {
376        Locale locale = new Locale("an");
377        assertEquals("arg", locale.getISO3Language());
378
379        locale = new Locale("PS");
380        assertEquals("pus", locale.getISO3Language());
381
382        List<String> languages = Arrays.asList(Locale.getISOLanguages());
383        assertTrue(languages.contains("ak"));
384
385		// Non-bug difference for HARMONY-5442
386        List<String> countries = Arrays.asList(Locale.getISOCountries());
387        assertFalse(countries.contains("CS"));
388    }
389
390    /**
391     * @tests {@value java.util.Locale#ROOT}
392     * @since 1.6
393     */
394    public void test_constantROOT() {
395        Locale root = Locale.ROOT;
396        assertEquals("", root.getLanguage());
397        assertEquals("", root.getCountry());
398        assertEquals("", root.getVariant());
399    }
400
401	/**
402	 * Sets up the fixture, for example, open a network connection. This method
403	 * is called before a test is executed.
404	 */
405	protected void setUp() {
406		defaultLocale = Locale.getDefault();
407		Locale.setDefault(Locale.US);
408		testLocale = new Locale("en", "CA", "WIN32");
409		l = new Locale("fr", "CA", "WIN32");
410	}
411
412	/**
413	 * Tears down the fixture, for example, close a network connection. This
414	 * method is called after a test is executed.
415	 */
416	protected void tearDown() {
417		Locale.setDefault(defaultLocale);
418	}
419}
420