diff --git a/src/java.base/share/classes/java/util/Locale.java b/src/java.base/share/classes/java/util/Locale.java index 1dd4c2feca7..f6abefb29a7 100644 --- a/src/java.base/share/classes/java/util/Locale.java +++ b/src/java.base/share/classes/java/util/Locale.java @@ -3266,9 +3266,7 @@ public final class Locale implements Cloneable, Serializable { * or greater than {@code MAX_WEIGHT} */ public LanguageRange(String range, double weight) { - if (range == null) { - throw new NullPointerException(); - } + Objects.requireNonNull(range); if (weight < MIN_WEIGHT || weight > MAX_WEIGHT) { throw new IllegalArgumentException("weight=" + weight); } @@ -3278,8 +3276,8 @@ public final class Locale implements Cloneable, Serializable { // Do syntax check. boolean isIllFormed = false; String[] subtags = range.split("-"); - if (isSubtagIllFormed(subtags[0], true) - || range.endsWith("-")) { + if (range.endsWith("-") || + isSubtagIllFormed(subtags[0], true)) { isIllFormed = true; } else { for (int i = 1; i < subtags.length; i++) { diff --git a/test/jdk/java/util/Locale/LRToString.java b/test/jdk/java/util/Locale/LRToString.java deleted file mode 100644 index 229507b71b3..00000000000 --- a/test/jdk/java/util/Locale/LRToString.java +++ /dev/null @@ -1,64 +0,0 @@ -/* - * Copyright (c) 2016, 2023, Oracle and/or its affiliates. All rights reserved. - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * This code is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 2 only, as - * published by the Free Software Foundation. - * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA - * or visit www.oracle.com if you need additional information or have any - * questions. - */ - -/* - * @test - * @bug 8026766 - * @summary Confirm that LanguageRange.toString() returns an expected result. - * @run junit LRToString - */ - -import java.util.Locale.LanguageRange; -import java.util.stream.Stream; - -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.Arguments; -import org.junit.jupiter.params.provider.MethodSource; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -public class LRToString { - - /** - * This test ensures that the output of LanguageRange.toString() - * returns an expected result, that is, the weight is hidden if it is - * equal to 1.0. - */ - @ParameterizedTest - @MethodSource("ranges") - public void toStringTest(String range, double weight) { - LanguageRange lr = new LanguageRange(range, weight); - String expected = weight == 1.0 - ? range - : range+";q="+weight; - assertEquals(lr.toString(), expected); - } - - private static Stream ranges() { - return Stream.of( - Arguments.of("ja", 1.0), - Arguments.of("de", 0.5), - Arguments.of("fr", 0.0) - ); - } -} diff --git a/test/jdk/java/util/Locale/LanguageRangeTest.java b/test/jdk/java/util/Locale/LanguageRangeTest.java index cb0dd9fda66..d269cb9dc15 100644 --- a/test/jdk/java/util/Locale/LanguageRangeTest.java +++ b/test/jdk/java/util/Locale/LanguageRangeTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2020, 2025, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -20,21 +20,62 @@ * or visit www.oracle.com if you need additional information or have any * questions. */ -/** + +/* * @test - * @bug 8253321 - * @summary test LanguageRange class - * @run testng LanguageRangeTest + * @bug 8026766 8253321 8349883 + * @summary LanguageRange tests: toString(), hashCode()/equals(), checking + * for IAE on ill-formed ranges + * @run junit LanguageRangeTest */ import static java.util.Locale.LanguageRange; -import org.testng.annotations.Test; -import static org.testng.Assert.assertEquals; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +import java.util.HashMap; +import java.util.Locale; +import java.util.stream.Stream; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; -@Test public class LanguageRangeTest { + // 8349883: Test endpoints w/ ill-formed language range fail with IAE + @ParameterizedTest + @MethodSource("illegalRanges") + public void illformedRangeTest(String range) { + // static parses + assertThrows(IllegalArgumentException.class, + () -> Locale.LanguageRange.parse(range)); + assertThrows(IllegalArgumentException.class, + () -> Locale.LanguageRange.parse(range, new HashMap<>())); + // ctors + assertThrows(IllegalArgumentException.class, + () -> new Locale.LanguageRange(range)); + assertThrows(IllegalArgumentException.class, + () -> new Locale.LanguageRange(range, Locale.LanguageRange.MIN_WEIGHT)); + } + + private static Stream illegalRanges() { + return Stream.of( + // 8349883 offending range + "-", + // Other general ill-formed test cases + "-foo", + "foo-", + "foo1", + "foo-123456789", + "*-*-", + "" + ); + } + + // 8253321: Ensure invoking hashCode does not affect equals result @Test public void hashCodeTest() { var range1 = new LanguageRange("en-GB", 0); @@ -45,4 +86,23 @@ public class LanguageRangeTest { range2.hashCode(); assertEquals(range1, range2); } + + // 8026766: toString() should hide weight if equal to MAX_WEIGHT (1.0) + @ParameterizedTest + @MethodSource("ranges") + public void toStringTest(String range, double weight) { + LanguageRange lr = new LanguageRange(range, weight); + String expected = weight == 1.0 + ? range + : range+";q="+weight; + assertEquals(lr.toString(), expected); + } + + private static Stream ranges() { + return Stream.of( + Arguments.of("ja", 1.0), + Arguments.of("de", 0.5), + Arguments.of("fr", 0.0) + ); + } }