Read.java revision 3261:a06412e13bf7
1229159Sadrian/*
2229159Sadrian * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
3229159Sadrian * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4229159Sadrian *
5229159Sadrian * This code is free software; you can redistribute it and/or modify it
6229159Sadrian * under the terms of the GNU General Public License version 2 only, as
7229159Sadrian * published by the Free Software Foundation.
8229159Sadrian *
9229159Sadrian * This code is distributed in the hope that it will be useful, but WITHOUT
10229159Sadrian * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11229159Sadrian * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12229159Sadrian * version 2 for more details (a copy is included in the LICENSE file that
13229159Sadrian * accompanied this code).
14229159Sadrian *
15229159Sadrian * You should have received a copy of the GNU General Public License version
16229159Sadrian * 2 along with this work; if not, write to the Free Software Foundation,
17229159Sadrian * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18229159Sadrian *
19229159Sadrian * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20229159Sadrian * or visit www.oracle.com if you need additional information or have any
21229159Sadrian * questions.
22229159Sadrian */
23229159Sadrian
24229159Sadrian/*
25229159Sadrian * @bug 4511532
26229159Sadrian * @summary Verify that the message string of a ClassCastException thrown by
27229159Sadrian *          ObjectInputStream when attempting to assign a value to a field of
28229159Sadrian *          an incompatible type contains the names of the value's class, the
29229159Sadrian *          field's declaring class, the field's type, and the field itself.
30229159Sadrian */
31229159Sadrian
32229159Sadrianimport java.io.*;
33229159Sadrian
34229159Sadrianclass Foo implements Serializable {
35229159Sadrian    private static final long serialVersionUID = 0L;
36229159Sadrian    private Float bar;
37229159Sadrian}
38229159Sadrian
39229159Sadrianclass Gub extends Foo {}
40229159Sadrian
41229159Sadrianpublic class Read {
42229159Sadrian    public static void main(String[] args) throws Exception {
43229159Sadrian        FileInputStream in = new FileInputStream("tmp.ser");
44229159Sadrian        try {
45229159Sadrian            ObjectInputStream oin = new ObjectInputStream(in);
46229159Sadrian            oin.readObject();
47229159Sadrian            throw new Error("readObject should not succeed");
48229159Sadrian        } catch (ClassCastException e) {
49            String s = e.getMessage();
50            System.out.println("ClassCastException message: " + s);
51            if (s == null ||
52                s.indexOf(Foo.class.getName()) == -1 ||
53                s.indexOf(Integer.class.getName()) == -1 ||
54                s.indexOf(Float.class.getName()) == -1 ||
55                s.indexOf(Gub.class.getName()) == -1 ||
56                s.indexOf("bar") == -1)
57            {
58                throw new Error("ClassNotFoundException message incomplete");
59            }
60        } finally {
61            in.close();
62        }
63    }
64}
65