EnumCaseForgivingConverter extends EnumConverter { public static <T extends Enum<T>> T resolve(Class<T> type, String token) { try { return Enum.valueOf(type, token.toUpperCase()); } catch (Exception e) { Exceptions.propagateIfFatal(e); try { for (T v: type.getEnumConstants()) if (v.name().equalsIgnoreCase(token)) return v; throw e; } catch (Exception e2) { throw Exceptions.propagate(e2); } } }  @Override @SuppressWarnings({ "unchecked", "rawtypes" }) Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context); static T resolve(Class<T> type, String token);  }
@Test public void testFindsCaseInsensitive() throws Exception { assertEquals(EnumCaseForgivingConverter.resolve(MyEnum.class, "FOO"), MyEnum.FOO); assertEquals(EnumCaseForgivingConverter.resolve(MyEnum.class, "foo"), MyEnum.FOO); assertEquals(EnumCaseForgivingConverter.resolve(MyEnum.class, "Foo"), MyEnum.FOO); assertEquals(EnumCaseForgivingConverter.resolve(MyEnum.class, "BAR"), MyEnum.BaR); assertEquals(EnumCaseForgivingConverter.resolve(MyEnum.class, "bar"), MyEnum.BaR); assertEquals(EnumCaseForgivingConverter.resolve(MyEnum.class, "Bar"), MyEnum.BaR); }
@Test public void testFailsIfNoMatch() throws Exception { try { assertEquals(EnumCaseForgivingConverter.resolve(MyEnum.class, "DoesNotExist"), MyEnum.BaR); fail(); } catch (IllegalArgumentException e) { if (!e.toString().matches(".*No enum.*MyEnum.DOESNOTEXIST")) throw e; } }