001// Copyright 2006-2014 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
015package org.apache.tapestry5.test;
016
017import org.apache.tapestry5.*;
018import org.apache.tapestry5.annotations.Id;
019import org.apache.tapestry5.annotations.Parameter;
020import org.apache.tapestry5.annotations.Path;
021import org.apache.tapestry5.beanmodel.BeanModel;
022import org.apache.tapestry5.beanmodel.PropertyConduit;
023import org.apache.tapestry5.beanmodel.PropertyModel;
024import org.apache.tapestry5.beanmodel.services.*;
025import org.apache.tapestry5.commons.*;
026import org.apache.tapestry5.commons.util.CollectionFactory;
027import org.apache.tapestry5.http.Link;
028import org.apache.tapestry5.http.services.BaseURLSource;
029import org.apache.tapestry5.http.services.Context;
030import org.apache.tapestry5.http.services.HttpServletRequestHandler;
031import org.apache.tapestry5.http.services.Request;
032import org.apache.tapestry5.http.services.RequestGlobals;
033import org.apache.tapestry5.http.services.RequestHandler;
034import org.apache.tapestry5.http.services.Response;
035import org.apache.tapestry5.http.services.Session;
036import org.apache.tapestry5.internal.services.MapMessages;
037import org.apache.tapestry5.internal.services.MarkupWriterImpl;
038import org.apache.tapestry5.ioc.annotations.Inject;
039import org.apache.tapestry5.ioc.internal.util.InternalUtils;
040import org.apache.tapestry5.ioc.test.IOCTestCase;
041import org.apache.tapestry5.model.ComponentModel;
042import org.apache.tapestry5.model.EmbeddedComponentModel;
043import org.apache.tapestry5.model.MutableComponentModel;
044import org.apache.tapestry5.model.ParameterModel;
045import org.apache.tapestry5.runtime.Component;
046import org.apache.tapestry5.services.ApplicationStateCreator;
047import org.apache.tapestry5.services.ApplicationStateManager;
048import org.apache.tapestry5.services.ApplicationStatePersistenceStrategy;
049import org.apache.tapestry5.services.ApplicationStatePersistenceStrategySource;
050import org.apache.tapestry5.services.AssetFactory;
051import org.apache.tapestry5.services.AssetSource;
052import org.apache.tapestry5.services.BindingFactory;
053import org.apache.tapestry5.services.BindingSource;
054import org.apache.tapestry5.services.ClasspathAssetAliasManager;
055import org.apache.tapestry5.services.ComponentClassResolver;
056import org.apache.tapestry5.services.ComponentEventRequestHandler;
057import org.apache.tapestry5.services.ComponentEventResultProcessor;
058import org.apache.tapestry5.services.ComponentRequestHandler;
059import org.apache.tapestry5.services.Environment;
060import org.apache.tapestry5.services.FieldTranslatorSource;
061import org.apache.tapestry5.services.FieldValidatorSource;
062import org.apache.tapestry5.services.FormSupport;
063import org.apache.tapestry5.services.Heartbeat;
064import org.apache.tapestry5.services.Html5Support;
065import org.apache.tapestry5.services.MetaDataLocator;
066import org.apache.tapestry5.services.PageRenderLinkSource;
067import org.apache.tapestry5.services.ResourceDigestGenerator;
068import org.apache.tapestry5.services.TranslatorSource;
069import org.apache.tapestry5.services.ValidationConstraintGenerator;
070import org.apache.tapestry5.services.ValueEncoderSource;
071import org.apache.tapestry5.services.javascript.JavaScriptSupport;
072import org.easymock.IAnswer;
073
074import javax.servlet.ServletOutputStream;
075import javax.servlet.http.HttpServletRequest;
076import javax.servlet.http.HttpServletResponse;
077import javax.servlet.http.HttpSession;
078import java.io.IOException;
079import java.io.InputStream;
080import java.net.URL;
081import java.util.Arrays;
082import java.util.Locale;
083import java.util.Map;
084import java.util.Properties;
085
086/**
087 * Base test case that adds a number of convenience factory and training methods for the public
088 * interfaces of
089 * Tapestry.
090 */
091@SuppressWarnings("all")
092public abstract class TapestryTestCase extends IOCTestCase
093{
094
095    /**
096     * Creates a new markup writer instance (not a markup writer mock). Output can be directed at
097     * the writer, which uses
098     * the default (HTML) markup model. The writer's toString() value represents all the collected
099     * markup in the
100     * writer.
101     */
102    protected final MarkupWriter createMarkupWriter()
103    {
104        return new MarkupWriterImpl();
105    }
106
107    protected final ApplicationStateCreator mockApplicationStateCreator()
108    {
109        return newMock(ApplicationStateCreator.class);
110    }
111
112    protected final ApplicationStatePersistenceStrategy mockApplicationStatePersistenceStrategy()
113    {
114        return newMock(ApplicationStatePersistenceStrategy.class);
115    }
116
117    protected final ApplicationStatePersistenceStrategySource mockApplicationStatePersistenceStrategySource()
118    {
119        return newMock(ApplicationStatePersistenceStrategySource.class);
120    }
121
122    protected final Asset mockAsset()
123    {
124        return newMock(Asset.class);
125    }
126
127    protected final AssetFactory mockAssetFactory()
128    {
129        return newMock(AssetFactory.class);
130    }
131
132    protected final AssetSource mockAssetSource()
133    {
134        return newMock(AssetSource.class);
135    }
136
137    protected final Binding mockBinding()
138    {
139        return newMock(Binding.class);
140    }
141
142    protected final BindingFactory mockBindingFactory()
143    {
144        return newMock(BindingFactory.class);
145    }
146
147    protected final BindingSource mockBindingSource()
148    {
149        return newMock(BindingSource.class);
150    }
151
152    protected final Block mockBlock()
153    {
154        return newMock(Block.class);
155    }
156
157    protected final ClasspathAssetAliasManager mockClasspathAssetAliasManager()
158    {
159        return newMock(ClasspathAssetAliasManager.class);
160    }
161
162    protected final Component mockComponent()
163    {
164        return newMock(Component.class);
165    }
166
167    protected final ComponentClassResolver mockComponentClassResolver()
168    {
169        return newMock(ComponentClassResolver.class);
170    }
171
172    protected final ComponentEventCallback mockComponentEventHandler()
173    {
174        return newMock(ComponentEventCallback.class);
175    }
176
177    protected final ComponentModel mockComponentModel()
178    {
179        return newMock(ComponentModel.class);
180    }
181
182    protected final ComponentResources mockComponentResources()
183    {
184        return newMock(ComponentResources.class);
185    }
186
187    protected final Context mockContext()
188    {
189        return newMock(Context.class);
190    }
191
192    protected final Environment mockEnvironment()
193    {
194        return newMock(Environment.class);
195    }
196
197    protected final Field mockField()
198    {
199        return newMock(Field.class);
200    }
201
202    protected final Html5Support mockHtml5Support()
203    {
204        return newMock(Html5Support.class);
205    }
206
207    protected final FieldValidator mockFieldValidator()
208    {
209        return newMock(FieldValidator.class);
210    }
211
212    protected FieldValidatorSource mockFieldValidatorSource()
213    {
214        return newMock(FieldValidatorSource.class);
215    }
216
217    protected final Field mockFieldWithLabel(String label)
218    {
219        Field field = mockField();
220
221        train_getLabel(field, label);
222
223        return field;
224    }
225
226    protected final Heartbeat mockHeartbeat()
227    {
228        return newMock(Heartbeat.class);
229    }
230
231    protected final HttpServletRequest mockHttpServletRequest()
232    {
233        return newMock(HttpServletRequest.class);
234    }
235
236    protected final HttpServletResponse mockHttpServletResponse()
237    {
238        return newMock(HttpServletResponse.class);
239    }
240
241    protected final HttpSession mockHttpSession()
242    {
243        return newMock(HttpSession.class);
244    }
245
246    protected final Inject mockInject()
247    {
248        return newMock(Inject.class);
249    }
250
251    protected final Link mockLink()
252    {
253        return newMock(Link.class);
254    }
255
256    protected final MarkupWriter mockMarkupWriter()
257    {
258        return newMock(MarkupWriter.class);
259    }
260
261    protected final MutableComponentModel mockMutableComponentModel()
262    {
263        return newMock(MutableComponentModel.class);
264    }
265
266    protected final ParameterModel mockParameterModel()
267    {
268        return newMock(ParameterModel.class);
269    }
270
271    protected final Path mockPath()
272    {
273        return newMock(Path.class);
274    }
275
276    protected final PropertyConduit mockPropertyConduit()
277    {
278        return newMock(PropertyConduit.class);
279    }
280
281    protected final PropertyModel mockPropertyModel()
282    {
283        return newMock(PropertyModel.class);
284    }
285
286    protected final Request mockRequest()
287    {
288        return newMock(Request.class);
289    }
290
291    protected final RequestHandler mockRequestHandler()
292    {
293        return newMock(RequestHandler.class);
294    }
295
296    protected final Response mockResponse()
297    {
298        return newMock(Response.class);
299    }
300
301    protected final Session mockSession()
302    {
303        return newMock(Session.class);
304    }
305
306    protected final Translator mockTranslator()
307    {
308        return newMock(Translator.class);
309    }
310
311    protected final ValidationConstraintGenerator mockValidationConstraintGenerator()
312    {
313        return newMock(ValidationConstraintGenerator.class);
314    }
315
316    protected final ValidationTracker mockValidationTracker()
317    {
318        return newMock(ValidationTracker.class);
319    }
320
321    protected final Validator mockValidator()
322    {
323        return newMock(Validator.class);
324    }
325
326    protected final void train_buildConstraints(ValidationConstraintGenerator generator, Class propertyType,
327                                                AnnotationProvider provider, String... constraints)
328    {
329        expect(generator.buildConstraints(propertyType, provider)).andReturn(Arrays.asList(constraints));
330    }
331
332    protected final <T> void train_create(ApplicationStateCreator<T> creator, T aso)
333    {
334        expect(creator.create()).andReturn(aso);
335    }
336
337    protected final void train_createAsset(AssetFactory factory, Resource resource, Asset asset)
338    {
339        expect(factory.createAsset(resource)).andReturn(asset);
340    }
341
342    protected final void train_createValidator(FieldValidatorSource source, Field field, String validatorType,
343                                               String constraintValue, String overrideId, Messages overrideMessages, Locale locale, FieldValidator result)
344    {
345        expect(source.createValidator(field, validatorType, constraintValue, overrideId, overrideMessages, locale))
346                .andReturn(result);
347    }
348
349    protected final void train_encodeRedirectURL(Response response, String URI, String encoded)
350    {
351        expect(response.encodeRedirectURL(URI)).andReturn(encoded);
352    }
353
354    protected final void train_encodeURL(Response response, String inputURL, String outputURL)
355    {
356        expect(response.encodeURL(inputURL)).andReturn(outputURL);
357    }
358
359    protected final <T> void train_exists(ApplicationStatePersistenceStrategy strategy, Class<T> asoClass,
360                                          boolean exists)
361    {
362        expect(strategy.exists(asoClass)).andReturn(exists);
363    }
364
365    protected final void train_getAsset(AssetSource source, Resource root, String path, Locale locale, Asset asset)
366    {
367        expect(source.getAsset(root, path, locale)).andReturn(asset);
368    }
369
370    protected final void train_generateChecksum(ResourceDigestGenerator generator, URL url, String digest)
371    {
372        expect(generator.generateDigest(url)).andReturn(digest);
373    }
374
375    protected final <T> void train_get(ApplicationStatePersistenceStrategy strategy, Class<T> asoClass,
376                                       ApplicationStateCreator<T> creator, T aso)
377    {
378        expect(strategy.get(asoClass, creator)).andReturn(aso);
379    }
380
381    protected final void train_get(ApplicationStatePersistenceStrategySource source, String strategyName,
382                                   ApplicationStatePersistenceStrategy strategy)
383    {
384        expect(source.get(strategyName)).andReturn(strategy).atLeastOnce();
385    }
386
387    protected final void train_get(Binding binding, Object value)
388    {
389        expect(binding.get()).andReturn(value);
390    }
391
392    protected final <T> void train_getIfExists(ApplicationStatePersistenceStrategy strategy, Class<T> asoClass,
393           T aso)
394    {
395        expect(strategy.getIfExists(asoClass)).andReturn(aso);
396    }
397
398    protected void train_getAttribute(HttpSession session, String attributeName, Object value)
399    {
400        expect(session.getAttribute(attributeName)).andReturn(value);
401    }
402
403    protected final void train_getAttribute(Session session, String name, Object attribute)
404    {
405        expect(session.getAttribute(name)).andReturn(attribute);
406    }
407
408    protected final void train_getAttributeNames(Session session, String prefix, String... names)
409    {
410        expect(session.getAttributeNames(prefix)).andReturn(Arrays.asList(names));
411    }
412
413    protected final void train_getBaseResource(ComponentModel model, Resource resource)
414    {
415        expect(model.getBaseResource()).andReturn(resource).atLeastOnce();
416    }
417
418    protected final void train_getClasspathAsset(AssetSource source, String path, Asset asset)
419    {
420        expect(source.getClasspathAsset(path)).andReturn(asset);
421    }
422
423    protected final void train_getClasspathAsset(AssetSource source, String path, Locale locale, Asset asset)
424    {
425        expect(source.getClasspathAsset(path, locale)).andReturn(asset);
426    }
427
428    protected final void train_getCompleteId(ComponentResourcesCommon resources, String completeId)
429    {
430        expect(resources.getCompleteId()).andReturn(completeId).atLeastOnce();
431    }
432
433    protected final void train_getComponent(ComponentResources resources, Component component)
434    {
435        expect(resources.getComponent()).andReturn(component).atLeastOnce();
436    }
437
438    protected final void train_getComponentClassName(ComponentModel model, String className)
439    {
440        expect(model.getComponentClassName()).andReturn(className).atLeastOnce();
441    }
442
443    protected final void train_getComponentResources(Component component, ComponentResources resources)
444    {
445        expect(component.getComponentResources()).andReturn(resources).atLeastOnce();
446    }
447
448    protected final void train_getConduit(PropertyModel model, PropertyConduit conduit)
449    {
450        expect(model.getConduit()).andReturn(conduit).atLeastOnce();
451    }
452
453    protected <C, T> void train_getConstraintType(Validator<C, T> validator, Class<C> constraintType)
454    {
455        expect(validator.getConstraintType()).andReturn(constraintType).atLeastOnce();
456    }
457
458    protected final void train_getContainer(ComponentResources resources, Component container)
459    {
460        expect(resources.getContainer()).andReturn(container).atLeastOnce();
461    }
462
463    protected final void train_getContainerMessages(ComponentResources resources, Messages containerMessages)
464    {
465        expect(resources.getContainerMessages()).andReturn(containerMessages).atLeastOnce();
466    }
467
468    protected final void train_getContainerResources(ComponentResources resources, ComponentResources containerResources)
469    {
470        expect(resources.getContainerResources()).andReturn(containerResources).atLeastOnce();
471    }
472
473    protected final void train_getDateHeader(Request request, String name, long value)
474    {
475        expect(request.getDateHeader(name)).andReturn(value).atLeastOnce();
476    }
477
478    protected final void train_getFieldPersistenceStrategy(ComponentModel model, String fieldName, String fieldStrategy)
479    {
480        expect(model.getFieldPersistenceStrategy(fieldName)).andReturn(fieldStrategy).atLeastOnce();
481    }
482
483    protected final void train_getId(ComponentResources resources, String id)
484    {
485        expect(resources.getId()).andReturn(id).atLeastOnce();
486    }
487
488    protected final void train_getLabel(Field field, String label)
489    {
490        expect(field.getLabel()).andReturn(label).atLeastOnce();
491    }
492
493    protected final void train_getLocale(ComponentResourcesCommon resources, Locale locale)
494    {
495        expect(resources.getLocale()).andReturn(locale).atLeastOnce();
496    }
497
498    protected final void train_getLocale(Request request, Locale locale)
499    {
500        expect(request.getLocale()).andReturn(locale).atLeastOnce();
501    }
502
503    protected void train_getMessageKey(Validator validator, String messageKey)
504    {
505        expect(validator.getMessageKey()).andReturn(messageKey).atLeastOnce();
506    }
507
508    protected final void train_getMessages(ComponentResources resources, Messages messages)
509    {
510        expect(resources.getMessages()).andReturn(messages).atLeastOnce();
511    }
512
513    protected final void train_getMeta(ComponentModel model, String key, String value)
514    {
515        expect(model.getMeta(key)).andReturn(value).atLeastOnce();
516    }
517
518    protected final void train_getOutputStream(HttpServletResponse response, ServletOutputStream stream)
519    {
520        try
521        {
522            expect(response.getOutputStream()).andReturn(stream);
523        } catch (IOException e)
524        {
525            fail(e.getMessage(), e);
526        }
527    }
528
529    protected final void train_getPage(ComponentResources resources, Component page)
530    {
531        expect(resources.getPage()).andReturn(page).atLeastOnce();
532    }
533
534    protected final void train_getParameterModel(ComponentModel model, String parameterName,
535                                                 ParameterModel parameterModel)
536    {
537        expect(model.getParameterModel(parameterName)).andReturn(parameterModel).atLeastOnce();
538    }
539
540    protected final void train_getParameterNames(ComponentModel model, String... names)
541    {
542        expect(model.getParameterNames()).andReturn(Arrays.asList(names));
543    }
544
545    protected final void train_getParentModel(ComponentModel model, ComponentModel parentModel)
546    {
547        expect(model.getParentModel()).andReturn(parentModel).atLeastOnce();
548    }
549
550    protected final void train_getPath(Request request, String path)
551    {
552        expect(request.getPath()).andReturn(path).atLeastOnce();
553    }
554
555    protected final void train_getPersistentFieldNames(ComponentModel model, String... names)
556    {
557        expect(model.getPersistentFieldNames()).andReturn(Arrays.asList(names)).atLeastOnce();
558    }
559
560    protected final void train_getRootResource(AssetFactory factory, Resource rootResource)
561    {
562        expect(factory.getRootResource()).andReturn(rootResource);
563    }
564
565    protected final void train_getSession(HttpServletRequest request, boolean create, HttpSession session)
566    {
567        expect(request.getSession(create)).andReturn(session);
568    }
569
570    protected void train_getSession(Request request, boolean create, Session session)
571    {
572        expect(request.getSession(create)).andReturn(session);
573    }
574
575    protected final void train_getSupportsInformalParameters(ComponentModel model, boolean supports)
576    {
577        expect(model.getSupportsInformalParameters()).andReturn(supports).atLeastOnce();
578    }
579
580    protected final void train_getValueType(Validator validator, Class valueType)
581    {
582        expect(validator.getValueType()).andReturn(valueType).atLeastOnce();
583    }
584
585    @SuppressWarnings("unchecked")
586    protected final void train_handleResult(ComponentEventCallback handler, Object result, boolean abort)
587    {
588        expect(handler.handleResult(result)).andReturn(abort);
589    }
590
591    protected final void train_inError(ValidationTracker tracker, Field field, boolean inError)
592    {
593        expect(tracker.inError(field)).andReturn(inError);
594    }
595
596    protected final void train_isRequired(Validator validator, boolean isRequired)
597    {
598        expect(validator.isRequired()).andReturn(isRequired).atLeastOnce();
599    }
600
601    protected final void train_isInvariant(Binding binding, boolean isInvariant)
602    {
603        expect(binding.isInvariant()).andReturn(isInvariant);
604    }
605
606    protected final void train_isRequired(ParameterModel model, boolean isRequired)
607    {
608        expect(model.isRequired()).andReturn(isRequired);
609    }
610
611    protected final void train_isRootClass(MutableComponentModel model, boolean isRootClass)
612    {
613        expect(model.isRootClass()).andReturn(isRootClass);
614    }
615
616    protected final void train_name(Parameter parameter, String name)
617    {
618        expect(parameter.name()).andReturn(name).atLeastOnce();
619    }
620
621    protected final void train_newBinding(BindingFactory factory, String description, ComponentResources container,
622                                          ComponentResources component, String expression, Location l, Binding binding)
623    {
624        expect(factory.newBinding(description, container, component, expression, l)).andReturn(binding);
625    }
626
627    protected void train_newBinding(BindingSource bindingSource, String description,
628                                    ComponentResources componentResources, String defaultBindingPrefix, String expression, Binding binding)
629    {
630        expect(bindingSource.newBinding(description, componentResources, defaultBindingPrefix, expression)).andReturn(
631                binding);
632    }
633
634    protected final <T> void train_peek(Environment env, Class<T> type, T value)
635    {
636        expect(env.peek(type)).andReturn(value);
637    }
638
639    protected final <T> void train_peekRequired(Environment env, Class<T> type, T value)
640    {
641        expect(env.peekRequired(type)).andReturn(value);
642    }
643
644    @SuppressWarnings("unchecked")
645    protected final void train_renderInformalParameters(ComponentResources resources, final MarkupWriter writer,
646                                                        final Object... informals)
647    {
648        resources.renderInformalParameters(writer);
649        IAnswer answer = new IAnswer()
650        {
651            public Object answer() throws Throwable
652            {
653                writer.attributes(informals);
654
655                return null;
656            }
657        };
658
659        setAnswer(answer);
660    }
661
662    protected final void train_service(RequestHandler handler, Request request, Response response, boolean result)
663            throws IOException
664    {
665        expect(handler.service(request, response)).andReturn(result);
666    }
667
668    protected final void train_setContentLength(HttpServletResponse response, int length)
669    {
670        response.setContentLength(length);
671    }
672
673    protected final void train_setContentType(HttpServletResponse response, String contentType)
674    {
675        response.setContentType(contentType);
676    }
677
678    protected final void train_setDateHeader(HttpServletResponse response, String headerName, long date)
679    {
680        response.setDateHeader(headerName, date);
681    }
682
683    /**
684     * @deprecated Deprecated in 5.4 with no replacement.
685     */
686    protected final void train_toClientURL(Asset asset, String URL)
687    {
688        expect(asset.toClientURL()).andReturn(URL).atLeastOnce();
689    }
690
691    protected final void train_toRedirectURI(Link link, String URI)
692    {
693        expect(link.toRedirectURI()).andReturn(URI).atLeastOnce();
694    }
695
696    protected final void train_value(Id annotation, String value)
697    {
698        expect(annotation.value()).andReturn(value).atLeastOnce();
699    }
700
701    protected final void train_value(Path annotation, String value)
702    {
703        expect(annotation.value()).andReturn(value).atLeastOnce();
704    }
705
706    protected final void train_getBoundType(ComponentResources resources, String parameterName, Class type)
707    {
708        expect(resources.getBoundType(parameterName)).andReturn(type);
709    }
710
711    protected final BeanModel mockBeanModel()
712    {
713        return newMock(BeanModel.class);
714    }
715
716    protected final BeanModelSource mockBeanModelSource()
717    {
718        return newMock(BeanModelSource.class);
719    }
720
721    public final void train_getLocation(Locatable locatable, Location location)
722    {
723        expect(locatable.getLocation()).andReturn(location).atLeastOnce();
724    }
725
726    public final void train_getResource(Location location, Resource resource)
727    {
728        expect(location.getResource()).andReturn(resource).atLeastOnce();
729    }
730
731    public final void train_getLine(Location location, int line)
732    {
733        expect(location.getLine()).andReturn(line).atLeastOnce();
734    }
735
736    protected final void train_getParameter(Request request, String elementName, String value)
737    {
738        expect(request.getParameter(elementName)).andReturn(value).atLeastOnce();
739    }
740
741    protected final void train_getPageName(ComponentResourcesCommon resources, String pageName)
742    {
743        expect(resources.getPageName()).andReturn(pageName).atLeastOnce();
744    }
745
746    protected final FormSupport mockFormSupport()
747    {
748        return newMock(FormSupport.class);
749    }
750
751    /**
752     * Provides access to component messages, suitable for testing. Reads the associated .properties
753     * file for the class
754     * (NOT any localization of it). Only the messages directly in the .properties file is
755     * available.
756     *
757     * @param componentClass component class whose messages are needed *
758     * @return the Messages instance
759     */
760    protected final Messages messagesFor(Class componentClass) throws IOException
761    {
762        String file = componentClass.getSimpleName() + ".properties";
763
764        Properties properties = new Properties();
765
766        InputStream is = null;
767
768        try
769        {
770            is = componentClass.getResourceAsStream(file);
771
772            if (is == null)
773                throw new RuntimeException(String.format("Class %s does not have a message catalog.",
774                        componentClass.getName()));
775
776            properties.load(is);
777        } finally
778        {
779            InternalUtils.close(is);
780        }
781
782        Map<String, String> map = CollectionFactory.newCaseInsensitiveMap();
783
784        for (Object key : properties.keySet())
785        {
786
787            String skey = (String) key;
788
789            map.put(skey, properties.getProperty(skey));
790        }
791
792        return new MapMessages(Locale.ENGLISH, map);
793    }
794
795    protected final FieldValidationSupport mockFieldValidationSupport()
796    {
797        return newMock(FieldValidationSupport.class);
798    }
799
800    protected final void train_getInheritInformalParameters(EmbeddedComponentModel model, boolean inherits)
801    {
802        expect(model.getInheritInformalParameters()).andReturn(inherits).atLeastOnce();
803    }
804
805    protected final ApplicationStateManager mockApplicationStateManager()
806    {
807        return newMock(ApplicationStateManager.class);
808    }
809
810    protected final <T> void train_get(ApplicationStateManager manager, Class<T> asoClass, T aso)
811    {
812        expect(manager.get(asoClass)).andReturn(aso);
813    }
814
815    protected final void train_getInput(ValidationTracker tracker, Field field, String input)
816    {
817        expect(tracker.getInput(field)).andReturn(input);
818    }
819
820    protected final void train_isXHR(Request request, boolean isXHR)
821    {
822        expect(request.isXHR()).andReturn(isXHR).atLeastOnce();
823    }
824
825    protected void train_getPathInfo(HttpServletRequest request, String pathInfo)
826    {
827        expect(request.getPathInfo()).andReturn(pathInfo).atLeastOnce();
828    }
829
830    protected final void train_service(HttpServletRequestHandler handler, HttpServletRequest request,
831                                       HttpServletResponse response, boolean result) throws IOException
832    {
833        expect(handler.service(request, response)).andReturn(result);
834    }
835
836    protected final void train_getServletPath(HttpServletRequest request, String path)
837    {
838        expect(request.getServletPath()).andReturn(path).atLeastOnce();
839    }
840
841    protected final HttpServletRequestHandler mockHttpServletRequestHandler()
842    {
843        return newMock(HttpServletRequestHandler.class);
844    }
845
846    protected final NullFieldStrategy mockNullFieldStrategy()
847    {
848        return newMock(NullFieldStrategy.class);
849    }
850
851    protected final ValueEncoderSource mockValueEncoderSource()
852    {
853        return newMock(ValueEncoderSource.class);
854    }
855
856    protected final ValueEncoder mockValueEncoder()
857    {
858        return newMock(ValueEncoder.class);
859    }
860
861    protected final void train_toClient(ValueEncoder valueEncoder, Object value, String encoded)
862    {
863        expect(valueEncoder.toClient(value)).andReturn(encoded);
864    }
865
866    protected final void train_getValueEncoder(ValueEncoderSource source, Class type, ValueEncoder valueEncoder)
867    {
868        expect(source.getValueEncoder(type)).andReturn(valueEncoder).atLeastOnce();
869    }
870
871    protected final void train_toValue(ValueEncoder valueEncoder, String clientValue, Object value)
872    {
873        expect(valueEncoder.toValue(clientValue)).andReturn(value);
874    }
875
876    protected <T> void train_findMeta(MetaDataLocator locator, String key, ComponentResources resources,
877                                      Class<T> expectedType, T value)
878    {
879        expect(locator.findMeta(key, resources, expectedType)).andReturn(value).atLeastOnce();
880    }
881
882    protected MetaDataLocator mockMetaDataLocator()
883    {
884        return newMock(MetaDataLocator.class);
885    }
886
887    protected final void train_isSecure(Request request, boolean isSecure)
888    {
889        expect(request.isSecure()).andReturn(isSecure).atLeastOnce();
890    }
891
892    protected final void train_getBaseURL(BaseURLSource baseURLSource, boolean secure, String baseURL)
893    {
894        expect(baseURLSource.getBaseURL(secure)).andReturn(baseURL);
895    }
896
897    protected final BaseURLSource mockBaseURLSource()
898    {
899        return newMock(BaseURLSource.class);
900    }
901
902    protected final void train_getAttribute(Request request, String attibuteName, Object value)
903    {
904        expect(request.getAttribute(attibuteName)).andReturn(value);
905    }
906
907    protected final void train_getBlockParameter(ComponentResources resources, String name, Block block)
908    {
909        expect(resources.getBlockParameter(name)).andReturn(block).atLeastOnce();
910    }
911
912    protected final PropertyOverrides mockPropertyOverrides()
913    {
914        return newMock(PropertyOverrides.class);
915    }
916
917    protected void train_getOverrideBlock(PropertyOverrides overrides, String name, Block block)
918    {
919        expect(overrides.getOverrideBlock(name)).andReturn(block).atLeastOnce();
920    }
921
922    protected final void train_getOverrideMessages(PropertyOverrides overrides, Messages messages)
923    {
924        expect(overrides.getOverrideMessages()).andReturn(messages);
925    }
926
927    protected final void train_isDisabled(Field field, boolean disabled)
928    {
929        expect(field.isDisabled()).andReturn(disabled);
930    }
931
932    protected final ValidationDecorator mockValidationDecorator()
933    {
934        return newMock(ValidationDecorator.class);
935    }
936
937    protected final void train_isRequired(Field field, boolean required)
938    {
939        expect(field.isRequired()).andReturn(required);
940    }
941
942    protected final void train_getClientId(ClientElement element, String clientId)
943    {
944        expect(element.getClientId()).andReturn(clientId);
945    }
946
947    protected final FieldTranslator mockFieldTranslator()
948    {
949        return newMock(FieldTranslator.class);
950    }
951
952    protected final Translator mockTranslator(String name, Class type)
953    {
954        Translator translator = mockTranslator();
955
956        train_getName(translator, name);
957        train_getType(translator, type);
958
959        return translator;
960    }
961
962    protected final void train_getName(Translator translator, String name)
963    {
964        expect(translator.getName()).andReturn(name).atLeastOnce();
965    }
966
967    protected final void train_getType(Translator translator, Class type)
968    {
969        expect(translator.getType()).andReturn(type).atLeastOnce();
970    }
971
972    protected final void train_createDefaultTranslator(FieldTranslatorSource source, ComponentResources resources,
973                                                       String parameterName, FieldTranslator translator)
974    {
975        expect(source.createDefaultTranslator(resources, parameterName)).andReturn(translator);
976    }
977
978    protected final TranslatorSource mockTranslatorSource()
979    {
980        return newMock(TranslatorSource.class);
981    }
982
983    protected final void train_get(TranslatorSource translatorSource, String name, Translator translator)
984    {
985        expect(translatorSource.get(name)).andReturn(translator).atLeastOnce();
986    }
987
988    protected final void train_getMessageKey(Translator translator, String messageKey)
989    {
990        expect(translator.getMessageKey()).andReturn(messageKey).atLeastOnce();
991    }
992
993    protected final void train_findByType(TranslatorSource ts, Class propertyType, Translator translator)
994    {
995        expect(ts.findByType(propertyType)).andReturn(translator);
996    }
997
998    protected final void train_toURI(Link link, String URI)
999    {
1000        expect(link.toURI()).andReturn(URI);
1001    }
1002
1003    protected final void train_createEditModel(BeanModelSource source, Class beanClass, Messages messages,
1004                                               BeanModel model)
1005    {
1006        expect(source.createEditModel(beanClass, messages)).andReturn(model);
1007    }
1008
1009    protected final ComponentEventResultProcessor mockComponentEventResultProcessor()
1010    {
1011        return newMock(ComponentEventResultProcessor.class);
1012    }
1013
1014    protected final void train_getFormComponentId(FormSupport formSupport, String componentId)
1015    {
1016        expect(formSupport.getFormComponentId()).andReturn(componentId).atLeastOnce();
1017    }
1018
1019    protected final void train_getFormValidationId(FormSupport formSupport, String validationId)
1020    {
1021        expect(formSupport.getFormValidationId()).andReturn(validationId).atLeastOnce();
1022    }
1023
1024    protected final void train_isAllowNull(ParameterModel model, boolean allowNull)
1025    {
1026        expect(model.isAllowNull()).andReturn(allowNull).atLeastOnce();
1027    }
1028
1029    protected final void train_isInvalidated(Session session, boolean invalidated)
1030    {
1031        expect(session.isInvalidated()).andReturn(invalidated);
1032    }
1033
1034    protected final ComponentEventRequestHandler mockComponentEventRequestHandler()
1035    {
1036        return newMock(ComponentEventRequestHandler.class);
1037    }
1038
1039    protected final ComponentRequestHandler mockComponentRequestHandler()
1040    {
1041        return newMock(ComponentRequestHandler.class);
1042    }
1043
1044    /**
1045     * @since 5.2.0
1046     */
1047    protected final RequestGlobals mockRequestGlobals()
1048    {
1049        return newMock(RequestGlobals.class);
1050    }
1051
1052    protected final PageRenderLinkSource mockPageRenderLinkSource()
1053    {
1054        return newMock(PageRenderLinkSource.class);
1055    }
1056
1057    protected final JavaScriptSupport mockJavaScriptSupport()
1058    {
1059        return newMock(JavaScriptSupport.class);
1060    }
1061
1062    protected final Asset mockAsset(String assetURL)
1063    {
1064        Asset asset = mockAsset();
1065
1066        train_toClientURL(asset, assetURL);
1067
1068        return asset;
1069    }
1070
1071    protected final Link mockLink(String absoluteURI)
1072    {
1073        Link link = mockLink();
1074
1075        expect(link.toURI()).andReturn(absoluteURI).atLeastOnce();
1076
1077        return link;
1078    }
1079}