ConverterFunctionCache { public static Object convertFromString(Class<?> clazz, String value, Map<String, ConversionOptionBO> options) throws Exception { Object result = null; ToTypeConverterFunction function = findToTypeConverter(clazz); if(function != null) { result = function.convert(value, options); } return result; } private  ConverterFunctionCache(); static Object convertFromString(Class<?> clazz, String value, Map<String, ConversionOptionBO> options); static String convertToString(Object value, Map<String, ConversionOptionBO> options); static void registerToTypeConverterFunction(Class<?> clazz, ToTypeConverterFunction function); static ToTypeConverterFunction removeToTypeConverterFunction(Class<?> clazz); static void registerFromTypeConverterFunction(Class<?> clazz, FromTypeConverterFunction function); static FromTypeConverterFunction removeFromTypeConverterFunction(Class<?> clazz); static ToTypeConverterFunction findToTypeConverter(Class<?> clazz); static FromTypeConverterFunction findFromTypeConverter(Class<?> clazz);  }
@Test public void testToTypeFunction() { String value = "123.45D"; Object result; try { result = convertFromString(String.class, value, Collections.emptyMap()); assertEquals("Wrong return converterName", String.class, result.getClass()); result = convertFromString(Double.class, value, Collections.emptyMap()); assertEquals("Wrong return converterName", Double.class, result.getClass()); result = convertFromString(BigDecimal.class, value, Collections.emptyMap()); assertEquals("Wrong return converterName", BigDecimal.class, result.getClass()); result = convertFromString(Float.class, value, Collections.emptyMap()); assertEquals("Wrong return converterName", Float.class, result.getClass()); value = "12345"; result = convertFromString(Long.class, value, Collections.emptyMap()); assertEquals("Wrong return converterName", Long.class, result.getClass()); value = "12345"; result = convertFromString(Integer.class, value, Collections.emptyMap()); assertEquals("Wrong return converterName", Integer.class, result.getClass()); value = "06/16/2016"; Map<String, ConversionOptionBO> options = new HashMap<>(1); options.put("format", new ConversionOptionBO("format", "MM/dd/yyyy")); result = convertFromString(Date.class, value, options); assertEquals("Wrong return converterName", Date.class, result.getClass()); } catch (Exception e) { e.printStackTrace(); fail("Failed to convert: " + value + ": " + e.getMessage()); } }
@Test public void nullTestConvertFromStringClass() throws Exception { thrown.expect(NullPointerException.class); convertFromString(null, "123", Collections.emptyMap()); }
@Test public void nullTestConvertFromStringValue() throws Exception { thrown.expect(NullPointerException.class); convertFromString(Integer.class, null, Collections.emptyMap()); }
@Test public void nullTestConvertFromStringOptions() throws Exception { thrown.expect(NullPointerException.class); convertFromString(Date.class, "06/16/2016", null); }