KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.Collections JavaDoc;
18 import java.util.Iterator JavaDoc;
19 import java.util.Locale JavaDoc;
20
21 import org.apache.hivemind.ClassResolver;
22 import org.apache.hivemind.service.ThreadLocale;
23 import org.apache.hivemind.test.AggregateArgumentsMatcher;
24 import org.apache.hivemind.test.ArgumentMatcher;
25 import org.apache.hivemind.test.TypeMatcher;
26 import org.apache.tapestry.IComponent;
27 import org.apache.tapestry.IForm;
28 import org.apache.tapestry.IMarkupWriter;
29 import org.apache.tapestry.IPage;
30 import org.apache.tapestry.IRequestCycle;
31 import org.apache.tapestry.PageRenderSupport;
32 import org.apache.tapestry.TapestryUtils;
33 import org.apache.tapestry.coerce.ValueConverter;
34 import org.apache.tapestry.components.BaseComponentTestCase;
35 import org.apache.tapestry.form.translator.Translator;
36 import org.apache.tapestry.form.validator.Validator;
37 import org.apache.tapestry.services.Infrastructure;
38 import org.apache.tapestry.valid.IValidationDelegate;
39 import org.apache.tapestry.valid.ValidatorException;
40 import org.easymock.MockControl;
41
42 /**
43  * Test case for {@link ValidatableFieldSupportImpl}. TODO: Desperate need to make this conform to
44  * the HiveMindTestCase conventions!
45  *
46  * @author Paul Ferraro
47  * @since 4.0
48  */

