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