1 /*
2  * Copyright (c) 1999, 2016, 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 4278402
27  * @library /java/text/testlib
28  * @build DateFormatSymbolsSerializationTest HexDumpReader
29  * @run main DateFormatSymbolsSerializationTest
30  * @summary Make sure DateFormatSymbols serialization
31  */
32 
33 import java.util.*;
34 import java.text.*;
35 import java.io.*;
36 
37 public class DateFormatSymbolsSerializationTest {
38 
main(String[] args)39     public static void main(String[] args) throws Exception {
40         Locale reservedLocale = Locale.getDefault();
41         try {
42             boolean err = false;
43             Locale.setDefault(Locale.ENGLISH);
44             SimpleDateFormat sdf =
45                     new SimpleDateFormat("yyyy.MM.dd E hh.mm.ss zzz",
46                                                         Locale.ENGLISH);
47             Calendar calendar =
48                     new GregorianCalendar(TimeZone.getTimeZone("GMT"),
49                                                       Locale.ENGLISH);
50             calendar.setTime(new Date(0L));
51             DecimalFormat df = new DecimalFormat("");
52             df.setDecimalSeparatorAlwaysShown(true);
53             df.setGroupingSize(3);
54             df.setMultiplier(1);
55             df.setNegativePrefix("-");
56             df.setNegativeSuffix("");
57             df.setPositivePrefix("");
58             df.setPositiveSuffix("");
59             df.setMaximumFractionDigits(3); //
60             df.setMinimumIntegerDigits(1);  // for compatibility 1.2 and 1.3
61             df.setMaximumIntegerDigits(40); //
62 
63             sdf.setCalendar(calendar);
64             sdf.setNumberFormat(df);
65 
66             SimpleDateFormat sdf1;
67 
68             if (args.length > 0) {
69                 try (FileOutputStream fos =
70                         new FileOutputStream("SDFserialized.ser")) {
71                     ObjectOutputStream oStream = new ObjectOutputStream(fos);
72                     oStream.writeObject(sdf);
73                 } catch (Exception e) {
74                     e.printStackTrace();
75                 }
76             } else {
77                 try (InputStream is =
78                      HexDumpReader.getStreamFromHexDump("SDFserialized.ser.txt")) {
79                     ObjectInputStream iStream = new ObjectInputStream(is);
80                     sdf1 = (SimpleDateFormat)iStream.readObject();
81                 }
82 
83                 DateFormatSymbols dfs = sdf.getDateFormatSymbols();
84                 DateFormatSymbols dfs1 = sdf1.getDateFormatSymbols();
85                 System.out.println(sdf + "," + sdf.toPattern());
86                 System.out.println(sdf1 + "," + sdf1.toPattern());
87 
88                 // time zone display names should not be a part of this
89                 // compatibility test. See 4112924 and 4282899.
90                 dfs.setZoneStrings(dfs1.getZoneStrings());
91                 // localPatternChars should not be a part of this
92                 // compatibility test. See 4322313.
93                 dfs.setLocalPatternChars(dfs1.getLocalPatternChars());
94                 sdf.setDateFormatSymbols(dfs);
95 
96                 // decimal format symbols should not be part of this
97                 // compatibility test - old decimal format symbols get filled
98                 // in with the root locale (4290801)
99                 DecimalFormat df1 = (DecimalFormat) sdf1.getNumberFormat();
100                 df1.setDecimalFormatSymbols(df.getDecimalFormatSymbols());
101 
102                 if (!dfs.equals(dfs1)) {
103                     err = true;
104                     System.err.println(
105                         "Error: serialized DateFormatSymbols is different");
106                 }
107                 if (!sdf.equals(sdf1)) {
108                     err = true;
109                     System.err.println(
110                         "Error: serialized SimpleDateFormat is different");
111                 }
112                 if (err) {
113                     throw new Exception("Serialization failed.");
114                 }
115             }
116         } finally {
117             // restore the reserved locale
118             Locale.setDefault(reservedLocale);
119         }
120     }
121 }
122