Skip to content

Revise StringUtils.parseLocale(String) for proper handling of corner cases [SPR-16651] #21192

Closed
@spring-projects-issues

Description

@spring-projects-issues

Michael Decker opened SPR-16651 and commented

We are using Spring Boot 2.0.0.RELEASE (jdk1.8.0_144, Windows 10)

We had to realize, that org.springframework.util.StringUtils.parseLocaleString(String) does not return always the same instance, as it was created from:

from '' (und) to 'null (null)'
from 'sr_BA_#Latn' (sr-Latn-BA) to 'sr_BA_#Latn (sr-BA)'
from 'ja_JP_JP_#u-ca-japanese' (ja-JP-u-ca-japanese-x-lvariant-JP) to 'ja_JP_JP_#u-ca-japanese (ja-JP-x-lvariant-JP)'
from 'sr_ME_#Latn' (sr-Latn-ME) to 'sr_ME_#Latn (sr-ME)'
from 'sr__#Latn' (sr-Latn) to 'sr_#LATN (sr)'
from 'th_TH_TH_#u-nu-thai' (th-TH-u-nu-thai-x-lvariant-TH) to 'th_TH_TH_#u-nu-thai (th-TH-x-lvariant-TH)'
from 'sr_RS_#Latn' (sr-Latn-RS) to 'sr_RS_#Latn (sr-RS)'

The test is:

import static org.junit.Assert.assertEquals;
import static org.springframework.util.StringUtils.parseLocale;
import static org.springframework.util.StringUtils.parseLocaleString;

import java.util.List;
import java.util.Locale;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.junit.Test;

public class SpringStringUtilsParseLocaleTest {

  @Test
  public void testParseLocale() {
    List<String> failing = Stream.of(Locale.getAvailableLocales()) //
        .filter(locale -> !locale.equals(parseLocale(locale.toString()))) //
        .map(locale -> String.format("from '%s' (%s) to '%s (%s)'", locale, toLanguageTag(locale),
            parseLocale(locale.toString()), toLanguageTag(parseLocale(locale.toString())))) //
        .collect(Collectors.toList());

    // assertEquals(Collections.emptyList(), failing);
    assertEquals("", failing.stream().collect(Collectors.joining("\n")));
  }

  @Test
  public void testParseLocaleString() {
    List<String> failing = Stream.of(Locale.getAvailableLocales()) //
        .filter(locale -> !locale.equals(parseLocaleString(locale.toString()))) //
        .map(locale -> String.format("from '%s' (%s) to '%s (%s)'", locale, toLanguageTag(locale),
            parseLocaleString(locale.toString()), toLanguageTag(parseLocaleString(locale.toString())))) //
        .collect(Collectors.toList());

    // assertEquals(Collections.emptyList(), failing);
    assertEquals("", failing.stream().collect(Collectors.joining("\n")));
  }

  private static String toLanguageTag(Locale locale) {
    return locale == null ? null : locale.toLanguageTag();
  }

}

Affects: 5.0.4

Issue Links:

Referenced from: commits cab35aa, 55563c1, 88e4006

Metadata

Metadata

Assignees

Labels

in: coreIssues in core modules (aop, beans, core, context, expression)type: enhancementA general enhancement

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions