J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2005 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. |
| 8 | * |
| 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | * accompanied this code). |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License version |
| 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | * |
| 19 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 21 | * have any questions. |
| 22 | */ |
| 23 | |
| 24 | /* @test |
| 25 | * @summary it is new version of old test which was under |
| 26 | * /src/share/test/serialization/psiotest.java |
| 27 | * Test validation callbacks |
| 28 | */ |
| 29 | |
| 30 | import java.io.*; |
| 31 | |
| 32 | public class ValidateClass { |
| 33 | public static void main (String argv[]) { |
| 34 | System.err.println("\nRegression test for validation of callbacks \n"); |
| 35 | |
| 36 | FileInputStream istream = null; |
| 37 | try { |
| 38 | |
| 39 | FileOutputStream ostream = new FileOutputStream("psiotest4.tmp"); |
| 40 | ObjectOutputStream p = new ObjectOutputStream(ostream); |
| 41 | |
| 42 | /* Catch the expected exception and |
| 43 | * complain if it does not occur. |
| 44 | */ |
| 45 | |
| 46 | // Serialize a bunch of objects that will be validated when read |
| 47 | // Make a list of classes with intermingled priorities |
| 48 | Validator vc = new Validator(0, null); |
| 49 | vc = new Validator(2, vc); |
| 50 | vc = new Validator(0, vc); |
| 51 | vc = new Validator(3, vc); |
| 52 | vc = new Validator(Integer.MIN_VALUE, vc); |
| 53 | vc = new Validator(1, vc); |
| 54 | vc = new Validator(1, vc); |
| 55 | vc = new Validator(0, vc); |
| 56 | |
| 57 | p.writeObject(vc); |
| 58 | p.flush(); |
| 59 | ostream.close(); |
| 60 | |
| 61 | istream = new FileInputStream("psiotest4.tmp"); |
| 62 | ObjectInputStream q = new ObjectInputStream(istream); |
| 63 | |
| 64 | Validator vc_u; |
| 65 | |
| 66 | vc_u = (Validator)q.readObject(); |
| 67 | if (vc_u.validated != Integer.MIN_VALUE) { |
| 68 | System.err.println("\nTEST FAILED: Validation callbacks did " + |
| 69 | "not complete."); |
| 70 | throw new Error(); |
| 71 | } |
| 72 | istream.close(); |
| 73 | System.err.println("\nTEST PASSED"); |
| 74 | } catch (Exception e) { |
| 75 | System.err.print("TEST FAILED: "); |
| 76 | e.printStackTrace(); |
| 77 | throw new Error(); |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | class MissingWriterClass implements java.io.Serializable { |
| 83 | int i = 77; |
| 84 | |
| 85 | private void writeObject(ObjectOutputStream pw) throws IOException { |
| 86 | pw.writeInt(i); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | class MissingReaderClass implements java.io.Serializable { |
| 91 | int i = 77; |
| 92 | |
| 93 | private void readObject(ObjectInputStream pr) throws IOException { |
| 94 | i = pr.readInt(); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | |
| 99 | class Validator implements ObjectInputValidation, java.io.Serializable { |
| 100 | static int validated = Integer.MAX_VALUE; // Last value validated |
| 101 | int priority; |
| 102 | Validator next = null; |
| 103 | |
| 104 | public Validator(int prio, Validator n) { |
| 105 | priority = prio; |
| 106 | next = n; |
| 107 | } |
| 108 | |
| 109 | // Handle serialization/deserialization |
| 110 | private void writeObject(ObjectOutputStream pw) throws IOException { |
| 111 | pw.writeInt(priority); |
| 112 | pw.writeObject(next); |
| 113 | } |
| 114 | |
| 115 | private void readObject(ObjectInputStream pr) |
| 116 | throws IOException, ClassNotFoundException |
| 117 | { |
| 118 | priority = pr.readInt(); |
| 119 | next = (Validator)pr.readObject(); |
| 120 | |
| 121 | pr.registerValidation(this, priority); |
| 122 | } |
| 123 | |
| 124 | public void validateObject() throws InvalidObjectException { |
| 125 | if (validated < priority) { |
| 126 | System.err.println("\nTEST FAILED: Validations called out " + |
| 127 | "of order: Previous priority: " + validated + " < " + |
| 128 | "new priority: " + priority); |
| 129 | throw new Error(); |
| 130 | } |
| 131 | validated = priority; |
| 132 | } |
| 133 | } |