jdk/test/java/io/Serializable/classDescHooks/ClassDescHooks.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) 1999, 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
 * @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;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
    Short s = new Short((short) 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
    Integer i = new Integer(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    Long l = new Long(3);
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
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
class CustomOutputStream extends ObjectOutputStream {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    boolean hookCalled = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    CustomOutputStream(OutputStream out) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        super(out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        useProtocolVersion(PROTOCOL_VERSION_2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    protected void writeClassDescriptor(ObjectStreamClass desc)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        writeUTF(desc.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        hookCalled = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
class CustomInputStream extends ObjectInputStream {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    boolean hookCalled = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    CustomInputStream(InputStream in) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        super(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    protected ObjectStreamClass readClassDescriptor()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        throws IOException, ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        hookCalled = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        return ObjectStreamClass.lookup(Class.forName(readUTF()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
public class ClassDescHooks implements ObjectStreamConstants {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        ByteArrayOutputStream bout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        ByteArrayInputStream bin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        ObjectOutputStream oout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        ObjectInputStream oin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        FileInputStream fin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        File foof;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        CustomOutputStream cout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        CustomInputStream cin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        // test for backwards compatibility
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        bout = new ByteArrayOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        foof = new File(System.getProperty("test.src", "."), "Foo.ser");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        fin = new FileInputStream(foof);
5809
6e38efd0293f 6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents: 5506
diff changeset
    96
        try {
6e38efd0293f 6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents: 5506
diff changeset
    97
            while (fin.available() > 0)
6e38efd0293f 6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents: 5506
diff changeset
    98
                bout.write(fin.read());
6e38efd0293f 6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents: 5506
diff changeset
    99
        } finally {
6e38efd0293f 6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents: 5506
diff changeset
   100
            fin.close();
6e38efd0293f 6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents: 5506
diff changeset
   101
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        byte[] buf1 = bout.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        bout = new ByteArrayOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        oout = new ObjectOutputStream(bout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        Foo foo = new Foo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        oout.writeObject(foo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        oout.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        byte[] buf2 = bout.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        if (! Arrays.equals(buf1, buf2))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            throw new Error("Incompatible stream format (write)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
5809
6e38efd0293f 6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents: 5506
diff changeset
   114
        Foo foocopy;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        fin = new FileInputStream(foof);
5809
6e38efd0293f 6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents: 5506
diff changeset
   116
        try {
6e38efd0293f 6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents: 5506
diff changeset
   117
            oin = new ObjectInputStream(fin);
6e38efd0293f 6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents: 5506
diff changeset
   118
            foocopy = (Foo) oin.readObject();
6e38efd0293f 6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents: 5506
diff changeset
   119
            if (! foo.equals(foocopy))
6e38efd0293f 6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents: 5506
diff changeset
   120
                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
   121
        } finally {
6e38efd0293f 6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents: 5506
diff changeset
   122
            fin.close();
6e38efd0293f 6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents: 5506
diff changeset
   123
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        // make sure write hook not called when old protocol in use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        bout = new ByteArrayOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        cout = new CustomOutputStream(bout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        cout.useProtocolVersion(PROTOCOL_VERSION_1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        cout.writeObject(foo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        if (cout.hookCalled)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            throw new Error("write descriptor hook should not be called");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        // write custom class descriptor representations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        bout = new ByteArrayOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        cout = new CustomOutputStream(bout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        cout.writeObject(foo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        cout.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        bin = new ByteArrayInputStream(bout.toByteArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        cin = new CustomInputStream(bin);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        foocopy = (Foo) cin.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        if (! cout.hookCalled)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            throw new Error("write descriptor hook never called");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        if (! cin.hookCalled)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            throw new Error("read descriptor hook never called");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        if (! foo.equals(foocopy))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            throw new Error("serialization failed when hooks active");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
}