1 /*
   2  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. 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 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 /* @test
  25  * @summary it is a new version of an old test which was
  26  *          /src/share/test/serialization/piotest.java
  27  *          Test of serialization/deserialization of
  28  *          objects with Arrays types
  29  *
  30  * @build ArrayOpsTest PrimitivesTest
  31  * @run main SimpleArrays
  32  */
  33 
  34 import java.io.*;
  35 
  36 
  37 public class SimpleArrays {
  38     public static void main (String argv[]) throws IOException {
  39        System.err.println("\nRegression test for testing of " +
  40            "serialization/deserialization of objects with Arrays types\n");
  41 
  42        FileInputStream istream = null;
  43        FileOutputStream ostream = null;
  44        try {
  45            ostream = new FileOutputStream("piotest2.tmp");
  46            ObjectOutputStream p = new ObjectOutputStream(ostream);
  47 
  48             byte b[] = { 0, 1};
  49             p.writeObject((Object)b);
  50 
  51             short s[] = { 0, 1, 2};
  52             p.writeObject((Object)s);
  53 
  54             char c[] = { 'A', 'B', 'C', 'D'};
  55             p.writeObject((Object)c);
  56 
  57             int i[] = { 0, 1, 2, 3, 4};
  58             p.writeObject((Object)i);
  59 
  60             long l[] = { 0, 1, 2, 3, 4, 5};
  61             p.writeObject((Object)l);
  62 
  63             boolean z[] = new boolean[4];
  64             z[0] = true;
  65             z[1] = false;
  66             z[2] = true;
  67             z[3] = false;
  68             p.writeObject(z);
  69 
  70             float f[] = { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f};
  71             p.writeObject((Object)f);
  72 
  73             double d[] = { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0d};
  74             p.writeObject((Object)d);
  75 
  76             String string[] = { "A", "B", "C", "D"};
  77             p.writeObject((Object) string);
  78 
  79             PrimitivesTest prim[] = new PrimitivesTest[5];
  80             prim[0] = new PrimitivesTest();
  81             prim[1] = prim[0];
  82             prim[2] = new PrimitivesTest();
  83             prim[3] = prim[2];
  84             prim[4] = null;
  85             p.writeObject((Object)prim);
  86 
  87             p.flush();
  88 
  89             /* now read them back */
  90             istream = new FileInputStream("piotest2.tmp");
  91             ObjectInputStream q = new ObjectInputStream(istream);
  92 
  93             Object obj;
  94 
  95             byte b_u[] = (byte[])q.readObject();
  96             short s_u[] = (short[])q.readObject();
  97             char c_u[] = (char[])q.readObject();
  98             int i_u[] = (int[])q.readObject();
  99             long l_u[] = (long[])q.readObject();
 100 
 101             /* This should be boolean, but they were serialized as bytes
 102              */
 103             boolean z_u[] = null;
 104             Object z_obj = null;
 105             try {
 106                 z_obj = q.readObject();
 107                 z_u = (boolean[])z_obj;
 108             } catch (ClassCastException e) {
 109                 System.err.println("\nClassCastException " + e.getMessage());
 110                 System.err.println("\nBoolean array read as " +
 111                     z_obj.getClass().getName());
 112                 System.err.println("\nAn Exception occurred. " +
 113                     e.getMessage());
 114                 z_u = z;
 115                 throw new Error();
 116             }
 117 
 118             float f_u[] = (float[])q.readObject();
 119             double d_u[] = (double[])q.readObject();
 120             String string_u[] = (String[])q.readObject();
 121 
 122             PrimitivesTest prim_u[] = (PrimitivesTest[])q.readObject();
 123 
 124             if (!ArrayOpsTest.verify(i, i_u)) {
 125                 System.err.println("\nUnpickling of int array failed");
 126                 throw new Error();
 127             }
 128             if (!ArrayOpsTest.verify(b, b_u)) {
 129                 System.err.println("\nUnpickling of byte array failed");
 130                 throw new Error();
 131             }
 132             if (!ArrayOpsTest.verify(s, s_u)) {
 133                 System.err.println("\nUnpickling of short array failed");
 134                 throw new Error();
 135             }
 136             if (!ArrayOpsTest.verify(c, c_u)) {
 137                 System.err.println("\nUnpickling of char array failed");
 138                 throw new Error();
 139             }
 140             if (!ArrayOpsTest.verify(l, l_u)) {
 141                 System.err.println("\nUnpickling of long array failed");
 142                 throw new Error();
 143             }
 144             if (!ArrayOpsTest.verify(f, f_u)) {
 145                 System.err.println("\nUnpickling of float array failed");
 146                 throw new Error();
 147             }
 148             if (!ArrayOpsTest.verify(d, d_u)) {
 149                 System.err.println("\nUnpickling of double array failed");
 150                 throw new Error();
 151             }
 152             if (!ArrayOpsTest.verify(z, z_u)) {
 153                 System.err.println("\nUnpickling of boolean array failed");
 154                 throw new Error();
 155             }
 156             if (!ArrayOpsTest.verify(string, string_u)) {
 157                 System.err.println("\nUnpickling of String array failed");
 158                 throw new Error();
 159             }
 160             if (!ArrayOpsTest.verify(prim, prim_u)) {
 161                 System.err.println("\nUnpickling of PrimitivesTest array " +
 162                     "failed");
 163                 throw new Error();
 164             }
 165             System.err.println("\nTEST PASSED");
 166        } catch (Exception e) {
 167            System.err.print("TEST FAILED: ");
 168            e.printStackTrace();
 169 
 170            System.err.println("\nInput remaining");
 171            int ch;
 172            try {
 173                while ((ch = istream.read()) != -1) {
 174                    System.err.print("\n " + Integer.toString(ch, 16) + " ");
 175                }
 176                System.err.println("\n ");
 177            } catch (Exception f) {
 178                throw new Error();
 179            }
 180            throw new Error();
 181        } finally {
 182            if (istream != null) istream.close();
 183            if (ostream != null) ostream.close();
 184        }
 185     }
 186 }