Search in sources :

Example 21 with TypeCoercer

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

the class MetaDataLocatorImplTest method not_found_by_page_name_but_found_in_configuration.

@Test
public void not_found_by_page_name_but_found_in_configuration() {
    ComponentModel model = mockComponentModel();
    SymbolSource symbolSource = mockSymbolSource();
    ComponentModelSource modelSource = mockComponentModelSource();
    String key = "foo.bar";
    String value = "zaphod";
    String pageName = "gnip/Gnop";
    expect(modelSource.getPageModel(pageName)).andReturn(model);
    train_getMeta(model, key, null);
    train_expandSymbols(symbolSource, value, "*expanded*");
    replay();
    Map<String, String> configuration = CollectionFactory.newMap();
    configuration.put("gnip:foo.bar", value);
    MetaDataLocator locator = new MetaDataLocatorImpl(symbolSource, typeCoercer, modelSource, configuration);
    assertSame(locator.findMeta(key, pageName, String.class), "*expanded*");
    verify();
}
Also used : SymbolSource(org.apache.tapestry5.ioc.services.SymbolSource) ComponentModel(org.apache.tapestry5.model.ComponentModel) MetaDataLocator(org.apache.tapestry5.services.MetaDataLocator) Test(org.testng.annotations.Test)

Example 22 with TypeCoercer

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

the class MetaDataLocatorImplTest method find_by_page_name.

@Test
public void find_by_page_name() {
    ComponentModel model = mockComponentModel();
    SymbolSource symbolSource = mockSymbolSource();
    ComponentModelSource modelSource = mockComponentModelSource();
    String key = "foo.bar";
    String value = "zaphod";
    String pageName = "foo/Bar";
    expect(modelSource.getPageModel(pageName)).andReturn(model);
    train_getMeta(model, key, value);
    train_expandSymbols(symbolSource, value, "*expanded*");
    replay();
    Map<String, String> configuration = Collections.emptyMap();
    MetaDataLocator locator = new MetaDataLocatorImpl(symbolSource, typeCoercer, modelSource, configuration);
    assertSame(locator.findMeta(key, pageName, String.class), "*expanded*");
    verify();
}
Also used : SymbolSource(org.apache.tapestry5.ioc.services.SymbolSource) ComponentModel(org.apache.tapestry5.model.ComponentModel) MetaDataLocator(org.apache.tapestry5.services.MetaDataLocator) Test(org.testng.annotations.Test)

Example 23 with TypeCoercer

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

the class MetaDataLocatorImplTest method default_to_symbol_source.

@Test
public void default_to_symbol_source() {
    ComponentResources resources = mockComponentResources();
    ComponentModel model = mockComponentModel();
    SymbolSource symbolSource = mockSymbolSource();
    ComponentModelSource modelSource = mockComponentModelSource();
    String key = "foo.bar";
    String value = "zaphod";
    String completeId = "foo/Bar:baz";
    train_getCompleteId(resources, completeId);
    train_getComponentModel(resources, model);
    train_getMeta(model, key, null);
    train_getContainerResources(resources, null);
    train_getPageName(resources, "foo/Bar");
    train_valueForSymbol(symbolSource, key, value);
    replay();
    Map<String, String> configuration = Collections.emptyMap();
    MetaDataLocator locator = new MetaDataLocatorImpl(symbolSource, typeCoercer, modelSource, configuration);
    assertSame(locator.findMeta(key, resources, String.class), value);
    verify();
    // And check that it's cached:
    train_getCompleteId(resources, completeId);
    replay();
    assertSame(locator.findMeta(key, resources, String.class), value);
    verify();
}
Also used : SymbolSource(org.apache.tapestry5.ioc.services.SymbolSource) ComponentModel(org.apache.tapestry5.model.ComponentModel) MetaDataLocator(org.apache.tapestry5.services.MetaDataLocator) ComponentResources(org.apache.tapestry5.ComponentResources) Test(org.testng.annotations.Test)

Example 24 with TypeCoercer

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

the class TapestryModule method contributeObjectRenderer.

/**
 * Contributes a default object renderer for type Object, plus specialized
 * renderers for {@link org.apache.tapestry5.http.services.Request}, {@link org.apache.tapestry5.commons.Location},
 * {@link org.apache.tapestry5.ComponentResources}, {@link org.apache.tapestry5.EventContext},
 * {@link AvailableValues},
 * List, and Object[].
 */
