1 /*
2  * Copyright (c) 2005, 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 package test.java.math.BigDecimal;
24 
25 /*
26  * @test
27  * @bug 6177836
28  * @summary Verify BigDecimal objects with collapsed values are serialized properly.
29  * @author Joseph D. Darcy
30  */
31 
32 import java.math.*;
33 import java.io.*;
34 
35 import org.testng.Assert;
36 import org.testng.annotations.Test;
37 
38 // Android-changed: Replace error counting with asserts.
39 public class SerializationTests {
40 
checkSerialForm(BigDecimal bd)41     static void checkSerialForm(BigDecimal bd) throws Exception {
42         ByteArrayOutputStream bos = new ByteArrayOutputStream();
43         ObjectOutputStream oos = new ObjectOutputStream(bos);
44         oos.writeObject(bd);
45         oos.flush();
46         oos.close();
47         ObjectInputStream ois = new
48             ObjectInputStream(new ByteArrayInputStream(bos.toByteArray()));
49         BigDecimal tmp = (BigDecimal)ois.readObject();
50 
51         if (!bd.equals(tmp) || bd.hashCode() != tmp.hashCode()) {
52             Assert.fail("  original : " + bd +
53                 " (hash: 0x" + Integer.toHexString(bd.hashCode()) + ")" +
54                 " serialized : " + tmp +
55                 " (hash: 0x" + Integer.toHexString(tmp.hashCode()) + ")");
56         }
57     }
58 
59     @Test
testSerialization()60     public void testSerialization() throws Exception {
61         BigDecimal values[] = {
62             BigDecimal.ZERO,
63             BigDecimal.ONE,
64             BigDecimal.TEN,
65             new BigDecimal(0),
66             new BigDecimal(1),
67             new BigDecimal(10),
68             new BigDecimal(Integer.MAX_VALUE),
69             new BigDecimal(Long.MAX_VALUE-1),
70             new BigDecimal(BigInteger.valueOf(1), 1),
71             new BigDecimal(BigInteger.valueOf(100), 50),
72         };
73 
74         for(BigDecimal value : values) {
75             checkSerialForm(value);
76             checkSerialForm(value.negate());
77         }
78 
79     }
80 }
81