Path: blob/master/test/jdk/java/beans/PropertyChangeSupport/Test4985020.java
41149 views
/*1* Copyright (c) 2004, 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 498502026* @summary Tests passing of null as property name27* @author Brent Christian28*/2930import java.beans.PropertyChangeEvent;31import java.beans.PropertyChangeListener;32import java.beans.PropertyChangeSupport;33import java.beans.VetoableChangeListener;34import java.beans.VetoableChangeSupport;3536public class Test4985020 implements PropertyChangeListener, VetoableChangeListener {37private static final String NAME = new String("Property Name");38private static boolean failed;3940public static void main(String[] args) {41PropertyChangeSupport pcs = new PropertyChangeSupport(args);42VetoableChangeSupport vcs = new VetoableChangeSupport(args);43Test4985020 listener = new Test4985020();444546//PropertyChangeSupport.addPropertyChangeListener(null, listener)47System.out.println("PropertyChangeSupport.addPropertyChangeListener(null, listener)");48try {49pcs.addPropertyChangeListener(null, listener);50pcs.firePropertyChange(NAME, null, null);51}52catch (Throwable error) {53print(error);54}5556//PropertyChangeSupport.removePropertyChangeListener(null, listener)57System.out.println("PropertyChangeSupport.removePropertyChangeListener(null, listener)");58try {59pcs.removePropertyChangeListener(null, listener);60pcs.firePropertyChange(NAME, null, null);61}62catch (Throwable error) {63print(error);64}6566//PropertyChangeSupport.getPropertyChangeListeners(null)67System.out.println("PropertyChangeSupport.getPropertyChangeListeners(null)");68try {69PropertyChangeListener[] pcls = pcs.getPropertyChangeListeners(null);70if (pcls == null) {71throw new Error("getPropertyChangeListener() returned null");72}73if (pcls.length != 0) {74throw new Error("getPropertyChangeListener() did not return an empty array");75}76}77catch (Throwable error) {78print(error);79}8081//PropertyChangeSupport.hasListeners(null)82System.out.println("PropertyChangeSupport.hasListeners(null)");83try {84pcs.hasListeners(null);85}86catch (Throwable error) {87print(error);88}8990//PropertyChangeSupport.hasListeners(null): with a generic listener91System.out.println("PropertyChangeSupport.hasListeners(null) with a generic listener");92try {93pcs.addPropertyChangeListener(listener);94if (!pcs.hasListeners(null)) {95throw new Error("hasListeners(null) returned false, but there was a generic listener");96}97}98catch (Throwable error) {99print(error);100}101102pcs = new PropertyChangeSupport(args); // reset103104//PropertyChangeSupport.hasListeners(null): with a specific listener105System.out.println("PropertyChangeSupport.hasListeners(null) with a specific listener");106try {107pcs.addPropertyChangeListener(NAME, listener);108if (pcs.hasListeners(null)) {109throw new Error("hasListeners(null) returned true, but there were no generic listeners - only a specific listener");110}111}112catch (Throwable error) {113print(error);114}115116117//VetoableChangeSupport.addVetoableChangeListener(null, listener)118System.out.println("VetoableChangeSupport.addVetoableChangeListener(null, listener)");119try {120vcs.addVetoableChangeListener(null, listener);121vcs.fireVetoableChange(NAME, null, null);122}123catch (Throwable error) {124print(error);125}126127//VetoableChangeSupport.removeVetoableChangeListener(null, listener)128System.out.println("VetoableChangeSupport.removeVetoableChangeListener(null, listener)");129try {130vcs.removeVetoableChangeListener(null, listener);131vcs.fireVetoableChange(NAME, null, null);132}133catch (Throwable error) {134print(error);135}136137//VetoableChangeSupport.getVetoableChangeListeners(null)138System.out.println("VetoableChangeSupport.getVetoableChangeListeners(null)");139try {140VetoableChangeListener[] pcls = vcs.getVetoableChangeListeners(null);141if (pcls == null) {142throw new Error("getVetoableChangeListener() returned null");143}144if (pcls.length != 0) {145throw new Error("getVetoableChangeListener() did not return an empty array");146}147}148catch (Throwable error) {149print(error);150}151152//VetoableChangeSupport.hasListeners(null)153System.out.println("VetoableChangeSupport.hasListeners(null)");154try {155boolean result = vcs.hasListeners(null);156}157catch (Throwable error) {158print(error);159}160161//VetoableChangeSupport.hasListeners(null): with a generic listener162System.out.println("VetoableChangeSupport.hasListeners(null) with a generic listener");163try {164vcs.addVetoableChangeListener(listener);165if (!vcs.hasListeners(null)) {166throw new Error("hasListeners(null) returned false, but there was a generic listener");167}168}169catch (Throwable error) {170print(error);171}172173vcs = new VetoableChangeSupport(args); // reset174175//VetoableChangeSupport.hasListeners(null): with a specific listener176System.out.println("VetoableChangeSupport.hasListeners(null) with a specific listener");177try {178vcs.addVetoableChangeListener(NAME, listener);179if (vcs.hasListeners(null)) {180throw new Error("hasListeners(null) returned true, but there were no generic listeners - only a specific listener");181}182}183catch (Throwable error) {184print(error);185}186187188if (failed) {189throw new Error("TEST FAILED");190}191}192193private static void print(Throwable error) {194error.printStackTrace();195failed = true;196}197198public void propertyChange(PropertyChangeEvent event) {199System.out.println("* propertyChange(event) event is " + event.getPropertyName());200throw new Error("shouldn't be any listeners");201}202203public void vetoableChange(PropertyChangeEvent event) {204System.out.println("* vetoableChange(event) event is " + event.getPropertyName());205throw new Error("shouldn't be any listeners");206}207}208209210