Search in sources :

Example 1 with ValueEncoder

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);
}
Also used : Messages(org.apache.tapestry5.commons.Messages) 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) Test(org.testng.annotations.Test)

Example 2 with ValueEncoder

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();
}
Also used : Platform(org.apache.tapestry5.corelib.components.SelectTest.Platform) InternalComponentResources(org.apache.tapestry5.internal.InternalComponentResources) TypeCoercer(org.apache.tapestry5.commons.services.TypeCoercer) StringValueEncoder(org.apache.tapestry5.internal.services.StringValueEncoder) ValueEncoderSource(org.apache.tapestry5.services.ValueEncoderSource) EnumValueEncoder(org.apache.tapestry5.util.EnumValueEncoder) ContextValueEncoder(org.apache.tapestry5.services.ContextValueEncoder) StringValueEncoder(org.apache.tapestry5.internal.services.StringValueEncoder) URLEventContext(org.apache.tapestry5.internal.URLEventContext) ContextValueEncoder(org.apache.tapestry5.services.ContextValueEncoder) ContextValueEncoderImpl(org.apache.tapestry5.internal.services.ContextValueEncoderImpl) Test(org.testng.annotations.Test)

Example 3 with ValueEncoder

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);
}
Also used : ComponentEventHandler(org.apache.tapestry5.services.ComponentEventHandler) ComponentEvent(org.apache.tapestry5.runtime.ComponentEvent) Component(org.apache.tapestry5.runtime.Component) Link(org.apache.tapestry5.http.Link)

Example 4 with ValueEncoder

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);
}
Also used : ComponentEventHandler(org.apache.tapestry5.services.ComponentEventHandler) ComponentEvent(org.apache.tapestry5.runtime.ComponentEvent) Component(org.apache.tapestry5.runtime.Component) TapestryException(org.apache.tapestry5.commons.internal.util.TapestryException)

Example 5 with ValueEncoder

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();
}
Also used : JSONArray(org.apache.tapestry5.json.JSONArray)

Aggregations

ValueEncoder (org.apache.tapestry5.ValueEncoder)7 ValueEncoderSource (org.apache.tapestry5.services.ValueEncoderSource)5 ContextValueEncoder (org.apache.tapestry5.services.ContextValueEncoder)4 ValueEncoderFactory (org.apache.tapestry5.services.ValueEncoderFactory)4 TypeCoercer (org.apache.tapestry5.commons.services.TypeCoercer)3 InternalComponentResources (org.apache.tapestry5.internal.InternalComponentResources)3 Test (org.testng.annotations.Test)3 Messages (org.apache.tapestry5.commons.Messages)2 TapestryException (org.apache.tapestry5.commons.internal.util.TapestryException)2 Platform (org.apache.tapestry5.corelib.components.SelectTest.Platform)2 Request (org.apache.tapestry5.http.services.Request)2 PlasticClass (org.apache.tapestry5.plastic.PlasticClass)2 Component (org.apache.tapestry5.runtime.Component)2 ComponentEvent (org.apache.tapestry5.runtime.ComponentEvent)2 ComponentEventHandler (org.apache.tapestry5.services.ComponentEventHandler)2 DateFormat (java.text.DateFormat)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Calendar (java.util.Calendar)1 Collection (java.util.Collection)1 Date (java.util.Date)1