Search in sources :

Example 26 with FieldValidator

use of org.apache.tapestry5.FieldValidator 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 27 with FieldValidator

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

the class FieldValidatorSourceImplTest method component_messages_overrides_validator_messages_per_form.

@Test
public void component_messages_overrides_validator_messages_per_form() 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", true);
    train_getMessageFormatter(containerMessages, "form-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 28 with FieldValidator

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

the class FieldValidatorSourceImplTest method validator_with_no_constraint.

@SuppressWarnings("unchecked")
@Test
public void validator_with_no_constraint() 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();
    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", 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);
    replay();
    FieldValidatorSource source = new FieldValidatorSourceImpl(globalMessages, 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 29 with FieldValidator

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

the class UploadTest method begin_render_invokes_field_validator.

@SuppressWarnings("unchecked")
@Test
public void begin_render_invokes_field_validator() throws Exception {
    getMocksControl().checkOrder(true);
    FieldValidator<Object> validate = mockFieldValidator();
    ComponentResources resources = mockComponentResources();
    Upload component = new Upload(null, validate, null, null, resources, null);
    MarkupWriter writer = createMarkupWriter();
    writer.element("form");
    Request request = mockRequest();
    FormSupport formSupport = mockFormSupport();
    formSupport.setEncodingType(Upload.MULTIPART_ENCTYPE);
    ValidationDecorator decorator = mockValidationDecorator();
    component.injectDecorator(decorator).injectRequest(request).injectFormSupport(formSupport);
    validate.render(writer);
    resources.renderInformalParameters(writer);
    decorator.insideField(component);
    train_isXHR(request, false);
    replay();
    component.beginRender(writer);
    verify();
}
Also used : Request(org.apache.tapestry5.http.services.Request) FormSupport(org.apache.tapestry5.services.FormSupport) Test(org.testng.annotations.Test)

Example 30 with FieldValidator

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

the class UploadTest method process_submission_calls_validator.

@SuppressWarnings("unchecked")
@Test
public void process_submission_calls_validator() throws Exception {
    MultipartDecoder decoder = mockMultipartDecoder();
    UploadedFile uploadedFile = mockUploadedFile();
    FieldValidator<Object> validate = mockFieldValidator();
    ComponentResources resources = mockComponentResources();
    FieldValidationSupport support = mockFieldValidationSupport();
    Upload component = new Upload(null, validate, decoder, null, resources, support);
    expect(decoder.getFileUpload("test")).andReturn(uploadedFile);
    expect(uploadedFile.getFileName()).andReturn("test").atLeastOnce();
    support.validate(uploadedFile, resources, validate);
    replay();
    component.processSubmission("test");
    verify();
}
Also used : UploadedFile(org.apache.tapestry5.upload.services.UploadedFile) MultipartDecoder(org.apache.tapestry5.upload.services.MultipartDecoder) Test(org.testng.annotations.Test)

Aggregations

Test (org.testng.annotations.Test)24 FieldValidator (org.apache.tapestry5.FieldValidator)22 Validator (org.apache.tapestry5.Validator)14 MessageFormatter (org.apache.tapestry5.commons.MessageFormatter)14 Messages (org.apache.tapestry5.commons.Messages)14 FieldValidatorSource (org.apache.tapestry5.services.FieldValidatorSource)13 ComponentResources (org.apache.tapestry5.ComponentResources)12 FormSupport (org.apache.tapestry5.services.FormSupport)12 Field (org.apache.tapestry5.Field)10 TypeCoercer (org.apache.tapestry5.commons.services.TypeCoercer)9 ValidatorMacro (org.apache.tapestry5.validator.ValidatorMacro)9 Locale (java.util.Locale)4 AnnotationProvider (org.apache.tapestry5.commons.AnnotationProvider)4 FieldValidatorDefaultSource (org.apache.tapestry5.services.FieldValidatorDefaultSource)4 MultipartDecoder (org.apache.tapestry5.upload.services.MultipartDecoder)4 UploadedFile (org.apache.tapestry5.upload.services.UploadedFile)4 Request (org.apache.tapestry5.http.services.Request)3 ValidationConstraintGenerator (org.apache.tapestry5.services.ValidationConstraintGenerator)3 Annotation (java.lang.annotation.Annotation)1 Binding (org.apache.tapestry5.Binding)1