001: /*
002: * Copyright (c) 2002-2007 JGoodies Karsten Lentzsch. All Rights Reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * o Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * o Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * o Neither the name of JGoodies Karsten Lentzsch nor the names of
015: * its contributors may be used to endorse or promote products derived
016: * from this software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
020: * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
021: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
022: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
023: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
025: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
026: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
027: * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
028: * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029: */
030:
031: package com.jgoodies.binding.tests;
032:
033: import java.beans.PropertyChangeListener;
034:
035: import junit.framework.TestCase;
036:
037: import com.jgoodies.binding.PresentationModel;
038: import com.jgoodies.binding.beans.BeanAdapter;
039: import com.jgoodies.binding.beans.IndirectPropertyChangeSupport;
040: import com.jgoodies.binding.tests.beans.EquityTestBean;
041: import com.jgoodies.binding.tests.beans.TestBean;
042: import com.jgoodies.binding.tests.event.PropertyChangeReport;
043: import com.jgoodies.binding.value.Trigger;
044:
045: /**
046: * A test case for class {@link IndirectPropertyChangeSupport}
047: * that checks whether listeners are added, removed and re-registered properly.
048: * Also tests the classes BeanAdapter and PresentationModel that use
049: * the IndirectPropertyChangeSupport directly or indirectly.
050: *
051: * @author Karsten Lentzsch
052: * @version $Revision: 1.13 $
053: */
054: public final class IndirectPropertyChangeSupportTest extends TestCase {
055:
056: // Re-Registering Listeners on Different Bean Types ***********************
057:
058: public void testReregisterListenerOnDifferentBeans() {
059: testReregisterListener(new EquityTestBean("key1"),
060: new EquityTestBean("key2"),
061: new IndirectPropertyChangeSupport());
062:
063: TestBean bean1 = new EquityTestBean("key1");
064: testReregisterListener(bean1, new EquityTestBean("key2"),
065: new IndirectPropertyChangeSupport(bean1));
066: }
067:
068: public void testReregisterListenerOnEqualBeans() {
069: testReregisterListener(new EquityTestBean("key1"),
070: new EquityTestBean("key1"),
071: new IndirectPropertyChangeSupport());
072:
073: TestBean bean1 = new EquityTestBean("key1");
074: testReregisterListener(bean1, new EquityTestBean("key1"),
075: new IndirectPropertyChangeSupport(bean1));
076: }
077:
078: public void testBeanAdapterReregisterListenerOnEqualBeans() {
079: TestBean bean1 = new EquityTestBean("key1");
080: testReregisterListener(bean1, new EquityTestBean("key1"),
081: new BeanAdapter<TestBean>(bean1));
082:
083: bean1 = new EquityTestBean("key1");
084: testReregisterListener(bean1, new EquityTestBean("key1"),
085: new BeanAdapter<TestBean>(bean1, false));
086: }
087:
088: public void testPresentationModelReregisterListenerOnEqualBeans() {
089: TestBean bean1 = new EquityTestBean("key1");
090: testReregisterListener(bean1, new EquityTestBean("key1"),
091: new PresentationModel<TestBean>(bean1));
092:
093: bean1 = new EquityTestBean("key1");
094: testReregisterListener(bean1, new EquityTestBean("key1"),
095: new PresentationModel<TestBean>(bean1, new Trigger()));
096: }
097:
098: // Reusable Basic Test ****************************************************
099:
100: private void testReregisterListener(TestBean bean1, TestBean bean2,
101: Object beanHolder) {
102: String propertyName = "readWriteObjectProperty";
103: setBean(beanHolder, bean1);
104: PropertyChangeReport report1a = new PropertyChangeReport();
105: PropertyChangeReport report1b = new PropertyChangeReport();
106: PropertyChangeReport report2a = new PropertyChangeReport();
107: PropertyChangeReport report2b = new PropertyChangeReport();
108: addIndirectListener(beanHolder, report1a);
109: addIndirectListener(beanHolder, report1b);
110: addIndirectListener(beanHolder, propertyName, report2a);
111: addIndirectListener(beanHolder, propertyName, report2b);
112: bean1.setReadWriteObjectProperty("bean1value1");
113: assertEquals("Changing the observed bean1 fires a change.", 1,
114: report1a.eventCount());
115: assertEquals(
116: "All unnamed listeners receive the same number of events.",
117: report1a.eventCount(), report1b.eventCount());
118: assertEquals(
119: "Changing the observed bean1 fires a named change.", 1,
120: report2a.eventCount());
121: assertEquals(
122: "All named listeners receive the same number of events.",
123: report2a.eventCount(), report2b.eventCount());
124:
125: bean2.setReadWriteObjectProperty("bean2value1");
126: assertEquals("Changing the unobserved bean2 fires no change.",
127: 1, report1a.eventCount());
128: assertEquals(
129: "Changing the unobserved bean2 fires no named change.",
130: 1, report1a.eventCount());
131:
132: // Change the bean
133: setBean(beanHolder, bean2);
134: bean1.setReadWriteObjectProperty("bean1value2");
135: assertEquals("Changing the unobserved bean1 fires no change.",
136: 1, report1a.eventCount());
137: assertEquals(
138: "Changing the unobserved bean1 fires no named change.",
139: 1, report2a.eventCount());
140:
141: bean2.setReadWriteObjectProperty("bean2value2");
142: assertEquals("Changing the observed bean2 fires a change.", 2,
143: report1a.eventCount());
144: assertEquals(
145: "2) All unnamed listeners receive the same number of events.",
146: report1a.eventCount(), report1b.eventCount());
147: assertEquals("Changing the observed bean2 fires a change.", 2,
148: report2a.eventCount());
149: assertEquals(
150: "2) All named listeners receive the same number of events.",
151: report2a.eventCount(), report2b.eventCount());
152:
153: removeIndirectListener(beanHolder, report1a);
154: removeIndirectListener(beanHolder, report1b);
155: removeIndirectListener(beanHolder, propertyName, report2a);
156: removeIndirectListener(beanHolder, propertyName, report2b);
157:
158: // Change the bean back to bean1
159: setBean(beanHolder, bean1);
160: bean1.setReadWriteObjectProperty("bean1value3");
161: assertEquals("Unregistered listeners receive no more events.",
162: 2, report1a.eventCount());
163: assertEquals(
164: "All unregistered listeners receive no more events.",
165: report1a.eventCount(), report1b.eventCount());
166: assertEquals(
167: "Deregistered named listeners receive no more events.",
168: 2, report2a.eventCount());
169: assertEquals(
170: "All deregistered named listeners receive no more events.",
171: report2a.eventCount(), report2b.eventCount());
172:
173: }
174:
175: // Helper Code ***********************************************************
176:
177: private void setBean(Object beanHolder, TestBean newBean) {
178: if (beanHolder instanceof IndirectPropertyChangeSupport)
179: ((IndirectPropertyChangeSupport) beanHolder)
180: .setBean(newBean);
181: else if (beanHolder instanceof BeanAdapter)
182: ((BeanAdapter<TestBean>) beanHolder).setBean(newBean);
183: else if (beanHolder instanceof PresentationModel)
184: ((PresentationModel<TestBean>) beanHolder).setBean(newBean);
185: else
186: throw new IllegalArgumentException(
187: "Unknown bean holder type. "
188: + "Must be one of: IndirectPropertyChangeSupport, BeanAdapter, PresentationModel");
189: }
190:
191: private void addIndirectListener(Object beanHolder,
192: PropertyChangeListener listener) {
193: if (beanHolder instanceof IndirectPropertyChangeSupport)
194: ((IndirectPropertyChangeSupport) beanHolder)
195: .addPropertyChangeListener(listener);
196: else if (beanHolder instanceof BeanAdapter)
197: ((BeanAdapter<?>) beanHolder)
198: .addBeanPropertyChangeListener(listener);
199: else if (beanHolder instanceof PresentationModel)
200: ((PresentationModel<?>) beanHolder)
201: .addBeanPropertyChangeListener(listener);
202: else
203: throw new IllegalArgumentException(
204: "Unknown bean holder type. "
205: + "Must be one of: IndirectPropertyChangeSupport, BeanAdapter, PresentationModel");
206: }
207:
208: private void addIndirectListener(Object beanHolder,
209: String propertyName, PropertyChangeListener listener) {
210: if (beanHolder instanceof IndirectPropertyChangeSupport)
211: ((IndirectPropertyChangeSupport) beanHolder)
212: .addPropertyChangeListener(propertyName, listener);
213: else if (beanHolder instanceof BeanAdapter)
214: ((BeanAdapter<?>) beanHolder)
215: .addBeanPropertyChangeListener(propertyName,
216: listener);
217: else if (beanHolder instanceof PresentationModel)
218: ((PresentationModel<?>) beanHolder)
219: .addBeanPropertyChangeListener(propertyName,
220: listener);
221: else
222: throw new IllegalArgumentException(
223: "Unknown bean holder type. "
224: + "Must be one of: IndirectPropertyChangeSupport, BeanAdapter, PresentationModel");
225: }
226:
227: private void removeIndirectListener(Object beanHolder,
228: PropertyChangeListener listener) {
229: if (beanHolder instanceof IndirectPropertyChangeSupport)
230: ((IndirectPropertyChangeSupport) beanHolder)
231: .removePropertyChangeListener(listener);
232: else if (beanHolder instanceof BeanAdapter)
233: ((BeanAdapter<?>) beanHolder)
234: .removeBeanPropertyChangeListener(listener);
235: else if (beanHolder instanceof PresentationModel)
236: ((PresentationModel<?>) beanHolder)
237: .removeBeanPropertyChangeListener(listener);
238: else
239: throw new IllegalArgumentException(
240: "Unknown bean holder type. "
241: + "Must be one of: IndirectPropertyChangeSupport, BeanAdapter, PresentationModel");
242: }
243:
244: private void removeIndirectListener(Object beanHolder,
245: String propertyName, PropertyChangeListener listener) {
246: if (beanHolder instanceof IndirectPropertyChangeSupport)
247: ((IndirectPropertyChangeSupport) beanHolder)
248: .removePropertyChangeListener(propertyName,
249: listener);
250: else if (beanHolder instanceof BeanAdapter)
251: ((BeanAdapter<?>) beanHolder)
252: .removeBeanPropertyChangeListener(propertyName,
253: listener);
254: else if (beanHolder instanceof PresentationModel)
255: ((PresentationModel<?>) beanHolder)
256: .removeBeanPropertyChangeListener(propertyName,
257: listener);
258: else
259: throw new IllegalArgumentException(
260: "Unknown bean holder type. "
261: + "Must be one of: IndirectPropertyChangeSupport, BeanAdapter, PresentationModel");
262: }
263:
264: }
|