2
|
1 |
/*
|
5506
|
2 |
* Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.
|
2
|
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 |
*
|
5506
|
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.
|
2
|
22 |
*/
|
|
23 |
|
|
24 |
/*
|
|
25 |
* @test
|
|
26 |
* @bug 5004188
|
|
27 |
* @summary Tests sequence of listeners
|
|
28 |
* @author Sergey Malenkov
|
|
29 |
*/
|
|
30 |
|
|
31 |
import java.beans.PropertyChangeEvent;
|
|
32 |
import java.beans.PropertyChangeListener;
|
|
33 |
import java.beans.PropertyChangeListenerProxy;
|
|
34 |
import java.beans.PropertyChangeSupport;
|
|
35 |
|
|
36 |
public final class TestListeners implements PropertyChangeListener {
|
|
37 |
private static final String NAME = "property";
|
|
38 |
private static final String NONE = "broken";
|
|
39 |
|
|
40 |
private static int current;
|
|
41 |
|
|
42 |
public static void main(String[] args) {
|
|
43 |
PropertyChangeSupport pcs = new PropertyChangeSupport(TestListeners.class);
|
|
44 |
pcs.addPropertyChangeListener(new TestListeners(0));
|
|
45 |
pcs.addPropertyChangeListener(NAME, new TestListeners(2));
|
|
46 |
pcs.addPropertyChangeListener(new TestListeners(1));
|
|
47 |
pcs.addPropertyChangeListener(NAME, new PropertyChangeListenerProxy(NAME, new TestListeners(3)));
|
|
48 |
|
|
49 |
|
|
50 |
current = 0;
|
|
51 |
pcs.firePropertyChange(NAME, 0, 1);
|
|
52 |
if (current != 4)
|
|
53 |
throw new Error("Expected 4 listeners, but called " + current);
|
|
54 |
|
|
55 |
current = 0;
|
|
56 |
pcs.firePropertyChange(NONE, 1, 0);
|
|
57 |
if (current != 2)
|
|
58 |
throw new Error("Expected 2 listeners, but called " + current);
|
|
59 |
|
|
60 |
|
|
61 |
PropertyChangeListener[] all = pcs.getPropertyChangeListeners();
|
|
62 |
if (all.length != 4)
|
|
63 |
throw new Error("Expected 4 listeners, but contained " + all.length);
|
|
64 |
|
|
65 |
PropertyChangeListener[] named = pcs.getPropertyChangeListeners(NAME);
|
|
66 |
if (named.length != 2)
|
|
67 |
throw new Error("Expected 2 named listeners, but contained " + named.length);
|
|
68 |
|
|
69 |
PropertyChangeListener[] other = pcs.getPropertyChangeListeners(NONE);
|
|
70 |
if (other.length != 0)
|
|
71 |
throw new Error("Expected 0 other listeners, but contained " + other.length);
|
|
72 |
|
|
73 |
pcs.removePropertyChangeListener(new TestListeners(0));
|
|
74 |
pcs.removePropertyChangeListener(new TestListeners(1));
|
|
75 |
pcs.removePropertyChangeListener(NAME, new TestListeners(2));
|
|
76 |
pcs.removePropertyChangeListener(NAME, new PropertyChangeListenerProxy(NAME, new TestListeners(3)));
|
|
77 |
|
|
78 |
all = pcs.getPropertyChangeListeners();
|
|
79 |
if (all.length != 0)
|
|
80 |
throw new Error("Expected 4 listeners, but contained " + all.length);
|
|
81 |
|
|
82 |
named = pcs.getPropertyChangeListeners(NAME);
|
|
83 |
if (named.length != 0)
|
|
84 |
throw new Error("Expected 2 named listeners, but contained " + named.length);
|
|
85 |
|
|
86 |
other = pcs.getPropertyChangeListeners(NONE);
|
|
87 |
if (other.length != 0)
|
|
88 |
throw new Error("Expected 0 other listeners, but contained " + other.length);
|
|
89 |
}
|
|
90 |
|
|
91 |
private final int index;
|
|
92 |
|
|
93 |
public TestListeners(int index) {
|
|
94 |
this.index = index;
|
|
95 |
}
|
|
96 |
|
|
97 |
public void propertyChange(PropertyChangeEvent event) {
|
|
98 |
if (this.index < 0)
|
|
99 |
throw new Error("Unexpected listener: " + this.index);
|
|
100 |
|
|
101 |
System.out.println("index = " + this.index);
|
|
102 |
if (this.index != current++)
|
|
103 |
throw new Error("Unexpected listener: " + this.index);
|
|
104 |
}
|
|
105 |
|
|
106 |
@Override
|
|
107 |
public boolean equals(Object object) {
|
|
108 |
if (object instanceof TestListeners) {
|
|
109 |
TestListeners test = (TestListeners)object;
|
|
110 |
return test.index == this.index;
|
|
111 |
}
|
|
112 |
return false;
|
|
113 |
}
|
|
114 |
|
|
115 |
@Override
|
|
116 |
public int hashCode() {
|
|
117 |
return this.index;
|
|
118 |
}
|
|
119 |
}
|