Skip to content

Commit 9488fdd

Browse files
committed
FIX: change test code, spotless
1 parent 35afa46 commit 9488fdd

File tree

2 files changed

+28
-20
lines changed

2 files changed

+28
-20
lines changed

flink-cdc-connect/flink-cdc-source-connectors/flink-connector-mysql-cdc/src/main/java/org/apache/flink/cdc/connectors/mysql/source/utils/StatementUtils.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ public static long queryApproximateRowCnt(JdbcConnection jdbc, TableId tableId)
8080

8181
// PreparedStatement#setObject method will be converted to long type when handling bigint
8282
// unsigned, which poses a data overflow issue.
83-
// Therefore, we need to handle the overflow issue by converting the bigint unsigned to BigDecimal.
83+
// Therefore, we need to handle the overflow issue by converting the bigint unsigned to
84+
// BigDecimal.
8485
public static void setSafeObject(PreparedStatement ps, int parameterIndex, Object value)
8586
throws SQLException {
8687
if (value instanceof BigInteger) {

flink-cdc-connect/flink-cdc-source-connectors/flink-connector-mysql-cdc/src/test/java/org/apache/flink/cdc/connectors/mysql/source/utils/StatementUtilsTest.java

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.junit.jupiter.api.Test;
2121

2222
import java.lang.reflect.Proxy;
23+
import java.math.BigDecimal;
2324
import java.math.BigInteger;
2425
import java.sql.PreparedStatement;
2526
import java.sql.SQLException;
@@ -32,36 +33,36 @@
3233
class StatementUtilsTest {
3334

3435
@Test
35-
void testSetSafeObjectCorrectlyHandlesOverflow() throws SQLException {
36+
void testSetSafeObjectConvertsBigIntegerToBigDecimal() throws SQLException {
3637
Map<String, Object> invocationDetails = new HashMap<>();
3738
PreparedStatement psProxy = createPreparedStatementProxy(invocationDetails);
3839

39-
long overflowValue = Long.MAX_VALUE + 1L;
40-
BigInteger expectedBigInteger = new BigInteger(Long.toUnsignedString(overflowValue));
40+
// Create a BigInteger value that exceeds Long.MAX_VALUE
41+
BigInteger bigIntValue = new BigInteger("9223372036854775808"); // Long.MAX_VALUE + 1
42+
BigDecimal expectedBigDecimal = new BigDecimal(bigIntValue);
4143

4244
// Use the safe method
43-
StatementUtils.setSafeObject(psProxy, 1, overflowValue);
45+
StatementUtils.setSafeObject(psProxy, 1, bigIntValue);
4446

45-
// Assert that it correctly used setObject with the converted BigInteger value
46-
assertThat(invocationDetails.get("methodName")).isEqualTo("setObject");
47-
assertThat(invocationDetails.get("value")).isInstanceOf(BigInteger.class);
48-
assertThat(invocationDetails.get("value")).isEqualTo(expectedBigInteger);
47+
// Assert that it correctly used setBigDecimal with the converted BigDecimal value
48+
assertThat(invocationDetails.get("methodName")).isEqualTo("setBigDecimal");
49+
assertThat(invocationDetails.get("value")).isInstanceOf(BigDecimal.class);
50+
assertThat(invocationDetails.get("value")).isEqualTo(expectedBigDecimal);
4951
}
5052

5153
@Test
52-
void testDirectSetObjectFailsOnOverflow() throws SQLException {
54+
void testSetSafeObjectHandlesLargeBigIntegerValues() throws SQLException {
5355
Map<String, Object> invocationDetails = new HashMap<>();
5456
PreparedStatement psProxy = createPreparedStatementProxy(invocationDetails);
5557

56-
long overflowValue = Long.MAX_VALUE + 1L;
58+
// Test with BIGINT UNSIGNED max value
59+
BigInteger maxUnsignedBigInt = new BigInteger("18446744073709551615"); // 2^64 - 1
60+
BigDecimal expectedBigDecimal = new BigDecimal(maxUnsignedBigInt);
5761

58-
// Directly call the unsafe method on the proxy
59-
psProxy.setObject(1, overflowValue);
62+
StatementUtils.setSafeObject(psProxy, 1, maxUnsignedBigInt);
6063

61-
// Assert that it incorrectly used setObject, preserving the wrong negative long value
62-
assertThat(invocationDetails.get("methodName")).isEqualTo("setObject");
63-
assertThat(invocationDetails.get("value")).isInstanceOf(Long.class);
64-
assertThat(invocationDetails.get("value")).isEqualTo(Long.MIN_VALUE);
64+
assertThat(invocationDetails.get("methodName")).isEqualTo("setBigDecimal");
65+
assertThat(invocationDetails.get("value")).isEqualTo(expectedBigDecimal);
6566
}
6667

6768
@Test
@@ -81,11 +82,16 @@ void testSetSafeObjectHandlesRegularValues() throws SQLException {
8182
assertThat(invocationDetails.get("value")).isEqualTo("test");
8283
invocationDetails.clear();
8384

85+
// Test with an Integer
86+
StatementUtils.setSafeObject(psProxy, 3, 456);
87+
assertThat(invocationDetails.get("methodName")).isEqualTo("setObject");
88+
assertThat(invocationDetails.get("value")).isEqualTo(456);
89+
invocationDetails.clear();
90+
8491
// Test with null
85-
StatementUtils.setSafeObject(psProxy, 3, null);
92+
StatementUtils.setSafeObject(psProxy, 4, null);
8693
assertThat(invocationDetails.get("methodName")).isEqualTo("setObject");
8794
assertThat(invocationDetails.get("value")).isNull();
88-
invocationDetails.clear();
8995
}
9096

9197
private PreparedStatement createPreparedStatementProxy(Map<String, Object> invocationDetails) {
@@ -95,7 +101,8 @@ private PreparedStatement createPreparedStatementProxy(Map<String, Object> invoc
95101
new Class<?>[] {PreparedStatement.class},
96102
(proxy, method, args) -> {
97103
String methodName = method.getName();
98-
if (methodName.equals("setObject")) {
104+
if (methodName.equals("setObject")
105+
|| methodName.equals("setBigDecimal")) {
99106
invocationDetails.put("methodName", methodName);
100107
invocationDetails.put("parameterIndex", args[0]);
101108
invocationDetails.put("value", args[1]);

0 commit comments

Comments
 (0)