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) 1999, 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 4227189 |
|
26 |
* @summary Ensure that class descriptor read, write hooks exist, are backwards |
|
27 |
* compatible, and work as advertised. |
|
28 |
*/ |
|
29 |
||
30 |
import java.io.*; |
|
31 |
import java.util.*; |
|
32 |
||
33 |
class Foo implements Serializable { |
|
34 |
private static final long serialVersionUID = 1L; |
|
58565
baa5969ecf34
8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents:
47216
diff
changeset
|
35 |
Short s = (short) 1; |
baa5969ecf34
8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents:
47216
diff
changeset
|
36 |
Integer i = 2; |
baa5969ecf34
8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents:
47216
diff
changeset
|
37 |
Long l = 3L; |
2 | 38 |
|
39 |
public boolean equals(Object obj) { |
|
40 |
if (obj instanceof Foo) { |
|
41 |
Foo ofoo = (Foo) obj; |
|
42 |
return s.equals(ofoo.s) && i.equals(ofoo.i) && l.equals(ofoo.l); |
|
43 |
} |
|
44 |
return false; |
|
45 |
} |
|
58565
baa5969ecf34
8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents:
47216
diff
changeset
|
46 |
|
baa5969ecf34
8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents:
47216
diff
changeset
|
47 |
public int hashCode() { |
baa5969ecf34
8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents:
47216
diff
changeset
|
48 |
return i; |
baa5969ecf34
8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents:
47216
diff
changeset
|
49 |
} |
2 | 50 |
} |
51 |
||
52 |
class CustomOutputStream extends ObjectOutputStream { |
|
53 |
||
54 |
boolean hookCalled = false; |
|
55 |
||
56 |
CustomOutputStream(OutputStream out) throws IOException { |
|
57 |
super(out); |
|
58 |
useProtocolVersion(PROTOCOL_VERSION_2); |
|
59 |
} |
|
60 |
||
61 |
protected void writeClassDescriptor(ObjectStreamClass desc) |
|
62 |
throws IOException |
|
63 |
{ |
|
64 |
writeUTF(desc.getName()); |
|
65 |
hookCalled = true; |
|
66 |
} |
|
67 |
} |
|
68 |
||
69 |
class CustomInputStream extends ObjectInputStream { |
|
70 |
||
71 |
boolean hookCalled = false; |
|
72 |
||
73 |
CustomInputStream(InputStream in) throws IOException { |
|
74 |
super(in); |
|
75 |
} |
|
76 |
||
77 |
protected ObjectStreamClass readClassDescriptor() |
|
78 |
throws IOException, ClassNotFoundException |
|
79 |
{ |
|
80 |
hookCalled = true; |
|
81 |
return ObjectStreamClass.lookup(Class.forName(readUTF())); |
|
82 |
} |
|
83 |
} |
|
84 |
||
85 |
public class ClassDescHooks implements ObjectStreamConstants { |
|
86 |
public static void main(String[] args) throws Exception { |
|
87 |
ByteArrayOutputStream bout; |
|
88 |
ByteArrayInputStream bin; |
|
89 |
ObjectOutputStream oout; |
|
90 |
ObjectInputStream oin; |
|
91 |
FileInputStream fin; |
|
92 |
File foof; |
|
93 |
CustomOutputStream cout; |
|
94 |
CustomInputStream cin; |
|
95 |
||
96 |
// test for backwards compatibility |
|
97 |
bout = new ByteArrayOutputStream(); |
|
98 |
foof = new File(System.getProperty("test.src", "."), "Foo.ser"); |
|
99 |
fin = new FileInputStream(foof); |
|
5809
6e38efd0293f
6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents:
5506
diff
changeset
|
100 |
try { |
6e38efd0293f
6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents:
5506
diff
changeset
|
101 |
while (fin.available() > 0) |
6e38efd0293f
6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents:
5506
diff
changeset
|
102 |
bout.write(fin.read()); |
6e38efd0293f
6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents:
5506
diff
changeset
|
103 |
} finally { |
6e38efd0293f
6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents:
5506
diff
changeset
|
104 |
fin.close(); |
6e38efd0293f
6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents:
5506
diff
changeset
|
105 |
} |
2 | 106 |
byte[] buf1 = bout.toByteArray(); |
107 |
||
108 |
bout = new ByteArrayOutputStream(); |
|
109 |
oout = new ObjectOutputStream(bout); |
|
110 |
Foo foo = new Foo(); |
|
111 |
oout.writeObject(foo); |
|
112 |
oout.flush(); |
|
113 |
byte[] buf2 = bout.toByteArray(); |
|
114 |
||
115 |
if (! Arrays.equals(buf1, buf2)) |
|
116 |
throw new Error("Incompatible stream format (write)"); |
|
117 |
||
5809
6e38efd0293f
6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents:
5506
diff
changeset
|
118 |
Foo foocopy; |
2 | 119 |
fin = new FileInputStream(foof); |
5809
6e38efd0293f
6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents:
5506
diff
changeset
|
120 |
try { |
6e38efd0293f
6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents:
5506
diff
changeset
|
121 |
oin = new ObjectInputStream(fin); |
6e38efd0293f
6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents:
5506
diff
changeset
|
122 |
foocopy = (Foo) oin.readObject(); |
6e38efd0293f
6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents:
5506
diff
changeset
|
123 |
if (! foo.equals(foocopy)) |
6e38efd0293f
6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents:
5506
diff
changeset
|
124 |
throw new Error("Incompatible stream format (read)"); |
6e38efd0293f
6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents:
5506
diff
changeset
|
125 |
} finally { |
6e38efd0293f
6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents:
5506
diff
changeset
|
126 |
fin.close(); |
6e38efd0293f
6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents:
5506
diff
changeset
|
127 |
} |
2 | 128 |
|
129 |
// make sure write hook not called when old protocol in use |
|
130 |
bout = new ByteArrayOutputStream(); |
|
131 |
cout = new CustomOutputStream(bout); |
|
132 |
cout.useProtocolVersion(PROTOCOL_VERSION_1); |
|
133 |
cout.writeObject(foo); |
|
134 |
if (cout.hookCalled) |
|
135 |
throw new Error("write descriptor hook should not be called"); |
|
136 |
||
137 |
// write custom class descriptor representations |
|
138 |
bout = new ByteArrayOutputStream(); |
|
139 |
cout = new CustomOutputStream(bout); |
|
140 |
cout.writeObject(foo); |
|
141 |
cout.flush(); |
|
142 |
bin = new ByteArrayInputStream(bout.toByteArray()); |
|
143 |
cin = new CustomInputStream(bin); |
|
144 |
foocopy = (Foo) cin.readObject(); |
|
145 |
if (! cout.hookCalled) |
|
146 |
throw new Error("write descriptor hook never called"); |
|
147 |
if (! cin.hookCalled) |
|
148 |
throw new Error("read descriptor hook never called"); |
|
149 |
if (! foo.equals(foocopy)) |
|
150 |
throw new Error("serialization failed when hooks active"); |
|
151 |
} |
|
152 |
} |