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