1 7 8 package org.jdesktop.dataset; 9 10 import junit.framework.*; 11 import java.util.List ; 12 13 import org.jdesktop.dataset.provider.LoadTask; 14 import org.jdesktop.dataset.provider.SaveTask; 15 16 17 21 public class DataTableTest extends TestCase { 22 23 public DataTableTest(String testName) { 24 super(testName); 25 } 26 27 30 protected void setUp() throws Exception { 31 } 32 33 protected void tearDown() throws Exception { 34 } 35 36 public static Test suite() { 37 TestSuite suite = new TestSuite(DataTableTest.class); 38 39 return suite; 40 } 41 42 45 public void testCreateDataColumn() { 46 System.out.println("testCreateDataColumn"); 47 48 DataSet ds = new DataSet(); 49 DataTable table = ds.createTable(); 50 DataColumn col = table.createColumn(); 51 assertNotNull(col); 53 col.setName("col"); 56 assertEquals(table.getColumn("col"), col); 57 List <DataColumn> cols = table.getColumns(); 59 assertEquals(cols.size(), 1); 60 assertEquals(cols.get(0), col); 61 } 62 63 66 public void testGetColumns() { 67 System.out.println("testGetColumns"); 68 69 DataSet ds = new DataSet(); 70 DataTable table = ds.createTable(); 71 DataColumn col = table.createColumn(); 72 assertNotNull(col); 74 col.setName("col"); 77 assertEquals(table.getColumn("col"), col); 78 List <DataColumn> cols = table.getColumns(); 80 assertEquals(cols.size(), 1); 81 assertEquals(cols.get(0), col); 82 } 83 84 88 public void testSelectors() { 89 System.out.println("testSelectors"); 90 91 DataSet ds = new DataSet(); 92 DataTable table = ds.createTable(); 93 DataSelector sel = table.createSelector(); 94 assertNotNull(sel); 96 sel.setName("sel"); 99 assertEquals(table.getSelector("sel"), sel); 100 List <DataSelector> sels = table.getSelectors(); 102 assertEquals(sels.size(), 1); 103 assertEquals(sels.get(0), sel); 104 table.createSelector().setName("sel2"); 106 table.createSelector().setName("sel3"); 107 assertEquals(3, table.getSelectors().size()); 108 assertEquals("sel", table.getSelector("sel").getName()); 109 assertEquals("sel2", table.getSelector("sel2").getName()); 110 assertEquals("sel3", table.getSelector("sel3").getName()); 111 table.dropSelector(sel); 113 assertEquals(2, table.getSelectors().size()); 114 assertEquals("sel2", table.getSelector("sel2").getName()); 115 assertEquals("sel3", table.getSelector("sel3").getName()); 116 table.dropSelector("sel3"); 118 assertEquals(1, table.getSelectors().size()); 119 assertEquals("sel2", table.getSelector("sel2").getName()); 120 } 121 122 125 public void testGetRows() { 126 System.out.println("testGetRows"); 127 128 DataSet ds = new DataSet(); 129 DataTable table = ds.createTable(); 130 DataColumn column = table.createColumn(); 131 column.setName("pid"); 132 try { 134 table.getRow(0); 135 assertTrue(false); 136 } catch (IndexOutOfBoundsException e) { 137 assertTrue(true); 138 } 139 DataRow row = table.appendRow(); 141 assertNotNull(row); 142 assertNotNull(table.getRow(0)); 144 try { 146 table.getRow(1); 147 assertTrue(false); 148 } catch (IndexOutOfBoundsException e) { 149 assertTrue(true); 150 } 151 152 row.setValue("pid", 100); 154 table.appendRow().setValue("pid", 101); 155 table.appendRow().setValue("pid", 102); 156 157 List <DataRow> rows = table.getRows(); 159 assertEquals(rows.size(), 3); 160 assertEquals(rows.get(0).getValue("pid"), 100); 161 assertEquals(rows.get(1).getValue("pid"), 101); 162 assertEquals(rows.get(2).getValue("pid"), 102); 163 } 164 165 168 public void testGetName() { 169 System.out.println("testGetName"); 170 171 DataSet ds = new DataSet(); 173 DataTable table = ds.createTable(); 174 table.setName("table_1"); 175 assertEquals(table.getName(), "table_1"); 176 177 try { 179 table.setName(null); 180 assertTrue(false); 181 } catch (Error e) { 182 assertTrue(true); 183 } 184 185 try { 187 table.setName(""); 188 assertTrue(false); 189 } catch (Error e) { 190 assertTrue(true); 191 } 192 193 194 try { 196 table.setName(" "); 197 assertTrue(false); 198 } catch (Error e) { 199 assertTrue(true); 200 } 201 } 202 203 206 public void testIsDeleteRecordSupported() { 207 System.out.println("testIsDeleteRecordSupported"); 208 209 DataSet ds = new DataSet(); 210 DataTable table = ds.createTable(); 211 assertTrue(table.isDeleteRowSupported()); 213 table.setDeleteRowSupported(false); 215 assertFalse(table.isDeleteRowSupported()); 216 table.setDeleteRowSupported(false); 218 assertFalse(table.isDeleteRowSupported()); 219 table.setDeleteRowSupported(true); 221 assertTrue(table.isDeleteRowSupported()); 222 table.setDeleteRowSupported(true); 224 assertTrue(table.isDeleteRowSupported()); 225 } 226 227 230 public void testIsAppendRecordSupported() { 231 System.out.println("testIsAppendRecordSupported"); 232 233 DataSet ds = new DataSet(); 234 DataTable table = ds.createTable(); 235 assertTrue(table.isAppendRowSupported()); 237 table.setAppendRowSupported(false); 239 assertFalse(table.isAppendRowSupported()); 240 table.setAppendRowSupported(false); 242 assertFalse(table.isAppendRowSupported()); 243 table.setAppendRowSupported(true); 245 assertTrue(table.isAppendRowSupported()); 246 table.setAppendRowSupported(true); 248 assertTrue(table.isAppendRowSupported()); 249 } 250 251 254 public void testGetDataProvider() { 255 System.out.println("testGetDataProvider"); 256 257 DataSet ds = new DataSet(); 258 DataTable table = ds.createTable(); 259 assertNull(table.getDataProvider()); 261 DataProvider provider = new TestDataProvider(); 263 table.setDataProvider(provider); 264 assertNotNull(table.getDataProvider()); 265 table.setDataProvider(null); 267 assertNull(table.getDataProvider()); 268 } 269 270 273 public void testGetDataSet() { 274 System.out.println("testGetDataSet"); 275 276 DataSet ds = new DataSet(); 277 DataTable table = ds.createTable(); 278 assertEquals(ds, table.getDataSet()); 279 } 280 281 284 public void testAppendRow() { 285 System.out.println("testAppendRow"); 286 287 DataSet ds = new DataSet(); 288 DataTable table = ds.createTable(); 289 DataColumn column = table.createColumn(); 290 column.setName("pid"); 291 try { 293 table.getRow(0); 294 assertTrue(false); 295 } catch (IndexOutOfBoundsException e) { 296 assertTrue(true); 297 } 298 DataRow row = table.appendRow(); 300 assertNotNull(row); 301 assertNotNull(table.getRow(0)); 303 try { 305 table.getRow(1); 306 assertTrue(false); 307 } catch (IndexOutOfBoundsException e) { 308 assertTrue(true); 309 } 310 311 table.setAppendRowSupported(false); 313 314 assertNull(table.appendRow()); 316 } 317 318 321 public void testDeleteRow() { 322 System.out.println("testDeleteRow"); 323 324 DataSet ds = new DataSet(); 325 DataTable table = ds.createTable(); 326 DataColumn column = table.createColumn(); 327 column.setName("pid"); 328 table.appendRow().setValue("pid", 100); 329 table.appendRow().setValue("pid", 101); 330 table.appendRow().setValue("pid", 102); 331 332 assertEquals(3, table.getRows().size()); 334 assertEquals(table.getRow(0).getValue("pid"), 100); 335 assertEquals(DataRow.DataRowStatus.INSERTED, table.getRow(0).getStatus()); 336 assertEquals(table.getRow(1).getValue("pid"), 101); 337 assertEquals(DataRow.DataRowStatus.INSERTED, table.getRow(1).getStatus()); 338 assertEquals(table.getRow(2).getValue("pid"), 102); 339 assertEquals(DataRow.DataRowStatus.INSERTED, table.getRow(2).getStatus()); 340 341 table.deleteRow(1); 343 assertEquals(3, table.getRows().size()); 346 assertEquals(100, table.getRow(0).getValue("pid")); 347 assertEquals(DataRow.DataRowStatus.INSERTED, table.getRow(0).getStatus()); 348 assertEquals(101, table.getRow(1).getValue("pid")); 349 assertEquals(DataRow.DataRowStatus.DELETED, table.getRow(1).getStatus()); 350 assertEquals(102, table.getRow(2).getValue("pid")); 351 assertEquals(DataRow.DataRowStatus.INSERTED, table.getRow(2).getStatus()); 352 353 table.setDeleteRowSupported(false); 355 356 table.deleteRow(2); 358 assertEquals(3, table.getRows().size()); 361 assertEquals(100, table.getRow(0).getValue("pid")); 362 assertEquals(DataRow.DataRowStatus.INSERTED, table.getRow(0).getStatus()); 363 assertEquals(101, table.getRow(1).getValue("pid")); 364 assertEquals(DataRow.DataRowStatus.DELETED, table.getRow(1).getStatus()); 365 assertEquals(102, table.getRow(2).getValue("pid")); 366 assertEquals(DataRow.DataRowStatus.INSERTED, table.getRow(2).getStatus()); 367 } 368 369 372 public void testLoad() { 373 System.out.println("testLoad"); 374 375 DataSet ds = new DataSet(); 376 DataTable table = ds.createTable(); 377 TestDataProvider provider = new TestDataProvider(); 378 table.setDataProvider(provider); 379 assertFalse(provider.loaded); 380 assertFalse(provider.saved); 381 table.load(); 382 assertTrue(provider.loaded); 383 assertFalse(provider.saved); 384 } 385 386 389 public void testSave() { 390 System.out.println("testSave"); 391 392 DataSet ds = new DataSet(); 393 DataTable table = ds.createTable(); 394 TestDataProvider provider = new TestDataProvider(); 395 table.setDataProvider(provider); 396 assertFalse(provider.loaded); 397 assertFalse(provider.saved); 398 table.save(); 399 assertFalse(provider.loaded); 400 assertTrue(provider.saved); 401 } 402 403 406 public void testClear() { 407 System.out.println("testClear"); 408 409 DataSet ds = new DataSet(); 410 DataTable table = ds.createTable(); 411 DataColumn column = table.createColumn(); 412 column.setName("pid"); 413 table.appendRow().setValue("pid", 100); 414 table.appendRow().setValue("pid", 101); 415 table.appendRow().setValue("pid", 102); 416 assertEquals(3, table.getRows().size()); 417 418 table.clear(); 419 assertEquals(0, table.getRows().size()); 420 } 421 422 425 public void testRefresh() { 426 System.out.println("testRefresh"); 427 428 DataSet ds = new DataSet(); 429 DataTable table = ds.createTable(); 430 TestDataProvider provider = new TestDataProvider(); 431 table.setDataProvider(provider); 432 assertFalse(provider.loaded); 433 assertFalse(provider.saved); 434 table.load(); 435 assertTrue(provider.loaded); 436 assertFalse(provider.saved); 437 } 438 439 442 public void testGetValue() { 443 System.out.println("testGetValue"); 444 445 DataSet ds = new DataSet(); 446 DataTable table = ds.createTable(); 447 DataColumn column = table.createColumn(); 448 column.setName("pid"); 449 table.appendRow().setValue("pid", 100); 450 table.appendRow().setValue("pid", 101); 451 table.appendRow().setValue("pid", 102); 452 453 assertEquals(101, table.getRow(1).getValue("pid")); 454 assertEquals(100, table.getRow(0).getValue("pid")); 455 assertEquals(102, table.getRow(2).getValue("pid")); 456 } 457 458 461 public void testIndexOfRow() { 462 System.out.println("testIndexOfRow"); 463 464 DataSet ds = new DataSet(); 465 DataTable table = ds.createTable(); 466 DataRow row0 = table.appendRow(); 467 DataRow row1 = table.appendRow(); 468 DataRow row2 = table.appendRow(); 469 470 assertEquals(1, table.indexOfRow(row1)); 471 assertEquals(0, table.indexOfRow(row0)); 472 assertEquals(2, table.indexOfRow(row2)); 473 } 474 475 482 protected static final class TestDataProvider extends DataProvider { 483 private boolean saved = false; 484 private boolean loaded = false; 485 486 public void save(DataTable t) { 487 saved = true; 488 } 489 490 public void load(DataTable t) { 491 loaded = true; 492 } 493 494 private void clearFlags() { 495 saved = false; 496 loaded = false; 497 } 498 499 protected SaveTask createSaveTask(DataTable[] tables) { 500 return null; 502 } 503 504 protected LoadTask createLoadTask(DataTable[] tables) { 505 return null; 507 } 508 } 509 } 510 | Popular Tags |