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