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 java.util.Arrays;
023: import java.util.Vector;
024: import javax.swing.event.TableModelEvent;
025:
026: @SuppressWarnings("unchecked")
027: public class DefaultTableModelTest extends BasicSwingTableTestCase {
028: private DefaultTableModel model;
029:
030: public DefaultTableModelTest(final String name) {
031: super (name);
032: }
033:
034: @Override
035: protected void setUp() throws Exception {
036: model = new DefaultTableModel();
037: }
038:
039: @Override
040: protected void tearDown() throws Exception {
041: model = null;
042: }
043:
044: public void testDefaultTableModel() throws Exception {
045: assertNotNull(model.dataVector);
046: assertNotNull(model.columnIdentifiers);
047: assertEquals(0, model.dataVector.size());
048: assertEquals(0, model.columnIdentifiers.size());
049: model = new DefaultTableModel(3, 4);
050: assertEquals(4, model.columnIdentifiers.size());
051: assertNull(model.columnIdentifiers.get(0));
052: assertNull(model.columnIdentifiers.get(3));
053: assertEquals(3, model.dataVector.size());
054: assertTrue(model.dataVector.get(0) instanceof Vector);
055: assertTrue(model.dataVector.get(2) instanceof Vector);
056: assertEquals(4, ((Vector) model.dataVector.get(0)).size());
057: assertEquals(4, ((Vector) model.dataVector.get(2)).size());
058: assertNull(((Vector) model.dataVector.get(0)).get(0));
059: assertNull(((Vector) model.dataVector.get(2)).get(3));
060: model = new DefaultTableModel(new Object[] { "col1", "col2",
061: "col3" }, 4);
062: assertEquals(3, model.columnIdentifiers.size());
063: assertEquals("col2", model.columnIdentifiers.get(1));
064: assertEquals(4, model.dataVector.size());
065: Vector columnNames = new Vector(Arrays.asList(new Object[] {
066: "col1", "col2", "col3" }));
067: model = new DefaultTableModel(columnNames, 4);
068: assertTrue(columnNames == model.columnIdentifiers);
069: assertEquals(3, model.columnIdentifiers.size());
070: assertEquals("col2", model.columnIdentifiers.get(1));
071: assertEquals(4, model.dataVector.size());
072: Vector dataVector = new Vector();
073: dataVector.add(new Vector(Arrays.asList(new Object[] {
074: "data11", "data12", "data13" })));
075: dataVector.add(new Vector(Arrays.asList(new Object[] {
076: "data21", "data22", "data23" })));
077: model = new DefaultTableModel(dataVector, new Vector(Arrays
078: .asList(new Object[] { "col1", "col2", "col3" })));
079: assertTrue(dataVector == model.dataVector);
080: assertEquals(3, model.columnIdentifiers.size());
081: assertEquals(2, model.dataVector.size());
082: model = new DefaultTableModel(null, new Vector(Arrays
083: .asList(new Object[] { "col1", "col2", "col3" })));
084: assertNotNull(model.dataVector);
085: assertEquals(0, model.dataVector.size());
086: model = new DefaultTableModel(dataVector, null);
087: assertNotNull(model.columnIdentifiers);
088: assertEquals(0, model.columnIdentifiers.size());
089: assertEquals(2, model.dataVector.size());
090: assertEquals(0, ((Vector) model.dataVector.get(0)).size());
091: model = new DefaultTableModel(new Object[][] {
092: { "data11", "data12", "data13" },
093: { "data21", "data22", "data23" } }, new Object[] {
094: "col1", "col2", "col3" });
095: assertEquals(3, model.columnIdentifiers.size());
096: assertEquals(2, model.dataVector.size());
097: }
098:
099: public void testGetSetDataVector() throws Exception {
100: Vector dataVector = new Vector();
101: dataVector.add(new Vector(Arrays.asList(new Object[] {
102: "data11", "data12", "data13" })));
103: dataVector.add(new Vector(Arrays.asList(new Object[] {
104: "data21", "data22", "data23" })));
105: model = new DefaultTableModel(dataVector, new Vector(Arrays
106: .asList(new Object[] { "col1", "col2", "col3" })));
107: assertTrue(dataVector == model.getDataVector());
108: TestTableModelListener listener = new TestTableModelListener();
109: model.addTableModelListener(listener);
110: dataVector = new Vector();
111: dataVector.add(new Vector(Arrays.asList(new Object[] {
112: "value11", "value12" })));
113: dataVector.add(new Vector(Arrays.asList(new Object[] {
114: "value21", "value22", "value23" })));
115: dataVector.add(new Vector(Arrays
116: .asList(new Object[] { "value31" })));
117: model.setDataVector(dataVector, new Vector(Arrays
118: .asList(new Object[] { "col1", "col2" })));
119: assertTrue(dataVector == model.getDataVector());
120: assertTrue(dataVector == model.dataVector);
121: assertEquals(2, model.columnIdentifiers.size());
122: assertEquals(2, ((Vector) model.getDataVector().get(0)).size());
123: assertEquals(2, ((Vector) model.getDataVector().get(1)).size());
124: assertEquals(2, ((Vector) model.getDataVector().get(2)).size());
125: assertEquals("value22", ((Vector) model.getDataVector().get(1))
126: .get(1));
127: assertNull(((Vector) model.getDataVector().get(2)).get(1));
128: assertTrue(listener.eventOccured());
129: assertEquals(model, listener.getEvent().getSource());
130: assertEquals(TableModelEvent.HEADER_ROW, listener.getEvent()
131: .getFirstRow());
132: assertEquals(TableModelEvent.HEADER_ROW, listener.getEvent()
133: .getLastRow());
134: assertEquals(TableModelEvent.ALL_COLUMNS, listener.getEvent()
135: .getColumn());
136: assertEquals(TableModelEvent.UPDATE, listener.getEvent()
137: .getType());
138: listener.reset();
139: model.addTableModelListener(listener);
140: Object[][] dataArray = new Object[][] {
141: new Object[] { "value11", "value12" },
142: new Object[] { "value21", "value22", "value23" },
143: new Object[] { "value31" } };
144: model.setDataVector(dataArray, new Object[] { "col1", "col2" });
145: assertEquals(2, model.columnIdentifiers.size());
146: assertEquals(2, ((Vector) model.dataVector.get(0)).size());
147: assertEquals(2, ((Vector) model.dataVector.get(1)).size());
148: assertEquals(2, ((Vector) model.dataVector.get(2)).size());
149: assertEquals("value22", ((Vector) model.dataVector.get(1))
150: .get(1));
151: assertNull(((Vector) model.dataVector.get(2)).get(1));
152: assertTrue(listener.eventOccured());
153: assertEquals(model, listener.getEvent().getSource());
154: assertEquals(TableModelEvent.HEADER_ROW, listener.getEvent()
155: .getFirstRow());
156: assertEquals(TableModelEvent.HEADER_ROW, listener.getEvent()
157: .getLastRow());
158: assertEquals(TableModelEvent.ALL_COLUMNS, listener.getEvent()
159: .getColumn());
160: assertEquals(TableModelEvent.UPDATE, listener.getEvent()
161: .getType());
162: }
163:
164: public void testNewDataAvailable() throws Exception {
165: TestTableModelListener listener = new TestTableModelListener();
166: model.addTableModelListener(listener);
167: TableModelEvent event = new TableModelEvent(model);
168: model.newDataAvailable(event);
169: assertTrue(listener.eventOccured());
170: assertEquals(event, listener.getEvent());
171: }
172:
173: public void testNewRowsAdded() throws Exception {
174: TestTableModelListener listener = new TestTableModelListener();
175: model.addTableModelListener(listener);
176: model.columnIdentifiers = new Vector(Arrays
177: .asList(new Object[] { "col1", "col2" }));
178: model.dataVector = new Vector();
179: model.dataVector.add(new Vector(Arrays.asList(new Object[] {
180: "value11", "value12" })));
181: model.dataVector.add(new Vector(Arrays.asList(new Object[] {
182: "value21", "value22", "value23" })));
183: model.dataVector.add(new Vector(Arrays
184: .asList(new Object[] { "value31" })));
185: TableModelEvent event = new TableModelEvent(model, 1, 2,
186: TableModelEvent.HEADER_ROW, TableModelEvent.DELETE);
187: model.newRowsAdded(event);
188: assertEquals(2, model.columnIdentifiers.size());
189: assertEquals(2, ((Vector) model.getDataVector().get(0)).size());
190: assertEquals(2, ((Vector) model.getDataVector().get(1)).size());
191: assertEquals(2, ((Vector) model.getDataVector().get(2)).size());
192: assertTrue(listener.eventOccured());
193: assertEquals(event, listener.getEvent());
194: }
195:
196: public void testRowsRemoved() throws Exception {
197: TestTableModelListener listener = new TestTableModelListener();
198: model.addTableModelListener(listener);
199: TableModelEvent event = new TableModelEvent(model);
200: model.rowsRemoved(event);
201: assertTrue(listener.eventOccured());
202: assertEquals(event, listener.getEvent());
203: }
204:
205: public void testSetNumRowsSetGetRowCount() throws Exception {
206: assertEquals(0, model.getRowCount());
207: TestTableModelListener listener = new TestTableModelListener();
208: model.addTableModelListener(listener);
209: model.columnIdentifiers = new Vector(Arrays
210: .asList(new Object[] { "col1", "col2" }));
211: model.dataVector = new Vector();
212: model.dataVector.add(new Vector(Arrays.asList(new Object[] {
213: "value11", "value12" })));
214: model.dataVector.add(new Vector(Arrays.asList(new Object[] {
215: "value21", "value22", "value23" })));
216: model.dataVector.add(new Vector(Arrays
217: .asList(new Object[] { "value31" })));
218: assertEquals(3, model.getRowCount());
219: listener.reset();
220: model.setRowCount(5);
221: assertEquals(5, model.getDataVector().size());
222: assertEquals(5, model.getRowCount());
223: assertTrue(listener.eventOccured());
224: assertEquals(model, listener.getEvent().getSource());
225: assertEquals(3, listener.getEvent().getFirstRow());
226: assertEquals(4, listener.getEvent().getLastRow());
227: assertEquals(TableModelEvent.ALL_COLUMNS, listener.getEvent()
228: .getColumn());
229: assertEquals(TableModelEvent.INSERT, listener.getEvent()
230: .getType());
231: assertEquals(3, ((Vector) model.dataVector.get(1)).size());
232: assertEquals(1, ((Vector) model.dataVector.get(2)).size());
233: assertEquals(2, ((Vector) model.dataVector.get(3)).size());
234: assertEquals(2, ((Vector) model.dataVector.get(4)).size());
235: listener.reset();
236: model.setRowCount(5);
237: assertEquals(5, model.getDataVector().size());
238: assertFalse(listener.eventOccured());
239: listener.reset();
240: model.setNumRows(1);
241: assertEquals(1, model.getDataVector().size());
242: assertEquals(1, model.getRowCount());
243: assertTrue(listener.eventOccured());
244: assertEquals(model, listener.getEvent().getSource());
245: assertEquals(1, listener.getEvent().getFirstRow());
246: assertEquals(4, listener.getEvent().getLastRow());
247: assertEquals(TableModelEvent.ALL_COLUMNS, listener.getEvent()
248: .getColumn());
249: assertEquals(TableModelEvent.DELETE, listener.getEvent()
250: .getType());
251: }
252:
253: public void testAddRow() throws Exception {
254: TestTableModelListener listener = new TestTableModelListener();
255: model.addTableModelListener(listener);
256: model.columnIdentifiers = new Vector(Arrays
257: .asList(new Object[] { "col1", "col2" }));
258: model.dataVector = new Vector();
259: model.dataVector.add(new Vector(Arrays.asList(new Object[] {
260: "value11", "value12" })));
261: model.dataVector.add(new Vector(Arrays.asList(new Object[] {
262: "value21", "value22", "value23" })));
263: model.dataVector.add(new Vector(Arrays
264: .asList(new Object[] { "value31" })));
265: listener.reset();
266: model.addRow((Vector) null);
267: assertEquals(4, model.getDataVector().size());
268: assertEquals(3, ((Vector) model.dataVector.get(1)).size());
269: assertEquals(1, ((Vector) model.dataVector.get(2)).size());
270: assertEquals(2, ((Vector) model.dataVector.get(3)).size());
271: assertNull(((Vector) model.dataVector.get(3)).get(0));
272: assertNull(((Vector) model.dataVector.get(3)).get(1));
273: assertTrue(listener.eventOccured());
274: assertEquals(model, listener.getEvent().getSource());
275: assertEquals(3, listener.getEvent().getFirstRow());
276: assertEquals(3, listener.getEvent().getLastRow());
277: assertEquals(TableModelEvent.ALL_COLUMNS, listener.getEvent()
278: .getColumn());
279: assertEquals(TableModelEvent.INSERT, listener.getEvent()
280: .getType());
281: listener.reset();
282: model.addRow(new Object[] { "a" });
283: assertEquals(5, model.getDataVector().size());
284: assertEquals(3, ((Vector) model.dataVector.get(1)).size());
285: assertEquals(1, ((Vector) model.dataVector.get(2)).size());
286: assertEquals(2, ((Vector) model.dataVector.get(3)).size());
287: assertEquals(2, ((Vector) model.dataVector.get(4)).size());
288: assertEquals("a", ((Vector) model.dataVector.get(4)).get(0));
289: assertNull(((Vector) model.dataVector.get(4)).get(1));
290: }
291:
292: public void testInsertRow() throws Exception {
293: TestTableModelListener listener = new TestTableModelListener();
294: model.addTableModelListener(listener);
295: model.columnIdentifiers = new Vector(Arrays
296: .asList(new Object[] { "col1", "col2" }));
297: model.dataVector = new Vector();
298: model.dataVector.add(new Vector(Arrays.asList(new Object[] {
299: "value11", "value12" })));
300: model.dataVector.add(new Vector(Arrays.asList(new Object[] {
301: "value21", "value22", "value23" })));
302: model.dataVector.add(new Vector(Arrays
303: .asList(new Object[] { "value31" })));
304: listener.reset();
305: model.insertRow(1, (Vector) null);
306: assertEquals(4, model.getDataVector().size());
307: assertEquals(2, ((Vector) model.dataVector.get(0)).size());
308: assertEquals(2, ((Vector) model.dataVector.get(1)).size());
309: assertEquals(3, ((Vector) model.dataVector.get(2)).size());
310: assertEquals(1, ((Vector) model.dataVector.get(3)).size());
311: assertNull(((Vector) model.dataVector.get(1)).get(0));
312: assertNull(((Vector) model.dataVector.get(1)).get(1));
313: assertTrue(listener.eventOccured());
314: assertEquals(model, listener.getEvent().getSource());
315: assertEquals(1, listener.getEvent().getFirstRow());
316: assertEquals(1, listener.getEvent().getLastRow());
317: assertEquals(TableModelEvent.ALL_COLUMNS, listener.getEvent()
318: .getColumn());
319: assertEquals(TableModelEvent.INSERT, listener.getEvent()
320: .getType());
321: listener.reset();
322: model.insertRow(0, new Object[] { "a", "b", "c" });
323: assertEquals(5, model.getDataVector().size());
324: assertEquals(2, ((Vector) model.dataVector.get(0)).size());
325: assertEquals(2, ((Vector) model.dataVector.get(1)).size());
326: assertEquals(2, ((Vector) model.dataVector.get(2)).size());
327: assertEquals(3, ((Vector) model.dataVector.get(3)).size());
328: assertEquals(1, ((Vector) model.dataVector.get(4)).size());
329: assertEquals("a", ((Vector) model.dataVector.get(0)).get(0));
330: assertEquals("b", ((Vector) model.dataVector.get(0)).get(1));
331: testExceptionalCase(new ArrayIndexOutOfBoundsExceptionalCase() {
332: @Override
333: public void exceptionalAction() throws Exception {
334: model.insertRow(6, new Object[] { "a", "b", "c" });
335: }
336: });
337: }
338:
339: public void testMoveRow() throws Exception {
340: TestTableModelListener listener = new TestTableModelListener();
341: model.addTableModelListener(listener);
342: model.columnIdentifiers = new Vector(Arrays
343: .asList(new Object[] { "col1", "col2" }));
344: model.dataVector = new Vector();
345: model.dataVector.add(new Vector(Arrays
346: .asList(new Object[] { "value11" })));
347: model.dataVector.add(new Vector(Arrays.asList(new Object[] {
348: "value21", "value22" })));
349: model.dataVector.add(new Vector(Arrays.asList(new Object[] {
350: "value31", "value32", "value33" })));
351: model.dataVector.add(new Vector(Arrays.asList(new Object[] {
352: "value41", "value42", "value43", "value44" })));
353: model.dataVector.add(new Vector(Arrays
354: .asList(new Object[] { "value51", "value52", "value53",
355: "value54", "value55" })));
356: listener.reset();
357: model.moveRow(0, 1, 1);
358: assertEquals(5, model.getDataVector().size());
359: assertEquals(3, ((Vector) model.dataVector.get(0)).size());
360: assertEquals(1, ((Vector) model.dataVector.get(1)).size());
361: assertEquals(2, ((Vector) model.dataVector.get(2)).size());
362: assertEquals(4, ((Vector) model.dataVector.get(3)).size());
363: assertEquals(5, ((Vector) model.dataVector.get(4)).size());
364: assertTrue(listener.eventOccured());
365: assertEquals(model, listener.getEvent().getSource());
366: assertEquals(0, listener.getEvent().getFirstRow());
367: assertEquals(2, listener.getEvent().getLastRow());
368: assertEquals(TableModelEvent.ALL_COLUMNS, listener.getEvent()
369: .getColumn());
370: assertEquals(TableModelEvent.UPDATE, listener.getEvent()
371: .getType());
372: listener.reset();
373: model.moveRow(1, 1, 2);
374: assertEquals(5, model.getDataVector().size());
375: assertEquals(3, ((Vector) model.dataVector.get(0)).size());
376: assertEquals(2, ((Vector) model.dataVector.get(1)).size());
377: assertEquals(1, ((Vector) model.dataVector.get(2)).size());
378: assertEquals(4, ((Vector) model.dataVector.get(3)).size());
379: assertEquals(5, ((Vector) model.dataVector.get(4)).size());
380: assertTrue(listener.eventOccured());
381: assertEquals(model, listener.getEvent().getSource());
382: assertEquals(1, listener.getEvent().getFirstRow());
383: assertEquals(2, listener.getEvent().getLastRow());
384: assertEquals(TableModelEvent.ALL_COLUMNS, listener.getEvent()
385: .getColumn());
386: assertEquals(TableModelEvent.UPDATE, listener.getEvent()
387: .getType());
388: listener.reset();
389: model.moveRow(2, 2, 0);
390: assertEquals(5, model.getDataVector().size());
391: assertEquals(1, ((Vector) model.dataVector.get(0)).size());
392: assertEquals(3, ((Vector) model.dataVector.get(1)).size());
393: assertEquals(2, ((Vector) model.dataVector.get(2)).size());
394: assertEquals(4, ((Vector) model.dataVector.get(3)).size());
395: assertEquals(5, ((Vector) model.dataVector.get(4)).size());
396: assertTrue(listener.eventOccured());
397: assertEquals(model, listener.getEvent().getSource());
398: assertEquals(0, listener.getEvent().getFirstRow());
399: assertEquals(2, listener.getEvent().getLastRow());
400: assertEquals(TableModelEvent.ALL_COLUMNS, listener.getEvent()
401: .getColumn());
402: assertEquals(TableModelEvent.UPDATE, listener.getEvent()
403: .getType());
404: listener.reset();
405: model.moveRow(0, 3, 1);
406: assertEquals(5, model.getDataVector().size());
407: assertEquals(5, ((Vector) model.dataVector.get(0)).size());
408: assertEquals(1, ((Vector) model.dataVector.get(1)).size());
409: assertEquals(3, ((Vector) model.dataVector.get(2)).size());
410: assertEquals(2, ((Vector) model.dataVector.get(3)).size());
411: assertEquals(4, ((Vector) model.dataVector.get(4)).size());
412: assertTrue(listener.eventOccured());
413: assertEquals(model, listener.getEvent().getSource());
414: assertEquals(0, listener.getEvent().getFirstRow());
415: assertEquals(4, listener.getEvent().getLastRow());
416: assertEquals(TableModelEvent.ALL_COLUMNS, listener.getEvent()
417: .getColumn());
418: assertEquals(TableModelEvent.UPDATE, listener.getEvent()
419: .getType());
420: listener.reset();
421: model.moveRow(2, 4, 1);
422: assertEquals(5, model.getDataVector().size());
423: assertEquals(5, ((Vector) model.dataVector.get(0)).size());
424: assertEquals(3, ((Vector) model.dataVector.get(1)).size());
425: assertEquals(2, ((Vector) model.dataVector.get(2)).size());
426: assertEquals(4, ((Vector) model.dataVector.get(3)).size());
427: assertEquals(1, ((Vector) model.dataVector.get(4)).size());
428: assertTrue(listener.eventOccured());
429: assertEquals(model, listener.getEvent().getSource());
430: assertEquals(1, listener.getEvent().getFirstRow());
431: assertEquals(4, listener.getEvent().getLastRow());
432: assertEquals(TableModelEvent.ALL_COLUMNS, listener.getEvent()
433: .getColumn());
434: assertEquals(TableModelEvent.UPDATE, listener.getEvent()
435: .getType());
436: testExceptionalCase(new ArrayIndexOutOfBoundsExceptionalCase() {
437: @Override
438: public void exceptionalAction() throws Exception {
439: model.moveRow(-1, 2, 0);
440: }
441: });
442: testExceptionalCase(new ArrayIndexOutOfBoundsExceptionalCase() {
443: @Override
444: public void exceptionalAction() throws Exception {
445: model.moveRow(0, 3, 2);
446: }
447: });
448: testExceptionalCase(new ArrayIndexOutOfBoundsExceptionalCase() {
449: @Override
450: public void exceptionalAction() throws Exception {
451: model.moveRow(1, 2, 4);
452: }
453: });
454: testExceptionalCase(new ArrayIndexOutOfBoundsExceptionalCase() {
455: @Override
456: public void exceptionalAction() throws Exception {
457: model.moveRow(1, 7, 0);
458: }
459: });
460: }
461:
462: public void testRemoveRow() throws Exception {
463: TestTableModelListener listener = new TestTableModelListener();
464: model.addTableModelListener(listener);
465: model.columnIdentifiers = new Vector(Arrays
466: .asList(new Object[] { "col1", "col2" }));
467: model.dataVector = new Vector();
468: model.dataVector.add(new Vector(Arrays.asList(new Object[] {
469: "value11", "value12" })));
470: model.dataVector.add(new Vector(Arrays.asList(new Object[] {
471: "value21", "value22", "value23" })));
472: model.dataVector.add(new Vector(Arrays
473: .asList(new Object[] { "value31" })));
474: listener.reset();
475: model.removeRow(1);
476: assertEquals(2, model.getDataVector().size());
477: assertEquals(2, ((Vector) model.dataVector.get(0)).size());
478: assertEquals(1, ((Vector) model.dataVector.get(1)).size());
479: assertTrue(listener.eventOccured());
480: assertEquals(model, listener.getEvent().getSource());
481: assertEquals(1, listener.getEvent().getFirstRow());
482: assertEquals(1, listener.getEvent().getLastRow());
483: assertEquals(TableModelEvent.ALL_COLUMNS, listener.getEvent()
484: .getColumn());
485: assertEquals(TableModelEvent.DELETE, listener.getEvent()
486: .getType());
487: testExceptionalCase(new ArrayIndexOutOfBoundsExceptionalCase() {
488: @Override
489: public void exceptionalAction() throws Exception {
490: model.removeRow(-1);
491: }
492: });
493: testExceptionalCase(new ArrayIndexOutOfBoundsExceptionalCase() {
494: @Override
495: public void exceptionalAction() throws Exception {
496: model.removeRow(3);
497: }
498: });
499: }
500:
501: public void testSetColumnIdentifiers() throws Exception {
502: TestTableModelListener listener = new TestTableModelListener();
503: model.addTableModelListener(listener);
504: model.columnIdentifiers = new Vector(Arrays
505: .asList(new Object[] { "col1", "col2" }));
506: model.dataVector = new Vector();
507: model.dataVector.add(new Vector(Arrays.asList(new Object[] {
508: "value11", "value12" })));
509: model.dataVector.add(new Vector(Arrays.asList(new Object[] {
510: "value21", "value22", "value23" })));
511: model.dataVector.add(new Vector(Arrays
512: .asList(new Object[] { "value31" })));
513: listener.reset();
514: model.setColumnIdentifiers(new Vector(Arrays
515: .asList(new Object[] { "col1", "col2" })));
516: assertEquals(2, model.columnIdentifiers.size());
517: assertEquals(3, model.getDataVector().size());
518: assertEquals(2, ((Vector) model.dataVector.get(0)).size());
519: assertEquals(2, ((Vector) model.dataVector.get(1)).size());
520: assertEquals(2, ((Vector) model.dataVector.get(2)).size());
521: assertTrue(listener.eventOccured());
522: assertEquals(model, listener.getEvent().getSource());
523: assertEquals(TableModelEvent.HEADER_ROW, listener.getEvent()
524: .getFirstRow());
525: assertEquals(TableModelEvent.HEADER_ROW, listener.getEvent()
526: .getLastRow());
527: assertEquals(TableModelEvent.ALL_COLUMNS, listener.getEvent()
528: .getColumn());
529: assertEquals(TableModelEvent.UPDATE, listener.getEvent()
530: .getType());
531: listener.reset();
532: model.setColumnIdentifiers(new Vector(Arrays
533: .asList(new Object[] { "col1", "col2", "col3" })));
534: assertEquals(3, model.columnIdentifiers.size());
535: assertEquals(3, model.getDataVector().size());
536: assertEquals(3, ((Vector) model.dataVector.get(0)).size());
537: assertEquals(3, ((Vector) model.dataVector.get(1)).size());
538: assertEquals(3, ((Vector) model.dataVector.get(2)).size());
539: assertTrue(listener.eventOccured());
540: assertEquals(model, listener.getEvent().getSource());
541: assertEquals(TableModelEvent.HEADER_ROW, listener.getEvent()
542: .getFirstRow());
543: assertEquals(TableModelEvent.HEADER_ROW, listener.getEvent()
544: .getLastRow());
545: assertEquals(TableModelEvent.ALL_COLUMNS, listener.getEvent()
546: .getColumn());
547: assertEquals(TableModelEvent.UPDATE, listener.getEvent()
548: .getType());
549: listener.reset();
550: model.setColumnIdentifiers(new Object[] { "col1" });
551: assertEquals(1, model.columnIdentifiers.size());
552: assertEquals(3, model.getDataVector().size());
553: assertEquals(1, ((Vector) model.dataVector.get(0)).size());
554: assertEquals(1, ((Vector) model.dataVector.get(1)).size());
555: assertEquals(1, ((Vector) model.dataVector.get(2)).size());
556: assertTrue(listener.eventOccured());
557: assertEquals(model, listener.getEvent().getSource());
558: assertEquals(TableModelEvent.HEADER_ROW, listener.getEvent()
559: .getFirstRow());
560: assertEquals(TableModelEvent.HEADER_ROW, listener.getEvent()
561: .getLastRow());
562: assertEquals(TableModelEvent.ALL_COLUMNS, listener.getEvent()
563: .getColumn());
564: assertEquals(TableModelEvent.UPDATE, listener.getEvent()
565: .getType());
566: listener.reset();
567: model.setColumnIdentifiers(new Object[] { "col1" });
568: assertEquals(1, model.columnIdentifiers.size());
569: assertEquals(3, model.getDataVector().size());
570: assertEquals(1, ((Vector) model.dataVector.get(0)).size());
571: assertEquals(1, ((Vector) model.dataVector.get(1)).size());
572: assertEquals(1, ((Vector) model.dataVector.get(2)).size());
573: assertTrue(listener.eventOccured());
574: assertEquals(model, listener.getEvent().getSource());
575: assertEquals(TableModelEvent.HEADER_ROW, listener.getEvent()
576: .getFirstRow());
577: assertEquals(TableModelEvent.HEADER_ROW, listener.getEvent()
578: .getLastRow());
579: assertEquals(TableModelEvent.ALL_COLUMNS, listener.getEvent()
580: .getColumn());
581: assertEquals(TableModelEvent.UPDATE, listener.getEvent()
582: .getType());
583: }
584:
585: public void testGetSetColumnCount() throws Exception {
586: assertEquals(0, model.getColumnCount());
587: TestTableModelListener listener = new TestTableModelListener();
588: model.addTableModelListener(listener);
589: model.columnIdentifiers = new Vector(Arrays
590: .asList(new Object[] { "col1", "col2" }));
591: model.dataVector = new Vector();
592: model.dataVector.add(new Vector(Arrays.asList(new Object[] {
593: "value11", "value12" })));
594: model.dataVector.add(new Vector(Arrays.asList(new Object[] {
595: "value21", "value22", "value23" })));
596: model.dataVector.add(new Vector(Arrays
597: .asList(new Object[] { "value31" })));
598: listener.reset();
599: model.setColumnCount(2);
600: assertEquals(2, model.columnIdentifiers.size());
601: assertEquals(2, model.getColumnCount());
602: assertEquals(3, model.getDataVector().size());
603: assertEquals(2, ((Vector) model.dataVector.get(0)).size());
604: assertEquals(2, ((Vector) model.dataVector.get(1)).size());
605: assertEquals(2, ((Vector) model.dataVector.get(2)).size());
606: assertTrue(listener.eventOccured());
607: assertEquals(model, listener.getEvent().getSource());
608: assertEquals(TableModelEvent.HEADER_ROW, listener.getEvent()
609: .getFirstRow());
610: assertEquals(TableModelEvent.HEADER_ROW, listener.getEvent()
611: .getLastRow());
612: assertEquals(TableModelEvent.ALL_COLUMNS, listener.getEvent()
613: .getColumn());
614: assertEquals(TableModelEvent.UPDATE, listener.getEvent()
615: .getType());
616: listener.reset();
617: model.setColumnCount(3);
618: assertEquals(3, model.columnIdentifiers.size());
619: assertEquals(3, model.getColumnCount());
620: assertEquals(3, model.getDataVector().size());
621: assertEquals(3, ((Vector) model.dataVector.get(0)).size());
622: assertEquals(3, ((Vector) model.dataVector.get(1)).size());
623: assertEquals(3, ((Vector) model.dataVector.get(2)).size());
624: assertTrue(listener.eventOccured());
625: assertEquals(model, listener.getEvent().getSource());
626: assertEquals(TableModelEvent.HEADER_ROW, listener.getEvent()
627: .getFirstRow());
628: assertEquals(TableModelEvent.HEADER_ROW, listener.getEvent()
629: .getLastRow());
630: assertEquals(TableModelEvent.ALL_COLUMNS, listener.getEvent()
631: .getColumn());
632: assertEquals(TableModelEvent.UPDATE, listener.getEvent()
633: .getType());
634: listener.reset();
635: model.setColumnCount(1);
636: assertEquals(1, model.columnIdentifiers.size());
637: assertEquals(1, model.getColumnCount());
638: assertEquals(3, model.getDataVector().size());
639: assertEquals(1, ((Vector) model.dataVector.get(0)).size());
640: assertEquals(1, ((Vector) model.dataVector.get(1)).size());
641: assertEquals(1, ((Vector) model.dataVector.get(2)).size());
642: assertTrue(listener.eventOccured());
643: assertEquals(model, listener.getEvent().getSource());
644: assertEquals(TableModelEvent.HEADER_ROW, listener.getEvent()
645: .getFirstRow());
646: assertEquals(TableModelEvent.HEADER_ROW, listener.getEvent()
647: .getLastRow());
648: assertEquals(TableModelEvent.ALL_COLUMNS, listener.getEvent()
649: .getColumn());
650: assertEquals(TableModelEvent.UPDATE, listener.getEvent()
651: .getType());
652: }
653:
654: public void testAddColumn() throws Exception {
655: TestTableModelListener listener = new TestTableModelListener();
656: model.addTableModelListener(listener);
657: model.columnIdentifiers = new Vector(Arrays
658: .asList(new Object[] { "col1", "col2" }));
659: model.dataVector = new Vector();
660: model.dataVector.add(new Vector(Arrays.asList(new Object[] {
661: "value11", "value12" })));
662: model.dataVector.add(new Vector(Arrays.asList(new Object[] {
663: "value21", "value22", "value23" })));
664: model.dataVector.add(new Vector(Arrays
665: .asList(new Object[] { "value31" })));
666: listener.reset();
667: model.addColumn(null, new Object[] { "val13", "val23" });
668: assertEquals(3, model.columnIdentifiers.size());
669: assertEquals(3, model.getDataVector().size());
670: assertEquals(3, ((Vector) model.dataVector.get(0)).size());
671: assertEquals(3, ((Vector) model.dataVector.get(1)).size());
672: assertEquals(3, ((Vector) model.dataVector.get(2)).size());
673: assertNull(model.columnIdentifiers.get(2));
674: assertEquals("val13", ((Vector) model.dataVector.get(0)).get(2));
675: assertEquals("val23", ((Vector) model.dataVector.get(1)).get(2));
676: assertNull(((Vector) model.dataVector.get(2)).get(2));
677: assertTrue(listener.eventOccured());
678: assertEquals(model, listener.getEvent().getSource());
679: assertEquals(TableModelEvent.HEADER_ROW, listener.getEvent()
680: .getFirstRow());
681: assertEquals(TableModelEvent.HEADER_ROW, listener.getEvent()
682: .getLastRow());
683: assertEquals(TableModelEvent.ALL_COLUMNS, listener.getEvent()
684: .getColumn());
685: assertEquals(TableModelEvent.UPDATE, listener.getEvent()
686: .getType());
687: listener.reset();
688: model.addColumn("col4");
689: assertEquals(4, model.columnIdentifiers.size());
690: assertEquals(3, model.getDataVector().size());
691: assertEquals(4, ((Vector) model.dataVector.get(0)).size());
692: assertEquals(4, ((Vector) model.dataVector.get(1)).size());
693: assertEquals(4, ((Vector) model.dataVector.get(2)).size());
694: assertEquals("col4", model.columnIdentifiers.get(3));
695: assertTrue(listener.eventOccured());
696: }
697:
698: public void testGetColumnName() throws Exception {
699: assertEquals("A", model.getColumnName(0));
700: assertEquals("B", model.getColumnName(1));
701: model.setColumnCount(2);
702: assertEquals("A", model.getColumnName(0));
703: assertEquals("B", model.getColumnName(1));
704: model.setColumnIdentifiers(new Object[] { "col1", null, "col3",
705: new Integer(4) });
706: assertEquals("col1", model.getColumnName(0));
707: assertEquals("B", model.getColumnName(1));
708: assertEquals("col3", model.getColumnName(2));
709: assertEquals("4", model.getColumnName(3));
710: assertEquals("E", model.getColumnName(4));
711: testExceptionalCase(new ArrayIndexOutOfBoundsExceptionalCase() {
712: @Override
713: public void exceptionalAction() throws Exception {
714: model.getColumnName(-1);
715: }
716: });
717: }
718:
719: public void testIsCellEditable() throws Exception {
720: assertTrue(model.isCellEditable(2, 4));
721: }
722:
723: public void testGetSetValueAt() throws Exception {
724: TestTableModelListener listener = new TestTableModelListener();
725: model.addTableModelListener(listener);
726: model.columnIdentifiers = new Vector(Arrays
727: .asList(new Object[] { "col1", "col2" }));
728: model.dataVector = new Vector();
729: model.dataVector.add(new Vector(Arrays.asList(new Object[] {
730: "value11", "value12" })));
731: model.dataVector.add(new Vector(Arrays.asList(new Object[] {
732: "value21", "value22", "value23" })));
733: model.dataVector.add(new Vector(Arrays
734: .asList(new Object[] { "value31" })));
735: assertEquals("value22", model.getValueAt(1, 1));
736: assertEquals("value31", model.getValueAt(2, 0));
737: testExceptionalCase(new ArrayIndexOutOfBoundsExceptionalCase() {
738: @Override
739: public void exceptionalAction() throws Exception {
740: model.getValueAt(2, 1);
741: }
742: });
743: testExceptionalCase(new ArrayIndexOutOfBoundsExceptionalCase() {
744: @Override
745: public void exceptionalAction() throws Exception {
746: model.setValueAt("value32", 2, 1);
747: }
748: });
749: listener.reset();
750: model.setValueAt(new Integer(3), 1, 0);
751: assertEquals(new Integer(3), model.getValueAt(1, 0));
752: assertTrue(listener.eventOccured());
753: assertEquals(model, listener.getEvent().getSource());
754: assertEquals(1, listener.getEvent().getFirstRow());
755: assertEquals(1, listener.getEvent().getLastRow());
756: assertEquals(0, listener.getEvent().getColumn());
757: assertEquals(TableModelEvent.UPDATE, listener.getEvent()
758: .getType());
759: }
760:
761: public void testConvertToVector() throws Exception {
762: Object[] array = new Object[] { "1", new Integer(6),
763: new String[] { "31", "32" } };
764: assertEquals(new Vector(Arrays.asList(array)),
765: DefaultTableModel.convertToVector(array));
766: Object[][] arrayOfArray = new Object[][] {
767: new Object[] { "1" },
768: new Object[] { new Integer(6), new Character('c') },
769: new String[] { "31", "32" } };
770: Vector expected = new Vector();
771: expected.add(Arrays.asList(new Object[] { "1" }));
772: expected.add(Arrays.asList(new Object[] { new Integer(6),
773: new Character('c') }));
774: expected.add(Arrays.asList(new String[] { "31", "32" }));
775: assertEquals(expected, DefaultTableModel
776: .convertToVector(arrayOfArray));
777: }
778: }
|