ConverterTypeUtil { public static Object convert(Class<?> expectedClass, Object originalObject) { if (originalObject == null) { return null; } Class<?> currentClass = originalObject.getClass(); if (expectedClass.isAssignableFrom(currentClass)) { return originalObject; } Object toReturn; if (currentClass.equals(String.class)) { try { toReturn = convertFromString(expectedClass, (String) originalObject); } catch (Exception e) { throw new KiePMMLException(String.format(FAILED_CONVERSION, originalObject, expectedClass.getName()), e); } } else if (expectedClass == String.class) { toReturn = originalObject.toString(); } else { throw new KiePMMLException(String.format(FAILED_CONVERSION, originalObject, expectedClass.getName())); } return toReturn; } private  ConverterTypeUtil(); static Object convert(Class<?> expectedClass, Object originalObject);  }
@Test public void convertConvertibleToString() { CONVERTIBLE_TO_STRING.forEach((s, o) -> { Class<?> expectedClass = o.getClass(); Object retrieved = ConverterTypeUtil.convert(expectedClass, s); assertEquals(retrieved, o); }); }
@Test public void convertConvertibleFromString() { CONVERTIBLE_FROM_STRING.forEach((s, o) -> { Class<?> expectedClass = o.getClass(); Object retrieved = ConverterTypeUtil.convert(expectedClass, s); assertEquals(retrieved, o); }); }
@Test public void convertUnconvertible() { UNCONVERTIBLE_FROM_STRING.forEach((s, o) -> { Class<?> expectedClass = o.getClass(); try { ConverterTypeUtil.convert(expectedClass, s); fail(String.format("Expecting KiePMMLException for %s %s", s, o)); } catch (Exception e) { assertEquals(KiePMMLException.class, e.getClass()); } }); }