|
| 1 | +/* |
| 2 | + * Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * This code is free software; you can redistribute it and/or modify it |
| 6 | + * under the terms of the GNU General Public License version 2 only, as |
| 7 | + * published by the Free Software Foundation. |
| 8 | + * |
| 9 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | + * accompanied this code). |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License version |
| 16 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | + * |
| 19 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 20 | + * or visit www.oracle.com if you need additional information or have any |
| 21 | + * questions. |
| 22 | + */ |
| 23 | + |
| 24 | +/* |
| 25 | + * @test |
| 26 | + * @bug 8321545 |
| 27 | + * @library /java/text/testlib |
| 28 | + * @summary Ensure value returned by overridden toString method is as expected |
| 29 | + * @run junit ToStringTest |
| 30 | + */ |
| 31 | + |
| 32 | +import java.io.InputStream; |
| 33 | +import java.io.ObjectInputStream; |
| 34 | +import java.text.DateFormat; |
| 35 | +import java.text.DateFormatSymbols; |
| 36 | +import java.text.SimpleDateFormat; |
| 37 | +import java.util.Locale; |
| 38 | + |
| 39 | +import org.junit.jupiter.api.Test; |
| 40 | + |
| 41 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 42 | + |
| 43 | +public class ToStringTest { |
| 44 | + |
| 45 | + // Check a normal expected value |
| 46 | + @Test |
| 47 | + public void normalValueTest() { |
| 48 | + String expectedStr = |
| 49 | + "SimpleDateFormat [locale: \"English (Canada)\", pattern: \"MMM d, y\"]\n"; |
| 50 | + var s = DateFormat.getDateInstance(DateFormat.DEFAULT, Locale.CANADA); |
| 51 | + assertEquals(expectedStr, s.toString()); |
| 52 | + } |
| 53 | + |
| 54 | + // Check an odd value. SimpleDateFormat constructor that takes DFS, will use |
| 55 | + // the default locale, not the locale from the DFS. |
| 56 | + @Test |
| 57 | + public void oddValueTest() { |
| 58 | + String expectedStr = |
| 59 | + "SimpleDateFormat [locale: \"" + Locale.getDefault().getDisplayName() + "\", pattern: \"MMM d, y\"]\n"; |
| 60 | + var s = new SimpleDateFormat("MMM d, y", new DateFormatSymbols(Locale.JAPAN)); |
| 61 | + assertEquals(expectedStr, s.toString()); |
| 62 | + } |
| 63 | + |
| 64 | + |
| 65 | + // Check the expected value when the locale is null. This is only possible |
| 66 | + // via an older SimpleDateFormat that was deserialized. The current constructor |
| 67 | + // will throw NPE if locale is null. |
| 68 | + @Test |
| 69 | + public void nullLocaleTest() { |
| 70 | + String expectedStr = |
| 71 | + "SimpleDateFormat [locale: null, pattern: \"yyyy.MM.dd E hh.mm.ss zzz\"]\n"; |
| 72 | + // Borrowed from DateFormatSymbolsSerializationTest |
| 73 | + SimpleDateFormat s; |
| 74 | + try (InputStream is = HexDumpReader.getStreamFromHexDump("SDFserialized.ser.txt"); |
| 75 | + ObjectInputStream iStream = new ObjectInputStream(is)) { |
| 76 | + s = (SimpleDateFormat)iStream.readObject(); |
| 77 | + assertEquals(expectedStr, s.toString()); |
| 78 | + } catch (Exception e) { |
| 79 | + System.out.println("Error building stream from deserialized simple date format"); |
| 80 | + } |
| 81 | + } |
| 82 | +} |
0 commit comments