KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdesktop > dataset > DataTableTest


1 /*
2  * $Id: DataTableTest.java,v 1.4 2005/02/28 12:02:30 kleopatra Exp $
3  *
4  * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
5  * Santa Clara, California 95054, U.S.A. All rights reserved.
6  */

7
8 package org.jdesktop.dataset;
9
10 import junit.framework.*;
11 import java.util.List JavaDoc;
12
13 import org.jdesktop.dataset.provider.LoadTask;
14 import org.jdesktop.dataset.provider.SaveTask;
15
16
17 /**
18  *
19  * @author rbair
20  */

21 public class DataTableTest extends TestCase {
22     
23     public DataTableTest(String JavaDoc testName) {
24         super(testName);
25     }
26     
27     // TODO add test methods here. The name must begin with 'test'. For example:
28
// public void testHello() {}
29

30     protected void setUp() throws Exception JavaDoc {
31     }
32
33     protected void tearDown() throws Exception JavaDoc {
34     }
35
36     public static Test suite() {
37         TestSuite suite = new TestSuite(DataTableTest.class);
38         
39         return suite;
40     }
41
42     /**
43      * Test of createDataColumn method, of class org.jdesktop.dataset.DataTable.
44      */

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         //assert that the column was created
52
assertNotNull(col);
53         //test that changing the column name stays in sync in the table,
54
//and that the table returns the column by name
55
col.setName("col");
56         assertEquals(table.getColumn("col"), col);
57         //test that retrieving all of the columns works
58
List JavaDoc<DataColumn> cols = table.getColumns();
59         assertEquals(cols.size(), 1);
60         assertEquals(cols.get(0), col);
61     }
62
63     /**
64      * Test of getColumns method, of class org.jdesktop.dataset.DataTable.
65      */

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         //assert that the column was created
73
assertNotNull(col);
74         //test that changing the column name stays in sync in the table,
75
//and that the table returns the column by name
76
col.setName("col");
77         assertEquals(table.getColumn("col"), col);
78         //test that retrieving all of the columns works
79
List JavaDoc<DataColumn> cols = table.getColumns();
80         assertEquals(cols.size(), 1);
81         assertEquals(cols.get(0), col);
82     }
83
84     /**
85      * Test of getSelectors, getSelector, addSelector, dropSelector methods,
86      * of class org.jdesktop.dataset.DataTable.
87      */

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         //assert that the selector was created
95
assertNotNull(sel);
96         //test that changing the selector name stays in sync in the table,
97
//and that the table returns the selector by name
98
sel.setName("sel");
99         assertEquals(table.getSelector("sel"), sel);
100         //test that retrieving all of the selectors works
101
List JavaDoc<DataSelector> sels = table.getSelectors();
102         assertEquals(sels.size(), 1);
103         assertEquals(sels.get(0), sel);
104         //add some more
105
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         //drop the first one by reference
112
table.dropSelector(sel);
113         assertEquals(2, table.getSelectors().size());
114         assertEquals("sel2", table.getSelector("sel2").getName());
115         assertEquals("sel3", table.getSelector("sel3").getName());
116         //drop the last one by name
117
table.dropSelector("sel3");
118         assertEquals(1, table.getSelectors().size());
119         assertEquals("sel2", table.getSelector("sel2").getName());
120     }
121
122     /**
123      * Test of getRows method, of class org.jdesktop.dataset.DataTable.
124      */

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         //test that I CANNOT get row 0
133
try {
134             table.getRow(0);
135             assertTrue(false);
136         } catch (IndexOutOfBoundsException JavaDoc e) {
137             assertTrue(true);
138         }
139         //assert that the row was created
140
DataRow row = table.appendRow();
141         assertNotNull(row);
142         //test that I can get row 0
143
assertNotNull(table.getRow(0));
144         //test that I CANNOT get row 1
145
try {
146             table.getRow(1);
147             assertTrue(false);
148         } catch (IndexOutOfBoundsException JavaDoc e) {
149             assertTrue(true);
150         }
151         
152         //add 2 more rows
153
row.setValue("pid", 100);
154         table.appendRow().setValue("pid", 101);
155         table.appendRow().setValue("pid", 102);
156         
157         //test getting all of the rows back
158
List JavaDoc<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     /**
166      * Test of getName method, of class org.jdesktop.dataset.DataTable.
167      */

168     public void testGetName() {
169         System.out.println("testGetName");
170         
171         //test that the name can be set
172
DataSet ds = new DataSet();
173         DataTable table = ds.createTable();
174         table.setName("table_1");
175         assertEquals(table.getName(), "table_1");
176         
177         //test that the name cannot be set to null
178
try {
179             table.setName(null);
180             assertTrue(false);
181         } catch (Error JavaDoc e) {
182             assertTrue(true);
183         }
184         
185         //test that the name cannot be set to empty string
186
try {
187             table.setName("");
188             assertTrue(false);
189         } catch (Error JavaDoc e) {
190             assertTrue(true);
191         }
192         
193         
194         //test that the name cannot be set to empty string with spaces
195
try {
196             table.setName(" ");
197             assertTrue(false);
198         } catch (Error JavaDoc e) {
199             assertTrue(true);
200         }
201     }
202
203     /**
204      * Test of isDeleteRecordSupported method, of class org.jdesktop.dataset.DataTable.
205      */

