<i18n dev> RFR: 8369590: LocaleEnhanceTest has incorrectly passing test case
Justin Lu
jlu at openjdk.org
Mon Oct 13 21:27:15 UTC 2025
On Mon, 13 Oct 2025 20:55:45 GMT, Justin Lu <jlu at openjdk.org> wrote:
> This PR corrects _test/jdk/java/util/Locale/LocaleEnhanceTest.java_, which has two test cases under `testBuilderSetLanguageTag()` which accidentally pass. One checks that Locale.setLanguageTag(String) throws ILE for duplicate extensions and the other for duplicate U-extension keys. The test cases are updated to actually test the provided code. When the test cases are fixed, they now fail.
>
> Fixing the behavior to match the expectation of those test cases is consistent with the specification.
>
> From `Locale.forLanguageTag(String)`,
>
>>
>> * <p>If the specified language tag contains any ill-formed subtags,
>> * the first such subtag and all following subtags are ignored. Compare
>> * to {@link Locale.Builder#setLanguageTag(String)} which throws an exception
>> * in this case.
>
> and the RFC specification
>
>> Each singleton subtag MUST appear at most one time in each tag
>> (other than as a private use subtag). That is, singleton subtags
>> MUST NOT be repeated. For example, the tag "en-a-bbb-a-ccc" is
>> invalid because the subtag 'a' appears twice.
>
> Since duplicate extensions (and Unicode keys/attributes) are invalid, throwing `IllformedLocaleException` in (the strict) `Locale.Builder` and ignoring in (the lenient) `Locale.forLanguageTag` for such tags would be appropriate. This PR updates the implementation as such.
src/java.base/share/classes/sun/util/locale/InternalLocaleBuilder.java line 212:
> 210:
> 211: if (UnicodeLocaleExtension.isSingletonChar(key.value())) {
> 212: setUnicodeLocaleExtension(val, true);
This code is called under `Locale.Builder.setExtension(char, String)`, which remains lenient for duplicate U-extension keys and attributes since this existing behavior is confirmed as a test case in _LocaleEnhanceTest_,
// redundant locale extensions are ignored
result = builder
.clear()
.setExtension('u', "nu-thai-NU-chinese-xx-1234")
.build()
.toLanguageTag();
assertEquals("duplicate keys", "und-u-nu-thai-xx-1234", result);
test/jdk/java/util/Locale/LocaleEnhanceTest.java line 767:
> 765:
> 766: // redundant Unicode locale extension keys within an Unicode locale extension cause a failure
> 767: new BuilderILE() { public void call() { b.setLanguageTag("und-u-nu-thai-NU-chinese-xx-1234"); }};
These lines both relied on the `BuilderILE` testing class. However, since they provided no args to the ctor, the `run()` call does not actually execute the provided code under `call()`. `BuilderILE` and the other helper testing classes are replaced with JUnit in https://github.com/openjdk/jdk/pull/27775/commits/f4927421ad3f7b2647ecd29059e2e64683b9f270.
-------------
PR Review Comment: https://git.openjdk.org/jdk/pull/27775#discussion_r2427333112
PR Review Comment: https://git.openjdk.org/jdk/pull/27775#discussion_r2427320692
More information about the i18n-dev
mailing list