1 7 package org.jdesktop.swing.data; 8 9 import java.net.URL ; 10 import java.util.List ; 11 import java.util.logging.Logger ; 12 13 import org.jdesktop.swing.util.TestSupport; 14 15 20 public class TableModelExtAdapterTest extends AbstractDataModelTst { 21 private static final String PREFIX = "/org/jdesktop/swing/resources/"; 23 24 private static final String STRINGDATA = PREFIX + "stringdata.csv"; 25 26 private URL stringDataURL; 27 28 32 public void testBidiNotification() { 33 DefaultTableModelExt tabularData = TestSupport.loadTabularCSVData(stringDataURL, true); 34 TableModelExtAdapter adapter = new TableModelExtAdapter(tabularData); 35 adapter.setRecordIndex(0); 36 adapter.addValueChangeListener(valueReport); 37 tabularData.setValueAt("dummy", 0, 0); 38 String [] fieldNames = adapter.getFieldNames(); 39 assertTrue("must have fired", valueReport.gotEvent(fieldNames[0])); 40 assertEquals(adapter.getValue(fieldNames[0]), tabularData 41 .getValueAt(0, 0)); 42 valueReport.clear(); 43 adapter.setValue( fieldNames[0], "othervalue"); 44 assertTrue("must have fired valueChange", 45 valueReport.gotEvent(fieldNames[0])); 46 assertEquals(adapter.getValue(fieldNames[0]), tabularData.getValueAt(0, 0)); 47 48 49 } 50 51 56 public void testFireValueChangeOnIndexChange() { 57 TableModelExtAdapter model = (TableModelExtAdapter) createFilledDataModel(); 58 if (model.getRecordCount() < 2) { 59 Logger.global.info("valueChangeOnIndexChange not run for " + model); 60 return; 61 } 62 assertFalse("sanity check: we are moving the recordIndex", model.getRecordIndex() == (model.getRecordCount() - 1)); 63 List oldValues = getValues(model); 64 model.addValueChangeListener(valueReport); 65 model.setRecordIndex(model.getRecordCount() - 1); 66 int changeCount = assertValueChangeFired(oldValues, model); 67 assertTrue("# changed values", changeCount > 0); 68 } 69 70 75 public void testDeselect() { 76 TableModelExtAdapter model = (TableModelExtAdapter) createFilledDataModel(); 77 if (model.getRecordCount() < 2) { 78 Logger.global.info("deselect not run for " + model); 79 return; 80 } 81 model.setRecordIndex(model.getRecordCount() - 1); 82 List oldValues = getValues(model); 83 model.addValueChangeListener(valueReport); 84 model.setRecordIndex(-1); 85 int changeCount = assertValueChangeFired(oldValues, model); 86 assertTrue("# changed values", changeCount > 0); 87 assertNullValues(getValues(model)); 88 } 89 90 95 public void testSelect() { 96 TableModelExtAdapter model = (TableModelExtAdapter) createFilledDataModel(); 97 if (model.getRecordCount() < 2) { 98 Logger.global.info("Select not run for " + model); 99 return; 100 } 101 List oldValues = getValues(model); 102 103 model.addValueChangeListener(valueReport); 104 model.setRecordIndex(model.getRecordCount() - 1); 105 assertEquals("recordIndex set", model.getRecordCount() - 1, model.getRecordIndex()); 106 assertTrue("value changes fired", valueReport.hasEvents()); 107 int changeCount = assertValueChangeFired(oldValues, model); 108 assertTrue("# changed values", changeCount > 0); 109 } 110 111 120 121 public void testEmptySelectionOnInit() { 122 TableModelExtAdapter adapter = (TableModelExtAdapter) createEmptyDataModel(new MetaData[] { metaData }, 1 ); 123 assertEquals("recordIndex on start", -1, adapter.getRecordIndex()); 124 String [] fields = adapter.getFieldNames(); 125 assertNull("value must be null if empty current", adapter.getValue(fields[0])); 126 adapter.setValue(fields[0], VALUE); 127 } 128 129 131 132 protected boolean guaranteeNonEmptySelection(DataModel model) { 133 ((TableModelExtAdapter) model).setRecordIndex(0); 134 return true; 135 } 136 137 protected DataModel createDataModelWithValidator(boolean valid) { 138 TableModelExtAdapter model = (TableModelExtAdapter) createEmptyDataModel(new MetaData[] { metaData }, 1); 139 model.setRecordIndex(0); 140 model.addValidator(TestDataModelFactory.createValidator(valid)); 141 return model; 142 } 143 144 protected DataModel createEmptyDataModel(MetaData[] metaData, int rowCount) { 145 DefaultTableModelExt tabularData = TestSupport.createTabularDataModel(metaData, rowCount); 146 return new TableModelExtAdapter(tabularData); 147 } 148 149 protected DataModel createFilledDataModel() { 150 DefaultTableModelExt data = TestSupport.loadTabularCSVData(stringDataURL, true); 151 assertEquals("row count", 10, data.getRowCount()); 152 TableModelExtAdapter adapter = new TableModelExtAdapter(data); 153 return adapter; 154 } 155 156 157 protected void setUp() throws Exception { 158 super.setUp(); 159 System.setProperty(DataLoader.READER_PRIORITY_KEY, String 160 .valueOf(Thread.NORM_PRIORITY)); 161 stringDataURL = getClass().getResource(STRINGDATA); 162 } 163 protected void tearDown() throws Exception { 164 System.getProperties().remove(DataLoader.READER_PRIORITY_KEY); 165 super.tearDown(); 166 } 167 } 168 | Popular Tags |