You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by zh...@apache.org on 2024/10/12 15:37:31 UTC
(shardingsphere) branch master updated: Add more test cases on ResultSetUtils (#33226)
This is an automated email from the ASF dual-hosted git repository.
zhangliang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new 590684b3e5b Add more test cases on ResultSetUtils (#33226)
590684b3e5b is described below
commit 590684b3e5b31c308e2ebbabcbf16d6cef180611
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Sat Oct 12 23:37:24 2024 +0800
Add more test cases on ResultSetUtils (#33226)
---
.../impl/driver/jdbc/type/util/ResultSetUtils.java | 120 ++++++++++-----------
.../driver/jdbc/type/util/ResultSetUtilsTest.java | 115 +++++++++-----------
2 files changed, 112 insertions(+), 123 deletions(-)
diff --git a/infra/executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/execute/result/query/impl/driver/jdbc/type/util/ResultSetUtils.java b/infra/executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/execute/result/query/impl/driver/jdbc/type/util/ResultSetUtils.java
index 969103d5aa3..2ca6e53ca93 100644
--- a/infra/executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/execute/result/query/impl/driver/jdbc/type/util/ResultSetUtils.java
+++ b/infra/executor/src/main/java/org/apache/shardingsphere/infra/executor/sql/execute/result/query/impl/driver/jdbc/type/util/ResultSetUtils.java
@@ -22,8 +22,8 @@ import com.google.common.primitives.Longs;
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;
@@ -92,46 +92,25 @@ public final class ResultSetUtils {
}
}
- 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) {
@@ -163,24 +142,11 @@ public final class ResultSetUtils {
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);
}
}
@@ -218,6 +184,10 @@ public final class ResultSetUtils {
}
}
+ 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":
@@ -260,7 +230,37 @@ public final class ResultSetUtils {
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;
}
}
diff --git a/infra/executor/src/test/java/org/apache/shardingsphere/infra/executor/sql/execute/result/query/impl/driver/jdbc/type/util/ResultSetUtilsTest.java b/infra/executor/src/test/java/org/apache/shardingsphere/infra/executor/sql/execute/result/query/impl/driver/jdbc/type/util/ResultSetUtilsTest.java
index 58ebd92c7e3..440c3e16d95 100644
--- a/infra/executor/src/test/java/org/apache/shardingsphere/infra/executor/sql/execute/result/query/impl/driver/jdbc/type/util/ResultSetUtilsTest.java
+++ b/infra/executor/src/test/java/org/apache/shardingsphere/infra/executor/sql/execute/result/query/impl/driver/jdbc/type/util/ResultSetUtilsTest.java
@@ -20,7 +20,6 @@ package org.apache.shardingsphere.infra.executor.sql.execute.result.query.impl.d
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;
@@ -28,33 +27,58 @@ 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
@@ -64,18 +88,23 @@ class ResultSetUtilsTest {
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));
@@ -92,41 +121,18 @@ class ResultSetUtilsTest {
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
@@ -142,45 +148,28 @@ class ResultSetUtilsTest {
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));
- }
}