test/java/io/Serializable/evolution/RenamePackage/test/SerialDriver.java

Print this page
rev 3565 : 7022624: use try-with-resources in java.io tests
Reviewed-by: XXX


  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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  *
  26  * @bug 4087295
  27  * @build install/SerialDriver.java test/SerialDriver.java extension/ExtendedObjectInputStream.java
  28  * @summary Enable resolveClass() to accommodate package renaming.
  29  * This fix enables one to implement a resolveClass method that maps a
  30  * Serialiazable class within a serialization stream to the same class
  31  * in a different package within the JVM runtime. See run shell script
  32  * for instructions on how to run this test.
  33  */
  34 
  35 package test;
  36 
  37 import java.io.*;
  38 import extension.ExtendedObjectInputStream;
  39 
  40 public class SerialDriver implements Serializable {
  41     private static final long serialVersionUID = 1L;
  42     String name;
  43     SerialDriver next;
  44 
  45     public SerialDriver() {
  46         name = "<terminator>";
  47         next = null;
  48     }
  49 
  50     public SerialDriver(String name, SerialDriver next) {


  66          * The ability to deserialize or serialize allows
  67          * us to see the bidirectional readability and writeability
  68          */
  69         if (args.length == 1) {
  70             if (args[0].equals("-d")) {
  71                 deserialize = true;
  72             } else if (args[0].equals("-s")) {
  73                 serialize = true;
  74             } else {
  75                 usage();
  76                 throw new Exception("incorrect command line arguments");
  77             }
  78         } else {
  79             usage();
  80             throw new Exception("incorrect command line arguments");
  81         }
  82 
  83         File f = new File("stream.ser");
  84         if (serialize) {
  85             // Serialize the subclass
  86             try {
  87                 FileOutputStream fo = new FileOutputStream(f);
  88                 ObjectOutputStream so = new ObjectOutputStream(fo);
  89                 so.writeObject(obj);
  90                 /* Comment out since renaming arrays does not work
  91                    since it changes the serialVersionUID. */
  92                 so.writeObject(array);
  93                 so.flush();
  94             } catch (Exception e) {
  95                 System.out.println(e);
  96                 throw e;
  97             }
  98         }
  99         if (deserialize) {
 100             // Deserialize the subclass
 101             try {
 102                 FileInputStream fi = new FileInputStream(f);
 103                 ExtendedObjectInputStream si = new ExtendedObjectInputStream(fi);
 104                 si.addRenamedClassName("install.SerialDriver",
 105                                        "test.SerialDriver");
 106                 si.addRenamedClassName("[Linstall.SerialDriver;",
 107                                        "[Ltest.SerialDriver");
 108                 obj = (SerialDriver) si.readObject();
 109                 array = (SerialDriver[]) si.readObject();
 110                 si.close();
 111             } catch (Exception e) {
 112                 System.out.println(e);
 113                 throw e;
 114             }
 115             System.out.println();
 116             System.out.println("Printing deserialized class: ");
 117             System.out.println();
 118             System.out.println(obj.toString());
 119             System.out.println();
 120         }
 121     }
 122 
 123 
 124     public String toString() {
 125         String nextString = next != null ? next.toString() : "<null>";
 126         return "name =" + name + " next = <" + nextString + ">";
 127     }
 128 
 129     /**
 130      * Prints out the usage


  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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  *
  26  * @bug 4087295
  27  * @build install/SerialDriver.java test/SerialDriver.java extension/ExtendedObjectInputStream.java
  28  * @summary Enable resolveClass() to accommodate package renaming.
  29  * This fix enables one to implement a resolveClass method that maps a
  30  * Serializable class within a serialization stream to the same class
  31  * in a different package within the JVM runtime. See run shell script
  32  * for instructions on how to run this test.
  33  */
  34 
  35 package test;
  36 
  37 import java.io.*;
  38 import extension.ExtendedObjectInputStream;
  39 
  40 public class SerialDriver implements Serializable {
  41     private static final long serialVersionUID = 1L;
  42     String name;
  43     SerialDriver next;
  44 
  45     public SerialDriver() {
  46         name = "<terminator>";
  47         next = null;
  48     }
  49 
  50     public SerialDriver(String name, SerialDriver next) {


  66          * The ability to deserialize or serialize allows
  67          * us to see the bidirectional readability and writeability
  68          */
  69         if (args.length == 1) {
  70             if (args[0].equals("-d")) {
  71                 deserialize = true;
  72             } else if (args[0].equals("-s")) {
  73                 serialize = true;
  74             } else {
  75                 usage();
  76                 throw new Exception("incorrect command line arguments");
  77             }
  78         } else {
  79             usage();
  80             throw new Exception("incorrect command line arguments");
  81         }
  82 
  83         File f = new File("stream.ser");
  84         if (serialize) {
  85             // Serialize the subclass
  86             try (FileOutputStream fo = new FileOutputStream(f);
  87                  ObjectOutputStream so = new ObjectOutputStream(fo))
  88             {
  89                 so.writeObject(obj);
  90                 /* Comment out since renaming arrays does not work
  91                    since it changes the serialVersionUID. */
  92                 so.writeObject(array);

  93             } catch (Exception e) {
  94                 System.out.println(e);
  95                 throw e;
  96             }
  97         }
  98         if (deserialize) {
  99             // Deserialize the subclass
 100             try (FileInputStream fi = new FileInputStream(f);
 101                  ExtendedObjectInputStream si = new ExtendedObjectInputStream(fi))
 102             {
 103                 si.addRenamedClassName("install.SerialDriver",
 104                                        "test.SerialDriver");
 105                 si.addRenamedClassName("[Linstall.SerialDriver;",
 106                                        "[Ltest.SerialDriver");
 107                 obj = (SerialDriver) si.readObject();
 108                 array = (SerialDriver[]) si.readObject();

 109             } catch (Exception e) {
 110                 System.out.println(e);
 111                 throw e;
 112             }
 113             System.out.println();
 114             System.out.println("Printing deserialized class: ");
 115             System.out.println();
 116             System.out.println(obj.toString());
 117             System.out.println();
 118         }
 119     }
 120 
 121 
 122     public String toString() {
 123         String nextString = next != null ? next.toString() : "<null>";
 124         return "name =" + name + " next = <" + nextString + ">";
 125     }
 126 
 127     /**
 128      * Prints out the usage