use of org.apache.tapestry5.services.FieldValidatorDefaultSource in project tapestry-5 by apache.
the class FieldValidatorDefaultSourceImplTest method invokes_all_constraint_generators.
@SuppressWarnings("unchecked")
@Test
public void invokes_all_constraint_generators() throws Exception {
getMocksControl().checkOrder(true);
ValidationConstraintGenerator gen = mockValidationConstraintGenerator();
FieldValidator fv1 = mockFieldValidator();
FieldValidator fv2 = mockFieldValidator();
FieldValidatorSource source = mockFieldValidatorSource();
Class propertyType = Integer.class;
AnnotationProvider provider = mockAnnotationProvider();
String overrideId = "overrideId";
Messages overrideMessages = mockMessages();
Field field = mockField();
Locale locale = Locale.ENGLISH;
String value = "*VALUE*";
train_buildConstraints(gen, propertyType, provider, "cons1", "cons2");
train_createValidator(source, field, "cons1", null, overrideId, overrideMessages, locale, fv1);
train_createValidator(source, field, "cons2", null, overrideId, overrideMessages, locale, fv2);
fv1.validate(value);
fv2.validate(value);
replay();
FieldValidatorDefaultSource fieldValidatorSource = new FieldValidatorDefaultSourceImpl(gen, source);
FieldValidator composite = fieldValidatorSource.createDefaultValidator(field, overrideId, overrideMessages, locale, propertyType, provider);
composite.validate(value);
verify();
}
use of org.apache.tapestry5.services.FieldValidatorDefaultSource in project tapestry-5 by apache.
the class FieldValidatorDefaultSourceImplTest method validator_with_constraint.
@SuppressWarnings("unchecked")
@Test
public void validator_with_constraint() throws Exception {
ValidationConstraintGenerator gen = mockValidationConstraintGenerator();
FieldValidator fv = mockFieldValidator();
FieldValidatorSource source = mockFieldValidatorSource();
Class propertyType = Integer.class;
AnnotationProvider provider = mockAnnotationProvider();
String overrideId = "overrideId";
Messages overrideMessages = mockMessages();
Field field = mockField();
Locale locale = Locale.ENGLISH;
train_buildConstraints(gen, propertyType, provider, "foo=bar");
train_createValidator(source, field, "foo", "bar", overrideId, overrideMessages, locale, fv);
replay();
FieldValidatorDefaultSource fieldValidatorSource = new FieldValidatorDefaultSourceImpl(gen, source);
FieldValidator composite = fieldValidatorSource.createDefaultValidator(field, overrideId, overrideMessages, locale, propertyType, provider);
assertSame(composite, fv);
verify();
}
use of org.apache.tapestry5.services.FieldValidatorDefaultSource in project tapestry-5 by apache.
the class FieldValidatorDefaultSourceImplTest method no_validators_at_all.
@SuppressWarnings("unchecked")
@Test
public void no_validators_at_all() throws Exception {
ValidationConstraintGenerator gen = mockValidationConstraintGenerator();
FieldValidatorSource source = mockFieldValidatorSource();
Class propertyType = Integer.class;
AnnotationProvider provider = mockAnnotationProvider();
String overrideId = "overrideId";
Messages overrideMessages = mockMessages();
Field field = mockField();
Locale locale = Locale.ENGLISH;
String value = "*VALUE*";
train_buildConstraints(gen, propertyType, provider);
replay();
FieldValidatorDefaultSource fieldValidatorSource = new FieldValidatorDefaultSourceImpl(gen, source);
FieldValidator composite = fieldValidatorSource.createDefaultValidator(field, overrideId, overrideMessages, locale, propertyType, provider);
composite.validate(value);
verify();
}
use of org.apache.tapestry5.services.FieldValidatorDefaultSource in project tapestry-5 by apache.
the class TapestryModule method decorateFieldValidatorDefaultSource.
/**
* Decorate FieldValidatorDefaultSource to setup the EnvironmentMessages
* object and place it in the environment.
* Although this could have been implemented directly in the default
* implementation of the service, doing it
* as service decoration ensures that the environment will be properly setup
* even if a user overrides the default
* service implementation.
*
* @param defaultSource
* The service to decorate
* @param environment
*/
public static FieldValidatorDefaultSource decorateFieldValidatorDefaultSource(final FieldValidatorDefaultSource defaultSource, final Environment environment) {
return new FieldValidatorDefaultSource() {
public FieldValidator createDefaultValidator(Field field, String overrideId, Messages overrideMessages, Locale locale, Class propertyType, AnnotationProvider propertyAnnotations) {
environment.push(EnvironmentMessages.class, new EnvironmentMessages(overrideMessages, overrideId));
FieldValidator fieldValidator = defaultSource.createDefaultValidator(field, overrideId, overrideMessages, locale, propertyType, propertyAnnotations);
environment.pop(EnvironmentMessages.class);
return fieldValidator;
}
public FieldValidator createDefaultValidator(ComponentResources resources, String parameterName) {
EnvironmentMessages em = new EnvironmentMessages(resources.getContainerMessages(), resources.getId());
environment.push(EnvironmentMessages.class, em);
FieldValidator fieldValidator = defaultSource.createDefaultValidator(resources, parameterName);
environment.pop(EnvironmentMessages.class);
return fieldValidator;
}
};
}
Aggregations