use of org.apache.tapestry5.ValueEncoder 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.ValueEncoder 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.ValueEncoder in project tapestry-5 by apache.
the class ActivationRequestParameterWorker method decorateLinks.
@SuppressWarnings("all")
private static void decorateLinks(TransformationSupport support, String fieldName, final FieldHandle handle, final String parameterName, final ValueEncoder encoder, final URLEncoder urlEncoder) {
ComponentEventHandler handler = new ComponentEventHandler() {
public void handleEvent(Component instance, ComponentEvent event) {
Object value = handle.get(instance);
if (value == null) {
return;
}
Link link = event.getEventContext().get(Link.class, 0);
String clientValue = encoder.toClient(value);
// TAP5-1768: escape special characters
clientValue = urlEncoder.encode(clientValue);
link.addParameter(parameterName, clientValue);
}
};
support.addEventHandler(EventConstants.DECORATE_COMPONENT_EVENT_LINK, 0, String.format("ActivationRequestParameterWorker decorate component event link event handler for field %s as query parameter '%s'", fieldName, parameterName), handler);
support.addEventHandler(EventConstants.DECORATE_PAGE_RENDER_LINK, 0, String.format("ActivationRequestParameterWorker decorate page render link event handler for field %s as query parameter '%s'", fieldName, parameterName), handler);
}
use of org.apache.tapestry5.ValueEncoder in project tapestry-5 by apache.
the class ActivationRequestParameterWorker method setValueFromInitializeEventHandler.
@SuppressWarnings("all")
private void setValueFromInitializeEventHandler(final TransformationSupport support, final String fieldName, final boolean required, final FieldHandle handle, final String parameterName, final ValueEncoder encoder, final URLEncoder urlEncoder) {
ComponentEventHandler handler = new ComponentEventHandler() {
public void handleEvent(Component instance, ComponentEvent event) {
String clientValue = request.getParameter(parameterName);
if (clientValue == null) {
if (required) {
throw new TapestryException(String.format("Activation request parameter field %s is marked as required, but query parameter '%s' is null.", fieldName, parameterName), null);
}
return;
}
// TAP5-1768: unescape encoded value
clientValue = urlEncoder.decode(clientValue);
Object value = encoder.toValue(clientValue);
handle.set(instance, value);
}
};
support.addEventHandler(EventConstants.ACTIVATE, 0, String.format("Restoring field %s from query parameter '%s'", fieldName, parameterName), handler);
}
use of org.apache.tapestry5.ValueEncoder in project tapestry-5 by apache.
the class Palette method processSubmission.
@Override
protected void processSubmission(String controlName) {
String parameterValue = request.getParameter(controlName);
JSONArray values = new JSONArray(parameterValue);
// Use a couple of local variables to cut down on access via bindings
Collection<Object> selected = this.selected;
selected.clear();
ValueEncoder encoder = this.encoder;
// TODO: Validation error if the model does not contain a value.
int count = values.length();
for (int i = 0; i < count; i++) {
String value = values.getString(i);
Object objectValue = encoder.toValue(value);
selected.add(objectValue);
}
putPropertyNameIntoBeanValidationContext("selected");
try {
fieldValidationSupport.validate(selected, resources, validate);
this.selected = selected;
} catch (final ValidationException e) {
validationTracker.recordError(this, e.getMessage());
}
removePropertyNameFromBeanValidationContext();
}
Aggregations