Search in sources :

Example 46 with TypeCoercer

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

the class TapestryInternalUtilsTest method type_coersion_from_component_resources_aware_to_component_resources.

@Test
public void type_coersion_from_component_resources_aware_to_component_resources() {
    ComponentResourcesAware input = newMock(ComponentResourcesAware.class);
    ComponentResources resources = mockComponentResources();
    expect(input.getComponentResources()).andReturn(resources);
    TypeCoercer coercer = getObject(TypeCoercer.class, null);
    replay();
    ComponentResources actual = coercer.coerce(input, ComponentResources.class);
    assertSame(actual, resources);
    verify();
}
Also used : ComponentResourcesAware(org.apache.tapestry5.runtime.ComponentResourcesAware) TypeCoercer(org.apache.tapestry5.commons.services.TypeCoercer) Test(org.testng.annotations.Test)

Example 47 with TypeCoercer

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

the class TapestryInternalUtilsTest method type_coersion_string_to_pattern.

@Test
public void type_coersion_string_to_pattern() {
    TypeCoercer coercer = getObject(TypeCoercer.class, null);
    String input = "\\s+";
    Pattern pattern = coercer.coerce(input, Pattern.class);
    assertEquals(pattern.toString(), input);
}
Also used : Pattern(java.util.regex.Pattern) TypeCoercer(org.apache.tapestry5.commons.services.TypeCoercer) Test(org.testng.annotations.Test)

Example 48 with TypeCoercer

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

the class TapestryModule method contributeApplicationInitializer.

/**
 * Adds a listener to the {@link org.apache.tapestry5.internal.services.ComponentInstantiatorSource} that clears the
 * {@link PropertyAccess} and {@link TypeCoercer} caches on
 * a class loader invalidation. In addition, forces the
 * realization of {@link ComponentClassResolver} at startup.
 */
public void contributeApplicationInitializer(OrderedConfiguration<ApplicationInitializerFilter> configuration, final TypeCoercer typeCoercer, final ComponentClassResolver componentClassResolver, @ComponentClasses final InvalidationEventHub invalidationEventHub, @Autobuild final RestoreDirtySessionObjects restoreDirtySessionObjects) {
    final Runnable callback = new Runnable() {

        public void run() {
            propertyAccess.clearCache();
            typeCoercer.clearCache();
        }
    };
    ApplicationInitializerFilter clearCaches = new ApplicationInitializerFilter() {

        public void initializeApplication(Context context, ApplicationInitializer initializer) {
            // Snuck in here is the logic to clear the PropertyAccess
            // service's cache whenever
            // the component class loader is invalidated.
            invalidationEventHub.addInvalidationCallback(callback);
            endOfRequestEventHub.addEndOfRequestListener(restoreDirtySessionObjects);
            // Perform other pending initialization
            initializer.initializeApplication(context);
            // We don't care about the result, but this forces a load of the
            // service
            // at application startup, rather than on first request.
            componentClassResolver.isPageName("ForceLoadAtStartup");
        }
    };
    configuration.add("ClearCachesOnInvalidation", clearCaches);
}
Also used : PageActivationContext(org.apache.tapestry5.annotations.PageActivationContext) EventContext(org.apache.tapestry5.EventContext) Context(org.apache.tapestry5.http.services.Context) ApplicationInitializer(org.apache.tapestry5.http.services.ApplicationInitializer) ApplicationInitializerFilter(org.apache.tapestry5.http.services.ApplicationInitializerFilter)

Example 49 with TypeCoercer

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

the class SelectTest method checkSubmittedOption.

/**
 * Utility for testing the "secure" option with various values and model
 * states. This avoids a lot of redundant test setup code.
 *
 * @param withModel whether there should be a model to test against
 * @param secureOption which "secure" option to test
 * @param expectedError the expected error message, nor null if no error
 * @throws ValidationException
 */
