1 /*
2 * Scope: a generic MVC framework.
3 * Copyright (c) 2000-2002, Steve Meyfroidt
4 * All rights reserved.
5 * Email: smeyfroi@users.sourceforge.net
6 *
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * Neither the name "Scope" nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
28 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
30 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
31 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
32 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
33 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 *
36 *
37 * $Id: TestSTextField.java,v 1.10 2002/08/05 13:19:26 ludovicc Exp $
38 */
39
40
41 package test.view.swing;
42
43
44 import java.awt.event.FocusEvent;
45 import junit.framework.TestCase;
46 import org.apache.commons.logging.LogFactory;import org.apache.commons.logging.Log;
47 import org.scopemvc.core.Selector;
48 import org.scopemvc.util.convertor.StringStringConvertor;
49 import org.scopemvc.view.swing.SPanel;
50 import org.scopemvc.view.swing.STextField;
51
52
53 /***
54 * <P>
55 * </P>
56 *
57 * @author <A HREF="mailto:smeyfroi@users.sourceforge.net>Steve Meyfroidt</A>
58 * @version $Revision: 1.10 $ $Date: 2002/08/05 13:19:26 $
59 */
60 public final class TestSTextField extends TestCase {
61
62
63 private static final Log LOG = LogFactory.getLog(TestSTextField.class);
64
65
66 private STextField textfield;
67 private SwingDummyController controller;
68 private SPanel view;
69 private SwingDummyModel model;
70
71
72 public TestSTextField(String inName) {
73 super(inName);
74 }
75
76
77 protected void setUp() throws Exception {
78
79 textfield = new STextField();
80 textfield.setColumns(10);
81 textfield.setDisableOnNull(true);
82
83 view = new SPanel();
84 view.add(textfield);
85
86 controller = new SwingDummyController();
87 controller.setView(view);
88 controller.startup(); // does showView()
89
90 model = new SwingDummyModel();
91 }
92
93
94 protected void tearDown() {
95 controller.shutdown();
96 }
97
98
99 public void testUnbound() throws Exception {
100 SuiteViewSwing.waitForAWT();
101 assertTrue(! textfield.isEnabled());
102 // assertTrue(textfield.getText().length() < 1);
103
104 textfield.setSelector(Selector.fromString("stringProperty"));
105 SuiteViewSwing.waitForAWT();
106 assertEquals(Selector.fromString("stringProperty"), textfield.getSelector());
107 assertTrue(! textfield.isEnabled());
108 // assertTrue(textfield.getText().length() < 1);
109 }
110
111
112 public void testBind1() throws Exception {
113 textfield.setSelector(Selector.fromString("stringProperty"));
114 assertEquals(Selector.fromString("stringProperty"), textfield.getSelector());
115
116 controller.setModel(model);
117 SuiteViewSwing.waitForAWT();
118 assertSame(view.getBoundModel(), model);
119 assertSame(textfield.getBoundModel(), model);
120 assertTrue(textfield.isEnabled());
121 assertEquals(model.getStringProperty(), textfield.getText());
122
123 model.setStringProperty("abc");
124 SuiteViewSwing.waitForAWT();
125 assertTrue(textfield.isEnabled());
126 assertEquals(model.getStringProperty(), textfield.getText());
127
128 model.setStringProperty("xyz");
129 SuiteViewSwing.waitForAWT();
130 assertTrue(textfield.isEnabled());
131 assertEquals(model.getStringProperty(), textfield.getText());
132
133 textfield.setText("gui");
134 textfield.postActionEvent();
135 SuiteViewSwing.waitForAWT();
136 assertTrue(textfield.isEnabled());
137 assertEquals("gui", textfield.getText());
138 assertEquals(model.getStringProperty(), textfield.getText());
139
140 textfield.setText("");
141 textfield.postActionEvent();
142 SuiteViewSwing.waitForAWT();
143 assertTrue(! textfield.isEnabled());
144 assertNull(model.getStringProperty());
145
146 model.setStringProperty("abc");
147 SuiteViewSwing.waitForAWT();
148
149 model.setStringProperty(null);
150 SuiteViewSwing.waitForAWT();
151 assertTrue(! textfield.isEnabled());
152 assertNull(model.getStringProperty());
153 }
154
155
156 public void testConvenience() throws Exception {
157 textfield.setSelectorString("stringProperty");
158 assertEquals(Selector.fromString("stringProperty"), textfield.getSelector());
159 }
160
161
162 public void testBindNull() throws Exception {
163 textfield.setDisableOnNull(false);
164 textfield.setSelector(Selector.fromString("stringProperty"));
165 assertEquals(Selector.fromString("stringProperty"), textfield.getSelector());
166
167 controller.setModel(model);
168 SuiteViewSwing.waitForAWT();
169 assertSame(view.getBoundModel(), model);
170 assertSame(textfield.getBoundModel(), model);
171 assertTrue(textfield.isEnabled());
172 assertEquals(model.getStringProperty(), textfield.getText());
173
174 model.setStringProperty("abc");
175 SuiteViewSwing.waitForAWT();
176 assertTrue(textfield.isEnabled());
177 assertEquals(model.getStringProperty(), textfield.getText());
178
179 model.setStringProperty("xyz");
180 SuiteViewSwing.waitForAWT();
181 assertTrue(textfield.isEnabled());
182 assertEquals(model.getStringProperty(), textfield.getText());
183
184 textfield.setText("gui");
185 textfield.postActionEvent();
186 SuiteViewSwing.waitForAWT();
187 assertTrue(textfield.isEnabled());
188 assertEquals("gui", textfield.getText());
189 assertEquals(model.getStringProperty(), textfield.getText());
190
191 textfield.setText("");
192 textfield.postActionEvent();
193 SuiteViewSwing.waitForAWT();
194 assertTrue(textfield.isEnabled());
195 assertTrue(model.getStringProperty() == null);
196
197 model.setStringProperty(null);
198 SuiteViewSwing.waitForAWT();
199 assertTrue(textfield.isEnabled());
200 assertNull(model.getStringProperty());
201 assertTrue(textfield.getText().length() < 1);
202 }
203
204
205 public void testBadBind() throws Exception {
206 textfield.setSelector(Selector.fromString("subModel"));
207 controller.setModel(model);
208 SuiteViewSwing.waitForAWT();
209
210 assertTrue(! textfield.isEnabled());
211 // assertTrue(! textfield.isSelected());
212
213 textfield.postActionEvent();
214 }
215
216
217 public void testBadBind2() throws Exception {
218 textfield.setDisableOnNull(false);
219 textfield.setSelector(Selector.fromString("subModel"));
220 controller.setModel(model);
221 SuiteViewSwing.waitForAWT();
222
223 assertTrue(! textfield.isEnabled());
224 // assertTrue(! textfield.isSelected());
225
226 textfield.postActionEvent();
227 }
228
229
230 public void testReadOnlyBind() throws Exception {
231 textfield.setSelector(Selector.fromString("readOnlyStringProperty"));
232 controller.setModel(model);
233 SuiteViewSwing.waitForAWT();
234
235 assertTrue(! textfield.isEnabled());
236 assertEquals(model.getReadOnlyStringProperty(), textfield.getText());
237 }
238
239
240 public void testControlIssue1() throws Exception {
241 textfield.setSelector(Selector.fromString("stringProperty"));
242 controller.setModel(model);
243 textfield.setControlID("test1");
244 SuiteViewSwing.waitForAWT();
245
246 assertNull(controller.lastControl);
247
248 textfield.postActionEvent();
249 SuiteViewSwing.waitForAWT();
250 assertTrue(controller.controlMatches("test1"));
251
252 textfield.setControlID("test1a");
253 textfield.postActionEvent();
254 SuiteViewSwing.waitForAWT();
255 assertTrue(controller.controlMatches("test1a"));
256 }
257
258
259 public void testBooleanBind() throws Exception {
260 textfield.setSelector(Selector.fromString("booleanProperty1"));
261 assertEquals(Selector.fromString("booleanProperty1"), textfield.getSelector());
262
263 controller.setModel(model);
264 SuiteViewSwing.waitForAWT();
265 assertSame(view.getBoundModel(), model);
266 assertSame(textfield.getBoundModel(), model);
267
268 model.setBooleanProperty1(Boolean.TRUE);
269 SuiteViewSwing.waitForAWT();
270 assertTrue(textfield.isEnabled());
271 assertEquals("" + model.getBooleanProperty1(), textfield.getText());
272
273 model.setBooleanProperty1(Boolean.FALSE);
274 SuiteViewSwing.waitForAWT();
275 assertTrue(textfield.isEnabled());
276 assertEquals("" + model.getBooleanProperty1(), textfield.getText());
277
278 model.setBooleanProperty1(Boolean.FALSE);
279 SuiteViewSwing.waitForAWT();
280 textfield.setText("true");
281 textfield.postActionEvent();
282 SuiteViewSwing.waitForAWT();
283 assertTrue(textfield.isEnabled());
284 assertEquals("true", textfield.getText());
285 assertEquals("" + model.getBooleanProperty1(), textfield.getText());
286
287 model.setBooleanProperty1(null);
288 SuiteViewSwing.waitForAWT();
289 assertTrue(! textfield.isEnabled());
290 assertNull(model.getBooleanProperty1());
291 }
292
293
294 public void testInvalidBooleanBind() throws Exception {
295 textfield.setSelector(Selector.fromString("booleanProperty1"));
296 assertEquals(Selector.fromString("booleanProperty1"), textfield.getSelector());
297
298 controller.setModel(model);
299 SuiteViewSwing.waitForAWT();
300 assertSame(view.getBoundModel(), model);
301 assertSame(textfield.getBoundModel(), model);
302
303 model.setBooleanProperty1(Boolean.FALSE);
304 SuiteViewSwing.waitForAWT();
305
306 textfield.setText("rubbish");
307 textfield.focusLost(new FocusEvent(textfield, FocusEvent.FOCUS_LOST));
308 SuiteViewSwing.waitForAWT();
309 assertTrue(textfield.isEnabled());
310 assertEquals("rubbish", textfield.getText());
311 assertEquals(Boolean.FALSE, model.getBooleanProperty1());
312 }
313
314
315 public void testInvalidBooleanBind2() throws Exception {
316 textfield.setSelector(Selector.fromString("booleanProperty1"));
317 assertEquals(Selector.fromString("booleanProperty1"), textfield.getSelector());
318
319 textfield.setControlID("test1");
320 controller.setModel(model);
321 SuiteViewSwing.waitForAWT();
322 assertSame(view.getBoundModel(), model);
323 assertSame(textfield.getBoundModel(), model);
324
325 model.setBooleanProperty1(Boolean.FALSE);
326 SuiteViewSwing.waitForAWT();
327
328 textfield.setText("rubbish");
329 textfield.postActionEvent();
330 SuiteViewSwing.waitForAWT();
331 assertTrue(controller.controlMatches("test1"));
332 assertTrue(textfield.isEnabled());
333 assertEquals("rubbish", textfield.getText());
334 assertEquals(Boolean.FALSE, model.getBooleanProperty1());
335 }
336
337
338 public void testBindNoMCE() throws Exception {
339 textfield.setSelector(Selector.fromString("stringProperty"));
340 assertEquals(Selector.fromString("stringProperty"), textfield.getSelector());
341
342 SwingDummyModelNoMCE model = new SwingDummyModelNoMCE();
343
344 controller.setModel(model);
345 SuiteViewSwing.waitForAWT();
346 assertSame(view.getBoundModel(), model);
347
348 assertSame(textfield.getBoundModel(), model);
349 assertTrue(textfield.isEnabled());
350 assertEquals(model.getStringProperty(), textfield.getText());
351 String x = model.getStringProperty();
352
353 model.setStringProperty("abc");
354 SuiteViewSwing.waitForAWT();
355 assertTrue(textfield.isEnabled());
356 assertEquals(x, textfield.getText());
357
358 view.refresh();
359 assertTrue(textfield.isEnabled());
360 assertEquals(model.getStringProperty(), textfield.getText());
361 x = model.getStringProperty();
362
363 model.setStringProperty("xyz");
364 SuiteViewSwing.waitForAWT();
365 assertTrue(textfield.isEnabled());
366 assertEquals(x, textfield.getText());
367
368 view.refresh();
369 assertTrue(textfield.isEnabled());
370 assertEquals(model.getStringProperty(), textfield.getText());
371
372 textfield.setText("gui");
373 textfield.postActionEvent();
374 SuiteViewSwing.waitForAWT();
375 assertTrue(textfield.isEnabled());
376 assertEquals("gui", textfield.getText());
377 assertEquals(model.getStringProperty(), textfield.getText());
378
379 textfield.setText("");
380 textfield.postActionEvent();
381 SuiteViewSwing.waitForAWT();
382 // assertTrue(! textfield.isEnabled());
383 assertNull(model.getStringProperty());
384
385 model.setStringProperty("abc");
386 SuiteViewSwing.waitForAWT();
387
388 model.setStringProperty(null);
389 SuiteViewSwing.waitForAWT();
390 view.refresh();
391 assertTrue(! textfield.isEnabled());
392 assertNull(model.getStringProperty());
393 }
394
395
396 public void testForcedStringConvertor() throws Exception {
397 textfield.setSelector(Selector.fromString("stringProperty"));
398 StringStringConvertor s = new StringStringConvertor();
399 s.setNullAsString("xyz");
400 textfield.setStringConvertor(s);
401 textfield.setDisableOnNull(false);
402 model.setStringProperty(null);
403 assertEquals(Selector.fromString("stringProperty"), textfield.getSelector());
404
405 controller.setModel(model);
406 SuiteViewSwing.waitForAWT();
407 assertSame(view.getBoundModel(), model);
408 assertSame(textfield.getBoundModel(), model);
409 // assertTrue(textfield.isEnabled());
410 assertEquals("xyz", textfield.getText());
411
412 model.setStringProperty("abc");
413 SuiteViewSwing.waitForAWT();
414 assertTrue(textfield.isEnabled());
415 assertEquals(model.getStringProperty(), textfield.getText());
416 }
417
418
419 public void testInitialNullBind() throws Exception {
420 textfield.setSelectorString("subModel.stringProperty");
421 model.setSubModel(null);
422 controller.setModel(model);
423 SuiteViewSwing.waitForAWT();
424
425 assertTrue(! textfield.isEnabled());
426 assertEquals("", textfield.getText());
427
428 SwingDummyModel submodel = new SwingDummyModel();
429 submodel.setStringProperty("test");
430 model.setSubModel(submodel);
431 SuiteViewSwing.waitForAWT();
432
433 assertTrue(textfield.isEnabled());
434 assertEquals("test", textfield.getText());
435 }
436
437
438 public void testNullNavigation() throws Exception {
439 textfield.setSelectorString("subModel.stringProperty");
440 controller.setModel(model);
441
442 SwingDummyModel submodel = new SwingDummyModel();
443 submodel.setStringProperty("test");
444 model.setSubModel(submodel);
445 SuiteViewSwing.waitForAWT();
446
447 assertTrue(textfield.isEnabled());
448 assertEquals("test", textfield.getText());
449
450 model.setSubModel(null);
451 controller.setModel(model);
452 SuiteViewSwing.waitForAWT();
453
454 assertTrue(! textfield.isEnabled());
455 assertEquals("", textfield.getText());
456 }
457
458
459 // ***** setDisableOnNull(false); and test empty string, null, boolean
460 }
This page was automatically generated by Maven