StringUtils.parseLocaleString accepts Java 7 variants

Issue: SPR-14718
(cherry picked from commit 818c72a)
This commit is contained in:
Juergen Hoeller
2016-09-16 11:16:08 +02:00
parent cfd16f9c94
commit 703b3b1524
2 changed files with 29 additions and 33 deletions
@@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2014 the original author or authors. * Copyright 2002-2016 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -707,7 +707,7 @@ public abstract class StringUtils {
private static void validateLocalePart(String localePart) { private static void validateLocalePart(String localePart) {
for (int i = 0; i < localePart.length(); i++) { for (int i = 0; i < localePart.length(); i++) {
char ch = localePart.charAt(i); char ch = localePart.charAt(i);
if (ch != '_' && ch != ' ' && !Character.isLetterOrDigit(ch)) { if (ch != ' ' && ch != '_' && ch != '#' && !Character.isLetterOrDigit(ch)) {
throw new IllegalArgumentException( throw new IllegalArgumentException(
"Locale part \"" + localePart + "\" contains invalid characters"); "Locale part \"" + localePart + "\" contains invalid characters");
} }
@@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2014 the original author or authors. * Copyright 2002-2016 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -618,62 +618,55 @@ public class StringUtilsTests {
assertNull("When given an empty Locale string, must return null.", locale); assertNull("When given an empty Locale string, must return null.", locale);
} }
// SPR-8637 @Test // SPR-8637
@Test
public void testParseLocaleWithMultiSpecialCharactersInVariant() throws Exception { public void testParseLocaleWithMultiSpecialCharactersInVariant() throws Exception {
final String variant = "proper-northern"; String variant = "proper-northern";
final String localeString = "en_GB_" + variant; String localeString = "en_GB_" + variant;
Locale locale = StringUtils.parseLocaleString(localeString); Locale locale = StringUtils.parseLocaleString(localeString);
assertEquals("Multi-valued variant portion of the Locale not extracted correctly.", variant, locale.getVariant()); assertEquals("Multi-valued variant portion of the Locale not extracted correctly.", variant, locale.getVariant());
} }
// SPR-3671 @Test // SPR-3671
@Test
public void testParseLocaleWithMultiValuedVariant() throws Exception { public void testParseLocaleWithMultiValuedVariant() throws Exception {
final String variant = "proper_northern"; String variant = "proper_northern";
final String localeString = "en_GB_" + variant; String localeString = "en_GB_" + variant;
Locale locale = StringUtils.parseLocaleString(localeString); Locale locale = StringUtils.parseLocaleString(localeString);
assertEquals("Multi-valued variant portion of the Locale not extracted correctly.", variant, locale.getVariant()); assertEquals("Multi-valued variant portion of the Locale not extracted correctly.", variant, locale.getVariant());
} }
// SPR-3671 @Test // SPR-3671
@Test
public void testParseLocaleWithMultiValuedVariantUsingSpacesAsSeparators() throws Exception { public void testParseLocaleWithMultiValuedVariantUsingSpacesAsSeparators() throws Exception {
final String variant = "proper northern"; String variant = "proper northern";
final String localeString = "en GB " + variant; String localeString = "en GB " + variant;
Locale locale = StringUtils.parseLocaleString(localeString); Locale locale = StringUtils.parseLocaleString(localeString);
assertEquals("Multi-valued variant portion of the Locale not extracted correctly.", variant, locale.getVariant()); assertEquals("Multi-valued variant portion of the Locale not extracted correctly.", variant, locale.getVariant());
} }
// SPR-3671 @Test // SPR-3671
@Test
public void testParseLocaleWithMultiValuedVariantUsingMixtureOfUnderscoresAndSpacesAsSeparators() throws Exception { public void testParseLocaleWithMultiValuedVariantUsingMixtureOfUnderscoresAndSpacesAsSeparators() throws Exception {
final String variant = "proper northern"; String variant = "proper northern";
final String localeString = "en_GB_" + variant; String localeString = "en_GB_" + variant;
Locale locale = StringUtils.parseLocaleString(localeString); Locale locale = StringUtils.parseLocaleString(localeString);
assertEquals("Multi-valued variant portion of the Locale not extracted correctly.", variant, locale.getVariant()); assertEquals("Multi-valued variant portion of the Locale not extracted correctly.", variant, locale.getVariant());
} }
// SPR-3671 @Test // SPR-3671
@Test
public void testParseLocaleWithMultiValuedVariantUsingSpacesAsSeparatorsWithLotsOfLeadingWhitespace() throws Exception { public void testParseLocaleWithMultiValuedVariantUsingSpacesAsSeparatorsWithLotsOfLeadingWhitespace() throws Exception {
final String variant = "proper northern"; String variant = "proper northern";
final String localeString = "en GB " + variant; // lots of whitespace String localeString = "en GB " + variant; // lots of whitespace
Locale locale = StringUtils.parseLocaleString(localeString); Locale locale = StringUtils.parseLocaleString(localeString);
assertEquals("Multi-valued variant portion of the Locale not extracted correctly.", variant, locale.getVariant()); assertEquals("Multi-valued variant portion of the Locale not extracted correctly.", variant, locale.getVariant());
} }
// SPR-3671 @Test // SPR-3671
@Test
public void testParseLocaleWithMultiValuedVariantUsingUnderscoresAsSeparatorsWithLotsOfLeadingWhitespace() throws Exception { public void testParseLocaleWithMultiValuedVariantUsingUnderscoresAsSeparatorsWithLotsOfLeadingWhitespace() throws Exception {
final String variant = "proper_northern"; String variant = "proper_northern";
final String localeString = "en_GB_____" + variant; // lots of underscores String localeString = "en_GB_____" + variant; // lots of underscores
Locale locale = StringUtils.parseLocaleString(localeString); Locale locale = StringUtils.parseLocaleString(localeString);
assertEquals("Multi-valued variant portion of the Locale not extracted correctly.", variant, locale.getVariant()); assertEquals("Multi-valued variant portion of the Locale not extracted correctly.", variant, locale.getVariant());
} }
// SPR-7779 @Test // SPR-7779
@Test
public void testParseLocaleWithInvalidCharacters() { public void testParseLocaleWithInvalidCharacters() {
try { try {
StringUtils.parseLocaleString("%0D%0AContent-length:30%0D%0A%0D%0A%3Cscript%3Ealert%28123%29%3C/script%3E"); StringUtils.parseLocaleString("%0D%0AContent-length:30%0D%0A%0D%0A%3Cscript%3Ealert%28123%29%3C/script%3E");
@@ -684,15 +677,13 @@ public class StringUtilsTests {
} }
} }
// SPR-9420 @Test // SPR-9420
@Test
public void testParseLocaleWithSameLowercaseTokenForLanguageAndCountry() { public void testParseLocaleWithSameLowercaseTokenForLanguageAndCountry() {
assertEquals("tr_TR", StringUtils.parseLocaleString("tr_tr").toString()); assertEquals("tr_TR", StringUtils.parseLocaleString("tr_tr").toString());
assertEquals("bg_BG_vnt", StringUtils.parseLocaleString("bg_bg_vnt").toString()); assertEquals("bg_BG_vnt", StringUtils.parseLocaleString("bg_bg_vnt").toString());
} }
// SPR-11806 @Test // SPR-11806
@Test
public void testParseLocaleWithVariantContainingCountryCode() { public void testParseLocaleWithVariantContainingCountryCode() {
String variant = "GBtest"; String variant = "GBtest";
String localeString = "en_GB_" + variant; String localeString = "en_GB_" + variant;
@@ -700,4 +691,9 @@ public class StringUtilsTests {
assertEquals("Variant containing country code not extracted correctly", variant, locale.getVariant()); assertEquals("Variant containing country code not extracted correctly", variant, locale.getVariant());
} }
@Test // SPR-14718
public void testParseJava7Variant() {
assertEquals("sr_#LATN", StringUtils.parseLocaleString("sr_#LATN").toString());
}
} }