use of org.apache.tapestry5.util.EnumValueEncoder 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);
}
use of org.apache.tapestry5.util.EnumValueEncoder in project tapestry-5 by apache.
the class EnumValueEncoderTest method valid_values_can_be_retrieved_from_exception.
@Test
public // TAP5-1331
void valid_values_can_be_retrieved_from_exception() {
TypeCoercer typeCoercer = getService(TypeCoercer.class);
EnumValueEncoder<Stooge> encoder = new EnumValueEncoder<Stooge>(typeCoercer, Stooge.class);
try {
encoder.toValue("Foo");
fail();
} catch (RuntimeException e) {
assertTrue(e.getCause() instanceof UnknownValueException);
UnknownValueException cause = (UnknownValueException) e.getCause();
List<String> availableValues = cause.getAvailableValues().getValues();
assertTrue(availableValues.contains("MOE"));
assertTrue(availableValues.contains("LARRY"));
assertTrue(availableValues.contains("CURLY_JOE"));
}
}
Aggregations