J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2007 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 | * @bug 6541870 |
| 26 | * @summary this test checks that ObjectInputStream throws an IOException |
| 27 | * instead of a NullPointerException when deserializing an ArrayList |
| 28 | * of Externalizables if there is an IOException while deserializing |
| 29 | * one of these Externalizables. |
| 30 | * |
| 31 | * @author Andrey Ozerov |
| 32 | */ |
| 33 | |
| 34 | import java.io.ByteArrayInputStream; |
| 35 | import java.io.ByteArrayOutputStream; |
| 36 | import java.io.IOException; |
| 37 | import java.io.ObjectInput; |
| 38 | import java.io.ObjectInputStream; |
| 39 | import java.io.ObjectOutput; |
| 40 | import java.io.ObjectOutputStream; |
| 41 | import java.util.ArrayList; |
| 42 | |
| 43 | public class NPEProvoker implements java.io.Externalizable { |
| 44 | private String test = "test"; |
| 45 | |
| 46 | public void readExternal(ObjectInput in) throws IOException, |
| 47 | ClassNotFoundException |
| 48 | { |
| 49 | throw new IOException(); //io exception for whatever reason |
| 50 | } |
| 51 | |
| 52 | public void writeExternal(ObjectOutput out) throws IOException { |
| 53 | out.writeObject(test); |
| 54 | } |
| 55 | |
| 56 | public static void main(String[] args) { |
| 57 | System.err.println("\n Regression test for bug 6541870\n"); |
| 58 | try { |
| 59 | ArrayList<NPEProvoker> list = new ArrayList<NPEProvoker>(); |
| 60 | list.add(new NPEProvoker()); |
| 61 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 62 | ObjectOutputStream oos = new ObjectOutputStream(baos); |
| 63 | oos.writeObject(list); |
| 64 | |
| 65 | ObjectInputStream ois = |
| 66 | new ObjectInputStream(new ByteArrayInputStream( |
| 67 | baos.toByteArray())); |
| 68 | ois.readObject(); |
| 69 | throw new Error(); |
| 70 | } catch (IOException e) { |
| 71 | System.err.println("\nTEST PASSED"); |
| 72 | } catch (ClassNotFoundException e) { |
| 73 | throw new Error(); |
| 74 | } catch (NullPointerException e) { |
| 75 | throw new Error(); |
| 76 | } |
| 77 | } |
| 78 | } |