1 7 8 package org.jdesktop.swing.data; 9 10 import java.beans.PropertyChangeEvent ; 11 import java.beans.PropertyChangeListener ; 12 13 import java.io.InputStream ; 14 import java.io.IOException ; 15 16 import java.net.MalformedURLException ; 17 import java.net.URL ; 18 19 import java.util.ArrayList ; 20 import java.util.Locale ; 21 22 import javax.swing.SwingUtilities ; 23 import javax.swing.event.TableModelEvent ; 24 import javax.swing.event.TableModelListener ; 25 26 import junit.framework.Test; 27 import junit.framework.TestCase; 28 import junit.framework.TestSuite; 29 30 import org.jdesktop.swing.event.ProgressEvent; 31 import org.jdesktop.swing.event.ProgressListener; 32 33 34 39 public class DefaultTableModelExtUnitTest extends TestCase { 40 41 private static final String PREFIX = "/org/jdesktop/swing/resources/"; 43 44 private static final String BUGDATA = PREFIX + "bugdata.txt"; 45 public static String DATA_URL_STR; 46 47 private static final String STRINGDATA = PREFIX + "stringdata.csv"; 48 private static final String STRINGDATA2 = PREFIX + "stringdata2.txt"; 49 public static String STRINGDATA_URL_STR; 50 public static String STRINGDATA2_URL_STR; 51 public static final String STRINGDATA_COL_NAMES[] = {"firstname", 52 "lastname", "street", "city", "state", "zipcode", "phonenumber"}; 53 54 public URL dataURL; 55 public URL stringDataURL; 56 public URL stringData2URL; 57 58 public static void verifyTabularDataModelProperties(DefaultTableModelExt data, 59 int columnCount, 60 int rowCount, 61 URL sourceURL, 62 DataLoader loader, 63 boolean loading) { 64 65 assertEquals(columnCount, data.getColumnCount()); 66 assertEquals(rowCount, data.getRowCount()); 67 assertEquals(sourceURL, data.getSource()); 68 DataLoader theLoader = data.getLoader(); 69 if (loader == null) { 70 assertEquals(loader, theLoader); 71 } 72 else { 73 assertEquals(loader.getClass(), theLoader.getClass()); 74 } 75 assertEquals(loading, data.isLoading()); 76 77 } 78 79 public static void verifyTabularDataTextLoaderProperties( 80 TableModelExtTextLoader loader, 81 String columnDelimiter, 82 boolean columnNamesInFirstRow, 83 int blockIncrementSize) { 84 assertEquals(columnDelimiter, loader.getColumnDelimiter()); 85 assertEquals(columnNamesInFirstRow, loader.isFirstRowHeader()); 86 assertEquals(blockIncrementSize, loader.getBlockIncrementSize()); 87 } 88 89 90 public void setUp() throws Exception { 91 super.setUp(); 92 System.setProperty(DataLoader.READER_PRIORITY_KEY, String 93 .valueOf(Thread.NORM_PRIORITY)); 94 95 96 dataURL = DefaultTableModelExtUnitTest.class.getResource(BUGDATA); 97 DATA_URL_STR = dataURL.toExternalForm(); 98 99 stringDataURL = DefaultTableModelExtUnitTest.class.getResource(STRINGDATA); 100 STRINGDATA_URL_STR = stringDataURL.toExternalForm(); 101 102 stringData2URL = DefaultTableModelExtUnitTest.class.getResource(STRINGDATA2); 103 STRINGDATA2_URL_STR = stringData2URL.toExternalForm(); 104 105 SwingUtilities.invokeLater(new Runnable () { 107 public void run() { 108 ; 109 } 110 }); 111 } 112 113 114 protected void tearDown() throws Exception { 115 System.getProperties().remove(DataLoader.READER_PRIORITY_KEY); 116 117 super.tearDown(); 118 } 119 public void testTabularDataModelConstructors() { 120 DefaultTableModelExt data = null; 121 122 data = new DefaultTableModelExt(); 123 verifyTabularDataModelProperties(data, 0, 0, null, 124 new TableModelExtTextLoader(), 125 false); 126 127 data = new DefaultTableModelExt(5); 128 verifyTabularDataModelProperties(data, 5, 0, null, 129 new TableModelExtTextLoader(), 130 false); 131 132 try { 133 data = new DefaultTableModelExt(DATA_URL_STR); 134 verifyTabularDataModelProperties(data, 8, 0, dataURL, 135 new TableModelExtTextLoader(), 136 false); 137 } 138 catch (MalformedURLException e) { 139 fail("testConstructors MalFormedURLException: url="+DATA_URL_STR); 140 } 141 142 try { 143 data = new DefaultTableModelExt(DATA_URL_STR, 8); 144 verifyTabularDataModelProperties(data, 8, 0, dataURL, 145 new TableModelExtTextLoader(), 146 false); 147 } 148 catch (MalformedURLException e) { 149 fail("testConstructors MalFormedURLException: url="+DATA_URL_STR); 150 } 151 152 } 153 154 public void testTabularDataModelProperties() { 155 DefaultTableModelExt data = new DefaultTableModelExt(); 156 data.setColumnCount(10); 157 data.setSource(dataURL); 158 DataLoader dummyLoader = new DummyDataLoader(); 159 data.setLoader(dummyLoader); 160 161 verifyTabularDataModelProperties(data, 10, 0, dataURL, 162 dummyLoader, false); 163 164 try { 165 data.setColumnCount( -2); 166 fail("should not allow setting columnCount to negative number"); 167 } catch (IllegalArgumentException e) { 168 } 170 } 171 172 public void testTabularDataModelColumnMetaData() { 173 int columnCount = 3; 174 DefaultTableModelExt data = new DefaultTableModelExt(columnCount); 175 176 for (int i = 0; i < columnCount; i ++) { 177 MetaData metaData = data.getColumnMetaData(i); 178 assertNotNull(metaData); 179 assertEquals(String .class, metaData.getElementClass()); 180 assertEquals("column"+i, metaData.getName()); 181 assertEquals(String .class, data.getColumnClass(i)); 182 assertEquals("column"+i, data.getColumnName(i)); 184 MetaData newMetaData = new MetaData("field"+i, Integer .class); 185 data.setColumnMetaData(i, newMetaData); 186 metaData = data.getColumnMetaData(i); 187 assertEquals(newMetaData, metaData); 188 assertEquals(Integer .class, data.getColumnClass(i)); 189 assertEquals("field"+i, data.getColumnName(i)); } 191 } 192 193 public void testTabularDataModelLoading() { 194 DefaultTableModelExt data = null; 195 try { 196 data = new DefaultTableModelExt(DATA_URL_STR); 197 198 final TableModelEventTracker tracker = new TableModelEventTracker(1) { 200 private int rowCount = 0; 201 public void tableChanged(TableModelEvent e) { 202 if (e.getType() == TableModelEvent.INSERT) { 203 rowCount += (e.getLastRow() - e.getFirstRow()); 204 if (rowCount == 1275) { 205 gotEvent[0] = true; 206 } 207 } 208 } 209 }; 210 data.addTableModelListener(tracker); 211 212 PropertyChangeListener pcl = new PropertyChangeListener () { 213 private boolean doingLoad = false; 214 public void propertyChange(PropertyChangeEvent e) { 215 DefaultTableModelExt data = (DefaultTableModelExt)e.getSource(); 216 if (e.getPropertyName().equals("loading")) { 217 boolean loading = ((Boolean )e.getNewValue()).booleanValue(); 218 if (!doingLoad && loading) { 219 doingLoad = true; 220 } 221 if (doingLoad && !loading) { 222 int count = data.getColumnCount(); 224 assertEquals("ERROR: columnCount should be 8 != " + count, 225 8, count); 226 count = data.getRowCount(); 227 assertEquals("ERROR: rowCount should be 1275 == " + count, 228 1275, count); 229 assertTrue(tracker.gotEvent[0]); 230 data.removePropertyChangeListener(this); 231 } 232 } 233 } 234 }; 235 data.addPropertyChangeListener(pcl); 236 237 data.startLoading(); 238 } 239 catch (Exception e) { 240 fail("testTabularDataModelLoading: exception: msg="+e.getMessage()); 241 } 242 243 try { 245 data = new DefaultTableModelExt(stringDataURL); 246 TableModelExtTextLoader loader = (TableModelExtTextLoader) data. 247 getLoader(); 248 loader.setColumnDelimiter(","); 249 loader.setFirstRowHeader(true); 250 } catch (Exception e) { 251 fail("testTabularDataModelLoading: "+STRINGDATA_URL_STR+" "+e.getMessage()); 252 } 253 assertEquals(7, data.getColumnCount()); 254 assertEquals("firstname", data.getColumnName(0)); 255 assertEquals("lastname", data.getColumnName(1)); 256 assertEquals("street", data.getColumnName(2)); 257 assertEquals("city", data.getColumnName(3)); 258 assertEquals("state", data.getColumnName(4)); 259 assertEquals("zipcode", data.getColumnName(5)); 260 assertEquals("phonenumber", data.getColumnName(6)); 261 262 try { 263 data.startLoading(); 264 } catch (Exception e) { 265 fail("testTabularDataModelLoading: "+STRINGDATA_URL_STR+" "+e.getMessage()); 266 } 267 268 while(data.isLoading()); 269 270 assertEquals(10, data.getRowCount()); 271 } 272 273 public void testTabularDataModelInsert() { 274 final int listRowCount = 3; 275 DefaultTableModelExt data = new DefaultTableModelExt(3); 276 Object row[] = {"John", "Doe", new Integer (25)}; 277 Object dataRow[] = null; 278 ArrayList rowList = new ArrayList (); 279 for(int i = 0; i < listRowCount; i++) { 280 rowList.add(row); 281 } 282 283 TableModelEventTracker tracker = new TableModelEventTracker(4) { 285 public void tableChanged(TableModelEvent e) { 286 if (e.getType() == TableModelEvent.INSERT) { 287 if (e.getFirstRow() == 0 && e.getLastRow() == 0) { 288 gotEvent[0] = true; 289 } 290 if (e.getFirstRow() == 1 && e.getLastRow() == listRowCount) { 291 gotEvent[1] = true; 292 } 293 if (e.getFirstRow() == 3 && e.getLastRow() == 3) { 294 gotEvent[2] = true; 295 } 296 if (e.getFirstRow() == 2 && e.getLastRow() == listRowCount+1) { 297 gotEvent[3] = true; 298 } 299 } 300 } 301 }; 302 data.addTableModelListener(tracker); 303 304 data.addRow(row); 306 assertEquals(1, data.getRowCount()); 307 dataRow = data.getRow(0); 308 assertEquals(row, dataRow); 309 assertNotSame(row, dataRow); 311 assertTrue(tracker.gotEvent[0]); 312 313 row[0] = "Jane"; 315 data.addRows(rowList); 316 assertEquals(1+listRowCount, data.getRowCount()); 317 for(int i = 1; i < listRowCount+1; i++) { 318 dataRow = data.getRow(i); 319 assertEquals(row, dataRow); 320 assertNotSame(row, dataRow); 322 } 323 assertTrue(tracker.gotEvent[1]); 324 325 row[1] = "Smith"; 327 data.insertRow(3, row); 328 assertEquals(2+listRowCount, data.getRowCount()); 329 dataRow = data.getRow(3); 330 assertEquals(row, dataRow); 331 assertNotSame(row, dataRow); 333 assertTrue(tracker.gotEvent[2]); 334 335 row[0] = "Jim"; 337 data.insertRows(2, rowList); 338 assertEquals(2+(2*listRowCount), data.getRowCount()); 339 for(int i = 2; i < listRowCount+2; i++) { 340 dataRow = data.getRow(i); 341 assertEquals(row, dataRow); 342 assertNotSame(row, dataRow); 344 } 345 assertTrue(tracker.gotEvent[3]); 346 } 347 348 public void testTabularDataModelDelete() { 349 350 Object dataRow[] = null; 351 int rowCount = 10; 352 DefaultTableModelExt data = initializeSimpleTabularDataModel(rowCount); 353 354 TableModelEventTracker tracker = new TableModelEventTracker(2) { 356 public void tableChanged(TableModelEvent e) { 357 if (e.getType() == TableModelEvent.DELETE) { 358 if (e.getFirstRow() == 0 && e.getLastRow() == 0) { 359 gotEvent[0] = true; 360 } 361 if (e.getFirstRow() == 0 && e.getLastRow() == 3) { 362 gotEvent[1] = true; 363 } 364 } 365 } 366 }; 367 data.addTableModelListener(tracker); 368 369 data.deleteRow(0); 371 assertEquals(rowCount-1, data.getRowCount()); 372 dataRow = data.getRow(0); 374 assertEquals("value1", dataRow[0]); 375 assertEquals(new Integer (1), dataRow[1]); 376 assertTrue(tracker.gotEvent[0]); 377 378 data.deleteRows(0,3); 380 assertEquals(rowCount-5, data.getRowCount()); 381 assertTrue(tracker.gotEvent[1]); 382 383 data.deleteRow(data.getRowCount()-1); 385 assertEquals(rowCount-6, data.getRowCount()); 386 387 } 388 389 public void testTabularDataModelGetValue() { 390 int rowCount = 10; 391 DefaultTableModelExt data = initializeSimpleTabularDataModel(rowCount); 392 393 assertEquals("value1", (String )data.getValueAt(1, 0)); 394 assertEquals(new Integer (0), (Integer )data.getValueAt(0, 1)); 395 assertEquals(Boolean.TRUE, (Boolean )data.getValueAt(3, 2)); 396 assertEquals(new Float (5*3.3), (Float )data.getValueAt(5, 3)); 397 assertEquals(new Long (9+5555), (Long )data.getValueAt(9, 4)); 398 399 } 400 401 public void testTabularDataModelSetValue() { 402 int rowCount = 10; 403 DefaultTableModelExt data = initializeSimpleTabularDataModel(rowCount); 404 405 TableModelEventTracker tracker = new TableModelEventTracker(2) { 407 public void tableChanged(TableModelEvent e) { 408 if (e.getType() == TableModelEvent.UPDATE) { 409 if (e.getFirstRow() == 0 && e.getLastRow() == 0 && 410 e.getColumn() == 0) { 411 gotEvent[0] = true; 412 } 413 if (e.getFirstRow() == 5 && e.getLastRow() == 5 && 414 e.getColumn() == 2) { 415 gotEvent[1] = true; 416 } 417 } 418 } 419 }; 420 data.addTableModelListener(tracker); 421 422 data.setValueAt("string0", 0, 0); 424 assertEquals("string0", (String )data.getValueAt(0,0)); 425 assertTrue(tracker.gotEvent[0]); 426 427 data.setValueAt(Boolean.FALSE, 5, 2); 429 assertEquals(Boolean.FALSE, (Boolean )data.getValueAt(5,2)); 430 assertTrue(tracker.gotEvent[1]); 431 } 432 433 public void testTabularDataModelgetColumnIndex() { 434 int rowCount = 5; 435 DefaultTableModelExt data = initializeSimpleTabularDataModel(rowCount); 436 437 assertEquals(0, data.getColumnIndex("stringcolumn")); 438 assertEquals(4, data.getColumnIndex("longcolumn")); 439 440 try { 441 data.getColumnIndex("whichcolumn"); 442 fail("getColumnIndex should throw IllegalArgumentException"); 443 } catch (IllegalArgumentException e) { 444 } 446 } 447 448 public void testTabularDataModelgetColumnName() { 449 int rowCount = 5; 450 DefaultTableModelExt data = initializeSimpleTabularDataModel(rowCount); 451 452 assertEquals("Strings", data.getColumnName(0)); 454 assertEquals("Integers", data.getColumnName(1)); 455 assertEquals("Booleans", data.getColumnName(2)); 456 assertEquals("Floats", data.getColumnName(3)); 457 assertEquals("Longs", data.getColumnName(4)); 458 } 459 460 461 public void testTabularDataTextLoaderConstructors() { 462 TableModelExtTextLoader loader = new TableModelExtTextLoader(); 463 verifyTabularDataTextLoaderProperties(loader, "\t", false, 50); 464 465 loader = new TableModelExtTextLoader("foo", true, 100); 466 verifyTabularDataTextLoaderProperties(loader, "foo", true, 100); 467 } 468 469 public void testTabularDataTextLoaderProperties() { 470 TableModelExtTextLoader loader = new TableModelExtTextLoader(); 471 loader.setBlockIncrementSize(25); 472 loader.setColumnDelimiter("bar"); 473 loader.setFirstRowHeader(true); 474 verifyTabularDataTextLoaderProperties(loader, "bar", true, 25); 475 } 476 477 public void testTabularDataTextLoaderInitializeMetaData() { 478 DefaultTableModelExt data = new DefaultTableModelExt(); 479 TableModelExtTextLoader loader = new TableModelExtTextLoader(",", true, 480 100); 481 try { 482 final InputStream is = stringDataURL.openStream(); 483 loader.loadMetaData(data, is); 484 485 int columnCount = data.getColumnCount(); 486 assertEquals(7, columnCount); 487 for (int i = 0; i < columnCount; i++) { 488 MetaData metaData = data.getColumnMetaData(i); 489 assertEquals(STRINGDATA_COL_NAMES[i], metaData.getName()); 490 } 491 } catch (Exception e) { 492 fail("testTabularDataTextLoaderInitializeMetaData exception: msg="+e.getMessage()); 493 } 494 } 495 496 500 public void testTabularDataTextLoaderStartLoading() { 501 DefaultTableModelExt data = new DefaultTableModelExt(); 502 TableModelExtTextLoader loader = new TableModelExtTextLoader(",", true, 3); 503 try { 504 InputStream is = stringDataURL.openStream(); 505 ProgressTracker progressTracker = new ProgressTracker(); 506 loader.addProgressListener(progressTracker); 507 loader.loadMetaData(data, is); 508 is.close(); 509 510 is = stringDataURL.openStream(); 511 loader.startLoading(data, is); 512 513 while(!progressTracker.done && progressTracker.error == null); 514 515 assertTrue(progressTracker.done); 516 assertEquals(10, data.getRowCount()); 517 assertEquals("Gibson", (String )data.getValueAt(7,1)); 518 } catch (Exception e) { 519 fail("testTabularDataTextLoaderStartLoading exception: msg="+e.getMessage()); 520 } 521 522 data = new DefaultTableModelExt(); 523 loader = new TableModelExtTextLoader("boo", false, 10); 524 try { 525 InputStream is = stringData2URL.openStream(); 526 ProgressTracker progressTracker = new ProgressTracker(); 527 loader.addProgressListener(progressTracker); 528 loader.loadMetaData(data, is); 529 is.close(); 530 531 is = stringData2URL.openStream(); 532 loader.startLoading(data, is); 533 534 while (!progressTracker.done && progressTracker.error == null); 535 536 assertTrue(progressTracker.done); 537 assertEquals(30, data.getRowCount()); 538 assertEquals("two", (String ) data.getValueAt(0, 1)); 539 assertEquals("5", (String )data.getValueAt(1,4)); 540 assertEquals("first", (String )data.getValueAt(5, 0)); 541 assertEquals("six", (String )data.getValueAt(9,5)); 542 } 543 catch (Exception e) { 544 fail("testTabularDataTextLoaderStartLoading exception: msg=" + 545 e.getMessage()); 546 } 547 } 548 549 public void assertEquals(Object [] expected, Object [] actual) { 550 assertEquals(expected.length, actual.length); 551 for(int i = 0; i < expected.length; i++) { 552 assertEquals(expected[i], actual[i]); 553 } 554 } 555 556 protected DefaultTableModelExt initializeSimpleTabularDataModel(int rowCount) { 557 int columnCount = 5; 558 DefaultTableModelExt data = new DefaultTableModelExt(columnCount); 559 data.getColumnMetaData(0).setName("stringcolumn"); 560 data.getColumnMetaData(0).setLabel("Strings"); 561 data.getColumnMetaData(1).setName("intcolumn"); 562 data.getColumnMetaData(1).setLabel("Integers"); 563 data.getColumnMetaData(2).setName("booleancolumn"); 564 data.getColumnMetaData(2).setLabel("Booleans"); 565 data.getColumnMetaData(3).setName("floatcolumn"); 566 data.getColumnMetaData(3).setLabel("Floats"); 567 data.getColumnMetaData(4).setName("longcolumn"); 568 data.getColumnMetaData(4).setLabel("Longs"); 569 570 data.getColumnMetaData(1).setElementClass(Integer .class); 571 data.getColumnMetaData(2).setElementClass(Boolean .class); 572 data.getColumnMetaData(3).setElementClass(Float .class); 573 data.getColumnMetaData(4).setElementClass(Long .class); 574 575 for (int i = 0; i < rowCount; i++) { 576 Object row[] = new Object [columnCount]; 577 row[0] = "value" + i; 578 row[1] = new Integer (i); 579 row[2] = Boolean.TRUE; 580 row[3] = new Float (i*3.3); 581 row[4] = new Long (i+5555); 582 data.addRow(row); 583 } 584 return data; 585 } 586 587 private class DummyDataLoader extends DataLoader { 588 public void loadMetaData(Object model, InputStream is) { 589 590 } 591 public void readData(InputStream is) throws IOException , 592 ConversionException { 593 } 594 public void loadData(Object model) { 595 } 596 597 } 598 599 private abstract class TableModelEventTracker implements TableModelListener { 600 public boolean gotEvent[]; 601 public TableModelEventTracker(int testCount) { 602 gotEvent = new boolean[testCount]; 603 for(int i = 0; i < gotEvent.length; i++) { 604 gotEvent[i] = false; 605 } 606 } 607 public abstract void tableChanged(TableModelEvent e); 608 } 609 610 628 private class ProgressTracker implements ProgressListener { 629 public boolean done; 630 public Throwable error; 631 public ProgressTracker() { 632 done = false; 633 error = null; 634 } 635 public void progressStarted(ProgressEvent e) { 636 637 } 638 public void progressIncremented(ProgressEvent e) { 639 640 } 641 public void progressEnded(ProgressEvent evt) { 642 done = true; 643 } 644 public void exception(ProgressEvent evt) { 645 error = evt.getThrowable(); 646 } 647 } 648 649 652 676 } 677 | Popular Tags |