Path: blob/master/test/jdk/java/io/Serializable/oldTests/CheckForException.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/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 {107private static final long serialVersionUID = 1L;108109int ii = 17;110transient int tmp[];111112private void writeObject(ObjectOutputStream pw) throws IOException {113pw.writeUTF("PickleClass");114pw.writeInt(ii);115}116117private void readObject(ObjectInputStream pr) throws IOException {118tmp = new int[32];119pr.readUTF();120ii = pr.readInt();121}122123private void readObjectCleanup(ObjectInputStream pr) {124System.err.println("\nPickleClass cleanup correctly called on abort.");125if (tmp != null) {126tmp = null;127}128}129130}131132class NoPickleClass extends PickleClass {133private static final long serialVersionUID = 1L;134135private void writeObject(ObjectOutputStream pw)136throws NotSerializableException137{138throw new NotSerializableException("NoPickleClass");139}140141private void readObject(ObjectInputStream pr)142throws NotSerializableException143{144throw new NotSerializableException("NoPickleClass");145}146}147148class TryPickleClass extends NoPickleClass {149private static final long serialVersionUID = 1L;150151int i = 7;152transient int tmp[];153154private void writeObject(ObjectOutputStream pw) throws IOException {155pw.writeInt(i);156}157158private void readObject(ObjectInputStream pr) throws IOException {159tmp = new int[32];160i = pr.readInt();161}162163private void readObjectCleanup(ObjectInputStream pr) {164System.err.println("\nCleanup called on abort");165if (tmp != null) {166tmp = null;167}168}169}170171172