StringToEnumTypeMapper implements TypeMapper<String, T> { @Override public String toDatabaseType(T constant) { if (constant == null) { return null; } else { final Class<?> enumClass = constant.getClass(); final Method toDatabase; try { toDatabase = enumClass.getMethod(TO_DATABASE_METHOD); } catch (final NoSuchMethodException ex) { throw new IllegalArgumentException( "Could not find generated '" + TO_DATABASE_METHOD + "'-method in enum class '" + constant.getClass().getName() + "'.", ex ); } try { @SuppressWarnings("unchecked") final String result = (String) toDatabase.invoke(constant); return result; } catch (final IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) { throw new IllegalArgumentException("Error executing '" + TO_DATABASE_METHOD + "' in generated enum class '" + constant.getClass().getName() + "'.", ex); } } } StringToEnumTypeMapper(); @Override String getLabel(); @Override Type getJavaType(Column column); @Override Category getJavaTypeCategory(Column column); @Override T toJavaType(Column column, Class<?> entityType, String value); @Override String toDatabaseType(T constant); @Inject public Injector injector; }
@Test void toDatabaseType() { assertEquals("One", instance.toDatabaseType(User.Name.ONE)); }
@Test void toDatabaseTypeNull() { assertNull(instance.toDatabaseType(null)); }
@Test void toDatabaseTypeNoSuchMethod() { assertThrows(IllegalArgumentException.class, () -> instance.toDatabaseType(User.Name.TWO)); }
@Test void toDatabaseTypeIllegalAccess() { assertThrows(IllegalArgumentException.class, () -> instance.toDatabaseType(User.Name.THREE)); }