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/18 15:41:27 UTC

(shardingsphere) branch master updated: Add more test cases on PipelineDataSourceCheckEngine (#33310)

This is an automated email from the ASF dual-hosted git repository.

zhaojinchao 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 04c321e1f35 Add more test cases on PipelineDataSourceCheckEngine (#33310)
04c321e1f35 is described below

commit 04c321e1f355a6c190a6d9f376c426ceda05c098
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Fri Oct 18 23:41:21 2024 +0800

    Add more test cases on PipelineDataSourceCheckEngine (#33310)
---
 .../core/checker/PipelineDataSourceCheckEngine.java   | 15 ++++-----------
 .../PipelineDataSourceCheckEngineTest.java            | 19 ++++++++++++++++---
 2 files changed, 20 insertions(+), 14 deletions(-)

diff --git a/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/checker/PipelineDataSourceCheckEngine.java b/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/checker/PipelineDataSourceCheckEngine.java
index 2554fe837c0..66c2137a016 100644
--- a/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/checker/PipelineDataSourceCheckEngine.java
+++ b/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/checker/PipelineDataSourceCheckEngine.java
@@ -40,15 +40,12 @@ import java.util.Collection;
  */
 public final class PipelineDataSourceCheckEngine {
     
-    private final DialectDatabasePrivilegeChecker privilegeChecker;
-    
-    private final DialectPipelineDatabaseVariableChecker variableChecker;
+    private final DatabaseType databaseType;
     
     private final PipelinePrepareSQLBuilder sqlBuilder;
     
     public PipelineDataSourceCheckEngine(final DatabaseType databaseType) {
-        privilegeChecker = DatabaseTypedSPILoader.findService(DialectDatabasePrivilegeChecker.class, databaseType).orElse(null);
-        variableChecker = DatabaseTypedSPILoader.findService(DialectPipelineDatabaseVariableChecker.class, databaseType).orElse(null);
+        this.databaseType = databaseType;
         sqlBuilder = new PipelinePrepareSQLBuilder(databaseType);
     }
     
@@ -75,12 +72,8 @@ public final class PipelineDataSourceCheckEngine {
      */
     public void checkSourceDataSources(final Collection<DataSource> dataSources) {
         checkConnection(dataSources);
-        if (null != privilegeChecker) {
-            dataSources.forEach(each -> privilegeChecker.check(each, PrivilegeCheckType.PIPELINE));
-        }
-        if (null != variableChecker) {
-            dataSources.forEach(variableChecker::check);
-        }
+        DatabaseTypedSPILoader.findService(DialectDatabasePrivilegeChecker.class, databaseType).ifPresent(optional -> dataSources.forEach(each -> optional.check(each, PrivilegeCheckType.PIPELINE)));
+        DatabaseTypedSPILoader.findService(DialectPipelineDatabaseVariableChecker.class, databaseType).ifPresent(optional -> dataSources.forEach(optional::check));
     }
     
     /**
diff --git a/kernel/data-pipeline/core/src/test/java/org/apache/shardingsphere/data/pipeline/core/preparer/datasource/PipelineDataSourceCheckEngineTest.java b/kernel/data-pipeline/core/src/test/java/org/apache/shardingsphere/data/pipeline/core/checker/PipelineDataSourceCheckEngineTest.java
similarity index 84%
rename from kernel/data-pipeline/core/src/test/java/org/apache/shardingsphere/data/pipeline/core/preparer/datasource/PipelineDataSourceCheckEngineTest.java
rename to kernel/data-pipeline/core/src/test/java/org/apache/shardingsphere/data/pipeline/core/checker/PipelineDataSourceCheckEngineTest.java
index 024fa7ae7b7..1c513940779 100644
--- a/kernel/data-pipeline/core/src/test/java/org/apache/shardingsphere/data/pipeline/core/preparer/datasource/PipelineDataSourceCheckEngineTest.java
+++ b/kernel/data-pipeline/core/src/test/java/org/apache/shardingsphere/data/pipeline/core/checker/PipelineDataSourceCheckEngineTest.java
@@ -15,9 +15,8 @@
  * limitations under the License.
  */
 
-package org.apache.shardingsphere.data.pipeline.core.preparer.datasource;
+package org.apache.shardingsphere.data.pipeline.core.checker;
 
-import org.apache.shardingsphere.data.pipeline.core.checker.PipelineDataSourceCheckEngine;
 import org.apache.shardingsphere.data.pipeline.core.exception.job.PrepareJobWithTargetTableNotEmptyException;
 import org.apache.shardingsphere.data.pipeline.core.importer.ImporterConfiguration;
 import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
@@ -39,6 +38,7 @@ import java.util.Collection;
 import java.util.Collections;
 import java.util.LinkedList;
 
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
 import static org.junit.jupiter.api.Assertions.assertThrows;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.verify;
@@ -83,6 +83,13 @@ class PipelineDataSourceCheckEngineTest {
         assertThrows(SQLWrapperException.class, () -> pipelineDataSourceCheckEngine.checkConnection(dataSources));
     }
     
+    @Test
+    void assertCheckSourceDataSources() throws SQLException {
+        when(dataSource.getConnection()).thenReturn(connection);
+        pipelineDataSourceCheckEngine.checkSourceDataSources(dataSources);
+        verify(dataSource).getConnection();
+    }
+    
     @Test
     void assertCheckTargetDataSources() throws SQLException {
         when(dataSource.getConnection()).thenReturn(connection);
@@ -90,7 +97,7 @@ class PipelineDataSourceCheckEngineTest {
         when(preparedStatement.executeQuery()).thenReturn(resultSet);
         ImporterConfiguration importerConfig = mock(ImporterConfiguration.class);
         when(importerConfig.getQualifiedTables()).thenReturn(Collections.singleton(new CaseInsensitiveQualifiedTable(null, "t_order")));
-        pipelineDataSourceCheckEngine.checkTargetDataSources(dataSources, importerConfig);
+        assertDoesNotThrow(() -> pipelineDataSourceCheckEngine.checkTargetDataSources(dataSources, importerConfig));
     }
     
     @Test
@@ -103,4 +110,10 @@ class PipelineDataSourceCheckEngineTest {
         when(importerConfig.getQualifiedTables()).thenReturn(Collections.singleton(new CaseInsensitiveQualifiedTable(null, "t_order")));
         assertThrows(PrepareJobWithTargetTableNotEmptyException.class, () -> pipelineDataSourceCheckEngine.checkTargetDataSources(dataSources, importerConfig));
     }
+    
+    @Test
+    void assertCheckTargetDataSourcesWhenSQLExceptionThrown() throws SQLException {
+        when(dataSource.getConnection()).thenThrow(new SQLException(""));
+        assertThrows(SQLWrapperException.class, () -> pipelineDataSourceCheckEngine.checkTargetDataSources(dataSources, mock(ImporterConfiguration.class)));
+    }
 }