1 |
14 |
jlechner |
import java.beans.PropertyChangeSupport;
|
2 |
|
|
import java.beans.PropertyChangeListener;
|
3 |
|
|
import java.beans.PropertyChangeEvent;
|
4 |
|
|
import java.util.Hashtable;
|
5 |
|
|
|
6 |
|
|
public class PropertyChangeSupportTest implements Runnable {
|
7 |
|
|
class Source {
|
8 |
|
|
PropertyChangeSupport support = new PropertyChangeSupport(this);
|
9 |
|
|
public void addPropertyChangeListener(PropertyChangeListener l) {
|
10 |
|
|
support.addPropertyChangeListener(l);
|
11 |
|
|
}
|
12 |
|
|
public void addPropertyChangeListener(String s, PropertyChangeListener l) {
|
13 |
|
|
support.addPropertyChangeListener(s,l);
|
14 |
|
|
}
|
15 |
|
|
public void removePropertyChangeListener(PropertyChangeListener l) {
|
16 |
|
|
support.removePropertyChangeListener(l);
|
17 |
|
|
}
|
18 |
|
|
public void removePropertyChangeListener(String s, PropertyChangeListener l) {
|
19 |
|
|
support.removePropertyChangeListener(s,l);
|
20 |
|
|
}
|
21 |
|
|
|
22 |
|
|
void changeProperty(String name) {
|
23 |
|
|
support.firePropertyChange(name,"old","new");
|
24 |
|
|
|
25 |
|
|
}
|
26 |
|
|
}
|
27 |
|
|
|
28 |
|
|
class Listener implements PropertyChangeListener {
|
29 |
|
|
Hashtable numEventsReceived = new Hashtable();
|
30 |
|
|
int getNumEvents(String propertyName) {
|
31 |
|
|
Integer i = (Integer)numEventsReceived.get(propertyName);
|
32 |
|
|
try {
|
33 |
|
|
return i.intValue();
|
34 |
|
|
} catch(NullPointerException e) {
|
35 |
|
|
return 0;
|
36 |
|
|
}
|
37 |
|
|
}
|
38 |
|
|
|
39 |
|
|
public void propertyChange(PropertyChangeEvent e) {
|
40 |
|
|
Integer i = (Integer)numEventsReceived.get(e.getPropertyName());
|
41 |
|
|
try {
|
42 |
|
|
int newI = i.intValue() + 1;
|
43 |
|
|
numEventsReceived.put(e.getPropertyName(), new Integer(newI));
|
44 |
|
|
} catch(NullPointerException exc) {
|
45 |
|
|
numEventsReceived.put(e.getPropertyName(), new Integer(1));
|
46 |
|
|
}
|
47 |
|
|
}
|
48 |
|
|
|
49 |
|
|
public void reset() {
|
50 |
|
|
numEventsReceived = new Hashtable();
|
51 |
|
|
}
|
52 |
|
|
}
|
53 |
|
|
|
54 |
|
|
public void setProperties(Source s) {
|
55 |
|
|
s.changeProperty("foo");
|
56 |
|
|
s.changeProperty("foo");
|
57 |
|
|
s.changeProperty("foo");
|
58 |
|
|
s.changeProperty("bar");
|
59 |
|
|
s.changeProperty("bar");
|
60 |
|
|
s.changeProperty("foobar");
|
61 |
|
|
}
|
62 |
|
|
|
63 |
|
|
public void shouldEqual(Listener l, int foo, int bar, int foobar, String testName) {
|
64 |
|
|
String whatsWrong = "";
|
65 |
|
|
if(l.getNumEvents("foo") != foo) {
|
66 |
|
|
whatsWrong += ("foo(" + l.getNumEvents("foo") + ") != " + foo);
|
67 |
|
|
}
|
68 |
|
|
if(l.getNumEvents("bar") != bar) {
|
69 |
|
|
whatsWrong += (" bar(" + l.getNumEvents("bar") + ") != " + bar);
|
70 |
|
|
}
|
71 |
|
|
if(l.getNumEvents("foobar") != foobar) {
|
72 |
|
|
whatsWrong += (" foobar(" + l.getNumEvents("foobar") + ") != " + foobar);
|
73 |
|
|
}
|
74 |
|
|
|
75 |
|
|
if(!whatsWrong.equals("")) {
|
76 |
|
|
System.out.println("FAILURE: " + testName + ": " + whatsWrong);
|
77 |
|
|
} else {
|
78 |
|
|
System.out.println("Success: " + testName);
|
79 |
|
|
}
|
80 |
|
|
}
|
81 |
|
|
|
82 |
|
|
public void run() {
|
83 |
|
|
Source s = new Source();
|
84 |
|
|
|
85 |
|
|
/* Test: single multi-property adds */
|
86 |
|
|
Listener l = new Listener();
|
87 |
|
|
s.addPropertyChangeListener(l);
|
88 |
|
|
setProperties(s);
|
89 |
|
|
shouldEqual(l, 3, 2, 1, "single multi-property adds");
|
90 |
|
|
|
91 |
|
|
/* Test: multiple listeners */
|
92 |
|
|
Listener l2 = new Listener();
|
93 |
|
|
s.addPropertyChangeListener(l2);
|
94 |
|
|
setProperties(s);
|
95 |
|
|
shouldEqual(l, 6, 4, 2, "multiple listeners-l");
|
96 |
|
|
shouldEqual(l2, 3, 2, 1, "multiple listeners-l2");
|
97 |
|
|
|
98 |
|
|
/* Test: multiple multi-property adds */
|
99 |
|
|
s.addPropertyChangeListener(l);
|
100 |
|
|
setProperties(s);
|
101 |
|
|
shouldEqual(l, 12, 8, 4, "multiple multi-property adds-l");
|
102 |
|
|
shouldEqual(l2, 6, 4, 2, "multiple multi-property adds-l2");
|
103 |
|
|
|
104 |
|
|
/* Test: remove multi-property add */
|
105 |
|
|
s.removePropertyChangeListener(l);
|
106 |
|
|
setProperties(s);
|
107 |
|
|
shouldEqual(l, 15, 10, 5, "remove multi-property add-l");
|
108 |
|
|
shouldEqual(l2, 9, 6, 3, "remove multi-property add-l2");
|
109 |
|
|
|
110 |
|
|
|
111 |
|
|
s.removePropertyChangeListener(l);
|
112 |
|
|
s.removePropertyChangeListener(l2);
|
113 |
|
|
l.reset();
|
114 |
|
|
l2.reset();
|
115 |
|
|
|
116 |
|
|
/* ENABLE THIS IF YOU THINK RESET ISN'T HAPPENING
|
117 |
|
|
shouldEqual(l, 0, 0, 0, "RESET-l");
|
118 |
|
|
shouldEqual(l2, 0, 0, 0, "RESET-l2");
|
119 |
|
|
setProperties(s);
|
120 |
|
|
shouldEqual(l, 0, 0, 0, "RESET_AGAIN-l");
|
121 |
|
|
shouldEqual(l2, 0, 0, 0, "RESET_AGAIN-l2");
|
122 |
|
|
*/
|
123 |
|
|
|
124 |
|
|
|
125 |
|
|
/* Test: single property listener */
|
126 |
|
|
s.addPropertyChangeListener("foo", l);
|
127 |
|
|
setProperties(s);
|
128 |
|
|
shouldEqual(l, 3, 0, 0, "single property listener");
|
129 |
|
|
|
130 |
|
|
/* Test: multiple different properties */
|
131 |
|
|
s.addPropertyChangeListener("bar", l);
|
132 |
|
|
setProperties(s);
|
133 |
|
|
shouldEqual(l, 6, 2, 0, "multiple different properties");
|
134 |
|
|
|
135 |
|
|
/* Test: multiple of same property */
|
136 |
|
|
s.addPropertyChangeListener("foo", l);
|
137 |
|
|
setProperties(s);
|
138 |
|
|
shouldEqual(l, 12, 4, 0, "multiple of same property");
|
139 |
|
|
|
140 |
|
|
/* Test: multiple single-property listeners */
|
141 |
|
|
s.addPropertyChangeListener("foo", l2);
|
142 |
|
|
setProperties(s);
|
143 |
|
|
shouldEqual(l, 18, 6, 0, "multiple single-property listeners-l");
|
144 |
|
|
shouldEqual(l2, 3, 0, 0, "multiple single-property listeners-l2");
|
145 |
|
|
|
146 |
|
|
/* Test: remove single-property add */
|
147 |
|
|
s.removePropertyChangeListener("foo", l);
|
148 |
|
|
setProperties(s);
|
149 |
|
|
shouldEqual(l, 21, 8, 0, "remove single-property add-l");
|
150 |
|
|
shouldEqual(l2, 6, 0, 0, "remove single-property add-l2");
|
151 |
|
|
|
152 |
|
|
|
153 |
|
|
s.removePropertyChangeListener("foo", l);
|
154 |
|
|
s.removePropertyChangeListener("bar", l);
|
155 |
|
|
s.removePropertyChangeListener("foo", l2);
|
156 |
|
|
l.reset();
|
157 |
|
|
l2.reset();
|
158 |
|
|
|
159 |
|
|
/* ENABLE THIS IF YOU THINK RESET ISN'T HAPPENING
|
160 |
|
|
shouldEqual(l, 0, 0, 0, "RESET-l");
|
161 |
|
|
shouldEqual(l2, 0, 0, 0, "RESET-l2");
|
162 |
|
|
setProperties(s);
|
163 |
|
|
shouldEqual(l, 0, 0, 0, "RESET_AGAIN-l");
|
164 |
|
|
shouldEqual(l2, 0, 0, 0, "RESET_AGAIN-l2");
|
165 |
|
|
*/
|
166 |
|
|
|
167 |
|
|
/* Test: multiple- and single-property interaction */
|
168 |
|
|
s.addPropertyChangeListener(l);
|
169 |
|
|
s.addPropertyChangeListener("foo", l);
|
170 |
|
|
setProperties(s);
|
171 |
|
|
shouldEqual(l, 6, 2, 1, "multiple- and single-property interaction");
|
172 |
|
|
|
173 |
|
|
/* Test: multiple- and single-property interaction: multiple-listener removal */
|
174 |
|
|
s.removePropertyChangeListener(l);
|
175 |
|
|
setProperties(s);
|
176 |
|
|
shouldEqual(l, 9, 2, 1, "multiple- and single-property interaction: multiple-listener removal");
|
177 |
|
|
|
178 |
|
|
/* Test: hasListeners() with multiple cases */
|
179 |
|
|
if(s.support.hasListeners("foo")) {
|
180 |
|
|
System.out.println("Success: hasListeners() returning true");
|
181 |
|
|
} else {
|
182 |
|
|
System.out.println("FAILURE: hasListeners() returning true");
|
183 |
|
|
}
|
184 |
|
|
if(s.support.hasListeners("bar")) {
|
185 |
|
|
System.out.println("FAILURE: hasListeners() returning false");
|
186 |
|
|
} else {
|
187 |
|
|
System.out.println("Success: hasListeners() returning false");
|
188 |
|
|
}
|
189 |
|
|
s.addPropertyChangeListener(l);
|
190 |
|
|
if(s.support.hasListeners("bar")) {
|
191 |
|
|
System.out.println("Success: hasListeners() with all-event listeners");
|
192 |
|
|
} else {
|
193 |
|
|
System.out.println("FAILURE: hasListeners() with all-event listeners");
|
194 |
|
|
}
|
195 |
|
|
}
|
196 |
|
|
|
197 |
|
|
public static void main(String[] args) {
|
198 |
|
|
new PropertyChangeSupportTest().run();
|
199 |
|
|
}
|
200 |
|
|
}
|