jdk/test/java/io/Serializable/oldTests/WritePrimitive.java
author ohair
Tue, 28 Dec 2010 15:53:50 -0800
changeset 7668 d4a77089c587
parent 5809 6e38efd0293f
permissions -rw-r--r--
6962318: Update copyright year Reviewed-by: xdono
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
7668
d4a77089c587 6962318: Update copyright year
ohair
parents: 5809
diff changeset
     2
 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/* @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @summary it is a new version of an old test which was
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 *          /src/share/test/serialization/piotest.java
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 *          Test of serialization/deserialization of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *          primitives
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * @build PrimitivesTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * @run main WritePrimitive
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
public class WritePrimitive {
5809
6e38efd0293f 6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents: 5506
diff changeset
    37
    public static void main (String argv[]) throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
        System.err.println("\nRegression test for testing of " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
            "serialization/deserialization of primitives \n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
        FileInputStream istream = null;
5809
6e38efd0293f 6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents: 5506
diff changeset
    42
        FileOutputStream ostream = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
            int i = 123456;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
            byte b = 12;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
            short s = 45;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
            char c = 'A';
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
            long l = 1234567890000L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
            float f = 3.14159f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
            double d = f*2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
            boolean z = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
            String string = "The String";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
            PrimitivesTest prim = new PrimitivesTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
5809
6e38efd0293f 6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents: 5506
diff changeset
    55
            ostream = new FileOutputStream("piotest1.tmp");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            ObjectOutputStream p = new ObjectOutputStream(ostream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            p.writeInt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            p.writeByte(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            p.writeShort(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            p.writeChar(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            p.writeLong(l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            p.writeFloat(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            p.writeDouble(d);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            p.writeBoolean(z);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            p.writeUTF(string);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            p.writeObject(string);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            p.writeObject(prim);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            p.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            istream = new FileInputStream("piotest1.tmp");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            ObjectInputStream q = new ObjectInputStream(istream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            int i_u = q.readInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            byte b_u = q.readByte();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            short s_u = q.readShort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            char c_u = q.readChar();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            long l_u = q.readLong();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            float f_u = q.readFloat();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            double d_u = q.readDouble();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            boolean z_u = q.readBoolean();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            String string_utf = q.readUTF();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            String string_u = (String)q.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            if (i != i_u) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                System.err.println("\nint:  expected " + i + " actual " +i_u);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                throw new Error();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            if (b != b_u) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                System.err.println("\nbyte:  expected " + b + " actual " +b_u);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                throw new Error();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            if (s != s_u) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                System.err.println("\nshort:  expected " + s + " actual " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                                   s_u);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                throw new Error();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            if (c != c_u) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                System.err.println("\nchar:  expected " + c + " actual " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                                   c_u);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                throw new Error();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            if (l != l_u) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                System.err.println("\nlong:  expected " + l + " actual " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                                   l_u);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                throw new Error();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            if (f != f_u) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                System.err.println("\nfloat:  expected " + f + " actual " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                                   f_u);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                throw new Error();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            if (d != d_u) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                System.err.println("\ndouble:  expected " + d + " actual " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                                   d_u);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                throw new Error();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            if (z != z_u) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                System.err.println("\nboolean:  expected " + z + " actual " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                                   z_u);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                throw new Error();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            if (!string.equals(string_utf)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                System.err.println("\nString:  expected " + string +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                                   " actual " + string_utf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                throw new Error();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            if (!string.equals(string_u)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                System.err.println("\nString:  expected " + string +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                                   " actual " + string_u);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                throw new Error();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            PrimitivesTest prim_u = (PrimitivesTest)q.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            if (!prim.equals(prim_u)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                System.err.println("\nTEST FAILED: Read primitive object " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                    "correctly = " + false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                System.err.println("\n " + prim);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                System.err.println("\n " + prim_u);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                throw new Error();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            System.err.println("\nTEST PASSED");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            System.err.print("TEST FAILED: ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            System.err.println("\nInput remaining");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            int ch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                while ((ch = istream.read()) != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                    System.err.print("\n " + Integer.toString(ch, 16)+ " ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                System.out.println("\n ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            } catch (Exception f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                throw new Error();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            throw new Error();
5809
6e38efd0293f 6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents: 5506
diff changeset
   158
        } finally {
6e38efd0293f 6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents: 5506
diff changeset
   159
            if (istream != null) istream.close();
6e38efd0293f 6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents: 5506
diff changeset
   160
            if (ostream != null) ostream.close();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
}