Skip to content

Commit

Permalink
Add more test cases on ResultSetUtils (#33226)
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu authored Oct 12, 2024
1 parent aa55b97 commit 590684b
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 123 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
import com.google.common.primitives.Shorts;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.shardingsphere.infra.exception.kernel.data.UnsupportedDataTypeConversionException;
import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions;
import org.apache.shardingsphere.infra.exception.kernel.data.UnsupportedDataTypeConversionException;

import java.math.BigDecimal;
import java.math.RoundingMode;
Expand Down Expand Up @@ -92,46 +92,25 @@ public static Object convertValue(final Object value, final Class<?> convertType
}
}

private static Object convertURL(final Object value) {
try {
return new URL(value.toString());
} catch (final MalformedURLException ignored) {
throw new UnsupportedDataTypeConversionException(URL.class, value);
}
}

/**
* Convert object to BigDecimal.
*
* @param value current db object
* @param needScale need scale
* @param scale scale size
* @return big decimal
* @throws UnsupportedDataTypeConversionException unsupported data type conversion exception
*/
public static Object convertBigDecimalValue(final Object value, final boolean needScale, final int scale) {
if (null == value) {
return convertNullValue(BigDecimal.class);
}
if (BigDecimal.class == value.getClass()) {
return adjustBigDecimalResult((BigDecimal) value, needScale, scale);
}
if (value instanceof Number || value instanceof String) {
BigDecimal bigDecimal = new BigDecimal(value.toString());
return adjustBigDecimalResult(bigDecimal, needScale, scale);
}
throw new UnsupportedDataTypeConversionException(BigDecimal.class, value);
}

