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