jdk/test/java/io/Serializable/class/Test.java
changeset 2 90ce3da70b43
child 5506 202f599c92aa
equal deleted inserted replaced
0:fd16c54261b3 2:90ce3da70b43
       
     1 /*
       
     2  * Copyright 1998-2001 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 /*
       
    25  * @bug 4075221
       
    26  * @summary Enable serialize of nonSerializable Class descriptor.
       
    27  */
       
    28 
       
    29 import java.io.*;
       
    30 
       
    31 class Test {
       
    32     public static void main(String args[]) throws Exception {
       
    33         File f = new File("tmp.ser");
       
    34         if (args[0].compareTo("-s") == 0) {
       
    35             FileOutputStream of = new FileOutputStream(f);
       
    36             ObjectOutputStream oos = new ObjectOutputStream(of);
       
    37             Class cl = Class.forName(args[1]);
       
    38             oos.writeObject(cl);
       
    39             if (ObjectStreamClass.lookup(cl) != null)
       
    40                 oos.writeObject(cl.newInstance());
       
    41             oos.close();
       
    42             System.out.println("Serialized Class " + cl.getName());
       
    43         } else if (args[0].compareTo("-de") == 0) {
       
    44             FileInputStream inf = new FileInputStream(f);
       
    45             ObjectInputStream ois = new ObjectInputStream(inf);
       
    46             Class cl = null;
       
    47             try {
       
    48                 cl = (Class)ois.readObject();
       
    49                 throw new Error("Expected InvalidClassException to be thrown");
       
    50             } catch (InvalidClassException e) {
       
    51                 System.out.println("Caught expected exception DeSerializing class " + e.getMessage());
       
    52             }
       
    53             ois.close();
       
    54         } else if (args[0].compareTo("-doe") == 0) {
       
    55             FileInputStream inf = new FileInputStream(f);
       
    56             ObjectInputStream ois = new ObjectInputStream(inf);
       
    57             Class cl = null;
       
    58             cl = (Class)ois.readObject();
       
    59             try {
       
    60                 ois.readObject();
       
    61                 throw new Error("Expected InvalidClassException to be thrown");
       
    62             } catch (InvalidClassException e) {
       
    63                 System.out.println("Caught expected exception DeSerializing class " + e.getMessage());
       
    64             }
       
    65             ois.close();
       
    66         } else if (args[0].compareTo("-d") == 0) {
       
    67             FileInputStream inf = new FileInputStream(f);
       
    68             ObjectInputStream ois = new ObjectInputStream(inf);
       
    69             Class cl = (Class)ois.readObject();
       
    70             try {
       
    71                 ois.readObject();
       
    72             } catch (EOFException e) {
       
    73             }
       
    74             ois.close();
       
    75             System.out.println("DeSerialized Class " + cl.getName());
       
    76         }
       
    77     }
       
    78 }