Search in sources :

Example 51 with TypeCoercer

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

the class HibernateEntityValueEncoderTest method to_client_id_null.

@Test
public void to_client_id_null() {
    Session session = mockSession();
    Logger logger = mockLogger();
    replay();
    SampleEntity entity = new SampleEntity();
    HibernateEntityValueEncoder<SampleEntity> encoder = new HibernateEntityValueEncoder<SampleEntity>(SampleEntity.class, "id", session, access, typeCoercer, logger);
    assertNull(encoder.toClient(entity));
    verify();
}
Also used : HibernateEntityValueEncoder(org.apache.tapestry5.hibernate.web.internal.HibernateEntityValueEncoder) Logger(org.slf4j.Logger) Session(org.hibernate.Session) Test(org.testng.annotations.Test)

Example 52 with TypeCoercer

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

the class HibernateEntityValueEncoderTest method to_value_not_found.

@Test
public void to_value_not_found() {
    Session session = mockSession();
    Logger logger = mockLogger();
    expect(session.get(SampleEntity.class, new Long(12345))).andReturn(null);
    logger.error("Unable to convert client value '12345' into an entity instance.");
    replay();
    SampleEntity entity = new SampleEntity();
    HibernateEntityValueEncoder<SampleEntity> encoder = new HibernateEntityValueEncoder<SampleEntity>(SampleEntity.class, "id", session, access, typeCoercer, logger);
    assertNull(encoder.toValue("12345"));
    verify();
}
Also used : HibernateEntityValueEncoder(org.apache.tapestry5.hibernate.web.internal.HibernateEntityValueEncoder) Logger(org.slf4j.Logger) Session(org.hibernate.Session) Test(org.testng.annotations.Test)

Example 53 with TypeCoercer

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

the class HibernateEntityValueEncoderTest method to_value_bad_type_coercion.

@Test
public void to_value_bad_type_coercion() {
    Session session = mockSession();
    Logger logger = mockLogger();
    replay();
    HibernateEntityValueEncoder<SampleEntity> encoder = new HibernateEntityValueEncoder<SampleEntity>(SampleEntity.class, "id", session, access, typeCoercer, logger);
    try {
        encoder.toValue("xyz");
        unreachable();
    } catch (RuntimeException ex) {
        assertMessageContains(ex, "Exception converting 'xyz' to instance of java.lang.Long (id type for entity org.apache.tapestry5.internal.hibernate.SampleEntity)");
    }
    assertNull(encoder.toValue(""));
    verify();
}
Also used : HibernateEntityValueEncoder(org.apache.tapestry5.hibernate.web.internal.HibernateEntityValueEncoder) Logger(org.slf4j.Logger) Session(org.hibernate.Session) Test(org.testng.annotations.Test)

Example 54 with TypeCoercer

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

the class JpaModule method provideValueEncoders.

@Contribute(ValueEncoderSource.class)
public static void provideValueEncoders(final MappedConfiguration<Class, ValueEncoderFactory> configuration, @Symbol(JpaSymbols.PROVIDE_ENTITY_VALUE_ENCODERS) final boolean provideEncoders, final EntityManagerSource entityManagerSource, final EntityManagerManager entityManagerManager, final TypeCoercer typeCoercer, final PropertyAccess propertyAccess, final LoggerSource loggerSource) {
    if (!provideEncoders)
        return;
    for (final PersistenceUnitInfo info : entityManagerSource.getPersistenceUnitInfos()) {
        final EntityManagerFactory emf = entityManagerSource.getEntityManagerFactory(info.getPersistenceUnitName());
        final Metamodel metamodel = emf.getMetamodel();
        for (final EntityType<?> entity : metamodel.getEntities()) {
            final Class<?> javaType = entity.getJavaType();
            final ValueEncoderFactory factory = new ValueEncoderFactory() {

                @Override
                public ValueEncoder create(final Class type) {
                    return new JpaValueEncoder(entity, entityManagerManager, info.getPersistenceUnitName(), propertyAccess, typeCoercer, loggerSource.getLogger(javaType));
                }
            };
            configuration.add(javaType, factory);
        }
    }
}
Also used : JpaValueEncoder(org.apache.tapestry5.internal.jpa.JpaValueEncoder) EntityManagerFactory(javax.persistence.EntityManagerFactory) PersistenceUnitInfo(javax.persistence.spi.PersistenceUnitInfo) Metamodel(javax.persistence.metamodel.Metamodel) ValueEncoderFactory(org.apache.tapestry5.services.ValueEncoderFactory) Contribute(org.apache.tapestry5.ioc.annotations.Contribute)

Example 55 with TypeCoercer

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

the class ExpansionPageElementImplTest method variant_binding_is_not_cached.

@Test
public void variant_binding_is_not_cached() {
    Binding binding = mockBinding();
    TypeCoercer coercer = mockTypeCoercer();
    MarkupWriter writer = mockMarkupWriter();
    RenderQueue queue = mockRenderQueue();
    Object value = new Object();
    train_isInvariant(binding, false);
    replay();
    RenderCommand element = new ExpansionPageElement(binding, coercer);
    verify();
    train_get(binding, value);
    train_coerce(coercer, value, String.class, "STRING-VALUE");
    writer.write("STRING-VALUE");
    replay();
    element.render(writer, queue);
    verify();
    train_get(binding, value);
    train_coerce(coercer, value, String.class, "STRING-VALUE2");
    writer.write("STRING-VALUE2");
    replay();
    element.render(writer, queue);
    verify();
}
Also used : Binding(org.apache.tapestry5.Binding) RenderCommand(org.apache.tapestry5.runtime.RenderCommand) TypeCoercer(org.apache.tapestry5.commons.services.TypeCoercer) RenderQueue(org.apache.tapestry5.runtime.RenderQueue) MarkupWriter(org.apache.tapestry5.MarkupWriter) Test(org.testng.annotations.Test)

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