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