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);
}
});
}
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);
}
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();
}
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();
}
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);
}
};
}
};
}
Aggregations