jdk/test/java/beans/PropertyChangeSupport/TestSerialization.java
author lana
Tue, 23 May 2017 21:11:42 +0000
changeset 45187 331a6542b5b8
parent 5506 202f599c92aa
permissions -rw-r--r--
Added tag jdk-10+7 for changeset d554736d963e
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) 2007, 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 5004188
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @run main TestSerialization 1.5.0_10.ser 1.6.0.ser
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @summary Tests serialization of PropertyChangeSupport
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * @author Sergey Malenkov
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.beans.PropertyChangeEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.beans.PropertyChangeListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.beans.PropertyChangeListenerProxy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.beans.PropertyChangeSupport;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.io.ByteArrayOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.io.File;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.io.FileInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.io.FileOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.io.ObjectInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.io.ObjectOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.io.Serializable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
public final class TestSerialization implements PropertyChangeListener, Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private static final String NAME = "property";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        if (args.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
            String file = System.getProperty("java.version") + ".ser";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
            serialize(file, create());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
            byte[] array = serialize(create());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
            for (String file : args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
                check(deserialize(file));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
                check(array, read(file));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private static PropertyChangeSupport create() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        PropertyChangeSupport pcs = new PropertyChangeSupport(TestSerialization.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        pcs.addPropertyChangeListener(new TestSerialization(0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        pcs.addPropertyChangeListener(NAME, new TestSerialization(1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        return pcs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    private static void check(PropertyChangeSupport pcs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        PropertyChangeListener[] namedListeners = pcs.getPropertyChangeListeners(NAME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        check(namedListeners, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        check(namedListeners[0], 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        PropertyChangeListener[] allListeners = pcs.getPropertyChangeListeners();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        check(allListeners, 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        check(allListeners[0], 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        check(allListeners[1], 1, NAME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    private static void check(byte[] a1, byte[] a2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        int length = a1.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        if (length != a2.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            throw new Error("Different file sizes: " + length + " != " + a2.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        for (int i = 0; i < length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            if (a1[i] != a2[i])
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                throw new Error("Different bytes at " + i + " position");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    private static void check(PropertyChangeListener[] array, int length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        if (length != array.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            throw new Error("Unexpected amount of listeners: " + array.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    private static void check(PropertyChangeListener listener, int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        if (!(listener instanceof TestSerialization))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            throw new Error("Unexpected listener: " + listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        TestSerialization object = (TestSerialization)listener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        if (index != object.index)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            throw new Error("Unexpected index: " + index + " != " + object.index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    private static void check(PropertyChangeListener listener, int index, String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        if (!(listener instanceof PropertyChangeListenerProxy))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            throw new Error("Unexpected listener: " + listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        PropertyChangeListenerProxy object = (PropertyChangeListenerProxy)listener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        if (!name.equals(object.getPropertyName()))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            throw new Error("Unexpected name: " + name + " != " + object.getPropertyName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        check((PropertyChangeListener)object.getListener(), index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    private static byte[] read(String file) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        FileInputStream stream = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            stream = new FileInputStream(new File(System.getProperty("test.src", "."), file));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            ByteArrayOutputStream out = new ByteArrayOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            for (int i = stream.read(); i != -1; i = stream.read())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                out.write(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            return out.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            if (stream != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                stream.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    private static PropertyChangeSupport deserialize(String file) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        ObjectInputStream stream = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            stream = new ObjectInputStream(new FileInputStream(new File(System.getProperty("test.src", "."), file)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            return (PropertyChangeSupport)stream.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            if (stream != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                stream.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    private static void serialize(String file, PropertyChangeSupport pcs) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        ObjectOutputStream stream = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            stream = new ObjectOutputStream(new FileOutputStream(new File(System.getProperty("test.src", "."), file)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            stream.writeObject(pcs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            if (stream != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                stream.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    private static byte[] serialize(PropertyChangeSupport pcs) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        ObjectOutputStream stream = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            ByteArrayOutputStream out = new ByteArrayOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            stream = new ObjectOutputStream(out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            stream.writeObject(pcs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            return out.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            if (stream != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                stream.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    private int index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    public TestSerialization(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        this.index = index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    public int getIndex() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        return this.index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    public void propertyChange(PropertyChangeEvent event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
}