StringUtils.parseLocale consistently handles invalid locale values

Closes gh-22603
This commit is contained in:
Juergen Hoeller 2019-03-25 13:53:54 +01:00
parent 8d7676a5ec
commit 507d4ba825
2 changed files with 20 additions and 2 deletions

View File

@ -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);
}

View File

@ -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(""));
}
}