summaryrefslogtreecommitdiff
path: root/libjava/classpath/test/java.beans/PropertyChangeSupportTest.java
blob: 8db2af03fb0c8fa9cf871808ae46bde743ebd162 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import java.beans.PropertyChangeSupport;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeEvent;
import java.util.Hashtable;

public class PropertyChangeSupportTest implements Runnable {
	class Source {
		PropertyChangeSupport support = new PropertyChangeSupport(this);
		public void addPropertyChangeListener(PropertyChangeListener l) {
			support.addPropertyChangeListener(l);
		}
		public void addPropertyChangeListener(String s, PropertyChangeListener l) {
			support.addPropertyChangeListener(s,l);
		}
		public void removePropertyChangeListener(PropertyChangeListener l) {
			support.removePropertyChangeListener(l);
		}
		public void removePropertyChangeListener(String s, PropertyChangeListener l) {
			support.removePropertyChangeListener(s,l);
		}

		void changeProperty(String name) {
			support.firePropertyChange(name,"old","new");
			
		}
	}

	class Listener implements PropertyChangeListener {
		Hashtable numEventsReceived = new Hashtable();
		int getNumEvents(String propertyName) {
			Integer i = (Integer)numEventsReceived.get(propertyName);
			try {
				return i.intValue();
			} catch(NullPointerException e) {
				return 0;
			}
		}

		public void propertyChange(PropertyChangeEvent e) {
			Integer i = (Integer)numEventsReceived.get(e.getPropertyName());
			try {
				int newI = i.intValue() + 1;
				numEventsReceived.put(e.getPropertyName(), new Integer(newI));
			} catch(NullPointerException exc) {
				numEventsReceived.put(e.getPropertyName(), new Integer(1));
			}
		}

		public void reset() {
			numEventsReceived = new Hashtable();
		}
	}

	public void setProperties(Source s) {
		s.changeProperty("foo");
		s.changeProperty("foo");
		s.changeProperty("foo");
		s.changeProperty("bar");
		s.changeProperty("bar");
		s.changeProperty("foobar");
	}

	public void shouldEqual(Listener l, int foo, int bar, int foobar, String testName) {
		String whatsWrong = "";
		if(l.getNumEvents("foo") != foo) {
			whatsWrong += ("foo(" + l.getNumEvents("foo") + ") != " + foo);
		}
		if(l.getNumEvents("bar") != bar) {
			whatsWrong += (" bar(" + l.getNumEvents("bar") + ") != " + bar);
		}
		if(l.getNumEvents("foobar") != foobar) {
			whatsWrong += (" foobar(" + l.getNumEvents("foobar") + ") != " + foobar);
		}

		if(!whatsWrong.equals("")) {
			System.out.println("FAILURE: " + testName + ": " + whatsWrong);
		} else {
			System.out.println("Success: " + testName);
		}
	}

	public void run() {
		Source s = new Source();

		/* Test: single multi-property adds */
		Listener l = new Listener();
		s.addPropertyChangeListener(l);
		setProperties(s);
		shouldEqual(l, 3, 2, 1, "single multi-property adds");

		/* Test: multiple listeners */
		Listener l2 = new Listener();
		s.addPropertyChangeListener(l2);
		setProperties(s);
		shouldEqual(l, 6, 4, 2, "multiple listeners-l");
		shouldEqual(l2, 3, 2, 1, "multiple listeners-l2");

		/* Test: multiple multi-property adds */
		s.addPropertyChangeListener(l);
		setProperties(s);
		shouldEqual(l, 12, 8, 4, "multiple multi-property adds-l");
		shouldEqual(l2, 6, 4, 2, "multiple multi-property adds-l2");

		/* Test: remove multi-property add */
		s.removePropertyChangeListener(l);
		setProperties(s);
		shouldEqual(l, 15, 10, 5, "remove multi-property add-l");
		shouldEqual(l2, 9, 6, 3, "remove multi-property add-l2");


		s.removePropertyChangeListener(l);
		s.removePropertyChangeListener(l2);
		l.reset();
		l2.reset();

		/* ENABLE THIS IF YOU THINK RESET ISN'T HAPPENING
		 shouldEqual(l, 0, 0, 0, "RESET-l");
		 shouldEqual(l2, 0, 0, 0, "RESET-l2");
		 setProperties(s);
		 shouldEqual(l, 0, 0, 0, "RESET_AGAIN-l");
		 shouldEqual(l2, 0, 0, 0, "RESET_AGAIN-l2");
		*/


		/* Test: single property listener */
		s.addPropertyChangeListener("foo", l);
		setProperties(s);
		shouldEqual(l, 3, 0, 0, "single property listener");

		/* Test: multiple different properties */
		s.addPropertyChangeListener("bar", l);
		setProperties(s);
		shouldEqual(l, 6, 2, 0, "multiple different properties");

		/* Test: multiple of same property */
		s.addPropertyChangeListener("foo", l);
		setProperties(s);
		shouldEqual(l, 12, 4, 0, "multiple of same property");

		/* Test: multiple single-property listeners */
		s.addPropertyChangeListener("foo", l2);
		setProperties(s);
		shouldEqual(l, 18, 6, 0, "multiple single-property listeners-l");
		shouldEqual(l2, 3, 0, 0, "multiple single-property listeners-l2");

		/* Test: remove single-property add */
		s.removePropertyChangeListener("foo", l);
		setProperties(s);
		shouldEqual(l, 21, 8, 0, "remove single-property add-l");
		shouldEqual(l2, 6, 0, 0, "remove single-property add-l2");


		s.removePropertyChangeListener("foo", l);
		s.removePropertyChangeListener("bar", l);
		s.removePropertyChangeListener("foo", l2);
		l.reset();
		l2.reset();

		/* ENABLE THIS IF YOU THINK RESET ISN'T HAPPENING
		 shouldEqual(l, 0, 0, 0, "RESET-l");
		 shouldEqual(l2, 0, 0, 0, "RESET-l2");
		 setProperties(s);
		 shouldEqual(l, 0, 0, 0, "RESET_AGAIN-l");
		 shouldEqual(l2, 0, 0, 0, "RESET_AGAIN-l2");
		*/

		/* Test: multiple- and single-property interaction */
		s.addPropertyChangeListener(l);
		s.addPropertyChangeListener("foo", l);
		setProperties(s);
		shouldEqual(l, 6, 2, 1, "multiple- and single-property interaction");

		/* Test: multiple- and single-property interaction: multiple-listener removal */
		s.removePropertyChangeListener(l);
		setProperties(s);
		shouldEqual(l, 9, 2, 1, "multiple- and single-property interaction: multiple-listener removal");

		/* Test: hasListeners() with multiple cases */
		if(s.support.hasListeners("foo")) {
			System.out.println("Success: hasListeners() returning true");
		} else {
			System.out.println("FAILURE: hasListeners() returning true");
		}
		if(s.support.hasListeners("bar")) {
			System.out.println("FAILURE: hasListeners() returning false");
		} else {
			System.out.println("Success: hasListeners() returning false");
		}
		s.addPropertyChangeListener(l);
		if(s.support.hasListeners("bar")) {
			System.out.println("Success: hasListeners() with all-event listeners");
		} else {
			System.out.println("FAILURE: hasListeners() with all-event listeners");
		}
	}

	public static void main(String[] args) {
		new PropertyChangeSupportTest().run();
	}
}