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 |
| 26 | * /src/share/test/serialization/psiotest.java |
| 27 | * Test pickling and unpickling an object with derived classes |
| 28 | * and using a read special to serialize the "middle" class, |
| 29 | * which raises NotSerializableException inside writeObject() |
| 30 | * and readObject() methods. |
| 31 | */ |
| 32 | |
| 33 | import java.io.*; |
| 34 | |
| 35 | public class CheckForException { |
| 36 | public static void main (String argv[]) { |
| 37 | System.err.println("\nRegression test of " + |
| 38 | "serialization/deserialization of " + |
| 39 | "complex objects which raise " + |
| 40 | "NotSerializableException inside " + |
| 41 | "writeObject() and readObject() methods.\n"); |
| 42 | |
| 43 | |
| 44 | FileInputStream istream = null; |
| 45 | try { |
| 46 | |
| 47 | FileOutputStream ostream = new FileOutputStream("psiotest3.tmp"); |
| 48 | ObjectOutputStream p = new ObjectOutputStream(ostream); |
| 49 | |
| 50 | /* Catch the expected exception and |
| 51 | * complain if it does not occur. |
| 52 | */ |
| 53 | TryPickleClass npc = new TryPickleClass(); |
| 54 | |
| 55 | NotSerializableException we = null; |
| 56 | try { |
| 57 | // Two objects of the same class that are used |
| 58 | // in cleanup test below |
| 59 | p.writeObject("test"); |
| 60 | p.writeObject("test2"); |
| 61 | p.writeObject(npc); |
| 62 | } catch (NotSerializableException e) { |
| 63 | we = e; |
| 64 | } |
| 65 | if (we == null) { |
| 66 | System.err.println("\nTEST FAILED: Write of NoPickleClass " + |
| 67 | "should have raised an exception"); |
| 68 | throw new Error(); |
| 69 | } |
| 70 | |
| 71 | p.flush(); |
| 72 | ostream.close(); |
| 73 | |
| 74 | istream = new FileInputStream("psiotest3.tmp"); |
| 75 | ObjectInputStream q = new ObjectInputStream(istream); |
| 76 | |
| 77 | /* Catch the expected exception and |
| 78 | * complain if it does not occur. |
| 79 | */ |
| 80 | TryPickleClass npc_u; |
| 81 | |
| 82 | NotSerializableException re = null; |
| 83 | try { |
| 84 | // Read the two objects, neither has a cleanup method |
| 85 | q.readObject(); |
| 86 | q.readObject(); |
| 87 | npc_u = (TryPickleClass)q.readObject(); |
| 88 | } catch (NotSerializableException e) { |
| 89 | re = e; |
| 90 | } |
| 91 | if (re == null) { |
| 92 | System.err.println("\nTEST FAILED: Read of NoPickleClass " + |
| 93 | "should have raised an exception"); |
| 94 | throw new Error(); |
| 95 | } |
| 96 | |
| 97 | istream.close(); |
| 98 | System.err.println("\nTEST PASSED"); |
| 99 | } catch (Exception e) { |
| 100 | System.err.print("TEST FAILED: "); |
| 101 | e.printStackTrace(); |
| 102 | throw new Error(); |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | class PickleClass implements java.io.Serializable { |
| 108 | int ii = 17; |
| 109 | transient int tmp[]; |
| 110 | |
| 111 | private void writeObject(ObjectOutputStream pw) throws IOException { |
| 112 | pw.writeUTF("PickleClass"); |
| 113 | pw.writeInt(ii); |
| 114 | } |
| 115 | |
| 116 | private void readObject(ObjectInputStream pr) throws IOException { |
| 117 | tmp = new int[32]; |
| 118 | pr.readUTF(); |
| 119 | ii = pr.readInt(); |
| 120 | } |
| 121 | |
| 122 | private void readObjectCleanup(ObjectInputStream pr) { |
| 123 | System.err.println("\nPickleClass cleanup correctly called on abort."); |
| 124 | if (tmp != null) { |
| 125 | tmp = null; |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | } |
| 130 | |
| 131 | class NoPickleClass extends PickleClass { |
| 132 | private void writeObject(ObjectOutputStream pw) |
| 133 | throws NotSerializableException |
| 134 | { |
| 135 | throw new NotSerializableException("NoPickleClass"); |
| 136 | } |
| 137 | |
| 138 | private void readObject(ObjectInputStream pr) |
| 139 | throws NotSerializableException |
| 140 | { |
| 141 | throw new NotSerializableException("NoPickleClass"); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | class TryPickleClass extends NoPickleClass { |
| 146 | int i = 7; |
| 147 | transient int tmp[]; |
| 148 | |
| 149 | private void writeObject(ObjectOutputStream pw) throws IOException { |
| 150 | pw.writeInt(i); |
| 151 | } |
| 152 | |
| 153 | private void readObject(ObjectInputStream pr) throws IOException { |
| 154 | tmp = new int[32]; |
| 155 | i = pr.readInt(); |
| 156 | } |
| 157 | |
| 158 | private void readObjectCleanup(ObjectInputStream pr) { |
| 159 | System.err.println("\nCleanup called on abort"); |
| 160 | if (tmp != null) { |
| 161 | tmp = null; |
| 162 | } |
| 163 | } |
| 164 | } |