Path: blob/master/test/jdk/java/io/Serializable/duplicateSerialFields/Test.java
41153 views
/*1* Copyright (c) 2002, 2010, 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/* @test24* @bug 476428025*26* @clean Setup Test A B27* @compile Setup.java28* @run main Setup29* @clean Setup A B30* @compile Test.java31* @run main Test32*33* @summary Verify that if a serializable class declares multiple34* serialPersistentFields that share the same name, calling35* ObjectStreamClass.lookup() for that class will not result in an36* InternalError, and that attempts at default serialization or37* deserialization of such a class will result in38* InvalidClassExceptions.39*/4041import java.io.*;4243class A implements Serializable {44private static final long serialVersionUID = 0L;45private static final ObjectStreamField[] serialPersistentFields = {46new ObjectStreamField("i", int.class),47new ObjectStreamField("i", int.class)48};49int i;50}5152class B implements Serializable {53private static final long serialVersionUID = 0L;54private static final ObjectStreamField[] serialPersistentFields = {55new ObjectStreamField("i", int.class),56new ObjectStreamField("i", String.class)57};58int i;59}6061public class Test {62public static void main(String[] args) throws Exception {6364ObjectStreamClass.lookup(A.class);65ObjectStreamClass.lookup(B.class);6667ObjectOutputStream oout =68new ObjectOutputStream(new ByteArrayOutputStream());69try {70oout.writeObject(new A());71throw new Error(72"write of A should fail with InvalidClassException");73} catch (InvalidClassException e) {74}7576oout = new ObjectOutputStream(new ByteArrayOutputStream());77try {78oout.writeObject(new B());79throw new Error(80"write of B should fail with InvalidClassException");81} catch (InvalidClassException e) {82}8384FileInputStream in = new FileInputStream("a.ser");85try {86ObjectInputStream oin = new ObjectInputStream(in);87oin.readObject();88throw new Error(89"read of A should fail with InvalidClassException");90} catch (InvalidClassException e) {91} finally {92in.close();93}9495in = new FileInputStream("b.ser");96try {97ObjectInputStream oin = new ObjectInputStream(in);98oin.readObject();99throw new Error(100"read of B should fail with InvalidClassException");101} catch (InvalidClassException e) {102} finally {103in.close();104}105}106}107108109