1 5 package org.jdesktop.dataset; 6 7 import java.util.List ; 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 22 public class DataTableIssues extends TestCase { 23 24 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 assertEquals("table with rowCount rows", rowCount, table.getRows().size()); 42 PropertyChangeReport report = new PropertyChangeReport(); 43 sel.addPropertyChangeListener("rowIndices", report); 44 List 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 60 public void testSelectorSelectNonExistingRows() { 61 DataSet ds = new DataSet(); 62 DataTable table = ds.createTable(); 63 DataSelector sel = table.createSelector(); 64 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 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 assertEquals("empty table", 0, table.getRows().size()); 88 sel.setRowIndices(new int[] {0, 1}); 89 List indices = sel.getRowIndices(); 90 assertEquals("selected indices must be empty if no rows exist", 0, indices.size()); 96 } 97 98 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 table.getRow(0).setStatus(DataRow.DataRowStatus.UNCHANGED); 112 DataEventReport report = new DataEventReport(); 113 table.addDataTableListener(report); 114 table.setValue(0, "pid", "newValue"); 115 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 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 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 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 assertEquals(1, table.getRows().size()); 154 DataEventReport report = new DataEventReport(); 155 table.addDataTableListener(report); 156 table.deleteRow(0); 157 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 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 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 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 |