Search in sources :

Example 16 with TypeCoercer

use of org.apache.tapestry5.commons.services.TypeCoercer in project tapestry-5 by apache.

the class LinkSourceImplTest method testPageLinkCreation.

private void testPageLinkCreation(String pageName, boolean overrideContext, Object... context) {
    PageActivationContextCollector collector = mockPageActivationContextCollector();
    LinkCreationListener2 listener = mockLinkCreationListener2();
    ComponentEventLinkEncoder linkEncoder = mockComponentEventLinkEncoder();
    Link link = mockLink();
    ComponentClassResolver resolver = mockComponentClassResolver();
    String canonical = "CanonicalPageName";
    RequestGlobals globals = mockRequestGlobals();
    // Ensure that the loopback logic is off; loopback logic is fully tested via an
    // integration test.
    expect(globals.getActivePageName()).andReturn("someOtherPage");
    train_canonicalizePageName(resolver, pageName, canonical);
    if (!overrideContext)
        train_collectPageActivationContext(collector, canonical, context);
    PageRenderRequestParameters parameters = new PageRenderRequestParameters(canonical, new ArrayEventContext(typeCoercer, context), false);
    expect(linkEncoder.createPageRenderLink(parameters)).andReturn(link);
    listener.createdPageRenderLink(link, parameters);
    replay();
    LinkSource source = new LinkSourceImpl(null, collector, typeCoercer, resolver, linkEncoder, globals, null, emptyConfiguration);
    source.getLinkCreationHub().addListener(listener);
    Object[] passedContext = overrideContext ? context : new Object[0];
    Link returnedLink = source.createPageRenderLink(pageName, overrideContext, passedContext);
    assertSame(returnedLink, link);
    verify();
}
Also used : PageRenderRequestParameters(org.apache.tapestry5.services.PageRenderRequestParameters) ComponentEventLinkEncoder(org.apache.tapestry5.services.ComponentEventLinkEncoder) ComponentClassResolver(org.apache.tapestry5.services.ComponentClassResolver) LinkCreationListener2(org.apache.tapestry5.services.LinkCreationListener2) RequestGlobals(org.apache.tapestry5.http.services.RequestGlobals) Link(org.apache.tapestry5.http.Link)

Example 17 with TypeCoercer

use of org.apache.tapestry5.commons.services.TypeCoercer in project tapestry-5 by apache.

the class FieldValidatorSourceImplTest method validator_with_constraint_and_macro.

@SuppressWarnings("unchecked")
@Test
public void validator_with_constraint_and_macro() throws Exception {
    Validator validator = mockValidator();
    TypeCoercer coercer = mockTypeCoercer();
    FieldComponent field = newFieldComponent();
    MessageFormatter formatter = mockMessageFormatter();
    Object inputValue = new Object();
    ComponentResources resources = mockComponentResources();
    Messages containerMessages = mockMessages();
    Integer five = 5;
    FormSupport fs = mockFormSupport();
    ValidatorMacro macro = mockValidatorMacro();
    Messages globalMessages = mockMessages();
    Map<String, Validator> map = singletonMap("minLength", validator);
    train_getConstraintType(validator, Integer.class);
    train_getFormValidationId(fs, "myform");
    train_coerce(coercer, "77", Integer.class, five);
    train_getComponentResources(field, resources);
    train_getId(resources, "fred");
    train_getContainerMessages(resources, containerMessages);
    expect(macro.valueForMacro("foo-bar-baz")).andReturn("minLength=77");
    expect(macro.valueForMacro("minLength")).andReturn(null);
    train_contains(containerMessages, "myform-fred-minLength-message", false);
    train_contains(containerMessages, "fred-minLength-message", false);
    train_getMessageKey(validator, "key");
    train_getMessageFormatter(globalMessages, "key", formatter);
    train_isRequired(validator, false);
    train_getValueType(validator, Object.class);
    validator.validate(field, five, formatter, inputValue);
    replay();
    FieldValidatorSource source = new FieldValidatorSourceImpl(globalMessages, coercer, fs, map, macro);
    FieldValidator fieldValidator = source.createValidator(field, "foo-bar-baz", null);
    fieldValidator.validate(inputValue);
    verify();
}
Also used : Messages(org.apache.tapestry5.commons.Messages) TypeCoercer(org.apache.tapestry5.commons.services.TypeCoercer) FieldValidator(org.apache.tapestry5.FieldValidator) ValidatorMacro(org.apache.tapestry5.validator.ValidatorMacro) MessageFormatter(org.apache.tapestry5.commons.MessageFormatter) FormSupport(org.apache.tapestry5.services.FormSupport) ComponentResources(org.apache.tapestry5.ComponentResources) FieldValidatorSource(org.apache.tapestry5.services.FieldValidatorSource) FieldValidator(org.apache.tapestry5.FieldValidator) Validator(org.apache.tapestry5.Validator) Test(org.testng.annotations.Test)

