KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdesktop > swing > form > JFormUnitTest


1 /*
2  * $Id: JFormUnitTest.java,v 1.3 2005/03/01 13:27:09 kleopatra Exp $
3  *
4  * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
5  * Santa Clara, California 95054, U.S.A. All rights reserved.
6  */

7
8 package org.jdesktop.swing.form;
9
10 import java.awt.BorderLayout JavaDoc;
11 import java.util.Locale JavaDoc;
12
13 import javax.swing.JButton JavaDoc;
14 import javax.swing.JComponent JavaDoc;
15 import javax.swing.JFrame JavaDoc;
16 import javax.swing.JPanel JavaDoc;
17 import javax.swing.JSpinner JavaDoc;
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 /**
36  * JUnit test class for JForm.
37  *
38  * @author Amy Fowler
39  * @author Jeanette Winzenburg
40  */

41 public class JFormUnitTest extends InteractiveTestCase {
42
43     // public void testJFormConstructors
44
// public void testJFormProperties
45
// public void testJFormBinding
46

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         // Just instantiating the form factory should pick up
61
// missing icons.
62
FormFactory factory = new DefaultFormFactory();
63     }
64
65     public void testNonVisualField() {
66         MetaData[] metaData = new MetaData[1];
67         String JavaDoc 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 JavaDoc 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             // TODO Auto-generated catch block
87
e.printStackTrace();
88         }
89     }
90
91     /**
92      * Issue 127: isFormValid() did throw ArrayIndexOutOfBounds
93      * if dataModel has validators.
94      *
95      */

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             // TODO Auto-generated catch block
104
e.printStackTrace();
105         }
106     }
107
108     public void testFormValiditySynchedOnFields() {
109         assertFormValiditySynchedOnFields(false);
110         assertFormValiditySynchedOnFields(true);
111
112     }
113
114     /**
115      * assert that form detects invalid DataModel,
116      * invalid by datamodel validator, invalid data set in model.
117      *
118      */

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 JavaDoc(10));
125         assertFalse("form must be invalid", form.isFormValid());
126     }
127
128     /**
129      * creates and returns a form with a default filled dataModel.
130      * the model contains valid/invalid (on field-level) data
131      * according by the boolean parameter.
132      *
133      * asserts that form's validity is as expected.
134      *
135      */

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 JavaDoc value, Locale JavaDoc locale, String JavaDoc[] error) {
149                 DataModel personModel = (DataModel) value;
150                 Boolean JavaDoc married = (Boolean JavaDoc) personModel.getValue("married");
151                 Integer JavaDoc age = (Integer JavaDoc) 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 //----------------------- interactive tests
163

164     /**
165      * show the synthetic DataTable in a JForm (auto-created).
166      *
167      */

168     public void interactiveTestShowAdapterInForm() {
169         TabularDataModel adaptee = DataSetTestSupport.createDefaultFilledTabularDataModel(3);
170         MetaData[] metaData = adaptee.getMetaData();
171         // JW: doesn't work: JXTable ignores MetaData settings in autocreateColumns()
172
metaData[0].setDisplayWidth(50);
173         TabularMetaData tabularMeta = new TabularMetaData("defaultDataTable");
174         tabularMeta.setFieldNames(new String JavaDoc[] { 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 JavaDoc 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 JavaDoc e) {
191             System.out.println(e);
192         }
193         JFrame JavaDoc frame = wrapWithScrollingInFrame(form, "Nested Form");
194         JPanel JavaDoc buttonPanel = new JPanel JavaDoc();
195         JButton JavaDoc button = new JButton JavaDoc(form.getActionMap().get("submit"));
196         buttonPanel.add(button);
197         frame.getContentPane().add(BorderLayout.SOUTH, buttonPanel);
198         frame.setVisible(true);
199     }
200
201     /**
202      * Shows a JavaBeanData model with all the types that the FormFactory will
203      * support.
204      */

205     public void interactiveTestJavaBeanWrapper() {
206         JForm form = null;
207         try {
208             form = new JForm(new JavaBeanDataModel(TestBean.class,
209                     new TestBean()));
210         } catch (Exception JavaDoc ex) {
211             ex.printStackTrace();
212             System.out.println(ex);
213         }
214         JFrame JavaDoc frame = wrapWithScrollingInFrame(form, "Wrapping JavaBean");
215         frame.setVisible(true);
216     }
217
218 //--------------------- utility methods
219

220     protected void setSpinnerValue(Binding binding, Integer JavaDoc value) {
221         JComponent JavaDoc comp = binding.getComponent();
222         if (comp instanceof JSpinner JavaDoc) {
223             ((JSpinner JavaDoc) comp).setValue(value);
224         }
225
226     }
227
228     protected Binding getBinding(JForm form, String JavaDoc 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             // TODO Auto-generated catch block
245
e.printStackTrace();
246         }
247         return form;
248     }
249
250     
251     
252     protected void setUp() throws Exception JavaDoc {
253         super.setUp();
254         // make sure to use the default
255
FormFactory.setDefaultFormFactory(new DefaultFormFactory());
256         changeReport = new ValueChangeReport();
257     }
258     
259     public static void main(String JavaDoc args[]) {
260         JFormUnitTest test = new JFormUnitTest();
261         try {
262             test.runInteractiveTests();
263         } catch (Exception JavaDoc e) {
264             // TODO Auto-generated catch block
265
e.printStackTrace();
266         }
267     }
268
269 }
Popular Tags