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