Path: blob/master/test/jdk/java/beans/PropertyChangeSupport/TestSerialization.java
41152 views
/*1* Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 500418826* @run main TestSerialization 1.5.0_10.ser 1.6.0.ser27* @summary Tests serialization of PropertyChangeSupport28* @author Sergey Malenkov29*/3031import java.beans.PropertyChangeEvent;32import java.beans.PropertyChangeListener;33import java.beans.PropertyChangeListenerProxy;34import java.beans.PropertyChangeSupport;3536import java.io.ByteArrayOutputStream;37import java.io.File;38import java.io.FileInputStream;39import java.io.FileOutputStream;40import java.io.ObjectInputStream;41import java.io.ObjectOutputStream;42import java.io.Serializable;4344public final class TestSerialization implements PropertyChangeListener, Serializable {45private static final String NAME = "property";4647public static void main(String[] args) throws Exception {48if (args.length == 0) {49String file = System.getProperty("java.version") + ".ser";50serialize(file, create());51}52else {53byte[] array = serialize(create());54for (String file : args) {55check(deserialize(file));56check(array, read(file));57}58}59}6061private static PropertyChangeSupport create() {62PropertyChangeSupport pcs = new PropertyChangeSupport(TestSerialization.class);63pcs.addPropertyChangeListener(new TestSerialization(0));64pcs.addPropertyChangeListener(NAME, new TestSerialization(1));65return pcs;66}6768private static void check(PropertyChangeSupport pcs) {69PropertyChangeListener[] namedListeners = pcs.getPropertyChangeListeners(NAME);70check(namedListeners, 1);71check(namedListeners[0], 1);7273PropertyChangeListener[] allListeners = pcs.getPropertyChangeListeners();74check(allListeners, 2);75check(allListeners[0], 0);76check(allListeners[1], 1, NAME);77}7879private static void check(byte[] a1, byte[] a2) {80int length = a1.length;81if (length != a2.length)82throw new Error("Different file sizes: " + length + " != " + a2.length);8384for (int i = 0; i < length; i++)85if (a1[i] != a2[i])86throw new Error("Different bytes at " + i + " position");87}8889private static void check(PropertyChangeListener[] array, int length) {90if (length != array.length)91throw new Error("Unexpected amount of listeners: " + array.length);92}9394private static void check(PropertyChangeListener listener, int index) {95if (!(listener instanceof TestSerialization))96throw new Error("Unexpected listener: " + listener);9798TestSerialization object = (TestSerialization)listener;99if (index != object.index)100throw new Error("Unexpected index: " + index + " != " + object.index);101}102103private static void check(PropertyChangeListener listener, int index, String name) {104if (!(listener instanceof PropertyChangeListenerProxy))105throw new Error("Unexpected listener: " + listener);106107PropertyChangeListenerProxy object = (PropertyChangeListenerProxy)listener;108if (!name.equals(object.getPropertyName()))109throw new Error("Unexpected name: " + name + " != " + object.getPropertyName());110111check((PropertyChangeListener)object.getListener(), index);112}113114private static byte[] read(String file) throws Exception {115FileInputStream stream = null;116try {117stream = new FileInputStream(new File(System.getProperty("test.src", "."), file));118ByteArrayOutputStream out = new ByteArrayOutputStream();119for (int i = stream.read(); i != -1; i = stream.read())120out.write(i);121122return out.toByteArray();123}124finally {125if (stream != null)126stream.close();127}128}129130private static PropertyChangeSupport deserialize(String file) throws Exception {131ObjectInputStream stream = null;132try {133stream = new ObjectInputStream(new FileInputStream(new File(System.getProperty("test.src", "."), file)));134return (PropertyChangeSupport)stream.readObject();135}136finally {137if (stream != null)138stream.close();139}140}141142private static void serialize(String file, PropertyChangeSupport pcs) throws Exception {143ObjectOutputStream stream = null;144try {145stream = new ObjectOutputStream(new FileOutputStream(new File(System.getProperty("test.src", "."), file)));146stream.writeObject(pcs);147}148finally {149if (stream != null)150stream.close();151}152}153154private static byte[] serialize(PropertyChangeSupport pcs) throws Exception {155ObjectOutputStream stream = null;156try {157ByteArrayOutputStream out = new ByteArrayOutputStream();158stream = new ObjectOutputStream(out);159stream.writeObject(pcs);160return out.toByteArray();161}162finally {163if (stream != null)164stream.close();165}166}167168169private int index;170171public TestSerialization(int index) {172this.index = index;173}174175public int getIndex() {176return this.index;177}178179public void propertyChange(PropertyChangeEvent event) {180}181}182183184