ToEnumConverter implements Converter { @Override public <T> T convert(Class<T> cls, Object value) { try { if (ClassUtils.isAssignable(value.getClass(), String.class)) { return stringToEnum(cls, (String) value); } else if (ClassUtils.isAssignable(value.getClass(), Integer.class, true)) { return intToEnum(cls, (Integer) value); } else { throw new UnsupportedOperationException(value.getClass().getSimpleName() + " to Enum no supported"); } } catch (IndexOutOfBoundsException | ReflectiveOperationException | UnsupportedOperationException | IllegalArgumentException e) { throw new InvalidAttributeException("Unknown " + cls.getSimpleName() + " value " + value, e); } }  @Override T convert(Class<T> cls, Object value);  }
@Test public void testIntToEnumConversion() throws Exception { assertEquals( Seasons.WINTER, converter.convert(Seasons.class, 0), "Enum converter correctly converted int to enum"); assertEquals( Seasons.SPRING, converter.convert(Seasons.class, 1), "Enum converter correctly converted int to enum"); }
@Test public void testMissingNumberValueException() throws Exception { assertThrows(InvalidAttributeException.class, () -> converter.convert(Seasons.class, 5)); }
@Test public void testStringToEnumConversion() throws Exception { assertEquals( Seasons.SUMMER, converter.convert(Seasons.class, "SUMMER"), "Enum converter correctly converted String to enum"); assertEquals( Seasons.FALL, converter.convert(Seasons.class, "FALL"), "Enum converter correctly converted String to enum"); }
@Test public void testMissingStringValueException() throws Exception { assertThrows(InvalidAttributeException.class, () -> converter.convert(Seasons.class, "AUTUMN")); }
@Test public void testInvalidType() throws Exception { assertThrows(InvalidAttributeException.class, () -> converter.convert(Seasons.class, 'A')); }