ScenarioBeanUtil { public static Object convertValue(String className, Object cleanValue, ClassLoader classLoader) { cleanValue = "null".equals(cleanValue) ? null : cleanValue; if (!isPrimitive(className) && cleanValue == null) { return null; } Class<?> clazz = loadClass(className, classLoader); if (!(cleanValue instanceof String)) { if (clazz.isInstance(cleanValue)) { return cleanValue; } throw new IllegalArgumentException(new StringBuilder().append("Object ").append(cleanValue) .append(" is not a String or an instance of ").append(className).toString()); } String value = (String) cleanValue; try { if (clazz.isAssignableFrom(String.class)) { return value; } else if (clazz.isAssignableFrom(Boolean.class) || clazz.isAssignableFrom(boolean.class)) { return parseBoolean(value); } else if (clazz.isAssignableFrom(Integer.class) || clazz.isAssignableFrom(int.class)) { return Integer.parseInt(cleanStringForNumberParsing(value)); } else if (clazz.isAssignableFrom(Long.class) || clazz.isAssignableFrom(long.class)) { return Long.parseLong(cleanStringForNumberParsing(value)); } else if (clazz.isAssignableFrom(Double.class) || clazz.isAssignableFrom(double.class)) { return Double.parseDouble(cleanStringForNumberParsing(value)); } else if (clazz.isAssignableFrom(Float.class) || clazz.isAssignableFrom(float.class)) { return Float.parseFloat(cleanStringForNumberParsing(value)); } else if (clazz.isAssignableFrom(Character.class) || clazz.isAssignableFrom(char.class)) { return parseChar(value); } else if (clazz.isAssignableFrom(Byte.class) || clazz.isAssignableFrom(byte.class)) { return Byte.parseByte(value); } else if (clazz.isAssignableFrom(Short.class) || clazz.isAssignableFrom(short.class)) { return Short.parseShort(cleanStringForNumberParsing(value)); } else if (clazz.isAssignableFrom(LocalDate.class)) { return LocalDate.parse(value, DateTimeFormatter.ofPattern("yyyy-MM-dd")); } else if (Enum.class.isAssignableFrom(clazz)) { return Enum.valueOf(((Class<? extends Enum>) clazz), value); } } catch (RuntimeException e) { throw new IllegalArgumentException(new StringBuilder().append("Impossible to parse '") .append(value).append("' as ").append(className).append(" [") .append(e.getMessage()).append("]").toString()); } throw new IllegalArgumentException(new StringBuilder().append("Class ").append(className) .append(" is not natively supported. Please use an MVEL expression" + " to use it.").toString()); } private  ScenarioBeanUtil(); static T fillBean(String className, Map<List<String>, Object> params, ClassLoader classLoader); @SuppressWarnings("unchecked") static T fillBean(ValueWrapper<Object> initialInstance, String className, Map<List<String>, Object> params, ClassLoader classLoader); static ScenarioBeanWrapper<?> navigateToObject(Object rootObject, List<String> steps); static ScenarioBeanWrapper<?> navigateToObject(Object rootObject, List<String> steps, boolean createIfNull); static Object convertValue(String className, Object cleanValue, ClassLoader classLoader); static String revertValue(Object cleanValue); @SuppressWarnings("unchecked") static Class<T> loadClass(String className, ClassLoader classLoader); static Field getField(Class<?> clazz, String fieldName);  }
@Test public void convertValueTest() { assertEquals("Test", convertValue(String.class.getCanonicalName(), "Test", classLoader)); assertEquals(false, convertValue(boolean.class.getCanonicalName(), "false", classLoader)); assertEquals(true, convertValue(Boolean.class.getCanonicalName(), "true", classLoader)); assertEquals(1, convertValue(int.class.getCanonicalName(), "1", classLoader)); assertEquals(1, convertValue(Integer.class.getCanonicalName(), "1", classLoader)); assertEquals(1L, convertValue(long.class.getCanonicalName(), "1", classLoader)); assertEquals(1L, convertValue(Long.class.getCanonicalName(), "1", classLoader)); assertEquals(1.0d, convertValue(double.class.getCanonicalName(), "1", classLoader)); assertEquals(1.0d, convertValue(Double.class.getCanonicalName(), "1", classLoader)); assertEquals(1.0f, convertValue(float.class.getCanonicalName(), "1", classLoader)); assertEquals(1.0f, convertValue(Float.class.getCanonicalName(), "1", classLoader)); assertEquals(1.0d, convertValue(double.class.getCanonicalName(), "1.0", classLoader)); assertEquals(1.0d, convertValue(Double.class.getCanonicalName(), "1.0", classLoader)); assertEquals(1.0f, convertValue(float.class.getCanonicalName(), "1.0", classLoader)); assertEquals(1.0f, convertValue(Float.class.getCanonicalName(), "1.0", classLoader)); assertEquals(1.0d, convertValue(double.class.getCanonicalName(), "1.0d", classLoader)); assertEquals(1.0d, convertValue(Double.class.getCanonicalName(), "1.0d", classLoader)); assertEquals(1.0f, convertValue(float.class.getCanonicalName(), "1.0f", classLoader)); assertEquals(1.0f, convertValue(Float.class.getCanonicalName(), "1.0f", classLoader)); assertEquals(1.0d, convertValue(double.class.getCanonicalName(), "1.0D", classLoader)); assertEquals(1.0d, convertValue(Double.class.getCanonicalName(), "1.0D", classLoader)); assertEquals(1.0f, convertValue(float.class.getCanonicalName(), "1.0F", classLoader)); assertEquals(1.0f, convertValue(Float.class.getCanonicalName(), "1.0F", classLoader)); assertEquals('a', convertValue(char.class.getCanonicalName(), "a", classLoader)); assertEquals('a', convertValue(Character.class.getCanonicalName(), "a", classLoader)); assertEquals((short) 1, convertValue(short.class.getCanonicalName(), "1", classLoader)); assertEquals((short) 1, convertValue(Short.class.getCanonicalName(), "1", classLoader)); assertEquals("0".getBytes()[0], convertValue(byte.class.getCanonicalName(), Byte.toString("0".getBytes()[0]), classLoader)); assertEquals("0".getBytes()[0], convertValue(Byte.class.getCanonicalName(), Byte.toString("0".getBytes()[0]), classLoader)); assertEquals(LocalDate.of(2018, 5, 20), convertValue(LocalDate.class.getCanonicalName(), "2018-05-20", classLoader)); assertEquals(EnumTest.FIRST, convertValue(EnumTest.class.getCanonicalName(), "FIRST", classLoader)); assertNull(convertValue(Float.class.getCanonicalName(), null, classLoader)); }
@Test public void convertValueFailLoadClassTest() { assertThatThrownBy(() -> convertValue("my.NotExistingClass", "Test", classLoader)) .isInstanceOf(IllegalArgumentException.class) .hasMessageStartingWith("Impossible to load "); }
@Test public void convertValueFailUnsupportedTest() { assertThatThrownBy(() -> convertValue(RuleScenarioRunnerHelperTest.class.getCanonicalName(), "Test", classLoader)) .isInstanceOf(IllegalArgumentException.class) .hasMessageEndingWith("Please use an MVEL expression to use it."); }
@Test public void convertValueFailPrimitiveNullTest() { assertThatThrownBy(() -> convertValue("int", null, classLoader)) .isInstanceOf(IllegalArgumentException.class) .hasMessageContaining(" is not a String or an instance of"); }
@Test public void convertValueFailNotStringOrTypeTest() { assertThatThrownBy(() -> convertValue(RuleScenarioRunnerHelperTest.class.getCanonicalName(), 1, classLoader)) .isInstanceOf(IllegalArgumentException.class) .hasMessageStartingWith("Object 1 is not a String or an instance of"); }
@Test public void convertValueFailParsing() { String integerCanonicalName = Integer.class.getCanonicalName(); assertThatThrownBy(() -> convertValue(integerCanonicalName, "wrongValue", classLoader)) .isInstanceOf(IllegalArgumentException.class) .hasMessageStartingWith("Impossible to parse 'wrongValue' as " + integerCanonicalName); }
@Test public void convertValueEnumWrongValue() { String enumTestCanonicalName = EnumTest.class.getCanonicalName(); assertThatThrownBy(() -> convertValue(EnumTest.class.getCanonicalName(), "FIRS", classLoader)) .isInstanceOf(IllegalArgumentException.class) .hasMessageStartingWith("Impossible to parse 'FIRS' as " + enumTestCanonicalName); }