Search in sources :

Example 6 with TypeCoercer

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

the class TapestryInternalUtilsTest method string_to_select_model_type_coercion_integration.

@Test
public void string_to_select_model_type_coercion_integration() {
    TypeCoercer coercer = getService(TypeCoercer.class);
    SelectModel selectModel = coercer.coerce(" UK , USA , DE=Germany ", SelectModel.class);
    assertNull(selectModel.getOptionGroups());
    assertEquals(selectModel.getOptions().size(), 3);
// Waste of effort to re-test each individual option model.
}
Also used : TypeCoercer(org.apache.tapestry5.commons.services.TypeCoercer) Test(org.testng.annotations.Test)

Example 7 with TypeCoercer

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

the class AppPageRenderLinkTransformer method decodePageRenderRequest.

public PageRenderRequestParameters decodePageRenderRequest(Request request) {
    String path = request.getPath();
    String[] split = path.substring(1).split("/");
    if (split.length == 1 && split[0].equals(""))
        return null;
    int pacx = 0;
    String possibleLocaleName = split[0];
    // Might be just the page activation context, or it might be locale then page
    // activation context
    boolean localeSpecified = localizationSetter.isSupportedLocaleName(possibleLocaleName);
    if (localeSpecified) {
        pacx++;
    }
    if (pacx >= split.length)
        return null;
    if (localeSpecified)
        localizationSetter.setLocaleFromLocaleName(possibleLocaleName);
    boolean isLoopback = request.getParameter(TapestryConstants.PAGE_LOOPBACK_PARAMETER_NAME) != null;
    return new PageRenderRequestParameters("View", new ArrayEventContext(typeCoercer, split[pacx]), isLoopback);
}
Also used : PageRenderRequestParameters(org.apache.tapestry5.services.PageRenderRequestParameters) ArrayEventContext(org.apache.tapestry5.internal.services.ArrayEventContext)

Example 8 with TypeCoercer

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

the class StreamPageContentResultProcessor method processResultValue.

public void processResultValue(StreamPageContent value) throws IOException {
    Class<?> pageClass = value.getPageClass();
    Object[] activationContext = value.getPageActivationContext();
    final String pageName = pageClass == null ? requestGlobals.getActivePageName() : resolver.resolvePageClassNameToPageName(pageClass.getName());
    final EventContext context = activationContext == null ? new EmptyEventContext() : new ArrayEventContext(typeCoercer, activationContext);
    if (value.isBypassActivation()) {
        request.setAttribute(InternalConstants.BYPASS_ACTIVATION, true);
    }
    request.setAttribute(TapestryConstants.RESPONSE_RENDERER, new IOOperation<Void>() {

        public Void perform() throws IOException {
            handler.handle(new PageRenderRequestParameters(pageName, context, false));
            return null;
        }
    });
}
Also used : EmptyEventContext(org.apache.tapestry5.internal.EmptyEventContext) EventContext(org.apache.tapestry5.EventContext) PageRenderRequestParameters(org.apache.tapestry5.services.PageRenderRequestParameters) EmptyEventContext(org.apache.tapestry5.internal.EmptyEventContext) IOException(java.io.IOException)

Example 9 with TypeCoercer

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

the class ExpansionPageElementImplTest method invariant_binding_is_cached.

@Test
public void invariant_binding_is_cached() {
    Binding binding = mockBinding();
    TypeCoercer coercer = mockTypeCoercer();
    MarkupWriter writer = mockMarkupWriter();
    RenderQueue queue = mockRenderQueue();
    Object value = new Object();
    train_isInvariant(binding, true);
    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();
    // It is now cached ...
    writer.write("STRING-VALUE");
    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)

Example 10 with TypeCoercer

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

the class EnumValueEncoderTest method roundtrip_with_custom_coercer.

@Test
public // TAP5-2496
void roundtrip_with_custom_coercer() {
    CoercionTuple<Stooge, String> stoogeToString = CoercionTuple.create(Stooge.class, String.class, new Coercion<Stooge, String>() {

        @Override
        public String coerce(Stooge input) {
            return String.valueOf(input.ordinal());
        }
    });
    CoercionTuple<String, Stooge> stringToStooge = CoercionTuple.create(String.class, Stooge.class, new Coercion<String, Stooge>() {

        @Override
        public Stooge coerce(String input) {
            return Stooge.values()[Integer.parseInt(input)];
        }
    });
    Map<CoercionTuple.Key, CoercionTuple> map = new HashMap<>();
    map.put(stoogeToString.getKey(), stoogeToString);
    map.put(stringToStooge.getKey(), stringToStooge);
    TypeCoercer typeCoercer = new TypeCoercerImpl(map);
    EnumValueEncoder<Stooge> encoder = new EnumValueEncoder<Stooge>(typeCoercer, Stooge.class);
    Stooge serverValue = Stooge.LARRY;
    String clientValue = encoder.toClient(serverValue);
    Stooge convertedBack = encoder.toValue(clientValue);
    assertEquals(convertedBack, serverValue);
}
Also used : HashMap(java.util.HashMap) TypeCoercer(org.apache.tapestry5.commons.services.TypeCoercer) CoercionTuple(org.apache.tapestry5.commons.services.CoercionTuple) TypeCoercerImpl(org.apache.tapestry5.commons.internal.services.TypeCoercerImpl) 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