EnumConverter implements Converter<T> { @SuppressWarnings("unchecked") public T convert(String value, Class<? extends T> type, ResourceBundle bundle) { if (isNullOrEmpty(value)) { return null; } if (Character.isDigit(value.charAt(0))) { return resolveByOrdinal(value, (Class<T>) type, bundle); } else { return resolveByName(value, (Class<T>) type, bundle); } }  @SuppressWarnings("unchecked") T convert(String value, Class<? extends T> type, ResourceBundle bundle);  }
@Test public void shouldBeAbleToConvertByOrdinal() { assertThat(converter.convert("1", MyCustomEnum.class, bundle), is(equalTo(MyCustomEnum.SECOND))); }
@Test public void shouldBeAbleToConvertByName() { assertThat(converter.convert("FIRST", MyCustomEnum.class, bundle), is(equalTo(MyCustomEnum.FIRST))); }
@Test public void shouldConvertEmptyToNull() { assertThat(converter.convert("", MyCustomEnum.class, bundle), is(nullValue())); }
@Test public void shouldComplainAboutInvalidIndex() { try { converter.convert("3200", MyCustomEnum.class, bundle); } catch (ConversionError e) { assertThat(e.getMessage(), is(equalTo("3200 is not a valid option."))); } }
@Test public void shouldComplainAboutInvalidNumber() { try { converter.convert("32a00", MyCustomEnum.class, bundle); } catch (ConversionError e) { assertThat(e.getMessage(), is(equalTo("32a00 is not a valid option."))); } }
@Test public void shouldComplainAboutInvalidOrdinal() { try { converter.convert("THIRD", MyCustomEnum.class, bundle); } catch (ConversionError e) { assertThat(e.getMessage(), is(equalTo("THIRD is not a valid option."))); } }
@Test public void shouldAcceptNull() { assertThat(converter.convert(null, MyCustomEnum.class, bundle), is(nullValue())); }