ResultSetUtil { public static Object convertValue(final Object value, final Class<?> convertType) { if (null == value) { return convertNullValue(convertType); } if (value.getClass() == convertType) { return value; } if (LocalDateTime.class.equals(convertType)) { return convertLocalDateTimeValue(value); } if (LocalDate.class.equals(convertType)) { return convertLocalDateValue(value); } if (LocalTime.class.equals(convertType)) { return convertLocalTimeValue(value); } if (URL.class.equals(convertType)) { return convertURL(value); } if (value instanceof Number) { return convertNumberValue(value, convertType); } if (value instanceof Date) { return convertDateValue(value, convertType); } if (value instanceof byte[]) { return convertByteArrayValue(value, convertType); } if (String.class.equals(convertType)) { return value.toString(); } else { return value; } }  static Object convertValue(final Object value, final Class<?> convertType); static Object convertBigDecimalValue(final Object value, final boolean needScale, final int scale);  }
@Test public void assertConvertValue() { Object object = new Object(); assertThat(ResultSetUtil.convertValue(object, String.class), is(object.toString())); assertThat(ResultSetUtil.convertValue("1", int.class), is("1")); }
@Test public void assertConvertLocalDateTime() { Timestamp timestamp = new Timestamp(System.currentTimeMillis()); LocalDateTime dateTime = (LocalDateTime) ResultSetUtil.convertValue(timestamp, LocalDateTime.class); assertNotNull(dateTime); assertThat(dateTime.toString(), is(timestamp.toLocalDateTime().toString())); }
@Test public void assertConvertNumberValueSuccess() { assertThat(ResultSetUtil.convertValue("1", String.class), is("1")); assertThat(ResultSetUtil.convertValue(1, boolean.class), is(true)); assertThat(ResultSetUtil.convertValue((byte) 1, byte.class), is((byte) 1)); assertThat(ResultSetUtil.convertValue((short) 1, short.class), is((short) 1)); assertThat(ResultSetUtil.convertValue(new BigDecimal("1"), int.class), is(1)); assertThat(ResultSetUtil.convertValue(new BigDecimal("1"), long.class), is(1L)); assertThat(ResultSetUtil.convertValue(new BigDecimal("1"), double.class), is(1.0d)); assertThat(ResultSetUtil.convertValue(new BigDecimal("1"), float.class), is(1.0f)); assertThat(ResultSetUtil.convertValue(new BigDecimal("1"), BigDecimal.class), is(new BigDecimal("1"))); assertThat(ResultSetUtil.convertValue((short) 1, BigDecimal.class), is(new BigDecimal("1"))); assertThat(ResultSetUtil.convertValue(new Date(0L), Date.class), is(new Date(0L))); assertThat(ResultSetUtil.convertValue((short) 1, Object.class), is(Short.valueOf("1"))); assertThat(ResultSetUtil.convertValue((short) 1, String.class), is("1")); }
@Test(expected = ShardingSphereException.class) public void assertConvertNumberValueError() { ResultSetUtil.convertValue(1, Date.class); }
@Test public void assertConvertNullValue() { assertThat(ResultSetUtil.convertValue(null, boolean.class), is(false)); assertThat(ResultSetUtil.convertValue(null, byte.class), is((byte) 0)); assertThat(ResultSetUtil.convertValue(null, short.class), is((short) 0)); assertThat(ResultSetUtil.convertValue(null, int.class), is(0)); assertThat(ResultSetUtil.convertValue(null, long.class), is(0L)); assertThat(ResultSetUtil.convertValue(null, double.class), is(0.0D)); assertThat(ResultSetUtil.convertValue(null, float.class), is(0.0F)); assertThat(ResultSetUtil.convertValue(null, String.class), is((Object) null)); assertThat(ResultSetUtil.convertValue(null, Object.class), is((Object) null)); assertThat(ResultSetUtil.convertValue(null, BigDecimal.class), is((Object) null)); assertThat(ResultSetUtil.convertValue(null, Date.class), is((Object) null)); }
@Test public void assertConvertDateValueSuccess() { Date now = new Date(); assertThat(ResultSetUtil.convertValue(now, Date.class), is(now)); assertThat(ResultSetUtil.convertValue(now, java.sql.Date.class), is(now)); assertThat(ResultSetUtil.convertValue(now, Time.class), is(now)); assertThat(ResultSetUtil.convertValue(now, Timestamp.class), is(new Timestamp(now.getTime()))); assertThat(ResultSetUtil.convertValue(now, String.class), is(now.toString())); }
@Test public void assertConvertByteArrayValueSuccess() { byte[] bytesValue = {}; assertThat(ResultSetUtil.convertValue(bytesValue, byte.class), is(bytesValue)); assertThat(ResultSetUtil.convertValue(new byte[]{1}, byte.class), is((byte) 1)); assertThat(ResultSetUtil.convertValue(Shorts.toByteArray((short) 1), short.class), is((short) 1)); assertThat(ResultSetUtil.convertValue(Ints.toByteArray(1), int.class), is(1)); assertThat(ResultSetUtil.convertValue(Longs.toByteArray(1L), long.class), is(1L)); assertThat(ResultSetUtil.convertValue(Longs.toByteArray(1L), double.class), is(1.0d)); assertThat(ResultSetUtil.convertValue(Longs.toByteArray(1L), float.class), is(1.0f)); assertThat(ResultSetUtil.convertValue(Longs.toByteArray(1L), BigDecimal.class), is(new BigDecimal("1"))); }
@SneakyThrows(MalformedURLException.class) @Test public void assertConvertURLValue() { String urlString = "http: URL url = (URL) ResultSetUtil.convertValue(urlString, URL.class); assertNotNull(url); assertThat(url, is(new URL(urlString))); }
@Test(expected = ShardingSphereException.class) public void assertConvertURLValueError() { String urlString = "no-exist:apache.org"; ResultSetUtil.convertValue(urlString, URL.class); }
@Test(expected = ShardingSphereException.class) public void assertConvertDateValueError() { ResultSetUtil.convertValue(new Date(), int.class); }