@SuppressWarnings("unchecked")
public void contributeObjectRenderer(MappedConfiguration<Class, ObjectRenderer> configuration, @InjectService("LocationRenderer") ObjectRenderer locationRenderer, final TypeCoercer typeCoercer) {
    configuration.add(Object.class, new DefaultObjectRenderer());
    configuration.addInstance(Request.class, RequestRenderer.class);
    configuration.add(Location.class, locationRenderer);
    ObjectRenderer preformatted = new ObjectRenderer<Object>() {

        public void render(Object object, MarkupWriter writer) {
            writer.element("pre");
            writer.write(typeCoercer.coerce(object, String.class));
            writer.end();
        }
    };
    configuration.addInstance(List.class, ListRenderer.class);
    configuration.addInstance(Object[].class, ObjectArrayRenderer.class);
    configuration.addInstance(ComponentResources.class, ComponentResourcesRenderer.class);
    configuration.addInstance(EventContext.class, EventContextRenderer.class);
    configuration.add(AvailableValues.class, new AvailableValuesRenderer());
}
Also used : AvailableValuesRenderer(org.apache.tapestry5.internal.renderers.AvailableValuesRenderer) ObjectRenderer(org.apache.tapestry5.services.ObjectRenderer) DefaultObjectRenderer(org.apache.tapestry5.services.DefaultObjectRenderer) DefaultObjectRenderer(org.apache.tapestry5.services.DefaultObjectRenderer) JSONObject(org.apache.tapestry5.json.JSONObject) MarkupWriter(org.apache.tapestry5.MarkupWriter)

Example 25 with TypeCoercer

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

the class BeanModelSourceImpl method create.

public <T> BeanModel<T> create(Class<T> beanClass, boolean filterReadOnlyProperties, Messages messages) {
    assert beanClass != null;
    assert messages != null;
    ClassPropertyAdapter adapter = propertyAccess.getAdapter(beanClass);
    BeanModel<T> model = new BeanModelImpl<T>(beanClass, propertyConduitSource, typeCoercer, messages, locator);
    for (final String propertyName : adapter.getPropertyNames()) {
        PropertyAdapter pa = adapter.getPropertyAdapter(propertyName);
        if (!pa.isRead()) {
            continue;
        }
        if (isStaticFieldProperty(pa)) {
            continue;
        }
        if (pa.getAnnotation(NonVisual.class) != null) {
            continue;
        }
        if (filterReadOnlyProperties && !pa.isUpdate()) {
            continue;
        }
        final String dataType = dataTypeAnalyzer.identifyDataType(pa);
        if (dataType == null) {
            continue;
        }
        model.add(propertyName).dataType(dataType);
    }
    // First, order the properties based on the location of the getter method
    // within the class.
    List<String> propertyNames = model.getPropertyNames();
    orderProperties(adapter, propertyNames);
    model.reorder(propertyNames.toArray(new String[propertyNames.size()]));
    // Next, check for an annotation with specific ordering information.
    ReorderProperties reorderAnnotation = beanClass.getAnnotation(ReorderProperties.class);
    if (reorderAnnotation != null) {
        BeanModelUtils.reorder(model, reorderAnnotation.value());
    }
    return model;
}
Also used : BeanModelImpl(org.apache.tapestry5.beanmodel.internal.beanmodel.BeanModelImpl) NonVisual(org.apache.tapestry5.beaneditor.NonVisual) ReorderProperties(org.apache.tapestry5.beaneditor.ReorderProperties) ClassPropertyAdapter(org.apache.tapestry5.commons.services.ClassPropertyAdapter) ClassPropertyAdapter(org.apache.tapestry5.commons.services.ClassPropertyAdapter) PropertyAdapter(org.apache.tapestry5.commons.services.PropertyAdapter)

Aggregations

Test (org.testng.annotations.Test)43 TypeCoercer (org.apache.tapestry5.commons.services.TypeCoercer)27 ComponentResources (org.apache.tapestry5.ComponentResources)19 Messages (org.apache.tapestry5.commons.Messages)13 FieldValidator (org.apache.tapestry5.FieldValidator)11 Validator (org.apache.tapestry5.Validator)11 SymbolSource (org.apache.tapestry5.ioc.services.SymbolSource)11 FieldValidatorSource (org.apache.tapestry5.services.FieldValidatorSource)11 ValidatorMacro (org.apache.tapestry5.validator.ValidatorMacro)11 Link (org.apache.tapestry5.http.Link)10 ComponentModel (org.apache.tapestry5.model.ComponentModel)10 FormSupport (org.apache.tapestry5.services.FormSupport)10 MessageFormatter (org.apache.tapestry5.commons.MessageFormatter)9 MetaDataLocator (org.apache.tapestry5.services.MetaDataLocator)9 EventContext (org.apache.tapestry5.EventContext)7 ComponentEventLinkEncoder (org.apache.tapestry5.services.ComponentEventLinkEncoder)7 PageRenderRequestParameters (org.apache.tapestry5.services.PageRenderRequestParameters)7 LinkCreationListener2 (org.apache.tapestry5.services.LinkCreationListener2)6 MarkupWriter (org.apache.tapestry5.MarkupWriter)5 HibernateEntityValueEncoder (org.apache.tapestry5.hibernate.web.internal.HibernateEntityValueEncoder)4