test/jdk/java/io/Serializable/oldTests/AnnotateClass.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) 2005, 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
 * @summary it is new version of old test which was
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 *          /src/share/test/serialization/subtest.java
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 *          This test verifies of invocation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *          annotateClass/replaceObject methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
public class AnnotateClass {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
    public static void main (String argv[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
        System.err.println("\nRegression test for verification " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
                           "of invocation annotateClass/replaceObject " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
                           "methods \n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
            FileOutputStream ostream = new FileOutputStream("subtest1.tmp");
5809
6e38efd0293f 6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents: 5506
diff changeset
    40
            try {
6e38efd0293f 6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents: 5506
diff changeset
    41
                TestOutputStream p = new TestOutputStream(ostream);
6e38efd0293f 6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents: 5506
diff changeset
    42
                p.writeObject(System.out);
6e38efd0293f 6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents: 5506
diff changeset
    43
                p.writeObject(System.err);
6e38efd0293f 6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents: 5506
diff changeset
    44
                p.writeObject(new PrintStream(ostream));
6e38efd0293f 6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents: 5506
diff changeset
    45
                p.flush();
6e38efd0293f 6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents: 5506
diff changeset
    46
            } finally {
6e38efd0293f 6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents: 5506
diff changeset
    47
                ostream.close();
6e38efd0293f 6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents: 5506
diff changeset
    48
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
            FileInputStream istream = new FileInputStream("subtest1.tmp");
5809
6e38efd0293f 6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents: 5506
diff changeset
    51
            try {
6e38efd0293f 6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents: 5506
diff changeset
    52
                TestInputStream q = new TestInputStream(istream);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
5809
6e38efd0293f 6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents: 5506
diff changeset
    54
                PrintStream out = (PrintStream)q.readObject();
6e38efd0293f 6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents: 5506
diff changeset
    55
                PrintStream err = (PrintStream)q.readObject();
6e38efd0293f 6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents: 5506
diff changeset
    56
                Object other = q.readObject();
6e38efd0293f 6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents: 5506
diff changeset
    57
                if (out != System.out) {
6e38efd0293f 6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents: 5506
diff changeset
    58
                    System.err.println(
6e38efd0293f 6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents: 5506
diff changeset
    59
                        "\nTEST FAILED: System.out not read correctly");
6e38efd0293f 6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents: 5506
diff changeset
    60
                    throw new Error();
6e38efd0293f 6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents: 5506
diff changeset
    61
                }
6e38efd0293f 6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents: 5506
diff changeset
    62
                if (err != System.err) {
6e38efd0293f 6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents: 5506
diff changeset
    63
                    System.err.println(
6e38efd0293f 6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents: 5506
diff changeset
    64
                        "\nTEST FAILED: System.err not read correctly");
6e38efd0293f 6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents: 5506
diff changeset
    65
                    throw new Error();
6e38efd0293f 6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents: 5506
diff changeset
    66
                }
6e38efd0293f 6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents: 5506
diff changeset
    67
                if (other != null) {
6e38efd0293f 6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents: 5506
diff changeset
    68
                    System.err.println(
6e38efd0293f 6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents: 5506
diff changeset
    69
                        "\nTEST FAILED: Non-system PrintStream should have " +
6e38efd0293f 6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents: 5506
diff changeset
    70
                        "been written/read as null");
6e38efd0293f 6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents: 5506
diff changeset
    71
                    throw new Error();
6e38efd0293f 6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents: 5506
diff changeset
    72
                }
6e38efd0293f 6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents: 5506
diff changeset
    73
            } finally {
6e38efd0293f 6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents: 5506
diff changeset
    74
                istream.close();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            }
5809
6e38efd0293f 6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open
alanb
parents: 5506
diff changeset
    76
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            System.err.println("\nTEST PASSED");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            System.err.print("TEST FAILED: ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            throw new Error();
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
/** ObjectOutputStream is extended to test the annotateClass()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * and replaceObject() subclassable methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * In annotateClass a magic string is written to the stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * so that it can be verified in ObjectInputStream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * replaceObject is used to subsititute a handle object for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * one of the standard PrintStreams (stdout or stderr).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
class TestOutputStream extends ObjectOutputStream {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    /* Construct a new test stream */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    TestOutputStream(OutputStream out)  throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        super(out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        enableReplaceObject(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    /* When any class is written, add a "magic" string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * that must be verified by the TestInputStream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     */
58565
baa5969ecf34 8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents: 47216
diff changeset
   105
    protected void annotateClass(Class<?> cl) throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        this.writeUTF("magic");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    /* For each object of type PrintStream, substitute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * a StdStream handle object that encodes which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * of the standard print streams is being written.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * Other objects are written as themselves.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    protected Object replaceObject(Object obj)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        /* For PrintStreams, like stdout and stderr, encode */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        if (obj instanceof PrintStream) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            return new StdStream((PrintStream)obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        return obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
/** Reverse the effects of TestOutputStream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
class TestInputStream extends ObjectInputStream {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    TestInputStream(InputStream in)  throws IOException  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        super(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        enableResolveObject(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    /** Verify that the magic string was written to the stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * Also use the default classname->class resolution.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    protected Class<?> resolveClass(ObjectStreamClass classdesc)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        throws ClassNotFoundException, IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            String s = readUTF();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            if (!(s.equals("magic"))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                System.err.println(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                    "\nTEST FAILED: Bad magic number");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                throw new Error();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        } catch (IOException ee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            System.err.println(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                "\nTEST FAILED: I/O Exception");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            throw new Error();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        return super.resolveClass(classdesc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    /** If the object in the stream is a StdStream,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * get the mapping of it to the local System printstream and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * return it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * Other objects are returned as themselves.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    protected Object resolveObject(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        if (obj instanceof StdStream) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            return ((StdStream)obj).getStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        return obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
/* A holder class to map between standard print streams (stdout, stderr)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
 * and a small integer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
class StdStream implements java.io.Serializable {
58565
baa5969ecf34 8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents: 47216
diff changeset
   171
    private static final long serialVersionUID = 1L;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    private int stream = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    public StdStream(PrintStream s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        if (s == System.out) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            stream = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        } else if (s == System.err) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            stream = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    public PrintStream getStream() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        if (stream == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            return System.out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        } else if (stream == 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            return System.err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
}