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.table;
021:
022: import javax.swing.event.TableModelEvent;
023: import javax.swing.event.TableModelListener;
024:
025: public class AbstractTableModelTest extends BasicSwingTableTestCase {
026: private AbstractTableModel model;
027:
028: public AbstractTableModelTest(final String name) {
029: super (name);
030: }
031:
032: @Override
033: protected void setUp() throws Exception {
034: model = new AbstractTableModel() {
035: private static final long serialVersionUID = 1L;
036:
037: public int getRowCount() {
038: return 5;
039: }
040:
041: public int getColumnCount() {
042: return 20000;
043: }
044:
045: public Object getValueAt(final int rowIndex,
046: final int columnIndex) {
047: return "value at [" + rowIndex + "," + columnIndex
048: + "]";
049: }
050: };
051: }
052:
053: public void testAbstractTableModel() throws Exception {
054: assertNotNull(model.listenerList);
055: assertEquals(0, model.listenerList.getListenerCount());
056: }
057:
058: public void testGetColumnName() throws Exception {
059: assertEquals("A", model.getColumnName(0));
060: assertEquals("B", model.getColumnName(1));
061: assertEquals("C", model.getColumnName(2));
062: assertEquals("Z", model.getColumnName(25));
063: assertEquals("AA", model.getColumnName(26));
064: assertEquals("AK", model.getColumnName(36));
065: assertEquals("SU", model.getColumnName(514));
066: assertEquals("AAJ", model.getColumnName(711));
067: assertEquals("ATS", model.getColumnName(1214));
068: assertEquals("BFG", model.getColumnName(1514));
069: assertEquals("ABVO", model.getColumnName(19514));
070: }
071:
072: public void testFindColumn() throws Exception {
073: assertEquals(-1, model.findColumn("a"));
074: assertEquals(-1, model.findColumn("Ba"));
075: assertEquals(0, model.findColumn("A"));
076: assertEquals(514, model.findColumn("SU"));
077: assertEquals(711, model.findColumn("AAJ"));
078: assertEquals(1514, model.findColumn("BFG"));
079: assertEquals(19514, model.findColumn("ABVO"));
080: assertEquals(-1, model.findColumn("BBVO"));
081: }
082:
083: public void testGetColumnClass() throws Exception {
084: assertEquals(Object.class, model.getColumnClass(555));
085: }
086:
087: public void testIsCellEditable() throws Exception {
088: assertFalse(model.isCellEditable(2, 4));
089: assertFalse(model.isCellEditable(20, 40000));
090: }
091:
092: public void testSetValueAt() throws Exception {
093: model.setValueAt("any", 0, 0);
094: model.setValueAt("any", 10000, 10000);
095: assertNotSame("any", model.getValueAt(0, 0));
096: }
097:
098: public void testAddRemoveGetTableModelListener() throws Exception {
099: assertEquals(0, model.getTableModelListeners().length);
100: assertEquals(0,
101: model.getListeners(TableModelListener.class).length);
102: TableModelListener listener1 = new TestTableModelListener();
103: TableModelListener listener2 = new TestTableModelListener();
104: model.addTableModelListener(listener1);
105: assertEquals(1, model.getTableModelListeners().length);
106: assertEquals(1,
107: model.getListeners(TableModelListener.class).length);
108: model.addTableModelListener(listener2);
109: assertEquals(2, model.getTableModelListeners().length);
110: assertEquals(2,
111: model.getListeners(TableModelListener.class).length);
112: model.removeTableModelListener(listener1);
113: assertEquals(1, model.getTableModelListeners().length);
114: assertEquals(1,
115: model.getListeners(TableModelListener.class).length);
116: }
117:
118: public void testFireTableDataChanged() throws Exception {
119: TestTableModelListener listener = new TestTableModelListener();
120: model.addTableModelListener(listener);
121: model.fireTableDataChanged();
122: assertTrue(listener.eventOccured());
123: assertEquals(model, listener.getEvent().getSource());
124: assertEquals(0, listener.getEvent().getFirstRow());
125: assertEquals(Integer.MAX_VALUE, listener.getEvent()
126: .getLastRow());
127: assertEquals(TableModelEvent.ALL_COLUMNS, listener.getEvent()
128: .getColumn());
129: assertEquals(TableModelEvent.UPDATE, listener.getEvent()
130: .getType());
131: }
132:
133: public void testFireTableStructureChanged() throws Exception {
134: TestTableModelListener listener = new TestTableModelListener();
135: model.addTableModelListener(listener);
136: model.fireTableStructureChanged();
137: assertTrue(listener.eventOccured());
138: assertEquals(model, listener.getEvent().getSource());
139: assertEquals(TableModelEvent.HEADER_ROW, listener.getEvent()
140: .getFirstRow());
141: assertEquals(TableModelEvent.HEADER_ROW, listener.getEvent()
142: .getLastRow());
143: assertEquals(TableModelEvent.ALL_COLUMNS, listener.getEvent()
144: .getColumn());
145: assertEquals(TableModelEvent.UPDATE, listener.getEvent()
146: .getType());
147: }
148:
149: public void testFireTableRowsInserted() throws Exception {
150: TestTableModelListener listener = new TestTableModelListener();
151: model.addTableModelListener(listener);
152: model.fireTableRowsInserted(5, 40000);
153: assertTrue(listener.eventOccured());
154: assertEquals(model, listener.getEvent().getSource());
155: assertEquals(5, listener.getEvent().getFirstRow());
156: assertEquals(40000, listener.getEvent().getLastRow());
157: assertEquals(TableModelEvent.ALL_COLUMNS, listener.getEvent()
158: .getColumn());
159: assertEquals(TableModelEvent.INSERT, listener.getEvent()
160: .getType());
161: }
162:
163: public void testFireTableRowsUpdated() throws Exception {
164: TestTableModelListener listener = new TestTableModelListener();
165: model.addTableModelListener(listener);
166: model.fireTableRowsUpdated(5, 40000);
167: assertTrue(listener.eventOccured());
168: assertEquals(model, listener.getEvent().getSource());
169: assertEquals(5, listener.getEvent().getFirstRow());
170: assertEquals(40000, listener.getEvent().getLastRow());
171: assertEquals(TableModelEvent.ALL_COLUMNS, listener.getEvent()
172: .getColumn());
173: assertEquals(TableModelEvent.UPDATE, listener.getEvent()
174: .getType());
175: }
176:
177: public void testFireTableRowsDeleted() throws Exception {
178: TestTableModelListener listener = new TestTableModelListener();
179: model.addTableModelListener(listener);
180: model.fireTableRowsDeleted(5, 40000);
181: assertTrue(listener.eventOccured());
182: assertEquals(model, listener.getEvent().getSource());
183: assertEquals(5, listener.getEvent().getFirstRow());
184: assertEquals(40000, listener.getEvent().getLastRow());
185: assertEquals(TableModelEvent.ALL_COLUMNS, listener.getEvent()
186: .getColumn());
187: assertEquals(TableModelEvent.DELETE, listener.getEvent()
188: .getType());
189: }
190:
191: public void testFireTableCellUpdated() throws Exception {
192: TestTableModelListener listener = new TestTableModelListener();
193: model.addTableModelListener(listener);
194: model.fireTableCellUpdated(5, 50);
195: assertTrue(listener.eventOccured());
196: assertEquals(model, listener.getEvent().getSource());
197: assertEquals(5, listener.getEvent().getFirstRow());
198: assertEquals(5, listener.getEvent().getLastRow());
199: assertEquals(50, listener.getEvent().getColumn());
200: assertEquals(TableModelEvent.UPDATE, listener.getEvent()
201: .getType());
202: }
203:
204: public void testFireTableChanged() throws Exception {
205: TestTableModelListener listener = new TestTableModelListener();
206: model.addTableModelListener(listener);
207: TableModelEvent event = new TableModelEvent(model, -5, 4, 3,
208: TableModelEvent.INSERT);
209: model.fireTableChanged(event);
210: assertTrue(listener.eventOccured());
211: assertEquals(model, listener.getEvent().getSource());
212: assertEquals(-5, listener.getEvent().getFirstRow());
213: assertEquals(4, listener.getEvent().getLastRow());
214: assertEquals(3, listener.getEvent().getColumn());
215: assertEquals(TableModelEvent.INSERT, listener.getEvent()
216: .getType());
217: }
218: }
|