diff --git a/src/java.base/share/classes/java/text/ChoiceFormat.java b/src/java.base/share/classes/java/text/ChoiceFormat.java index 883c8b5d46b..1252efe33b5 100644 --- a/src/java.base/share/classes/java/text/ChoiceFormat.java +++ b/src/java.base/share/classes/java/text/ChoiceFormat.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1996, 2023, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1996, 2024, 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 @@ -614,6 +614,17 @@ public class ChoiceFormat extends NumberFormat { return result; } + /** + * {@return a string identifying this {@code ChoiceFormat}, for debugging} + */ + @Override + public String toString() { + return + """ + ChoiceFormat [pattern: "%s"] + """.formatted(toPattern()); + } + /** * Compares the specified object with this {@code ChoiceFormat} for equality. * Returns true if the object is also a {@code ChoiceFormat} and the diff --git a/src/java.base/share/classes/java/text/CompactNumberFormat.java b/src/java.base/share/classes/java/text/CompactNumberFormat.java index 3926a807b41..1bda7855fba 100644 --- a/src/java.base/share/classes/java/text/CompactNumberFormat.java +++ b/src/java.base/share/classes/java/text/CompactNumberFormat.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018, 2023, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2018, 2024, 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 @@ -2391,6 +2391,17 @@ public final class CompactNumberFormat extends NumberFormat { + Boolean.hashCode(parseBigDecimal); } + /** + * {@return a string identifying this {@code CompactNumberFormat}, for debugging} + */ + @Override + public String toString() { + return + """ + CompactNumberFormat [locale: "%s", decimal pattern: "%s", compact patterns: "%s"] + """.formatted(symbols.getLocale().getDisplayName(), decimalPattern, Arrays.toString(compactPatterns)); + } + /** * Creates and returns a copy of this {@code CompactNumberFormat} * instance. diff --git a/src/java.base/share/classes/java/text/DecimalFormat.java b/src/java.base/share/classes/java/text/DecimalFormat.java index 23c20266f41..a94e7ec5981 100644 --- a/src/java.base/share/classes/java/text/DecimalFormat.java +++ b/src/java.base/share/classes/java/text/DecimalFormat.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1996, 2023, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1996, 2024, 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 @@ -2996,6 +2996,17 @@ public class DecimalFormat extends NumberFormat { // just enough fields for a reasonable distribution } + /** + * {@return a string identifying this {@code DecimalFormat}, for debugging} + */ + @Override + public String toString() { + return + """ + DecimalFormat [locale: "%s", pattern: "%s"] + """.formatted(symbols.getLocale().getDisplayName(), toPattern()); + } + /** * Synthesizes a pattern string that represents the current state * of this Format object. diff --git a/src/java.base/share/classes/java/text/ListFormat.java b/src/java.base/share/classes/java/text/ListFormat.java index cd26f68bf46..bc155605934 100644 --- a/src/java.base/share/classes/java/text/ListFormat.java +++ b/src/java.base/share/classes/java/text/ListFormat.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2023, 2024, 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 @@ -530,7 +530,7 @@ public final class ListFormat extends Format { } /** - * {@inheritDoc} + * {@return a string identifying this {@code ListFormat}, for debugging} */ @Override public String toString() { diff --git a/src/java.base/share/classes/java/text/MessageFormat.java b/src/java.base/share/classes/java/text/MessageFormat.java index 1f265c63da8..fea896281c8 100644 --- a/src/java.base/share/classes/java/text/MessageFormat.java +++ b/src/java.base/share/classes/java/text/MessageFormat.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1996, 2023, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1996, 2024, 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 @@ -1184,6 +1184,17 @@ public class MessageFormat extends Format { return pattern.hashCode(); // enough for reasonable distribution } + /** + * {@return a string identifying this {@code MessageFormat}, for debugging} + */ + @Override + public String toString() { + return + """ + MessageFormat [locale: %s, pattern: "%s"] + """.formatted(locale == null ? null : '"' + locale.getDisplayName() + '"', toPattern()); + } + /** * Defines constants that are used as attribute keys in the diff --git a/src/java.base/share/classes/java/text/SimpleDateFormat.java b/src/java.base/share/classes/java/text/SimpleDateFormat.java index ee6ff005c49..d2f71bd7751 100644 --- a/src/java.base/share/classes/java/text/SimpleDateFormat.java +++ b/src/java.base/share/classes/java/text/SimpleDateFormat.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1996, 2023, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1996, 2024, 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 @@ -2422,6 +2422,17 @@ public class SimpleDateFormat extends DateFormat { // just enough fields for a reasonable distribution } + /** + * {@return a string identifying this {@code SimpleDateFormat}, for debugging} + */ + @Override + public String toString() { + return + """ + SimpleDateFormat [locale: %s, pattern: "%s"] + """.formatted(locale == null ? null : '"' + locale.getDisplayName() + '"', toPattern()); + } + /** * Compares the specified object with this {@code SimpleDateFormat} for equality. * Returns true if the object is also a {@code SimpleDateFormat} and the diff --git a/test/jdk/java/text/Format/ChoiceFormat/ToStringTest.java b/test/jdk/java/text/Format/ChoiceFormat/ToStringTest.java new file mode 100644 index 00000000000..4b6c84a0cb2 --- /dev/null +++ b/test/jdk/java/text/Format/ChoiceFormat/ToStringTest.java @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2024, 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 8321545 + * @summary Ensure value returned by overridden toString method is as expected + * @run junit ToStringTest + */ + +import java.text.ChoiceFormat; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class ToStringTest { + + // Check a normal expected value. There is no null locale test as + // ChoiceFormat is not localized. + @Test + public void normalValueTest() { + String expectedStr = "ChoiceFormat [pattern: \"1.0#foo\"]\n"; + var c = new ChoiceFormat("1.0#foo"); + assertEquals(expectedStr, c.toString()); + } + + // Ensure toString throws no exception when ChoiceFormat is created + // with empty arrays + @Test + public void oddValueTest() { + var c2 = new ChoiceFormat(new double[]{}, new String[]{}); + System.out.println(c2); + } +} diff --git a/test/jdk/java/text/Format/CompactNumberFormat/ToStringTest.java b/test/jdk/java/text/Format/CompactNumberFormat/ToStringTest.java new file mode 100644 index 00000000000..29ac0edef80 --- /dev/null +++ b/test/jdk/java/text/Format/CompactNumberFormat/ToStringTest.java @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2024, 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 8321545 + * @summary Ensure value returned by overridden toString method is as expected + * @run junit ToStringTest + */ + +import java.text.CompactNumberFormat; +import java.text.DecimalFormatSymbols; +import java.text.NumberFormat; +import java.util.Locale; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class ToStringTest { + + // Check a normal expected value. There is no null test as the getInstance + // methods and constructor (which takes DFS) throw NPE if the locale param is null. + @Test + public void expectedValueTest() { + String expectedStr = "CompactNumberFormat [locale: \"English (Canada)\", decimal pattern: \"#,##0.###\", compact patterns: \"[, , , {one:0' 'thousand other:0' 'thousand}, {one:00' 'thousand other:00' 'thousand}, {one:000' 'thousand other:000' 'thousand}, {one:0' 'million other:0' 'million}, {one:00' 'million other:00' 'million}, {one:000' 'million other:000' 'million}, {one:0' 'billion other:0' 'billion}, {one:00' 'billion other:00' 'billion}, {one:000' 'billion other:000' 'billion}, {one:0' 'trillion other:0' 'trillion}, {one:00' 'trillion other:00' 'trillion}, {one:000' 'trillion other:000' 'trillion}]\"]\n"; + var c = NumberFormat.getCompactNumberInstance(Locale.CANADA, NumberFormat.Style.LONG); + assertEquals(expectedStr, c.toString()); + } + + // Check an odd value with empty decimal pattern and compact patterns. + @Test + public void oddValueTest() { + String expectedStr = "CompactNumberFormat [locale: \"English (Canada)\", decimal pattern: \"\", compact patterns: \"[]\"]\n"; + var c = new CompactNumberFormat("", new DecimalFormatSymbols(Locale.CANADA), new String[]{}); + assertEquals(expectedStr, c.toString()); + } +} diff --git a/test/jdk/java/text/Format/DateFormat/ToStringTest.java b/test/jdk/java/text/Format/DateFormat/ToStringTest.java new file mode 100644 index 00000000000..4a210f12d5d --- /dev/null +++ b/test/jdk/java/text/Format/DateFormat/ToStringTest.java @@ -0,0 +1,82 @@ +/* + * Copyright (c) 2024, 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 8321545 + * @library /java/text/testlib + * @summary Ensure value returned by overridden toString method is as expected + * @run junit ToStringTest + */ + +import java.io.InputStream; +import java.io.ObjectInputStream; +import java.text.DateFormat; +import java.text.DateFormatSymbols; +import java.text.SimpleDateFormat; +import java.util.Locale; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class ToStringTest { + + // Check a normal expected value + @Test + public void normalValueTest() { + String expectedStr = + "SimpleDateFormat [locale: \"English (Canada)\", pattern: \"MMM d, y\"]\n"; + var s = DateFormat.getDateInstance(DateFormat.DEFAULT, Locale.CANADA); + assertEquals(expectedStr, s.toString()); + } + + // Check an odd value. SimpleDateFormat constructor that takes DFS, will use + // the default locale, not the locale from the DFS. + @Test + public void oddValueTest() { + String expectedStr = + "SimpleDateFormat [locale: \"" + Locale.getDefault().getDisplayName() + "\", pattern: \"MMM d, y\"]\n"; + var s = new SimpleDateFormat("MMM d, y", new DateFormatSymbols(Locale.JAPAN)); + assertEquals(expectedStr, s.toString()); + } + + + // Check the expected value when the locale is null. This is only possible + // via an older SimpleDateFormat that was deserialized. The current constructor + // will throw NPE if locale is null. + @Test + public void nullLocaleTest() { + String expectedStr = + "SimpleDateFormat [locale: null, pattern: \"yyyy.MM.dd E hh.mm.ss zzz\"]\n"; + // Borrowed from DateFormatSymbolsSerializationTest + SimpleDateFormat s; + try (InputStream is = HexDumpReader.getStreamFromHexDump("SDFserialized.ser.txt"); + ObjectInputStream iStream = new ObjectInputStream(is)) { + s = (SimpleDateFormat)iStream.readObject(); + assertEquals(expectedStr, s.toString()); + } catch (Exception e) { + System.out.println("Error building stream from deserialized simple date format"); + } + } +} diff --git a/test/jdk/java/text/Format/DecimalFormat/ToStringTest.java b/test/jdk/java/text/Format/DecimalFormat/ToStringTest.java new file mode 100644 index 00000000000..ecbd5000a2b --- /dev/null +++ b/test/jdk/java/text/Format/DecimalFormat/ToStringTest.java @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2024, 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 8321545 + * @summary Ensure value returned by overridden toString method is as expected + * @run junit ToStringTest + */ + +import java.text.DecimalFormat; +import java.text.DecimalFormatSymbols; +import java.text.NumberFormat; +import java.util.Locale; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class ToStringTest { + + // Check a normal expected value. There is no null locale test as + // DecimalFormatSymbols will throw an exception when created with a null locale. + @Test + public void expectedValueTest() { + String expectedStr = + "DecimalFormat [locale: \"English (Canada)\", pattern: \"foo#00.00bar\"]\n"; + var d = new DecimalFormat("foo#00.00bar", new DecimalFormatSymbols(Locale.CANADA)); + assertEquals(expectedStr, d.toString()); + + String expectedStr2 = + "DecimalFormat [locale: \"English (Canada)\", pattern: \"#,##0.###\"]\n"; + var d2 = NumberFormat.getInstance(Locale.CANADA); + assertEquals(expectedStr2, d2.toString()); + } +} diff --git a/test/jdk/java/text/Format/ListFormat/ToStringTest.java b/test/jdk/java/text/Format/ListFormat/ToStringTest.java new file mode 100644 index 00000000000..a8a1e127f84 --- /dev/null +++ b/test/jdk/java/text/Format/ListFormat/ToStringTest.java @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2024, 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 8321545 + * @summary Ensure value returned by overridden toString method is as expected + * @run junit ToStringTest + */ + +import java.text.ListFormat; +import java.util.Locale; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class ToStringTest { + + // Check a normal expected value. There is no null test as the getInstance + // methods throw NPE if any arguments are null. + @Test + public void expectedValueTest() { + String expectedStr = + "ListFormat [locale: \"English (Canada)\", start: \"{0}, {1}\", middle: \"{0}, {1}\", end: \"{0} and {1}\", two: \"{0} and {1}\", three: \"{0}, {1} and {2}\"]\n"; + var l = ListFormat.getInstance(Locale.CANADA, ListFormat.Type.STANDARD, ListFormat.Style.FULL); + assertEquals(expectedStr, l.toString()); + } +} diff --git a/test/jdk/java/text/Format/MessageFormat/ToStringTest.java b/test/jdk/java/text/Format/MessageFormat/ToStringTest.java new file mode 100644 index 00000000000..01eaf7dd353 --- /dev/null +++ b/test/jdk/java/text/Format/MessageFormat/ToStringTest.java @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2024, 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 8321545 + * @summary Ensure value returned by overridden toString method is as expected + * @run junit ToStringTest + */ + +import java.text.MessageFormat; +import java.util.Locale; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class ToStringTest { + + // Check a normal expected value + @Test + public void expectedValueTest() { + String expectedStr = + "MessageFormat [locale: \"English (Canada)\", pattern: \"foo {0}\"]\n"; + var m = new MessageFormat("foo {0}", Locale.CANADA); + assertEquals(expectedStr, m.toString()); + } + + // Check the expected value when the locale is null + @Test + public void nullLocaleTest() { + String expectedStr = + "MessageFormat [locale: null, pattern: \"foo {0}\"]\n"; + var m = new MessageFormat("foo {0}", null); + assertEquals(expectedStr, m.toString()); + } +}