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 2496881168f Add test cases on MySQLIncrementalPositionManager (#33420)
2496881168f is described below

commit 2496881168f08f979e71255f380e075ccba1d6d8
Author: Liang Zhang <[email protected]>
AuthorDate: Sun Oct 27 14:05:30 2024 +0800

    Add test cases on MySQLIncrementalPositionManager (#33420)
---
 .../position/MySQLIncrementalPositionManager.java  |  7 +---
 .../MySQLIncrementalPositionManagerTest.java       | 44 +++++++++++++---------
 2 files changed, 28 insertions(+), 23 deletions(-)

diff --git 
a/kernel/data-pipeline/dialect/mysql/src/main/java/org/apache/shardingsphere/data/pipeline/mysql/ingest/incremental/binlog/position/MySQLIncrementalPositionManager.java
 
b/kernel/data-pipeline/dialect/mysql/src/main/java/org/apache/shardingsphere/data/pipeline/mysql/ingest/incremental/binlog/position/MySQLIncrementalPositionManager.java
index 9d9ceb665be..d6f35c375e0 100644
--- 
a/kernel/data-pipeline/dialect/mysql/src/main/java/org/apache/shardingsphere/data/pipeline/mysql/ingest/incremental/binlog/position/MySQLIncrementalPositionManager.java
+++ 
b/kernel/data-pipeline/dialect/mysql/src/main/java/org/apache/shardingsphere/data/pipeline/mysql/ingest/incremental/binlog/position/MySQLIncrementalPositionManager.java
@@ -40,13 +40,8 @@ public final class MySQLIncrementalPositionManager 
implements DialectIncremental
     
     @Override
     public MySQLBinlogPosition init(final DataSource dataSource, final String 
slotNameSuffix) throws SQLException {
-        try (Connection connection = dataSource.getConnection()) {
-            return getBinlogPosition(connection);
-        }
-    }
-    
-    private MySQLBinlogPosition getBinlogPosition(final Connection connection) 
throws SQLException {
         try (
+                Connection connection = dataSource.getConnection();
                 PreparedStatement preparedStatement = 
connection.prepareStatement("SHOW MASTER STATUS");
                 ResultSet resultSet = preparedStatement.executeQuery()) {
             resultSet.next();
diff --git 
a/kernel/data-pipeline/dialect/mysql/src/test/java/org/apache/shardingsphere/data/pipeline/mysql/ingest/incremental/binlog/position/MySQLIncrementalPositionManagerTest.java
 
b/kernel/data-pipeline/dialect/mysql/src/test/java/org/apache/shardingsphere/data/pipeline/mysql/ingest/incremental/binlog/position/MySQLIncrementalPositionManagerTest.java
index 7e52691966f..522f56cd6e5 100644
--- 
a/kernel/data-pipeline/dialect/mysql/src/test/java/org/apache/shardingsphere/data/pipeline/mysql/ingest/incremental/binlog/position/MySQLIncrementalPositionManagerTest.java
+++ 
b/kernel/data-pipeline/dialect/mysql/src/test/java/org/apache/shardingsphere/data/pipeline/mysql/ingest/incremental/binlog/position/MySQLIncrementalPositionManagerTest.java
@@ -17,11 +17,12 @@
 
 package 
org.apache.shardingsphere.data.pipeline.mysql.ingest.incremental.binlog.position;
 
-import org.junit.jupiter.api.BeforeEach;
+import 
org.apache.shardingsphere.data.pipeline.core.ingest.position.DialectIncrementalPositionManager;
+import 
org.apache.shardingsphere.infra.database.core.spi.DatabaseTypedSPILoader;
+import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
+import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
+import org.apache.shardingsphere.test.fixture.jdbc.MockedDataSource;
 import org.junit.jupiter.api.Test;
-import org.junit.jupiter.api.extension.ExtendWith;
-import org.mockito.Mock;
-import org.mockito.junit.jupiter.MockitoExtension;
 
 import javax.sql.DataSource;
 import java.sql.Connection;
@@ -31,37 +32,46 @@ import java.sql.SQLException;
 
 import static org.hamcrest.CoreMatchers.is;
 import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.jupiter.api.Assertions.assertThrows;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
 
-@ExtendWith(MockitoExtension.class)
 class MySQLIncrementalPositionManagerTest {
     
     private static final String LOG_FILE_NAME = "binlog-000001";
     
     private static final long LOG_POSITION = 4L;
     
-    @Mock(extraInterfaces = AutoCloseable.class)
-    private DataSource dataSource;
+    private final DatabaseType databaseType = 
TypedSPILoader.getService(DatabaseType.class, "MySQL");
     
-    @Mock
-    private Connection connection;
+    private final DialectIncrementalPositionManager incrementalPositionManager 
= DatabaseTypedSPILoader.getService(DialectIncrementalPositionManager.class, 
databaseType);
     
-    @BeforeEach
-    void setUp() throws SQLException {
-        when(dataSource.getConnection()).thenReturn(connection);
-        PreparedStatement positionStatement = mockPositionStatement();
-        when(connection.prepareStatement("SHOW MASTER 
STATUS")).thenReturn(positionStatement);
+    @Test
+    void assertInitWithData() {
+        MySQLBinlogPosition actual = (MySQLBinlogPosition) 
incrementalPositionManager.init("binlog-000001#4");
+        assertThat(actual.getFilename(), is(LOG_FILE_NAME));
+        assertThat(actual.getPosition(), is(LOG_POSITION));
+    }
+    
+    @Test
+    void assertInitWithDataFailed() {
+        assertThrows(IllegalArgumentException.class, () -> 
incrementalPositionManager.init("binlog-000001"));
     }
     
     @Test
-    void assertGetCurrentPosition() throws SQLException {
-        MySQLIncrementalPositionManager incrementalPositionManager = new 
MySQLIncrementalPositionManager();
-        MySQLBinlogPosition actual = 
incrementalPositionManager.init(dataSource, "");
+    void assertInitWithDataSource() throws SQLException {
+        MySQLBinlogPosition actual = (MySQLBinlogPosition) 
incrementalPositionManager.init(createDataSource(), "");
         assertThat(actual.getFilename(), is(LOG_FILE_NAME));
         assertThat(actual.getPosition(), is(LOG_POSITION));
     }
     
+    DataSource createDataSource() throws SQLException {
+        Connection connection = mock(Connection.class);
+        PreparedStatement positionStatement = mockPositionStatement();
+        when(connection.prepareStatement("SHOW MASTER 
STATUS")).thenReturn(positionStatement);
+        return new MockedDataSource(connection);
+    }
+    
     private PreparedStatement mockPositionStatement() throws SQLException {
         PreparedStatement result = mock(PreparedStatement.class);
         ResultSet resultSet = mock(ResultSet.class);

Reply via email to