1 7 8 package org.jdesktop.swing.form; 9 10 import java.awt.BorderLayout ; 11 import java.util.Locale ; 12 13 import javax.swing.JButton ; 14 import javax.swing.JComponent ; 15 import javax.swing.JFrame ; 16 import javax.swing.JPanel ; 17 import javax.swing.JSpinner ; 18 19 import org.jdesktop.swing.InteractiveTestCase; 20 import org.jdesktop.swing.binding.BindException; 21 import org.jdesktop.swing.binding.Binding; 22 import org.jdesktop.swing.data.DataConstants; 23 import org.jdesktop.swing.data.DataModel; 24 import org.jdesktop.swing.data.DefaultDataModel; 25 import org.jdesktop.swing.data.JavaBeanDataModel; 26 import org.jdesktop.swing.data.MetaData; 27 import org.jdesktop.swing.data.TabularDataModel; 28 import org.jdesktop.swing.data.TabularMetaData; 29 import org.jdesktop.swing.data.TestBean; 30 import org.jdesktop.swing.data.TestDataModelFactory; 31 import org.jdesktop.swing.data.Validator; 32 import org.jdesktop.swing.util.DataSetTestSupport; 33 import org.jdesktop.swing.util.ValueChangeReport; 34 35 41 public class JFormUnitTest extends InteractiveTestCase { 42 43 47 ValueChangeReport changeReport; 48 49 public JFormUnitTest() { 50 super("JForm Tests"); 51 } 52 53 public void testJFormCtor() { 54 JForm form = new JForm(); 55 assertNotNull(form); 56 assertNotNull(form.getFormFactory()); 57 } 58 59 public void testFormFactoryCtor() { 60 FormFactory factory = new DefaultFormFactory(); 63 } 64 65 public void testNonVisualField() { 66 MetaData[] metaData = new MetaData[1]; 67 String fieldName = "invisibleField"; 68 metaData[0] = new MetaData(fieldName); 69 metaData[0].setCustomProperty(DataConstants.NON_VISUAL_FIELD, Boolean.TRUE); 70 DefaultDataModel model = new DefaultDataModel(metaData); 71 JForm form = createForm(model); 72 assertNull("must not have binding for non-visual fields", getBinding(form, fieldName)); 73 } 74 75 public void testNonVisualBindings() { 76 MetaData[] metaData = new MetaData[1]; 77 String fieldName = "invisibleField"; 78 metaData[0] = new MetaData(fieldName); 79 metaData[0].setCustomProperty(DataConstants.NON_VISUAL_FIELD, Boolean.TRUE); 80 DefaultDataModel model = new DefaultDataModel(metaData); 81 JForm form = new JForm(); 82 try { 83 Binding[] bindings = form.bind(model); 84 assertEquals("must not have binding for non-visual fields", 0, bindings.length); 85 } catch (BindException e) { 86 e.printStackTrace(); 88 } 89 } 90 91 96 public void testValidatorLoop() { 97 DataModel dataModel = TestDataModelFactory 98 .createDataModelWithValidator(false); 99 try { 100 JForm form = new JForm(dataModel); 101 form.isFormValid(); 102 } catch (BindException e) { 103 e.printStackTrace(); 105 } 106 } 107 108 public void testFormValiditySynchedOnFields() { 109 assertFormValiditySynchedOnFields(false); 110 assertFormValiditySynchedOnFields(true); 111 112 } 113 114 119 public void testFormInvalidOnInvalidModel() { 120 JForm form = assertFormValiditySynchedOnFields(true); 121 DataModel personModel = form.getBindings()[0].getDataModel(); 122 Validator marriedAgeValidator = createMarriedAgeValidator(personModel); 123 personModel.addValidator(marriedAgeValidator); 124 personModel.setValue("age", new Integer (10)); 125 assertFalse("form must be invalid", form.isFormValid()); 126 } 127 128 136 protected JForm assertFormValiditySynchedOnFields(boolean valid) { 137 DataModel personModel = TestDataModelFactory 138 .createFilledPersonModel(valid); 139 JForm form = createForm(personModel); 140 assertEquals("form's validity ", valid, form.isFormValid()); 141 return form; 142 } 143 144 protected Validator createMarriedAgeValidator(DataModel personModel) { 145 146 Validator v = new Validator() { 147 148 public boolean validate(Object value, Locale locale, String [] error) { 149 DataModel personModel = (DataModel) value; 150 Boolean married = (Boolean ) personModel.getValue("married"); 151 Integer age = (Integer ) personModel.getValue("age"); 152 if (married.booleanValue()) { 153 return age.intValue() >= 16; 154 } 155 return true; 156 } 157 158 }; 159 return v; 160 } 161 162 164 168 public void interactiveTestShowAdapterInForm() { 169 TabularDataModel adaptee = DataSetTestSupport.createDefaultFilledTabularDataModel(3); 170 MetaData[] metaData = adaptee.getMetaData(); 171 metaData[0].setDisplayWidth(50); 173 TabularMetaData tabularMeta = new TabularMetaData("defaultDataTable"); 174 tabularMeta.setFieldNames(new String [] { metaData[metaData.length - 1].getName(), metaData[0].getName()} ); 175 DefaultDataModel model = new DefaultDataModel(new MetaData[] {tabularMeta}); 176 model.setValue("defaultDataTable", adaptee); 177 JForm form = createForm(model); 178 JFrame frame = wrapInFrame(form, "autocreate table with subset of fields"); 179 frame.setVisible(true); 180 181 } 182 183 public void interactiveTestNestedForm() { 184 DataModel personModel = TestDataModelFactory.createNestedPersonModel(); 185 186 JForm form = null; 187 try { 188 form = new JForm(personModel); 189 190 } catch (Exception e) { 191 System.out.println(e); 192 } 193 JFrame frame = wrapWithScrollingInFrame(form, "Nested Form"); 194 JPanel buttonPanel = new JPanel (); 195 JButton button = new JButton (form.getActionMap().get("submit")); 196 buttonPanel.add(button); 197 frame.getContentPane().add(BorderLayout.SOUTH, buttonPanel); 198 frame.setVisible(true); 199 } 200 201 205 public void interactiveTestJavaBeanWrapper() { 206 JForm form = null; 207 try { 208 form = new JForm(new JavaBeanDataModel(TestBean.class, 209 new TestBean())); 210 } catch (Exception ex) { 211 ex.printStackTrace(); 212 System.out.println(ex); 213 } 214 JFrame frame = wrapWithScrollingInFrame(form, "Wrapping JavaBean"); 215 frame.setVisible(true); 216 } 217 218 220 protected void setSpinnerValue(Binding binding, Integer value) { 221 JComponent comp = binding.getComponent(); 222 if (comp instanceof JSpinner ) { 223 ((JSpinner ) comp).setValue(value); 224 } 225 226 } 227 228 protected Binding getBinding(JForm form, String fieldname) { 229 Binding[] bindings = form.getBindings(); 230 for (int i = 0; i < bindings.length; i++) { 231 if (fieldname.equals(bindings[i].getFieldName())) { 232 return bindings[i]; 233 } 234 } 235 return null; 236 } 237 238 protected JForm createForm(DataModel model) { 239 JForm form = new JForm(); 240 try { 241 form.bind(model); 242 form.pull(); 243 } catch (BindException e) { 244 e.printStackTrace(); 246 } 247 return form; 248 } 249 250 251 252 protected void setUp() throws Exception { 253 super.setUp(); 254 FormFactory.setDefaultFormFactory(new DefaultFormFactory()); 256 changeReport = new ValueChangeReport(); 257 } 258 259 public static void main(String args[]) { 260 JFormUnitTest test = new JFormUnitTest(); 261 try { 262 test.runInteractiveTests(); 263 } catch (Exception e) { 264 e.printStackTrace(); 266 } 267 } 268 269 } | Popular Tags |