Path: blob/master/test/jdk/java/io/Serializable/enum/unshared/Test.java
41159 views
/*1* Copyright (c) 2003, 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 483837925* @summary Verify that unshared write and read operations work properly with26* enum constants.27*/2829import java.io.*;3031enum Foo { foo, bar, baz }3233abstract class WriteReadTest {3435abstract void write(ObjectOutputStream out) throws Exception;36abstract void read(ObjectInputStream in) throws Exception;3738void run() throws Exception {39ByteArrayOutputStream bout = new ByteArrayOutputStream();40ObjectOutputStream oout = new ObjectOutputStream(bout);41write(oout);42oout.close();43read(new ObjectInputStream(44new ByteArrayInputStream(bout.toByteArray())));45}46}4748public class Test {49public static void main(String[] args) throws Exception {50WriteReadTest[] tests = {51new WriteReadTest() {52void write(ObjectOutputStream out) throws Exception {53out.writeObject(Foo.foo);54out.writeObject(Foo.foo);55}56void read(ObjectInputStream in) throws Exception {57Object obj = in.readObject();58if (obj != Foo.foo) {59throw new Error(60"expected " + Foo.foo + " instead of " + obj);61}62try {63obj = in.readUnshared();64throw new Error(65"read of " + obj + " should not have succeeded");66} catch (ObjectStreamException e) {67System.out.println("caught expected exception " + e);68}69}70},71new WriteReadTest() {72void write(ObjectOutputStream out) throws Exception {73out.writeObject(Foo.foo);74out.writeObject(Foo.foo);75}76void read(ObjectInputStream in) throws Exception {77Object obj = in.readUnshared();78if (obj != Foo.foo) {79throw new Error(80"expected " + Foo.foo + " instead of " + obj);81}82try {83obj = in.readObject();84throw new Error(85"read of " + obj + " should not have succeeded");86} catch (ObjectStreamException e) {87System.out.println("caught expected exception " + e);88}89}90},91new WriteReadTest() {92void write(ObjectOutputStream out) throws Exception {93out.writeObject(Foo.foo);94out.writeUnshared(Foo.foo);95}96void read(ObjectInputStream in) throws Exception {97Object obj = in.readUnshared();98if (obj != Foo.foo) {99throw new Error(100"expected " + Foo.foo + " instead of " + obj);101}102obj = in.readUnshared();103if (obj != Foo.foo) {104throw new Error(105"expected " + Foo.foo + " instead of " + obj);106}107}108},109new WriteReadTest() {110void write(ObjectOutputStream out) throws Exception {111out.writeUnshared(Foo.foo);112out.writeObject(Foo.foo);113}114void read(ObjectInputStream in) throws Exception {115Object obj = in.readUnshared();116if (obj != Foo.foo) {117throw new Error(118"expected " + Foo.foo + " instead of " + obj);119}120obj = in.readUnshared();121if (obj != Foo.foo) {122throw new Error(123"expected " + Foo.foo + " instead of " + obj);124}125}126}127};128for (int i = 0; i < tests.length; i++) {129tests[i].run();130}131}132}133134135