KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > form > TestTextField


1 // Copyright 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.tapestry.form;
16
17 import org.apache.tapestry.IBinding;
18 import org.apache.tapestry.IForm;
19 import org.apache.tapestry.IMarkupWriter;
20 import org.apache.tapestry.IRequestCycle;
21 import org.apache.tapestry.form.translator.Translator;
22 import org.apache.tapestry.spec.ComponentSpecification;
23 import org.apache.tapestry.valid.IValidationDelegate;
24 import org.apache.tapestry.valid.ValidatorException;
25 import org.easymock.MockControl;
26
27 /**
28  * Tests for {@link org.apache.tapestry.form.TextField}.
29  *
30  * @author Howard M. Lewis Ship
31  * @since 4.0
32  */

33 public class TestTextField extends BaseFormComponentTest
34 {
35     public void testRewind() throws Exception JavaDoc
36     {
37         MockControl translatorc = newControl(Translator.class);
38         Translator translator = (Translator) translatorc.getMock();
39
40         MockControl supportc = newControl(ValidatableFieldSupport.class);
41         ValidatableFieldSupport support = (ValidatableFieldSupport) supportc.getMock();
42
43         TextField component = (TextField) newInstance(TextField.class, new Object JavaDoc[]
44         { "translator", translator, "validatableFieldSupport", support, "name", "barney" });
45
46         MockControl cyclec = newControl(IRequestCycle.class);
47         IRequestCycle cycle = (IRequestCycle) cyclec.getMock();
48
49         IMarkupWriter writer = newWriter();
50
51         trainGetParameter(cyclec, cycle, "barney", "value");
52
53         support.bind(component, writer, cycle, "value");
54
55         replayControls();
56
57         component.rewindFormComponent(writer, cycle);
58
59         verifyControls();
60     }
61
62     public void testWriteValue()
63     {
64         TextField component = (TextField) newInstance(TextField.class);
65
66         component.writeValue(new Integer JavaDoc(10));
67
68         assertEquals(new Integer JavaDoc(10), component.getValue());
69     }
70
71     public void testReadValue()
72     {
73         TextField component = (TextField) newInstance(TextField.class);
74
75         component.setValue(new Integer JavaDoc(10));
76
77         Object JavaDoc value = component.readValue();
78
79         assertEquals(new Integer JavaDoc(10), value);
80     }
81
82     public void testGetSubmittedValue()
83     {
84         TextField component = (TextField) newInstance(TextField.class, "name", "fred");
85
86         MockControl cyclec = newControl(IRequestCycle.class);
87         IRequestCycle cycle = (IRequestCycle) cyclec.getMock();
88
89         cycle.getParameter("fred");
90         cyclec.setReturnValue("10");
91
92         replayControls();
93
94         String JavaDoc value = component.getSubmittedValue(cycle);
95
96         verifyControls();
97
98         assertEquals("10", value);
99     }
100
101     public void testWasPrerendered()
102     {
103         TextField component = (TextField) newInstance(TextField.class);
104
105         MockControl cyclec = newControl(IRequestCycle.class);
106         IRequestCycle cycle = (IRequestCycle) cyclec.getMock();
107
108         MockControl formc = newControl(IForm.class);
109         IForm form = (IForm) formc.getMock();
110
111         IMarkupWriter writer = newWriter();
112
113         trainGetForm(cyclec, cycle, form);
114         trainWasPrerendered(formc, form, writer, component, true);
115
116         replayControls();
117
118         component.render(writer, cycle);
119
120         verifyControls();
121     }
122
123     public void testRewindNotForm()
124     {
125         TextField component = (TextField) newInstance(TextField.class);
126
127         MockControl cyclec = newControl(IRequestCycle.class);
128         IRequestCycle cycle = (IRequestCycle) cyclec.getMock();
129
130         MockControl formc = newControl(IForm.class);
131         IForm form = (IForm) formc.getMock();
132
133         IMarkupWriter writer = newWriter();
134
135         IValidationDelegate delegate = newDelegate();
136
137         trainGetForm(cyclec, cycle, form);
138         trainWasPrerendered(formc, form, writer, component, false);
139         trainGetDelegate(formc, form, delegate);
140
141         delegate.setFormComponent(component);
142
143         trainGetElementId(formc, form, component, "barney");
144         trainIsRewinding(formc, form, false);
145         trainIsRewinding(cyclec, cycle, true);
146
147         replayControls();
148
149         component.render(writer, cycle);
150
151         verifyControls();
152     }
153
154     public void testRewindingForm()
155     {
156         MockControl translatorc = newControl(Translator.class);
157         Translator translator = (Translator) translatorc.getMock();
158
159         MockControl supportc = newControl(ValidatableFieldSupport.class);
160         ValidatableFieldSupport support = (ValidatableFieldSupport) supportc.getMock();
161
162         TextField component = (TextField) newInstance(TextField.class, new Object JavaDoc[]
163         { "translator", translator, "validatableFieldSupport", support });
164
165         MockControl cyclec = newControl(IRequestCycle.class);
166         IRequestCycle cycle = (IRequestCycle) cyclec.getMock();
167
168         MockControl formc = newControl(IForm.class);
169         IForm form = (IForm) formc.getMock();
170
171         IMarkupWriter writer = newWriter();
172
173         IValidationDelegate delegate = newDelegate();
174
175         trainGetForm(cyclec, cycle, form);
176         trainWasPrerendered(formc, form, writer, component, false);
177         trainGetDelegate(formc, form, delegate);
178
179         delegate.setFormComponent(component);
180
181         trainGetElementId(formc, form, component, "barney");
182         trainIsRewinding(formc, form, true);
183         
184         trainGetParameter(cyclec, cycle, "barney", "rubble");
185         
186         support.bind(component, writer, cycle, "rubble");
187
188         replayControls();
189
190         component.render(writer, cycle);
191
192         verifyControls();
193     }
194
195     public void testRewindFormDisabled()
196     {
197         TextField component = (TextField) newInstance(TextField.class, "disabled", Boolean.TRUE);
198
199         MockControl cyclec = newControl(IRequestCycle.class);
200         IRequestCycle cycle = (IRequestCycle) cyclec.getMock();
201
202         MockControl formc = newControl(IForm.class);
203         IForm form = (IForm) formc.getMock();
204
205         IMarkupWriter writer = newWriter();
206
207         IValidationDelegate delegate = newDelegate();
208
209         trainGetForm(cyclec, cycle, form);
210         trainWasPrerendered(formc, form, writer, component, false);
211         trainGetDelegate(formc, form, delegate);
212
213         delegate.setFormComponent(component);
214
215         trainGetElementId(formc, form, component, "barney");
216         trainIsRewinding(formc, form, true);
217
218         replayControls();
219
220         component.render(writer, cycle);
221
222         assertNull(component.getProperty("value"));
223
224         verifyControls();
225     }
226
227     public void testRender()
228     {
229         MockControl translatorc = newControl(Translator.class);
230         Translator translator = (Translator) translatorc.getMock();
231
232         MockControl supportc = newControl(ValidatableFieldSupport.class);
233         ValidatableFieldSupport support = (ValidatableFieldSupport) supportc.getMock();
234
235         TextField component = (TextField) newInstance(
236                 TextField.class,
237                 new Object JavaDoc[]
238                 { "value", "text area value", "translator", translator, "validatableFieldSupport",
239                         support });
240
241         MockControl cyclec = newControl(IRequestCycle.class);
242         IRequestCycle cycle = (IRequestCycle) cyclec.getMock();
243
244         MockControl formc = newControl(IForm.class);
245         IForm form = (IForm) formc.getMock();
246
247         IMarkupWriter writer = newBufferWriter();
248
249         MockDelegate delegate = new MockDelegate();
250
251         trainGetForm(cyclec, cycle, form);
252         trainWasPrerendered(formc, form, writer, component, false);
253         trainGetDelegate(formc, form, delegate);
254
255         delegate.setFormComponent(component);
256
257         trainGetElementId(formc, form, component, "fred");
258         trainIsRewinding(formc, form, false);
259         trainIsRewinding(cyclec, cycle, false);
260
261         support.render(component, writer, cycle);
262
263         replayControls();
264
265         component.render(writer, cycle);
266
267         verifyControls();
268     }
269
270     public void testRenderValue()
271     {
272         MockControl translatorc = newControl(Translator.class);
273         Translator translator = (Translator) translatorc.getMock();
274
275         MockControl validatableFieldSupportc = newControl(ValidatableFieldSupport.class);
276         ValidatableFieldSupport validatableFieldSupport = (ValidatableFieldSupport) validatableFieldSupportc
277                 .getMock();
278
279         MockControl cyclec = newControl(IRequestCycle.class);
280         IRequestCycle cycle = (IRequestCycle) cyclec.getMock();
281
282         MockControl formc = newControl(IForm.class);
283         IForm form = (IForm) formc.getMock();
284
285         IMarkupWriter writer = newBufferWriter();
286
287         MockDelegate delegate = new MockDelegate();
288
289         TextField component = (TextField) newInstance(TextField.class, new Object JavaDoc[]
290         { "name", "fred", "translator", translator, "validatableFieldSupport",
291                 validatableFieldSupport, "form", form });
292
293         delegate.setFormComponent(component);
294
295         trainGetDelegate(formc, form, delegate);
296         trainGetDelegate(formc, form, delegate);
297         trainGetDelegate(formc, form, delegate);
298
299         validatableFieldSupport.renderContributions(component, writer, cycle);
300
301         replayControls();
302
303         component.render(writer, cycle, "10");
304
305         verifyControls();
306
307         assertBuffer("<span class=\"prefix\"><input type=\"text\" name=\"fred\" value=\"10\" class=\"validation-delegate\"/></span>");
308     }
309
310     public void testRenderHidden()
311     {
312         MockControl translatorc = newControl(Translator.class);
313         Translator translator = (Translator) translatorc.getMock();
314
315         MockControl validatableFieldSupportc = newControl(ValidatableFieldSupport.class);
316         ValidatableFieldSupport validatableFieldSupport = (ValidatableFieldSupport) validatableFieldSupportc
317                 .getMock();
318
319         MockControl cyclec = newControl(IRequestCycle.class);
320         IRequestCycle cycle = (IRequestCycle) cyclec.getMock();
321
322         MockControl formc = newControl(IForm.class);
323         IForm form = (IForm) formc.getMock();
324
325         IMarkupWriter writer = newBufferWriter();
326
327         MockDelegate delegate = new MockDelegate();
328
329         TextField component = (TextField) newInstance(TextField.class, new Object JavaDoc[]
330         { "name", "fred", "translator", translator, "validatableFieldSupport",
331                 validatableFieldSupport, "form", form, "hidden", Boolean.TRUE });
332
333         delegate.setFormComponent(component);
334
335         trainGetDelegate(formc, form, delegate);
336         trainGetDelegate(formc, form, delegate);
337         trainGetDelegate(formc, form, delegate);
338
339         validatableFieldSupport.renderContributions(component, writer, cycle);
340
341         replayControls();
342
343         component.render(writer, cycle, "10");
344
345         verifyControls();
346
347         assertBuffer("<span class=\"prefix\"><input type=\"password\" name=\"fred\" value=\"10\" class=\"validation-delegate\"/></span>");
348     }
349
350     public void testRenderDisabled()
351     {
352         MockControl translatorc = newControl(Translator.class);
353         Translator translator = (Translator) translatorc.getMock();
354
355         MockControl validatableFieldSupportc = newControl(ValidatableFieldSupport.class);
356         ValidatableFieldSupport validatableFieldSupport = (ValidatableFieldSupport) validatableFieldSupportc
357                 .getMock();
358
359         MockControl cyclec = newControl(IRequestCycle.class);
360         IRequestCycle cycle = (IRequestCycle) cyclec.getMock();
361
362         MockControl formc = newControl(IForm.class);
363         IForm form = (IForm) formc.getMock();
364
365         IMarkupWriter writer = newBufferWriter();
366
367         MockDelegate delegate = new MockDelegate();
368
369         TextField component = (TextField) newInstance(TextField.class, new Object JavaDoc[]
370         { "name", "fred", "translator", translator, "validatableFieldSupport",
371                 validatableFieldSupport, "form", form, "disabled", Boolean.TRUE });
372
373         delegate.setFormComponent(component);
374
375         trainGetDelegate(formc, form, delegate);
376         trainGetDelegate(formc, form, delegate);
377         trainGetDelegate(formc, form, delegate);
378
379         validatableFieldSupport.renderContributions(component, writer, cycle);
380
381         replayControls();
382
383         component.render(writer, cycle, "10");
384
385         verifyControls();
386
387         assertBuffer("<span class=\"prefix\"><input type=\"text\" name=\"fred\" disabled=\"disabled\" value=\"10\" class=\"validation-delegate\"/></span>");
388     }
389
390     public void testRenderWithInformalParameters()
391     {
392         MockControl translatorc = newControl(Translator.class);
393         Translator translator = (Translator) translatorc.getMock();
394
395         MockControl validatableFieldSupportc = newControl(ValidatableFieldSupport.class);
396         ValidatableFieldSupport validatableFieldSupport = (ValidatableFieldSupport) validatableFieldSupportc
397                 .getMock();
398
399         MockControl cyclec = newControl(IRequestCycle.class);
400         IRequestCycle cycle = (IRequestCycle) cyclec.getMock();
401
402         MockControl formc = newControl(IForm.class);
403         IForm form = (IForm) formc.getMock();
404
405         IMarkupWriter writer = newBufferWriter();
406
407         MockDelegate delegate = new MockDelegate();
408
409         IBinding binding = newBinding("informal-value");
410
411         TextField component = (TextField) newInstance(TextField.class, new Object JavaDoc[]
412         { "name", "fred", "translator", translator, "validatableFieldSupport",
413                 validatableFieldSupport, "form", form, "specification",
414                 new ComponentSpecification() });
415
416         component.setBinding("informal", binding);
417
418         delegate.setFormComponent(component);
419
420         trainGetDelegate(formc, form, delegate);
421         trainGetDelegate(formc, form, delegate);
422         trainGetDelegate(formc, form, delegate);
423
424         validatableFieldSupport.renderContributions(component, writer, cycle);
425
426         replayControls();
427
428         component.render(writer, cycle, "10");
429
430         verifyControls();
431
432         assertBuffer("<span class=\"prefix\"><input type=\"text\" name=\"fred\" value=\"10\" class=\"validation-delegate\" informal=\"informal-value\"/></span>");
433     }
434
435     public void testRenderNullValue()
436     {
437         MockControl translatorc = newControl(Translator.class);
438         Translator translator = (Translator) translatorc.getMock();
439
440         MockControl validatableFieldSupportc = newControl(ValidatableFieldSupport.class);
441         ValidatableFieldSupport validatableFieldSupport = (ValidatableFieldSupport) validatableFieldSupportc
442                 .getMock();
443
444         MockControl cyclec = newControl(IRequestCycle.class);
445         IRequestCycle cycle = (IRequestCycle) cyclec.getMock();
446
447         MockControl formc = newControl(IForm.class);
448         IForm form = (IForm) formc.getMock();
449
450         IMarkupWriter writer = newBufferWriter();
451
452         MockDelegate delegate = new MockDelegate();
453
454         TextField component = (TextField) newInstance(TextField.class, new Object JavaDoc[]
455         { "name", "fred", "translator", translator, "validatableFieldSupport",
456                 validatableFieldSupport, "form", form });
457
458         delegate.setFormComponent(component);
459
460         trainGetDelegate(formc, form, delegate);
461         trainGetDelegate(formc, form, delegate);
462         trainGetDelegate(formc, form, delegate);
463
464         validatableFieldSupport.renderContributions(component, writer, cycle);
465
466         replayControls();
467
468         component.render(writer, cycle, null);
469
470         verifyControls();
471
472         assertBuffer("<span class=\"prefix\"><input type=\"text\" name=\"fred\" class=\"validation-delegate\"/></span>");
473     }
474 }
475
Popular Tags