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