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