Path: blob/master/test/jdk/java/io/Serializable/evolution/AddedField/ReadAddedField.java
41161 views
/*1* Copyright (c) 1997, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @bug 408817625* @build WriteAddedField ReadAddedField26* @run main ReadAddedField27*28* @summary Deserialize an evolved class with a new field, field type is new.29*30*/3132import java.io.*;3334class IncompatibleFieldClass implements Serializable {35private static final long serialVersionUID = 3L;36int x = 5;37};3839class A implements Serializable {40// Version 1.0 of class A.41private static final long serialVersionUID = 1L;42public int bar;43}4445/** Test serial persistent fields w/o using Alternate API.46*/47class B implements Serializable {48private static final long serialVersionUID = 2L;49int bar;5051B() {52bar = 4;53}54}5556/** Test serial persistent fields using Alternate API.57* Also make sure that optional data to non-existent classes can be skipped.58*/59class C implements Serializable {60private static final long serialVersionUID = 3L;61int bar;6263C() {64bar = 4;65}6667private void readObject(ObjectInputStream s)68throws IOException, ClassNotFoundException69{70s.defaultReadObject();71}7273private void writeObject(ObjectOutputStream s)74throws IOException75{76s.defaultWriteObject();77}78}7980public class ReadAddedField {81public static void main(String args[])82throws IOException, ClassNotFoundException83{84File f = new File("tmp.ser");85ObjectInput in =86new ObjectInputStream(new FileInputStream(f));87A a = (A)in.readObject();88if (a.bar != 4)89throw new RuntimeException("a.bar does not equal 4, it equals " +90a.bar);91B b = (B)in.readObject();92if (b.bar != 4)93throw new RuntimeException("b.bar does not equal 4, it equals " +94b.bar);95C c = (C)in.readObject();96if (c.bar != 4)97throw new RuntimeException("c.bar does not equal 4, it equals " +98c.bar);99A aa = (A)in.readObject();100if (aa.bar != 4)101throw new RuntimeException("a.bar does not equal 4, it equals " +102aa.bar);103B bb = (B)in.readObject();104if (bb.bar != 4)105throw new RuntimeException("b.bar does not equal 4, it equals " +106bb.bar);107C cc = (C)in.readObject();108if (cc.bar != 4)109throw new RuntimeException("c.bar does not equal 4, it equals " +110cc.bar);111in.close();112}113}114115116