EnumConverter implements PropertyConverter<T> { @Override public T convert(String value, ConversionContext ctx) { ctx.addSupportedFormats(getClass(), "<enumValue>"); try { return (T) factory.invoke(null, value); } catch (InvocationTargetException | IllegalAccessException e) { LOG.log(Level.FINEST, "Invalid enum createValue '" + value + "' for " + enumType.getName(), e); } try { if (value != null) { return (T) factory.invoke(null, value.toUpperCase(Locale.ENGLISH)); } } catch (InvocationTargetException | IllegalAccessException e) { LOG.log(Level.FINEST, "Invalid enum createValue '" + value + "' for " + enumType.getName(), e); } return null; } EnumConverter(Class<T> enumType); @Override T convert(String value, ConversionContext ctx); @Override boolean equals(Object o); @Override int hashCode();  }
@Test public void testConversionWithMixedCasing() { ConversionContext ctx = new ConversionContext.Builder(TypeLiteral.of(RoundingMode.class)).build(); for (String input : Arrays.asList(RoundingMode.CEILING.toString(), "ceiling", "CeiLinG")) { assertThat(RoundingMode.CEILING).isEqualTo(testConverter.convert(input, ctx)); } }
@Test public void testConvert_OtherValue() { ConversionContext ctx = new ConversionContext.Builder(TypeLiteral.of(RoundingMode.class)).build(); assertThat(testConverter.convert("fooBars", ctx)).isNull(); }
@Test public void testConvert_Nulls() { ConversionContext ctx = new ConversionContext.Builder(TypeLiteral.of(RoundingMode.class)).build(); assertThat(testConverter.convert(null, ctx)).isNull(); }
@Test public void callToConvertAddsMoreSupportedFormatsToTheContext() throws Exception { ConversionContext context = new ConversionContext.Builder("someKey", TypeLiteral.of(Enum.class)).build(); EnumConverter<RoundingMode> converter = new EnumConverter<>(RoundingMode.class); converter.convert("fooBars", context); assertThat(context.getSupportedFormats()).contains("<enumValue> (EnumConverter)"); }