ConverterFunctionCache { public static String convertToString(Object value, Map<String, ConversionOptionBO> options) throws Exception { String result = null; FromTypeConverterFunction function = findFromTypeConverter(value.getClass()); 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 testFromTypeFunction() { Object value = "123.45D"; String result; try { result = convertToString("123", Collections.emptyMap()); assertEquals("Wrong return converterName", String.class, result.getClass()); result = convertToString(123.45D, Collections.emptyMap()); assertNotNull("Conversion failed", result); assertEquals("Wrong return converterName", String.class, result.getClass()); result = convertToString(new BigDecimal("123.45"), Collections.emptyMap()); assertNotNull("Conversion failed", result); assertEquals("Wrong return converterName", String.class, result.getClass()); result = convertToString(123.45F, Collections.emptyMap()); assertNotNull("Conversion failed", result); assertEquals("Wrong return converterName", String.class, result.getClass()); value = "12345"; result = convertToString(12345L, Collections.emptyMap()); assertNotNull("Conversion failed", result); assertEquals("Wrong return converterName", String.class, result.getClass()); value = "12345"; result = convertToString(12345, Collections.emptyMap()); assertNotNull("Conversion failed", result); assertEquals("Wrong return converterName", String.class, result.getClass()); Calendar cal = Calendar.getInstance(); cal.set(Calendar.YEAR, 2016); cal.set(Calendar.MONTH, 5); cal.set(Calendar.DATE, 16); Map<String, ConversionOptionBO> options = new HashMap<>(1); options.put("format", new ConversionOptionBO("format", "MM/dd/yyyy")); result = convertToString(cal.getTime(), options); assertNotNull("Conversion failed", result); assertEquals("Wrong return converterName", String.class, result.getClass()); } catch (Exception e) { e.printStackTrace(); fail("Failed to convert: " + value + ": " + e.getMessage()); } }
@Test public void nullTestConvertToStringValue() throws Exception { thrown.expect(NullPointerException.class); convertToString(null, Collections.emptyMap()); }
@Test public void nullTestConvertToStringOptions() throws Exception { thrown.expect(NullPointerException.class); convertToString(new Date(), null); }