jdk/test/java/io/Serializable/evolution/RenamePackage/test/SerialDriver.java
author ohair
Wed, 06 Apr 2011 22:06:11 -0700
changeset 9035 1255eb81cc2f
parent 8559 826c03991926
permissions -rw-r--r--
7033660: Update copyright year to 2011 on any files changed in 2011 Reviewed-by: dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
9035
1255eb81cc2f 7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents: 8559
diff changeset
     2
 * Copyright (c) 1998, 2011, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 4087295
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @build install/SerialDriver.java test/SerialDriver.java extension/ExtendedObjectInputStream.java
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @summary Enable resolveClass() to accommodate package renaming.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * This fix enables one to implement a resolveClass method that maps a
8559
826c03991926 7022624: use try-with-resources in java.io tests
smarks
parents: 5506
diff changeset
    30
 * Serializable class within a serialization stream to the same class
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * in a different package within the JVM runtime. See run shell script
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * for instructions on how to run this test.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
package test;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import extension.ExtendedObjectInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
public class SerialDriver implements Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    private static final long serialVersionUID = 1L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    String name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    SerialDriver next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    public SerialDriver() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        name = "<terminator>";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        next = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    public SerialDriver(String name, SerialDriver next) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        this.name = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        this.next = next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    static boolean serialize = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    static boolean deserialize = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    public static void main(String args[])  throws Exception  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        SerialDriver obj = new SerialDriver("SerialDriver_1", new SerialDriver());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        SerialDriver[] array = new SerialDriver[5];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        for (int i = 0; i < array.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            array[i] = new SerialDriver("SerialDriver_1_" + i, new SerialDriver());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
         * see if we are serializing or deserializing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
         * The ability to deserialize or serialize allows
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
         * us to see the bidirectional readability and writeability
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        if (args.length == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            if (args[0].equals("-d")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                deserialize = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            } else if (args[0].equals("-s")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                serialize = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                usage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                throw new Exception("incorrect command line arguments");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            usage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            throw new Exception("incorrect command line arguments");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        File f = new File("stream.ser");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        if (serialize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            // Serialize the subclass
8559
826c03991926 7022624: use try-with-resources in java.io tests
smarks
parents: 5506
diff changeset
    86
            try (FileOutputStream fo = new FileOutputStream(f);
826c03991926 7022624: use try-with-resources in java.io tests
smarks
parents: 5506
diff changeset
    87
                 ObjectOutputStream so = new ObjectOutputStream(fo))
826c03991926 7022624: use try-with-resources in java.io tests
smarks
parents: 5506
diff changeset
    88
            {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                so.writeObject(obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                /* Comment out since renaming arrays does not work
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                   since it changes the serialVersionUID. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                so.writeObject(array);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                System.out.println(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        if (deserialize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            // Deserialize the subclass
8559
826c03991926 7022624: use try-with-resources in java.io tests
smarks
parents: 5506
diff changeset
   100
            try (FileInputStream fi = new FileInputStream(f);
826c03991926 7022624: use try-with-resources in java.io tests
smarks
parents: 5506
diff changeset
   101
                 ExtendedObjectInputStream si = new ExtendedObjectInputStream(fi))
826c03991926 7022624: use try-with-resources in java.io tests
smarks
parents: 5506
diff changeset
   102
            {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                si.addRenamedClassName("install.SerialDriver",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                                       "test.SerialDriver");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                si.addRenamedClassName("[Linstall.SerialDriver;",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                                       "[Ltest.SerialDriver");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                obj = (SerialDriver) si.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                array = (SerialDriver[]) si.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                System.out.println(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            System.out.println("Printing deserialized class: ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            System.out.println(obj.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        String nextString = next != null ? next.toString() : "<null>";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        return "name =" + name + " next = <" + nextString + ">";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * Prints out the usage
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    static void usage() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        System.out.println("Usage:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        System.out.println("      -s (in order to serialize)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        System.out.println("      -d (in order to deserialize)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
}