Path: blob/master/test/jdk/java/io/Serializable/oldTests/SimpleArrays.java
41153 views
/*1* Copyright (c) 2005, 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* @summary it is a new version of an old test which was25* /src/share/test/serialization/piotest.java26* Test of serialization/deserialization of27* objects with Arrays types28*29* @build ArrayOpsTest PrimitivesTest30* @run main SimpleArrays31*/3233import java.io.*;343536public class SimpleArrays {37public static void main (String argv[]) throws IOException {38System.err.println("\nRegression test for testing of " +39"serialization/deserialization of objects with Arrays types\n");4041FileInputStream istream = null;42FileOutputStream ostream = null;43try {44ostream = new FileOutputStream("piotest2.tmp");45ObjectOutputStream p = new ObjectOutputStream(ostream);4647byte b[] = { 0, 1};48p.writeObject((Object)b);4950short s[] = { 0, 1, 2};51p.writeObject((Object)s);5253char c[] = { 'A', 'B', 'C', 'D'};54p.writeObject((Object)c);5556int i[] = { 0, 1, 2, 3, 4};57p.writeObject((Object)i);5859long l[] = { 0, 1, 2, 3, 4, 5};60p.writeObject((Object)l);6162boolean z[] = new boolean[4];63z[0] = true;64z[1] = false;65z[2] = true;66z[3] = false;67p.writeObject(z);6869float f[] = { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f};70p.writeObject((Object)f);7172double d[] = { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0d};73p.writeObject((Object)d);7475String string[] = { "A", "B", "C", "D"};76p.writeObject((Object) string);7778PrimitivesTest prim[] = new PrimitivesTest[5];79prim[0] = new PrimitivesTest();80prim[1] = prim[0];81prim[2] = new PrimitivesTest();82prim[3] = prim[2];83prim[4] = null;84p.writeObject((Object)prim);8586p.flush();8788/* now read them back */89istream = new FileInputStream("piotest2.tmp");90ObjectInputStream q = new ObjectInputStream(istream);9192Object obj;9394byte b_u[] = (byte[])q.readObject();95short s_u[] = (short[])q.readObject();96char c_u[] = (char[])q.readObject();97int i_u[] = (int[])q.readObject();98long l_u[] = (long[])q.readObject();99100/* This should be boolean, but they were serialized as bytes101*/102boolean z_u[] = null;103Object z_obj = null;104try {105z_obj = q.readObject();106z_u = (boolean[])z_obj;107} catch (ClassCastException e) {108System.err.println("\nClassCastException " + e.getMessage());109System.err.println("\nBoolean array read as " +110z_obj.getClass().getName());111System.err.println("\nAn Exception occurred. " +112e.getMessage());113z_u = z;114throw new Error();115}116117float f_u[] = (float[])q.readObject();118double d_u[] = (double[])q.readObject();119String string_u[] = (String[])q.readObject();120121PrimitivesTest prim_u[] = (PrimitivesTest[])q.readObject();122123if (!ArrayOpsTest.verify(i, i_u)) {124System.err.println("\nUnpickling of int array failed");125throw new Error();126}127if (!ArrayOpsTest.verify(b, b_u)) {128System.err.println("\nUnpickling of byte array failed");129throw new Error();130}131if (!ArrayOpsTest.verify(s, s_u)) {132System.err.println("\nUnpickling of short array failed");133throw new Error();134}135if (!ArrayOpsTest.verify(c, c_u)) {136System.err.println("\nUnpickling of char array failed");137throw new Error();138}139if (!ArrayOpsTest.verify(l, l_u)) {140System.err.println("\nUnpickling of long array failed");141throw new Error();142}143if (!ArrayOpsTest.verify(f, f_u)) {144System.err.println("\nUnpickling of float array failed");145throw new Error();146}147if (!ArrayOpsTest.verify(d, d_u)) {148System.err.println("\nUnpickling of double array failed");149throw new Error();150}151if (!ArrayOpsTest.verify(z, z_u)) {152System.err.println("\nUnpickling of boolean array failed");153throw new Error();154}155if (!ArrayOpsTest.verify(string, string_u)) {156System.err.println("\nUnpickling of String array failed");157throw new Error();158}159if (!ArrayOpsTest.verify(prim, prim_u)) {160System.err.println("\nUnpickling of PrimitivesTest array " +161"failed");162throw new Error();163}164System.err.println("\nTEST PASSED");165} catch (Exception e) {166System.err.print("TEST FAILED: ");167e.printStackTrace();168169System.err.println("\nInput remaining");170int ch;171try {172while ((ch = istream.read()) != -1) {173System.err.print("\n " + Integer.toString(ch, 16) + " ");174}175System.err.println("\n ");176} catch (Exception f) {177throw new Error();178}179throw new Error();180} finally {181if (istream != null) istream.close();182if (ostream != null) ostream.close();183}184}185}186187188