Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/io/Serializable/expectedStackTrace/ExpectedStackTrace.java
38828 views
/*1* Copyright (c) 2005, 2011, 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}4344class SerializableObject extends NotSerializableObject45implements Serializable46{4748public SerializableObject(String m_str, Integer m_int) {49super(m_str, m_int);50}5152public SerializableObject() {53super("test", 10);54}55}5657public class ExpectedStackTrace {58private static final String SER_METHOD_NAME = "checkSerializable";5960public static final void main(String[] args) throws Exception {61System.err.println("\nRegression test for CRs 6317435, 7110700");62checkSerializable(getObject());63}6465private static Object getObject() throws Exception {66ObjectStreamClass osc =67ObjectStreamClass.lookup(SerializableObject.class);68SerializableObject initObj =69(SerializableObject) osc.forClass().newInstance();70return initObj;71}7273private static void checkSerializable(Object initObj) throws Exception {74try {75// Serialize to a byte array76ByteArrayOutputStream bos = new ByteArrayOutputStream() ;77ObjectOutputStream out = new ObjectOutputStream(bos) ;78out.writeObject(initObj);79out.close();8081// Get the bytes of the serialized object82byte[] buf = bos.toByteArray();8384// Deserialize from a byte array85ByteArrayInputStream bais = new ByteArrayInputStream(buf);86ObjectInputStream in = new ObjectInputStream(bais);87SerializableObject finObj = (SerializableObject) in.readObject();88in.close();89throw new Error();90} catch(ObjectStreamException ex) {91StackTraceElement[] stes = ex.getStackTrace();92boolean found = false;93for (int i = 0; i<stes.length-1; i++) {94StackTraceElement ste = stes[i];95String nme = ste.getMethodName();96if (nme.equals(SER_METHOD_NAME)) {97found = true;98}99}100if (found) {101if (ex.getCause() != null) {102throw new Error("\nTest for CR 7110700 FAILED");103}104System.err.println("\nTEST PASSED");105} else {106throw new Error("\nTest for CR 6317435 FAILED");107}108}109}110}111112113