2
|
1 |
/*
|
|
2 |
* Copyright 2003 Sun Microsystems, Inc. All Rights Reserved.
|
|
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 |
*
|
|
19 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
20 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
21 |
* have any questions.
|
|
22 |
*/
|
|
23 |
|
|
24 |
/* @test
|
|
25 |
* @bug 4838379
|
|
26 |
* @summary Verify that unshared write and read operations work properly with
|
|
27 |
* enum constants.
|
|
28 |
*/
|
|
29 |
|
|
30 |
import java.io.*;
|
|
31 |
|
|
32 |
enum Foo { foo, bar, baz }
|
|
33 |
|
|
34 |
abstract class WriteReadTest {
|
|
35 |
|
|
36 |
abstract void write(ObjectOutputStream out) throws Exception;
|
|
37 |
abstract void read(ObjectInputStream in) throws Exception;
|
|
38 |
|
|
39 |
void run() throws Exception {
|
|
40 |
ByteArrayOutputStream bout = new ByteArrayOutputStream();
|
|
41 |
ObjectOutputStream oout = new ObjectOutputStream(bout);
|
|
42 |
write(oout);
|
|
43 |
oout.close();
|
|
44 |
read(new ObjectInputStream(
|
|
45 |
new ByteArrayInputStream(bout.toByteArray())));
|
|
46 |
}
|
|
47 |
}
|
|
48 |
|
|
49 |
public class Test {
|
|
50 |
public static void main(String[] args) throws Exception {
|
|
51 |
WriteReadTest[] tests = {
|
|
52 |
new WriteReadTest() {
|
|
53 |
void write(ObjectOutputStream out) throws Exception {
|
|
54 |
out.writeObject(Foo.foo);
|
|
55 |
out.writeObject(Foo.foo);
|
|
56 |
}
|
|
57 |
void read(ObjectInputStream in) throws Exception {
|
|
58 |
Object obj = in.readObject();
|
|
59 |
if (obj != Foo.foo) {
|
|
60 |
throw new Error(
|
|
61 |
"expected " + Foo.foo + " instead of " + obj);
|
|
62 |
}
|
|
63 |
try {
|
|
64 |
obj = in.readUnshared();
|
|
65 |
throw new Error(
|
|
66 |
"read of " + obj + " should not have succeeded");
|
|
67 |
} catch (ObjectStreamException e) {
|
|
68 |
System.out.println("caught expected exception " + e);
|
|
69 |
}
|
|
70 |
}
|
|
71 |
},
|
|
72 |
new WriteReadTest() {
|
|
73 |
void write(ObjectOutputStream out) throws Exception {
|
|
74 |
out.writeObject(Foo.foo);
|
|
75 |
out.writeObject(Foo.foo);
|
|
76 |
}
|
|
77 |
void read(ObjectInputStream in) throws Exception {
|
|
78 |
Object obj = in.readUnshared();
|
|
79 |
if (obj != Foo.foo) {
|
|
80 |
throw new Error(
|
|
81 |
"expected " + Foo.foo + " instead of " + obj);
|
|
82 |
}
|
|
83 |
try {
|
|
84 |
obj = in.readObject();
|
|
85 |
throw new Error(
|
|
86 |
"read of " + obj + " should not have succeeded");
|
|
87 |
} catch (ObjectStreamException e) {
|
|
88 |
System.out.println("caught expected exception " + e);
|
|
89 |
}
|
|
90 |
}
|
|
91 |
},
|
|
92 |
new WriteReadTest() {
|
|
93 |
void write(ObjectOutputStream out) throws Exception {
|
|
94 |
out.writeObject(Foo.foo);
|
|
95 |
out.writeUnshared(Foo.foo);
|
|
96 |
}
|
|
97 |
void read(ObjectInputStream in) throws Exception {
|
|
98 |
Object obj = in.readUnshared();
|
|
99 |
if (obj != Foo.foo) {
|
|
100 |
throw new Error(
|
|
101 |
"expected " + Foo.foo + " instead of " + obj);
|
|
102 |
}
|
|
103 |
obj = in.readUnshared();
|
|
104 |
if (obj != Foo.foo) {
|
|
105 |
throw new Error(
|
|
106 |
"expected " + Foo.foo + " instead of " + obj);
|
|
107 |
}
|
|
108 |
}
|
|
109 |
},
|
|
110 |
new WriteReadTest() {
|
|
111 |
void write(ObjectOutputStream out) throws Exception {
|
|
112 |
out.writeUnshared(Foo.foo);
|
|
113 |
out.writeObject(Foo.foo);
|
|
114 |
}
|
|
115 |
void read(ObjectInputStream in) throws Exception {
|
|
116 |
Object obj = in.readUnshared();
|
|
117 |
if (obj != Foo.foo) {
|
|
118 |
throw new Error(
|
|
119 |
"expected " + Foo.foo + " instead of " + obj);
|
|
120 |
}
|
|
121 |
obj = in.readUnshared();
|
|
122 |
if (obj != Foo.foo) {
|
|
123 |
throw new Error(
|
|
124 |
"expected " + Foo.foo + " instead of " + obj);
|
|
125 |
}
|
|
126 |
}
|
|
127 |
}
|
|
128 |
};
|
|
129 |
for (int i = 0; i < tests.length; i++) {
|
|
130 |
tests[i].run();
|
|
131 |
}
|
|
132 |
}
|
|
133 |
}
|