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: package org.apache.commons.lang;
018:
019: import java.lang.reflect.Constructor;
020: import java.lang.reflect.Modifier;
021: import java.util.Arrays;
022: import java.util.Collection;
023: import java.util.HashSet;
024: import java.util.Iterator;
025: import java.util.List;
026: import java.util.Locale;
027: import java.util.Set;
028:
029: import junit.framework.Test;
030: import junit.framework.TestCase;
031: import junit.framework.TestSuite;
032: import junit.textui.TestRunner;
033:
034: /**
035: * Unit tests for {@link LocaleUtils}.
036: *
037: * @author Chris Hyzer
038: * @author Stephen Colebourne
039: * @version $Id: LocaleUtilsTest.java 489749 2006-12-22 20:34:37Z bayard $
040: */
041: public class LocaleUtilsTest extends TestCase {
042:
043: private static final Locale LOCALE_EN = new Locale("en", "");
044: private static final Locale LOCALE_EN_US = new Locale("en", "US");
045: private static final Locale LOCALE_EN_US_ZZZZ = new Locale("en",
046: "US", "ZZZZ");
047: private static final Locale LOCALE_FR = new Locale("fr", "");
048: private static final Locale LOCALE_FR_CA = new Locale("fr", "CA");
049: private static final Locale LOCALE_QQ = new Locale("qq", "");
050: private static final Locale LOCALE_QQ_ZZ = new Locale("qq", "ZZ");
051:
052: /**
053: * Constructor.
054: *
055: * @param name
056: */
057: public LocaleUtilsTest(String name) {
058: super (name);
059: }
060:
061: /**
062: * Main.
063: * @param args
064: */
065: public static void main(String[] args) {
066: TestRunner.run(suite());
067: }
068:
069: /**
070: * Run the test cases as a suite.
071: * @return the Test
072: */
073: public static Test suite() {
074: TestSuite suite = new TestSuite(LocaleUtilsTest.class);
075: suite.setName("LocaleUtilsTest Tests");
076: return suite;
077: }
078:
079: public void setUp() throws Exception {
080: super .setUp();
081:
082: // Testing #LANG-304. Must be called before availableLocaleSet is called.
083: LocaleUtils.isAvailableLocale(Locale.getDefault());
084: }
085:
086: //-----------------------------------------------------------------------
087: /**
088: * Test that constructors are public, and work, etc.
089: */
090: public void testConstructor() {
091: assertNotNull(new LocaleUtils());
092: Constructor[] cons = LocaleUtils.class
093: .getDeclaredConstructors();
094: assertEquals(1, cons.length);
095: assertEquals(true, Modifier.isPublic(cons[0].getModifiers()));
096: assertEquals(true, Modifier.isPublic(LocaleUtils.class
097: .getModifiers()));
098: assertEquals(false, Modifier.isFinal(LocaleUtils.class
099: .getModifiers()));
100: }
101:
102: //-----------------------------------------------------------------------
103: /**
104: * Pass in a valid language, test toLocale.
105: *
106: * @param language the language string
107: */
108: private void assertValidToLocale(String language) {
109: Locale locale = LocaleUtils.toLocale(language);
110: assertNotNull("valid locale", locale);
111: assertEquals(language, locale.getLanguage());
112: //country and variant are empty
113: assertTrue(locale.getCountry() == null
114: || locale.getCountry().length() == 0);
115: assertTrue(locale.getVariant() == null
116: || locale.getVariant().length() == 0);
117: }
118:
119: /**
120: * Pass in a valid language, test toLocale.
121: *
122: * @param localeString to pass to toLocale()
123: * @param language of the resulting Locale
124: * @param country of the resulting Locale
125: */
126: private void assertValidToLocale(String localeString,
127: String language, String country) {
128: Locale locale = LocaleUtils.toLocale(localeString);
129: assertNotNull("valid locale", locale);
130: assertEquals(language, locale.getLanguage());
131: assertEquals(country, locale.getCountry());
132: //variant is empty
133: assertTrue(locale.getVariant() == null
134: || locale.getVariant().length() == 0);
135: }
136:
137: /**
138: * Pass in a valid language, test toLocale.
139: *
140: * @param localeString to pass to toLocale()
141: * @param language of the resulting Locale
142: * @param country of the resulting Locale
143: * @param variant of the resulting Locale
144: */
145: private void assertValidToLocale(String localeString,
146: String language, String country, String variant) {
147: Locale locale = LocaleUtils.toLocale(localeString);
148: assertNotNull("valid locale", locale);
149: assertEquals(language, locale.getLanguage());
150: assertEquals(country, locale.getCountry());
151: assertEquals(variant, locale.getVariant());
152:
153: }
154:
155: /**
156: * Test toLocale() method.
157: */
158: public void testToLocale_1Part() {
159: assertEquals(null, LocaleUtils.toLocale((String) null));
160:
161: assertValidToLocale("us");
162: assertValidToLocale("fr");
163: assertValidToLocale("de");
164: assertValidToLocale("zh");
165: // Valid format but lang doesnt exist, should make instance anyway
166: assertValidToLocale("qq");
167:
168: try {
169: LocaleUtils.toLocale("Us");
170: fail("Should fail if not lowercase");
171: } catch (IllegalArgumentException iae) {
172: }
173: try {
174: LocaleUtils.toLocale("US");
175: fail("Should fail if not lowercase");
176: } catch (IllegalArgumentException iae) {
177: }
178: try {
179: LocaleUtils.toLocale("uS");
180: fail("Should fail if not lowercase");
181: } catch (IllegalArgumentException iae) {
182: }
183: try {
184: LocaleUtils.toLocale("u#");
185: fail("Should fail if not lowercase");
186: } catch (IllegalArgumentException iae) {
187: }
188:
189: try {
190: LocaleUtils.toLocale("u");
191: fail("Must be 2 chars if less than 5");
192: } catch (IllegalArgumentException iae) {
193: }
194:
195: try {
196: LocaleUtils.toLocale("uuu");
197: fail("Must be 2 chars if less than 5");
198: } catch (IllegalArgumentException iae) {
199: }
200:
201: try {
202: LocaleUtils.toLocale("uu_U");
203: fail("Must be 2 chars if less than 5");
204: } catch (IllegalArgumentException iae) {
205: }
206: }
207:
208: /**
209: * Test toLocale() method.
210: */
211: public void testToLocale_2Part() {
212: assertValidToLocale("us_EN", "us", "EN");
213: //valid though doesnt exist
214: assertValidToLocale("us_ZH", "us", "ZH");
215:
216: try {
217: LocaleUtils.toLocale("us-EN");
218: fail("Should fail as not underscore");
219: } catch (IllegalArgumentException iae) {
220: }
221: try {
222: LocaleUtils.toLocale("us_En");
223: fail("Should fail second part not uppercase");
224: } catch (IllegalArgumentException iae) {
225: }
226: try {
227: LocaleUtils.toLocale("us_en");
228: fail("Should fail second part not uppercase");
229: } catch (IllegalArgumentException iae) {
230: }
231: try {
232: LocaleUtils.toLocale("us_eN");
233: fail("Should fail second part not uppercase");
234: } catch (IllegalArgumentException iae) {
235: }
236: try {
237: LocaleUtils.toLocale("uS_EN");
238: fail("Should fail first part not lowercase");
239: } catch (IllegalArgumentException iae) {
240: }
241: try {
242: LocaleUtils.toLocale("us_E3");
243: fail("Should fail second part not uppercase");
244: } catch (IllegalArgumentException iae) {
245: }
246: }
247:
248: /**
249: * Test toLocale() method.
250: */
251: public void testToLocale_3Part() {
252: assertValidToLocale("us_EN_A", "us", "EN", "A");
253: // this isn't pretty, but was caused by a jdk bug it seems
254: // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4210525
255: if (SystemUtils.isJavaVersionAtLeast(1.4f)) {
256: assertValidToLocale("us_EN_a", "us", "EN", "a");
257: assertValidToLocale("us_EN_SFsafdFDsdfF", "us", "EN",
258: "SFsafdFDsdfF");
259: } else {
260: assertValidToLocale("us_EN_a", "us", "EN", "A");
261: assertValidToLocale("us_EN_SFsafdFDsdfF", "us", "EN",
262: "SFSAFDFDSDFF");
263: }
264:
265: try {
266: LocaleUtils.toLocale("us_EN-a");
267: fail("Should fail as not underscore");
268: } catch (IllegalArgumentException iae) {
269: }
270: try {
271: LocaleUtils.toLocale("uu_UU_");
272: fail("Must be 3, 5 or 7+ in length");
273: } catch (IllegalArgumentException iae) {
274: }
275: }
276:
277: //-----------------------------------------------------------------------
278: /**
279: * Helper method for local lookups.
280: *
281: * @param locale the input locale
282: * @param defaultLocale the input default locale
283: * @param expected expected results
284: */
285: private void assertLocaleLookupList(Locale locale,
286: Locale defaultLocale, Locale[] expected) {
287: List localeList = defaultLocale == null ? LocaleUtils
288: .localeLookupList(locale) : LocaleUtils
289: .localeLookupList(locale, defaultLocale);
290:
291: assertEquals(expected.length, localeList.size());
292: assertEquals(Arrays.asList(expected), localeList);
293: assertUnmodifiableCollection(localeList);
294: }
295:
296: //-----------------------------------------------------------------------
297: /**
298: * Test localeLookupList() method.
299: */
300: public void testLocaleLookupList_Locale() {
301: assertLocaleLookupList(null, null, new Locale[0]);
302: assertLocaleLookupList(LOCALE_QQ, null,
303: new Locale[] { LOCALE_QQ });
304: assertLocaleLookupList(LOCALE_EN, null,
305: new Locale[] { LOCALE_EN });
306: assertLocaleLookupList(LOCALE_EN, null,
307: new Locale[] { LOCALE_EN });
308: assertLocaleLookupList(LOCALE_EN_US, null, new Locale[] {
309: LOCALE_EN_US, LOCALE_EN });
310: assertLocaleLookupList(LOCALE_EN_US_ZZZZ, null, new Locale[] {
311: LOCALE_EN_US_ZZZZ, LOCALE_EN_US, LOCALE_EN });
312: }
313:
314: /**
315: * Test localeLookupList() method.
316: */
317: public void testLocaleLookupList_LocaleLocale() {
318: assertLocaleLookupList(LOCALE_QQ, LOCALE_QQ,
319: new Locale[] { LOCALE_QQ });
320: assertLocaleLookupList(LOCALE_EN, LOCALE_EN,
321: new Locale[] { LOCALE_EN });
322:
323: assertLocaleLookupList(LOCALE_EN_US, LOCALE_EN_US,
324: new Locale[] { LOCALE_EN_US, LOCALE_EN });
325: assertLocaleLookupList(LOCALE_EN_US, LOCALE_QQ, new Locale[] {
326: LOCALE_EN_US, LOCALE_EN, LOCALE_QQ });
327: assertLocaleLookupList(LOCALE_EN_US, LOCALE_QQ_ZZ,
328: new Locale[] { LOCALE_EN_US, LOCALE_EN, LOCALE_QQ_ZZ });
329:
330: assertLocaleLookupList(LOCALE_EN_US_ZZZZ, null, new Locale[] {
331: LOCALE_EN_US_ZZZZ, LOCALE_EN_US, LOCALE_EN });
332: assertLocaleLookupList(LOCALE_EN_US_ZZZZ, LOCALE_EN_US_ZZZZ,
333: new Locale[] { LOCALE_EN_US_ZZZZ, LOCALE_EN_US,
334: LOCALE_EN });
335: assertLocaleLookupList(LOCALE_EN_US_ZZZZ, LOCALE_QQ,
336: new Locale[] { LOCALE_EN_US_ZZZZ, LOCALE_EN_US,
337: LOCALE_EN, LOCALE_QQ });
338: assertLocaleLookupList(LOCALE_EN_US_ZZZZ, LOCALE_QQ_ZZ,
339: new Locale[] { LOCALE_EN_US_ZZZZ, LOCALE_EN_US,
340: LOCALE_EN, LOCALE_QQ_ZZ });
341: assertLocaleLookupList(LOCALE_FR_CA, LOCALE_EN, new Locale[] {
342: LOCALE_FR_CA, LOCALE_FR, LOCALE_EN });
343: }
344:
345: //-----------------------------------------------------------------------
346: /**
347: * Test availableLocaleList() method.
348: */
349: public void testAvailableLocaleList() {
350: List list = LocaleUtils.availableLocaleList();
351: List list2 = LocaleUtils.availableLocaleList();
352: assertNotNull(list);
353: assertSame(list, list2);
354: assertUnmodifiableCollection(list);
355:
356: Locale[] jdkLocaleArray = Locale.getAvailableLocales();
357: List jdkLocaleList = Arrays.asList(jdkLocaleArray);
358: assertEquals(jdkLocaleList, list);
359: }
360:
361: //-----------------------------------------------------------------------
362: /**
363: * Test availableLocaleSet() method.
364: */
365: public void testAvailableLocaleSet() {
366: Set set = LocaleUtils.availableLocaleSet();
367: Set set2 = LocaleUtils.availableLocaleSet();
368: assertNotNull(set);
369: assertSame(set, set2);
370: assertUnmodifiableCollection(set);
371:
372: Locale[] jdkLocaleArray = Locale.getAvailableLocales();
373: List jdkLocaleList = Arrays.asList(jdkLocaleArray);
374: Set jdkLocaleSet = new HashSet(jdkLocaleList);
375: assertEquals(jdkLocaleSet, set);
376: }
377:
378: //-----------------------------------------------------------------------
379: /**
380: * Test availableLocaleSet() method.
381: */
382: public void testIsAvailableLocale() {
383: Set set = LocaleUtils.availableLocaleSet();
384: assertEquals(set.contains(LOCALE_EN), LocaleUtils
385: .isAvailableLocale(LOCALE_EN));
386: assertEquals(set.contains(LOCALE_EN_US), LocaleUtils
387: .isAvailableLocale(LOCALE_EN_US));
388: assertEquals(set.contains(LOCALE_EN_US_ZZZZ), LocaleUtils
389: .isAvailableLocale(LOCALE_EN_US_ZZZZ));
390: assertEquals(set.contains(LOCALE_FR), LocaleUtils
391: .isAvailableLocale(LOCALE_FR));
392: assertEquals(set.contains(LOCALE_FR_CA), LocaleUtils
393: .isAvailableLocale(LOCALE_FR_CA));
394: assertEquals(set.contains(LOCALE_QQ), LocaleUtils
395: .isAvailableLocale(LOCALE_QQ));
396: assertEquals(set.contains(LOCALE_QQ_ZZ), LocaleUtils
397: .isAvailableLocale(LOCALE_QQ_ZZ));
398: }
399:
400: //-----------------------------------------------------------------------
401: /**
402: * Make sure the language by country is correct.
403: *
404: * @param country
405: * @param languages array of languages that should be returned
406: */
407: private void assertLanguageByCountry(String country,
408: String[] languages) {
409: List list = LocaleUtils.languagesByCountry(country);
410: List list2 = LocaleUtils.languagesByCountry(country);
411: assertNotNull(list);
412: assertSame(list, list2);
413: assertEquals(languages.length, list.size());
414: //search through langauges
415: for (int i = 0; i < languages.length; i++) {
416: Iterator iterator = list.iterator();
417: boolean found = false;
418: // see if it was returned by the set
419: while (iterator.hasNext()) {
420: Locale locale = (Locale) iterator.next();
421: // should have an en empty variant
422: assertTrue(locale.getVariant() == null
423: || locale.getVariant().length() == 0);
424: assertEquals(country, locale.getCountry());
425: if (languages[i].equals(locale.getLanguage())) {
426: found = true;
427: break;
428: }
429: }
430: if (!found) {
431: fail("Cound not find language: " + languages[i]
432: + " for country: " + country);
433: }
434: }
435: assertUnmodifiableCollection(list);
436: }
437:
438: /**
439: * Test languagesByCountry() method.
440: */
441: public void testLanguagesByCountry() {
442: assertLanguageByCountry(null, new String[0]);
443: assertLanguageByCountry("GB", new String[] { "en" });
444: assertLanguageByCountry("ZZ", new String[0]);
445: assertLanguageByCountry("CH", new String[] { "fr", "de", "it" });
446: }
447:
448: //-----------------------------------------------------------------------
449: /**
450: * Make sure the language by country is correct.
451: *
452: * @param language
453: * @param countries array of countries that should be returned
454: */
455: private void assertCountriesByLanguage(String language,
456: String[] countries) {
457: List list = LocaleUtils.countriesByLanguage(language);
458: List list2 = LocaleUtils.countriesByLanguage(language);
459: assertNotNull(list);
460: assertSame(list, list2);
461: assertEquals(countries.length, list.size());
462: //search through langauges
463: for (int i = 0; i < countries.length; i++) {
464: Iterator iterator = list.iterator();
465: boolean found = false;
466: // see if it was returned by the set
467: while (iterator.hasNext()) {
468: Locale locale = (Locale) iterator.next();
469: // should have an en empty variant
470: assertTrue(locale.getVariant() == null
471: || locale.getVariant().length() == 0);
472: assertEquals(language, locale.getLanguage());
473: if (countries[i].equals(locale.getCountry())) {
474: found = true;
475: break;
476: }
477: }
478: if (!found) {
479: fail("Cound not find language: " + countries[i]
480: + " for country: " + language);
481: }
482: }
483: assertUnmodifiableCollection(list);
484: }
485:
486: /**
487: * Test languagesByCountry() method.
488: */
489: public void testCountriesByLanguage() {
490: assertCountriesByLanguage(null, new String[0]);
491: assertCountriesByLanguage("de", new String[] { "DE", "CH",
492: "AT", "LU" });
493: assertCountriesByLanguage("zz", new String[0]);
494: assertCountriesByLanguage("it", new String[] { "IT", "CH" });
495: }
496:
497: /**
498: * @param coll the collection to check
499: */
500: private static void assertUnmodifiableCollection(Collection coll) {
501: try {
502: coll.add("Unmodifiable");
503: fail();
504: } catch (UnsupportedOperationException ex) {
505: }
506: }
507:
508: }
|