private void checkSubmittedOption(boolean withModel, SecureOption secureOption, String expectedError) throws ValidationException {
    ValueEncoder<Platform> encoder = getService(ValueEncoderSource.class).getValueEncoder(Platform.class);
    ValidationTracker tracker = mockValidationTracker();
    Request request = mockRequest();
    Messages messages = mockMessages();
    FieldValidationSupport fvs = mockFieldValidationSupport();
    TypeCoercer typeCoercer = mockTypeCoercer();
    InternalComponentResources resources = mockInternalComponentResources();
    Binding selectModelBinding = mockBinding();
    expect(request.getParameter("xyz")).andReturn("MAC");
    expect(messages.contains(EasyMock.anyObject(String.class))).andReturn(false).anyTimes();
    expect(resources.getBinding("model")).andReturn(selectModelBinding);
    final Holder<SelectModel> modelHolder = Holder.create();
    expect(typeCoercer.coerce(EasyMock.or(EasyMock.isA(SelectModel.class), EasyMock.isNull()), EasyMock.eq(SelectModel.class))).andAnswer(new IAnswer<SelectModel>() {

        @Override
        public SelectModel answer() throws Throwable {
            return modelHolder.get();
        }
    });
    expect(selectModelBinding.get()).andAnswer(new IAnswer<SelectModel>() {

        @Override
        public SelectModel answer() throws Throwable {
            return modelHolder.get();
        }
    });
    Select select = new Select();
    tracker.recordInput(select, "MAC");
    // when not failing we will expect to call the fvs.validate method
    if (expectedError == null) {
        fvs.validate(Platform.MAC, resources, null);
    } else {
        tracker.recordError(EasyMock.eq(select), EasyMock.contains(expectedError));
    }
    replay();
    if (withModel) {
        modelHolder.put(new EnumSelectModel(Platform.class, messages));
    }
    set(select, "encoder", encoder);
    set(select, "model", modelHolder.get());
    set(select, "request", request);
    set(select, "secure", secureOption);
    // Disable BeanValidationContextSupport
    set(select, "beanValidationDisabled", true);
    set(select, "tracker", tracker);
    set(select, "fieldValidationSupport", fvs);
    set(select, "typeCoercer", typeCoercer);
    set(select, "resources", resources);
    select.processSubmission("xyz");
    if (expectedError == null) {
        assertEquals(get(select, "value"), Platform.MAC);
    }
    verify();
}
Also used : Messages(org.apache.tapestry5.commons.Messages) Platform(org.apache.tapestry5.corelib.components.SelectTest.Platform) InternalComponentResources(org.apache.tapestry5.internal.InternalComponentResources) TypeCoercer(org.apache.tapestry5.commons.services.TypeCoercer) Request(org.apache.tapestry5.http.services.Request) ValueEncoderSource(org.apache.tapestry5.services.ValueEncoderSource) EnumSelectModel(org.apache.tapestry5.util.EnumSelectModel) EnumSelectModel(org.apache.tapestry5.util.EnumSelectModel)

Example 50 with TypeCoercer

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

the class HibernateModule method contributeValueEncoderSource.

/**
 * Contributes {@link ValueEncoderFactory}s for all registered Hibernate entity classes. Encoding and decoding are
 * based on the id property value of the entity using type coercion. Hence, if the id can be coerced to a String and
 * back then the entity can be coerced.
 */
@SuppressWarnings("unchecked")
public static void contributeValueEncoderSource(MappedConfiguration<Class, ValueEncoderFactory> configuration, @Symbol(HibernateSymbols.PROVIDE_ENTITY_VALUE_ENCODERS) boolean provideEncoders, final HibernateSessionSource sessionSource, final Session session, final TypeCoercer typeCoercer, final PropertyAccess propertyAccess, final LoggerSource loggerSource) {
    if (!provideEncoders)
        return;
    Set<EntityType<?>> entities = sessionSource.getSessionFactory().getMetamodel().getEntities();
    for (EntityType<?> entityType : entities) {
        Class<?> entityClass = entityType.getJavaType();
        if (entityClass != null) {
            SingularAttribute<?, ?> id = entityType.getId(entityType.getIdType().getJavaType());
            final String idenfierPropertyName = id.getName();
            ValueEncoderFactory factory = new ValueEncoderFactory() {

                @Override
                public ValueEncoder create(Class type) {
                    return new HibernateEntityValueEncoder(entityClass, idenfierPropertyName, session, propertyAccess, typeCoercer, loggerSource.getLogger(entityClass));
                }
            };
            configuration.add(entityClass, factory);
        }
    }
}
Also used : EntityType(javax.persistence.metamodel.EntityType) HibernateEntityValueEncoder(org.apache.tapestry5.hibernate.web.internal.HibernateEntityValueEncoder) ValueEncoderFactory(org.apache.tapestry5.services.ValueEncoderFactory)

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