jdk/test/java/io/Serializable/subclass/Test.java
author lana
Thu, 26 Dec 2013 12:04:16 -0800
changeset 23010 6dadb192ad81
parent 5506 202f599c92aa
child 32649 2ee9017c7597
permissions -rw-r--r--
8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013 Summary: updated files with 2011, 2012 and 2013 years according to the file's last updated date Reviewed-by: tbell, lancea, chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 1998, 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 *          be overridden. Howver, the testing for reading an object
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 *          that if SerializablePermission "enableSubclassImplamentation"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *          is not in the security policy and security is enabled, that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *          a security excepiton is thrown when constructing the
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * @compile AbstractObjectInputStream.java AbstractObjectOutputStream.java XObjectInputStream.java XObjectOutputStream.java Test.java
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * @run main  Test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * Test if customized readObject and writeObject are called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
class B implements Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    public int publicIntField;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    static public int numWriteObjectCalled = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    B(int v) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        publicIntField = v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private void writeObject(ObjectOutputStream os) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        numWriteObjectCalled++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        os.defaultWriteObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    private void readObject(ObjectInputStream is)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        throws IOException, ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        is.defaultReadObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    }
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
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * Test PutFields interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
class C implements Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    public int xx1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    public int xx2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    final static ObjectStreamField[] serialPersistentFields = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        new ObjectStreamField("x1", Integer.TYPE),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        new ObjectStreamField("x2", Integer.TYPE),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        new ObjectStreamField("x3", Integer.TYPE),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        new ObjectStreamField("x4", Integer.TYPE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    C() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        xx1 = 300;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        xx2 = 400;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    private void writeObject(ObjectOutputStream os) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        ObjectOutputStream.PutField putFields = os.putFields();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        putFields.put("x1", xx1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        putFields.put("x2", xx2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        putFields.put("x3", xx1 * 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        putFields.put("x4", xx2 * 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        os.writeFields();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
class A implements Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    public int  publicIntField;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    public long publicLongField;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    public B    publicBField;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    public B[]  publicBArray = { new B(4), new B(6)};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    public C    publicCField;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    public A() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        publicIntField = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        publicLongField = 10L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        publicBField = new B(5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        publicCField = new C();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
public class Test {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    static public void main(String argv[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        throws IOException, ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        boolean expectSecurityException = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        if (argv.length > 0 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            argv[0].compareTo("-expectSecurityException") == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            expectSecurityException = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        ByteArrayOutputStream baos = new ByteArrayOutputStream(20);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        XObjectOutputStream os = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            os = new XObjectOutputStream(baos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            if (expectSecurityException)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                throw new Error("Assertion failure. " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                                "Expected a security exception on previous line.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        } catch (SecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            if (expectSecurityException)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        os.writeObject(new A());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        os.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        if (B.numWriteObjectCalled != 3)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            throw new Error("Expected B.writeObject() to be called 3 times;" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                            " observed only " + B.numWriteObjectCalled + " times");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        XObjectInputStream is =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            new XObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            A a = (A)is.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            throw new Error("Expected readObjectOverride() to be called and throw IOException(not implemented)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        is.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
};