EnumValueConverter extends AbstractValueConverter<T> { @Override public T convert(String name) throws IllegalArgumentException { if (name == null) { throw new IllegalArgumentException("Cant convert 'null' to " + enumClass.getSimpleName()); } try { return Enum.valueOf(enumClass, name); } catch (IllegalArgumentException e) { } try { return Enum.valueOf(enumClass, name.toUpperCase()); } catch (IllegalArgumentException e) { } try { return Enum.valueOf(enumClass, name.toUpperCase().replace("-", "_")); } catch (IllegalArgumentException e) { } throw new IllegalArgumentException("Can't convert " + name + " to " + enumClass.getSimpleName()); } EnumValueConverter(Class<T> enumClass); @Override T convert(String name); @Override String toString(T value);  }
@Test public void testConvert() throws Exception { assertEquals(TestEnum.TEST_ENUM, converter.convert("TEST_ENUM")); assertEquals(TestEnum.TEST_ENUM, converter.convert("test_enum")); assertEquals(TestEnum.TEST_ENUM, converter.convert("test-enum")); }
@Test(expected = IllegalArgumentException.class) public void testConvertNull() throws Exception { converter.convert(null); }
@Test(expected = IllegalArgumentException.class) public void testConvertInvalid() throws Exception { converter.convert("BEST"); }