private static BigDecimal adjustBigDecimalResult(final BigDecimal value, final boolean needScale, final int scale) {
if (needScale) {
try {
return value.setScale(scale, RoundingMode.UNNECESSARY);
} catch (final ArithmeticException ex) {
return value.setScale(scale, RoundingMode.HALF_UP);
}
private static Object convertNullValue(final Class<?> convertType) {
switch (convertType.getName()) {
case "boolean":
return false;
case "byte":
return (byte) 0;
case "short":
return (short) 0;
case "int":
return 0;
case "long":
return 0L;
case "float":
return 0.0F;
case "double":
return 0.0D;
default:
return null;
}
return value;
}

private static Object convertLocalDateTimeValue(final LocalDateTime value, final Class<?> convertType) {
Expand Down Expand Up @@ -163,24 +142,11 @@ private static Object convertTimestampValue(final Timestamp value, final Class<?
return value;
}

private static Object convertNullValue(final Class<?> convertType) {
switch (convertType.getName()) {
case "boolean":
return false;
case "byte":
return (byte) 0;
case "short":
return (short) 0;
case "int":
return 0;
case "long":
return 0L;
case "float":
return 0.0F;
case "double":
return 0.0D;
default:
return null;
private static Object convertURL(final Object value) {
try {
return new URL(value.toString());
} catch (final MalformedURLException ignored) {
throw new UnsupportedDataTypeConversionException(URL.class, value);
}
}

Expand Down Expand Up @@ -218,6 +184,10 @@ private static Object convertNumberValue(final Object value, final Class<?> conv
}
}

private static Boolean longToBoolean(final long longVal) {
return -1L == longVal || longVal > 0L;
}

private static Object convertDateValue(final Date value, final Class<?> convertType) {
switch (convertType.getName()) {
case "java.sql.Date":
Expand Down Expand Up @@ -260,7 +230,37 @@ private static Object convertBooleanValue(final Object value) {
return 't' == firstChar || 'y' == firstChar || '1' == firstChar || "-1".equals(stringVal);
}

private static Boolean longToBoolean(final long longVal) {
return -1L == longVal || longVal > 0L;
/**
* Convert object to BigDecimal.
*
* @param value current db object
* @param needScale need scale
* @param scale scale size
* @return big decimal
* @throws UnsupportedDataTypeConversionException unsupported data type conversion exception
*/
public static Object convertBigDecimalValue(final Object value, final boolean needScale, final int scale) {
if (null == value) {
return convertNullValue(BigDecimal.class);
}
if (BigDecimal.class == value.getClass()) {
return adjustBigDecimalResult((BigDecimal) value, needScale, scale);
}
if (value instanceof Number || value instanceof String) {
BigDecimal bigDecimal = new BigDecimal(value.toString());
return adjustBigDecimalResult(bigDecimal, needScale, scale);
}
throw new UnsupportedDataTypeConversionException(BigDecimal.class, value);
}

private static BigDecimal adjustBigDecimalResult(final BigDecimal value, final boolean needScale, final int scale) {
if (needScale) {
try {
return value.setScale(scale, RoundingMode.UNNECESSARY);
} catch (final ArithmeticException ex) {
return value.setScale(scale, RoundingMode.HALF_UP);
}
}
return value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,41 +20,65 @@
import com.google.common.primitives.Ints;
import com.google.common.primitives.Longs;
import com.google.common.primitives.Shorts;
import lombok.SneakyThrows;
import org.apache.shardingsphere.infra.exception.kernel.data.UnsupportedDataTypeConversionException;
import org.junit.jupiter.api.Test;

import java.math.BigDecimal;
import java.net.MalformedURLException;
import java.net.URL;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.sql.Time;
import java.sql.Timestamp;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Month;
import java.time.OffsetDateTime;
import java.util.Date;

import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

class ResultSetUtilsTest {

@Test
void assertConvertValue() throws SQLException {
void assertConvertNullType() {
assertThrows(SQLException.class, () -> ResultSetUtils.convertValue(null, null));
}

@Test
void assertConvertObjectValue() throws SQLException {
Object object = new Object();
assertThat(ResultSetUtils.convertValue(object, String.class), is(object.toString()));
}

@Test
void assertConvertNullValue() throws SQLException {
assertFalse((boolean) ResultSetUtils.convertValue(null, boolean.class));
assertThat(ResultSetUtils.convertValue(null, byte.class), is((byte) 0));
assertThat(ResultSetUtils.convertValue(null, short.class), is((short) 0));
assertThat(ResultSetUtils.convertValue(null, int.class), is(0));
assertThat(ResultSetUtils.convertValue(null, long.class), is(0L));
assertThat(ResultSetUtils.convertValue(null, double.class), is(0.0D));
assertThat(ResultSetUtils.convertValue(null, float.class), is(0.0F));
assertThat(ResultSetUtils.convertValue(null, String.class), is((Object) null));
assertThat(ResultSetUtils.convertValue(null, Object.class), is((Object) null));
assertThat(ResultSetUtils.convertValue(null, BigDecimal.class), is((Object) null));
assertThat(ResultSetUtils.convertValue(null, Date.class), is((Object) null));
}

@Test
void assertConvertLocalDateTimeValue() throws SQLException {
LocalDateTime localDateTime = LocalDateTime.of(2021, Month.DECEMBER, 23, 19, 30);
assertThat(ResultSetUtils.convertValue(localDateTime, Object.class), is(localDateTime));
assertThat(ResultSetUtils.convertValue(localDateTime, Timestamp.class), is(Timestamp.valueOf(localDateTime)));
assertThat(ResultSetUtils.convertValue(localDateTime, String.class), is("2021-12-23T19:30"));
}

@Test
Expand All @@ -64,18 +88,23 @@ void assertConvertTimestampValue() throws SQLException {
assertThat(ResultSetUtils.convertValue(timestamp, LocalDateTime.class), is(localDateTime));
assertThat(ResultSetUtils.convertValue(timestamp, LocalDate.class), is(LocalDate.of(2021, Month.DECEMBER, 23)));
assertThat(ResultSetUtils.convertValue(timestamp, LocalTime.class), is(LocalTime.of(19, 30)));
assertThat(ResultSetUtils.convertValue(timestamp, OffsetDateTime.class), instanceOf(OffsetDateTime.class));
assertThat(ResultSetUtils.convertValue(timestamp, String.class), instanceOf(String.class));
assertThat(ResultSetUtils.convertValue(timestamp, Object.class), is(timestamp));
}

@Test
void assertConvertBooleanValue() throws SQLException {
assertFalse((boolean) ResultSetUtils.convertValue("-2", boolean.class));
assertTrue((boolean) ResultSetUtils.convertValue("1", boolean.class));
void assertConvertURLValue() throws SQLException, MalformedURLException {
assertThat(ResultSetUtils.convertValue("https://shardingsphere.apache.org/", URL.class), is(new URL("https://shardingsphere.apache.org/")));
assertThrows(UnsupportedDataTypeConversionException.class, () -> ResultSetUtils.convertValue("no-exist:shardingsphere.apache.org/", URL.class));
}

@Test
void assertConvertNumberValueSuccess() throws SQLException {
void assertConvertNumberValue() throws SQLException {
assertThat(ResultSetUtils.convertValue("1", String.class), is("1"));
assertTrue((boolean) ResultSetUtils.convertValue(-1, boolean.class));
assertTrue((boolean) ResultSetUtils.convertValue(1, boolean.class));
assertFalse((boolean) ResultSetUtils.convertValue(-2, boolean.class));
assertThat(ResultSetUtils.convertValue((byte) 1, byte.class), is((byte) 1));
assertThat(ResultSetUtils.convertValue((short) 1, short.class), is((short) 1));
assertThat(ResultSetUtils.convertValue(new BigDecimal("1"), int.class), is(1));
Expand All @@ -92,41 +121,18 @@ void assertConvertNumberValueSuccess() throws SQLException {
assertThat(ResultSetUtils.convertValue(1, Long.class), is(Long.valueOf("1")));
assertThat(ResultSetUtils.convertValue(1, Double.class), is(Double.valueOf("1")));
assertThat(ResultSetUtils.convertValue(1, Float.class), is(Float.valueOf("1")));
}

@Test
void assertConvertNumberValueError() {
assertThrows(UnsupportedDataTypeConversionException.class, () -> ResultSetUtils.convertValue(1, Date.class));
}

@Test
void assertConvertNullValue() throws SQLException {
assertFalse((boolean) ResultSetUtils.convertValue(null, boolean.class));
assertThat(ResultSetUtils.convertValue(null, byte.class), is((byte) 0));
assertThat(ResultSetUtils.convertValue(null, short.class), is((short) 0));
assertThat(ResultSetUtils.convertValue(null, int.class), is(0));
assertThat(ResultSetUtils.convertValue(null, long.class), is(0L));
assertThat(ResultSetUtils.convertValue(null, double.class), is(0.0D));
assertThat(ResultSetUtils.convertValue(null, float.class), is(0.0F));
assertThat(ResultSetUtils.convertValue(null, String.class), is((Object) null));
assertThat(ResultSetUtils.convertValue(null, Object.class), is((Object) null));
assertThat(ResultSetUtils.convertValue(null, BigDecimal.class), is((Object) null));
assertThat(ResultSetUtils.convertValue(null, Date.class), is((Object) null));
}

@Test
void assertConvertNullType() {
assertThrows(SQLException.class, () -> ResultSetUtils.convertValue(null, null));
}

@Test
void assertConvertDateValueSuccess() throws SQLException {
void assertConvertDateValue() throws SQLException {
Date now = new Date();
assertThat(ResultSetUtils.convertValue(now, Date.class), is(now));
assertThat(ResultSetUtils.convertValue(now, java.sql.Date.class), is(now));
assertThat(ResultSetUtils.convertValue(now, Time.class), is(now));
assertThat(ResultSetUtils.convertValue(now, Timestamp.class), is(new Timestamp(now.getTime())));
assertThat(ResultSetUtils.convertValue(now, String.class), is(now.toString()));
assertThrows(UnsupportedDataTypeConversionException.class, () -> ResultSetUtils.convertValue(new Date(), int.class));
}

@Test
Expand All @@ -142,45 +148,28 @@ void assertConvertByteArrayValueSuccess() throws SQLException {
assertThat(ResultSetUtils.convertValue(Longs.toByteArray(1L), BigDecimal.class), is(new BigDecimal("1")));
}

@SneakyThrows(MalformedURLException.class)
@Test
void assertConvertURLValue() throws SQLException {
String urlString = "https://shardingsphere.apache.org/";
URL url = (URL) ResultSetUtils.convertValue(urlString, URL.class);
assertThat(url, is(new URL(urlString)));
void assertConvertBooleanValue() throws SQLException {
assertTrue((boolean) ResultSetUtils.convertValue(true, boolean.class));
assertFalse((boolean) ResultSetUtils.convertValue("", boolean.class));
assertFalse((boolean) ResultSetUtils.convertValue("-2", boolean.class));
assertTrue((boolean) ResultSetUtils.convertValue("1", boolean.class));
assertTrue((boolean) ResultSetUtils.convertValue("t", boolean.class));
assertTrue((boolean) ResultSetUtils.convertValue("y", boolean.class));
}

@Test
void assertConvertURLValueError() {
String urlString = "no-exist:shardingsphere.apache.org/";
assertThrows(UnsupportedDataTypeConversionException.class, () -> ResultSetUtils.convertValue(urlString, URL.class));
void assertConvertValueWithMismatchedValueAndConvertType() {
assertThrows(SQLFeatureNotSupportedException.class, () -> ResultSetUtils.convertValue(new ResultSetUtilsTest(), Date.class));
}

@Test
void assertConvertBigDecimalValue() {
BigDecimal bigDecimal = (BigDecimal) ResultSetUtils.convertBigDecimalValue("12", false, 0);
assertThat(bigDecimal, is(BigDecimal.valueOf(12L)));
}

@Test
void assertConvertBigDecimalValueNull() {
BigDecimal bigDecimal = (BigDecimal) ResultSetUtils.convertBigDecimalValue(null, false, 0);
assertNull(bigDecimal);
}

@Test
void assertConvertBigDecimalValueWithScale() {
BigDecimal bigDecimal = (BigDecimal) ResultSetUtils.convertBigDecimalValue("12.243", true, 2);
assertThat(bigDecimal, is(BigDecimal.valueOf(12.24)));
}

@Test
void assertConvertBigDecimalValueError() {
assertThat(ResultSetUtils.convertBigDecimalValue(12, false, 0), is(BigDecimal.valueOf(12L)));
assertThat(ResultSetUtils.convertBigDecimalValue("12", false, 0), is(BigDecimal.valueOf(12L)));
assertThat(ResultSetUtils.convertBigDecimalValue(BigDecimal.valueOf(12), false, 0), is(BigDecimal.valueOf(12)));
assertNull(ResultSetUtils.convertBigDecimalValue(null, false, 0));
assertThat(ResultSetUtils.convertBigDecimalValue("12.243", true, 2), is(BigDecimal.valueOf(12.24)));
assertThrows(UnsupportedDataTypeConversionException.class, () -> ResultSetUtils.convertBigDecimalValue(new Date(), true, 2));
}

@Test
void assertConvertDateValueError() {
assertThrows(UnsupportedDataTypeConversionException.class, () -> ResultSetUtils.convertValue(new Date(), int.class));
}
}

0 comments on commit 590684b

Please sign in to comment.