jdk/test/java/io/Serializable/evolution/RenamePackage/install/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 install;
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    String name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    SerialDriver next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    transient Object objarray[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    public SerialDriver() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        name = "<terminator>";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        next = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    public SerialDriver(String name, SerialDriver next) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        this.name = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        this.next = next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    static boolean serialize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    static boolean deserialize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    public static void main(String args[]) throws Exception  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        SerialDriver obj = new SerialDriver("SerialDriver_2",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
                                            new SerialDriver());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        SerialDriver[] array = new SerialDriver[5];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        for (int i = 0; i < array.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            array[i] = new SerialDriver("SerialDriver_1_" + i, new SerialDriver());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
         * see if we are serializing or deserializing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
         * The ability to deserialize or serialize allows
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
         * us to see the bidirectional readability and writeability
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        if (args.length == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            if (args[0].equals("-d")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                deserialize = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            } else if (args[0].equals("-s")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                serialize = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                usage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                throw new Exception("incorrect command line arguments");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            usage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            throw new Exception("incorrect command line arguments");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        File f = new File("stream.ser");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        if (serialize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            // Serialize the subclass
8559
826c03991926 7022624: use try-with-resources in java.io tests
smarks
parents: 5506
diff changeset
    89
            try (FileOutputStream fo = new FileOutputStream(f);
826c03991926 7022624: use try-with-resources in java.io tests
smarks
parents: 5506
diff changeset
    90
                 ObjectOutputStream so = new ObjectOutputStream(fo))
826c03991926 7022624: use try-with-resources in java.io tests
smarks
parents: 5506
diff changeset
    91
            {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                so.writeObject(obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                /* Skip arrays since they do not work with rename yet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                   The serialVersionUID changes due to the name change
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                   and there is no way to set the serialVersionUID for an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                   array. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                so.writeObject(array);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                System.out.println(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        if (deserialize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            // Deserialize the subclass
8559
826c03991926 7022624: use try-with-resources in java.io tests
smarks
parents: 5506
diff changeset
   105
            try (FileInputStream fi = new FileInputStream(f);
826c03991926 7022624: use try-with-resources in java.io tests
smarks
parents: 5506
diff changeset
   106
                 ExtendedObjectInputStream si = new ExtendedObjectInputStream(fi))
826c03991926 7022624: use try-with-resources in java.io tests
smarks
parents: 5506
diff changeset
   107
            {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                si.addRenamedClassName("test.SerialDriver", "install.SerialDriver");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                si.addRenamedClassName("[Ltest.SerialDriver;",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                                       "[Linstall.SerialDriver");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                obj = (SerialDriver) si.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                array = (SerialDriver[]) si.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                System.out.println(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            System.out.println("Printing deserialized class: ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            System.out.println(obj.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        String nextString = next != null ? next.toString() : "<null>";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        return "name =" + name + " next = <" + nextString + ">";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * Prints out the usage
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    static void usage() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        System.out.println("Usage:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        System.out.println("      -s (in order to serialize)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        System.out.println("      -d (in order to deserialize)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
}