Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/io/Serializable/oldTests/AnnotateClass.java
38828 views
/*1* Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24* @summary it is new version of old test which was25* /src/share/test/serialization/subtest.java26* This test verifies of invocation27* annotateClass/replaceObject methods28*/2930import java.io.*;3132public class AnnotateClass {33public static void main (String argv[]) {34System.err.println("\nRegression test for verification " +35"of invocation annotateClass/replaceObject " +36"methods \n");37try {38FileOutputStream ostream = new FileOutputStream("subtest1.tmp");39try {40TestOutputStream p = new TestOutputStream(ostream);41p.writeObject(System.out);42p.writeObject(System.err);43p.writeObject(new PrintStream(ostream));44p.flush();45} finally {46ostream.close();47}4849FileInputStream istream = new FileInputStream("subtest1.tmp");50try {51TestInputStream q = new TestInputStream(istream);5253PrintStream out = (PrintStream)q.readObject();54PrintStream err = (PrintStream)q.readObject();55Object other = q.readObject();56if (out != System.out) {57System.err.println(58"\nTEST FAILED: System.out not read correctly");59throw new Error();60}61if (err != System.err) {62System.err.println(63"\nTEST FAILED: System.err not read correctly");64throw new Error();65}66if (other != null) {67System.err.println(68"\nTEST FAILED: Non-system PrintStream should have " +69"been written/read as null");70throw new Error();71}72} finally {73istream.close();74}7576System.err.println("\nTEST PASSED");77} catch (Exception e) {78System.err.print("TEST FAILED: ");79e.printStackTrace();80throw new Error();81}82}83}84858687/** ObjectOutputStream is extended to test the annotateClass()88* and replaceObject() subclassable methods.89* In annotateClass a magic string is written to the stream90* so that it can be verified in ObjectInputStream.91* replaceObject is used to subsititute a handle object for92* one of the standard PrintStreams (stdout or stderr).93*/94class TestOutputStream extends ObjectOutputStream {95/* Construct a new test stream */96TestOutputStream(OutputStream out) throws IOException {97super(out);98enableReplaceObject(true);99}100101/* When any class is written, add a "magic" string102* that must be verified by the TestInputStream.103*/104protected void annotateClass(Class cl) throws IOException {105this.writeUTF("magic");106}107108/* For each object of type PrintStream, substitute109* a StdStream handle object that encodes which110* of the standard print streams is being written.111* Other objects are written as themselves.112*/113protected Object replaceObject(Object obj)114throws IOException115{116/* For PrintStreams, like stdout and stderr, encode */117if (obj instanceof PrintStream) {118return new StdStream((PrintStream)obj);119}120return obj;121}122}123124/** Reverse the effects of TestOutputStream.125*/126class TestInputStream extends ObjectInputStream {127128TestInputStream(InputStream in) throws IOException {129super(in);130enableResolveObject(true);131}132133/** Verify that the magic string was written to the stream134* Also use the default classname->class resolution.135*/136protected Class<?> resolveClass(ObjectStreamClass classdesc)137throws ClassNotFoundException, IOException138{139try {140String s = readUTF();141if (!(s.equals("magic"))) {142System.err.println(143"\nTEST FAILED: Bad magic number");144throw new Error();145}146} catch (IOException ee) {147System.err.println(148"\nTEST FAILED: I/O Exception");149throw new Error();150}151return super.resolveClass(classdesc);152}153154/** If the object in the stream is a StdStream,155* get the mapping of it to the local System printstream and156* return it.157* Other objects are returned as themselves.158*/159protected Object resolveObject(Object obj) {160if (obj instanceof StdStream) {161return ((StdStream)obj).getStream();162}163return obj;164}165}166167/* A holder class to map between standard print streams (stdout, stderr)168* and a small integer.169*/170class StdStream implements java.io.Serializable {171private int stream = 0;172173public StdStream(PrintStream s) {174if (s == System.out) {175stream = 1;176} else if (s == System.err) {177stream = 2;178}179}180181public PrintStream getStream() {182if (stream == 1) {183return System.out;184} else if (stream == 2) {185return System.err;186} else {187return null;188}189}190}191192193