1 /*
   2  * Copyright (c) 2018, 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  * @test
  25  * @bug 8177552
  26  * @modules jdk.localedata
  27  * @summary Checks the serialization feature of CompactNumberFormat
  28  */
  29 
  30 import java.io.FileInputStream;
  31 import java.io.FileOutputStream;
  32 import java.io.IOException;
  33 import java.io.ObjectInputStream;
  34 import java.io.ObjectOutputStream;
  35 import java.math.RoundingMode;
  36 import java.text.CompactNumberFormat;
  37 import java.text.NumberFormat;
  38 import java.util.Locale;
  39 
  40 public class SerializationTest {
  41 
  42     public static void main(String[] args) {
  43 
  44         NumberFormat format1 = NumberFormat.getCompactNumberInstance(
  45                 new Locale("hi"), NumberFormat.Style.SHORT);
  46         format1.setMinimumFractionDigits(2);
  47         format1.setMinimumIntegerDigits(5);
  48 
  49         CompactNumberFormat format2 = (CompactNumberFormat) NumberFormat
  50                 .getCompactNumberInstance(Locale.US, NumberFormat.Style.LONG);
  51         format2.setRoundingMode(RoundingMode.HALF_UP);
  52         format2.setGroupingSize(3);
  53 
  54         NumberFormat format3 = NumberFormat.getCompactNumberInstance(
  55                 Locale.JAPAN, NumberFormat.Style.SHORT);
  56         format3.setMaximumFractionDigits(30);
  57         format3.setMaximumIntegerDigits(30);
  58 
  59         NumberFormat format4 = NumberFormat.getCompactNumberInstance(
  60                 Locale.FRANCE, NumberFormat.Style.LONG);
  61         format4.setParseIntegerOnly(true);
  62         format4.setGroupingUsed(true);
  63 
  64         // setting minimum integer digits beyond the allowed range
  65         NumberFormat format5 = NumberFormat.getCompactNumberInstance(
  66                 Locale.GERMANY, NumberFormat.Style.SHORT);
  67         format5.setMinimumIntegerDigits(320);
  68 
  69         // setting minimum fraction digits beyond the allowed range
  70         NumberFormat format6 = NumberFormat.getCompactNumberInstance(
  71                 Locale.KOREA, NumberFormat.Style.SHORT);
  72         format6.setMinimumFractionDigits(350);
  73 
  74         try {
  75             // serialize
  76             serialize("cdf.ser", format1, format2, format3, format4, format5, format6);
  77             // deserialize
  78             deserialize("cdf.ser", format1, format2, format3, format4, format5, format6);
  79 
  80         } catch (IOException ex) {
  81             throw new RuntimeException("[FAILED: Exception occured while"
  82                     + " serializing/deserializing]", ex);
  83         } catch (ClassNotFoundException ex) {
  84             throw new RuntimeException("[FAILED: Exception occured while"
  85                     + " deserializing the object]", ex);
  86         }
  87 
  88     }
  89 
  90     private static void serialize(String fileName, NumberFormat... formats)
  91             throws IOException {
  92         try (ObjectOutputStream os = new ObjectOutputStream(
  93                 new FileOutputStream(fileName))) {
  94             for (NumberFormat fmt : formats) {
  95                 os.writeObject(fmt);
  96             }
  97         }
  98     }
  99 
 100     private static void deserialize(String fileName, NumberFormat... formats)
 101             throws IOException, ClassNotFoundException {
 102         try (ObjectInputStream os = new ObjectInputStream(
 103                 new FileInputStream(fileName))) {
 104             for (NumberFormat fmt : formats) {
 105                 NumberFormat obj = (NumberFormat) os.readObject();
 106                 if (!fmt.equals(obj)) {
 107                     throw new RuntimeException("serialized and deserialized"
 108                             + " objects do not match");
 109                 }
 110 
 111                 long number = 123456789789L;
 112                 String expected = fmt.format(number);
 113                 String actual = obj.format(number);
 114                 if (!expected.equals(actual)) {
 115                     throw new RuntimeException("serialized and deserialized"
 116                             + " objects are expected to return similar formatted"
 117                             + " output for number: " + number + ", Expected: "
 118                             + expected + ", Actual: " + actual);
 119                 }
 120             }
 121         }
 122     }
 123 }
 124