001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Anton Avtamonov
019: * @version $Revision$
020: */package javax.swing;
021:
022: import java.util.ArrayList;
023: import java.util.List;
024: import java.util.Vector;
025: import javax.swing.event.ListDataEvent;
026: import javax.swing.event.ListDataListener;
027:
028: public class DefaultComboBoxModelTest extends SwingTestCase {
029: private DefaultComboBoxModel model;
030:
031: private TestListener listener;
032:
033: public DefaultComboBoxModelTest(final String name) {
034: super (name);
035: }
036:
037: @Override
038: protected void setUp() throws Exception {
039: model = new DefaultComboBoxModel();
040: listener = new TestListener();
041: model.addListDataListener(listener);
042: }
043:
044: @Override
045: protected void tearDown() throws Exception {
046: model = null;
047: listener = null;
048: }
049:
050: public void testDefaultComboBoxModel() throws Exception {
051: assertEquals(0, model.getSize());
052: assertNull(model.getSelectedItem());
053: assertEquals(0, listener.getEvents().size());
054: Object[] arrayData = new Object[] { "1", "2", "3" };
055: model = new DefaultComboBoxModel(arrayData);
056: assertEquals("1", model.getSelectedItem());
057: assertEquals(arrayData.length, model.getSize());
058: assertEquals("2", model.getElementAt(1));
059: arrayData[1] = "21";
060: assertEquals("2", model.getElementAt(1));
061: Vector<String> vectorData = new Vector<String>();
062: vectorData.add("a");
063: vectorData.add("b");
064: model = new DefaultComboBoxModel(vectorData);
065: assertEquals("a", model.getSelectedItem());
066: assertEquals(vectorData.size(), model.getSize());
067: assertEquals("a", model.getElementAt(0));
068: vectorData.setElementAt("a1", 0);
069: assertEquals("a1", model.getElementAt(0));
070: }
071:
072: public void testAddElement() throws Exception {
073: assertEquals(0, model.getSize());
074: assertNull(model.getSelectedItem());
075: model.addElement("1");
076: assertEquals(1, model.getSize());
077: checkListDataEvent(0, ListDataEvent.INTERVAL_ADDED, 0, 0);
078: checkListDataEvent(1, ListDataEvent.CONTENTS_CHANGED, -1, -1);
079: assertEquals("1", model.getSelectedItem());
080: listener.reset();
081: model.addElement("2");
082: assertEquals(2, model.getSize());
083: checkListDataEvent(0, ListDataEvent.INTERVAL_ADDED, 1, 1);
084: assertEquals("1", model.getElementAt(0));
085: assertEquals("2", model.getElementAt(1));
086: }
087:
088: public void testGetElementAt() throws Exception {
089: assertNull(model.getElementAt(0));
090: model.addElement("a");
091: model.addElement("b");
092: assertEquals("a", model.getElementAt(0));
093: assertEquals("b", model.getElementAt(1));
094: assertNull(model.getElementAt(2));
095: }
096:
097: public void testGetIndexOf() throws Exception {
098: assertEquals(-1, model.getIndexOf("a"));
099: model.addElement("a");
100: model.addElement("b");
101: model.addElement("a");
102: assertEquals(0, model.getIndexOf("a"));
103: assertEquals(1, model.getIndexOf("b"));
104: assertEquals(-1, model.getIndexOf("c"));
105: }
106:
107: public void testGetSize() throws Exception {
108: assertEquals(0, model.getSize());
109: model.addElement("a");
110: assertEquals(1, model.getSize());
111: model.addElement("a");
112: assertEquals(2, model.getSize());
113: }
114:
115: public void testInsertElementAt() throws Exception {
116: testExceptionalCase(new ExceptionalCase() {
117: @Override
118: public void exceptionalAction() throws Exception {
119: model.insertElementAt("a", 1);
120: }
121: });
122: model.insertElementAt("a", 0);
123: checkListDataEvent(0, ListDataEvent.INTERVAL_ADDED, 0, 0);
124: model.addElement("b");
125: model.addElement("c");
126: listener.reset();
127: model.insertElementAt("after_a", 1);
128: assertEquals("after_a", model.getElementAt(1));
129: checkListDataEvent(0, ListDataEvent.INTERVAL_ADDED, 1, 1);
130: }
131:
132: public void testRemoveAllElements() throws Exception {
133: model.addElement("a");
134: model.addElement("b");
135: model.addElement("c");
136: listener.reset();
137: model.removeAllElements();
138: assertEquals(0, model.getSize());
139: checkListDataEvent(0, ListDataEvent.INTERVAL_REMOVED, 0, 2);
140: assertNull(model.getSelectedItem());
141: }
142:
143: public void testRemoveElement() throws Exception {
144: model.removeElement("a");
145: assertEquals(0, model.getSize());
146: assertEquals(0, listener.getEvents().size());
147: model.addElement("a");
148: model.addElement("b");
149: model.addElement("c");
150: listener.reset();
151: model.removeElement("b");
152: assertEquals(2, model.getSize());
153: checkListDataEvent(0, ListDataEvent.INTERVAL_REMOVED, 1, 1);
154: assertEquals(1, listener.getEvents().size());
155: }
156:
157: public void testRemoveElementAt() throws Exception {
158: testExceptionalCase(new ExceptionalCase() {
159: @Override
160: public void exceptionalAction() throws Exception {
161: model.removeElementAt(0);
162: }
163: });
164: model.addElement("a");
165: model.addElement("b");
166: model.addElement("c");
167: testExceptionalCase(new ExceptionalCase() {
168: @Override
169: public void exceptionalAction() throws Exception {
170: model.removeElementAt(3);
171: }
172: });
173: model.removeElementAt(0);
174: assertEquals(2, model.getSize());
175: assertEquals("b", model.getElementAt(0));
176: model.removeElementAt(1);
177: assertEquals(1, model.getSize());
178: assertEquals("b", model.getElementAt(0));
179: model.removeElementAt(0);
180: assertEquals(0, model.getSize());
181: }
182:
183: public void testGetSetSelectedItem() throws Exception {
184: assertNull(model.getSelectedItem());
185: model.setSelectedItem("3");
186: assertEquals("3", model.getSelectedItem());
187: checkListDataEvent(0, ListDataEvent.CONTENTS_CHANGED, -1, -1);
188: listener.reset();
189: model.setSelectedItem(null);
190: checkListDataEvent(0, ListDataEvent.CONTENTS_CHANGED, -1, -1);
191: listener.reset();
192: model.setSelectedItem(null);
193: assertEquals(0, listener.getEvents().size());
194: model.setSelectedItem("3");
195: model.addElement("0");
196: model.addElement("1");
197: model.addElement("2");
198: assertEquals("3", model.getSelectedItem());
199: model.removeElement("0");
200: assertEquals("3", model.getSelectedItem());
201: model.addElement("3");
202: assertEquals("3", model.getSelectedItem());
203: model.addElement("4");
204: listener.reset();
205: model.removeElement("3");
206: assertEquals("2", model.getSelectedItem());
207: checkListDataEvent(0, ListDataEvent.CONTENTS_CHANGED, -1, -1);
208: checkListDataEvent(1, ListDataEvent.INTERVAL_REMOVED, 2, 2);
209: assertEquals(2, listener.getEvents().size());
210: model.removeElementAt(1);
211: assertEquals("1", model.getSelectedItem());
212: model.addElement("5");
213: model.addElement("6");
214: assertEquals("1", model.getSelectedItem());
215: model.removeElementAt(1);
216: assertEquals("1", model.getSelectedItem());
217: model.addElement("7");
218: model.addElement("1");
219: listener.reset();
220: model.removeElementAt(0);
221: assertEquals("5", model.getSelectedItem());
222: checkListDataEvent(0, ListDataEvent.CONTENTS_CHANGED, -1, -1);
223: checkListDataEvent(1, ListDataEvent.INTERVAL_REMOVED, 0, 0);
224: listener.reset();
225: model.removeAllElements();
226: assertNull(model.getSelectedItem());
227: checkListDataEvent(0, ListDataEvent.INTERVAL_REMOVED, 0, 3);
228: assertEquals(1, listener.getEvents().size());
229: listener.reset();
230: model.setSelectedItem("0");
231: checkListDataEvent(0, ListDataEvent.CONTENTS_CHANGED, -1, -1);
232: listener.reset();
233: model.removeAllElements();
234: assertNull(model.getSelectedItem());
235: assertEquals(0, listener.getEvents().size());
236: model.addElement("a");
237: model.addElement("b");
238: model.addElement("c");
239: listener.reset();
240: assertEquals("a", model.getSelectedItem());
241: model.setSelectedItem("b");
242: checkListDataEvent(0, ListDataEvent.CONTENTS_CHANGED, -1, -1);
243: listener.reset();
244: model.setSelectedItem("b");
245: assertEquals(0, listener.getEvents().size());
246: listener.reset();
247: model.setSelectedItem("c");
248: checkListDataEvent(0, ListDataEvent.CONTENTS_CHANGED, -1, -1);
249: assertEquals(1, listener.getEvents().size());
250: }
251:
252: private void checkListDataEvent(final int eventIndex,
253: final int eventType, final int index0, final int index1) {
254: assertTrue(listener.getEvents().size() > eventIndex);
255: ListDataEvent event = listener.getEvents().get(eventIndex);
256: assertEquals(model, event.getSource());
257: assertEquals(eventType, event.getType());
258: assertEquals(index0, event.getIndex0());
259: assertEquals(index1, event.getIndex1());
260: }
261:
262: private class TestListener implements ListDataListener {
263: private List<ListDataEvent> events = new ArrayList<ListDataEvent>();
264:
265: public void contentsChanged(final ListDataEvent e) {
266: events.add(e);
267: }
268:
269: public void intervalAdded(final ListDataEvent e) {
270: events.add(e);
271: }
272:
273: public void intervalRemoved(final ListDataEvent e) {
274: events.add(e);
275: }
276:
277: public List<ListDataEvent> getEvents() {
278: return events;
279: }
280:
281: public void reset() {
282: events.clear();
283: }
284: }
285: }
|