49 public class TestValidatableFieldSupportImpl extends BaseComponentTestCase
50 {
51     private ValidatableFieldSupportImpl _support = new ValidatableFieldSupportImpl();
52
53     private MockControl _componentControl = MockControl.createControl(ValidatableField.class);
54
55     private ValidatableField _component = (ValidatableField) _componentControl.getMock();
56
57     private MockControl _writerControl = MockControl.createControl(IMarkupWriter.class);
58
59     private IMarkupWriter _writer = (IMarkupWriter) _writerControl.getMock();
60
61     private MockControl _cycleControl = MockControl.createControl(IRequestCycle.class);
62
63     private IRequestCycle _cycle = (IRequestCycle) _cycleControl.getMock();
64
65     private MockControl _formControl = MockControl.createControl(IForm.class);
66
67     private IForm _form = (IForm) _formControl.getMock();
68
69     private MockControl _delegateControl = MockControl.createControl(IValidationDelegate.class);
70
71     private IValidationDelegate _delegate = (IValidationDelegate) _delegateControl.getMock();
72
73     private MockControl _translatorControl = MockControl.createControl(Translator.class);
74
75     private Translator _translator = (Translator) _translatorControl.getMock();
76
77     private MockControl _validatorControl = MockControl.createControl(Validator.class);
78
79     private Validator _validator = (Validator) _validatorControl.getMock();
80
81     private MockControl _valueConverterControl = MockControl.createControl(ValueConverter.class);
82
83     private ValueConverter _valueConverter = (ValueConverter) _valueConverterControl.getMock();
84
85     private ThreadLocale newThreadLocale()
86     {
87         MockControl control = newControl(ThreadLocale.class);
88         ThreadLocale tl = (ThreadLocale) control.getMock();
89
90         tl.getLocale();
91         control.setReturnValue(Locale.ENGLISH);
92
93         return tl;
94
95     }
96
97     /**
98      * @see junit.framework.TestCase#setUp()
99      */

100     protected void setUp() throws Exception JavaDoc
101     {
102         _support.setValueConverter(_valueConverter);
103     }
104
105     /**
106      * @see org.apache.hivemind.test.HiveMindTestCase#tearDown()
107      */

108     protected void tearDown() throws Exception JavaDoc
109     {
110         _componentControl.reset();
111         _writerControl.reset();
112         _cycleControl.reset();
113         _formControl.reset();
114         _delegateControl.reset();
115         _translatorControl.reset();
116         _validatorControl.reset();
117         _valueConverterControl.reset();
118
119         super.tearDown();
120     }
121
122     private void replay()
123     {
124         _componentControl.replay();
125         _writerControl.replay();
126         _cycleControl.replay();
127         _formControl.replay();
128         _delegateControl.replay();
129         _translatorControl.replay();
130         _validatorControl.replay();
131         _valueConverterControl.replay();
132
133         replayControls();
134     }
135
136     private void verify()
137     {
138         _componentControl.verify();
139         _writerControl.verify();
140         _cycleControl.verify();
141         _formControl.verify();
142         _delegateControl.verify();
143         _translatorControl.verify();
144         _validatorControl.verify();
145         _valueConverterControl.verify();
146
147         verifyControls();
148     }
149
150     public void testNullRender()
151     {
152         _component.getForm();
153         _componentControl.setReturnValue(_form);
154
155         _form.getDelegate();
156         _formControl.setReturnValue(_delegate);
157
158         _delegate.isInError();
159         _delegateControl.setReturnValue(false);
160
161         _component.readValue();
162         _componentControl.setReturnValue(null);
163
164         _component.render(_writer, _cycle, "");
165
166         replay();
167
168         _support.render(_component, _writer, _cycle);
169
170         verify();
171     }
172
173     public void testNotNullRender()
174     {
175         _component.getForm();
176         _componentControl.setReturnValue(_form);
177
178         _form.getDelegate();
179         _formControl.setReturnValue(_delegate);
180
181         _delegate.isInError();
182         _delegateControl.setReturnValue(false);
183
184         Object JavaDoc object = new Object JavaDoc();
185
186         _component.readValue();
187         _componentControl.setReturnValue(object);
188
189         _component.getTranslator();
190         _componentControl.setReturnValue(_translator);
191
192         String JavaDoc value = "some value";
193
194         _translator.format(_component, object);
195         _translatorControl.setReturnValue(value);
196
197         _component.render(_writer, _cycle, value);
198
199         replay();
200
201         _support.render(_component, _writer, _cycle);
202
203         verify();
204     }
205
206     public void testNotNullInErrorRender()
207     {
208         _component.getForm();
209         _componentControl.setReturnValue(_form);
210
211         _form.getDelegate();
212         _formControl.setReturnValue(_delegate);
213
214         _delegate.isInError();
215         _delegateControl.setReturnValue(true);
216
217         String JavaDoc value = "recorded value";
218
219         _delegate.getFieldInputValue();
220         _delegateControl.setReturnValue(value);
221
222         _component.render(_writer, _cycle, value);
223
224         replay();
225
226         _support.render(_component, _writer, _cycle);
227
228         verify();
229     }
230
231     public void testClientValidationDisabledRenderContributions()
232     {
233         _component.getForm();
234         _componentControl.setReturnValue(_form);
235
236         _form.isClientValidationEnabled();
237         _formControl.setReturnValue(false);
238
239         replay();
240
241         _support.renderContributions(_component, _writer, _cycle);
242
243         verify();
244     }
245
246     public void testClientValidationEnabledNoValidatorsRenderContributions()
247     {
248         _component.getForm();
249         _componentControl.setReturnValue(_form);
250
251         _form.isClientValidationEnabled();
252         _formControl.setReturnValue(true);
253
254         MockControl pagec = newControl(IPage.class);
255         IPage page = (IPage) pagec.getMock();
256
257         _component.getPage();
258         _componentControl.setReturnValue(page);
259
260         page.getLocale();
261         pagec.setReturnValue(Locale.ENGLISH);
262
263         _component.getForm();
264         _componentControl.setReturnValue(_form);
265
266         _form.getName();
267         _formControl.setReturnValue("myform");
268
269         _component.getName();
270         _componentControl.setReturnValue("myfield");
271
272         IRequestCycle cycle = newCycle(_component);
273
274         _component.getTranslator();
275         _componentControl.setReturnValue(_translator);
276
277         _translator.renderContribution(
278                 _writer,
279                 cycle,
280                 new FormComponentContributorContextImpl(),
281                 _component);
282         _translatorControl.setMatcher(new AggregateArgumentsMatcher(new ArgumentMatcher[]
283         { null, null, new TypeMatcher(), null }));
284
285         _component.getValidators();
286         _componentControl.setReturnValue(null);
287
288         _valueConverter.coerceValue(null, Iterator JavaDoc.class);
289         _valueConverterControl.setReturnValue(Collections.EMPTY_LIST.iterator());
290
291         replay();
292
293         _support.renderContributions(_component, _writer, cycle);
294
295         verify();
296     }
297
298     /**
299      * Lots of work to set up the request cycle here, since we have to train it about getting the
300      * ClassResolver and the PageRenderSupport.
301      */

302
303     protected IRequestCycle newCycle(IComponent component)
304     {
305         MockControl cyclec = newControl(IRequestCycle.class);
306         IRequestCycle cycle = (IRequestCycle) cyclec.getMock();
307
308         ClassResolver cr = (ClassResolver) newMock(ClassResolver.class);
309
310         MockControl infrac = newControl(Infrastructure.class);
311         Infrastructure infra = (Infrastructure) infrac.getMock();
312
313         PageRenderSupport prs = (PageRenderSupport) newMock(PageRenderSupport.class);
314
315         cycle.getInfrastructure();
316         cyclec.setReturnValue(infra);
317
318         infra.getClassResolver();
319         infrac.setReturnValue(cr);
320
321         trainGetAttribute(cyclec, cycle, TapestryUtils.PAGE_RENDER_SUPPORT_ATTRIBUTE, prs);
322
323         return cycle;
324     }
325
326     public void testClientValidationEnabledValidatorRenderContributions()
327     {
328         _component.getForm();
329         _componentControl.setReturnValue(_form);
330
331         _form.isClientValidationEnabled();
332         _formControl.setReturnValue(true);
333
334         MockControl pagec = newControl(IPage.class);
335         IPage page = (IPage) pagec.getMock();
336
337         _component.getPage();
338         _componentControl.setReturnValue(page);
339
340         page.getLocale();
341         pagec.setReturnValue(Locale.ENGLISH);
342
343         _component.getForm();
344         _componentControl.setReturnValue(_form);
345
346         _form.getName();
347         _formControl.setReturnValue("myform");
348
349         _component.getName();
350         _componentControl.setReturnValue("myfield");
351
352         IRequestCycle cycle = newCycle(_component);
353
354         _component.getTranslator();
355         _componentControl.setReturnValue(_translator);
356
357         _translator.renderContribution(
358                 _writer,
359                 cycle,
360                 new FormComponentContributorContextImpl(),
361                 _component);
362         _translatorControl.setMatcher(new AggregateArgumentsMatcher(new ArgumentMatcher[]
363         { null, null, new TypeMatcher(), null }));
364
365         _component.getValidators();
366         _componentControl.setReturnValue(_validator);
367
368         _valueConverter.coerceValue(_validator, Iterator JavaDoc.class);
369         _valueConverterControl.setReturnValue(Collections.singletonList(_validator).iterator());
370
371         _validator.renderContribution(
372                 _writer,
373                 cycle,
374                 new FormComponentContributorContextImpl(),
375                 _component);
376         _validatorControl.setMatcher(new AggregateArgumentsMatcher(new ArgumentMatcher[]
377         { null, null, new TypeMatcher(), null }));
378         replay();
379
380         _support.renderContributions(_component, _writer, cycle);
381
382         verify();
383     }
384
385     public void testNotNullNoValidatorsBind() throws Exception JavaDoc
386     {
387         _component.getForm();
388         _componentControl.setReturnValue(_form);
389
390         _form.getDelegate();
391         _formControl.setReturnValue(_delegate);
392
393         _delegate.recordFieldInputValue("some value");
394
395         _component.getTranslator();
396         _componentControl.setReturnValue(_translator);
397
398         Object JavaDoc object = new Object JavaDoc();
399
400         _translator.parse(_component, "some value");
401         _translatorControl.setReturnValue(object);
402
403         _component.writeValue(object);
404         _componentControl.setVoidCallable();
405
406         _component.getValidators();
407         _componentControl.setReturnValue(null);
408
409         _valueConverter.coerceValue(null, Iterator JavaDoc.class);
410         _valueConverterControl.setReturnValue(Collections.EMPTY_LIST.iterator());
411
412         _support.setThreadLocale(newThreadLocale());
413
414         replay();
415
416         _support.bind(_component, _writer, _cycle, "some value");
417
418         verify();
419     }
420
421     public void testNotNullTranslateFailBind() throws Exception JavaDoc
422     {
423         ValidatorException ex = new ValidatorException("Woops");
424
425         _component.getForm();
426         _componentControl.setReturnValue(_form);
427
428         _form.getDelegate();
429         _formControl.setReturnValue(_delegate);
430
431         _delegate.recordFieldInputValue("some value");
432
433         _component.getTranslator();
434         _componentControl.setReturnValue(_translator);
435
436         _translator.parse(_component, "some value");
437         _translatorControl.setThrowable(ex);
438
439         _delegate.record(ex);
440
441         replay();
442
443         _support.bind(_component, _writer, _cycle, "some value");
444
445         verify();
446
447     }
448
449     public void testNotNullBind() throws Exception JavaDoc
450     {
451         _component.getForm();
452         _componentControl.setReturnValue(_form);
453
454         _form.getDelegate();
455         _formControl.setReturnValue(_delegate);
456
457         _delegate.recordFieldInputValue("some value");
458
459         _component.getTranslator();
460         _componentControl.setReturnValue(_translator);
461
462         _support.setThreadLocale(newThreadLocale());
463
464         Object JavaDoc object = new Object JavaDoc();
465
466         _translator.parse(_component, "some value");
467         _translatorControl.setReturnValue(object);
468
469         _component.getValidators();
470         _componentControl.setReturnValue(_validator);
471
472         _valueConverter.coerceValue(_validator, Iterator JavaDoc.class);
473         _valueConverterControl.setReturnValue(Collections.singletonList(_validator).iterator());
474
475         _validator.validate(_component, new ValidationMessagesImpl(Locale.ENGLISH), object);
476         _validatorControl.setMatcher(new AggregateArgumentsMatcher(new ArgumentMatcher[]
477         { null, new TypeMatcher(), null }));
478
479         _component.writeValue(object);
480
481         replay();
482
483         _support.bind(_component, _writer, _cycle, "some value");
484
485         verify();
486     }
487
488     public void testNullBindValidatorAccepts() throws Exception JavaDoc
489     {
490         _component.getForm();
491         _componentControl.setReturnValue(_form);
492
493         _form.getDelegate();
494         _formControl.setReturnValue(_delegate);
495
496         _delegate.recordFieldInputValue("some value");
497
498         _component.getTranslator();
499         _componentControl.setReturnValue(_translator);
500
501         _support.setThreadLocale(newThreadLocale());
502
503         _translator.parse(_component, "some value");
504         _translatorControl.setReturnValue(null);
505
506         _component.getValidators();
507         _componentControl.setReturnValue(_validator);
508
509         _valueConverter.coerceValue(_validator, Iterator JavaDoc.class);
510         _valueConverterControl.setReturnValue(Collections.singletonList(_validator).iterator());
511
512         _validator.getAcceptsNull();
513         _validatorControl.setReturnValue(true);
514
515         _validator.validate(_component, new ValidationMessagesImpl(Locale.ENGLISH), null);
516         _validatorControl.setMatcher(new AggregateArgumentsMatcher(new ArgumentMatcher[]
517         { null, new TypeMatcher(), null }));
518
519         _component.writeValue(null);
520
521         replay();
522
523         _support.bind(_component, _writer, _cycle, "some value");
524
525         verify();
526     }
527
528     public void testNullBindValidatorRejects() throws Exception JavaDoc
529     {
530         _component.getForm();
531         _componentControl.setReturnValue(_form);
532
533         _form.getDelegate();
534         _formControl.setReturnValue(_delegate);
535
536         _delegate.recordFieldInputValue("some value");
537
538         _component.getTranslator();
539         _componentControl.setReturnValue(_translator);
540
541         _support.setThreadLocale(newThreadLocale());
542
543         _translator.parse(_component, "some value");
544         _translatorControl.setReturnValue(null);
545
546         _component.getValidators();
547         _componentControl.setReturnValue(_validator);
548
549         _valueConverter.coerceValue(_validator, Iterator JavaDoc.class);
550         _valueConverterControl.setReturnValue(Collections.singletonList(_validator).iterator());
551
552         _validator.getAcceptsNull();
553         _validatorControl.setReturnValue(false);
554
555         _component.writeValue(null);
556
557         replay();
558
559         _support.bind(_component, _writer, _cycle, "some value");
560
561         verify();
562     }
563
564     public void testNotNullValidateFailBind() throws Exception JavaDoc
565     {
566         _component.getForm();
567         _componentControl.setReturnValue(_form);
568
569         _form.getDelegate();
570         _formControl.setReturnValue(_delegate);
571
572         _delegate.recordFieldInputValue("some value");
573
574         _component.getTranslator();
575         _componentControl.setReturnValue(_translator);
576
577         _support.setThreadLocale(newThreadLocale());
578
579         ValidatorException ex = new ValidatorException("");
580
581         Object JavaDoc object = new Object JavaDoc();
582
583         _translator.parse(_component, "some value");
584         _translatorControl.setReturnValue(object);
585
586         _component.getValidators();
587         _componentControl.setReturnValue(_validator);
588
589         _valueConverter.coerceValue(_validator, Iterator JavaDoc.class);
590         _valueConverterControl.setReturnValue(Collections.singletonList(_validator).iterator());
591
592         _validator.validate(_component, new ValidationMessagesImpl(Locale.ENGLISH), object);
593         _validatorControl.setMatcher(new AggregateArgumentsMatcher(new ArgumentMatcher[]
594         { null, new TypeMatcher(), null }));
595         _validatorControl.setThrowable(ex);
596
597         _delegate.record(ex);
598
599         replay();
600
601         _support.bind(_component, _writer, _cycle, "some value");
602
603         verify();
604     }
605 }
606
Popular Tags