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