Search in sources :

Example 1 with InternalComponentResources

use of org.apache.tapestry5.internal.InternalComponentResources in project tapestry-5 by apache.

the class PageLoaderImpl method addParameterBindingAction.

private void addParameterBindingAction(AssemblerContext context, final EmbeddedComponentAssembler embeddedAssembler, final String parameterName, final String parameterValue, final String metaDefaultBindingPrefix, final Location location, final boolean ignoreUnmatchedFormal) {
    if (embeddedAssembler.isBound(parameterName))
        return;
    embeddedAssembler.setBound(parameterName);
    if (parameterValue.startsWith(InternalConstants.INHERIT_BINDING_PREFIX)) {
        String containerParameterName = parameterValue.substring(InternalConstants.INHERIT_BINDING_PREFIX.length());
        addInheritedBindingAction(context, parameterName, containerParameterName);
        return;
    }
    context.add(new PageAssemblyAction() {

        public void execute(PageAssembly pageAssembly) {
            // Because of published parameters, we have to wait until page assembly time to throw out
            // informal parameters bound to components that don't support informal parameters ...
            // otherwise we'd throw out (sometimes!) published parameters.
            final ParameterBinder binder = embeddedAssembler.createParameterBinder(parameterName);
            if (binder == null) {
                if (ignoreUnmatchedFormal) {
                    return;
                }
                throw new UnknownValueException(String.format("Component %s does not include a formal parameter '%s' (and does not support informal parameters).", pageAssembly.createdElement.peek().getCompleteId(), parameterName), null, null, new AvailableValues("Formal parameters", embeddedAssembler.getFormalParameterNames()));
            }
            final String defaultBindingPrefix = binder.getDefaultBindingPrefix(metaDefaultBindingPrefix);
            InternalComponentResources containerResources = pageAssembly.activeElement.peek().getComponentResources();
            ComponentPageElement embeddedElement = pageAssembly.createdElement.peek();
            InternalComponentResources embeddedResources = embeddedElement.getComponentResources();
            Binding binding = elementFactory.newBinding(parameterName, containerResources, embeddedResources, defaultBindingPrefix, parameterValue, location);
            binder.bind(embeddedElement, binding);
        }
    });
}
Also used : LiteralBinding(org.apache.tapestry5.internal.bindings.LiteralBinding) Binding(org.apache.tapestry5.Binding) InternalComponentResources(org.apache.tapestry5.internal.InternalComponentResources) UnknownValueException(org.apache.tapestry5.commons.util.UnknownValueException) AvailableValues(org.apache.tapestry5.commons.util.AvailableValues)

Example 2 with InternalComponentResources

use of org.apache.tapestry5.internal.InternalComponentResources 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 3 with InternalComponentResources

use of org.apache.tapestry5.internal.InternalComponentResources 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 4 with InternalComponentResources

use of org.apache.tapestry5.internal.InternalComponentResources in project tapestry-5 by apache.

the class PageRenderRequestHandlerImplTest method loopback_request.

@Test
public void loopback_request() throws Exception {
    RequestPageCache cache = mockRequestPageCache();
    ComponentEventResultProcessor processor = mockComponentEventResultProcessor();
    PageResponseRenderer renderer = mockPageResponseRenderer();
    Page page = mockPage();
    EventContext context = mockEventContext();
    ComponentPageElement root = mockComponentPageElement();
    InternalComponentResources pageResources = mockInternalComponentResources();
    PageActivator activator = newMock(PageActivator.class);
    Request request = mockRequest();
    train_getAttribute(request, InternalConstants.BYPASS_ACTIVATION, null);
    train_get(cache, "foo/Bar", page);
    train_getRootElement(page, root);
    train_getComponentResources(root, pageResources);
    expect(activator.activatePage(pageResources, context, processor)).andReturn(false);
    // Skips the pageReset()
    renderer.renderPageResponse(page);
    replay();
    PageRenderRequestHandler handler = new PageRenderRequestHandlerImpl(cache, processor, renderer, activator, request);
    PageRenderRequestParameters parameters = new PageRenderRequestParameters("foo/Bar", context, true);
    handler.handle(parameters);
    verify();
}
Also used : EventContext(org.apache.tapestry5.EventContext) PageRenderRequestParameters(org.apache.tapestry5.services.PageRenderRequestParameters) ComponentPageElement(org.apache.tapestry5.internal.structure.ComponentPageElement) ComponentEventResultProcessor(org.apache.tapestry5.services.ComponentEventResultProcessor) InternalComponentResources(org.apache.tapestry5.internal.InternalComponentResources) Request(org.apache.tapestry5.http.services.Request) PageRenderRequestHandler(org.apache.tapestry5.services.PageRenderRequestHandler) Page(org.apache.tapestry5.internal.structure.Page) Test(org.testng.annotations.Test)

Example 5 with InternalComponentResources

use of org.apache.tapestry5.internal.InternalComponentResources in project tapestry-5 by apache.

the class MixinWorker method createMixinFieldProvider.

private ComputedValue<FieldConduit<Object>> createMixinFieldProvider(final String fieldName, final String mixinClassName) {
    return new ComputedValue<FieldConduit<Object>>() {

        public FieldConduit get(InstanceContext context) {
            ComponentResources resources = context.get(ComponentResources.class);
            final InternalComponentResources icr = (InternalComponentResources) resources;
            return new ReadOnlyComponentFieldConduit(resources, fieldName) {

                public Object get(Object instance, InstanceContext context) {
                    return icr.getMixinByClassName(mixinClassName);
                }
            };
        }
    };
}
Also used : InternalComponentResources(org.apache.tapestry5.internal.InternalComponentResources) InternalComponentResources(org.apache.tapestry5.internal.InternalComponentResources) ComponentResources(org.apache.tapestry5.ComponentResources)

Aggregations

InternalComponentResources (org.apache.tapestry5.internal.InternalComponentResources)22 Test (org.testng.annotations.Test)10 ComponentModel (org.apache.tapestry5.model.ComponentModel)9 Component (org.apache.tapestry5.runtime.Component)8 Instantiator (org.apache.tapestry5.internal.services.Instantiator)7 Binding (org.apache.tapestry5.Binding)5 TypeCoercer (org.apache.tapestry5.commons.services.TypeCoercer)4 InternalPropBinding (org.apache.tapestry5.internal.bindings.InternalPropBinding)4 MarkupWriter (org.apache.tapestry5.MarkupWriter)3 Request (org.apache.tapestry5.http.services.Request)3 ValueEncoderSource (org.apache.tapestry5.services.ValueEncoderSource)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 LiteralBinding (org.apache.tapestry5.internal.bindings.LiteralBinding)2 ParameterModel (org.apache.tapestry5.model.ParameterModel)2 EnumSelectModel (org.apache.tapestry5.util.EnumSelectModel)2 ComponentResources (org.apache.tapestry5.ComponentResources)1 EventContext (org.apache.tapestry5.EventContext)1 Persist (org.apache.tapestry5.annotations.Persist)1