001: /*
002: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
003: * Distributed under the terms of either:
004: * - the common development and distribution license (CDDL), v1.0; or
005: * - the GNU Lesser General Public License, v2.1 or later
006: * $Id: TestAbstractPropertyChangeSupport.java 3634 2007-01-08 21:42:24Z gbevin $
007: */
008: package com.uwyn.rife.tools;
009:
010: import com.uwyn.rife.tools.exceptions.SourceBeanRequiredException;
011: import java.beans.PropertyChangeEvent;
012: import java.beans.PropertyChangeListener;
013: import junit.framework.TestCase;
014:
015: public class TestAbstractPropertyChangeSupport extends TestCase {
016: public TestAbstractPropertyChangeSupport(String name) {
017: super (name);
018: }
019:
020: public void testNoInitialListeners() {
021: PropertyChangeTest test = new PropertyChangeTest();
022:
023: assertEquals(0, test.getPropertyChangeListeners().length);
024:
025: assertEquals(0,
026: test.getPropertyChangeListeners("string").length);
027: assertEquals(0, test.getPropertyChangeListeners("int").length);
028: assertEquals(0,
029: test.getPropertyChangeListeners("boolean").length);
030:
031: assertFalse(test.hasListeners("string"));
032: assertFalse(test.hasListeners("int"));
033: assertFalse(test.hasListeners("boolean"));
034: }
035:
036: public void testNoSource() {
037: PropertyChangeTest test = new PropertyChangeTest(false);
038: PropertyChangeHandler handler = new PropertyChangeHandler();
039: try {
040: test.addPropertyChangeListener(handler);
041: fail();
042: } catch (SourceBeanRequiredException e) {
043: assertSame(e.getBeanClass(), PropertyChangeTest.class);
044: }
045: }
046:
047: public void testListeners() {
048: PropertyChangeTest test = new PropertyChangeTest();
049:
050: PropertyChangeHandler handler1 = new PropertyChangeHandler();
051: PropertyChangeHandler handler2 = new PropertyChangeHandler();
052: PropertyChangeHandler handler3 = new PropertyChangeHandler();
053: PropertyChangeHandler handler4 = new PropertyChangeHandler();
054: PropertyChangeHandler handler5 = new PropertyChangeHandler();
055: PropertyChangeHandler handler6 = new PropertyChangeHandler();
056: PropertyChangeHandler handler7 = new PropertyChangeHandler();
057:
058: test.addPropertyChangeListener(handler1);
059: test.addPropertyChangeListener("string", handler2);
060: test.addPropertyChangeListener("int", handler3);
061: test.addPropertyChangeListener("int", handler4);
062: test.addPropertyChangeListener("boolean", handler5);
063: test.addPropertyChangeListener("boolean", handler6);
064: test.addPropertyChangeListener("boolean", handler7);
065:
066: assertEquals(7, test.getPropertyChangeListeners().length);
067: assertEquals(1,
068: test.getPropertyChangeListeners("string").length);
069: assertEquals(2, test.getPropertyChangeListeners("int").length);
070: assertEquals(3,
071: test.getPropertyChangeListeners("boolean").length);
072:
073: assertTrue(test.hasListeners("string"));
074: assertTrue(test.hasListeners("int"));
075: assertTrue(test.hasListeners("boolean"));
076:
077: test.removePropertyChangeListener(handler1);
078: test.removePropertyChangeListener("string", handler2);
079: test.removePropertyChangeListener("int", handler3);
080: test.removePropertyChangeListener("int", handler4);
081: test.removePropertyChangeListener("boolean", handler5);
082: test.removePropertyChangeListener("boolean", handler6);
083: test.removePropertyChangeListener("boolean", handler7);
084:
085: assertEquals(0, test.getPropertyChangeListeners().length);
086:
087: assertEquals(0,
088: test.getPropertyChangeListeners("string").length);
089: assertEquals(0, test.getPropertyChangeListeners("int").length);
090: assertEquals(0,
091: test.getPropertyChangeListeners("boolean").length);
092:
093: assertFalse(test.hasListeners("string"));
094: assertFalse(test.hasListeners("int"));
095: assertFalse(test.hasListeners("boolean"));
096: }
097:
098: public void testFirePropertyChange() {
099: PropertyChangeTest test = new PropertyChangeTest();
100:
101: PropertyChangeHandler handler1 = new PropertyChangeHandler();
102: PropertyChangeHandler handler2 = new PropertyChangeHandler();
103: PropertyChangeHandler handler3 = new PropertyChangeHandler();
104: PropertyChangeHandler handler4 = new PropertyChangeHandler();
105:
106: test.addPropertyChangeListener(handler1);
107: test.addPropertyChangeListener("string", handler2);
108: test.addPropertyChangeListener("int", handler3);
109: test.addPropertyChangeListener("boolean", handler4);
110:
111: assertNull(handler1.getEvent());
112: assertNull(handler2.getEvent());
113: assertNull(handler3.getEvent());
114: assertNull(handler4.getEvent());
115:
116: PropertyChangeEvent handler1_event = null;
117: PropertyChangeEvent handler2_event = null;
118: PropertyChangeEvent handler3_event = null;
119: PropertyChangeEvent handler4_event = null;
120:
121: test.setString("one");
122:
123: assertNotNull(handler1.getEvent());
124: handler1_event = handler1.getEvent();
125: assertEquals("string", handler1_event.getPropertyName());
126: assertNull(handler1_event.getOldValue());
127: assertEquals("one", handler1_event.getNewValue());
128:
129: assertNotNull(handler2.getEvent());
130: handler2_event = handler2.getEvent();
131: assertEquals("string", handler2_event.getPropertyName());
132: assertNull(handler2_event.getOldValue());
133: assertEquals("one", handler2_event.getNewValue());
134:
135: test.setInt(2);
136:
137: assertNotNull(handler1.getEvent());
138: assertNotSame(handler1_event, handler1.getEvent());
139: handler1_event = handler1.getEvent();
140: assertEquals("int", handler1_event.getPropertyName());
141: assertEquals(-1, handler1_event.getOldValue());
142: assertEquals(2, handler1_event.getNewValue());
143:
144: assertSame(handler2_event, handler2.getEvent());
145:
146: assertNotNull(handler3.getEvent());
147: handler3_event = handler3.getEvent();
148: assertEquals("int", handler3_event.getPropertyName());
149: assertEquals(-1, handler3_event.getOldValue());
150: assertEquals(2, handler3_event.getNewValue());
151:
152: test.setBoolean(true);
153:
154: assertNotNull(handler1.getEvent());
155: assertNotSame(handler1_event, handler1.getEvent());
156: handler1_event = handler1.getEvent();
157: assertEquals("boolean", handler1_event.getPropertyName());
158: assertEquals(false, handler1_event.getOldValue());
159: assertEquals(true, handler1_event.getNewValue());
160:
161: assertSame(handler2_event, handler2.getEvent());
162: assertSame(handler3_event, handler3.getEvent());
163:
164: assertNotNull(handler4.getEvent());
165: handler4_event = handler4.getEvent();
166: assertEquals("boolean", handler4_event.getPropertyName());
167: assertEquals(false, handler4_event.getOldValue());
168: assertEquals(true, handler4_event.getNewValue());
169:
170: test.customEvent();
171:
172: assertNotNull(handler1.getEvent());
173: assertNotSame(handler1_event, handler1.getEvent());
174: handler1_event = handler1.getEvent();
175: assertEquals("blah", handler1_event.getPropertyName());
176: assertEquals("first", handler1_event.getOldValue());
177: assertEquals("second", handler1_event.getNewValue());
178:
179: assertSame(handler2_event, handler2.getEvent());
180: assertSame(handler3_event, handler3.getEvent());
181: assertSame(handler4_event, handler4.getEvent());
182: }
183:
184: public void testClone() {
185: PropertyChangeTest test1 = new PropertyChangeTest();
186: test1.setInt(1);
187:
188: PropertyChangeHandler handler = new PropertyChangeHandler();
189: test1.addPropertyChangeListener(handler);
190: assertEquals(1, test1.getPropertyChangeListeners().length);
191:
192: PropertyChangeTest test2 = (PropertyChangeTest) test1.clone();
193: assertEquals(0, test2.getPropertyChangeListeners().length);
194: }
195: }
196:
197: class PropertyChangeTest extends AbstractPropertyChangeSupport {
198: private String mString = null;
199: private int mInt = -1;
200: private boolean mBoolean = false;
201:
202: public PropertyChangeTest() {
203: this (true);
204: }
205:
206: public PropertyChangeTest(boolean registerSource) {
207: if (registerSource) {
208: setSource(this );
209: }
210: }
211:
212: public void customEvent() {
213: firePropertyChange(new PropertyChangeEvent(this , "blah",
214: "first", "second"));
215: }
216:
217: public void setString(String value) {
218: String old = mString;
219:
220: mString = value;
221:
222: firePropertyChange("string", old, value);
223: }
224:
225: public String getString() {
226: return mString;
227: }
228:
229: public void setInt(int value) {
230: int old = mInt;
231:
232: mInt = value;
233:
234: firePropertyChange("int", old, value);
235: }
236:
237: public int getInt() {
238: return mInt;
239: }
240:
241: public void setBoolean(boolean value) {
242: boolean old = mBoolean;
243:
244: mBoolean = value;
245:
246: firePropertyChange("boolean", old, value);
247: }
248:
249: public boolean isBoolean() {
250: return mBoolean;
251: }
252: }
253:
254: class PropertyChangeHandler implements PropertyChangeListener {
255: private PropertyChangeEvent mEvent = null;
256:
257: public void propertyChange(PropertyChangeEvent event) {
258: mEvent = event;
259: }
260:
261: public PropertyChangeEvent getEvent() {
262: return mEvent;
263: }
264: }
|