Path: blob/master/test/jdk/java/io/Serializable/oldTests/ValidateClass.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 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 (Validator.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 {82private static final long serialVersionUID = 1L;83int i = 77;8485private void writeObject(ObjectOutputStream pw) throws IOException {86pw.writeInt(i);87}88}8990class MissingReaderClass implements java.io.Serializable {91private static final long serialVersionUID = 1L;92int i = 77;9394private void readObject(ObjectInputStream pr) throws IOException {95i = pr.readInt();96}97}9899100class Validator implements ObjectInputValidation, java.io.Serializable {101private static final long serialVersionUID = 1L;102103static int validated = Integer.MAX_VALUE; // Last value validated104int priority;105Validator next = null;106107public Validator(int prio, Validator n) {108priority = prio;109next = n;110}111112// Handle serialization/deserialization113private void writeObject(ObjectOutputStream pw) throws IOException {114pw.writeInt(priority);115pw.writeObject(next);116}117118private void readObject(ObjectInputStream pr)119throws IOException, ClassNotFoundException120{121priority = pr.readInt();122next = (Validator)pr.readObject();123124pr.registerValidation(this, priority);125}126127public void validateObject() throws InvalidObjectException {128if (validated < priority) {129System.err.println("\nTEST FAILED: Validations called out " +130"of order: Previous priority: " + validated + " < " +131"new priority: " + priority);132throw new Error();133}134validated = priority;135}136}137138139