Search in sources :

Example 1 with EnumValueEncoder

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

Example 2 with EnumValueEncoder

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"));
    }
}
Also used : TypeCoercer(org.apache.tapestry5.commons.services.TypeCoercer) UnknownValueException(org.apache.tapestry5.commons.util.UnknownValueException) List(java.util.List) Test(org.testng.annotations.Test)

Aggregations

TypeCoercer (org.apache.tapestry5.commons.services.TypeCoercer)2 Test (org.testng.annotations.Test)2 HashMap (java.util.HashMap)1 List (java.util.List)1 TypeCoercerImpl (org.apache.tapestry5.commons.internal.services.TypeCoercerImpl)1 CoercionTuple (org.apache.tapestry5.commons.services.CoercionTuple)1 UnknownValueException (org.apache.tapestry5.commons.util.UnknownValueException)1