1 7 8 package org.jdesktop.swing.data; 9 10 import java.util.ArrayList ; 11 import java.util.Iterator ; 12 import java.util.List ; 13 import java.util.logging.Logger ; 14 15 import org.jdesktop.swing.util.PropertyChangeReport; 16 import org.jdesktop.swing.util.ValueChangeReport; 17 18 import junit.framework.Test; 19 import junit.framework.TestCase; 20 import junit.framework.TestSuite; 21 22 23 28 public abstract class AbstractDataModelTst extends TestCase { 29 protected static final String EXISTING_FIELDNAME = "existing"; 30 protected static final String NON_EXISTING_FIELDNAME = "non_existing"; 31 protected static final String VALUE = "somevalue"; 32 33 protected MetaData metaData; 34 protected ValueChangeReport valueReport; 35 36 37 public AbstractDataModelTst() { 38 } 39 40 public AbstractDataModelTst(String name) { 41 super(name); 42 } 43 48 public void testModelInstantiation() { 49 DataModel model = createEmptyDataModel(new MetaData[] { metaData }, 1 ); 50 guaranteeNonEmptySelection(model); 51 52 65 } 66 67 71 public void testNotificationDataModelSetValue() { 72 DataModel model = createEmptyDataModel(new MetaData[] { metaData }, 1 ); 73 if (!guaranteeNonEmptySelection(model)) return; 74 model.addValueChangeListener(valueReport); 75 model.setValue(EXISTING_FIELDNAME, VALUE); 76 assertEquals("value must be set", VALUE, model.getValue(EXISTING_FIELDNAME)); 77 assertEquals("valueChangeEvent must be fired for property ", EXISTING_FIELDNAME, valueReport.getLastFieldName()); 78 } 79 80 81 82 83 88 public void testValidatorAccess() { 89 DataModel dataModel = createDataModelWithValidator(false); 90 dataModel.getValidators(); 91 } 92 93 100 protected int assertValueChangeFired(List oldValues, DataModel model) { 101 String [] fieldNames = model.getFieldNames(); 102 int changeCount = 0; 103 for (int i = 0; i < fieldNames.length; i++) { 104 if (!equals(oldValues.get(i), model.getValue(fieldNames[i]))) { 105 changeCount++; 106 assertTrue("must have fired value change for " 107 + fieldNames[i] + "old/new: \n" + 108 oldValues.get(i) + "/" + model.getValue(fieldNames[i]), 109 valueReport.gotEvent(fieldNames[i])); 110 } else { 111 assertFalse("must not fire value change for unmodified field" 112 + fieldNames[i] + "old/new: \n" + 113 oldValues.get(i) + "/" + model.getValue(fieldNames[i]), 114 valueReport.gotEvent(fieldNames[i])); 115 } 116 } 117 return changeCount; 118 119 } 120 121 126 protected void assertNullValues(List values) { 127 for (Iterator iter = values.iterator(); iter.hasNext();) { 128 assertNull("value must be null ", iter.next()); 129 130 } 131 132 } 133 134 protected boolean equals(Object oldValue, Object value) { 135 if ((oldValue != null && !oldValue.equals(value)) || 137 (oldValue == null && value != null)) { 138 return false; 139 } 140 return true; 141 } 142 143 150 protected List getValues(DataModel model) { 151 List values = new ArrayList (); 152 String [] fieldNames = model.getFieldNames(); 153 for (int i = 0; i < fieldNames.length; i++) { 154 values.add(model.getValue(fieldNames[i])); 155 } 156 return values; 157 } 158 159 170 protected abstract boolean guaranteeNonEmptySelection(DataModel model); 171 172 178 protected abstract DataModel createDataModelWithValidator(boolean valid); 179 180 186 protected abstract DataModel createEmptyDataModel(MetaData[] metaData, int rowCount); 187 188 193 protected abstract DataModel createFilledDataModel(); 194 195 196 protected void setUp() throws Exception { 197 super.setUp(); 198 metaData = new MetaData(EXISTING_FIELDNAME, EXISTING_FIELDNAME.getClass(), EXISTING_FIELDNAME + "Label"); 199 valueReport = new ValueChangeReport(); 200 } 201 202 203 } 204 | Popular Tags |