Search in sources :

Example 6 with ValidatorMacro

use of org.apache.tapestry5.validator.ValidatorMacro 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)

Example 7 with ValidatorMacro

use of org.apache.tapestry5.validator.ValidatorMacro in project tapestry-5 by apache.

the class FieldValidatorSourceImplTest method single_validator_via_specification.

@SuppressWarnings("unchecked")
@Test
public void single_validator_via_specification() throws Exception {
    Validator validator = mockValidator();
    TypeCoercer coercer = mockTypeCoercer();
    FieldComponent field = newFieldComponent();
    Messages messages = mockMessages();
    MessageFormatter formatter = mockMessageFormatter();
    Object inputValue = new Object();
    ComponentResources resources = mockComponentResources();
    Messages containerMessages = mockMessages();
    FormSupport fs = mockFormSupport();
    Messages globalMessages = mockMessages();
    Map<String, Validator> map = singletonMap("required", validator);
    train_getFormValidationId(fs, "myform");
    train_getConstraintType(validator, null);
    train_getComponentResources(field, resources);
    train_getId(resources, "fred");
    train_getContainerMessages(resources, containerMessages);
    train_contains(containerMessages, "myform-fred-required-message", false);
    train_contains(containerMessages, "fred-required-message", false);
    train_getMessageKey(validator, "key");
    train_getMessageFormatter(globalMessages, "key", formatter);
    train_isRequired(validator, false);
    train_getValueType(validator, Object.class);
    validator.validate(field, null, formatter, inputValue);
    ValidatorMacro macro = mockValidatorMacro();
    train_alwaysNull(macro);
    replay();
    FieldValidatorSource source = new FieldValidatorSourceImpl(globalMessages, coercer, fs, map, macro);
    FieldValidator fieldValidator = source.createValidators(field, "required");
    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 8 with ValidatorMacro

use of org.apache.tapestry5.validator.ValidatorMacro in project tapestry-5 by apache.

the class FieldValidatorSourceImplTest method constraint_value_from_message_catalog_per.

@SuppressWarnings("unchecked")
@Test
public void constraint_value_from_message_catalog_per() throws Exception {
    Validator validator = mockValidator();
    TypeCoercer coercer = mockTypeCoercer();
    FieldComponent field = newFieldComponent();
    MessageFormatter formatter = mockMessageFormatter();
    Object inputValue = new Object();
    ComponentResources resources = mockComponentResources();
    Messages containerMessages = mockMessages();
    Messages globalMessages = 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_contains(containerMessages, "myform-fred-minlength", false);
    train_contains(containerMessages, "fred-minlength", true);
    train_get(containerMessages, "fred-minlength", "5");
    train_coerce(coercer, "5", Integer.class, 5);
    train_getContainerMessages(resources, containerMessages);
    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, 5, formatter, inputValue);
    ValidatorMacro macro = mockValidatorMacro();
    train_alwaysNull(macro);
    replay();
    FieldValidatorSource source = new FieldValidatorSourceImpl(globalMessages, coercer, fs, map, macro);
    FieldValidator fieldValidator = source.createValidators(field, "minlength");
    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 9 with ValidatorMacro

use of org.apache.tapestry5.validator.ValidatorMacro in project tapestry-5 by apache.

the class FieldValidatorSourceImplTest method constraint_value_from_message_catalog_per_form.

@SuppressWarnings("unchecked")
@Test
public void constraint_value_from_message_catalog_per_form() throws Exception {
    Validator validator = mockValidator();
    TypeCoercer coercer = mockTypeCoercer();
    FieldComponent field = newFieldComponent();
    Messages globalMessages = mockMessages();
    MessageFormatter formatter = mockMessageFormatter();
    Object inputValue = new Object();
    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_contains(containerMessages, "myform-fred-minlength", true);
    train_get(containerMessages, "myform-fred-minlength", "5");
    train_coerce(coercer, "5", Integer.class, 5);
    train_getContainerMessages(resources, containerMessages);
    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, 5, formatter, inputValue);
    ValidatorMacro macro = mockValidatorMacro();
    train_alwaysNull(macro);
    replay();
    FieldValidatorSource source = new FieldValidatorSourceImpl(globalMessages, coercer, fs, map, macro);
    FieldValidator fieldValidator = source.createValidators(field, "minlength");
    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 10 with ValidatorMacro

use of org.apache.tapestry5.validator.ValidatorMacro in project tapestry-5 by apache.

the class FieldValidatorSourceImplTest method unknown_validator_type.

@Test
public void unknown_validator_type() {
    Validator validator = mockValidator();
    TypeCoercer coercer = mockTypeCoercer();
    FieldComponent field = newFieldComponent();
    ComponentResources resources = mockComponentResources();
    Messages containerMessages = mockMessages();
    Map<String, Validator> map = newMap();
    ValidatorMacro macro = mockValidatorMacro();
    train_getComponentResources(field, resources);
    train_getId(resources, "fred");
    train_getContainerMessages(resources, containerMessages);
    train_alwaysNull(macro);
    map.put("alpha", validator);
    map.put("beta", validator);
    replay();
    FieldValidatorSource source = new FieldValidatorSourceImpl(null, coercer, null, map, macro);
    try {
        source.createValidator(field, "foo", null);
        unreachable();
    } catch (IllegalArgumentException ex) {
        assertEquals(ex.getMessage(), "Unknown validator type 'foo'. Configured validators are alpha, beta.");
    }
    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) ComponentResources(org.apache.tapestry5.ComponentResources) Test(org.testng.annotations.Test)

Aggregations

ValidatorMacro (org.apache.tapestry5.validator.ValidatorMacro)14 Test (org.testng.annotations.Test)14 ComponentResources (org.apache.tapestry5.ComponentResources)11 FieldValidator (org.apache.tapestry5.FieldValidator)11 Validator (org.apache.tapestry5.Validator)11 Messages (org.apache.tapestry5.commons.Messages)11 TypeCoercer (org.apache.tapestry5.commons.services.TypeCoercer)11 FieldValidatorSource (org.apache.tapestry5.services.FieldValidatorSource)11 FormSupport (org.apache.tapestry5.services.FormSupport)10 MessageFormatter (org.apache.tapestry5.commons.MessageFormatter)9