author | phh |
Sat, 30 Nov 2019 14:33:05 -0800 | |
changeset 59330 | 5b96c12f909d |
parent 58565 | baa5969ecf34 |
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) 2000, 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 4311991 |
|
26 |
* |
|
27 |
* @clean Write Read Foo Bar |
|
28 |
* @compile Write.java |
|
29 |
* @run main Write |
|
30 |
* @clean Write Read Foo Bar |
|
31 |
* @compile Read.java |
|
32 |
* @run main Read |
|
33 |
* |
|
34 |
* @summary Test ObjectOutputStream.writeUnshared/readUnshared functionality. |
|
35 |
*/ |
|
36 |
||
37 |
import java.io.*; |
|
38 |
||
39 |
class Foo implements Serializable { |
|
58565
baa5969ecf34
8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents:
47216
diff
changeset
|
40 |
private static final long serialVersionUID = 1L; |
baa5969ecf34
8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents:
47216
diff
changeset
|
41 |
|
2 | 42 |
private static final ObjectStreamField[] serialPersistentFields = |
43 |
new ObjectStreamField[] { |
|
44 |
new ObjectStreamField("shared1", String.class), |
|
45 |
new ObjectStreamField("shared2", String.class, false), |
|
46 |
new ObjectStreamField("unshared1", String.class, true), |
|
47 |
new ObjectStreamField("unshared2", String.class, true) |
|
48 |
}; |
|
49 |
||
50 |
String shared1, shared2, unshared1, unshared2; |
|
51 |
||
52 |
Foo() { |
|
53 |
shared1 = shared2 = unshared1 = unshared2 = "foo"; |
|
54 |
} |
|
55 |
} |
|
56 |
||
57 |
class Bar implements Serializable { |
|
58 |
private static final long serialVersionUID = 0L; |
|
58565
baa5969ecf34
8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents:
47216
diff
changeset
|
59 |
@SuppressWarnings("serial") /* Incorrect use is being tested */ |
2 | 60 |
Object obj; |
61 |
||
62 |
Bar(Object obj) { |
|
63 |
this.obj = obj; |
|
64 |
} |
|
65 |
} |
|
66 |
||
67 |
public class Write { |
|
68 |
public static void main(String[] args) throws Exception { |
|
69 |
String str1 = "foo"; |
|
70 |
ByteArrayOutputStream bout = new ByteArrayOutputStream(); |
|
71 |
ObjectOutputStream oout = new ObjectOutputStream(bout); |
|
72 |
||
73 |
oout.writeObject(str1); |
|
74 |
oout.writeObject(str1); |
|
75 |
oout.writeUnshared(str1); |
|
76 |
oout.writeUnshared(str1); |
|
77 |
oout.writeObject(new Foo()); |
|
78 |
oout.close(); |
|
79 |
||
80 |
ByteArrayInputStream bin = |
|
81 |
new ByteArrayInputStream(bout.toByteArray()); |
|
82 |
ObjectInputStream oin = new ObjectInputStream(bin); |
|
83 |
str1 = (String) oin.readObject(); |
|
84 |
if (oin.readObject() != str1) { |
|
85 |
throw new Error(); |
|
86 |
} |
|
87 |
String str2 = (String) oin.readObject(); |
|
88 |
String str3 = (String) oin.readObject(); |
|
89 |
if (str2 == str1 || str3 == str1 || str2 == str3) { |
|
90 |
throw new Error(); |
|
91 |
} |
|
92 |
if (! (str1.equals(str2) && str1.equals(str3))) { |
|
93 |
throw new Error(); |
|
94 |
} |
|
95 |
||
96 |
Foo foo = (Foo) oin.readObject(); |
|
97 |
if ((foo.shared1 != foo.shared2) || |
|
98 |
(foo.shared1 == foo.unshared1) || |
|
99 |
(foo.shared1 == foo.unshared2) || |
|
100 |
(foo.shared2 == foo.unshared1) || |
|
101 |
(foo.shared2 == foo.unshared2) || |
|
102 |
(foo.unshared1 == foo.unshared2)) |
|
103 |
{ |
|
104 |
throw new Error(); |
|
105 |
} |
|
106 |
||
107 |
// write out object to be read by Read.main() |
|
108 |
oout = new ObjectOutputStream(new FileOutputStream("tmp.ser")); |
|
109 |
oout.writeObject(new Bar(str1)); |
|
110 |
oout.writeObject(str1); |
|
111 |
oout.close(); |
|
112 |
} |
|
113 |
} |