Path: blob/master/test/jdk/java/io/Serializable/expectedStackTrace/ExpectedStackTrace.java
41153 views
/*1* Copyright (c) 2005, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24* @bug 6317435 711070025* @summary Verify that stack trace contains a proper cause of26* InvalidClassException (methods: checkSerialize,27* checkDeserialize or checkDefaultSerialize)28*29* @author Andrey Ozerov30*31*/32import java.io.*;3334class NotSerializableObject {35private String m_str;36private Integer m_int;3738public NotSerializableObject(String m_str, Integer m_int) {39this.m_str = m_str;40this.m_int = m_int;41}42}4344@SuppressWarnings("serial") /* Incorrect declarations are being tested */45class SerializableObject extends NotSerializableObject46implements Serializable47{48private static final long serialVersionUID = 1L;4950public SerializableObject(String m_str, Integer m_int) {51super(m_str, m_int);52}5354public SerializableObject() {55super("test", 10);56}57}5859public class ExpectedStackTrace {60private static final String SER_METHOD_NAME = "checkSerializable";6162public static final void main(String[] args) throws Exception {63System.err.println("\nRegression test for CRs 6317435, 7110700");64checkSerializable(getObject());65}6667private static Object getObject() throws Exception {68ObjectStreamClass osc =69ObjectStreamClass.lookup(SerializableObject.class);70SerializableObject initObj =71(SerializableObject) osc.forClass().getConstructor().newInstance();72return initObj;73}7475private static void checkSerializable(Object initObj) throws Exception {76try {77// Serialize to a byte array78ByteArrayOutputStream bos = new ByteArrayOutputStream() ;79ObjectOutputStream out = new ObjectOutputStream(bos) ;80out.writeObject(initObj);81out.close();8283// Get the bytes of the serialized object84byte[] buf = bos.toByteArray();8586// Deserialize from a byte array87ByteArrayInputStream bais = new ByteArrayInputStream(buf);88ObjectInputStream in = new ObjectInputStream(bais);89SerializableObject finObj = (SerializableObject) in.readObject();90in.close();91throw new Error();92} catch(ObjectStreamException ex) {93StackTraceElement[] stes = ex.getStackTrace();94boolean found = false;95for (int i = 0; i<stes.length-1; i++) {96StackTraceElement ste = stes[i];97String nme = ste.getMethodName();98if (nme.equals(SER_METHOD_NAME)) {99found = true;100}101}102if (found) {103if (ex.getCause() != null) {104throw new Error("\nTest for CR 7110700 FAILED");105}106System.err.println("\nTEST PASSED");107} else {108throw new Error("\nTest for CR 6317435 FAILED");109}110}111}112}113114115