ClassCast { public static final Object cast(final String value, final String typeName) { if (value == null) { return null; } try { switch (typeName) { case INTEGER_OBJ: case INT: return Integer.valueOf(value); case LONG_OBJ: case LONG: return Long.valueOf(value); case DOUBLE_OBJ: case DOUBLE: return Double.valueOf(value); case FLOAT_OBJ: case FLOAT: return Float.valueOf(value); case BOOLEAN_OBJ: case BOOLEAN: if (ObjectCompare.isInList(value.toUpperCase(), "1", "YES", "Y", "TRUE")) { return Boolean.TRUE; } else if (ObjectCompare.isInList(value.toUpperCase(), "0", "NO", "N", "FALSE")) { return Boolean.FALSE; } else { return Boolean.valueOf(value); } case STRING: return value; case DATE_UTIL: case DATE_SQL: return DateFormat.parse(value, Pattern.DATETIME); case TIMESTAMP: final long time = DateFormat.parse(value, Pattern.TIMESTAMP).getTime(); return new Timestamp(time); default: return JSON.parseObject(value, Class.forName(typeName)); } } catch (final ClassCastException | ClassNotFoundException | ParseException e) { throw new org.nanoframework.commons.exception.ClassCastException(e.getMessage(), e); } } private  ClassCast(); static final Object cast(final String value, final String typeName); static final Object cast(final Object value, final String typeName); static Date parseDate(final String date);  }
@Test public void castNullTest() { final Object value = ClassCast.cast(null, "java.lang.String"); assertEquals(value, null); }
@Test public void castStringTest() { final Object value = ClassCast.cast("cast", "java.lang.String"); assertEquals("cast", value); }
@Test public void castBaseTypeTest() { try { ClassCast.cast("1", "int"); } catch (final Throwable e) { assertEquals(e instanceof org.nanoframework.commons.exception.ClassCastException, true); } }
@Test public void castDateTest() { final Object value = ClassCast.cast("2016-07-03 20:35:00", "java.util.Date"); assertEquals(value.getClass(), java.util.Date.class); }
@Test public void castObjectEmptyTypeTest() { try { ClassCast.cast(1, ""); } catch (final Throwable e) { assertEquals(e instanceof IllegalArgumentException, true); } }
@Test public void castObjectNullTest() { final Object value = ClassCast.cast((Object) null, "java.lang.String"); assertEquals(value, null); }
@Test public void castObjectIntegerTest() { final Object value = ClassCast.cast(1, "java.lang.Integer"); assertEquals(value instanceof Integer, true); final Object value2 = ClassCast.cast(new BigDecimal(1), "java.lang.Integer"); assertEquals(value2 instanceof Integer, true); final Object value3 = ClassCast.cast(1.5, "java.lang.Integer"); assertEquals(value3 instanceof Integer, true); }
@Test public void castObjectIntegerArrayTest() { final Object value = ClassCast.cast(new Object[]{1.0, 2L, "0.6", null, "" }, "[Ljava.lang.Integer;"); assertEquals(value instanceof Integer[], true); assertEquals(((Integer[]) value)[0], (Integer) 1); assertEquals(((Integer[]) value)[1], (Integer) 2); assertEquals(((Integer[]) value)[2], (Integer) 0); }
@Test public void castObjectLongTest() { final Object value = ClassCast.cast(1L, "java.lang.Long"); assertEquals(value instanceof Long, true); assertEquals((Long) value, (Long) 1L); final Object value2 = ClassCast.cast(new BigDecimal(1), "java.lang.Long"); assertEquals(value2 instanceof Long, true); assertEquals((Long) value2, (Long) 1L); final Object value3 = ClassCast.cast(1.5, "java.lang.Long"); assertEquals(value3 instanceof Long, true); assertEquals((Long) value3, (Long) 1L); }
@Test public void castObjectLongArrayTest() { final Object value = ClassCast.cast(new Object[]{1.0, 2, "0.6", null, "" }, "[Ljava.lang.Long;"); assertEquals(value instanceof Long[], true); assertEquals(((Long[]) value)[0], (Long) 1L); assertEquals(((Long[]) value)[1], (Long) 2L); assertEquals(((Long[]) value)[2], (Long) 0L); }
@Test public void castObjectDoubleTest() { final Object value = ClassCast.cast(1.5, "java.lang.Double"); assertEquals(value instanceof Double, true); assertEquals((Double) value, (Double) 1.5); final Object value2 = ClassCast.cast(new BigDecimal(1), "java.lang.Double"); assertEquals(value2 instanceof Double, true); assertEquals((Double) value2, (Double) 1D); final Object value3 = ClassCast.cast(1L, "java.lang.Double"); assertEquals(value3 instanceof Double, true); assertEquals((Double) value3, (Double) 1D); }
@Test public void castObjectDoubleArrayTest() { final Object value = ClassCast.cast(new Object[]{1.0, 2, "0.6", null, "" }, "[Ljava.lang.Double;"); assertEquals(value instanceof Double[], true); assertEquals(((Double[]) value)[0], (Double) 1D); assertEquals(((Double[]) value)[1], (Double) 2D); assertEquals(((Double[]) value)[2], (Double) 0.6); }
@Test public void castObjectFloatTest() { final Object value = ClassCast.cast(1.5F, "java.lang.Float"); assertEquals(value instanceof Float, true); assertEquals((Float) value, (Float) 1.5F); final Object value2 = ClassCast.cast(new BigDecimal(1.5), "java.lang.Float"); assertEquals(value2 instanceof Float, true); assertEquals((Float) value2, (Float) 1.5F); final Object value3 = ClassCast.cast(1L, "java.lang.Float"); assertEquals(value3 instanceof Float, true); assertEquals((Float) value3, (Float) 1F); }
@Test public void castObjectFloatArrayTest() { final Object value = ClassCast.cast(new Object[]{1.0, 2, "0.6", null, "" }, "[Ljava.lang.Float;"); assertEquals(value instanceof Float[], true); assertEquals(((Float[]) value)[0], (Float) 1F); assertEquals(((Float[]) value)[1], (Float) 2F); assertEquals(((Float[]) value)[2], (Float) 0.6F); }
@Test public void castObjectBooleanTest() { final Object value = ClassCast.cast(true, "java.lang.Boolean"); assertEquals((boolean) value, true); final Object value2 = ClassCast.cast("1", "java.lang.Boolean"); assertEquals((boolean) value2, true); final Object value3 = ClassCast.cast("YES", "java.lang.Boolean"); assertEquals((boolean) value3, true); final Object value4 = ClassCast.cast("0", "java.lang.Boolean"); assertEquals((boolean) value4, false); final Object value5 = ClassCast.cast("NO", "java.lang.Boolean"); assertEquals((boolean) value5, false); final Object value6 = ClassCast.cast(1, "java.lang.Boolean"); assertEquals((boolean) value6, false); }
@Test public void castObjectDateTest() { final Object value = ClassCast.cast((Object) "2016-07-03 22:05:00", "java.util.Date"); assertEquals(value instanceof Date, true); final Object value2 = ClassCast.cast(new Date(), "java.util.Date"); assertEquals(value2 instanceof Date, true); }
@Test public void castObjectTimestampTest() { final Object value = ClassCast.cast((Object) "2016-07-03 22:05:00", "java.sql.Timestamp"); assertEquals(value instanceof Timestamp, true); final Object value2 = ClassCast.cast(new Date(), "java.sql.Timestamp"); assertEquals(value2 instanceof Timestamp, true); }
@Test public void castObjectUnsupportedTest() { try { ClassCast.cast(1, "int"); } catch (final Throwable e) { assertEquals(e instanceof org.nanoframework.commons.exception.ClassCastException, true); } }
@Test public void castObjectDefaultTest() { final Object value = ClassCast.cast((Object) "[\"1\",\"2\"]", "java.util.List"); assertEquals(value instanceof List, true); final Object value2 = ClassCast.cast((Object) "[\"1\",\"2\"]", "[Ljava.lang.String;"); assertEquals(value2 instanceof String[], true); final Object value3 = ClassCast.cast((Object) new String[]{"[\"1\",\"2\"]", "[1, 2]"}, "java.util.List"); assertEquals(value3 instanceof Object[], true); assertEquals(((Object[]) value3)[0] instanceof List, true); assertEquals(((List<?>) ((Object[]) value3)[0]).get(0), "1"); assertEquals(((List<?>) ((Object[]) value3)[1]).get(0), 1); final Object value4 = ClassCast.cast((Object) new String[]{"[\"1\",\"2\"]", "[1, 2]"}, "[Ljava.lang.String;"); assertEquals(value4 instanceof Object[], true); assertEquals(((Object[]) value3)[0] instanceof List, true); assertEquals(((List<?>) ((Object[]) value3)[0]).get(0), "1"); assertEquals(((List<?>) ((Object[]) value3)[1]).get(0), 1); }
@Test public void castByStringTest() { Object val = ClassCast.cast("1", Integer.class.getName()); assertEquals(1, val); val = ClassCast.cast("1", Long.class.getName()); assertEquals(1L, val); val = ClassCast.cast("1.0", Double.class.getName()); assertEquals(1D, val); val = ClassCast.cast("1.0", Float.class.getName()); assertEquals(1F, val); String now = "2015-01-01 12:00:00.000"; val = ClassCast.cast(String.valueOf(now), Timestamp.class.getName()); assertEquals(Timestamp.valueOf(now), val); Map<String, String> map = new HashMap<String, String>() { private static final long serialVersionUID = 1L; { put("id", "id0"); put("name", "name0"); } }; val = ClassCast.cast(JSON.toJSONString(map), UseEntity.class.getName()); assertEquals(UseEntity.class.getName(), val.getClass().getName()); assertEquals("id0", ((UseEntity) val).getId()); assertEquals("name0", ((UseEntity) val).getName()); }
@Test public void castByObjectTest() { Object val = ClassCast.cast(new BigDecimal(1), Integer.class.getName()); assertEquals(1, val); val = ClassCast.cast(new BigDecimal(1), Long.class.getName()); assertEquals(1L, val); val = ClassCast.cast(new BigDecimal(1), Double.class.getName()); assertEquals(1D, val); val = ClassCast.cast(new BigDecimal(1), Float.class.getName()); assertEquals(1F, val); Date now = new Timestamp(System.currentTimeMillis()); val = ClassCast.cast(DateFormatUtils.format(now, Pattern.TIMESTAMP.get()), Timestamp.class.getName()); assertEquals(now.getTime(), ((Timestamp) val).getTime()); }