Path: blob/master/test/jdk/java/io/Serializable/oldTests/WritePrimitive.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* primitives28*29* @build PrimitivesTest30* @run main WritePrimitive31*/3233import java.io.*;3435public class WritePrimitive {36public static void main (String argv[]) throws IOException {37System.err.println("\nRegression test for testing of " +38"serialization/deserialization of primitives \n");3940FileInputStream istream = null;41FileOutputStream ostream = null;42try {43int i = 123456;44byte b = 12;45short s = 45;46char c = 'A';47long l = 1234567890000L;48float f = 3.14159f;49double d = f*2;50boolean z = true;51String string = "The String";52PrimitivesTest prim = new PrimitivesTest();5354ostream = new FileOutputStream("piotest1.tmp");55ObjectOutputStream p = new ObjectOutputStream(ostream);5657p.writeInt(i);58p.writeByte(b);59p.writeShort(s);60p.writeChar(c);61p.writeLong(l);62p.writeFloat(f);63p.writeDouble(d);64p.writeBoolean(z);65p.writeUTF(string);66p.writeObject(string);6768p.writeObject(prim);69p.flush();7071istream = new FileInputStream("piotest1.tmp");72ObjectInputStream q = new ObjectInputStream(istream);7374int i_u = q.readInt();75byte b_u = q.readByte();76short s_u = q.readShort();77char c_u = q.readChar();78long l_u = q.readLong();79float f_u = q.readFloat();80double d_u = q.readDouble();81boolean z_u = q.readBoolean();82String string_utf = q.readUTF();83String string_u = (String)q.readObject();84if (i != i_u) {85System.err.println("\nint: expected " + i + " actual " +i_u);86throw new Error();87}88if (b != b_u) {89System.err.println("\nbyte: expected " + b + " actual " +b_u);90throw new Error();91}92if (s != s_u) {93System.err.println("\nshort: expected " + s + " actual " +94s_u);95throw new Error();96}97if (c != c_u) {98System.err.println("\nchar: expected " + c + " actual " +99c_u);100throw new Error();101}102if (l != l_u) {103System.err.println("\nlong: expected " + l + " actual " +104l_u);105throw new Error();106}107if (f != f_u) {108System.err.println("\nfloat: expected " + f + " actual " +109f_u);110throw new Error();111}112if (d != d_u) {113System.err.println("\ndouble: expected " + d + " actual " +114d_u);115throw new Error();116}117if (z != z_u) {118System.err.println("\nboolean: expected " + z + " actual " +119z_u);120throw new Error();121}122if (!string.equals(string_utf)) {123System.err.println("\nString: expected " + string +124" actual " + string_utf);125throw new Error();126}127if (!string.equals(string_u)) {128System.err.println("\nString: expected " + string +129" actual " + string_u);130throw new Error();131}132133PrimitivesTest prim_u = (PrimitivesTest)q.readObject();134if (!prim.equals(prim_u)) {135System.err.println("\nTEST FAILED: Read primitive object " +136"correctly = " + false);137System.err.println("\n " + prim);138System.err.println("\n " + prim_u);139throw new Error();140}141System.err.println("\nTEST PASSED");142} catch (Exception e) {143System.err.print("TEST FAILED: ");144e.printStackTrace();145146System.err.println("\nInput remaining");147int ch;148try {149while ((ch = istream.read()) != -1) {150System.err.print("\n " + Integer.toString(ch, 16)+ " ");151}152System.out.println("\n ");153} catch (Exception f) {154throw new Error();155}156throw new Error();157} finally {158if (istream != null) istream.close();159if (ostream != null) ostream.close();160}161}162}163164165