Skip to content

Commit 703b3b1

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

File tree

2 files changed

+29
-33
lines changed

2 files changed

+29
-33
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 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.
@@ -707,7 +707,7 @@ public static Locale parseLocaleString(String localeString) {
707707
private static void validateLocalePart(String localePart) {
708708
for (int i = 0; i < localePart.length(); i++) {
709709
char ch = localePart.charAt(i);
710-
if (ch != '_' && ch != ' ' && !Character.isLetterOrDigit(ch)) {
710+
if (ch != ' ' && ch != '_' && ch != '#' && !Character.isLetterOrDigit(ch)) {
711711
throw new IllegalArgumentException(
712712
"Locale part \"" + localePart + "\" contains invalid characters");
713713
}

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

Lines changed: 27 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 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.
@@ -618,62 +618,55 @@ public void testParseLocaleStringWithEmptyLocaleStringYieldsNullLocale() throws
618618
assertNull("When given an empty Locale string, must return null.", locale);
619619
}
620620

621-
// SPR-8637
622-
@Test
621+
@Test // SPR-8637
623622
public void testParseLocaleWithMultiSpecialCharactersInVariant() throws Exception {
624-
final String variant = "proper-northern";
625-
final String localeString = "en_GB_" + variant;
623+
String variant = "proper-northern";
624+
String localeString = "en_GB_" + variant;
626625
Locale locale = StringUtils.parseLocaleString(localeString);
627626
assertEquals("Multi-valued variant portion of the Locale not extracted correctly.", variant, locale.getVariant());
628627
}
629628

630-
// SPR-3671
631-
@Test
629+
@Test // SPR-3671
632630
public void testParseLocaleWithMultiValuedVariant() throws Exception {
633-
final String variant = "proper_northern";
634-
final String localeString = "en_GB_" + variant;
631+
String variant = "proper_northern";
632+
String localeString = "en_GB_" + variant;
635633
Locale locale = StringUtils.parseLocaleString(localeString);
636634
assertEquals("Multi-valued variant portion of the Locale not extracted correctly.", variant, locale.getVariant());
637635
}
638636

639-
// SPR-3671
640-
@Test
637+
@Test // SPR-3671
641638
public void testParseLocaleWithMultiValuedVariantUsingSpacesAsSeparators() throws Exception {
642-
final String variant = "proper northern";
643-
final String localeString = "en GB " + variant;
639+
String variant = "proper northern";
640+
String localeString = "en GB " + variant;
644641
Locale locale = StringUtils.parseLocaleString(localeString);
645642
assertEquals("Multi-valued variant portion of the Locale not extracted correctly.", variant, locale.getVariant());
646643
}
647644

648-
// SPR-3671
649-
@Test
645+
@Test // SPR-3671
650646
public void testParseLocaleWithMultiValuedVariantUsingMixtureOfUnderscoresAndSpacesAsSeparators() throws Exception {
651-
final String variant = "proper northern";
652-
final String localeString = "en_GB_" + variant;
647+
String variant = "proper northern";
648+
String localeString = "en_GB_" + variant;
653649
Locale locale = StringUtils.parseLocaleString(localeString);
654650
assertEquals("Multi-valued variant portion of the Locale not extracted correctly.", variant, locale.getVariant());
655651
}
656652

657-
// SPR-3671
658-
@Test
653+
@Test // SPR-3671
659654
public void testParseLocaleWithMultiValuedVariantUsingSpacesAsSeparatorsWithLotsOfLeadingWhitespace() throws Exception {
660-
final String variant = "proper northern";
661-
final String localeString = "en GB " + variant; // lots of whitespace
655+
String variant = "proper northern";
656+
String localeString = "en GB " + variant; // lots of whitespace
662657
Locale locale = StringUtils.parseLocaleString(localeString);
663658
assertEquals("Multi-valued variant portion of the Locale not extracted correctly.", variant, locale.getVariant());
664659
}
665660

666-
// SPR-3671
667-
@Test
661+
@Test // SPR-3671
668662
public void testParseLocaleWithMultiValuedVariantUsingUnderscoresAsSeparatorsWithLotsOfLeadingWhitespace() throws Exception {
669-
final String variant = "proper_northern";
670-
final String localeString = "en_GB_____" + variant; // lots of underscores
663+
String variant = "proper_northern";
664+
String localeString = "en_GB_____" + variant; // lots of underscores
671665
Locale locale = StringUtils.parseLocaleString(localeString);
672666
assertEquals("Multi-valued variant portion of the Locale not extracted correctly.", variant, locale.getVariant());
673667
}
674668

675-
// SPR-7779
676-
@Test
669+
@Test // SPR-7779
677670
public void testParseLocaleWithInvalidCharacters() {
678671
try {
679672
StringUtils.parseLocaleString("%0D%0AContent-length:30%0D%0A%0D%0A%3Cscript%3Ealert%28123%29%3C/script%3E");
@@ -684,20 +677,23 @@ public void testParseLocaleWithInvalidCharacters() {
684677
}
685678
}
686679

687-
// SPR-9420
688-
@Test
680+
@Test // SPR-9420
689681
public void testParseLocaleWithSameLowercaseTokenForLanguageAndCountry() {
690682
assertEquals("tr_TR", StringUtils.parseLocaleString("tr_tr").toString());
691683
assertEquals("bg_BG_vnt", StringUtils.parseLocaleString("bg_bg_vnt").toString());
692684
}
693685

694-
// SPR-11806
695-
@Test
686+
@Test // SPR-11806
696687
public void testParseLocaleWithVariantContainingCountryCode() {
697688
String variant = "GBtest";
698689
String localeString = "en_GB_" + variant;
699690
Locale locale = StringUtils.parseLocaleString(localeString);
700691
assertEquals("Variant containing country code not extracted correctly", variant, locale.getVariant());
701692
}
702693

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

0 commit comments

Comments
 (0)