WriteAddedField.java revision 0:37a05a11f281
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/* @test
25 * @bug 4088176
26 * @summary  Deserialize an evolved class with a new field, field type is new.
27 *
28 * @clean A B C NewExternField NewFieldClass ReadAddedField WriteAddedField
29 * @compile WriteAddedField.java
30 * @run main WriteAddedField
31 * @clean A B C NewExternField NewFieldClass ReadAddedField WriteAddedField
32 * @compile ReadAddedField.java
33 * @run main ReadAddedField
34
35
36 */
37import java.io.*;
38
39class NewFieldClass implements Serializable {
40    int k;
41
42    NewFieldClass(int value) {
43        k = value;
44    }
45};
46
47class IncompatibleFieldClass implements Serializable {
48    private static long serialVersionUID = 3L;
49    int x = 5;
50};
51
52class NewExternFieldClass implements Externalizable {
53    byte l;
54
55    public NewExternFieldClass(int value) {
56        l = (byte)value;
57    }
58
59    public void readExternal(ObjectInput s)
60        throws IOException, ClassNotFoundException
61    {
62        l = s.readByte();
63    }
64
65    public void writeExternal(ObjectOutput s) throws IOException
66    {
67        s.writeByte(l);
68    }
69}
70
71class A implements Serializable  {
72    // Version 1.1 of class A.  Added superclass NewSerializableSuper.
73    private static final long serialVersionUID = 1L;
74    NewFieldClass foo;
75    NewFieldClass fooarray[];
76    IncompatibleFieldClass boo;
77    IncompatibleFieldClass booarray[];
78    /* Excluded due to Bug 4089540
79    NewExternFieldClass abc;
80    NewExternFieldClass farray[];
81    */
82    int bar;
83    A() {
84        foo = new NewFieldClass(23);
85        fooarray = new NewFieldClass[24];
86        for (int i = 0; i < fooarray.length; i++)
87            fooarray[i] = new NewFieldClass(i);
88        boo = new IncompatibleFieldClass();
89        booarray = new IncompatibleFieldClass[24];
90        for (int i = 0; i < booarray.length; i++)
91            booarray[i] = new IncompatibleFieldClass();
92        bar = 4;
93        /* Excluded due to Bug 4089540
94        abc = new NewExternFieldClass(66);
95        farray = new NewExternFieldClass[10];
96        for (int k = 0; k < farray.length; k++)
97            farray[k] = new NewExternFieldClass(k);
98        */
99    }
100}
101
102/** Test serial persistent fields w/o using Alternate API.
103  */
104class B implements Serializable {
105    private static final long serialVersionUID = 2L;
106    NewFieldClass       foo;
107    NewFieldClass[]     array;
108    IncompatibleFieldClass boo;
109    IncompatibleFieldClass booarray[];
110    transient NewExternFieldClass abc;
111    transient NewExternFieldClass farray[];
112    int                 bar;
113
114    B() {
115        foo = new NewFieldClass(12);
116        array = new NewFieldClass[12];
117        for (int i = 0; i < array.length; i++)
118            array[i] = new NewFieldClass(i);
119        bar = 4;
120        abc = new NewExternFieldClass(66);
121        farray = new NewExternFieldClass[10];
122        for (int k = 0; k < farray.length; k++)
123            farray[k] = new NewExternFieldClass(k);
124    }
125}
126
127/** Test serial persistent fields using Alternate API.
128  * Also make sure that optional data to non-existent classes can be skipped.
129  */
130class C implements Serializable {
131    private static final long serialVersionUID = 3L;
132    NewFieldClass       foo;
133    NewFieldClass[]     array;
134    int                 bar;
135    transient NewExternFieldClass abc;
136    transient NewExternFieldClass farray[];
137
138    C() {
139        foo = new NewFieldClass(12);
140        array = new NewFieldClass[12];
141        for (int i = 0; i < array.length; i++)
142            array[i] = new NewFieldClass(i);
143        bar = 4;
144        abc = new NewExternFieldClass(3);
145        farray = new NewExternFieldClass[10];
146        for (int k = 0; k < farray.length; k++)
147            farray[k] = new NewExternFieldClass(k);
148    }
149
150    private void readObject(ObjectInputStream s)
151        throws IOException, ClassNotFoundException
152    {
153        s.defaultReadObject();
154        /* Excluded due to Bug 4089540
155        abc  = (NewExternFieldClass)fields.get("abc", null);
156        farray = (NewExternFieldClass[])fields.get("farray", null);
157        */
158
159        /* read optional data */
160        NewFieldClass   tmpfoo  = (NewFieldClass)s.readObject();
161        NewFieldClass[] tmparray= (NewFieldClass[])s.readObject();
162        int tmpbar = s.readInt();
163        /* Excluded due to Bug 4089540
164        NewExternFieldClass tmpabc = (NewExternFieldClass)s.readObject();
165        NewExternFieldClass[] tmpfarray = (NewExternFieldClass[])s.readObject();
166        */
167    }
168
169    private void writeObject(ObjectOutputStream s)
170        throws IOException
171    {
172        s.defaultWriteObject();
173
174        /* write optional data */
175        s.writeObject(foo);
176        s.writeObject(array);
177        s.writeInt(bar);
178
179        /* Excluded due to Bug 4089540
180        s.writeObject(abc);
181        s.writeObject(farray);
182        */
183    }
184}
185
186
187public class WriteAddedField {
188    public static void main(String args[]) throws IOException {
189        A a = new A();
190        B b = new B();
191        C c = new C();
192        File f = new File("tmp.ser");
193        ObjectOutput out =
194            new ObjectOutputStream(new FileOutputStream(f));
195        out.writeObject(a);
196        out.writeObject(b);
197        out.writeObject(c);
198        a = new A();
199        b = new B();
200        c = new C();
201        out.writeObject(a);
202        out.writeObject(b);
203        out.writeObject(c);
204        out.close();
205    }
206}
207