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