1/*
2 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * 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
34import java.io.ByteArrayInputStream;
35import java.io.ByteArrayOutputStream;
36import java.io.IOException;
37import java.io.ObjectInput;
38import java.io.ObjectInputStream;
39import java.io.ObjectOutput;
40import java.io.ObjectOutputStream;
41import java.util.ArrayList;
42
43public 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<>();
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}
79