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();
}
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();
}
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));
}
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));
}
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);
}
Aggregations