1/*
2 * Copyright (c) 2004, 2007, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/*
25 * @test
26 * @bug 4985020
27 * @summary Tests passing of null as property name
28 * @author Brent Christian
29 */
30
31import java.beans.PropertyChangeEvent;
32import java.beans.PropertyChangeListener;
33import java.beans.PropertyChangeSupport;
34import java.beans.VetoableChangeListener;
35import java.beans.VetoableChangeSupport;
36
37public class Test4985020 implements PropertyChangeListener, VetoableChangeListener {
38    private static final String NAME = new String("Property Name");
39    private static boolean failed;
40
41    public static void main(String[] args) {
42        PropertyChangeSupport pcs = new PropertyChangeSupport(args);
43        VetoableChangeSupport vcs = new VetoableChangeSupport(args);
44        Test4985020 listener = new Test4985020();
45
46
47        //PropertyChangeSupport.addPropertyChangeListener(null, listener)
48        System.out.println("PropertyChangeSupport.addPropertyChangeListener(null, listener)");
49        try {
50            pcs.addPropertyChangeListener(null, listener);
51            pcs.firePropertyChange(NAME, null, null);
52        }
53        catch (Throwable error) {
54            print(error);
55        }
56
57        //PropertyChangeSupport.removePropertyChangeListener(null, listener)
58        System.out.println("PropertyChangeSupport.removePropertyChangeListener(null, listener)");
59        try {
60            pcs.removePropertyChangeListener(null, listener);
61            pcs.firePropertyChange(NAME, null, null);
62        }
63        catch (Throwable error) {
64            print(error);
65        }
66
67        //PropertyChangeSupport.getPropertyChangeListeners(null)
68        System.out.println("PropertyChangeSupport.getPropertyChangeListeners(null)");
69        try {
70            PropertyChangeListener[] pcls = pcs.getPropertyChangeListeners(null);
71            if (pcls == null) {
72                throw new Error("getPropertyChangeListener() returned null");
73            }
74            if (pcls.length != 0) {
75                throw new Error("getPropertyChangeListener() did not return an empty array");
76            }
77        }
78        catch (Throwable error) {
79            print(error);
80        }
81
82        //PropertyChangeSupport.hasListeners(null)
83        System.out.println("PropertyChangeSupport.hasListeners(null)");
84        try {
85            pcs.hasListeners(null);
86        }
87        catch (Throwable error) {
88            print(error);
89        }
90
91        //PropertyChangeSupport.hasListeners(null): with a generic listener
92        System.out.println("PropertyChangeSupport.hasListeners(null) with a generic listener");
93        try {
94            pcs.addPropertyChangeListener(listener);
95            if (!pcs.hasListeners(null)) {
96                throw new Error("hasListeners(null) returned false, but there was a generic listener");
97            }
98        }
99        catch (Throwable error) {
100            print(error);
101        }
102
103        pcs = new PropertyChangeSupport(args); // reset
104
105        //PropertyChangeSupport.hasListeners(null): with a specific listener
106        System.out.println("PropertyChangeSupport.hasListeners(null) with a specific listener");
107        try {
108            pcs.addPropertyChangeListener(NAME, listener);
109            if (pcs.hasListeners(null)) {
110                throw new Error("hasListeners(null) returned true, but there were no generic listeners - only a specific listener");
111            }
112        }
113        catch (Throwable error) {
114            print(error);
115        }
116
117
118        //VetoableChangeSupport.addVetoableChangeListener(null, listener)
119        System.out.println("VetoableChangeSupport.addVetoableChangeListener(null, listener)");
120        try {
121            vcs.addVetoableChangeListener(null, listener);
122            vcs.fireVetoableChange(NAME, null, null);
123        }
124        catch (Throwable error) {
125            print(error);
126        }
127
128        //VetoableChangeSupport.removeVetoableChangeListener(null, listener)
129        System.out.println("VetoableChangeSupport.removeVetoableChangeListener(null, listener)");
130        try {
131            vcs.removeVetoableChangeListener(null, listener);
132            vcs.fireVetoableChange(NAME, null, null);
133        }
134        catch (Throwable error) {
135            print(error);
136        }
137
138        //VetoableChangeSupport.getVetoableChangeListeners(null)
139        System.out.println("VetoableChangeSupport.getVetoableChangeListeners(null)");
140        try {
141            VetoableChangeListener[] pcls = vcs.getVetoableChangeListeners(null);
142            if (pcls == null) {
143                throw new Error("getVetoableChangeListener() returned null");
144            }
145            if (pcls.length != 0) {
146                throw new Error("getVetoableChangeListener() did not return an empty array");
147            }
148        }
149        catch (Throwable error) {
150            print(error);
151        }
152
153        //VetoableChangeSupport.hasListeners(null)
154        System.out.println("VetoableChangeSupport.hasListeners(null)");
155        try {
156            boolean result = vcs.hasListeners(null);
157        }
158        catch (Throwable error) {
159            print(error);
160        }
161
162        //VetoableChangeSupport.hasListeners(null): with a generic listener
163        System.out.println("VetoableChangeSupport.hasListeners(null) with a generic listener");
164        try {
165            vcs.addVetoableChangeListener(listener);
166            if (!vcs.hasListeners(null)) {
167                throw new Error("hasListeners(null) returned false, but there was a generic listener");
168            }
169        }
170        catch (Throwable error) {
171            print(error);
172        }
173
174        vcs = new VetoableChangeSupport(args); // reset
175
176        //VetoableChangeSupport.hasListeners(null): with a specific listener
177        System.out.println("VetoableChangeSupport.hasListeners(null) with a specific listener");
178        try {
179            vcs.addVetoableChangeListener(NAME, listener);
180            if (vcs.hasListeners(null)) {
181                throw new Error("hasListeners(null) returned true, but there were no generic listeners - only a specific listener");
182            }
183        }
184        catch (Throwable error) {
185            print(error);
186        }
187
188
189        if (failed) {
190            throw new Error("TEST FAILED");
191        }
192    }
193
194    private static void print(Throwable error) {
195        error.printStackTrace();
196        failed = true;
197    }
198
199    public void propertyChange(PropertyChangeEvent event) {
200        System.out.println("* propertyChange(event) event is " + event.getPropertyName());
201        throw new Error("shouldn't be any listeners");
202    }
203
204    public void vetoableChange(PropertyChangeEvent event) {
205        System.out.println("* vetoableChange(event) event is " + event.getPropertyName());
206        throw new Error("shouldn't be any listeners");
207    }
208}
209