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/subtest.java |
|
27 |
* This test verifies of invocation |
|
28 |
* annotateClass/replaceObject methods |
|
29 |
*/ |
|
30 |
||
31 |
import java.io.*; |
|
32 |
||
33 |
public class AnnotateClass { |
|
34 |
public static void main (String argv[]) { |
|
35 |
System.err.println("\nRegression test for verification " + |
|
36 |
"of invocation annotateClass/replaceObject " + |
|
37 |
"methods \n"); |
|
38 |
try { |
|
39 |
FileOutputStream ostream = new FileOutputStream("subtest1.tmp"); |
|
5809
6e38efd0293f
6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents:
5506
diff
changeset
|
40 |
try { |
6e38efd0293f
6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents:
5506
diff
changeset
|
41 |
TestOutputStream p = new TestOutputStream(ostream); |
6e38efd0293f
6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents:
5506
diff
changeset
|
42 |
p.writeObject(System.out); |
6e38efd0293f
6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents:
5506
diff
changeset
|
43 |
p.writeObject(System.err); |
6e38efd0293f
6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents:
5506
diff
changeset
|
44 |
p.writeObject(new PrintStream(ostream)); |
6e38efd0293f
6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents:
5506
diff
changeset
|
45 |
p.flush(); |
6e38efd0293f
6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents:
5506
diff
changeset
|
46 |
} finally { |
6e38efd0293f
6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents:
5506
diff
changeset
|
47 |
ostream.close(); |
6e38efd0293f
6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents:
5506
diff
changeset
|
48 |
} |
2 | 49 |
|
50 |
FileInputStream istream = new FileInputStream("subtest1.tmp"); |
|
5809
6e38efd0293f
6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents:
5506
diff
changeset
|
51 |
try { |
6e38efd0293f
6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents:
5506
diff
changeset
|
52 |
TestInputStream q = new TestInputStream(istream); |
2 | 53 |
|
5809
6e38efd0293f
6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents:
5506
diff
changeset
|
54 |
PrintStream out = (PrintStream)q.readObject(); |
6e38efd0293f
6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents:
5506
diff
changeset
|
55 |
PrintStream err = (PrintStream)q.readObject(); |
6e38efd0293f
6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents:
5506
diff
changeset
|
56 |
Object other = q.readObject(); |
6e38efd0293f
6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents:
5506
diff
changeset
|
57 |
if (out != System.out) { |
6e38efd0293f
6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents:
5506
diff
changeset
|
58 |
System.err.println( |
6e38efd0293f
6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents:
5506
diff
changeset
|
59 |
"\nTEST FAILED: System.out not read correctly"); |
6e38efd0293f
6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents:
5506
diff
changeset
|
60 |
throw new Error(); |
6e38efd0293f
6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents:
5506
diff
changeset
|
61 |
} |
6e38efd0293f
6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents:
5506
diff
changeset
|
62 |
if (err != System.err) { |
6e38efd0293f
6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents:
5506
diff
changeset
|
63 |
System.err.println( |
6e38efd0293f
6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents:
5506
diff
changeset
|
64 |
"\nTEST FAILED: System.err not read correctly"); |
6e38efd0293f
6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents:
5506
diff
changeset
|
65 |
throw new Error(); |
6e38efd0293f
6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents:
5506
diff
changeset
|
66 |
} |
6e38efd0293f
6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents:
5506
diff
changeset
|
67 |
if (other != null) { |
6e38efd0293f
6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents:
5506
diff
changeset
|
68 |
System.err.println( |
6e38efd0293f
6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents:
5506
diff
changeset
|
69 |
"\nTEST FAILED: Non-system PrintStream should have " + |
6e38efd0293f
6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents:
5506
diff
changeset
|
70 |
"been written/read as null"); |
6e38efd0293f
6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents:
5506
diff
changeset
|
71 |
throw new Error(); |
6e38efd0293f
6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents:
5506
diff
changeset
|
72 |
} |
6e38efd0293f
6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents:
5506
diff
changeset
|
73 |
} finally { |
6e38efd0293f
6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents:
5506
diff
changeset
|
74 |
istream.close(); |
2 | 75 |
} |
5809
6e38efd0293f
6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents:
5506
diff
changeset
|
76 |
|
2 | 77 |
System.err.println("\nTEST PASSED"); |
78 |
} catch (Exception e) { |
|
79 |
System.err.print("TEST FAILED: "); |
|
80 |
e.printStackTrace(); |
|
81 |
throw new Error(); |
|
82 |
} |
|
83 |
} |
|
84 |
} |
|
85 |
||
86 |
||
87 |
||
88 |
/** ObjectOutputStream is extended to test the annotateClass() |
|
89 |
* and replaceObject() subclassable methods. |
|
90 |
* In annotateClass a magic string is written to the stream |
|
91 |
* so that it can be verified in ObjectInputStream. |
|
92 |
* replaceObject is used to subsititute a handle object for |
|
93 |
* one of the standard PrintStreams (stdout or stderr). |
|
94 |
*/ |
|
95 |
class TestOutputStream extends ObjectOutputStream { |
|
96 |
/* Construct a new test stream */ |
|
97 |
TestOutputStream(OutputStream out) throws IOException { |
|
98 |
super(out); |
|
99 |
enableReplaceObject(true); |
|
100 |
} |
|
101 |
||
102 |
/* When any class is written, add a "magic" string |
|
103 |
* that must be verified by the TestInputStream. |
|
104 |
*/ |
|
58565
baa5969ecf34
8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents:
47216
diff
changeset
|
105 |
protected void annotateClass(Class<?> cl) throws IOException { |
2 | 106 |
this.writeUTF("magic"); |
107 |
} |
|
108 |
||
109 |
/* For each object of type PrintStream, substitute |
|
110 |
* a StdStream handle object that encodes which |
|
111 |
* of the standard print streams is being written. |
|
112 |
* Other objects are written as themselves. |
|
113 |
*/ |
|
114 |
protected Object replaceObject(Object obj) |
|
115 |
{ |
|
116 |
/* For PrintStreams, like stdout and stderr, encode */ |
|
117 |
if (obj instanceof PrintStream) { |
|
118 |
return new StdStream((PrintStream)obj); |
|
119 |
} |
|
120 |
return obj; |
|
121 |
} |
|
122 |
} |
|
123 |
||
124 |
/** Reverse the effects of TestOutputStream. |
|
125 |
*/ |
|
126 |
class TestInputStream extends ObjectInputStream { |
|
127 |
||
128 |
TestInputStream(InputStream in) throws IOException { |
|
129 |
super(in); |
|
130 |
enableResolveObject(true); |
|
131 |
} |
|
132 |
||
133 |
/** Verify that the magic string was written to the stream |
|
134 |
* Also use the default classname->class resolution. |
|
135 |
*/ |
|
136 |
protected Class<?> resolveClass(ObjectStreamClass classdesc) |
|
137 |
throws ClassNotFoundException, IOException |
|
138 |
{ |
|
139 |
try { |
|
140 |
String s = readUTF(); |
|
141 |
if (!(s.equals("magic"))) { |
|
142 |
System.err.println( |
|
143 |
"\nTEST FAILED: Bad magic number"); |
|
144 |
throw new Error(); |
|
145 |
} |
|
146 |
} catch (IOException ee) { |
|
147 |
System.err.println( |
|
148 |
"\nTEST FAILED: I/O Exception"); |
|
149 |
throw new Error(); |
|
150 |
} |
|
151 |
return super.resolveClass(classdesc); |
|
152 |
} |
|
153 |
||
154 |
/** If the object in the stream is a StdStream, |
|
155 |
* get the mapping of it to the local System printstream and |
|
156 |
* return it. |
|
157 |
* Other objects are returned as themselves. |
|
158 |
*/ |
|
159 |
protected Object resolveObject(Object obj) { |
|
160 |
if (obj instanceof StdStream) { |
|
161 |
return ((StdStream)obj).getStream(); |
|
162 |
} |
|
163 |
return obj; |
|
164 |
} |
|
165 |
} |
|
166 |
||
167 |
/* A holder class to map between standard print streams (stdout, stderr) |
|
168 |
* and a small integer. |
|
169 |
*/ |
|
170 |
class StdStream implements java.io.Serializable { |
|
58565
baa5969ecf34
8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents:
47216
diff
changeset
|
171 |
private static final long serialVersionUID = 1L; |
2 | 172 |
private int stream = 0; |
173 |
||
174 |
public StdStream(PrintStream s) { |
|
175 |
if (s == System.out) { |
|
176 |
stream = 1; |
|
177 |
} else if (s == System.err) { |
|
178 |
stream = 2; |
|
179 |
} |
|
180 |
} |
|
181 |
||
182 |
public PrintStream getStream() { |
|
183 |
if (stream == 1) { |
|
184 |
return System.out; |
|
185 |
} else if (stream == 2) { |
|
186 |
return System.err; |
|
187 |
} else { |
|
188 |
return null; |
|
189 |
} |
|
190 |
} |
|
191 |
} |