jdk/test/java/util/Calendar/Bug7017458.java
author peytoia
Thu, 26 Jan 2012 17:06:47 +0900
changeset 11681 9a1076231db7
permissions -rw-r--r--
7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException Reviewed-by: okutsu
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
11681
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
     1
/*
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
     2
 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
     4
 *
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
     7
 * published by the Free Software Foundation.
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
     8
 *
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
    13
 * accompanied this code).
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
    14
 *
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
    18
 *
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
    21
 * questions.
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
    22
 */
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
    23
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
    24
/*
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
    25
 * @test
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
    26
 * @bug 7017458
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
    27
 * @summary Test of multithreaded serialization/deserialization of Calendar.
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
    28
 */
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
    29
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
    30
import java.io.*;
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
    31
import java.util.Calendar;
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
    32
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
    33
public class Bug7017458 {
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
    34
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
    35
    static volatile boolean err = false;
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
    36
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
    37
    public static void main(String[] args) {
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
    38
        try {
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
    39
            new Bug7017458().perform();
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
    40
        }
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
    41
        catch (Exception e) {
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
    42
            e.printStackTrace();
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
    43
            err = true;
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
    44
        }
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
    45
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
    46
        if (err) {
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
    47
            throw new RuntimeException("Multithreaded serialization/deserialization test failed.");
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
    48
        } else {
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
    49
            System.out.println("Multithreaded serialization/deserialization test passed.");
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
    50
        }
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
    51
    }
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
    52
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
    53
    public void perform() throws Exception {
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
    54
        int nbThreads = 8;
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
    55
        Calendar cal = Calendar.getInstance();
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
    56
        SerializationThread[] threads = new SerializationThread[nbThreads];
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
    57
        for (int i = 0; i < nbThreads; i++) {
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
    58
            threads[i] = new SerializationThread(cal);
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
    59
        }
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
    60
        for (int i = 0; i < nbThreads; i++) {
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
    61
            threads[i].start();
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
    62
        }
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
    63
        for (int i = 0; i < nbThreads; i++) {
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
    64
            threads[i].join();
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
    65
        }
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
    66
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
    67
        DeserializationThread[] threads2 = new DeserializationThread[nbThreads];
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
    68
        for (int i = 0; i < nbThreads; i++) {
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
    69
            threads2[i] = new DeserializationThread(threads[i].data);
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
    70
        }
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
    71
        for (int i = 0; i < nbThreads; i++) {
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
    72
            threads2[i].start();
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
    73
        }
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
    74
        for (int i = 0; i < nbThreads; i++) {
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
    75
            threads2[i].join();
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
    76
        }
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
    77
    }
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
    78
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
    79
    public class SerializationThread extends Thread {
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
    80
        private Calendar cal;
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
    81
        public byte[] data;
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
    82
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
    83
        public SerializationThread(Calendar cal) {
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
    84
            this.cal = cal;
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
    85
        }
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
    86
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
    87
        public void run() {
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
    88
            try {
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
    89
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
    90
                ObjectOutputStream oos = new ObjectOutputStream(baos);
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
    91
                oos.writeObject(cal);
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
    92
                oos.flush();
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
    93
                oos.close();
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
    94
                data = baos.toByteArray();
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
    95
                }
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
    96
            catch (Exception e) {
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
    97
                e.printStackTrace();
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
    98
                err = true;
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
    99
            }
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
   100
        }
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
   101
    }
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
   102
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
   103
    public class DeserializationThread extends Thread {
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
   104
        public Calendar cal;
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
   105
        public byte[] data;
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
   106
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
   107
        public DeserializationThread(byte[] data) {
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
   108
            this.data = data;
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
   109
        }
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
   110
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
   111
        public void run() {
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
   112
            try {
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
   113
                ByteArrayInputStream bais = new ByteArrayInputStream(data);
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
   114
                ObjectInputStream ois = new ObjectInputStream(bais);
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
   115
                cal = (Calendar) ois.readObject();
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
   116
                ois.close();
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
   117
            }
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
   118
            catch (Exception e) {
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
   119
                e.printStackTrace();
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
   120
                err = true;
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
   121
            }
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
   122
        }
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
   123
    }
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
   124
}
9a1076231db7 7017458: (cal) Multithreaded deserialization of Calendar leads to ClassCastException
peytoia
parents:
diff changeset
   125