diff --git a/src/java.base/share/classes/java/text/MessageFormat.java b/src/java.base/share/classes/java/text/MessageFormat.java index 2df3b2f0aa3..8a8ca3009fc 100644 --- a/src/java.base/share/classes/java/text/MessageFormat.java +++ b/src/java.base/share/classes/java/text/MessageFormat.java @@ -391,11 +391,19 @@ public class MessageFormat extends Format { * Patterns and their interpretation are specified in the * class description. * + * @implSpec The default implementation throws a + * {@code NullPointerException} if {@code locale} is {@code null} + * either during the creation of the {@code MessageFormat} object or later + * when {@code format()} is called by a {@code MessageFormat} + * instance with a null locale and the implementation utilizes a + * locale-dependent subformat. + * * @param pattern the pattern for this message format * @param locale the locale for this message format * @throws IllegalArgumentException if the pattern is invalid * @throws NullPointerException if {@code pattern} is - * {@code null} + * {@code null} or {@code locale} is {@code null} and the + * implementation uses a locale-dependent subformat. * @since 1.4 */ public MessageFormat(String pattern, Locale locale) { @@ -844,7 +852,10 @@ public class MessageFormat extends Format { * @throws IllegalArgumentException if an argument in the * {@code arguments} array is not of the type * expected by the format element(s) that use it. - * @throws NullPointerException if {@code result} is {@code null} + * @throws NullPointerException if {@code result} is {@code null} or + * if the {@code MessageFormat} instance that calls this method + * has locale set to null, and the implementation + * uses a locale-dependent subformat. */ public final StringBuffer format(Object[] arguments, StringBuffer result, FieldPosition pos) @@ -890,7 +901,10 @@ public class MessageFormat extends Format { * @throws IllegalArgumentException if an argument in the * {@code arguments} array is not of the type * expected by the format element(s) that use it. - * @throws NullPointerException if {@code result} is {@code null} + * @throws NullPointerException if {@code result} is {@code null} or + * if the {@code MessageFormat} instance that calls this method + * has locale set to null, and the implementation + * uses a locale-dependent subformat. */ public final StringBuffer format(Object arguments, StringBuffer result, FieldPosition pos) diff --git a/test/jdk/java/text/Format/MessageFormat/Bug6481179.java b/test/jdk/java/text/Format/MessageFormat/Bug6481179.java deleted file mode 100644 index be8e9e29f11..00000000000 --- a/test/jdk/java/text/Format/MessageFormat/Bug6481179.java +++ /dev/null @@ -1,64 +0,0 @@ -/* - * Copyright (c) 2011, 2016, 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 - * @summary Confirm that a bug in an error message has been fixed. - * @bug 6481179 - */ - -import java.text.MessageFormat; -import java.text.ParseException; - -public class Bug6481179 { - - public static void main(String[] args) { - boolean err = false; - - try { - MessageFormat.format("Testdata {1,invalid_format_type}", - new Object[] { "val0", "val1" }); - - System.err.println("Error: IllegalArgumentException should be thrown."); - err = true; - } - catch (IllegalArgumentException e) { - String expected = "unknown format type: invalid_format_type"; - String got = e.getMessage(); - - if (!expected.equals(got)) { - System.err.println("Error: Unexpected error message: " + got); - err = true; - } - } - catch (Exception e) { - System.err.println("Error: Unexpected exception was thrown: " + e); - err = true; - } - - if (err) { - throw new RuntimeException("Failed."); - } - } - -} diff --git a/test/jdk/java/text/Format/MessageFormat/MessageFormatExceptions.java b/test/jdk/java/text/Format/MessageFormat/MessageFormatExceptions.java new file mode 100644 index 00000000000..f28c3170e71 --- /dev/null +++ b/test/jdk/java/text/Format/MessageFormat/MessageFormatExceptions.java @@ -0,0 +1,83 @@ +/* + * Copyright (c) 2011, 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 + * @summary Validate some exceptions in MessageFormat + * @bug 6481179 8039165 + * @run junit MessageFormatExceptions + */ + +import java.text.MessageFormat; +import java.util.Locale; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +public class MessageFormatExceptions { + + // MessageFormat should throw NPE when constructed with a null pattern + @Test + public void nullPatternTest() { + assertThrows(NullPointerException.class, () -> new MessageFormat(null)); + assertThrows(NullPointerException.class, () -> new MessageFormat(null, Locale.US)); + assertThrows(NullPointerException.class, + () -> MessageFormat.format(null, new Object[] { "val0", "val1" })); + } + + // 8039165: When MessageFormat is constructed with a null locale a NPE + // can potentially be thrown depending on the subformat created. Either + // during the creation of the object itself, or later when format() is called. + // The following are some examples. + @Test + public void nullLocaleTest() { + // Fails when constructor invokes applyPattern() + assertThrows(NullPointerException.class, + () -> new MessageFormat("{0, date}", null)); + // Fail when constructor invokes applyPattern() + assertThrows(NullPointerException.class, + () -> new MessageFormat("{0, number}", null)); + // Fail when object calls format() + assertThrows(NullPointerException.class, + () -> new MessageFormat("{0}", null).format(new Object[]{42})); + // Fail when object calls format(), but locale is set via .setLocale() + MessageFormat msgFmt = new MessageFormat("{0}"); + msgFmt.setLocale(null); + assertThrows(NullPointerException.class, () -> msgFmt.format(new Object[]{42})); + // Does not always fail if locale is null + assertDoesNotThrow(() -> + new MessageFormat("{0}", null).format(new Object[]{"hello"})); + + } + + // 6481179: Invalid format type should be provided in error message of IAE + @Test + public void formatMsgTest() { + IllegalArgumentException iae = assertThrows(IllegalArgumentException.class, + () -> MessageFormat.format("Testdata {1,invalid_format_type}", new Object[] { "val0", "val1" })); + assertEquals("unknown format type: invalid_format_type", iae.getMessage()); + } +}