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.DefaultListSelectionModel;
034: import javax.swing.ListSelectionModel;
035: import javax.swing.event.ListSelectionEvent;
036:
037: import junit.framework.TestCase;
038:
039: import com.jgoodies.binding.adapter.SingleListSelectionAdapter;
040: import com.jgoodies.binding.tests.event.ListSelectionReport;
041: import com.jgoodies.binding.value.ValueHolder;
042: import com.jgoodies.binding.value.ValueModel;
043:
044: /**
045: * A test case for class
046: * {@link com.jgoodies.binding.adapter.SingleListSelectionAdapter}.
047: *
048: * @author Karsten Lentzsch
049: * @author Jeanette Winzenburg
050: * @version $Revision: 1.12 $
051: */
052: public final class SingleListSelectionAdapterTest extends TestCase {
053:
054: /**
055: * Holds a sequence of indices used to test index changes.
056: */
057: private static final int[] INDICES = { 0, -1, -1, 3, 2, -1, 1, 2,
058: 3, 4, 0, -1 };
059:
060: /**
061: * Checks that changes to the underlying ValueModel update the adapter.
062: */
063: public void testAdapterReflectsModelChanges() {
064: ValueModel model = new ValueHolder();
065: SingleListSelectionAdapter adapter = new SingleListSelectionAdapter(
066: model);
067: for (int index : INDICES) {
068: model.setValue(new Integer(index));
069: assertEquals("New adapter index", index, adapter
070: .getMinSelectionIndex());
071: }
072: }
073:
074: /**
075: * Checks that changes to the adapter update the underlying ValueModel.
076: */
077: public void testModelReflectsAdapterChanges() {
078: ValueModel model = new ValueHolder();
079: SingleListSelectionAdapter adapter = new SingleListSelectionAdapter(
080: model);
081: for (int index : INDICES) {
082: adapter.setLeadSelectionIndex(index);
083: assertEquals("New model index", new Integer(index), model
084: .getValue());
085: }
086: }
087:
088: /**
089: * Checks that index changes of the underlying ValueModel are observed and
090: * reported by the adapter.<p>
091: *
092: * Wrong test: changeReport.getNew/OldValue() !=
093: * listReport.getFirst/LastIndex() if switching between selected/unselected
094: *
095: */
096: // public void testAdapterFiresModelEvents() {
097: //
098: // ValueModel model = new ValueHolder(-1);
099: // SingleListSelectionAdapter adapter = new SingleListSelectionAdapter(
100: // model);
101: // PropertyChangeReport changeReport = new PropertyChangeReport();
102: // ListSelectionReport listReport = new ListSelectionReport();
103: // model.addValueChangeListener(changeReport);
104: // adapter.addListSelectionListener(listReport);
105: // for (int i = 0; i < INDICES.length; i++) {
106: // int index = INDICES[i];
107: // model.setValue(new Integer(index));
108: // if (changeReport.hasEvents()) {
109: // Object oldValue = changeReport.lastEvent().getOldValue();
110: // Object newValue = changeReport.lastEvent().getNewValue();
111: // int oldIndex = (oldValue == null) ? -1 : ((Integer) oldValue)
112: // .intValue();
113: // int newIndex = (newValue == null) ? -1 : ((Integer) newValue)
114: // .intValue();
115: // int firstIndex = Math.min(oldIndex, newIndex);
116: // int lastIndex = Math.max(oldIndex, newIndex);
117: // assertEquals("Last model event index", new Integer(index),
118: // newValue);
119: // if (listReport.hasEvents()) {
120: // assertEquals("Wrong test...Last adapter event first index",
121: // firstIndex, listReport.lastEvent().getFirstIndex());
122: // assertEquals("Wrong test...Last adapter event last index",
123: // lastIndex, listReport.lastEvent().getLastIndex());
124: // }
125: // }
126: // }
127: //
128: // // Check that the model and adapter throw the same number of events.
129: // assertEquals("Event count", changeReport.eventCount(), listReport
130: // .eventCount());
131: // }
132:
133: /**
134: * Checks for correct SelectionEvent on empty --> selected. (an index of -1
135: * is invalid because it does not represent a position in the list that
136: * might have changed selection state).
137: */
138: public void testSelectEvent() {
139: ValueModel model = new ValueHolder(-1);
140: SingleListSelectionAdapter adapter = new SingleListSelectionAdapter(
141: model);
142: ListSelectionReport report = new ListSelectionReport();
143: adapter.addListSelectionListener(report);
144: adapter.setSelectionInterval(2, 2);
145: ListSelectionEvent e = report.lastEvent();
146: assertEquals("first must be index of changed selection ", 2, e
147: .getFirstIndex());
148: }
149:
150: /**
151: * Checks for correct SelectionEvent on selected --> empty.
152: */
153: public void testDeSelectEvent() {
154: ValueModel model = new ValueHolder(2);
155: SingleListSelectionAdapter adapter = new SingleListSelectionAdapter(
156: model);
157: ListSelectionReport report = new ListSelectionReport();
158: adapter.addListSelectionListener(report);
159: adapter.clearSelection();
160: ListSelectionEvent e = report.lastEvent();
161: assertEquals("first must be index of changed selection ", 2, e
162: .getFirstIndex());
163: }
164:
165: public void testChangeSelectEvent() {
166: ValueModel model = new ValueHolder(2);
167: SingleListSelectionAdapter adapter = new SingleListSelectionAdapter(
168: model);
169: ListSelectionReport report = new ListSelectionReport();
170: adapter.addListSelectionListener(report);
171: adapter.setSelectionInterval(3, 3);
172: ListSelectionEvent e = report.lastEvent();
173: assertEquals("first must be lower index of changed selection ",
174: 2, e.getFirstIndex());
175: assertEquals("last must be upper index of changed selection ",
176: 3, e.getLastIndex());
177:
178: }
179:
180: public void testIsSelectedIndex() {
181: ListSelectionModel model = getSelectionAdapter(-1);
182: assertTrue("selection must return empty", model
183: .isSelectionEmpty());
184: assertFalse("isSelectedIndex(-1) must return false", model
185: .isSelectedIndex(-1));
186: }
187:
188: public void testRemoveSelectionInterval() {
189: ListSelectionModel adapter = getSelectionAdapter(1);
190: adapter.removeSelectionInterval(0, 2);
191: assertTrue("selection must be empty ", adapter
192: .isSelectionEmpty());
193: }
194:
195: public void testAddSelectionInterval() {
196: ListSelectionModel adapter = getSelectionAdapter(-1);
197: try {
198: adapter.addSelectionInterval(0, 2);
199: } catch (UnsupportedOperationException ex) {
200: fail("adapter must not fire on legal operation" + ex);
201: }
202:
203: }
204:
205: //--------------------- insert/removeIndexInterval
206:
207: public void testInsertIndexIntervalBefore() {
208: ListSelectionModel adapter = getSelectionAdapter(2);
209: adapter.insertIndexInterval(0, 1, false);
210: assertEquals("selectionindex must be increased by 1", 3,
211: adapter.getMinSelectionIndex());
212:
213: }
214:
215: public void testInsertIndexIntervalAfter() {
216: ListSelectionModel adapter = getSelectionAdapter(2);
217: adapter.insertIndexInterval(3, 1, false);
218: assertEquals("selectionindex must be unchanged", 2, adapter
219: .getMinSelectionIndex());
220: }
221:
222: public void testRemoveIndexIntervalAfter() {
223: ListSelectionModel adapter = getSelectionAdapter(2);
224: adapter.removeIndexInterval(3, 3);
225: assertEquals("selectionindex must be unchanged", 2, adapter
226: .getMinSelectionIndex());
227:
228: }
229:
230: public void testRemoveIndexIntervalBefore() {
231: ListSelectionModel adapter = getSelectionAdapter(2);
232: adapter.removeIndexInterval(0, 0);
233: assertEquals("selectionindex must be decreased by 1", 1,
234: adapter.getMinSelectionIndex());
235:
236: }
237:
238: public void testRemoveIndexIntervalOn() {
239: ListSelectionModel model = getSelectionAdapter(2);
240: model.removeIndexInterval(2, 2);
241: assertTrue("selection must be empty", model.isSelectionEmpty());
242:
243: }
244:
245: public void testLead() {
246: ListSelectionModel model = getSelectionAdapter(-1);
247: model.setSelectionInterval(0, 2);
248: assertEquals("second parameter must be lead", 2, model
249: .getLeadSelectionIndex());
250: }
251:
252: private ListSelectionModel getSelectionAdapter(int index) {
253: ValueModel model = new ValueHolder(index);
254: SingleListSelectionAdapter adapter = new SingleListSelectionAdapter(
255: model);
256: return adapter;
257: }
258:
259: //---------------- testing reference implementation
260:
261: public void testReferenceInsertIndexIntervalBefore() {
262: ListSelectionModel model = createReferenceSelection(2);
263: model.insertIndexInterval(0, 1, false);
264: assertEquals("selectionindex must be increased by 1", 3, model
265: .getMinSelectionIndex());
266: }
267:
268: public void testReferenceInsertIndexIntervalAfter() {
269: ListSelectionModel model = createReferenceSelection(2);
270: model.insertIndexInterval(3, 1, false);
271: assertEquals("selectionindex must be unchanged", 2, model
272: .getMinSelectionIndex());
273: }
274:
275: public void testReferenceRemoveIndexIntervalBefore() {
276: ListSelectionModel model = createReferenceSelection(2);
277: model.removeIndexInterval(0, 0);
278: assertEquals("selectionindex must be decreased by 1", 1, model
279: .getMinSelectionIndex());
280: }
281:
282: public void testReferenceRemoveIndexIntervalAfter() {
283: ListSelectionModel model = createReferenceSelection(2);
284: model.removeIndexInterval(3, 3);
285: assertEquals("selectionindex must be unchanged", 2, model
286: .getMinSelectionIndex());
287:
288: }
289:
290: public void testReferenceRemoveIndexIntervalOn() {
291: ListSelectionModel model = createReferenceSelection(2);
292: model.removeIndexInterval(2, 2);
293: assertTrue("selection must be empty", model.isSelectionEmpty());
294:
295: }
296:
297: public void testReferenceIsSelectedIndex() {
298: ListSelectionModel model = createReferenceSelection(-1);
299: assertTrue("selection must be empty", model.isSelectionEmpty());
300: assertFalse("isSelectedIndex(-1) must return false", model
301: .isSelectedIndex(-1));
302: }
303:
304: public void testReferenceRemoveSelectionInterval() {
305: ListSelectionModel adapter = createReferenceSelection(1);
306: adapter.removeSelectionInterval(0, 2);
307: assertTrue("selection must be empty ", adapter
308: .isSelectionEmpty());
309: }
310:
311: public void testReferenceAddSelectionInterval() {
312: ListSelectionModel adapter = createReferenceSelection(-1);
313: try {
314: adapter.addSelectionInterval(0, 2);
315: } catch (UnsupportedOperationException ex) {
316: fail("adapter must not fire on legal operation" + ex);
317: }
318: }
319:
320: public void testReferenceLead() {
321: ListSelectionModel model = createReferenceSelection(-1);
322: model.setSelectionInterval(0, 2);
323: assertEquals("second parameter must be lead", 2, model
324: .getLeadSelectionIndex());
325: }
326:
327: private ListSelectionModel createReferenceSelection(int i) {
328: ListSelectionModel model = new DefaultListSelectionModel();
329: model.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
330: model.setSelectionInterval(i, i);
331: return model;
332: }
333:
334: }
|