darcy | 32db449 | 2009-01-26 19:49:26 -0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2005 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 21 | * have any questions. |
| 22 | */ |
| 23 | |
| 24 | /* |
| 25 | * @test |
| 26 | * @bug 6177836 |
| 27 | * @summary Verify BigDecimal objects with collapsed values are serialized properly. |
| 28 | * @author Joseph D. Darcy |
| 29 | */ |
| 30 | |
| 31 | import java.math.*; |
| 32 | import java.io.*; |
| 33 | |
| 34 | public class SerializationTests { |
| 35 | |
| 36 | static void checkSerialForm(BigDecimal bd) throws Exception { |
| 37 | ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
| 38 | ObjectOutputStream oos = new ObjectOutputStream(bos); |
| 39 | oos.writeObject(bd); |
| 40 | oos.flush(); |
| 41 | oos.close(); |
| 42 | ObjectInputStream ois = new |
| 43 | ObjectInputStream(new ByteArrayInputStream(bos.toByteArray())); |
| 44 | BigDecimal tmp = (BigDecimal)ois.readObject(); |
| 45 | |
| 46 | if (!bd.equals(tmp) || |
| 47 | bd.hashCode() != tmp.hashCode()) { |
| 48 | System.err.print(" original : " + bd); |
| 49 | System.err.println(" (hash: 0x" + Integer.toHexString(bd.hashCode()) + ")"); |
| 50 | System.err.print("serialized : " + tmp); |
| 51 | System.err.println(" (hash: 0x" + Integer.toHexString(tmp.hashCode()) + ")"); |
| 52 | throw new RuntimeException("Bad serial roundtrip"); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | public static void main(String[] args) throws Exception { |
| 57 | BigDecimal values[] = { |
| 58 | BigDecimal.ZERO, |
| 59 | BigDecimal.ONE, |
| 60 | BigDecimal.TEN, |
| 61 | new BigDecimal(0), |
| 62 | new BigDecimal(1), |
| 63 | new BigDecimal(10), |
| 64 | new BigDecimal(Integer.MAX_VALUE), |
| 65 | new BigDecimal(Long.MAX_VALUE-1), |
| 66 | new BigDecimal(BigInteger.valueOf(1), 1), |
| 67 | new BigDecimal(BigInteger.valueOf(100), 50), |
| 68 | }; |
| 69 | |
| 70 | for(BigDecimal value : values) { |
| 71 | checkSerialForm(value); |
| 72 | checkSerialForm(value.negate()); |
| 73 | } |
| 74 | |
| 75 | } |
| 76 | } |