ExpectedStackTrace.java revision 2362
0N/A/*
2362N/A * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/* @test
0N/A * @bug 6317435
0N/A * @summary Verify that stack trace contains a proper cause of
0N/A * InvalidClassException (methods: checkSerialize,
0N/A * checkDeserialize or checkDefaultSerialize)
0N/A *
0N/A * @author Andrey Ozerov
0N/A *
0N/A */
0N/Aimport java.io.*;
0N/A
0N/Aclass NotSerializableObject {
0N/A private String m_str;
0N/A private Integer m_int;
0N/A
0N/A public NotSerializableObject(String m_str, Integer m_int) {
0N/A this.m_str = m_str;
0N/A this.m_int = m_int;
0N/A }
0N/A}
0N/A
0N/Aclass SerializableObject extends NotSerializableObject
0N/A implements Serializable
0N/A{
0N/A
0N/A public SerializableObject(String m_str, Integer m_int) {
0N/A super(m_str, m_int);
0N/A }
0N/A
0N/A public SerializableObject() {
0N/A super("test", 10);
0N/A }
0N/A}
0N/A
0N/Apublic class ExpectedStackTrace {
0N/A private static final String SER_METHOD_NAME = "checkSerializable";
0N/A
0N/A public static final void main(String[] args) throws Exception {
0N/A System.err.println("\nRegression test for CR6317435");
0N/A checkSerializable(getObject());
0N/A }
0N/A
0N/A private static Object getObject() throws Exception {
0N/A ObjectStreamClass osc =
0N/A ObjectStreamClass.lookup(SerializableObject.class);
0N/A SerializableObject initObj =
0N/A (SerializableObject) osc.forClass().newInstance();
0N/A return initObj;
0N/A }
0N/A
0N/A private static void checkSerializable(Object initObj) throws Exception {
0N/A try {
0N/A // Serialize to a byte array
0N/A ByteArrayOutputStream bos = new ByteArrayOutputStream() ;
0N/A ObjectOutputStream out = new ObjectOutputStream(bos) ;
0N/A out.writeObject(initObj);
0N/A out.close();
0N/A
0N/A // Get the bytes of the serialized object
0N/A byte[] buf = bos.toByteArray();
0N/A
0N/A // Deserialize from a byte array
0N/A ByteArrayInputStream bais = new ByteArrayInputStream(buf);
0N/A ObjectInputStream in = new ObjectInputStream(bais);
0N/A SerializableObject finObj = (SerializableObject) in.readObject();
0N/A in.close();
0N/A throw new Error();
0N/A } catch(ObjectStreamException ex) {
0N/A StackTraceElement[] stes = ex.getStackTrace();
0N/A boolean found = false;
0N/A for (int i = 0; i<stes.length-1; i++) {
0N/A StackTraceElement ste = stes[i];
0N/A String nme = ste.getMethodName();
0N/A if (nme.equals(SER_METHOD_NAME)) {
0N/A found = true;
0N/A }
0N/A }
0N/A if (found) {
0N/A System.err.println("\nTEST PASSED");
0N/A } else {
0N/A throw new Error();
0N/A }
0N/A }
0N/A }
0N/A}