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.awt.event.KeyListener;
023: import javax.swing.event.ListDataEvent;
024: import javax.swing.event.ListDataListener;
025:
026: public class AbstractListModelTest extends SwingTestCase {
027: private AbstractListModel model;
028:
029: public AbstractListModelTest(final String name) {
030: super (name);
031: }
032:
033: @Override
034: protected void setUp() throws Exception {
035: model = new AbstractListModel() {
036: private static final long serialVersionUID = 1L;
037:
038: public Object getElementAt(final int index) {
039: return null;
040: }
041:
042: public int getSize() {
043: return 0;
044: }
045: };
046: }
047:
048: @Override
049: protected void tearDown() throws Exception {
050: model = null;
051: }
052:
053: public void testAddRemoveGetDataListener() throws Exception {
054: assertEquals(0, model.getListDataListeners().length);
055: TestListener l1 = new TestListener();
056: TestListener l2 = new TestListener();
057: TestListener l3 = new TestListener();
058: model.addListDataListener(l1);
059: model.addListDataListener(l3);
060: model.addListDataListener(l2);
061: assertEquals(3, model.getListDataListeners().length);
062: model.removeListDataListener(l2);
063: assertEquals(2, model.getListDataListeners().length);
064: model.removeListDataListener(new TestListener());
065: assertEquals(2, model.getListDataListeners().length);
066: }
067:
068: public void testGetListeners() throws Exception {
069: assertEquals(0,
070: model.getListeners(ListDataListener.class).length);
071: assertEquals(0, model.getListeners(KeyListener.class).length);
072: model.addListDataListener(new TestListener());
073: assertEquals(1,
074: model.getListeners(ListDataListener.class).length);
075: assertEquals(0, model.getListeners(KeyListener.class).length);
076: }
077:
078: public void testFireContentChanged() throws Exception {
079: TestListener l1 = new TestListener();
080: TestListener l2 = new TestListener();
081: model.addListDataListener(l1);
082: model.addListDataListener(l2);
083: model.fireContentsChanged(new Object(), 0, 2);
084: checkListDataEvent(l1.getEvent(),
085: ListDataEvent.CONTENTS_CHANGED, 0, 2);
086: assertEquals(ListDataEvent.CONTENTS_CHANGED, l1.getType());
087: checkListDataEvent(l2.getEvent(),
088: ListDataEvent.CONTENTS_CHANGED, 0, 2);
089: assertEquals(ListDataEvent.CONTENTS_CHANGED, l2.getType());
090: }
091:
092: public void testFireIntervalAdded() throws Exception {
093: TestListener l1 = new TestListener();
094: TestListener l2 = new TestListener();
095: model.addListDataListener(l1);
096: model.addListDataListener(l2);
097: model.fireIntervalAdded(new Object(), 5, 2);
098: checkListDataEvent(l1.getEvent(), ListDataEvent.INTERVAL_ADDED,
099: 2, 5);
100: assertEquals(ListDataEvent.INTERVAL_ADDED, l1.getType());
101: checkListDataEvent(l2.getEvent(), ListDataEvent.INTERVAL_ADDED,
102: 2, 5);
103: assertEquals(ListDataEvent.INTERVAL_ADDED, l2.getType());
104: model.fireIntervalAdded(new Object(), 2, 5);
105: checkListDataEvent(l1.getEvent(), ListDataEvent.INTERVAL_ADDED,
106: 2, 5);
107: }
108:
109: public void testFireIntervalRemoved() throws Exception {
110: TestListener l1 = new TestListener();
111: TestListener l2 = new TestListener();
112: model.addListDataListener(l1);
113: model.addListDataListener(l2);
114: model.fireIntervalRemoved(new Object(), 1, 4);
115: checkListDataEvent(l1.getEvent(),
116: ListDataEvent.INTERVAL_REMOVED, 1, 4);
117: assertEquals(ListDataEvent.INTERVAL_REMOVED, l1.getType());
118: checkListDataEvent(l2.getEvent(),
119: ListDataEvent.INTERVAL_REMOVED, 1, 4);
120: assertEquals(ListDataEvent.INTERVAL_REMOVED, l2.getType());
121: }
122:
123: private void checkListDataEvent(final ListDataEvent event,
124: final int expectedType, final int expectedIndex0,
125: final int expectedIndex1) {
126: assertNotNull(event);
127: assertEquals(expectedType, event.getType());
128: assertEquals(expectedIndex0, event.getIndex0());
129: assertEquals(expectedIndex1, event.getIndex1());
130: }
131:
132: private class TestListener implements ListDataListener {
133: private ListDataEvent event;
134:
135: private int eventType = -1;
136:
137: public void contentsChanged(final ListDataEvent e) {
138: event = e;
139: eventType = ListDataEvent.CONTENTS_CHANGED;
140: }
141:
142: public void intervalAdded(final ListDataEvent e) {
143: event = e;
144: eventType = ListDataEvent.INTERVAL_ADDED;
145: }
146:
147: public void intervalRemoved(final ListDataEvent e) {
148: event = e;
149: eventType = ListDataEvent.INTERVAL_REMOVED;
150: }
151:
152: public ListDataEvent getEvent() {
153: return event;
154: }
155:
156: public int getType() {
157: return eventType;
158: }
159: }
160: }
|