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.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 
26 /*
27  * @test
28  * @bug 8177552
29  * @modules jdk.localedata
30  * @summary Checks deserialization of compact number format
31  * @library /java/text/testlib
32  * @build TestDeserializeCNF HexDumpReader
33  * @run testng/othervm TestDeserializeCNF
34  */
35 
36 import org.testng.annotations.BeforeTest;
37 import org.testng.annotations.Test;
38 
39 import java.io.IOException;
40 import java.io.InputStream;
41 import java.io.ObjectInputStream;
42 import java.math.RoundingMode;
43 import java.text.CompactNumberFormat;
44 import java.text.DecimalFormatSymbols;
45 import java.util.Locale;
46 import static org.testng.Assert.*;
47 
48 public class TestDeserializeCNF {
49 
50     // This object is serialized in cnf1.ser.txt with HALF_UP
51     // rounding mode, groupingsize = 3 and parseBigDecimal = true
52     private static final CompactNumberFormat COMPACT_FORMAT1 = new CompactNumberFormat("#,##0.###",
53             DecimalFormatSymbols.getInstance(Locale.US),
54             new String[]{"", "", "", "0K", "00K", "000K", "0M", "00M", "000M", "0B", "00B", "000B", "0T", "00T", "000T"});
55 
56     // This object is serialized in cnf2.ser.txt with min integer digits = 20
57     // and min fraction digits = 5
58     private static final CompactNumberFormat COMPACT_FORMAT2 = new CompactNumberFormat("#,##0.###",
59             DecimalFormatSymbols.getInstance(Locale.JAPAN),
60             new String[]{"", "", "", "0", "0\u4e07", "00\u4e07", "000\u4e07", "0000\u4e07", "0\u5104", "00\u5104", "000\u5104", "0000\u5104", "0\u5146", "00\u5146", "000\u5146"});
61 
62     private static final String FILE_COMPACT_FORMAT1 = "cnf1.ser.txt";
63     private static final String FILE_COMPACT_FORMAT2 = "cnf2.ser.txt";
64 
65     @BeforeTest
mutateInstances()66     public void mutateInstances() {
67         COMPACT_FORMAT1.setRoundingMode(RoundingMode.HALF_UP);
68         COMPACT_FORMAT1.setGroupingSize(3);
69         COMPACT_FORMAT1.setParseBigDecimal(true);
70 
71         COMPACT_FORMAT2.setMinimumIntegerDigits(20);
72         COMPACT_FORMAT2.setMinimumFractionDigits(5);
73     }
74 
75     @Test
testDeserialization()76     public void testDeserialization() throws IOException, ClassNotFoundException {
77         try (InputStream istream1 = HexDumpReader.getStreamFromHexDump(FILE_COMPACT_FORMAT1);
78                 ObjectInputStream ois1 = new ObjectInputStream(istream1);
79                 InputStream istream2 = HexDumpReader.getStreamFromHexDump(FILE_COMPACT_FORMAT2);
80                 ObjectInputStream ois2 = new ObjectInputStream(istream2);) {
81 
82             CompactNumberFormat obj1 = (CompactNumberFormat) ois1.readObject();
83             assertEquals(obj1, COMPACT_FORMAT1, "Deserialized instance is not"
84                     + " equal to the instance serialized in " + FILE_COMPACT_FORMAT1);
85 
86             CompactNumberFormat obj2 = (CompactNumberFormat) ois2.readObject();
87             assertEquals(obj2, COMPACT_FORMAT2, "Deserialized instance is not"
88                     + " equal to the instance serialized in " + FILE_COMPACT_FORMAT2);
89         }
90     }
91 
92     // The objects are serialized using the serialize() method, the hex
93     // dump printed is copied to respective object files
94 //    void serialize(CompactNumberFormat cnf) {
95 //        ByteArrayOutputStream baos = new ByteArrayOutputStream();
96 //        try (ObjectOutputStream oos = new ObjectOutputStream(baos)) {
97 //            oos.writeObject(cnf);
98 //        } catch (IOException ioe) {
99 //            throw new RuntimeException(ioe);
100 //        }
101 //        byte[] ser = baos.toByteArray();
102 //        for (byte b : ser) {
103 //            System.out.print("" + String.format("%02x", b));
104 //        }
105 //    }
106 
107 }
108 
109