StringUtils.parseLocale consistently handles invalid locale values
Closes gh-22603
This commit is contained in:
parent
8d7676a5ec
commit
507d4ba825
|
|
@ -774,7 +774,9 @@ public abstract class StringUtils {
|
|||
if (tokens.length == 1) {
|
||||
validateLocalePart(localeValue);
|
||||
Locale resolved = Locale.forLanguageTag(localeValue);
|
||||
return (resolved.getLanguage().length() > 0 ? resolved : null);
|
||||
if (resolved.getLanguage().length() > 0) {
|
||||
return resolved;
|
||||
}
|
||||
}
|
||||
return parseLocaleTokens(localeValue, tokens);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
* Copyright 2002-2019 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
|
@ -765,4 +765,20 @@ public class StringUtilsTests {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInvalidLocaleWithLocaleString() {
|
||||
assertEquals(new Locale("invalid"), StringUtils.parseLocaleString("invalid"));
|
||||
assertEquals(new Locale("invalidvalue"), StringUtils.parseLocaleString("invalidvalue"));
|
||||
assertEquals(new Locale("invalidvalue", "foo"), StringUtils.parseLocaleString("invalidvalue_foo"));
|
||||
assertNull(StringUtils.parseLocaleString(""));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInvalidLocaleWithLanguageTag() {
|
||||
assertEquals(new Locale("invalid"), StringUtils.parseLocale("invalid"));
|
||||
assertEquals(new Locale("invalidvalue"), StringUtils.parseLocale("invalidvalue"));
|
||||
assertEquals(new Locale("invalidvalue", "foo"), StringUtils.parseLocale("invalidvalue_foo"));
|
||||
assertNull(StringUtils.parseLocale(""));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue