Search in sources :

Example 16 with Validator

use of org.apache.tapestry5.Validator 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 17 with Validator

use of org.apache.tapestry5.Validator 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 18 with Validator

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

the class BeanFieldValidatorDefaultSource method createDefaultValidator.

@Override
public FieldValidator createDefaultValidator(final Field field, final String overrideId, final Messages overrideMessages, final Locale locale, final Class propertyType, final AnnotationProvider propertyAnnotations) {
    FieldValidator validator = fieldValidatorDefaultSource.createDefaultValidator(field, overrideId, overrideMessages, locale, propertyType, propertyAnnotations);
    FieldValidator beanValidator = new BeanFieldValidator(field, validatorFactory, beanValidationGroupSource, clientValidatorSource, formSupport, environment);
    return new CompositeFieldValidator(Arrays.asList(validator, beanValidator));
}
Also used : CompositeFieldValidator(org.apache.tapestry5.internal.services.CompositeFieldValidator) FieldValidator(org.apache.tapestry5.FieldValidator) CompositeFieldValidator(org.apache.tapestry5.internal.services.CompositeFieldValidator)

Example 19 with Validator

use of org.apache.tapestry5.Validator in project cdmlib by cybertaxonomy.

the class SimpleMarshalTest method testMarshalEmptyDocument.

/**
 * Check that we can marshal an empty document
 * @throws JAXBException
 * @throws IOException
 */
@Test
public void testMarshalEmptyDocument() throws JAXBException, IOException {
    DataSet dataSet = new DataSet();
    StringWriter writer = new StringWriter();
    cdmDocumentBuilder.marshal(dataSet, writer);
    SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    schemaFactory.setResourceResolver(new CdmResourceResolver());
    Validator validator = new Validator(schemaFactory);
    for (String schemaName : CdmDocumentBuilder.CDM_SCHEMA_FILES) {
        validator.addSchemaSource(new StreamSource(this.getClass().getResourceAsStream(schemaName)));
    }
    StreamSource streamSource = new StreamSource(new StringReader(writer.toString()));
// assertTrue("CdmDocumentBuilder.marshal should produce valid xml",validator.isInstanceValid(streamSource));
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) StringWriter(java.io.StringWriter) StreamSource(javax.xml.transform.stream.StreamSource) StringReader(java.io.StringReader) Validator(org.custommonkey.xmlunit.jaxp13.Validator) Test(org.junit.Test)

Example 20 with Validator

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

the class FieldValidatorDefaultSourceImpl method createDefaultValidator.

public FieldValidator createDefaultValidator(Field field, String overrideId, Messages overrideMessages, Locale locale, Class propertyType, AnnotationProvider propertyAnnotations) {
    List<FieldValidator> validators = newList();
    for (String constraint : validationConstraintGenerator.buildConstraints(propertyType, propertyAnnotations)) {
        int equalsx = constraint.indexOf('=');
        String validatorType = equalsx > 0 ? constraint.substring(0, equalsx) : constraint;
        String constraintValue = equalsx > 0 ? constraint.substring(equalsx + 1) : null;
        FieldValidator validator = fieldValidatorSource.createValidator(field, validatorType, constraintValue, overrideId, overrideMessages, locale);
        validators.add(validator);
    }
    return validators.size() == 1 ? validators.get(0) : new CompositeFieldValidator(validators);
}
Also used : FieldValidator(org.apache.tapestry5.FieldValidator)

Aggregations

Test (org.testng.annotations.Test)32 MessageFormatter (org.apache.tapestry5.commons.MessageFormatter)26 FieldValidator (org.apache.tapestry5.FieldValidator)21 Field (org.apache.tapestry5.Field)16 Validator (org.apache.tapestry5.Validator)16 ValidatorMacro (org.apache.tapestry5.validator.ValidatorMacro)13 ComponentResources (org.apache.tapestry5.ComponentResources)12 FieldValidatorSource (org.apache.tapestry5.services.FieldValidatorSource)12 FormSupport (org.apache.tapestry5.services.FormSupport)12 Messages (org.apache.tapestry5.commons.Messages)11 TypeCoercer (org.apache.tapestry5.commons.services.TypeCoercer)11 ValidationException (org.apache.tapestry5.ValidationException)7 Pattern (java.util.regex.Pattern)2 Validator (javax.validation.Validator)2 PropertyDescriptor (javax.validation.metadata.PropertyDescriptor)2 Request (org.apache.tapestry5.http.services.Request)2 BeanValidationContext (org.apache.tapestry5.internal.BeanValidationContext)2 Html5Support (org.apache.tapestry5.services.Html5Support)2 StringReader (java.io.StringReader)1 StringWriter (java.io.StringWriter)1