J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 1997-1999 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 | * @bug 4088176 |
| 26 | * @build WriteAddedField ReadAddedField |
| 27 | * @run main ReadAddedField |
| 28 | * |
| 29 | * @summary Deserialize an evolved class with a new field, field type is new. |
| 30 | * |
| 31 | */ |
| 32 | |
| 33 | import java.io.*; |
| 34 | |
| 35 | class IncompatibleFieldClass implements Serializable { |
| 36 | private static long serialVersionUID = 4L; |
| 37 | int x = 5; |
| 38 | }; |
| 39 | |
| 40 | class A implements Serializable { |
| 41 | // Version 1.0 of class A. |
| 42 | private static final long serialVersionUID = 1L; |
| 43 | public int bar; |
| 44 | } |
| 45 | |
| 46 | /** Test serial persistent fields w/o using Alternate API. |
| 47 | */ |
| 48 | class B implements Serializable { |
| 49 | private static final long serialVersionUID = 2L; |
| 50 | int bar; |
| 51 | |
| 52 | B() { |
| 53 | bar = 4; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | /** Test serial persistent fields using Alternate API. |
| 58 | * Also make sure that optional data to non-existent classes can be skipped. |
| 59 | */ |
| 60 | class C implements Serializable { |
| 61 | private static final long serialVersionUID = 3L; |
| 62 | int bar; |
| 63 | |
| 64 | C() { |
| 65 | bar = 4; |
| 66 | } |
| 67 | |
| 68 | private void readObject(ObjectInputStream s) |
| 69 | throws IOException, ClassNotFoundException |
| 70 | { |
| 71 | s.defaultReadObject(); |
| 72 | } |
| 73 | |
| 74 | private void writeObject(ObjectOutputStream s) |
| 75 | throws IOException |
| 76 | { |
| 77 | s.defaultWriteObject(); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | public class ReadAddedField { |
| 82 | public static void main(String args[]) |
| 83 | throws IOException, ClassNotFoundException |
| 84 | { |
| 85 | File f = new File("tmp.ser"); |
| 86 | ObjectInput in = |
| 87 | new ObjectInputStream(new FileInputStream(f)); |
| 88 | A a = (A)in.readObject(); |
| 89 | if (a.bar != 4) |
| 90 | throw new RuntimeException("a.bar does not equal 4, it equals " + |
| 91 | a.bar); |
| 92 | B b = (B)in.readObject(); |
| 93 | if (b.bar != 4) |
| 94 | throw new RuntimeException("b.bar does not equal 4, it equals " + |
| 95 | b.bar); |
| 96 | C c = (C)in.readObject(); |
| 97 | if (c.bar != 4) |
| 98 | throw new RuntimeException("c.bar does not equal 4, it equals " + |
| 99 | c.bar); |
| 100 | A aa = (A)in.readObject(); |
| 101 | if (aa.bar != 4) |
| 102 | throw new RuntimeException("a.bar does not equal 4, it equals " + |
| 103 | aa.bar); |
| 104 | B bb = (B)in.readObject(); |
| 105 | if (bb.bar != 4) |
| 106 | throw new RuntimeException("b.bar does not equal 4, it equals " + |
| 107 | bb.bar); |
| 108 | C cc = (C)in.readObject(); |
| 109 | if (cc.bar != 4) |
| 110 | throw new RuntimeException("c.bar does not equal 4, it equals " + |
| 111 | cc.bar); |
| 112 | in.close(); |
| 113 | } |
| 114 | } |