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) 1998, 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 |
/* |
|
25 |
* @test |
|
26 |
* @bug 4100915 |
|
27 |
* @summary Verify that [write/read]ObjectOverride methods get called. |
|
28 |
* Test verifies that ALL methods to write an object can |
|
45295
c2764290902a
8166142: Refactor java.io.serialization shell tests to java
mli
parents:
32649
diff
changeset
|
29 |
* be overridden. However, the testing for reading an object |
2 | 30 |
* is incomplete. Only test that readObjectOverride is called. |
31 |
* An entire protocol would need to be implemented and written |
|
32 |
* out before being able to test the input side of the API. |
|
33 |
* |
|
34 |
* Also, would be appropriate that this program verify |
|
45295
c2764290902a
8166142: Refactor java.io.serialization shell tests to java
mli
parents:
32649
diff
changeset
|
35 |
* that if SerializablePermission "enableSubclassImplementation" |
2 | 36 |
* is not in the security policy and security is enabled, that |
45295
c2764290902a
8166142: Refactor java.io.serialization shell tests to java
mli
parents:
32649
diff
changeset
|
37 |
* a security exception is thrown when constructing the |
2 | 38 |
* ObjectOutputStream subclass. |
39 |
* |
|
40 |
* |
|
45295
c2764290902a
8166142: Refactor java.io.serialization shell tests to java
mli
parents:
32649
diff
changeset
|
41 |
* @compile AbstractObjectInputStream.java AbstractObjectOutputStream.java |
c2764290902a
8166142: Refactor java.io.serialization shell tests to java
mli
parents:
32649
diff
changeset
|
42 |
* @compile XObjectInputStream.java XObjectOutputStream.java |
c2764290902a
8166142: Refactor java.io.serialization shell tests to java
mli
parents:
32649
diff
changeset
|
43 |
* @compile SubclassTest.java |
c2764290902a
8166142: Refactor java.io.serialization shell tests to java
mli
parents:
32649
diff
changeset
|
44 |
* @run main SubclassTest |
c2764290902a
8166142: Refactor java.io.serialization shell tests to java
mli
parents:
32649
diff
changeset
|
45 |
* @run main/othervm/policy=Allow.policy SubclassTest -expectSecurityException |
2 | 46 |
*/ |
47 |
||
45295
c2764290902a
8166142: Refactor java.io.serialization shell tests to java
mli
parents:
32649
diff
changeset
|
48 |
import java.io.ByteArrayInputStream; |
c2764290902a
8166142: Refactor java.io.serialization shell tests to java
mli
parents:
32649
diff
changeset
|
49 |
import java.io.ByteArrayOutputStream; |
c2764290902a
8166142: Refactor java.io.serialization shell tests to java
mli
parents:
32649
diff
changeset
|
50 |
import java.io.IOException; |
c2764290902a
8166142: Refactor java.io.serialization shell tests to java
mli
parents:
32649
diff
changeset
|
51 |
import java.io.ObjectInputStream; |
c2764290902a
8166142: Refactor java.io.serialization shell tests to java
mli
parents:
32649
diff
changeset
|
52 |
import java.io.ObjectOutputStream; |
c2764290902a
8166142: Refactor java.io.serialization shell tests to java
mli
parents:
32649
diff
changeset
|
53 |
import java.io.ObjectStreamField; |
c2764290902a
8166142: Refactor java.io.serialization shell tests to java
mli
parents:
32649
diff
changeset
|
54 |
import java.io.Serializable; |
2 | 55 |
|
56 |
/** |
|
57 |
* Test if customized readObject and writeObject are called. |
|
58 |
*/ |
|
59 |
class B implements Serializable { |
|
58565
baa5969ecf34
8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents:
47216
diff
changeset
|
60 |
private static final long serialVersionUID = 1L; |
baa5969ecf34
8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents:
47216
diff
changeset
|
61 |
|
2 | 62 |
public int publicIntField; |
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
5506
diff
changeset
|
63 |
public static int numWriteObjectCalled = 0; |
2 | 64 |
B(int v) { |
65 |
publicIntField = v; |
|
66 |
} |
|
67 |
private void writeObject(ObjectOutputStream os) throws IOException { |
|
68 |
numWriteObjectCalled++; |
|
69 |
os.defaultWriteObject(); |
|
70 |
} |
|
71 |
||
72 |
private void readObject(ObjectInputStream is) |
|
73 |
throws IOException, ClassNotFoundException |
|
74 |
{ |
|
75 |
is.defaultReadObject(); |
|
76 |
} |
|
77 |
||
78 |
}; |
|
79 |
||
80 |
/** |
|
81 |
* Test PutFields interface. |
|
82 |
*/ |
|
83 |
||
84 |
class C implements Serializable { |
|
58565
baa5969ecf34
8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents:
47216
diff
changeset
|
85 |
private static final long serialVersionUID = 1L; |
baa5969ecf34
8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents:
47216
diff
changeset
|
86 |
|
2 | 87 |
public int xx1; |
88 |
public int xx2; |
|
58565
baa5969ecf34
8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents:
47216
diff
changeset
|
89 |
private static final ObjectStreamField[] serialPersistentFields = { |
2 | 90 |
new ObjectStreamField("x1", Integer.TYPE), |
91 |
new ObjectStreamField("x2", Integer.TYPE), |
|
92 |
new ObjectStreamField("x3", Integer.TYPE), |
|
93 |
new ObjectStreamField("x4", Integer.TYPE) |
|
94 |
}; |
|
95 |
C() { |
|
96 |
xx1 = 300; |
|
97 |
xx2 = 400; |
|
98 |
} |
|
99 |
||
100 |
private void writeObject(ObjectOutputStream os) throws IOException { |
|
101 |
ObjectOutputStream.PutField putFields = os.putFields(); |
|
102 |
putFields.put("x1", xx1); |
|
103 |
putFields.put("x2", xx2); |
|
104 |
putFields.put("x3", xx1 * 2); |
|
105 |
putFields.put("x4", xx2 * 2); |
|
106 |
os.writeFields(); |
|
107 |
} |
|
108 |
||
109 |
}; |
|
110 |
||
111 |
||
112 |
class A implements Serializable { |
|
58565
baa5969ecf34
8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents:
47216
diff
changeset
|
113 |
private static final long serialVersionUID = 1L; |
baa5969ecf34
8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents:
47216
diff
changeset
|
114 |
|
2 | 115 |
public int publicIntField; |
116 |
public long publicLongField; |
|
117 |
public B publicBField; |
|
118 |
public B[] publicBArray = { new B(4), new B(6)}; |
|
119 |
public C publicCField; |
|
120 |
||
121 |
public A() { |
|
122 |
publicIntField = 3; |
|
123 |
publicLongField = 10L; |
|
124 |
publicBField = new B(5); |
|
125 |
publicCField = new C(); |
|
126 |
} |
|
127 |
}; |
|
128 |
||
45295
c2764290902a
8166142: Refactor java.io.serialization shell tests to java
mli
parents:
32649
diff
changeset
|
129 |
public class SubclassTest { |
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
5506
diff
changeset
|
130 |
public static void main(String argv[]) |
2 | 131 |
throws IOException, ClassNotFoundException |
132 |
{ |
|
133 |
boolean expectSecurityException = false; |
|
134 |
||
135 |
if (argv.length > 0 && |
|
136 |
argv[0].compareTo("-expectSecurityException") == 0) |
|
137 |
expectSecurityException = true; |
|
138 |
||
139 |
ByteArrayOutputStream baos = new ByteArrayOutputStream(20); |
|
140 |
XObjectOutputStream os = null; |
|
141 |
try { |
|
142 |
os = new XObjectOutputStream(baos); |
|
143 |
if (expectSecurityException) |
|
144 |
throw new Error("Assertion failure. " + |
|
145 |
"Expected a security exception on previous line."); |
|
146 |
} catch (SecurityException e) { |
|
45295
c2764290902a
8166142: Refactor java.io.serialization shell tests to java
mli
parents:
32649
diff
changeset
|
147 |
if (expectSecurityException) { |
c2764290902a
8166142: Refactor java.io.serialization shell tests to java
mli
parents:
32649
diff
changeset
|
148 |
System.err.println("Caught expected security exception."); |
2 | 149 |
return; |
45295
c2764290902a
8166142: Refactor java.io.serialization shell tests to java
mli
parents:
32649
diff
changeset
|
150 |
} |
c2764290902a
8166142: Refactor java.io.serialization shell tests to java
mli
parents:
32649
diff
changeset
|
151 |
throw e; |
2 | 152 |
} |
153 |
os.writeObject(new A()); |
|
154 |
os.close(); |
|
155 |
if (B.numWriteObjectCalled != 3) |
|
156 |
throw new Error("Expected B.writeObject() to be called 3 times;" + |
|
157 |
" observed only " + B.numWriteObjectCalled + " times"); |
|
158 |
||
159 |
XObjectInputStream is = |
|
160 |
new XObjectInputStream(new ByteArrayInputStream(baos.toByteArray())); |
|
161 |
try { |
|
162 |
A a = (A)is.readObject(); |
|
163 |
throw new Error("Expected readObjectOverride() to be called and throw IOException(not implemented)"); |
|
164 |
} catch (IOException e) { |
|
165 |
} |
|
166 |
is.close(); |
|
167 |
} |
|
168 |
}; |