Path: blob/master/test/jdk/java/beans/PropertyChangeSupport/Test4092905.java
41152 views
/*1* Copyright (c) 1998, 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 409290526* @summary Tests that hasListeners() is not failed across serialization27* @author Graham Hamilton28*/2930import java.beans.PropertyChangeEvent;31import java.beans.PropertyChangeListener;32import java.beans.PropertyChangeSupport;3334import java.io.ByteArrayInputStream;35import java.io.ByteArrayOutputStream;36import java.io.ObjectInputStream;37import java.io.ObjectOutputStream;38import java.io.Serializable;3940public class Test4092905 {41private static final String PUBLIC = "public";42private static final String PRIVATE = "private";4344public static void main(String[] args) {45PropertyChangeSupport pcs = new PropertyChangeSupport(args);46pcs.addPropertyChangeListener(PUBLIC, new PublicListener());47pcs.addPropertyChangeListener(PRIVATE, new PrivateListener());4849if (!pcs.hasListeners(PUBLIC)) {50throw new Error("no public listener");51}52if (!pcs.hasListeners(PRIVATE)) {53throw new Error("no private listener");54}5556try {57// serialize into byte array58ByteArrayOutputStream baos = new ByteArrayOutputStream();59ObjectOutputStream output = new ObjectOutputStream(baos);60output.writeObject(pcs);61output.flush();62// deserialize from byte array63ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());64ObjectInputStream input = new ObjectInputStream(bais);65pcs = (PropertyChangeSupport) input.readObject();66} catch (Exception exception) {67throw new Error("unexpected exception", exception);68}6970if (!pcs.hasListeners(PUBLIC)) {71throw new Error("no public listener");72}73if (pcs.hasListeners(PRIVATE)) {74throw new Error("unexpected private listener");75}76}7778public static class PublicListener implements PropertyChangeListener, Serializable {79public void propertyChange(PropertyChangeEvent event) {80}81}8283private static class PrivateListener implements PropertyChangeListener {84public void propertyChange(PropertyChangeEvent event) {85}86}87}888990