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.PropertyChangeSupport;
034: import java.util.ArrayList;
035: import java.util.List;
036:
037: import junit.framework.TestCase;
038:
039: import com.jgoodies.binding.beans.ExtendedPropertyChangeSupport;
040: import com.jgoodies.binding.tests.event.PropertyChangeReport;
041:
042: /**
043: * A test case for class {@link ExtendedPropertyChangeSupport}.<p>
044: *
045: * TODO: Test multicast events.
046: *
047: * @author <a href="mailto:neuling@dakosy.de">Mattias Neuling</a>
048: * @author Karsten Lentzsch
049: * @version $Revision: 1.10 $
050: */
051: public final class ExtendedPropertyChangeSupportTest extends TestCase {
052:
053: private String one;
054: private String two;
055: private List<String> emptyList1;
056: private List<String> emptyList2;
057: private List<String> list1a;
058: private List<String> list1b;
059:
060: /**
061: * @throws Exception in case of an unexpected problem
062: */
063: @Override
064: protected void setUp() throws Exception {
065: super .setUp();
066: one = "one";
067: two = "two";
068: emptyList1 = new ArrayList<String>();
069: emptyList2 = new ArrayList<String>();
070: list1a = new ArrayList<String>();
071: list1a.add(one);
072: list1b = new ArrayList<String>();
073: list1b.add(one);
074: }
075:
076: /**
077: * @throws Exception in case of an unexpected problem
078: */
079: @Override
080: protected void tearDown() throws Exception {
081: super .tearDown();
082: emptyList1 = null;
083: emptyList2 = null;
084: list1a = null;
085: list1b = null;
086: }
087:
088: public void testCommonBehavior() {
089: testCommon("name1", null, null, null);
090: testCommon("name1", null, null, one);
091: testCommon("name1", null, one, null);
092: testCommon("name1", null, one, one);
093: testCommon("name1", null, one, two);
094: testCommon("name1", null, emptyList1, emptyList2);
095: testCommon("name1", null, list1a, list1b);
096:
097: testCommon("name1", "name1", null, null);
098: testCommon("name1", "name1", null, one);
099: testCommon("name1", "name1", one, null);
100: testCommon("name1", "name1", one, one);
101: testCommon("name1", "name1", one, two);
102: testCommon("name1", "name1", emptyList1, emptyList2);
103: testCommon("name1", "name1", list1a, list1b);
104:
105: testCommon("name1", "name2", null, null);
106: testCommon("name1", "name2", null, one);
107: testCommon("name1", "name2", one, null);
108: testCommon("name1", "name2", one, one);
109: testCommon("name1", "name2", one, two);
110: testCommon("name1", "name2", emptyList1, emptyList2);
111: testCommon("name1", "name2", list1a, list1b);
112: }
113:
114: public void testDifferences() {
115: testDifference("name1", "name1", emptyList1, emptyList2);
116: testDifference("name1", "name1", list1a, list1b);
117: }
118:
119: private void testCommon(String observedPropertyName,
120: String changedPropertyName, Object oldValue, Object newValue) {
121: fireAndCount(true, observedPropertyName, changedPropertyName,
122: oldValue, newValue, false, false);
123: }
124:
125: private void testDifference(String observedPropertyName,
126: String changedPropertyName, Object oldValue, Object newValue) {
127: fireAndCount(false, observedPropertyName, changedPropertyName,
128: oldValue, newValue, true, false);
129: fireAndCount(false, observedPropertyName, changedPropertyName,
130: oldValue, newValue, false, true);
131: }
132:
133: private void fireAndCount(boolean countsShallBeEqual,
134: String observedPropertyName, String changedPropertyName,
135: Object oldValue, Object newValue,
136: boolean checkIdentityDefault, boolean checkIdentity) {
137: PropertyChangeReport epcsNamedCounter = new PropertyChangeReport();
138: PropertyChangeReport epcsMulticastCounter = new PropertyChangeReport();
139: PropertyChangeReport pcsNamedCounter = new PropertyChangeReport();
140: PropertyChangeReport pcsMulticastCounter = new PropertyChangeReport();
141:
142: ExtendedPropertyChangeSupport epcs = new ExtendedPropertyChangeSupport(
143: this , checkIdentityDefault);
144: PropertyChangeSupport pcs = new PropertyChangeSupport(this );
145:
146: epcs.addPropertyChangeListener(observedPropertyName,
147: epcsNamedCounter);
148: epcs.addPropertyChangeListener(epcsMulticastCounter);
149: pcs.addPropertyChangeListener(observedPropertyName,
150: pcsNamedCounter);
151: pcs.addPropertyChangeListener(pcsMulticastCounter);
152:
153: if (checkIdentity) {
154: epcs.firePropertyChange(changedPropertyName, oldValue,
155: newValue, true);
156: } else {
157: epcs.firePropertyChange(changedPropertyName, oldValue,
158: newValue);
159: }
160: pcs.firePropertyChange(changedPropertyName, oldValue, newValue);
161:
162: boolean namedCountersAreEqual = pcsNamedCounter.eventCount() == epcsNamedCounter
163: .eventCount();
164:
165: boolean multicastCountersAreEqual = pcsMulticastCounter
166: .eventCount() == epcsMulticastCounter.eventCount();
167:
168: if (countsShallBeEqual) {
169: assertTrue("Named counters shall be equal",
170: namedCountersAreEqual);
171: assertTrue("Multicast counters shall be equal",
172: multicastCountersAreEqual);
173: } else {
174: assertFalse("Named counters shall differ",
175: namedCountersAreEqual);
176: assertFalse("Multicast counters shall differ",
177: multicastCountersAreEqual);
178: }
179: }
180:
181: }
|