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.isA;
021    
022    import java.util.Locale;
023    
024    import org.apache.hivemind.ClassResolver;
025    import org.apache.hivemind.service.ThreadLocale;
026    import org.apache.tapestry.BaseComponentTestCase;
027    import org.apache.tapestry.IComponent;
028    import org.apache.tapestry.IForm;
029    import org.apache.tapestry.IMarkupWriter;
030    import org.apache.tapestry.IRequestCycle;
031    import org.apache.tapestry.PageRenderSupport;
032    import org.apache.tapestry.TapestryUtils;
033    import org.apache.tapestry.form.translator.Translator;
034    import org.apache.tapestry.services.Infrastructure;
035    import org.apache.tapestry.valid.IValidationDelegate;
036    import org.apache.tapestry.valid.ValidatorException;
037    import org.testng.annotations.Test;
038    
039    /**
040     * Test case for {@link TranslatedFieldSupportImpl}.
041     * 
042     * @author Paul Ferraro
043     * @since 4.0
044     */
045    @Test
046    public class TestTranslatedFieldSupportImpl extends BaseComponentTestCase
047    {
048        private ThreadLocale newThreadLocale()
049        {
050            ThreadLocale tl = newMock(ThreadLocale.class);
051            checkOrder(tl, false);
052            
053            expect(tl.getLocale()).andReturn(Locale.ENGLISH).anyTimes();
054    
055            return tl;
056        }
057    
058        private IRequestCycle newCycle(IComponent component)
059        {
060            IRequestCycle cycle = newCycle();
061    
062            ClassResolver cr = newMock(ClassResolver.class);
063            Infrastructure infra = newMock(Infrastructure.class);
064    
065            PageRenderSupport prs = newMock(PageRenderSupport.class);
066    
067            expect(cycle.getInfrastructure()).andReturn(infra);
068    
069            expect(infra.getClassResolver()).andReturn(cr);
070    
071            trainGetAttribute(cycle, TapestryUtils.PAGE_RENDER_SUPPORT_ATTRIBUTE, prs);
072    
073            return cycle;
074        }
075    
076        public void testRenderContributionsClientValidationDisabled()
077        {
078            TranslatedFieldSupportImpl support = new TranslatedFieldSupportImpl();
079            
080            TranslatedField field = newMock(TranslatedField.class);
081            IForm form = newMock(IForm.class);
082    
083            IMarkupWriter writer = newWriter();
084            IRequestCycle cycle = newCycle();
085    
086            expect(field.getForm()).andReturn(form);
087    
088            expect(form.isClientValidationEnabled()).andReturn(false);
089    
090            replay();
091    
092            support.renderContributions(field, writer, cycle);
093    
094            verify();
095        }
096    
097        public void testRenderContributionsClientValidationEnabled()
098        {
099            TranslatedFieldSupportImpl support = new TranslatedFieldSupportImpl();
100            support.setThreadLocale(newThreadLocale());
101    
102            TranslatedField field = newMock(TranslatedField.class);
103            IForm form = newMock(IForm.class);
104    
105            IMarkupWriter writer = newWriter();
106            IRequestCycle cycle = newCycle(field);
107            
108            Translator translator = newMock(Translator.class);
109    
110            expect(field.getForm()).andReturn(form);
111    
112            expect(form.isClientValidationEnabled()).andReturn(true);
113    
114            expect(field.getForm()).andReturn(form);
115    
116            expect(form.getName()).andReturn("myform");
117    
118            expect(field.getTranslator()).andReturn(translator);
119            
120            translator.renderContribution(
121                    eq(writer),
122                    eq(cycle),
123                    isA(FormComponentContributorContextImpl.class),
124                    eq(field));
125            
126            replay();
127    
128            support.renderContributions(field, writer, cycle);
129    
130            verify();
131        }
132    
133        public void testFormat() throws Exception
134        {
135            TranslatedFieldSupportImpl support = new TranslatedFieldSupportImpl();
136            
137            TranslatedField field = newMock(TranslatedField.class);
138            IForm form = newMock(IForm.class);
139            
140            IValidationDelegate delegate = newMock(IValidationDelegate.class);
141            Translator translator = newMock(Translator.class);
142    
143            Object object = new Object();
144            String expected = "result";
145    
146            expect(field.getForm()).andReturn(form);
147    
148            expect(form.getDelegate()).andReturn(delegate);
149    
150            expect(delegate.isInError()).andReturn(false);
151    
152            expect(field.getTranslator()).andReturn(translator);
153    
154            support.setThreadLocale(newThreadLocale());
155            
156            trainFormat(translator, field, object, expected);
157    
158            replay();
159    
160            String result = support.format(field, object);
161    
162            verify();
163    
164            assertSame(expected, result);
165        }
166    
167        private void trainFormat(Translator translator, TranslatedField field,
168                Object input, String result)
169        {
170    
171            expect(translator.format(field, Locale.ENGLISH, input)).andReturn(result);
172        }
173    
174        public void testFormatInError()
175        {
176            TranslatedFieldSupportImpl support = new TranslatedFieldSupportImpl();
177    
178            TranslatedField field = newMock(TranslatedField.class);
179            IForm form = newMock(IForm.class);
180            
181            IValidationDelegate delegate = newMock(IValidationDelegate.class);
182    
183            String expected = "result";
184    
185            expect(field.getForm()).andReturn(form);
186    
187            expect(form.getDelegate()).andReturn(delegate);
188    
189            expect(delegate.isInError()).andReturn(true);
190    
191            expect(delegate.getFieldInputValue()).andReturn(expected);
192    
193            replay();
194    
195            String result = support.format(field, new Object());
196    
197            verify();
198    
199            assertSame(expected, result);
200        }
201    
202        public void testParse() throws Exception
203        {
204            TranslatedFieldSupportImpl support = new TranslatedFieldSupportImpl();
205    
206            TranslatedField field = newMock(TranslatedField.class);
207            IForm form = newMock(IForm.class);
208            
209            IValidationDelegate delegate = newMock(IValidationDelegate.class);
210            Translator translator = newMock(Translator.class);
211    
212            String text = "test";
213            Object expected = new Object();
214    
215            expect(field.getForm()).andReturn(form);
216    
217            expect(form.getDelegate()).andReturn(delegate);
218    
219            delegate.recordFieldInputValue(text);
220            
221            support.setThreadLocale(newThreadLocale());
222            
223            expect(field.getTranslator()).andReturn(translator);
224            
225            trainParse(translator, field, text, expected);
226            
227            replay();
228    
229            Object result = support.parse(field, text);
230    
231            verify();
232    
233            assertSame(expected, result);
234    
235        }
236    
237        private void trainParse(Translator translator, TranslatedField field,
238                String text, Object result) throws ValidatorException
239        {
240            // ValidationMessages messages = new ValidationMessagesImpl(field, Locale.ENGLISH);
241    
242            expect(translator.parse(eq(field), isA(ValidationMessages.class), eq(text))).andReturn(result);
243        }
244    
245        public void testParseFailed() throws Exception
246        {
247            TranslatedFieldSupportImpl support = new TranslatedFieldSupportImpl();
248    
249            support.setThreadLocale(newThreadLocale());
250    
251            TranslatedField field = newMock(TranslatedField.class);
252            IForm form = newMock(IForm.class);
253            
254            IValidationDelegate delegate = newMock(IValidationDelegate.class);
255            Translator translator = newMock(Translator.class);
256    
257            String text = "test";
258    
259            expect(field.getForm()).andReturn(form);
260    
261            expect(form.getDelegate()).andReturn(delegate);
262    
263            delegate.recordFieldInputValue(text);
264    
265            expect(field.getTranslator()).andReturn(translator);
266            
267            ValidatorException expected = new ValidatorException("Failure");
268    
269            //ValidationMessages messages = new ValidationMessagesImpl(field, Locale.ENGLISH);
270            
271            expect(translator.parse(isA(TranslatedField.class), 
272                    isA(ValidationMessages.class), isA(String.class))).andThrow(expected);
273            
274            replay();
275    
276            try
277            {
278                support.parse(field, text);
279    
280                unreachable();
281            }
282            catch (ValidatorException e)
283            {
284                verify();
285    
286                assertSame(expected, e);
287            }
288        }
289    }