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