1 7 8 package org.jdesktop.swing.binding; 9 10 import javax.swing.*; 11 12 import junit.framework.TestCase; 13 14 import org.jdesktop.swing.data.*; 15 import org.jdesktop.swing.util.TestSupport; 16 17 23 public class BindingUnitTest extends TestCase { 24 25 27 33 public void testTextBindingAfterChangeAndPull() { 34 DataModel personModel = TestDataModelFactory.createNestedPersonModel(); 35 JTextField textfield = new JTextField("somevalue"); 36 TextBinding binding = new TextBinding(textfield, personModel, 37 "comments"); 38 assertUnmodifiedAfterPull(binding); 40 textfield.setText("someothervalue"); 42 assertTrue(binding.isModified()); 43 44 assertUnmodifiedAfterPull(binding); 46 } 47 48 private void assertUnmodifiedAfterPull(Binding binding) { 49 assertTrue(binding.pull()); 50 assertFalse("Binding must be unmodified after pull", binding.isModified()); 51 } 52 53 62 public void testBooleanBindingIsValid() { 63 72 } 73 74 public void testBooleanBinding() { 75 DataModel personModel = TestDataModelFactory.createPersonModel(); 76 MetaData metaData = personModel.getMetaData("married"); 77 JCheckBox checkbox = new JCheckBox(metaData.getLabel()); 78 BooleanBinding binding = new BooleanBinding(checkbox, personModel, 79 "married"); 80 81 assertTrue(binding.pull()); 82 assertFalse(binding.isModified()); 83 assertTrue(binding.isValid()); 85 checkbox.setSelected(true); 86 87 assertTrue(binding.isModified()); 88 assertTrue(binding.isValid()); 89 assertTrue(binding.push()); 90 assertFalse(binding.isModified()); 91 assertTrue(binding.isValid()); 92 93 assertEquals(Boolean.TRUE, personModel.getValue("married")); 94 } 95 96 101 public void testComboBoxBindingAllowsNonContainedSelection() { 102 EnumeratedMetaData enumMeta = new EnumeratedMetaData("status", String .class, "Status"); 103 enumMeta.setEnumeration(TestDataModelFactory.statusTypes); 104 DefaultDataModel model = new DefaultDataModel(); 105 model.addField(enumMeta); 106 JComboBox comboBox = new JComboBox(enumMeta.getEnumeration()); 107 ComboBoxBinding binding = new ComboBoxBinding(comboBox, model, "status"); 108 binding.pull(); 109 assertEquals("initial value must be null ", null, comboBox.getSelectedItem()); 110 model.setValue("status", "unknown"); 111 assertEquals("comboBox must select uncontained value", 112 model.getValue("status"), comboBox.getSelectedItem()); 113 } 114 115 119 public void testAutoConfigureComboBox() { 120 EnumeratedMetaData enumMeta = new EnumeratedMetaData("status", String .class, "Status"); 121 enumMeta.setEnumeration(TestDataModelFactory.statusTypes); 122 DefaultDataModel model = new DefaultDataModel(); 123 model.addField(enumMeta); 124 JComboBox comboBox = new JComboBox(); 125 ComboBoxBinding binding = new ComboBoxBinding(comboBox, model, "status"); 126 binding.pull(); 127 assertEquals("initial value must be null ", null, comboBox.getSelectedItem()); 128 model.setValue("status", "unknown"); 129 assertEquals("comboBox must select uncontained value", 130 model.getValue("status"), comboBox.getSelectedItem()); 131 Object [] otherData = new Object [] {"other", "data"}; 132 enumMeta.setEnumeration(otherData); 133 assertEquals("combobox drop-down must be updated", otherData.length, comboBox.getItemCount()); 134 assertEquals("combobox drop-down must be updated", otherData[0], comboBox.getItemAt(0)); 135 assertEquals("selection must be unchanged", "unknown", comboBox.getSelectedItem()); 136 } 137 138 public void testComboBoxBinding() { 139 DataModel personModel = TestDataModelFactory.createNestedPersonModel(); 140 DataModel addressModel = (DataModel) personModel.getValue("address"); 141 EnumeratedMetaData metaData = (EnumeratedMetaData) addressModel 142 .getMetaData("state"); 143 JComboBox combobox = new JComboBox(metaData.getEnumeration()); 144 ComboBoxBinding binding = new ComboBoxBinding(combobox, addressModel, 145 "state"); 146 147 assertTrue(binding.pull()); 148 assertFalse(binding.isModified()); 149 assertFalse(binding.isValid()); 151 combobox.setSelectedIndex(4); 153 assertTrue(binding.isModified()); 154 assertTrue(binding.isValid()); 155 assertTrue(binding.push()); 156 assertFalse(binding.isModified()); 157 assertTrue(binding.isValid()); 158 159 assertEquals("California", addressModel.getValue("state")); 160 } 161 162 public void testLabelBinding() { 163 DataModel personModel = TestDataModelFactory.createPersonModel(); 164 MetaData metaData = personModel.getMetaData("customerid"); 165 JLabel label = new JLabel(metaData.getLabel()); 166 LabelBinding binding = new LabelBinding(label, personModel, 167 "customerid"); 168 169 assertTrue(binding.pull()); 170 assertFalse(binding.isModified()); 171 assertTrue(binding.isValid()); } 173 174 public void testTextBinding() { 175 DataModel personModel = TestDataModelFactory.createNestedPersonModel(); 176 StringMetaData metaData = (StringMetaData) personModel 177 .getMetaData("lastname"); 178 JTextField textfield = new JTextField(metaData.getDisplayWidth()); 179 TextBinding binding = new TextBinding(textfield, personModel, 180 "lastname"); 181 182 assertTrue(binding.pull()); 183 assertFalse(binding.isModified()); 184 assertFalse(binding.isValid()); 186 textfield.setText("Moore"); 187 188 assertTrue(binding.isModified()); 189 assertTrue(binding.isValid()); 190 assertTrue(binding.push()); 191 assertFalse(binding.isModified()); 192 assertTrue(binding.isValid()); 193 194 assertEquals("Moore", personModel.getValue("lastname")); 195 } 196 197 public void testPropertyChangeSupport() { 198 DataModel personModel = TestDataModelFactory.createPersonModel(); 199 StringMetaData metaData = (StringMetaData) personModel 200 .getMetaData("lastname"); 201 JTextField textfield = new JTextField(metaData.getDisplayWidth()); 202 TextBinding binding = new TextBinding(textfield, personModel, 203 "lastname"); 204 205 TestSupport.PropertyChangeTracker tracker = new TestSupport.PropertyChangeTracker(); 206 207 binding.addPropertyChangeListener(tracker); 209 assertEquals(1, binding.getPropertyChangeListeners().length); 210 211 assertFalse(tracker.hasPropertyChangeEvents()); 212 213 textfield.setText("Davidson"); 215 assertTrue(tracker.gotPropertyChangeEvent("modified")); 216 Boolean newValue = (Boolean ) tracker.getNewPropertyValue("modified"); 217 assertTrue(newValue.booleanValue()); 218 tracker.clear(); 219 220 binding.push(); 223 assertTrue(tracker.gotPropertyChangeEvent("validState")); 224 Integer stateValue = (Integer ) tracker 225 .getNewPropertyValue("validState"); 226 assertEquals(Binding.VALID, stateValue.intValue()); 227 assertTrue(tracker.gotPropertyChangeEvent("modified")); 228 newValue = (Boolean ) tracker.getNewPropertyValue("modified"); 229 assertFalse(newValue.booleanValue()); 230 tracker.clear(); 231 232 textfield.setText(""); 234 assertFalse(binding.isValid()); 235 assertTrue(tracker.gotPropertyChangeEvent("validState")); 236 stateValue = (Integer ) tracker.getNewPropertyValue("validState"); 237 assertEquals(Binding.INVALID, stateValue.intValue()); 238 239 } 240 241 251 } | Popular Tags |