1/*
2 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. 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 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.
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            try {
41                TestOutputStream p = new TestOutputStream(ostream);
42                p.writeObject(System.out);
43                p.writeObject(System.err);
44                p.writeObject(new PrintStream(ostream));
45                p.flush();
46            } finally {
47                ostream.close();
48            }
49
50            FileInputStream istream = new FileInputStream("subtest1.tmp");
51            try {
52                TestInputStream q = new TestInputStream(istream);
53
54                PrintStream out = (PrintStream)q.readObject();
55                PrintStream err = (PrintStream)q.readObject();
56                Object other = q.readObject();
57                if (out != System.out) {
58                    System.err.println(
59                        "\nTEST FAILED: System.out not read correctly");
60                    throw new Error();
61                }
62                if (err != System.err) {
63                    System.err.println(
64                        "\nTEST FAILED: System.err not read correctly");
65                    throw new Error();
66                }
67                if (other != null) {
68                    System.err.println(
69                        "\nTEST FAILED: Non-system PrintStream should have " +
70                        "been written/read as null");
71                    throw new Error();
72                }
73            } finally {
74                istream.close();
75            }
76
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 */
95class 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     */
105    protected void annotateClass(Class cl) throws IOException {
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        throws IOException
116    {
117        /* For PrintStreams, like stdout and stderr, encode */
118        if (obj instanceof PrintStream) {
119            return new StdStream((PrintStream)obj);
120        }
121        return obj;
122    }
123}
124
125/** Reverse the effects of TestOutputStream.
126 */
127class TestInputStream extends ObjectInputStream {
128
129    TestInputStream(InputStream in)  throws IOException  {
130        super(in);
131        enableResolveObject(true);
132    }
133
134    /** Verify that the magic string was written to the stream
135     * Also use the default classname->class resolution.
136     */
137    protected Class<?> resolveClass(ObjectStreamClass classdesc)
138        throws ClassNotFoundException, IOException
139    {
140        try {
141            String s = readUTF();
142            if (!(s.equals("magic"))) {
143                System.err.println(
144                    "\nTEST FAILED: Bad magic number");
145                throw new Error();
146            }
147        } catch (IOException ee) {
148            System.err.println(
149                "\nTEST FAILED: I/O Exception");
150            throw new Error();
151        }
152        return super.resolveClass(classdesc);
153    }
154
155    /** If the object in the stream is a StdStream,
156     * get the mapping of it to the local System printstream and
157     * return it.
158     * Other objects are returned as themselves.
159     */
160    protected Object resolveObject(Object obj) {
161        if (obj instanceof StdStream) {
162            return ((StdStream)obj).getStream();
163        }
164        return obj;
165    }
166}
167
168/* A holder class to map between standard print streams (stdout, stderr)
169 * and a small integer.
170 */
171class StdStream implements java.io.Serializable {
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}
192