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