Example 18 with TypeCoercer

use of org.apache.tapestry5.commons.services.TypeCoercer in project tapestry-5 by apache.

the class FieldValidatorSourceImplTest method component_messages_overrides_validator_messages.

@SuppressWarnings("unchecked")
@Test
public void component_messages_overrides_validator_messages() throws Exception {
    Validator validator = mockValidator();
    TypeCoercer coercer = mockTypeCoercer();
    FieldComponent field = newFieldComponent();
    MessageFormatter formatter = mockMessageFormatter();
    Object inputValue = new Object();
    ComponentResources resources = mockComponentResources();
    Messages containerMessages = mockMessages();
    FormSupport fs = mockFormSupport();
    ValidatorMacro macro = mockValidatorMacro();
    Map<String, Validator> map = singletonMap("required", validator);
    train_getConstraintType(validator, null);
    train_getFormValidationId(fs, "form");
    train_getComponentResources(field, resources);
    train_getId(resources, "fred");
    train_getContainerMessages(resources, containerMessages);
    train_alwaysNull(macro);
    train_contains(containerMessages, "form-fred-required-message", false);
    train_contains(containerMessages, "fred-required-message", true);
    train_getMessageFormatter(containerMessages, "fred-required-message", formatter);
    train_isRequired(validator, false);
    train_getValueType(validator, Object.class);
    validator.validate(field, null, formatter, inputValue);
    replay();
    FieldValidatorSource source = new FieldValidatorSourceImpl(null, coercer, fs, map, macro);
    FieldValidator fieldValidator = source.createValidator(field, "required", null);
    fieldValidator.validate(inputValue);
    verify();
}
Also used : Messages(org.apache.tapestry5.commons.Messages) TypeCoercer(org.apache.tapestry5.commons.services.TypeCoercer) FieldValidator(org.apache.tapestry5.FieldValidator) ValidatorMacro(org.apache.tapestry5.validator.ValidatorMacro) MessageFormatter(org.apache.tapestry5.commons.MessageFormatter) FormSupport(org.apache.tapestry5.services.FormSupport) ComponentResources(org.apache.tapestry5.ComponentResources) FieldValidatorSource(org.apache.tapestry5.services.FieldValidatorSource) FieldValidator(org.apache.tapestry5.FieldValidator) Validator(org.apache.tapestry5.Validator) Test(org.testng.annotations.Test)

Example 19 with TypeCoercer

use of org.apache.tapestry5.commons.services.TypeCoercer in project tapestry-5 by apache.

the class FieldValidatorSourceImplTest method missing_field_validator_constraint.

@SuppressWarnings("unchecked")
@Test
public void missing_field_validator_constraint() throws Exception {
    Validator validator = mockValidator();
    TypeCoercer coercer = mockTypeCoercer();
    FieldComponent field = newFieldComponent();
    ComponentResources resources = mockComponentResources();
    Messages containerMessages = mockMessages();
    FormSupport fs = mockFormSupport();
    Map<String, Validator> map = singletonMap("minlength", validator);
    train_getConstraintType(validator, Integer.class);
    train_getFormValidationId(fs, "myform");
    train_getComponentResources(field, resources);
    train_getId(resources, "fred");
    train_getContainerMessages(resources, containerMessages);
    train_contains(containerMessages, "myform-fred-minlength", false);
    train_contains(containerMessages, "fred-minlength", false);
    ValidatorMacro macro = mockValidatorMacro();
    train_alwaysNull(macro);
    replay();
    FieldValidatorSource source = new FieldValidatorSourceImpl(null, coercer, fs, map, macro);
    try {
        source.createValidators(field, "minlength");
        unreachable();
    } catch (IllegalArgumentException ex) {
        assertEquals(ex.getMessage(), "Validator 'minlength' requires a validation constraint (of type java.lang.Integer) but none was provided. The constraint may be provided inside the @Validator annotation on the property, or in the associated component message catalog as key 'myform-fred-minlength' or key 'fred-minlength'.");
    }
    verify();
}
Also used : FieldValidatorSource(org.apache.tapestry5.services.FieldValidatorSource) Messages(org.apache.tapestry5.commons.Messages) TypeCoercer(org.apache.tapestry5.commons.services.TypeCoercer) ValidatorMacro(org.apache.tapestry5.validator.ValidatorMacro) FieldValidator(org.apache.tapestry5.FieldValidator) Validator(org.apache.tapestry5.Validator) FormSupport(org.apache.tapestry5.services.FormSupport) ComponentResources(org.apache.tapestry5.ComponentResources) Test(org.testng.annotations.Test)

