This is an automated email from the ASF dual-hosted git repository.
garydgregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-lang.git
The following commit(s) were added to refs/heads/master by this push:
new 715d12385 Add missing test
715d12385 is described below
commit 715d1238562d3c031318606a29859438f4b7d014
Author: Gary Gregory <[email protected]>
AuthorDate: Sat Sep 5 07:48:49 2026 -0400
Add missing test
- Checkstyle
- Internal refactoring
---
.../java/org/apache/commons/lang3/LocaleUtils.java | 17 ++++++++---------
.../org/apache/commons/lang3/LocaleUtilsTest.java | 22 +++++++++++++++++-----
2 files changed, 25 insertions(+), 14 deletions(-)
diff --git a/src/main/java/org/apache/commons/lang3/LocaleUtils.java
b/src/main/java/org/apache/commons/lang3/LocaleUtils.java
index 9b5947481..33b0c12b1 100644
--- a/src/main/java/org/apache/commons/lang3/LocaleUtils.java
+++ b/src/main/java/org/apache/commons/lang3/LocaleUtils.java
@@ -85,6 +85,7 @@ private static final class SyncAvoid {
*/
private static final ConcurrentMap<String, List<Locale>> ccToLocalesMap =
new ConcurrentHashMap<>();
+
/**
* Concurrent map of country locales by language.
*/
@@ -133,19 +134,20 @@ public static Set<Locale> availableLocaleSet() {
* @return An unmodifiable List of Locale objects, not null.
*/
public static List<Locale> countriesByLanguage(final String languageCode) {
- if (languageCode == null) {
- return Collections.emptyList();
- }
// Only syntactically valid ISO 639 codes can match an available
locale's language; anything
// else is answered without touching the cache so that arbitrary
caller strings are never
// retained for the lifetime of the class loader.
- if (!languageCode.isEmpty() && !isISO639LanguageCode(languageCode)) {
+ if (languageCode == null || !languageCode.isEmpty() &&
!isISO639LanguageCode(languageCode)) {
return Collections.emptyList();
}
return lcToLocalesMap.computeIfAbsent(languageCode, lc -> Collections
.unmodifiableList(availableLocaleList(locale ->
languageCode.equals(locale.getLanguage()) && !hasCountry(locale) &&
hasVariant(locale))));
}
+ static ConcurrentMap<String, List<Locale>> getCcToLocalesMap() {
+ return ccToLocalesMap;
+ }
+
/**
* Gets the cache of country locales by language.
*
@@ -262,13 +264,10 @@ private static boolean isNumericAreaCode(final String
str) {
* @return An unmodifiable List of Locale objects, not null.
*/
public static List<Locale> languagesByCountry(final String countryCode) {
- if (countryCode == null) {
- return Collections.emptyList();
- }
// Only syntactically valid ISO 3166 alpha-2 / UN M.49 numeric codes
can match an available
// locale's country; anything else is answered without touching the
cache so that arbitrary
- // caller strings are never retained for the lifetime of the JVM.
- if (!countryCode.isEmpty() && !isISO3166CountryCode(countryCode) &&
!isNumericAreaCode(countryCode)) {
+ // caller strings are never retained for the lifetime of the class
loader.
+ if (countryCode == null || !countryCode.isEmpty() &&
!isISO3166CountryCode(countryCode) && !isNumericAreaCode(countryCode)) {
return Collections.emptyList();
}
return ccToLocalesMap.computeIfAbsent(countryCode,
diff --git a/src/test/java/org/apache/commons/lang3/LocaleUtilsTest.java
b/src/test/java/org/apache/commons/lang3/LocaleUtilsTest.java
index 5b086766c..38ea40ab7 100644
--- a/src/test/java/org/apache/commons/lang3/LocaleUtilsTest.java
+++ b/src/test/java/org/apache/commons/lang3/LocaleUtilsTest.java
@@ -36,11 +36,11 @@
import java.util.List;
import java.util.Locale;
import java.util.Set;
+import java.util.concurrent.ConcurrentMap;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
-import org.junit.jupiter.params.provider.EmptySource;
import org.junit.jupiter.params.provider.MethodSource;
import org.junit.jupiter.params.provider.ValueSource;
import org.junitpioneer.jupiter.DefaultLocale;
@@ -263,11 +263,12 @@ void testCountriesByLanguage() {
@ParameterizedTest
@ValueSource(strings = {"x", "abcd", "EN", "e1", "en-US", " "})
void testCountriesByLanguageDoesNotCacheInvalidLanguageCode(final String
languageCode) {
- assertFalse(LocaleUtils.getLcToLocalesMap().containsKey(languageCode));
- final int cacheSize = LocaleUtils.getLcToLocalesMap().size();
+ final ConcurrentMap<String, List<Locale>> map =
LocaleUtils.getLcToLocalesMap();
+ assertFalse(map.containsKey(languageCode));
+ final int cacheSize = map.size();
assertTrue(LocaleUtils.countriesByLanguage(languageCode).isEmpty());
- assertEquals(cacheSize, LocaleUtils.getLcToLocalesMap().size());
- assertFalse(LocaleUtils.getLcToLocalesMap().containsKey(languageCode));
+ assertEquals(cacheSize, map.size());
+ assertFalse(map.containsKey(languageCode));
}
@Test
@@ -369,6 +370,17 @@ void testLanguagesByCountry() {
assertLanguageByCountry("CH", new String[]{"fr", "de", "it"});
}
+ @ParameterizedTest
+ @ValueSource(strings = {"x", "abcd", "English", "e1", "en-US", " "})
+ void testLanguagesByCountryDoesNotCacheInvalidLanguageCode(final String
languageCode) {
+ final ConcurrentMap<String, List<Locale>> map =
LocaleUtils.getCcToLocalesMap();
+ assertFalse(map.containsKey(languageCode));
+ final int cacheSize = map.size();
+ assertTrue(LocaleUtils.languagesByCountry(languageCode).isEmpty());
+ assertEquals(cacheSize, map.size());
+ assertFalse(map.containsKey(languageCode));
+ }
+
/**
* Test localeLookupList() method.
*/