Search in sources :

Example 1 with FieldValidatorDefaultSource

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();
}
Also used : ValidationConstraintGenerator(org.apache.tapestry5.services.ValidationConstraintGenerator) Locale(java.util.Locale) FieldValidatorSource(org.apache.tapestry5.services.FieldValidatorSource) Field(org.apache.tapestry5.Field) Messages(org.apache.tapestry5.commons.Messages) AnnotationProvider(org.apache.tapestry5.commons.AnnotationProvider) FieldValidator(org.apache.tapestry5.FieldValidator) FieldValidatorDefaultSource(org.apache.tapestry5.services.FieldValidatorDefaultSource) Test(org.testng.annotations.Test)

Example 2 with FieldValidatorDefaultSource

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();
}
Also used : ValidationConstraintGenerator(org.apache.tapestry5.services.ValidationConstraintGenerator) Locale(java.util.Locale) FieldValidatorSource(org.apache.tapestry5.services.FieldValidatorSource) Field(org.apache.tapestry5.Field) Messages(org.apache.tapestry5.commons.Messages) AnnotationProvider(org.apache.tapestry5.commons.AnnotationProvider) FieldValidator(org.apache.tapestry5.FieldValidator) FieldValidatorDefaultSource(org.apache.tapestry5.services.FieldValidatorDefaultSource) Test(org.testng.annotations.Test)

Example 3 with FieldValidatorDefaultSource

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();
}
Also used : ValidationConstraintGenerator(org.apache.tapestry5.services.ValidationConstraintGenerator) Locale(java.util.Locale) FieldValidatorSource(org.apache.tapestry5.services.FieldValidatorSource) Field(org.apache.tapestry5.Field) Messages(org.apache.tapestry5.commons.Messages) AnnotationProvider(org.apache.tapestry5.commons.AnnotationProvider) FieldValidatorDefaultSource(org.apache.tapestry5.services.FieldValidatorDefaultSource) FieldValidator(org.apache.tapestry5.FieldValidator) Test(org.testng.annotations.Test)

Example 4 with FieldValidatorDefaultSource

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;
        }
    };
}
Also used : PersistentLocale(org.apache.tapestry5.services.PersistentLocale) Locale(java.util.Locale) ThreadLocale(org.apache.tapestry5.ioc.services.ThreadLocale) Field(org.apache.tapestry5.Field) ComponentMessages(org.apache.tapestry5.services.ComponentMessages) EnvironmentMessages(org.apache.tapestry5.internal.beaneditor.EnvironmentMessages) Messages(org.apache.tapestry5.commons.Messages) AnnotationProvider(org.apache.tapestry5.commons.AnnotationProvider) FieldValidatorDefaultSource(org.apache.tapestry5.services.FieldValidatorDefaultSource) FieldValidator(org.apache.tapestry5.FieldValidator) EnvironmentMessages(org.apache.tapestry5.internal.beaneditor.EnvironmentMessages) ComponentResources(org.apache.tapestry5.ComponentResources)

Aggregations

Locale (java.util.Locale)4 Field (org.apache.tapestry5.Field)4 FieldValidator (org.apache.tapestry5.FieldValidator)4 AnnotationProvider (org.apache.tapestry5.commons.AnnotationProvider)4 Messages (org.apache.tapestry5.commons.Messages)4 FieldValidatorDefaultSource (org.apache.tapestry5.services.FieldValidatorDefaultSource)4 FieldValidatorSource (org.apache.tapestry5.services.FieldValidatorSource)3 ValidationConstraintGenerator (org.apache.tapestry5.services.ValidationConstraintGenerator)3 Test (org.testng.annotations.Test)3 ComponentResources (org.apache.tapestry5.ComponentResources)1 EnvironmentMessages (org.apache.tapestry5.internal.beaneditor.EnvironmentMessages)1 ThreadLocale (org.apache.tapestry5.ioc.services.ThreadLocale)1 ComponentMessages (org.apache.tapestry5.services.ComponentMessages)1 PersistentLocale (org.apache.tapestry5.services.PersistentLocale)1