KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Created on 24.02.2005
3  *
4  */

5 package org.jdesktop.dataset;
6
7 import java.util.List JavaDoc;
8
9 import junit.framework.TestCase;
10
11 import org.jdesktop.dataset.DataTableTest.TestDataProvider;
12 import org.jdesktop.swing.util.DataEventReport;
13 import org.jdesktop.swing.util.PropertyChangeReport;
14
15 /**
16  * A couple of test fixtures meant to clarify the
17  * supposed usage of DataTable and related classes.
18  *
19  *
20  * @author Jeanette Winzenburg
21  */

22 public class DataTableIssues extends TestCase {
23
24     /**
25      * Issue: event fired on selection change.
26      *
27      * JW: we can go with a PropertyChangeEvent for now -
28      * but it should be as near to the bean spec as possible.
29      *
30      *
31      */

32     public void testSelectorEvent() {
33         DataSet ds = new DataSet();
34         DataTable table = ds.createTable();
35         DataSelector sel = table.createSelector();
36         int rowCount = 2;
37         for (int i = 0; i < rowCount; i++) {
38             table.appendRow();
39         }
40         // sanity assert
41
assertEquals("table with rowCount rows", rowCount, table.getRows().size());
42         PropertyChangeReport report = new PropertyChangeReport();
43         sel.addPropertyChangeListener("rowIndices", report);
44         List JavaDoc oldIndices = sel.getRowIndices();
45         sel.setRowIndices(new int[] {0, 1});
46         assertTrue("selector must have fire property change", report.hasEvents());
47         assertEquals("old value must equal empty selection",
48                 oldIndices, report.getLastOldValue());
49         assertEquals("new value must equal indices",
50                 sel.getRowIndices(), report.getLastNewValue());
51     }
52
53     /**
54      * Issue: selection of non-existing rows.
55      * JW: I would expect the selector to either not
56      * allow it in the first place or silently adjust
57      * to valid row indices (see test below this).
58      *
59      */

60     public void testSelectorSelectNonExistingRows() {
61         DataSet ds = new DataSet();
62         DataTable table = ds.createTable();
63         DataSelector sel = table.createSelector();
64         // sanity assert
65
assertEquals("empty table", 0, table.getRows().size());
66         sel.setRowIndices(new int[] {0, 1});
67         fail("selector must not allow selection of non-existing rows");
68     }
69     
70     /**
71      * Issue: selection of non-existing rows.
72      * JW: I would expect the selector (if it allows the method)
73      * to at least not return a selectionIndex to a non-existing
74      * row.
75      *
76      * The reasoning is that clients should be reasonably safe
77      * that the commented code block will succeed.
78      *
79      */

80     public void testSelectorSelectNonExistingRowsAlternative() {
81         DataSet ds = new DataSet();
82         DataTable table = ds.createTable();
83         DataSelector sel = table.createSelector();
84         DataColumn column = table.createColumn();
85         column.setName("pid");
86         // sanity assert
87
assertEquals("empty table", 0, table.getRows().size());
88         sel.setRowIndices(new int[] {0, 1});
89         List JavaDoc indices = sel.getRowIndices();
90         // the following is should succeed (ui-clients need it)
91
// for (Iterator iter = indices.iterator(); iter.hasNext();) {
92
// Integer element = (Integer) iter.next();
93
// table.getValue(element.intValue(), "pid");
94
// }
95
assertEquals("selected indices must be empty if no rows exist", 0, indices.size());
96     }
97     
98     /**
99      * Issue: Row/Table events fired on update.
100      * JW: I would expect the table to fire a update
101      * related event (with the exact nature of the update)
102      *
103      */

104     public void testDataEventOnUpdate() {
105         DataSet ds = new DataSet();
106         DataTable table = ds.createTable();
107         DataColumn column = table.createColumn();
108         column.setName("pid");
109         table.appendRow();
110         // fake a save
111
table.getRow(0).setStatus(DataRow.DataRowStatus.UNCHANGED);
112         DataEventReport report = new DataEventReport();
113         table.addDataTableListener(report);
114         table.setValue(0, "pid", "newValue");
115         // test row status changed
116
assertEquals("row status must be updated",
117                 DataRow.DataRowStatus.UPDATED, table.getRow(0).getStatus());
118         assertTrue("table must have fired some data related event", report.hasEvents());
119     }
120     
121     /**
122      * Issue: Row/Table events fired on insert.
123      *
124      */

125     public void testDataEventOnInsert() {
126         DataSet ds = new DataSet();
127         DataTable table = ds.createTable();
128         DataColumn column = table.createColumn();
129         column.setName("pid");
130         DataEventReport report = new DataEventReport();
131         table.addDataTableListener(report);
132         table.appendRow();
133         // test row status changed
134
assertEquals("row status must be inserted",
135                 DataRow.DataRowStatus.INSERTED, table.getRow(0).getStatus());
136         assertTrue("table must have fired some data related event", report.hasEvents());
137     }
138     
139     /**
140      * Issue: Row/Table events fired on remove.
141      * JW: I would expect the table to fire a remove
142      * related event (with the exact nature of the remove)
143      *
144      */

145     public void testDataEventOnRemove() {
146         DataSet ds = new DataSet();
147         DataTable table = ds.createTable();
148         DataColumn column = table.createColumn();
149         column.setName("pid");
150         table.appendRow();
151         // sanity assert:
152
// JW: shouldn't we have a convience method to get the rowCount?
153
assertEquals(1, table.getRows().size());
154         DataEventReport report = new DataEventReport();
155         table.addDataTableListener(report);
156         table.deleteRow(0);
157         // test row status changed
158
assertEquals("row status must be deleted",
159                 DataRow.DataRowStatus.DELETED, table.getRow(0).getStatus());
160         assertTrue("table must have fired some data related event", report.hasEvents());
161     }
162     
163     /**
164      * Issue: who is responsible for keeping the row status?
165      * JW: I would expect the status to be unchanged after
166      * provider.load/save.
167      *
168      */

169     public void testSaveResetRowStatus() {
170         DataSet ds = new DataSet();
171         DataTable table = ds.createTable();
172         TestDataProvider provider = new TestDataProvider();
173         table.setDataProvider(provider);
174         DataColumn column = table.createColumn();
175         column.setName("pid");
176         table.appendRow();
177         //test that everything worked
178
assertEquals(1, table.getRows().size());
179         assertEquals(DataRow.DataRowStatus.INSERTED, table.getRow(0).getStatus());
180         
181         assertNull("initial value in row 0 must be null", table.getValue(0, "pid"));
182         // JW: shouldn't DataSet have a save()?
183
table.save();
184         assertEquals("row status must be unchanged after save",
185                 DataRow.DataRowStatus.UNCHANGED, table.getRow(0).getStatus());
186         
187     }
188 }
189
Popular Tags