• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 package org.apache.commons.lang3;
18 
19 import static org.apache.commons.lang3.JavaVersion.JAVA_1_4;
20 import static org.junit.jupiter.api.Assertions.assertEquals;
21 import static org.junit.jupiter.api.Assertions.assertFalse;
22 import static org.junit.jupiter.api.Assertions.assertNotNull;
23 import static org.junit.jupiter.api.Assertions.assertNull;
24 import static org.junit.jupiter.api.Assertions.assertSame;
25 import static org.junit.jupiter.api.Assertions.assertThrows;
26 import static org.junit.jupiter.api.Assertions.assertTrue;
27 
28 import java.lang.reflect.Constructor;
29 import java.lang.reflect.Modifier;
30 import java.util.Arrays;
31 import java.util.Collection;
32 import java.util.HashSet;
33 import java.util.Iterator;
34 import java.util.List;
35 import java.util.Locale;
36 import java.util.Set;
37 
38 import org.junit.jupiter.api.BeforeEach;
39 import org.junit.jupiter.api.Test;
40 import org.junit.jupiter.params.ParameterizedTest;
41 import org.junit.jupiter.params.provider.MethodSource;
42 
43 /**
44  * Unit tests for {@link LocaleUtils}.
45  */
46 public class LocaleUtilsTest extends AbstractLangTest {
47 
48     private static final Locale LOCALE_EN = new Locale("en", "");
49     private static final Locale LOCALE_EN_US = new Locale("en", "US");
50     private static final Locale LOCALE_EN_US_ZZZZ = new Locale("en", "US", "ZZZZ");
51     private static final Locale LOCALE_FR = new Locale("fr", "");
52     private static final Locale LOCALE_FR_CA = new Locale("fr", "CA");
53     private static final Locale LOCALE_QQ = new Locale("qq", "");
54     private static final Locale LOCALE_QQ_ZZ = new Locale("qq", "ZZ");
55 
56     @BeforeEach
setUp()57     public void setUp() {
58         // Testing #LANG-304. Must be called before availableLocaleSet is called.
59         LocaleUtils.isAvailableLocale(Locale.getDefault());
60     }
61 
62     /**
63      * Test that constructors are public, and work, etc.
64      */
65     @Test
testConstructor()66     public void testConstructor() {
67         assertNotNull(new LocaleUtils());
68         final Constructor<?>[] cons = LocaleUtils.class.getDeclaredConstructors();
69         assertEquals(1, cons.length);
70         assertTrue(Modifier.isPublic(cons[0].getModifiers()));
71         assertTrue(Modifier.isPublic(LocaleUtils.class.getModifiers()));
72         assertFalse(Modifier.isFinal(LocaleUtils.class.getModifiers()));
73     }
74 
75     /**
76      * Pass in a valid language, test toLocale.
77      *
78      * @param language  the language string
79      */
assertValidToLocale(final String language)80     private static void assertValidToLocale(final String language) {
81         final Locale locale = LocaleUtils.toLocale(language);
82         assertNotNull(locale, "valid locale");
83         assertEquals(language, locale.getLanguage());
84         //country and variant are empty
85         assertTrue(StringUtils.isEmpty(locale.getCountry()));
86         assertTrue(StringUtils.isEmpty(locale.getVariant()));
87     }
88 
89     /**
90      * Pass in a valid language, test toLocale.
91      *
92      * @param localeString to pass to toLocale()
93      * @param language of the resulting Locale
94      * @param country of the resulting Locale
95      */
assertValidToLocale(final String localeString, final String language, final String country)96     private static void assertValidToLocale(final String localeString, final String language, final String country) {
97         final Locale locale = LocaleUtils.toLocale(localeString);
98         assertNotNull(locale, "valid locale");
99         assertEquals(language, locale.getLanguage());
100         assertEquals(country, locale.getCountry());
101         //variant is empty
102         assertTrue(StringUtils.isEmpty(locale.getVariant()));
103     }
104 
105     /**
106      * Pass in a valid language, test toLocale.
107      *
108      * @param localeString to pass to toLocale()
109      * @param language of the resulting Locale
110      * @param country of the resulting Locale
111      * @param variant of the resulting Locale
112      */
assertValidToLocale( final String localeString, final String language, final String country, final String variant)113     private static void assertValidToLocale(
114             final String localeString, final String language,
115             final String country, final String variant) {
116         final Locale locale = LocaleUtils.toLocale(localeString);
117         assertNotNull(locale, "valid locale");
118         assertEquals(language, locale.getLanguage());
119         assertEquals(country, locale.getCountry());
120         assertEquals(variant, locale.getVariant());
121     }
122 
123     /**
124      * Test toLocale(Locale) method.
125      */
126     @Test
testToLocale_Locale_defaults()127     public void testToLocale_Locale_defaults() {
128         assertNull(LocaleUtils.toLocale((String) null));
129         assertEquals(Locale.getDefault(), LocaleUtils.toLocale((Locale) null));
130         assertEquals(Locale.getDefault(), LocaleUtils.toLocale(Locale.getDefault()));
131     }
132 
133     /**
134      * Test toLocale(Locale) method.
135      */
136     @ParameterizedTest
137     @MethodSource("java.util.Locale#getAvailableLocales")
testToLocales(final Locale actualLocale)138     public void testToLocales(final Locale actualLocale) {
139         assertEquals(actualLocale, LocaleUtils.toLocale(actualLocale));
140     }
141 
142     /**
143      * Test toLocale(String) method.
144      */
145     @Test
testToLocale_1Part()146     public void testToLocale_1Part() {
147         assertNull(LocaleUtils.toLocale((String) null));
148 
149         assertValidToLocale("us");
150         assertValidToLocale("fr");
151         assertValidToLocale("de");
152         assertValidToLocale("zh");
153         // Valid format but lang doesn't exist, should make instance anyway
154         assertValidToLocale("qq");
155         // LANG-941: JDK 8 introduced the empty locale as one of the default locales
156         assertValidToLocale("");
157 
158         assertThrows(IllegalArgumentException.class, () -> LocaleUtils.toLocale("Us"), "Should fail if not lowercase");
159         assertThrows(IllegalArgumentException.class, () -> LocaleUtils.toLocale("uS"), "Should fail if not lowercase");
160         assertThrows(IllegalArgumentException.class, () -> LocaleUtils.toLocale("u#"), "Should fail if not lowercase");
161         assertThrows(
162                 IllegalArgumentException.class, () -> LocaleUtils.toLocale("u"), "Must be 2 chars if less than 5");
163         assertThrows(
164                 IllegalArgumentException.class, () -> LocaleUtils.toLocale("uu_U"), "Must be 2 chars if less than 5");
165     }
166 
167     /**
168      * Test toLocale() method.
169      */
170     @Test
testToLocale_2Part()171     public void testToLocale_2Part() {
172         assertValidToLocale("us_EN", "us", "EN");
173         assertValidToLocale("us-EN", "us", "EN");
174         //valid though doesn't exist
175         assertValidToLocale("us_ZH", "us", "ZH");
176 
177         assertThrows(
178                 IllegalArgumentException.class,
179                 () -> LocaleUtils.toLocale("us_En"),
180                 "Should fail second part not uppercase");
181         assertThrows(
182                 IllegalArgumentException.class,
183                 () -> LocaleUtils.toLocale("us_en"),
184                 "Should fail second part not uppercase");
185         assertThrows(
186                 IllegalArgumentException.class,
187                 () -> LocaleUtils.toLocale("us_eN"),
188                 "Should fail second part not uppercase");
189         assertThrows(
190                 IllegalArgumentException.class,
191                 () -> LocaleUtils.toLocale("uS_EN"),
192                 "Should fail first part not lowercase");
193         assertThrows(
194                 IllegalArgumentException.class,
195                 () -> LocaleUtils.toLocale("us_E3"),
196                 "Should fail second part not uppercase");
197     }
198 
199     /**
200      * Test toLocale() method.
201      */
202     @Test
testToLocale_3Part()203     public void testToLocale_3Part() {
204         assertValidToLocale("us_EN_A", "us", "EN", "A");
205         assertValidToLocale("us-EN-A", "us", "EN", "A");
206         // this isn't pretty, but was caused by a jdk bug it seems
207         // https://bugs.java.com/bugdatabase/view_bug.do?bug_id=4210525
208         if (SystemUtils.isJavaVersionAtLeast(JAVA_1_4)) {
209             assertValidToLocale("us_EN_a", "us", "EN", "a");
210             assertValidToLocale("us_EN_SFsafdFDsdfF", "us", "EN", "SFsafdFDsdfF");
211         } else {
212             assertValidToLocale("us_EN_a", "us", "EN", "A");
213             assertValidToLocale("us_EN_SFsafdFDsdfF", "us", "EN", "SFSAFDFDSDFF");
214         }
215 
216         assertThrows(
217                 IllegalArgumentException.class, () -> LocaleUtils.toLocale("us_EN-a"), "Should fail as no consistent delimiter");
218         assertThrows(
219                 IllegalArgumentException.class, () -> LocaleUtils.toLocale("uu_UU_"), "Must be 3, 5 or 7+ in length");
220     }
221 
222     /**
223      * Helper method for local lookups.
224      *
225      * @param locale  the input locale
226      * @param defaultLocale  the input default locale
227      * @param expected  expected results
228      */
assertLocaleLookupList(final Locale locale, final Locale defaultLocale, final Locale[] expected)229     private static void assertLocaleLookupList(final Locale locale, final Locale defaultLocale, final Locale[] expected) {
230         final List<Locale> localeList = defaultLocale == null ?
231                 LocaleUtils.localeLookupList(locale) :
232                 LocaleUtils.localeLookupList(locale, defaultLocale);
233 
234         assertEquals(expected.length, localeList.size());
235         assertEquals(Arrays.asList(expected), localeList);
236         assertUnmodifiableCollection(localeList);
237     }
238 
239     /**
240      * Test localeLookupList() method.
241      */
242     @Test
testLocaleLookupList_Locale()243     public void testLocaleLookupList_Locale() {
244         assertLocaleLookupList(null, null, new Locale[0]);
245         assertLocaleLookupList(LOCALE_QQ, null, new Locale[]{LOCALE_QQ});
246         assertLocaleLookupList(LOCALE_EN, null, new Locale[]{LOCALE_EN});
247         assertLocaleLookupList(LOCALE_EN, null, new Locale[]{LOCALE_EN});
248         assertLocaleLookupList(LOCALE_EN_US, null,
249             new Locale[] {
250                 LOCALE_EN_US,
251                 LOCALE_EN});
252         assertLocaleLookupList(LOCALE_EN_US_ZZZZ, null,
253             new Locale[] {
254                 LOCALE_EN_US_ZZZZ,
255                 LOCALE_EN_US,
256                 LOCALE_EN});
257     }
258 
259     /**
260      * Test localeLookupList() method.
261      */
262     @Test
testLocaleLookupList_LocaleLocale()263     public void testLocaleLookupList_LocaleLocale() {
264         assertLocaleLookupList(LOCALE_QQ, LOCALE_QQ,
265                 new Locale[]{LOCALE_QQ});
266         assertLocaleLookupList(LOCALE_EN, LOCALE_EN,
267                 new Locale[]{LOCALE_EN});
268 
269         assertLocaleLookupList(LOCALE_EN_US, LOCALE_EN_US,
270             new Locale[]{
271                 LOCALE_EN_US,
272                 LOCALE_EN});
273         assertLocaleLookupList(LOCALE_EN_US, LOCALE_QQ,
274             new Locale[] {
275                 LOCALE_EN_US,
276                 LOCALE_EN,
277                 LOCALE_QQ});
278         assertLocaleLookupList(LOCALE_EN_US, LOCALE_QQ_ZZ,
279             new Locale[] {
280                 LOCALE_EN_US,
281                 LOCALE_EN,
282                 LOCALE_QQ_ZZ});
283 
284         assertLocaleLookupList(LOCALE_EN_US_ZZZZ, null,
285             new Locale[] {
286                 LOCALE_EN_US_ZZZZ,
287                 LOCALE_EN_US,
288                 LOCALE_EN});
289         assertLocaleLookupList(LOCALE_EN_US_ZZZZ, LOCALE_EN_US_ZZZZ,
290             new Locale[] {
291                 LOCALE_EN_US_ZZZZ,
292                 LOCALE_EN_US,
293                 LOCALE_EN});
294         assertLocaleLookupList(LOCALE_EN_US_ZZZZ, LOCALE_QQ,
295             new Locale[] {
296                 LOCALE_EN_US_ZZZZ,
297                 LOCALE_EN_US,
298                 LOCALE_EN,
299                 LOCALE_QQ});
300         assertLocaleLookupList(LOCALE_EN_US_ZZZZ, LOCALE_QQ_ZZ,
301             new Locale[] {
302                 LOCALE_EN_US_ZZZZ,
303                 LOCALE_EN_US,
304                 LOCALE_EN,
305                 LOCALE_QQ_ZZ});
306         assertLocaleLookupList(LOCALE_FR_CA, LOCALE_EN,
307             new Locale[] {
308                 LOCALE_FR_CA,
309                 LOCALE_FR,
310                 LOCALE_EN});
311     }
312 
313     /**
314      * Test availableLocaleList() method.
315      */
316     @Test
testAvailableLocaleList()317     public void testAvailableLocaleList() {
318         final List<Locale> list = LocaleUtils.availableLocaleList();
319         final List<Locale> list2 = LocaleUtils.availableLocaleList();
320         assertNotNull(list);
321         assertSame(list, list2);
322         assertUnmodifiableCollection(list);
323 
324         final Locale[] jdkLocaleArray = Locale.getAvailableLocales();
325         final List<Locale> jdkLocaleList = Arrays.asList(jdkLocaleArray);
326         assertEquals(jdkLocaleList, list);
327     }
328 
329     /**
330      * Test availableLocaleSet() method.
331      */
332     @Test
testAvailableLocaleSet()333     public void testAvailableLocaleSet() {
334         final Set<Locale> set = LocaleUtils.availableLocaleSet();
335         final Set<Locale> set2 = LocaleUtils.availableLocaleSet();
336         assertNotNull(set);
337         assertSame(set, set2);
338         assertUnmodifiableCollection(set);
339 
340         final Locale[] jdkLocaleArray = Locale.getAvailableLocales();
341         final List<Locale> jdkLocaleList = Arrays.asList(jdkLocaleArray);
342         final Set<Locale> jdkLocaleSet = new HashSet<>(jdkLocaleList);
343         assertEquals(jdkLocaleSet, set);
344     }
345 
346     /**
347      * Test availableLocaleSet() method.
348      */
349     @SuppressWarnings("boxing") // JUnit4 does not support primitive equality testing apart from long
350     @Test
testIsAvailableLocale()351     public void testIsAvailableLocale() {
352         final Set<Locale> set = LocaleUtils.availableLocaleSet();
353         assertEquals(set.contains(LOCALE_EN), LocaleUtils.isAvailableLocale(LOCALE_EN));
354         assertEquals(set.contains(LOCALE_EN_US), LocaleUtils.isAvailableLocale(LOCALE_EN_US));
355         assertEquals(set.contains(LOCALE_EN_US_ZZZZ), LocaleUtils.isAvailableLocale(LOCALE_EN_US_ZZZZ));
356         assertEquals(set.contains(LOCALE_FR), LocaleUtils.isAvailableLocale(LOCALE_FR));
357         assertEquals(set.contains(LOCALE_FR_CA), LocaleUtils.isAvailableLocale(LOCALE_FR_CA));
358         assertEquals(set.contains(LOCALE_QQ), LocaleUtils.isAvailableLocale(LOCALE_QQ));
359         assertEquals(set.contains(LOCALE_QQ_ZZ), LocaleUtils.isAvailableLocale(LOCALE_QQ_ZZ));
360     }
361 
362     /**
363      * Test for 3-chars locale, further details at LANG-915
364      *
365      */
366     @Test
testThreeCharsLocale()367     public void testThreeCharsLocale() {
368         for (final String str : Arrays.asList("udm", "tet")) {
369             final Locale locale = LocaleUtils.toLocale(str);
370             assertNotNull(locale);
371             assertEquals(str, locale.getLanguage());
372             assertTrue(StringUtils.isBlank(locale.getCountry()));
373             assertEquals(new Locale(str), locale);
374         }
375     }
376 
377     /**
378      * Make sure the language by country is correct. It checks that
379      * the LocaleUtils.languagesByCountry(country) call contains the
380      * array of languages passed in. It may contain more due to JVM
381      * variations.
382      *
383      * @param country
384      * @param languages array of languages that should be returned
385      */
assertLanguageByCountry(final String country, final String[] languages)386     private static void assertLanguageByCountry(final String country, final String[] languages) {
387         final List<Locale> list = LocaleUtils.languagesByCountry(country);
388         final List<Locale> list2 = LocaleUtils.languagesByCountry(country);
389         assertNotNull(list);
390         assertSame(list, list2);
391         //search through languages
392         for (final String language : languages) {
393             final Iterator<Locale> iterator = list.iterator();
394             boolean found = false;
395             // see if it was returned by the set
396             while (iterator.hasNext()) {
397                 final Locale locale = iterator.next();
398                 // should have an en empty variant
399                 assertTrue(StringUtils.isEmpty(locale.getVariant()));
400                 assertEquals(country, locale.getCountry());
401                 if (language.equals(locale.getLanguage())) {
402                     found = true;
403                     break;
404                 }
405             }
406             assertTrue(found, "Could not find language: " + language + " for country: " + country);
407         }
408         assertUnmodifiableCollection(list);
409     }
410 
411     /**
412      * Test languagesByCountry() method.
413      */
414     @Test
testLanguagesByCountry()415     public void testLanguagesByCountry() {
416         assertLanguageByCountry(null, new String[0]);
417         assertLanguageByCountry("GB", new String[]{"en"});
418         assertLanguageByCountry("ZZ", new String[0]);
419         assertLanguageByCountry("CH", new String[]{"fr", "de", "it"});
420     }
421 
422     /**
423      * Make sure the country by language is correct. It checks that
424      * the LocaleUtils.countryByLanguage(language) call contains the
425      * array of countries passed in. It may contain more due to JVM
426      * variations.
427      *
428      *
429      * @param language
430      * @param countries array of countries that should be returned
431      */
assertCountriesByLanguage(final String language, final String[] countries)432     private static void assertCountriesByLanguage(final String language, final String[] countries) {
433         final List<Locale> list = LocaleUtils.countriesByLanguage(language);
434         final List<Locale> list2 = LocaleUtils.countriesByLanguage(language);
435         assertNotNull(list);
436         assertSame(list, list2);
437         //search through languages
438         for (final String country : countries) {
439             final Iterator<Locale> iterator = list.iterator();
440             boolean found = false;
441             // see if it was returned by the set
442             while (iterator.hasNext()) {
443                 final Locale locale = iterator.next();
444                 // should have an en empty variant
445                 assertTrue(StringUtils.isEmpty(locale.getVariant()));
446                 assertEquals(language, locale.getLanguage());
447                 if (country.equals(locale.getCountry())) {
448                     found = true;
449                     break;
450                 }
451             }
452             assertTrue(found, "Could not find language: " + country + " for country: " + language);
453         }
454         assertUnmodifiableCollection(list);
455     }
456 
457     /**
458      * Test countriesByLanguage() method.
459      */
460     @Test
testCountriesByLanguage()461     public void testCountriesByLanguage() {
462         assertCountriesByLanguage(null, new String[0]);
463         assertCountriesByLanguage("de", new String[]{"DE", "CH", "AT", "LU"});
464         assertCountriesByLanguage("zz", new String[0]);
465         assertCountriesByLanguage("it", new String[]{"IT", "CH"});
466     }
467 
468     /**
469      * @param coll  the collection to check
470      */
assertUnmodifiableCollection(final Collection<?> coll)471     private static void assertUnmodifiableCollection(final Collection<?> coll) {
472         assertThrows(UnsupportedOperationException.class, () -> coll.add(null));
473     }
474 
475     /**
476      * Tests #LANG-328 - only language+variant
477      */
478     @Test
testLang328()479     public void testLang328() {
480         assertValidToLocale("fr__P", "fr", "", "P");
481         assertValidToLocale("fr__POSIX", "fr", "", "POSIX");
482     }
483 
484     @Test
testLanguageAndUNM49Numeric3AreaCodeLang1312()485     public void testLanguageAndUNM49Numeric3AreaCodeLang1312() {
486         assertValidToLocale("en_001", "en", "001");
487         assertValidToLocale("en_150", "en", "150");
488         assertValidToLocale("ar_001", "ar", "001");
489 
490         // LANG-1312
491         assertValidToLocale("en_001_GB", "en", "001", "GB");
492         assertValidToLocale("en_150_US", "en", "150", "US");
493     }
494 
495     /**
496      * Tests #LANG-865, strings starting with an underscore.
497      */
498     @Test
testLang865()499     public void testLang865() {
500         assertValidToLocale("_GB", "", "GB", "");
501         assertValidToLocale("_GB_P", "", "GB", "P");
502         assertValidToLocale("_GB_POSIX", "", "GB", "POSIX");
503         assertThrows(
504                 IllegalArgumentException.class,
505                 () -> LocaleUtils.toLocale("_G"),
506                 "Must be at least 3 chars if starts with underscore");
507         assertThrows(
508                 IllegalArgumentException.class,
509                 () -> LocaleUtils.toLocale("_Gb"),
510                 "Must be uppercase if starts with underscore");
511         assertThrows(
512                 IllegalArgumentException.class,
513                 () -> LocaleUtils.toLocale("_gB"),
514                 "Must be uppercase if starts with underscore");
515         assertThrows(
516                 IllegalArgumentException.class,
517                 () -> LocaleUtils.toLocale("_1B"),
518                 "Must be letter if starts with underscore");
519         assertThrows(
520                 IllegalArgumentException.class,
521                 () -> LocaleUtils.toLocale("_G1"),
522                 "Must be letter if starts with underscore");
523         assertThrows(
524                 IllegalArgumentException.class,
525                 () -> LocaleUtils.toLocale("_GB_"),
526                 "Must be at least 5 chars if starts with underscore");
527         assertThrows(
528                 IllegalArgumentException.class,
529                 () -> LocaleUtils.toLocale("_GBAP"),
530                 "Must have underscore after the country if starts with underscore and is at least 5 chars");
531     }
532 
533     @ParameterizedTest
534     @MethodSource("java.util.Locale#getAvailableLocales")
testParseAllLocales(final Locale actualLocale)535     public void testParseAllLocales(final Locale actualLocale) {
536         // Check if it's possible to recreate the Locale using just the standard constructor
537         final Locale locale = new Locale(actualLocale.getLanguage(), actualLocale.getCountry(), actualLocale.getVariant());
538         if (actualLocale.equals(locale)) { // it is possible for LocaleUtils.toLocale to handle these Locales
539             final String str = actualLocale.toString();
540             // Look for the script/extension suffix
541             int suff = str.indexOf("_#");
542             if (suff == - 1) {
543                 suff = str.indexOf("#");
544             }
545             String localeStr = str;
546             if (suff >= 0) { // we have a suffix
547                 assertThrows(IllegalArgumentException.class, () -> LocaleUtils.toLocale(str));
548                 // try without suffix
549                 localeStr = str.substring(0, suff);
550             }
551             final Locale loc = LocaleUtils.toLocale(localeStr);
552             assertEquals(actualLocale, loc);
553         }
554     }
555 }
556