test/jdk/java/io/Serializable/subclass/SubclassTest.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) 1998, 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
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 4100915
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Verify that [write/read]ObjectOverride methods get called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *          Test verifies that ALL methods to write an object can
45295
c2764290902a 8166142: Refactor java.io.serialization shell tests to java
mli
parents: 32649
diff changeset
    29
 *          be overridden. However, the testing for reading an object
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 *          is incomplete. Only test that readObjectOverride is called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 *          An entire protocol would need to be implemented and written
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 *          out before being able to test the input side of the API.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 *          Also, would be appropriate that this program verify
45295
c2764290902a 8166142: Refactor java.io.serialization shell tests to java
mli
parents: 32649
diff changeset
    35
 *          that if SerializablePermission "enableSubclassImplementation"
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *          is not in the security policy and security is enabled, that
45295
c2764290902a 8166142: Refactor java.io.serialization shell tests to java
mli
parents: 32649
diff changeset
    37
 *          a security exception is thrown when constructing the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *          ObjectOutputStream subclass.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *
45295
c2764290902a 8166142: Refactor java.io.serialization shell tests to java
mli
parents: 32649
diff changeset
    41
 * @compile AbstractObjectInputStream.java AbstractObjectOutputStream.java
c2764290902a 8166142: Refactor java.io.serialization shell tests to java
mli
parents: 32649
diff changeset
    42
 * @compile XObjectInputStream.java XObjectOutputStream.java
c2764290902a 8166142: Refactor java.io.serialization shell tests to java
mli
parents: 32649
diff changeset
    43
 * @compile SubclassTest.java
c2764290902a 8166142: Refactor java.io.serialization shell tests to java
mli
parents: 32649
diff changeset
    44
 * @run main SubclassTest
c2764290902a 8166142: Refactor java.io.serialization shell tests to java
mli
parents: 32649
diff changeset
    45
 * @run main/othervm/policy=Allow.policy SubclassTest -expectSecurityException
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
45295
c2764290902a 8166142: Refactor java.io.serialization shell tests to java
mli
parents: 32649
diff changeset
    48
import java.io.ByteArrayInputStream;
c2764290902a 8166142: Refactor java.io.serialization shell tests to java
mli
parents: 32649
diff changeset
    49
import java.io.ByteArrayOutputStream;
c2764290902a 8166142: Refactor java.io.serialization shell tests to java
mli
parents: 32649
diff changeset
    50
import java.io.IOException;
c2764290902a 8166142: Refactor java.io.serialization shell tests to java
mli
parents: 32649
diff changeset
    51
import java.io.ObjectInputStream;
c2764290902a 8166142: Refactor java.io.serialization shell tests to java
mli
parents: 32649
diff changeset
    52
import java.io.ObjectOutputStream;
c2764290902a 8166142: Refactor java.io.serialization shell tests to java
mli
parents: 32649
diff changeset
    53
import java.io.ObjectStreamField;
c2764290902a 8166142: Refactor java.io.serialization shell tests to java
mli
parents: 32649
diff changeset
    54
import java.io.Serializable;
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
 * Test if customized readObject and writeObject are called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
class B implements Serializable {
58565
baa5969ecf34 8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents: 47216
diff changeset
    60
    private static final long serialVersionUID = 1L;
baa5969ecf34 8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents: 47216
diff changeset
    61
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    public int publicIntField;
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 5506
diff changeset
    63
    public static int numWriteObjectCalled = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    B(int v) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        publicIntField = v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    private void writeObject(ObjectOutputStream os) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        numWriteObjectCalled++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        os.defaultWriteObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    private void readObject(ObjectInputStream is)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        throws IOException, ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        is.defaultReadObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
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
 * Test PutFields interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
class C implements Serializable {
58565
baa5969ecf34 8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents: 47216
diff changeset
    85
    private static final long serialVersionUID = 1L;
baa5969ecf34 8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents: 47216
diff changeset
    86
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    public int xx1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    public int xx2;
58565
baa5969ecf34 8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents: 47216
diff changeset
    89
    private static final ObjectStreamField[] serialPersistentFields = {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        new ObjectStreamField("x1", Integer.TYPE),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        new ObjectStreamField("x2", Integer.TYPE),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        new ObjectStreamField("x3", Integer.TYPE),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        new ObjectStreamField("x4", Integer.TYPE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    C() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        xx1 = 300;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        xx2 = 400;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    private void writeObject(ObjectOutputStream os) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        ObjectOutputStream.PutField putFields = os.putFields();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        putFields.put("x1", xx1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        putFields.put("x2", xx2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        putFields.put("x3", xx1 * 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        putFields.put("x4", xx2 * 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        os.writeFields();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
class A implements Serializable {
58565
baa5969ecf34 8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents: 47216
diff changeset
   113
    private static final long serialVersionUID = 1L;
baa5969ecf34 8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents: 47216
diff changeset
   114
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    public int  publicIntField;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    public long publicLongField;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    public B    publicBField;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    public B[]  publicBArray = { new B(4), new B(6)};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    public C    publicCField;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    public A() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        publicIntField = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        publicLongField = 10L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        publicBField = new B(5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        publicCField = new C();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
45295
c2764290902a 8166142: Refactor java.io.serialization shell tests to java
mli
parents: 32649
diff changeset
   129
public class SubclassTest {
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 5506
diff changeset
   130
    public static void main(String argv[])
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        throws IOException, ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        boolean expectSecurityException = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        if (argv.length > 0 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            argv[0].compareTo("-expectSecurityException") == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            expectSecurityException = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        ByteArrayOutputStream baos = new ByteArrayOutputStream(20);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        XObjectOutputStream os = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            os = new XObjectOutputStream(baos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            if (expectSecurityException)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                throw new Error("Assertion failure. " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                                "Expected a security exception on previous line.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        } catch (SecurityException e) {
45295
c2764290902a 8166142: Refactor java.io.serialization shell tests to java
mli
parents: 32649
diff changeset
   147
            if (expectSecurityException) {
c2764290902a 8166142: Refactor java.io.serialization shell tests to java
mli
parents: 32649
diff changeset
   148
                System.err.println("Caught expected security exception.");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                return;
45295
c2764290902a 8166142: Refactor java.io.serialization shell tests to java
mli
parents: 32649
diff changeset
   150
            }
c2764290902a 8166142: Refactor java.io.serialization shell tests to java
mli
parents: 32649
diff changeset
   151
            throw e;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        os.writeObject(new A());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        os.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        if (B.numWriteObjectCalled != 3)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            throw new Error("Expected B.writeObject() to be called 3 times;" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                            " observed only " + B.numWriteObjectCalled + " times");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        XObjectInputStream is =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            new XObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            A a = (A)is.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            throw new Error("Expected readObjectOverride() to be called and throw IOException(not implemented)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        is.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
};