Search in sources :

Example 16 with AnnotationProvider

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

the class ValueObjectProvider method provide.

@Override
public <T> T provide(Class<T> objectType, AnnotationProvider annotationProvider, ObjectLocator locator) {
    Value annotation = annotationProvider.getAnnotation(Value.class);
    if (annotation == null)
        return null;
    String value = annotation.value();
    Object expanded = symbolSource.expandSymbols(value);
    IntermediateType intermediate = annotationProvider.getAnnotation(IntermediateType.class);
    if (intermediate != null)
        expanded = typeCoercer.coerce(expanded, intermediate.value());
    return typeCoercer.coerce(expanded, objectType);
}
Also used : IntermediateType(org.apache.tapestry5.ioc.annotations.IntermediateType) Value(org.apache.tapestry5.ioc.annotations.Value)

Example 17 with AnnotationProvider

use of org.apache.tapestry5.commons.AnnotationProvider 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)

Example 18 with AnnotationProvider

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

the class FieldTranslatorSourceImplTest method create_default_translator_with_per_form_override_message.

@Test
public void create_default_translator_with_per_form_override_message() {
    Field field = mockField();
    Messages messages = mockMessages();
    Locale locale = Locale.ENGLISH;
    Class propertyType = Map.class;
    TranslatorSource ts = mockTranslatorSource();
    FormSupport fs = mockFormSupport();
    Translator translator = mockTranslator("maptrans", Map.class);
    MessageFormatter formatter = mockMessageFormatter();
    MarkupWriter writer = mockMarkupWriter();
    String label = "My Label";
    String message = "Formatted Message";
    AnnotationProvider ap = mockAnnotationProvider(null);
    train_findByType(ts, propertyType, translator);
    train_getFormValidationId(fs, "myform");
    train_contains(messages, "myform-myfield-maptrans-message", true);
    train_getMessageFormatter(messages, "myform-myfield-maptrans-message", formatter);
    train_getLabel(field, label);
    train_format(formatter, message, label);
    translator.render(field, message, writer, fs);
    replay();
    FieldTranslatorSource source = new FieldTranslatorSourceImpl(ts, null, fs);
    FieldTranslator ft = source.createDefaultTranslator(field, "myfield", messages, locale, propertyType, ap);
    assertEquals(ft.getType(), Map.class);
    ft.render(writer);
    verify();
}
Also used : Locale(java.util.Locale) Messages(org.apache.tapestry5.commons.Messages) AnnotationProvider(org.apache.tapestry5.commons.AnnotationProvider) FieldTranslator(org.apache.tapestry5.FieldTranslator) MessageFormatter(org.apache.tapestry5.commons.MessageFormatter) MarkupWriter(org.apache.tapestry5.MarkupWriter) FormSupport(org.apache.tapestry5.services.FormSupport) TranslatorSource(org.apache.tapestry5.services.TranslatorSource) FieldTranslatorSource(org.apache.tapestry5.services.FieldTranslatorSource) Field(org.apache.tapestry5.Field) FieldTranslator(org.apache.tapestry5.FieldTranslator) Translator(org.apache.tapestry5.Translator) FieldTranslatorSource(org.apache.tapestry5.services.FieldTranslatorSource) Map(java.util.Map) Test(org.testng.annotations.Test)

Example 19 with AnnotationProvider

use of org.apache.tapestry5.commons.AnnotationProvider 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 20 with AnnotationProvider

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

the class FieldTranslatorSourceImplTest method create_default_translator_with_annotation.

@Test
public void create_default_translator_with_annotation() {
    TranslatorSource ts = mockTranslatorSource();
    AnnotationProvider ap = mockAnnotationProvider("fred");
    Translator t = mockTranslator();
    expect(ts.get("fred")).andReturn(t);
    replay();
    FieldTranslatorSourceImpl source = new FieldTranslatorSourceImpl(ts, null, null);
    assertSame(source.findTranslator(Map.class, ap), t);
    verify();
}
Also used : TranslatorSource(org.apache.tapestry5.services.TranslatorSource) FieldTranslatorSource(org.apache.tapestry5.services.FieldTranslatorSource) FieldTranslator(org.apache.tapestry5.FieldTranslator) Translator(org.apache.tapestry5.Translator) AnnotationProvider(org.apache.tapestry5.commons.AnnotationProvider) Map(java.util.Map) Test(org.testng.annotations.Test)

Aggregations

AnnotationProvider (org.apache.tapestry5.commons.AnnotationProvider)17 Test (org.testng.annotations.Test)13 Messages (org.apache.tapestry5.commons.Messages)10 Locale (java.util.Locale)9 Field (org.apache.tapestry5.Field)9 Map (java.util.Map)7 FieldValidator (org.apache.tapestry5.FieldValidator)6 FieldTranslatorSource (org.apache.tapestry5.services.FieldTranslatorSource)6 TranslatorSource (org.apache.tapestry5.services.TranslatorSource)6 FieldTranslator (org.apache.tapestry5.FieldTranslator)5 Translator (org.apache.tapestry5.Translator)5 MarkupWriter (org.apache.tapestry5.MarkupWriter)4 MessageFormatter (org.apache.tapestry5.commons.MessageFormatter)4 ObjectLocator (org.apache.tapestry5.commons.ObjectLocator)4 ObjectProvider (org.apache.tapestry5.commons.ObjectProvider)4 FieldValidatorDefaultSource (org.apache.tapestry5.services.FieldValidatorDefaultSource)4 Asset (org.apache.tapestry5.Asset)3 NullAnnotationProvider (org.apache.tapestry5.commons.internal.NullAnnotationProvider)3 MasterObjectProvider (org.apache.tapestry5.ioc.services.MasterObjectProvider)3 FormSupport (org.apache.tapestry5.services.FormSupport)3