Search in sources :

Example 1 with FieldComponent

use of org.apache.tapestry5.root.FieldComponent in project tapestry-5 by apache.

the class FieldValidatorSourceImplTest method multiple_validators_via_specification.

@SuppressWarnings("unchecked")
@Test
public void multiple_validators_via_specification() throws Exception {
    Validator required = mockValidator();
    Validator minLength = mockValidator();
    TypeCoercer coercer = mockTypeCoercer();
    FieldComponent field = newFieldComponent();
    Messages messages = mockMessages();
    MessageFormatter requiredFormatter = mockMessageFormatter();
    MessageFormatter minLengthFormatter = mockMessageFormatter();
    Object inputValue = "input value";
    ComponentResources resources = mockComponentResources();
    Messages containerMessages = mockMessages();
    Integer fifteen = 15;
    FormSupport fs = mockFormSupport();
    Messages globalMessages = mockMessages();
    Map<String, Validator> map = newMap();
    map.put("required", required);
    map.put("minLength", minLength);
    train_getFormValidationId(fs, "myform");
    train_getConstraintType(required, null);
    train_getConstraintType(minLength, Integer.class);
    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(required, "required");
    train_getMessageFormatter(globalMessages, "required", requiredFormatter);
    train_contains(containerMessages, "myform-fred-minLength-message", false);
    train_contains(containerMessages, "fred-minLength-message", false);
    train_getMessageKey(minLength, "min-length");
    train_getMessageFormatter(globalMessages, "min-length", minLengthFormatter);
    train_coerce(coercer, "15", Integer.class, fifteen);
    train_isRequired(required, true);
    train_getValueType(required, Object.class);
    required.validate(field, null, requiredFormatter, inputValue);
    train_isRequired(minLength, false);
    train_getValueType(minLength, String.class);
    minLength.validate(field, fifteen, minLengthFormatter, inputValue);
    ValidatorMacro macro = mockValidatorMacro();
    train_alwaysNull(macro);
    replay();
    FieldValidatorSource source = new FieldValidatorSourceImpl(globalMessages, coercer, fs, map, macro);
    FieldValidator fieldValidator = source.createValidators(field, "required,minLength=15");
    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 2 with FieldComponent

use of org.apache.tapestry5.root.FieldComponent 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 3 with FieldComponent

use of org.apache.tapestry5.root.FieldComponent 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 4 with FieldComponent

use of org.apache.tapestry5.root.FieldComponent 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 5 with FieldComponent

use of org.apache.tapestry5.root.FieldComponent 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

ComponentResources (org.apache.tapestry5.ComponentResources)13 Test (org.testng.annotations.Test)13 FieldValidator (org.apache.tapestry5.FieldValidator)12 Messages (org.apache.tapestry5.commons.Messages)12 FieldValidatorSource (org.apache.tapestry5.services.FieldValidatorSource)12 Validator (org.apache.tapestry5.Validator)11 TypeCoercer (org.apache.tapestry5.commons.services.TypeCoercer)11 FormSupport (org.apache.tapestry5.services.FormSupport)11 ValidatorMacro (org.apache.tapestry5.validator.ValidatorMacro)11 MessageFormatter (org.apache.tapestry5.commons.MessageFormatter)10 FieldComponent (org.apache.tapestry5.root.FieldComponent)2 Binding (org.apache.tapestry5.Binding)1 FieldTranslator (org.apache.tapestry5.FieldTranslator)1 MarkupWriter (org.apache.tapestry5.MarkupWriter)1 Translator (org.apache.tapestry5.Translator)1 Location (org.apache.tapestry5.commons.Location)1 BindingFactory (org.apache.tapestry5.services.BindingFactory)1 FieldTranslatorSource (org.apache.tapestry5.services.FieldTranslatorSource)1 TranslatorSource (org.apache.tapestry5.services.TranslatorSource)1