jdk/test/java/io/Serializable/expectedStackTrace/ExpectedStackTrace.java
changeset 2 90ce3da70b43
child 5506 202f599c92aa
equal deleted inserted replaced
0:fd16c54261b3 2:90ce3da70b43
       
     1 /*
       
     2  * Copyright 2005 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    20  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    21  * have any questions.
       
    22  */
       
    23 
       
    24 /* @test
       
    25  * @bug 6317435
       
    26  * @summary Verify that stack trace contains a proper cause of
       
    27  *          InvalidClassException (methods: checkSerialize,
       
    28  *          checkDeserialize or checkDefaultSerialize)
       
    29  *
       
    30  * @author Andrey Ozerov
       
    31  *
       
    32  */
       
    33 import java.io.*;
       
    34 
       
    35 class NotSerializableObject {
       
    36     private String m_str;
       
    37     private Integer m_int;
       
    38 
       
    39     public NotSerializableObject(String m_str, Integer m_int) {
       
    40         this.m_str = m_str;
       
    41         this.m_int = m_int;
       
    42     }
       
    43 }
       
    44 
       
    45 class SerializableObject extends NotSerializableObject
       
    46     implements Serializable
       
    47 {
       
    48 
       
    49     public SerializableObject(String m_str, Integer m_int) {
       
    50         super(m_str, m_int);
       
    51     }
       
    52 
       
    53     public SerializableObject() {
       
    54         super("test", 10);
       
    55     }
       
    56 }
       
    57 
       
    58 public class ExpectedStackTrace {
       
    59     private static final String SER_METHOD_NAME = "checkSerializable";
       
    60 
       
    61     public static final void main(String[] args) throws Exception {
       
    62         System.err.println("\nRegression test for CR6317435");
       
    63         checkSerializable(getObject());
       
    64     }
       
    65 
       
    66     private static Object getObject() throws Exception {
       
    67         ObjectStreamClass osc =
       
    68             ObjectStreamClass.lookup(SerializableObject.class);
       
    69         SerializableObject initObj =
       
    70             (SerializableObject) osc.forClass().newInstance();
       
    71         return initObj;
       
    72     }
       
    73 
       
    74     private static void checkSerializable(Object initObj) throws Exception {
       
    75         try {
       
    76             // Serialize to a byte array
       
    77             ByteArrayOutputStream bos = new ByteArrayOutputStream() ;
       
    78             ObjectOutputStream out = new ObjectOutputStream(bos) ;
       
    79             out.writeObject(initObj);
       
    80             out.close();
       
    81 
       
    82             // Get the bytes of the serialized object
       
    83             byte[] buf = bos.toByteArray();
       
    84 
       
    85             // Deserialize from a byte array
       
    86             ByteArrayInputStream bais = new ByteArrayInputStream(buf);
       
    87             ObjectInputStream in = new ObjectInputStream(bais);
       
    88             SerializableObject finObj = (SerializableObject) in.readObject();
       
    89             in.close();
       
    90             throw new Error();
       
    91         } catch(ObjectStreamException ex) {
       
    92             StackTraceElement[] stes = ex.getStackTrace();
       
    93             boolean found = false;
       
    94             for (int i = 0; i<stes.length-1; i++) {
       
    95                 StackTraceElement ste = stes[i];
       
    96                 String nme = ste.getMethodName();
       
    97                 if (nme.equals(SER_METHOD_NAME)) {
       
    98                     found = true;
       
    99                 }
       
   100             }
       
   101             if (found) {
       
   102                 System.err.println("\nTEST PASSED");
       
   103             } else {
       
   104                 throw new Error();
       
   105             }
       
   106         }
       
   107     }
       
   108 }