use of org.apache.tapestry5.commons.services.TypeCoercer in project tapestry-5 by apache.
the class LinkSourceImpl method createComponentEventLink.
public Link createComponentEventLink(Page page, String nestedId, String eventType, boolean forForm, Object... eventContext) {
assert page != null;
assert InternalUtils.isNonBlank(eventType);
Page activePage = pageRenderQueue.getRenderingPage();
// See TAPESTRY-2184
if (activePage == null)
activePage = page;
String activePageName = activePage.getName();
Object[] pageActivationContext = contextCollector.collectPageActivationContext(activePageName);
ComponentEventRequestParameters parameters = new ComponentEventRequestParameters(activePageName, page.getName(), toBlank(nestedId), eventType, new ArrayEventContext(typeCoercer, pageActivationContext), new ArrayEventContext(typeCoercer, eventContext));
Link link = linkEncoder.createComponentEventLink(parameters, forForm);
for (LinkCreationListener2 listener : listeners) listener.createdComponentEventLink(link, parameters);
return link;
}
use of org.apache.tapestry5.commons.services.TypeCoercer in project tapestry-5 by apache.
the class BeanModelSourceBuilder method createTypeCoercer.
private void createTypeCoercer() {
CoercionTupleConfiguration configuration = new CoercionTupleConfiguration();
BasicTypeCoercions.provideBasicTypeCoercions(configuration);
BasicTypeCoercions.provideJSR310TypeCoercions(configuration);
typeCoercer = new TypeCoercerImpl(configuration.getTuples());
}
use of org.apache.tapestry5.commons.services.TypeCoercer in project tapestry-5 by apache.
the class SelectTest method submitted_option_matches_against_value_encoded_option_model_value.
/**
* This a test for TAP5-2184
*/
@Test
public void submitted_option_matches_against_value_encoded_option_model_value() throws ValidationException {
ValueEncoder<Integer> encoder = getService(ValueEncoderSource.class).getValueEncoder(Integer.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("5");
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, "5");
fvs.validate(5, resources, null);
replay();
// TAP5-2184 is triggered by the automatic String->SelectModel coercion, because the OptionModel
// values are Strings even if the desired property type is not (Integer, here). Select has a little
// hack to run the model values through the ValueEncoder for comparison.
modelHolder.put(getService(TypeCoercer.class).coerce("1,5,10,20", SelectModel.class));
set(select, "encoder", encoder);
set(select, "model", modelHolder.get());
set(select, "request", request);
set(select, "secure", SecureOption.ALWAYS);
// 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");
verify();
assertEquals(get(select, "value"), 5);
}
use of org.apache.tapestry5.commons.services.TypeCoercer in project tapestry-5 by apache.
the class SelectTest method context_that_needs_to_be_encoded.
@Test
public void context_that_needs_to_be_encoded() throws Exception {
ValueEncoderSource valueEncoderSource = mockValueEncoderSource();
TypeCoercer typeCoercer = getService(TypeCoercer.class);
ContextValueEncoder contextValueEncoder = new ContextValueEncoderImpl(valueEncoderSource);
ValueEncoder<Platform> platformEncoder = new ValueEncoder<SelectTest.Platform>() {
@Override
public Platform toValue(String clientValue) {
return Platform.valueOf(clientValue.substring(10));
}
@Override
public String toClient(Platform value) {
return "Platform: " + value.name();
}
};
InternalComponentResources resources = mockInternalComponentResources();
expect(valueEncoderSource.getValueEncoder(Platform.class)).andReturn(platformEncoder).anyTimes();
expect(valueEncoderSource.getValueEncoder(String.class)).andReturn(new StringValueEncoder()).anyTimes();
expect(resources.triggerContextEvent(EasyMock.eq(EventConstants.VALUE_CHANGED), eqEventContext(null, Platform.LINUX), EasyMock.isA(ComponentEventCallback.class))).andReturn(true);
Select select = new Select();
set(select, "resources", resources);
set(select, "encoder", new StringValueEncoder());
set(select, "typeCoercer", typeCoercer);
replay();
select.onChange(new URLEventContext(contextValueEncoder, new String[] { platformEncoder.toClient(Platform.LINUX) }), null);
verify();
}
use of org.apache.tapestry5.commons.services.TypeCoercer in project tapestry-5 by apache.
the class PageElementFactoryImplTest method unclosed_attribute_expression.
@Test
public void unclosed_attribute_expression() {
TypeCoercer typeCoercer = mockTypeCoercer();
BindingSource bindingSource = mockBindingSource();
ComponentResources resources = mockComponentResources();
Location location = mockLocation();
AttributeToken token = new AttributeToken(null, "fred", "${flintstone", location);
replay();
PageElementFactory factory = new PageElementFactoryImpl(typeCoercer, bindingSource);
try {
factory.newAttributeElement(resources, token);
unreachable();
} catch (TapestryException ex) {
assertEquals(ex.getMessage(), "Attribute expression \'${flintstone\' is missing a closing brace.");
assertSame(ex.getLocation(), location);
}
verify();
}
Aggregations