Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/io/Serializable/oldTests/CheckForException.java
38828 views
/*1* Copyright (c) 2005, 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/psiotest.java26* Test pickling and unpickling an object with derived classes27* and using a read special to serialize the "middle" class,28* which raises NotSerializableException inside writeObject()29* and readObject() methods.30*/3132import java.io.*;3334public class CheckForException {35public static void main (String argv[]) {36System.err.println("\nRegression test of " +37"serialization/deserialization of " +38"complex objects which raise " +39"NotSerializableException inside " +40"writeObject() and readObject() methods.\n");414243FileInputStream istream = null;44try {4546FileOutputStream ostream = new FileOutputStream("psiotest3.tmp");47ObjectOutputStream p = new ObjectOutputStream(ostream);4849/* Catch the expected exception and50* complain if it does not occur.51*/52TryPickleClass npc = new TryPickleClass();5354NotSerializableException we = null;55try {56// Two objects of the same class that are used57// in cleanup test below58p.writeObject("test");59p.writeObject("test2");60p.writeObject(npc);61} catch (NotSerializableException e) {62we = e;63}64if (we == null) {65System.err.println("\nTEST FAILED: Write of NoPickleClass " +66"should have raised an exception");67throw new Error();68}6970p.flush();71ostream.close();7273istream = new FileInputStream("psiotest3.tmp");74ObjectInputStream q = new ObjectInputStream(istream);7576/* Catch the expected exception and77* complain if it does not occur.78*/79TryPickleClass npc_u;8081NotSerializableException re = null;82try {83// Read the two objects, neither has a cleanup method84q.readObject();85q.readObject();86npc_u = (TryPickleClass)q.readObject();87} catch (NotSerializableException e) {88re = e;89}90if (re == null) {91System.err.println("\nTEST FAILED: Read of NoPickleClass " +92"should have raised an exception");93throw new Error();94}9596istream.close();97System.err.println("\nTEST PASSED");98} catch (Exception e) {99System.err.print("TEST FAILED: ");100e.printStackTrace();101throw new Error();102}103}104}105106class PickleClass implements java.io.Serializable {107int ii = 17;108transient int tmp[];109110private void writeObject(ObjectOutputStream pw) throws IOException {111pw.writeUTF("PickleClass");112pw.writeInt(ii);113}114115private void readObject(ObjectInputStream pr) throws IOException {116tmp = new int[32];117pr.readUTF();118ii = pr.readInt();119}120121private void readObjectCleanup(ObjectInputStream pr) {122System.err.println("\nPickleClass cleanup correctly called on abort.");123if (tmp != null) {124tmp = null;125}126}127128}129130class NoPickleClass extends PickleClass {131private void writeObject(ObjectOutputStream pw)132throws NotSerializableException133{134throw new NotSerializableException("NoPickleClass");135}136137private void readObject(ObjectInputStream pr)138throws NotSerializableException139{140throw new NotSerializableException("NoPickleClass");141}142}143144class TryPickleClass extends NoPickleClass {145int i = 7;146transient int tmp[];147148private void writeObject(ObjectOutputStream pw) throws IOException {149pw.writeInt(i);150}151152private void readObject(ObjectInputStream pr) throws IOException {153tmp = new int[32];154i = pr.readInt();155}156157private void readObjectCleanup(ObjectInputStream pr) {158System.err.println("\nCleanup called on abort");159if (tmp != null) {160tmp = null;161}162}163}164165166