test/jdk/java/io/Serializable/classDescHooks/ClassDescHooks.java
author rriggs
Fri, 11 Oct 2019 13:11:56 -0400
changeset 58565 baa5969ecf34
parent 47216 71c04702a3d5
permissions -rw-r--r--
8231427: Warning cleanup in tests of java.io.Serializable Reviewed-by: darcy, lancea
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
58565
baa5969ecf34 8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents: 47216
diff changeset
     2
 * Copyright (c) 1999, 2019, 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
 * @bug 4227189
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @summary Ensure that class descriptor read, write hooks exist, are backwards
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 *          compatible, and work as advertised.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
class Foo implements Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
    private static final long serialVersionUID = 1L;
58565
baa5969ecf34 8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents: 47216
diff changeset
    35
    Short s = (short) 1;
baa5969ecf34 8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents: 47216
diff changeset
    36
    Integer i = 2;
baa5969ecf34 8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents: 47216
diff changeset
    37
    Long l = 3L;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
        if (obj instanceof Foo) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
            Foo ofoo = (Foo) obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
            return s.equals(ofoo.s) && i.equals(ofoo.i) && l.equals(ofoo.l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    }
58565
baa5969ecf34 8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents: 47216
diff changeset
    46
baa5969ecf34 8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents: 47216
diff changeset
    47
    public int hashCode() {
baa5969ecf34 8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents: 47216
diff changeset
    48
        return i;
baa5969ecf34 8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents: 47216
diff changeset
    49
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
class CustomOutputStream extends ObjectOutputStream {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    boolean hookCalled = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    CustomOutputStream(OutputStream out) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        super(out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        useProtocolVersion(PROTOCOL_VERSION_2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    protected void writeClassDescriptor(ObjectStreamClass desc)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        writeUTF(desc.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        hookCalled = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
class CustomInputStream extends ObjectInputStream {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    boolean hookCalled = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    CustomInputStream(InputStream in) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        super(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    protected ObjectStreamClass readClassDescriptor()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        throws IOException, ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        hookCalled = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        return ObjectStreamClass.lookup(Class.forName(readUTF()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
public class ClassDescHooks implements ObjectStreamConstants {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        ByteArrayOutputStream bout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        ByteArrayInputStream bin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        ObjectOutputStream oout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        ObjectInputStream oin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        FileInputStream fin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        File foof;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        CustomOutputStream cout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        CustomInputStream cin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        // test for backwards compatibility
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        bout = new ByteArrayOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        foof = new File(System.getProperty("test.src", "."), "Foo.ser");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        fin = new FileInputStream(foof);
5809
6e38efd0293f 6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents: 5506
diff changeset
   100
        try {
6e38efd0293f 6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents: 5506
diff changeset
   101
            while (fin.available() > 0)
6e38efd0293f 6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents: 5506
diff changeset
   102
                bout.write(fin.read());
6e38efd0293f 6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents: 5506
diff changeset
   103
        } finally {
6e38efd0293f 6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents: 5506
diff changeset
   104
            fin.close();
6e38efd0293f 6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents: 5506
diff changeset
   105
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        byte[] buf1 = bout.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        bout = new ByteArrayOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        oout = new ObjectOutputStream(bout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        Foo foo = new Foo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        oout.writeObject(foo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        oout.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        byte[] buf2 = bout.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        if (! Arrays.equals(buf1, buf2))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            throw new Error("Incompatible stream format (write)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
5809
6e38efd0293f 6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents: 5506
diff changeset
   118
        Foo foocopy;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        fin = new FileInputStream(foof);
5809
6e38efd0293f 6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents: 5506
diff changeset
   120
        try {
6e38efd0293f 6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents: 5506
diff changeset
   121
            oin = new ObjectInputStream(fin);
6e38efd0293f 6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents: 5506
diff changeset
   122
            foocopy = (Foo) oin.readObject();
6e38efd0293f 6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents: 5506
diff changeset
   123
            if (! foo.equals(foocopy))
6e38efd0293f 6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents: 5506
diff changeset
   124
                throw new Error("Incompatible stream format (read)");
6e38efd0293f 6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents: 5506
diff changeset
   125
        } finally {
6e38efd0293f 6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents: 5506
diff changeset
   126
            fin.close();
6e38efd0293f 6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents: 5506
diff changeset
   127
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        // make sure write hook not called when old protocol in use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        bout = new ByteArrayOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        cout = new CustomOutputStream(bout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        cout.useProtocolVersion(PROTOCOL_VERSION_1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        cout.writeObject(foo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        if (cout.hookCalled)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            throw new Error("write descriptor hook should not be called");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        // write custom class descriptor representations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        bout = new ByteArrayOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        cout = new CustomOutputStream(bout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        cout.writeObject(foo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        cout.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        bin = new ByteArrayInputStream(bout.toByteArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        cin = new CustomInputStream(bin);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        foocopy = (Foo) cin.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        if (! cout.hookCalled)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            throw new Error("write descriptor hook never called");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        if (! cin.hookCalled)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            throw new Error("read descriptor hook never called");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        if (! foo.equals(foocopy))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            throw new Error("serialization failed when hooks active");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
}