Example 20 with TypeCoercer

use of org.apache.tapestry5.commons.services.TypeCoercer in project tapestry-5 by apache.

the class FieldValidatorSourceImplTest method validator_with_constraint.

@SuppressWarnings("unchecked")
@Test
public void validator_with_constraint() throws Exception {
    Validator validator = mockValidator();
    TypeCoercer coercer = mockTypeCoercer();
    FieldComponent field = newFieldComponent();
    MessageFormatter formatter = mockMessageFormatter();
    Object inputValue = new Object();
    ComponentResources resources = mockComponentResources();
    Messages containerMessages = mockMessages();
    Integer five = 5;
    FormSupport fs = mockFormSupport();
    ValidatorMacro macro = mockValidatorMacro();
    Messages globalMessages = mockMessages();
    Map<String, Validator> map = singletonMap("minLength", validator);
    train_getConstraintType(validator, Integer.class);
    train_getFormValidationId(fs, "myform");
    train_coerce(coercer, "5", Integer.class, five);
    train_getComponentResources(field, resources);
    train_getId(resources, "fred");
    train_getContainerMessages(resources, containerMessages);
    train_alwaysNull(macro);
    train_contains(containerMessages, "myform-fred-minLength-message", false);
    train_contains(containerMessages, "fred-minLength-message", false);
    train_getMessageKey(validator, "key");
    train_getMessageFormatter(globalMessages, "key", formatter);
    train_isRequired(validator, false);
    train_getValueType(validator, Object.class);
    validator.validate(field, five, formatter, inputValue);
    replay();
    FieldValidatorSource source = new FieldValidatorSourceImpl(globalMessages, coercer, fs, map, macro);
    FieldValidator fieldValidator = source.createValidator(field, "minLength", "5");
    fieldValidator.validate(inputValue);
    verify();
}
Also used : Messages(org.apache.tapestry5.commons.Messages) TypeCoercer(org.apache.tapestry5.commons.services.TypeCoercer) FieldValidator(org.apache.tapestry5.FieldValidator) ValidatorMacro(org.apache.tapestry5.validator.ValidatorMacro) MessageFormatter(org.apache.tapestry5.commons.MessageFormatter) FormSupport(org.apache.tapestry5.services.FormSupport) ComponentResources(org.apache.tapestry5.ComponentResources) FieldValidatorSource(org.apache.tapestry5.services.FieldValidatorSource) FieldValidator(org.apache.tapestry5.FieldValidator) Validator(org.apache.tapestry5.Validator) Test(org.testng.annotations.Test)

Aggregations

Test (org.testng.annotations.Test)43 TypeCoercer (org.apache.tapestry5.commons.services.TypeCoercer)27 ComponentResources (org.apache.tapestry5.ComponentResources)19 Messages (org.apache.tapestry5.commons.Messages)13 FieldValidator (org.apache.tapestry5.FieldValidator)11 Validator (org.apache.tapestry5.Validator)11 SymbolSource (org.apache.tapestry5.ioc.services.SymbolSource)11 FieldValidatorSource (org.apache.tapestry5.services.FieldValidatorSource)11 ValidatorMacro (org.apache.tapestry5.validator.ValidatorMacro)11 Link (org.apache.tapestry5.http.Link)10 ComponentModel (org.apache.tapestry5.model.ComponentModel)10 FormSupport (org.apache.tapestry5.services.FormSupport)10 MessageFormatter (org.apache.tapestry5.commons.MessageFormatter)9 MetaDataLocator (org.apache.tapestry5.services.MetaDataLocator)9 EventContext (org.apache.tapestry5.EventContext)7 ComponentEventLinkEncoder (org.apache.tapestry5.services.ComponentEventLinkEncoder)7 PageRenderRequestParameters (org.apache.tapestry5.services.PageRenderRequestParameters)7 LinkCreationListener2 (org.apache.tapestry5.services.LinkCreationListener2)6 MarkupWriter (org.apache.tapestry5.MarkupWriter)5 HibernateEntityValueEncoder (org.apache.tapestry5.hibernate.web.internal.HibernateEntityValueEncoder)4