206     public void testIsDeleteRecordSupported() {
207         System.out.println("testIsDeleteRecordSupported");
208         
209         DataSet ds = new DataSet();
210         DataTable table = ds.createTable();
211         //test that this is true by default
212
assertTrue(table.isDeleteRowSupported());
213         //test that the flag changes to false
214
table.setDeleteRowSupported(false);
215         assertFalse(table.isDeleteRowSupported());
216         //test that the flag stays false
217
table.setDeleteRowSupported(false);
218         assertFalse(table.isDeleteRowSupported());
219         //test that the flag goes to true
220
table.setDeleteRowSupported(true);
221         assertTrue(table.isDeleteRowSupported());
222         //test that the flag stays true
223
table.setDeleteRowSupported(true);
224         assertTrue(table.isDeleteRowSupported());
225     }
226
227     /**
228      * Test of isAppendRecordSupported method, of class org.jdesktop.dataset.DataTable.
229      */

230     public void testIsAppendRecordSupported() {
231         System.out.println("testIsAppendRecordSupported");
232         
233         DataSet ds = new DataSet();
234         DataTable table = ds.createTable();
235         //test that this is true by default
236
assertTrue(table.isAppendRowSupported());
237         //test that the flag changes to false
238
table.setAppendRowSupported(false);
239         assertFalse(table.isAppendRowSupported());
240         //test that the flag stays false
241
table.setAppendRowSupported(false);
242         assertFalse(table.isAppendRowSupported());
243         //test that the flag goes to true
244
table.setAppendRowSupported(true);
245         assertTrue(table.isAppendRowSupported());
246         //test that the flag stays true
247
table.setAppendRowSupported(true);
248         assertTrue(table.isAppendRowSupported());
249     }
250
251     /**
252      * Test of getDataProvider method, of class org.jdesktop.dataset.DataTable.
253      */

254     public void testGetDataProvider() {
255         System.out.println("testGetDataProvider");
256         
257         DataSet ds = new DataSet();
258         DataTable table = ds.createTable();
259         //assert that the provider is null by default
260
assertNull(table.getDataProvider());
261         //add a provider
262
DataProvider provider = new TestDataProvider();
263         table.setDataProvider(provider);
264         assertNotNull(table.getDataProvider());
265         //set to null
266
table.setDataProvider(null);
267         assertNull(table.getDataProvider());
268     }
269
270     /**
271      * Test of getDataSet method, of class org.jdesktop.dataset.DataTable.
272      */

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     /**
282      * Test of appendRow method, of class org.jdesktop.dataset.DataTable.
283      */

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         //test that I CANNOT get row 0
292
try {
293             table.getRow(0);
294             assertTrue(false);
295         } catch (IndexOutOfBoundsException JavaDoc e) {
296             assertTrue(true);
297         }
298         //assert that the row was created
299
DataRow row = table.appendRow();
300         assertNotNull(row);
301         //test that I can get row 0
302
assertNotNull(table.getRow(0));
303         //test that I CANNOT get row 1
304
try {
305             table.getRow(1);
306             assertTrue(false);
307         } catch (IndexOutOfBoundsException JavaDoc e) {
308             assertTrue(true);
309         }
310
311         //turn off append row supported
312
table.setAppendRowSupported(false);
313         
314         //try to add something
315
assertNull(table.appendRow());
316     }
317
318     /**
319      * Test of deleteRow method, of class org.jdesktop.dataset.DataTable.
320      */

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         //test that everything worked
333
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         //delete the middle row
342
table.deleteRow(1);
343         //assert that I still have 3 rows, but that the middle one is now marked
344
//as deleted
345
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         //turn off delete row supported
354
table.setDeleteRowSupported(false);
355         
356         //try to delete something
357
table.deleteRow(2);
358         //assert that I still have 3 rows, but that the middle one is now marked
359
//as deleted
360
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     /**
370      * Test of load method, of class org.jdesktop.dataset.DataTable.
371      */

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     /**
387      * Test of save method, of class org.jdesktop.dataset.DataTable.
388      */

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     /**
404      * Test of clear method, of class org.jdesktop.dataset.DataTable.
405      */

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     /**
423      * Test of refresh method, of class org.jdesktop.dataset.DataTable.
424      */

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     /**
440      * Test of getValue method, of class org.jdesktop.dataset.DataTable.
441      */

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     /**
459      * Test of indexOfRow method, of class org.jdesktop.dataset.DataTable.
460      */

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     /**
476      * Simple class that records when the save/load methods are called, and
477      * provides a method to clear those flags. This is used to test the
478      * save/refresh/load etc methods of the DataTable
479      *
480      * JW: changed access to use from DataTableIssues.
481      */

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             // TODO Auto-generated method stub
501
return null;
502         }
503
504         protected LoadTask createLoadTask(DataTable[] tables) {
505             // TODO Auto-generated method stub
506
return null;
507         }
508     }
509 }
510
Popular Tags