SubclassAcrossPackage.java revision 0:37a05a11f281
1/*
2 * Copyright 1998 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 4093279
26 * REMOVED test, build and run tag since could not get this to work.
27 * Test is run via shell script, run.sh.
28 */
29
30package Serialize;
31
32import java.io.*;
33
34class PublicSerializable
35extends NonSerializable.PublicCtor implements Serializable
36{
37    int field1 = 5;
38};
39
40class ProtectedSerializable
41extends NonSerializable.ProtectedCtor implements Serializable
42{
43    int field1 = 5;
44};
45
46class DifferentPackageSerializable
47extends NonSerializable.PackageCtor implements Serializable
48{
49    int field1 = 5;
50    DifferentPackageSerializable() {
51        super(1);
52    }
53};
54
55class SamePackageSerializable
56extends Serialize.SamePackageCtor implements Serializable
57{
58    SamePackageSerializable() {
59    }
60};
61
62class SamePackageProtectedCtor {
63    protected SamePackageProtectedCtor() {
64    }
65};
66
67class SamePackageProtectedSerializable
68extends Serialize.SamePackageProtectedCtor implements Serializable
69{
70    SamePackageProtectedSerializable() {
71    }
72};
73
74
75class SamePackagePrivateCtor {
76    private SamePackagePrivateCtor() {
77    }
78    public SamePackagePrivateCtor(int l) {
79    }
80};
81
82class SamePackagePrivateSerializable
83extends Serialize.SamePackagePrivateCtor implements Serializable
84{
85    SamePackagePrivateSerializable() {
86        super(1);
87    }
88};
89
90class PrivateSerializable
91extends NonSerializable.PrivateCtor implements Serializable
92{
93    int field1 = 5;
94
95    PrivateSerializable() {
96        super(1);
97    }
98};
99
100class ExternalizablePublicCtor implements Externalizable {
101    public ExternalizablePublicCtor() {
102    }
103    public void writeExternal(ObjectOutput out) throws IOException {
104    }
105    public void readExternal(ObjectInput in)
106        throws IOException, ClassNotFoundException
107        {
108        }
109};
110
111class ExternalizableProtectedCtor implements Externalizable {
112    protected ExternalizableProtectedCtor() {
113    }
114    public void writeExternal(ObjectOutput out) throws IOException {
115    }
116    public void readExternal(ObjectInput in)
117        throws IOException, ClassNotFoundException
118        {
119        }
120};
121
122class ExternalizablePackageCtor implements Externalizable {
123    ExternalizablePackageCtor() {
124    }
125    public void writeExternal(ObjectOutput out) throws IOException {
126    }
127    public void readExternal(ObjectInput in)
128        throws IOException, ClassNotFoundException
129        {
130        }
131};
132
133class ExternalizablePrivateCtor implements Externalizable {
134    private ExternalizablePrivateCtor() {
135    }
136    public ExternalizablePrivateCtor(int i) {
137    }
138    public void writeExternal(ObjectOutput out) throws IOException {
139    }
140    public void readExternal(ObjectInput in)
141        throws IOException, ClassNotFoundException
142        {
143        }
144};
145
146
147public class SubclassAcrossPackage {
148    public static void main(String args[])
149        throws IOException, ClassNotFoundException
150        {
151            ByteArrayOutputStream baos = new ByteArrayOutputStream();
152            ObjectOutputStream out =   new ObjectOutputStream(baos);
153            out.writeObject(new PublicSerializable());
154            out.writeObject(new ProtectedSerializable());
155            out.writeObject(new SamePackageSerializable());
156            out.writeObject(new SamePackageProtectedSerializable());
157            out.writeObject(new DifferentPackageSerializable());
158
159            InputStream is = new ByteArrayInputStream(baos.toByteArray());
160            ObjectInputStream in = new ObjectInputStream(is);
161            /* (PublicSerializable)*/ in.readObject();
162            /* (ProtectedSerializable) */ in.readObject();
163            /* (SamePackageSerializable) */ in.readObject();
164            /* (SamePackageProtectedSerializable) */ in.readObject();
165            try {
166            /* (DifferentPackageSerializable) */ in.readObject();
167            } catch (InvalidClassException e) {
168            }
169            in.close();
170
171            baos.reset();
172            out = new ObjectOutputStream(baos);
173            out.writeObject(new PrivateSerializable());
174            in = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));
175            try {
176                /* (PrivateSerializable) */ in.readObject();
177                throw new Error("Expected InvalidClassException reading PrivateSerialziable");
178            } catch (InvalidClassException e) {
179            }
180            in.close();
181
182            baos.reset();
183            out = new ObjectOutputStream(baos);
184            out.writeObject(new SamePackagePrivateSerializable());
185            in = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));
186            try {
187                /* (SamePackagePrivateSerializable) */ in.readObject();
188                throw new Error("Expected InvalidClassException reading PrivateSerialziable");
189            } catch (InvalidClassException e) {
190            }
191            in.close();
192
193            baos.reset();
194            out = new ObjectOutputStream(baos);
195            out.writeObject(new ExternalizablePublicCtor());
196
197            in = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));
198            /* (ExternalizablePublicCtor) */ in.readObject();
199            in.close();
200
201            baos.reset();
202            out = new ObjectOutputStream(baos);
203            out.writeObject(new ExternalizableProtectedCtor());
204
205
206            in = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));
207            try {
208                /* (ExternalizableProtectedCtor) */ in.readObject();
209                throw new Error("Expected InvalidClassException reading ExternalizableProtectedCtor");
210            } catch (InvalidClassException e) {
211            }
212            in.close();
213
214            baos.reset();
215            out = new ObjectOutputStream(baos);
216            out.writeObject(new ExternalizablePackageCtor());
217
218            in = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));
219            try {
220                /* (ExternalizablePackageCtor) */ in.readObject();
221                throw new Error("Expected InvalidClassException reading ExternalizablePackageCtor");
222            } catch (InvalidClassException e) {
223            }
224            in.close();
225
226            baos.reset();
227            out = new ObjectOutputStream(baos);
228            out.writeObject(new ExternalizablePrivateCtor(2));
229
230            in = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));
231            try {
232                /* (ExternalizablePrivateCtor) */ in.readObject();
233                throw new Error("Expected InvalidClassException reading ExternalizablePrivateCtor");
234            } catch (InvalidClassException e) {
235            }
236            out.close();
237            in.close();
238        }
239}
240