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();
}
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);
}
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);
}
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();
}
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);
}
}
}
Aggregations