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