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) 2005, 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 |
* @summary it is new version of old test which was |
|
26 |
* /src/share/test/serialization/psiotest.java |
|
27 |
* Test pickling and unpickling an object with derived classes |
|
28 |
* and using a read special to serialize the "middle" class, |
|
29 |
* which raises NotSerializableException inside writeObject() |
|
30 |
* and readObject() methods. |
|
31 |
*/ |
|
32 |
||
33 |
import java.io.*; |
|
34 |
||
35 |
public class CheckForException { |
|
36 |
public static void main (String argv[]) { |
|
37 |
System.err.println("\nRegression test of " + |
|
38 |
"serialization/deserialization of " + |
|
39 |
"complex objects which raise " + |
|
40 |
"NotSerializableException inside " + |
|
41 |
"writeObject() and readObject() methods.\n"); |
|
42 |
||
43 |
||
44 |
FileInputStream istream = null; |
|
45 |
try { |
|
46 |
||
47 |
FileOutputStream ostream = new FileOutputStream("psiotest3.tmp"); |
|
48 |
ObjectOutputStream p = new ObjectOutputStream(ostream); |
|
49 |
||
50 |
/* Catch the expected exception and |
|
51 |
* complain if it does not occur. |
|
52 |
*/ |
|
53 |
TryPickleClass npc = new TryPickleClass(); |
|
54 |
||
55 |
NotSerializableException we = null; |
|
56 |
try { |
|
57 |
// Two objects of the same class that are used |
|
58 |
// in cleanup test below |
|
59 |
p.writeObject("test"); |
|
60 |
p.writeObject("test2"); |
|
61 |
p.writeObject(npc); |
|
62 |
} catch (NotSerializableException e) { |
|
63 |
we = e; |
|
64 |
} |
|
65 |
if (we == null) { |
|
66 |
System.err.println("\nTEST FAILED: Write of NoPickleClass " + |
|
67 |
"should have raised an exception"); |
|
68 |
throw new Error(); |
|
69 |
} |
|
70 |
||
71 |
p.flush(); |
|
72 |
ostream.close(); |
|
73 |
||
74 |
istream = new FileInputStream("psiotest3.tmp"); |
|
75 |
ObjectInputStream q = new ObjectInputStream(istream); |
|
76 |
||
77 |
/* Catch the expected exception and |
|
78 |
* complain if it does not occur. |
|
79 |
*/ |
|
80 |
TryPickleClass npc_u; |
|
81 |
||
82 |
NotSerializableException re = null; |
|
83 |
try { |
|
84 |
// Read the two objects, neither has a cleanup method |
|
85 |
q.readObject(); |
|
86 |
q.readObject(); |
|
87 |
npc_u = (TryPickleClass)q.readObject(); |
|
88 |
} catch (NotSerializableException e) { |
|
89 |
re = e; |
|
90 |
} |
|
91 |
if (re == null) { |
|
92 |
System.err.println("\nTEST FAILED: Read of NoPickleClass " + |
|
93 |
"should have raised an exception"); |
|
94 |
throw new Error(); |
|
95 |
} |
|
96 |
||
97 |
istream.close(); |
|
98 |
System.err.println("\nTEST PASSED"); |
|
99 |
} catch (Exception e) { |
|
100 |
System.err.print("TEST FAILED: "); |
|
101 |
e.printStackTrace(); |
|
102 |
throw new Error(); |
|
103 |
} |
|
104 |
} |
|
105 |
} |
|
106 |
||
107 |
class PickleClass implements java.io.Serializable { |
|
58565
baa5969ecf34
8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents:
47216
diff
changeset
|
108 |
private static final long serialVersionUID = 1L; |
baa5969ecf34
8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents:
47216
diff
changeset
|
109 |
|
2 | 110 |
int ii = 17; |
111 |
transient int tmp[]; |
|
112 |
||
113 |
private void writeObject(ObjectOutputStream pw) throws IOException { |
|
114 |
pw.writeUTF("PickleClass"); |
|
115 |
pw.writeInt(ii); |
|
116 |
} |
|
117 |
||
118 |
private void readObject(ObjectInputStream pr) throws IOException { |
|
119 |
tmp = new int[32]; |
|
120 |
pr.readUTF(); |
|
121 |
ii = pr.readInt(); |
|
122 |
} |
|
123 |
||
124 |
private void readObjectCleanup(ObjectInputStream pr) { |
|
125 |
System.err.println("\nPickleClass cleanup correctly called on abort."); |
|
126 |
if (tmp != null) { |
|
127 |
tmp = null; |
|
128 |
} |
|
129 |
} |
|
130 |
||
131 |
} |
|
132 |
||
133 |
class NoPickleClass extends PickleClass { |
|
58565
baa5969ecf34
8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents:
47216
diff
changeset
|
134 |
private static final long serialVersionUID = 1L; |
baa5969ecf34
8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents:
47216
diff
changeset
|
135 |
|
2 | 136 |
private void writeObject(ObjectOutputStream pw) |
137 |
throws NotSerializableException |
|
138 |
{ |
|
139 |
throw new NotSerializableException("NoPickleClass"); |
|
140 |
} |
|
141 |
||
142 |
private void readObject(ObjectInputStream pr) |
|
143 |
throws NotSerializableException |
|
144 |
{ |
|
145 |
throw new NotSerializableException("NoPickleClass"); |
|
146 |
} |
|
147 |
} |
|
148 |
||
149 |
class TryPickleClass extends NoPickleClass { |
|
58565
baa5969ecf34
8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents:
47216
diff
changeset
|
150 |
private static final long serialVersionUID = 1L; |
baa5969ecf34
8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents:
47216
diff
changeset
|
151 |
|
2 | 152 |
int i = 7; |
153 |
transient int tmp[]; |
|
154 |
||
155 |
private void writeObject(ObjectOutputStream pw) throws IOException { |
|
156 |
pw.writeInt(i); |
|
157 |
} |
|
158 |
||
159 |
private void readObject(ObjectInputStream pr) throws IOException { |
|
160 |
tmp = new int[32]; |
|
161 |
i = pr.readInt(); |
|
162 |
} |
|
163 |
||
164 |
private void readObjectCleanup(ObjectInputStream pr) { |
|
165 |
System.err.println("\nCleanup called on abort"); |
|
166 |
if (tmp != null) { |
|
167 |
tmp = null; |
|
168 |
} |
|
169 |
} |
|
170 |
} |