Path: blob/master/test/jdk/java/io/Serializable/oldTests/AnnotateClass.java
41153 views
/*1* Copyright (c) 2005, 2019, 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)114{115/* For PrintStreams, like stdout and stderr, encode */116if (obj instanceof PrintStream) {117return new StdStream((PrintStream)obj);118}119return obj;120}121}122123/** Reverse the effects of TestOutputStream.124*/125class TestInputStream extends ObjectInputStream {126127TestInputStream(InputStream in) throws IOException {128super(in);129enableResolveObject(true);130}131132/** Verify that the magic string was written to the stream133* Also use the default classname->class resolution.134*/135protected Class<?> resolveClass(ObjectStreamClass classdesc)136throws ClassNotFoundException, IOException137{138try {139String s = readUTF();140if (!(s.equals("magic"))) {141System.err.println(142"\nTEST FAILED: Bad magic number");143throw new Error();144}145} catch (IOException ee) {146System.err.println(147"\nTEST FAILED: I/O Exception");148throw new Error();149}150return super.resolveClass(classdesc);151}152153/** If the object in the stream is a StdStream,154* get the mapping of it to the local System printstream and155* return it.156* Other objects are returned as themselves.157*/158protected Object resolveObject(Object obj) {159if (obj instanceof StdStream) {160return ((StdStream)obj).getStream();161}162return obj;163}164}165166/* A holder class to map between standard print streams (stdout, stderr)167* and a small integer.168*/169class StdStream implements java.io.Serializable {170private static final long serialVersionUID = 1L;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