Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/io/Serializable/oldTests/ValidateClass.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 was under25* /src/share/test/serialization/psiotest.java26* Test validation callbacks27*/2829import java.io.*;3031public class ValidateClass {32public static void main (String argv[]) {33System.err.println("\nRegression test for validation of callbacks \n");3435FileInputStream istream = null;36try {3738FileOutputStream ostream = new FileOutputStream("psiotest4.tmp");39ObjectOutputStream p = new ObjectOutputStream(ostream);4041/* Catch the expected exception and42* complain if it does not occur.43*/4445// Serialize a bunch of objects that will be validated when read46// Make a list of classes with intermingled priorities47Validator vc = new Validator(0, null);48vc = new Validator(2, vc);49vc = new Validator(0, vc);50vc = new Validator(3, vc);51vc = new Validator(Integer.MIN_VALUE, vc);52vc = new Validator(1, vc);53vc = new Validator(1, vc);54vc = new Validator(0, vc);5556p.writeObject(vc);57p.flush();58ostream.close();5960istream = new FileInputStream("psiotest4.tmp");61ObjectInputStream q = new ObjectInputStream(istream);6263Validator vc_u;6465vc_u = (Validator)q.readObject();66if (vc_u.validated != Integer.MIN_VALUE) {67System.err.println("\nTEST FAILED: Validation callbacks did " +68"not complete.");69throw new Error();70}71istream.close();72System.err.println("\nTEST PASSED");73} catch (Exception e) {74System.err.print("TEST FAILED: ");75e.printStackTrace();76throw new Error();77}78}79}8081class MissingWriterClass implements java.io.Serializable {82int i = 77;8384private void writeObject(ObjectOutputStream pw) throws IOException {85pw.writeInt(i);86}87}8889class MissingReaderClass implements java.io.Serializable {90int i = 77;9192private void readObject(ObjectInputStream pr) throws IOException {93i = pr.readInt();94}95}969798class Validator implements ObjectInputValidation, java.io.Serializable {99static int validated = Integer.MAX_VALUE; // Last value validated100int priority;101Validator next = null;102103public Validator(int prio, Validator n) {104priority = prio;105next = n;106}107108// Handle serialization/deserialization109private void writeObject(ObjectOutputStream pw) throws IOException {110pw.writeInt(priority);111pw.writeObject(next);112}113114private void readObject(ObjectInputStream pr)115throws IOException, ClassNotFoundException116{117priority = pr.readInt();118next = (Validator)pr.readObject();119120pr.registerValidation(this, priority);121}122123public void validateObject() throws InvalidObjectException {124if (validated < priority) {125System.err.println("\nTEST FAILED: Validations called out " +126"of order: Previous priority: " + validated + " < " +127"new priority: " + priority);128throw new Error();129}130validated = priority;131}132}133134135