author | rriggs |
Fri, 11 Oct 2019 13:11:56 -0400 | |
changeset 58565 | baa5969ecf34 |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
58565
baa5969ecf34
8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents:
47216
diff
changeset
|
2 |
* Copyright (c) 2001, 2019, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 22 |
*/ |
23 |
||
24 |
/* @test |
|
25 |
* @bug 4461737 |
|
26 |
* @summary Verify that serialization functions properly for externalizable |
|
27 |
* classes if ObjectInputStream.readClassDescriptor() returns a local |
|
28 |
* class descriptor. |
|
29 |
*/ |
|
30 |
||
31 |
import java.io.*; |
|
32 |
import java.util.*; |
|
33 |
||
34 |
class LoopbackOutputStream extends ObjectOutputStream { |
|
58565
baa5969ecf34
8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents:
47216
diff
changeset
|
35 |
LinkedList<ObjectStreamClass> descs; |
2 | 36 |
|
58565
baa5969ecf34
8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents:
47216
diff
changeset
|
37 |
LoopbackOutputStream(OutputStream out, LinkedList<ObjectStreamClass> descs) |
2 | 38 |
throws IOException |
39 |
{ |
|
40 |
super(out); |
|
41 |
this.descs = descs; |
|
42 |
} |
|
43 |
||
44 |
protected void writeClassDescriptor(ObjectStreamClass desc) |
|
45 |
throws IOException |
|
46 |
{ |
|
47 |
descs.add(desc); |
|
48 |
} |
|
49 |
} |
|
50 |
||
51 |
class LoopbackInputStream extends ObjectInputStream { |
|
58565
baa5969ecf34
8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents:
47216
diff
changeset
|
52 |
LinkedList<ObjectStreamClass> descs; |
2 | 53 |
|
58565
baa5969ecf34
8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents:
47216
diff
changeset
|
54 |
LoopbackInputStream(InputStream in, LinkedList<ObjectStreamClass> descs) throws IOException { |
2 | 55 |
super(in); |
56 |
this.descs = descs; |
|
57 |
} |
|
58 |
||
59 |
protected ObjectStreamClass readClassDescriptor() |
|
60 |
throws IOException, ClassNotFoundException |
|
61 |
{ |
|
58565
baa5969ecf34
8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents:
47216
diff
changeset
|
62 |
return descs.removeFirst(); |
2 | 63 |
} |
64 |
} |
|
65 |
||
66 |
public class ExternLoopback implements Externalizable { |
|
58565
baa5969ecf34
8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents:
47216
diff
changeset
|
67 |
private static final long serialVersionUID = 1L; |
2 | 68 |
|
69 |
String a, b, c; |
|
70 |
||
71 |
public ExternLoopback() { |
|
72 |
} |
|
73 |
||
74 |
ExternLoopback(String a, String b, String c) { |
|
75 |
this.a = a; |
|
76 |
this.b = b; |
|
77 |
this.c = c; |
|
78 |
} |
|
79 |
||
80 |
public void writeExternal(ObjectOutput out) throws IOException { |
|
81 |
out.writeBoolean(false); |
|
82 |
out.writeObject(a); |
|
83 |
out.writeObject(b); |
|
84 |
out.writeObject(c); |
|
85 |
} |
|
86 |
||
87 |
public void readExternal(ObjectInput in) |
|
88 |
throws IOException, ClassNotFoundException |
|
89 |
{ |
|
90 |
in.readBoolean(); |
|
91 |
a = (String) in.readObject(); |
|
92 |
b = (String) in.readObject(); |
|
93 |
c = (String) in.readObject(); |
|
94 |
} |
|
95 |
||
96 |
public boolean equals(Object obj) { |
|
97 |
if (!(obj instanceof ExternLoopback)) { |
|
98 |
return false; |
|
99 |
} |
|
100 |
ExternLoopback other = (ExternLoopback) obj; |
|
101 |
return streq(a, other.a) && streq(b, other.b) && streq(c, other.c); |
|
102 |
} |
|
103 |
||
58565
baa5969ecf34
8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents:
47216
diff
changeset
|
104 |
public int hashCode() { |
baa5969ecf34
8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents:
47216
diff
changeset
|
105 |
return a.hashCode(); |
baa5969ecf34
8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents:
47216
diff
changeset
|
106 |
} |
baa5969ecf34
8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents:
47216
diff
changeset
|
107 |
|
2 | 108 |
static boolean streq(String s1, String s2) { |
109 |
return (s1 != null) ? s1.equals(s2) : (s2 == null); |
|
110 |
} |
|
111 |
||
112 |
public static void main(String[] args) throws Exception { |
|
113 |
ExternLoopback lb = new ExternLoopback("foo", "bar", "baz"); |
|
58565
baa5969ecf34
8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents:
47216
diff
changeset
|
114 |
LinkedList<ObjectStreamClass> descs = new LinkedList<>(); |
2 | 115 |
ByteArrayOutputStream bout = new ByteArrayOutputStream(); |
116 |
LoopbackOutputStream lout = new LoopbackOutputStream(bout, descs); |
|
117 |
lout.writeObject(lb); |
|
118 |
lout.close(); |
|
119 |
||
120 |
LoopbackInputStream lin = new LoopbackInputStream( |
|
121 |
new ByteArrayInputStream(bout.toByteArray()), descs); |
|
122 |
ExternLoopback lbcopy = (ExternLoopback) lin.readObject(); |
|
123 |
if (!lb.equals(lbcopy)) { |
|
124 |
throw new Error(); |
|
125 |
} |
|
126 |
} |
|
127 |
} |