KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdesktop > swing > binding > BindingUnitTest


1 /*
2  * $Id: BindingUnitTest.java,v 1.10 2005/02/28 17:40:20 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.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 /**
18  * JUnit test class for binding classes.
19  *
20  * @author Amy Fowler
21  * @author Jeanette Winzenburg
22  */

23 public class BindingUnitTest extends TestCase {
24
25     //--------------------------- testing reset after change
26

27     /**
28      * Issue #78:
29      * test TextBinding modified state after pull/change/pull.
30      * <p>
31      *
32      */

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         // initial pull
39
assertUnmodifiedAfterPull(binding);
40         // modify
41
textfield.setText("someothervalue");
42         assertTrue(binding.isModified());
43
44         // resetting pull
45
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     /**
54      * Issue #115 - BooleanBinding cannot cope with boolean.class. <p>
55      * Don't allow primitiv type? DataModel can handle Object only
56      * anyway..<p>
57      *
58      * NOTE (JW): this issue is not solved - moved to BindingIssues.java
59      * to not interfere with default build.
60      *
61      */

62     public void testBooleanBindingIsValid() {
63 // DefaultDataModel model = new DefaultDataModel();
64
// MetaData booleanMetaData = new MetaData("married", boolean.class);
65
// model.addField(booleanMetaData);
66
// JCheckBox checkbox = new JCheckBox(booleanMetaData.getLabel());
67
// BooleanBinding binding = new BooleanBinding(checkbox, model,
68
// "married");
69
// binding.pull();
70
// assertTrue("boolean binding must always be valid ", binding.isValid());
71

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()); // not required
84

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     /**
97      * Issue #84: ComboBoxBinding must guarantee to select
98      * items not contained in the listmodel.
99      *
100      */

101     public void testComboBoxBindingAllowsNonContainedSelection() {
102         EnumeratedMetaData enumMeta = new EnumeratedMetaData("status", String JavaDoc.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     /**
116      * Testing self-init and update of ComboboxBinding
117      *
118      */

119     public void testAutoConfigureComboBox() {
120         EnumeratedMetaData enumMeta = new EnumeratedMetaData("status", String JavaDoc.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 JavaDoc[] otherData = new Object JavaDoc[] {"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()); // required
150

151         combobox.setSelectedIndex(4); // "California"
152

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()); // not required
172
}
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()); // required
185

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         // test addPropertyChangeListener
208
binding.addPropertyChangeListener(tracker);
209         assertEquals(1, binding.getPropertyChangeListeners().length);
210
211         assertFalse(tracker.hasPropertyChangeEvents());
212
213         // test that changing UI value will cause "modified" PCE
214
textfield.setText("Davidson");
215         assertTrue(tracker.gotPropertyChangeEvent("modified"));
216         Boolean JavaDoc newValue = (Boolean JavaDoc) tracker.getNewPropertyValue("modified");
217         assertTrue(newValue.booleanValue());
218         tracker.clear();
219
220         // test that pushing value to model will cause "validState" and
221
// "modified" PCEs
222
binding.push();
223         assertTrue(tracker.gotPropertyChangeEvent("validState"));
224         Integer JavaDoc stateValue = (Integer JavaDoc) tracker
225                 .getNewPropertyValue("validState");
226         assertEquals(Binding.VALID, stateValue.intValue());
227         assertTrue(tracker.gotPropertyChangeEvent("modified"));
228         newValue = (Boolean JavaDoc) tracker.getNewPropertyValue("modified");
229         assertFalse(newValue.booleanValue());
230         tracker.clear();
231
232         // test that setting to invalid value will cause "validState" PCE
233
textfield.setText("");
234         assertFalse(binding.isValid());
235         assertTrue(tracker.gotPropertyChangeEvent("validState"));
236         stateValue = (Integer JavaDoc) tracker.getNewPropertyValue("validState");
237         assertEquals(Binding.INVALID, stateValue.intValue());
238
239     }
240
241 // public static void main(String args[]) {
242
// BindingUnitTest test = new BindingUnitTest();
243
//
244
// test.testBooleanBinding();
245
// test.testComboBoxBinding();
246
// test.testLabelBinding();
247
// test.testTextBinding();
248
// test.testPropertyChangeSupport();
249
// }
250

251 }
Popular Tags