001    // Copyright 2005 The Apache Software Foundation
002    //
003    // Licensed under the Apache License, Version 2.0 (the "License");
004    // you may not use this file except in compliance with the License.
005    // You may obtain a copy of the License at
006    //
007    //     http://www.apache.org/licenses/LICENSE-2.0
008    //
009    // Unless required by applicable law or agreed to in writing, software
010    // distributed under the License is distributed on an "AS IS" BASIS,
011    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012    // See the License for the specific language governing permissions and
013    // limitations under the License.
014    
015    package org.apache.tapestry.form;
016    
017    import static org.easymock.EasyMock.checkOrder;
018    import static org.easymock.EasyMock.eq;
019    import static org.easymock.EasyMock.expect;
020    import static org.easymock.EasyMock.expectLastCall;
021    import static org.easymock.EasyMock.isA;
022    import static org.easymock.EasyMock.isNull;
023    
024    import java.util.Collection;
025    import java.util.Collections;
026    import java.util.Iterator;
027    import java.util.Locale;
028    
029    import org.apache.hivemind.ClassResolver;
030    import org.apache.hivemind.service.ThreadLocale;
031    import org.apache.tapestry.BaseComponentTestCase;
032    import org.apache.tapestry.IComponent;
033    import org.apache.tapestry.IForm;
034    import org.apache.tapestry.IMarkupWriter;
035    import org.apache.tapestry.IRequestCycle;
036    import org.apache.tapestry.PageRenderSupport;
037    import org.apache.tapestry.TapestryUtils;
038    import org.apache.tapestry.coerce.ValueConverter;
039    import org.apache.tapestry.form.validator.Validator;
040    import org.apache.tapestry.services.Infrastructure;
041    import org.apache.tapestry.valid.ValidatorException;
042    import org.testng.annotations.Test;
043    
044    /**
045     * Test case for {@link ValidatableFieldSupportImpl}.
046     * 
047     * @author Paul Ferraro
048     * @since 4.0
049     */
050    @Test
051    public class TestValidatableFieldSupportImpl extends BaseComponentTestCase
052    {
053        private ThreadLocale newThreadLocale()
054        {
055            ThreadLocale tl = newMock(ThreadLocale.class);
056            checkOrder(tl, false);
057            
058            expect(tl.getLocale()).andReturn(Locale.ENGLISH).anyTimes();
059    
060            return tl;
061        }
062        
063        /**
064         * Lots of work to set up the request cycle here, since we have to train it about getting the
065         * ClassResolver and the PageRenderSupport.
066         */
067        private IRequestCycle newCycle(IComponent component)
068        {
069            IRequestCycle cycle = newCycle();
070    
071            ClassResolver cr = newMock(ClassResolver.class);
072            
073            Infrastructure infra = newMock(Infrastructure.class);
074    
075            PageRenderSupport prs = newMock(PageRenderSupport.class);
076    
077            expect(cycle.getInfrastructure()).andReturn(infra);
078    
079            expect(infra.getClassResolver()).andReturn(cr);
080    
081            trainGetAttribute(cycle, TapestryUtils.PAGE_RENDER_SUPPORT_ATTRIBUTE, prs);
082    
083            return cycle;
084        }
085    
086        public void testRenderContributionsClientValidationDisabled()
087        {
088            ValidatableFieldSupportImpl support = new ValidatableFieldSupportImpl();
089            
090            TranslatedField field = newMock(TranslatedField.class);
091            IForm form = newMock(IForm.class);
092            
093            IMarkupWriter writer = newWriter();
094            IRequestCycle cycle = newCycle();
095            
096            expect(field.getForm()).andReturn(form);
097    
098            expect(form.isClientValidationEnabled()).andReturn(false);
099    
100            replay();
101    
102            support.renderContributions(field, writer, cycle);
103    
104            verify();
105        }
106    
107        public void testRenderContributionsClientValidationEnabledNoValidators()
108        {
109            ValueConverter converter = newMock(ValueConverter.class);
110            
111            ValidatableFieldSupportImpl support = new ValidatableFieldSupportImpl();
112            support.setThreadLocale(newThreadLocale());
113            support.setValueConverter(converter);
114            
115            TranslatedField field = newMock(TranslatedField.class);
116            
117            IForm form = newMock(IForm.class);
118            
119            IMarkupWriter writer = newWriter();
120            IRequestCycle cycle = newCycle(field);
121            
122            expect(field.getForm()).andReturn(form);
123    
124            expect(form.isClientValidationEnabled()).andReturn(true);
125    
126            expect(field.getForm()).andReturn(form);
127    
128            expect(form.getName()).andReturn("myform");
129    
130            expect(field.getValidators()).andReturn(null);
131            
132            expect(converter.coerceValue(null, Iterator.class)).andReturn(Collections.EMPTY_LIST.iterator());
133            
134            replay();
135    
136            support.renderContributions(field, writer, cycle);
137    
138            verify();
139        }
140    
141        public void testRenderContributionsClientValidationEnabled()
142        {
143            ValueConverter converter = newMock(ValueConverter.class);
144            
145            ValidatableFieldSupportImpl support = new ValidatableFieldSupportImpl();
146            
147            TranslatedField field = newMock(TranslatedField.class);
148            
149            IForm form = newMock(IForm.class);
150            
151            IMarkupWriter writer = newWriter();
152            
153            Validator validator = newMock(Validator.class);
154            
155            expect(field.getForm()).andReturn(form);
156    
157            expect(form.isClientValidationEnabled()).andReturn(true);
158    
159            support.setThreadLocale(newThreadLocale());
160            support.setValueConverter(converter);
161            
162            expect(field.getForm()).andReturn(form);
163    
164            expect(form.getName()).andReturn("myform");
165    
166            expect(field.getValidators()).andReturn(validator);
167            
168            expect(converter.coerceValue(validator, Iterator.class))
169            .andReturn(Collections.singleton(validator).iterator());
170            
171            IRequestCycle cycle = newCycle(field);
172            
173            validator.renderContribution(eq(writer), eq(cycle), 
174                    isA(FormComponentContributorContext.class), eq(field));
175            
176            replay();
177    
178            support.renderContributions(field, writer, cycle);
179    
180            verify();
181        }
182    
183        public void testValidate()
184        {
185            ValueConverter converter = newMock(ValueConverter.class);
186            
187            ValidatableFieldSupportImpl support = new ValidatableFieldSupportImpl();
188            support.setThreadLocale(newThreadLocale());
189            support.setValueConverter(converter);
190            
191            TranslatedField field = newMock(TranslatedField.class);
192            
193            IMarkupWriter writer = newWriter();
194            IRequestCycle cycle = newCycle();
195            
196            Validator validator = newMock(Validator.class);
197            
198            Object object = new Object();
199    
200            expect(field.getValidators()).andReturn(validator);
201            
202            expect(converter.coerceValue(validator, Iterator.class))
203            .andReturn(Collections.singleton(validator).iterator());
204            
205            try
206            {
207                validator.validate(eq(field), isA(ValidationMessages.class), eq(object));
208                
209                replay();
210        
211                support.validate(field, writer, cycle, object);
212        
213                verify();
214            }
215            catch (ValidatorException e)
216            {
217                unreachable();
218            }
219        }
220    
221        public void testValidateFailed()
222        {
223            ValueConverter converter = newMock(ValueConverter.class);
224            
225            ValidatableFieldSupportImpl support = new ValidatableFieldSupportImpl();
226            
227            TranslatedField field = newMock(TranslatedField.class);
228            
229            IMarkupWriter writer = newWriter();
230            IRequestCycle cycle = newCycle();
231            
232            Validator validator = newMock(Validator.class);
233            
234            Object object = new Object();
235    
236            expect(field.getValidators()).andReturn(validator);
237            
238            expect(converter.coerceValue(validator, Iterator.class))
239            .andReturn(Collections.singleton(validator).iterator());
240            
241            support.setThreadLocale(newThreadLocale());
242            support.setValueConverter(converter);
243            
244            ValidatorException expected = new ValidatorException("test");
245            
246            try
247            {
248                validator.validate(eq(field), isA(ValidationMessages.class), eq(object));
249                expectLastCall().andThrow(expected);
250                
251                replay();
252        
253                support.validate(field, writer, cycle, object);
254        
255                unreachable();
256            }
257            catch (ValidatorException e)
258            {
259                verify();
260                
261                assertSame(expected, e);
262            }
263        }
264    
265        public void testValidateNoValidators()
266        {
267            ValueConverter converter = newMock(ValueConverter.class);
268            
269            ValidatableFieldSupportImpl support = new ValidatableFieldSupportImpl();
270            
271            TranslatedField field = newMock(TranslatedField.class);
272            
273            IMarkupWriter writer = newWriter();
274            IRequestCycle cycle = newCycle();
275            
276            Object object = new Object();
277    
278            expect(field.getValidators()).andReturn(null);
279            
280            expect(converter.coerceValue(null, Iterator.class))
281            .andReturn(Collections.EMPTY_LIST.iterator());
282            
283            support.setThreadLocale(newThreadLocale());
284            support.setValueConverter(converter);
285            
286            try
287            {
288                replay();
289        
290                support.validate(field, writer, cycle, object);
291        
292                verify();
293            }
294            catch (ValidatorException e)
295            {
296                unreachable();
297            }
298        }
299    
300        public void testValidateAcceptNull()
301        {
302            ValueConverter converter = newMock(ValueConverter.class);
303            
304            ValidatableFieldSupportImpl support = new ValidatableFieldSupportImpl();
305            
306            TranslatedField field = newMock(TranslatedField.class);
307            
308            IMarkupWriter writer = newWriter();
309            
310            Validator validator = newMock(Validator.class);
311            
312            expect(field.getValidators()).andReturn(validator);
313            
314            expect(converter.coerceValue(validator, Iterator.class))
315            .andReturn(Collections.singleton(validator).iterator());
316    
317            support.setThreadLocale(newThreadLocale());
318            support.setValueConverter(converter);
319            
320            expect(validator.getAcceptsNull()).andReturn(true);
321            
322            IRequestCycle cycle = newCycle();
323            
324            try
325            {
326                validator.validate(eq(field), isA(ValidationMessages.class), isNull());
327                
328                replay();
329        
330                support.validate(field, writer, cycle, null);
331        
332                verify();
333            }
334            catch (ValidatorException e)
335            {
336                unreachable();
337            }
338        }
339    
340        public void testValidateRejectNull()
341        {
342            ValueConverter converter = newMock(ValueConverter.class);
343            
344            ValidatableFieldSupportImpl support = new ValidatableFieldSupportImpl();
345            
346            TranslatedField field = newMock(TranslatedField.class);
347            
348            IMarkupWriter writer = newWriter();
349            IRequestCycle cycle = newCycle();
350            
351            Validator validator = newMock(Validator.class);
352            
353            expect(field.getValidators()).andReturn(validator);
354            
355            expect(converter.coerceValue(validator, Iterator.class))
356            .andReturn(Collections.singleton(validator).iterator());
357    
358            support.setThreadLocale(newThreadLocale());
359            support.setValueConverter(converter);
360            
361            expect(validator.getAcceptsNull()).andReturn(false);
362            
363            try
364            {
365                replay();
366        
367                support.validate(field, writer, cycle, null);
368        
369                verify();
370            }
371            catch (ValidatorException e)
372            {
373                unreachable();
374            }
375        }
376    
377        private ValidatableField newFieldGetValidators(Collection validators)
378        {
379            ValidatableField field = newMock(ValidatableField.class);
380    
381            expect(field.getValidators()).andReturn(validators);
382    
383            return field;
384        }
385    
386        private ValueConverter newValueConverter(Collection validators)
387        {
388            ValueConverter converter = newMock(ValueConverter.class);
389    
390            expect(converter.coerceValue(validators, Iterator.class)).andReturn(validators.iterator());
391    
392            return converter;
393        }
394    
395        private Validator newValidator(boolean isRequired)
396        {
397            Validator validator = newMock(Validator.class);
398            checkOrder(validator, false);
399            
400            expect(validator.isRequired()).andReturn(isRequired);
401    
402            return validator;
403        }
404    
405        public void testIsRequiredNoValidators()
406        {
407            Collection validators = Collections.EMPTY_LIST;
408            ValidatableField field = newFieldGetValidators(validators);
409            ValueConverter converter = newValueConverter(validators);
410    
411            replay();
412    
413            ValidatableFieldSupportImpl support = new ValidatableFieldSupportImpl();
414            support.setValueConverter(converter);
415    
416            assertEquals(false, support.isRequired(field));
417    
418            verify();
419        }
420    
421        public void testIsRequiredNoRequiredValidators()
422        {
423            Collection validators = Collections.singletonList(newValidator(false));
424            ValidatableField field = newFieldGetValidators(validators);
425            ValueConverter converter = newValueConverter(validators);
426            
427            replay();
428    
429            ValidatableFieldSupportImpl support = new ValidatableFieldSupportImpl();
430            support.setValueConverter(converter);
431    
432            assertEquals(false, support.isRequired(field));
433    
434            verify();
435        }
436    
437        public void testIsRequiredWithRequiredValidator()
438        {
439            Collection validators = Collections.singletonList(newValidator(true));
440            ValidatableField field = newFieldGetValidators(validators);
441            ValueConverter converter = newValueConverter(validators);
442    
443            replay();
444    
445            ValidatableFieldSupportImpl support = new ValidatableFieldSupportImpl();
446            support.setValueConverter(converter);
447    
448            assertEquals(true, support.isRequired(field));
449    
450            verify();
451        }
452    }