1 /*
   2  * Copyright 2003 Sun Microsystems, Inc.  All Rights Reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  20  * CA 95054 USA or visit www.sun.com if you need additional information or
  21  * have any questions.
  22  */
  23 
  24 /* @test
  25  * @bug 4838379
  26  * @summary Verify that unshared write and read operations work properly with
  27  *          enum constants.
  28  *
  29  * @compile -source 1.5 Test.java
  30  * @run main Test
  31  */
  32 
  33 import java.io.*;
  34 
  35 enum Foo { foo, bar, baz }
  36 
  37 abstract class WriteReadTest {
  38 
  39     abstract void write(ObjectOutputStream out) throws Exception;
  40     abstract void read(ObjectInputStream in) throws Exception;
  41 
  42     void run() throws Exception {
  43         ByteArrayOutputStream bout = new ByteArrayOutputStream();
  44         ObjectOutputStream oout = new ObjectOutputStream(bout);
  45         write(oout);
  46         oout.close();
  47         read(new ObjectInputStream(
  48             new ByteArrayInputStream(bout.toByteArray())));
  49     }
  50 }
  51 
  52 public class Test {
  53     public static void main(String[] args) throws Exception {
  54         WriteReadTest[] tests = {
  55             new WriteReadTest() {
  56                 void write(ObjectOutputStream out) throws Exception {
  57                     out.writeObject(Foo.foo);
  58                     out.writeObject(Foo.foo);
  59                 }
  60                 void read(ObjectInputStream in) throws Exception {
  61                     Object obj = in.readObject();
  62                     if (obj != Foo.foo) {
  63                         throw new Error(
  64                             "expected " + Foo.foo + " instead of " + obj);
  65                     }
  66                     try {
  67                         obj = in.readUnshared();
  68                         throw new Error(
  69                             "read of " + obj + " should not have succeeded");
  70                     } catch (ObjectStreamException e) {
  71                         System.out.println("caught expected exception " + e);
  72                     }
  73                 }
  74             },
  75             new WriteReadTest() {
  76                 void write(ObjectOutputStream out) throws Exception {
  77                     out.writeObject(Foo.foo);
  78                     out.writeObject(Foo.foo);
  79                 }
  80                 void read(ObjectInputStream in) throws Exception {
  81                     Object obj = in.readUnshared();
  82                     if (obj != Foo.foo) {
  83                         throw new Error(
  84                             "expected " + Foo.foo + " instead of " + obj);
  85                     }
  86                     try {
  87                         obj = in.readObject();
  88                         throw new Error(
  89                             "read of " + obj + " should not have succeeded");
  90                     } catch (ObjectStreamException e) {
  91                         System.out.println("caught expected exception " + e);
  92                     }
  93                 }
  94             },
  95             new WriteReadTest() {
  96                 void write(ObjectOutputStream out) throws Exception {
  97                     out.writeObject(Foo.foo);
  98                     out.writeUnshared(Foo.foo);
  99                 }
 100                 void read(ObjectInputStream in) throws Exception {
 101                     Object obj = in.readUnshared();
 102                     if (obj != Foo.foo) {
 103                         throw new Error(
 104                             "expected " + Foo.foo + " instead of " + obj);
 105                     }
 106                     obj = in.readUnshared();
 107                     if (obj != Foo.foo) {
 108                         throw new Error(
 109                             "expected " + Foo.foo + " instead of " + obj);
 110                     }
 111                 }
 112             },
 113             new WriteReadTest() {
 114                 void write(ObjectOutputStream out) throws Exception {
 115                     out.writeUnshared(Foo.foo);
 116                     out.writeObject(Foo.foo);
 117                 }
 118                 void read(ObjectInputStream in) throws Exception {
 119                     Object obj = in.readUnshared();
 120                     if (obj != Foo.foo) {
 121                         throw new Error(
 122                             "expected " + Foo.foo + " instead of " + obj);
 123                     }
 124                     obj = in.readUnshared();
 125                     if (obj != Foo.foo) {
 126                         throw new Error(
 127                             "expected " + Foo.foo + " instead of " + obj);
 128                     }
 129                 }
 130             }
 131         };
 132         for (int i = 0; i < tests.length; i++) {
 133             tests[i].run();
 134         }
 135     }
 136 }