Skip to content

Commit 818c72a

Browse files
committed
StringUtils.parseLocaleString accepts Java 7 variants
Issue: SPR-14718 (cherry picked from commit 7ddaf49)
1 parent c9943aa commit 818c72a

File tree

2 files changed

+17
-19
lines changed

2 files changed

+17
-19
lines changed

spring-core/src/main/java/org/springframework/util/StringUtils.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -50,6 +50,7 @@
5050
* @author Rick Evans
5151
* @author Arjen Poutsma
5252
* @author Sam Brannen
53+
* @author Brian Clozel
5354
* @since 16 April 2001
5455
*/
5556
public abstract class StringUtils {
@@ -714,7 +715,7 @@ public static Locale parseLocaleString(String localeString) {
714715
private static void validateLocalePart(String localePart) {
715716
for (int i = 0; i < localePart.length(); i++) {
716717
char ch = localePart.charAt(i);
717-
if (ch != '_' && ch != ' ' && !Character.isLetterOrDigit(ch)) {
718+
if (ch != ' ' && ch != '_' && ch != '#' && !Character.isLetterOrDigit(ch)) {
718719
throw new IllegalArgumentException(
719720
"Locale part \"" + localePart + "\" contains invalid characters");
720721
}

spring-core/src/test/java/org/springframework/util/StringUtilsTests.java

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -627,53 +627,47 @@ public void testParseLocaleWithMultiSpecialCharactersInVariant() throws Exceptio
627627
assertEquals("Multi-valued variant portion of the Locale not extracted correctly.", variant, locale.getVariant());
628628
}
629629

630-
// SPR-3671
631-
@Test
630+
@Test // SPR-3671
632631
public void testParseLocaleWithMultiValuedVariant() throws Exception {
633632
final String variant = "proper_northern";
634633
final String localeString = "en_GB_" + variant;
635634
Locale locale = StringUtils.parseLocaleString(localeString);
636635
assertEquals("Multi-valued variant portion of the Locale not extracted correctly.", variant, locale.getVariant());
637636
}
638637

639-
// SPR-3671
640-
@Test
638+
@Test // SPR-3671
641639
public void testParseLocaleWithMultiValuedVariantUsingSpacesAsSeparators() throws Exception {
642640
final String variant = "proper northern";
643641
final String localeString = "en GB " + variant;
644642
Locale locale = StringUtils.parseLocaleString(localeString);
645643
assertEquals("Multi-valued variant portion of the Locale not extracted correctly.", variant, locale.getVariant());
646644
}
647645

648-
// SPR-3671
649-
@Test
646+
@Test // SPR-3671
650647
public void testParseLocaleWithMultiValuedVariantUsingMixtureOfUnderscoresAndSpacesAsSeparators() throws Exception {
651648
final String variant = "proper northern";
652649
final String localeString = "en_GB_" + variant;
653650
Locale locale = StringUtils.parseLocaleString(localeString);
654651
assertEquals("Multi-valued variant portion of the Locale not extracted correctly.", variant, locale.getVariant());
655652
}
656653

657-
// SPR-3671
658-
@Test
654+
@Test // SPR-3671
659655
public void testParseLocaleWithMultiValuedVariantUsingSpacesAsSeparatorsWithLotsOfLeadingWhitespace() throws Exception {
660656
final String variant = "proper northern";
661657
final String localeString = "en GB " + variant; // lots of whitespace
662658
Locale locale = StringUtils.parseLocaleString(localeString);
663659
assertEquals("Multi-valued variant portion of the Locale not extracted correctly.", variant, locale.getVariant());
664660
}
665661

666-
// SPR-3671
667-
@Test
662+
@Test // SPR-3671
668663
public void testParseLocaleWithMultiValuedVariantUsingUnderscoresAsSeparatorsWithLotsOfLeadingWhitespace() throws Exception {
669664
final String variant = "proper_northern";
670665
final String localeString = "en_GB_____" + variant; // lots of underscores
671666
Locale locale = StringUtils.parseLocaleString(localeString);
672667
assertEquals("Multi-valued variant portion of the Locale not extracted correctly.", variant, locale.getVariant());
673668
}
674669

675-
// SPR-7779
676-
@Test
670+
@Test // SPR-7779
677671
public void testParseLocaleWithInvalidCharacters() {
678672
try {
679673
StringUtils.parseLocaleString("%0D%0AContent-length:30%0D%0A%0D%0A%3Cscript%3Ealert%28123%29%3C/script%3E");
@@ -684,20 +678,23 @@ public void testParseLocaleWithInvalidCharacters() {
684678
}
685679
}
686680

687-
// SPR-9420
688-
@Test
681+
@Test // SPR-9420
689682
public void testParseLocaleWithSameLowercaseTokenForLanguageAndCountry() {
690683
assertEquals("tr_TR", StringUtils.parseLocaleString("tr_tr").toString());
691684
assertEquals("bg_BG_vnt", StringUtils.parseLocaleString("bg_bg_vnt").toString());
692685
}
693686

694-
// SPR-11806
695-
@Test
687+
@Test // SPR-11806
696688
public void testParseLocaleWithVariantContainingCountryCode() {
697689
String variant = "GBtest";
698690
String localeString = "en_GB_" + variant;
699691
Locale locale = StringUtils.parseLocaleString(localeString);
700692
assertEquals("Variant containing country code not extracted correctly", variant, locale.getVariant());
701693
}
702694

695+
@Test // SPR-14718
696+
public void testParseJava7Variant() {
697+
assertEquals("sr_#LATN", StringUtils.parseLocaleString("sr_#LATN").toString());
698+
}
699+
703700
}

0 commit comments

Comments
 (0)