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 junit.framework.TestCase;
034:
035: import com.jgoodies.binding.BindingUtils;
036: import com.jgoodies.binding.adapter.ToggleButtonAdapter;
037: import com.jgoodies.binding.beans.PropertyAdapter;
038: import com.jgoodies.binding.tests.beans.TestBean;
039: import com.jgoodies.binding.tests.event.ChangeReport;
040: import com.jgoodies.binding.tests.event.ItemChangeReport;
041: import com.jgoodies.binding.tests.value.RejectingValueModel;
042: import com.jgoodies.binding.value.ValueHolder;
043: import com.jgoodies.binding.value.ValueModel;
044:
045: /**
046: * A test case for class {@link ToggleButtonAdapter}.
047: *
048: * @author Karsten Lentzsch
049: * @version $Revision: 1.10 $
050: */
051: public final class ToggleButtonAdapterTest extends TestCase {
052:
053: // Constructor Tests ******************************************************
054:
055: public void testConstructorRejectsNullSubject() {
056: try {
057: new ToggleButtonAdapter(null);
058: fail("ToggleButtonAdapter(ValueModel) failed to reject a null subject.");
059: } catch (NullPointerException ex) {
060: // The expected behavior
061: }
062: try {
063: new ToggleButtonAdapter(null, "one", "two");
064: fail("ToggleButtonAdapter(ValueModel, Object, Object) failed to reject a null subject.");
065: } catch (NullPointerException ex) {
066: // The expected behavior
067: }
068: }
069:
070: public void testConstructorRejectsIdenticalSelectionAndDeselectionValues() {
071: Object value = "selected";
072: try {
073: new ToggleButtonAdapter(new ValueHolder(), value, value);
074: fail("ToggleButtonAdapter(ValueModel, Object, Object) must reject identical values for selected and deselected.");
075: } catch (IllegalArgumentException ex) {
076: // The expected behavior
077: }
078: try {
079: new ToggleButtonAdapter(new ValueHolder(), new Integer(1),
080: new Integer(1));
081: fail("ToggleButtonAdapter(ValueModel, Object, Object) must reject equal values for selected and deselected.");
082: } catch (IllegalArgumentException ex) {
083: // The expected behavior
084: }
085: try {
086: new ToggleButtonAdapter(new ValueHolder(), null, null);
087: fail("ToggleButtonAdapter(ValueModel, Object, Object) must reject null for both the selected and deselected value.");
088: } catch (IllegalArgumentException ex) {
089: // The expected behavior
090: }
091: }
092:
093: // Basic Adapter Features *************************************************
094:
095: public void testAdaptsReadWriteBooleanProperty() {
096: TestBean bean = new TestBean();
097: bean.setReadWriteBooleanProperty(true);
098: ValueModel subject = new PropertyAdapter<TestBean>(bean,
099: "readWriteBooleanProperty", true);
100: ToggleButtonAdapter adapter = new ToggleButtonAdapter(subject);
101:
102: // Reading
103: assertTrue("Adapter is selected.", adapter.isSelected());
104:
105: bean.setReadWriteBooleanProperty(false);
106: assertFalse("Adapter is deselected.", adapter.isSelected());
107:
108: bean.setReadWriteBooleanProperty(true);
109: assertTrue("Adapter is selected again.", adapter.isSelected());
110:
111: // Writing
112: adapter.setSelected(false);
113: assertFalse("Adapted property is false.", bean
114: .isReadWriteBooleanProperty());
115:
116: adapter.setSelected(true);
117: assertTrue("Adapted property is true.", bean
118: .isReadWriteBooleanProperty());
119: }
120:
121: public void testReadWriteOperations() {
122: testReadWriteOperations(null, "one", "two");
123: testReadWriteOperations(null, null, "two");
124: testReadWriteOperations(null, "one", null);
125: testReadWriteOperations("one", "one", "two");
126: testReadWriteOperations("one", null, "two");
127: testReadWriteOperations("one", "one", null);
128: testReadWriteOperations("two", "one", "two");
129: testReadWriteOperations("two", null, "two");
130: testReadWriteOperations("two", "one", null);
131: }
132:
133: private void testReadWriteOperations(Object initialValue,
134: Object selectedValue, Object deselectedValue) {
135: ValueModel subject = new ValueHolder(initialValue);
136: ToggleButtonAdapter adapter = new ToggleButtonAdapter(subject,
137: selectedValue, deselectedValue);
138:
139: // Reading
140: assertEquals(
141: "Adapter selection reflects the initial subject value.",
142: BindingUtils.equals(initialValue, selectedValue),
143: adapter.isSelected());
144:
145: subject.setValue(selectedValue);
146: assertTrue("Adapter is selected again.", adapter.isSelected());
147:
148: subject.setValue(deselectedValue);
149: assertFalse("Adapter is deselected.", adapter.isSelected());
150:
151: // Writing
152: adapter.setSelected(true);
153: assertEquals("The subject value is the selected value.",
154: selectedValue, subject.getValue());
155:
156: adapter.setSelected(true);
157: assertEquals("The subject value is still the selected value.",
158: selectedValue, subject.getValue());
159:
160: adapter.setSelected(false);
161: assertEquals("The subject value is the deselected value.",
162: deselectedValue, subject.getValue());
163:
164: adapter.setSelected(false);
165: assertEquals(
166: "The subject value is still the deselected value.",
167: deselectedValue, subject.getValue());
168: }
169:
170: // Change and Item Events *************************************************
171:
172: public void testFiresChangeAndItemEvents() {
173: Object selectedValue = "selected";
174: Object deselectedValue = "deselected";
175: TestBean bean = new TestBean();
176: bean.setReadWriteObjectProperty(selectedValue);
177: ValueModel subject = new PropertyAdapter<TestBean>(bean,
178: "readWriteObjectProperty", true);
179: ToggleButtonAdapter adapter = new ToggleButtonAdapter(subject,
180: selectedValue, deselectedValue);
181:
182: ChangeReport changeReport = new ChangeReport();
183: ItemChangeReport itemChangeReport = new ItemChangeReport();
184: adapter.addChangeListener(changeReport);
185: adapter.addItemListener(itemChangeReport);
186:
187: bean.setReadWriteObjectProperty(deselectedValue);
188: assertEquals("Deselecting the property fires a ChangeEvent.",
189: 1, changeReport.eventCount());
190: assertEquals(
191: "Deselecting the property fires an ItemChangeEvent.",
192: 1, itemChangeReport.eventCount());
193: assertTrue("The last ItemChangeEvent indicates deselected.",
194: itemChangeReport.isLastStateChangeDeselected());
195:
196: bean.setReadWriteObjectProperty(selectedValue);
197: assertEquals(
198: "Selecting the property fires another ChangeEvent.", 2,
199: changeReport.eventCount());
200: assertEquals(
201: "Selecting the property fires another ItemChangeEvent.",
202: 2, itemChangeReport.eventCount());
203: assertTrue("The last ItemChangeEvent indicates selected.",
204: itemChangeReport.isLastStateChangeSelected());
205:
206: adapter.setSelected(false);
207: assertEquals(
208: "Deselecting the adapter fires a single ChangeEvent.",
209: 3, changeReport.eventCount());
210: assertEquals(
211: "Deselecting the adapter fires a single ItemChangeEvent.",
212: 3, itemChangeReport.eventCount());
213: assertTrue("The last ItemChangeEvent indicates deselected.",
214: itemChangeReport.isLastStateChangeDeselected());
215:
216: adapter.setSelected(true);
217: assertEquals(
218: "Selecting the adapter fires another ChangeEvent.", 4,
219: changeReport.eventCount());
220: assertEquals(
221: "Selecting the adapter fires another ItemChangeEvent.",
222: 4, itemChangeReport.eventCount());
223: assertTrue("The last ItemChangeEvent indicates selected.",
224: itemChangeReport.isLastStateChangeSelected());
225: }
226:
227: // Read-Only Model ********************************************************
228:
229: public void testAdaptsReadOnlyObjectProperty() {
230: Object selectedValue = "selected";
231: Object deselectedValue = "deselected";
232: TestBean bean = new TestBean();
233: bean.fireChangeOnReadOnlyObjectProperty(selectedValue);
234: ValueModel subject = new PropertyAdapter<TestBean>(bean,
235: "readOnlyObjectProperty", true);
236: ToggleButtonAdapter adapter = new ToggleButtonAdapter(subject,
237: selectedValue, deselectedValue);
238:
239: assertTrue("Adapter is selected.", adapter.isSelected());
240:
241: bean.fireChangeOnReadOnlyObjectProperty(deselectedValue);
242: assertFalse("Adapter is deselected.", adapter.isSelected());
243:
244: bean.fireChangeOnReadOnlyObjectProperty(selectedValue);
245: assertTrue("Adapter is selected again.", adapter.isSelected());
246: }
247:
248: // Re-Synchronizes if the Subject Change is Rejected **********************
249:
250: public void testResynchronizesAfterRejectedSubjectChange() {
251: ValueModel selectionHolder = new ValueHolder(Boolean.TRUE);
252: ValueModel rejectingValueHolder = new RejectingValueModel(
253: selectionHolder);
254: ToggleButtonAdapter adapter = new ToggleButtonAdapter(
255: rejectingValueHolder);
256:
257: assertTrue("Adapter is selected.", adapter.isSelected());
258: adapter.setSelected(false);
259: assertTrue("Adapter is still selected.", adapter.isSelected());
260: }
261:
262: }
|