Skip to content
This repository has been archived by the owner on Oct 21, 2023. It is now read-only.

Commit

Permalink
8039165: [Doc] MessageFormat null locale generates NullPointerException
Browse files Browse the repository at this point in the history
Reviewed-by: naoto, iris
Backport-of: c6396dceb9a64578d5b335af27ad1d968190a1fa
  • Loading branch information
Justin Lu committed Aug 1, 2023
1 parent a228185 commit f34ac12
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 67 deletions.
20 changes: 17 additions & 3 deletions src/java.base/share/classes/java/text/MessageFormat.java
Original file line number Diff line number Diff line change
Expand Up @@ -390,11 +390,19 @@ public MessageFormat(String pattern) {
* Patterns and their interpretation are specified in the
* <a href="#patterns">class description</a>.
*
* @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) {
Expand Down Expand Up @@ -843,7 +851,10 @@ public Format[] getFormats() {
* @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)
Expand Down Expand Up @@ -889,7 +900,10 @@ public static String format(String pattern, Object ... arguments) {
* @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)
Expand Down
64 changes: 0 additions & 64 deletions test/jdk/java/text/Format/MessageFormat/Bug6481179.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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());
}
}

0 comments on commit f34ac12

Please sign in to comment.