You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by me...@apache.org on 2021/01/24 23:10:50 UTC

[shardingsphere] branch master updated: Add more test cases for ShardingSphereDataSourceFactory (#9147)

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

menghaoran 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 5c911a0  Add more test cases for ShardingSphereDataSourceFactory (#9147)
5c911a0 is described below

commit 5c911a06c9b7c202c2c71a76d9160c77c6fe2e22
Author: Liang Zhang <te...@163.com>
AuthorDate: Mon Jan 25 07:10:16 2021 +0800

    Add more test cases for ShardingSphereDataSourceFactory (#9147)
    
    * Skip unit test when prepare IT environment
    
    * Add more test cases for ShardingSphereDataSourceFactory
    
    * Update ShardingSphereDataSourceFactoryTest
---
 .../shardingsphere-jdbc-core/pom.xml               |  7 ++++
 .../api/ShardingSphereDataSourceFactoryTest.java   | 46 +++++++---------------
 2 files changed, 22 insertions(+), 31 deletions(-)

diff --git a/shardingsphere-jdbc/shardingsphere-jdbc-core/pom.xml b/shardingsphere-jdbc/shardingsphere-jdbc-core/pom.xml
index faddd49..63085ef 100644
--- a/shardingsphere-jdbc/shardingsphere-jdbc-core/pom.xml
+++ b/shardingsphere-jdbc/shardingsphere-jdbc-core/pom.xml
@@ -109,6 +109,13 @@
         </dependency>
         
         <dependency>
+            <groupId>org.apache.shardingsphere</groupId>
+            <artifactId>shardingsphere-test-common</artifactId>
+            <version>${project.version}</version>
+            <scope>test</scope>
+        </dependency>
+        
+        <dependency>
             <groupId>com.zaxxer</groupId>
             <artifactId>HikariCP</artifactId>
         </dependency>
diff --git a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/api/ShardingSphereDataSourceFactoryTest.java b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/api/ShardingSphereDataSourceFactoryTest.java
index 32cc02a..397c22f 100644
--- a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/api/ShardingSphereDataSourceFactoryTest.java
+++ b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/api/ShardingSphereDataSourceFactoryTest.java
@@ -18,17 +18,14 @@
 package org.apache.shardingsphere.driver.api;
 
 import org.apache.shardingsphere.driver.jdbc.core.datasource.ShardingSphereDataSource;
+import org.apache.shardingsphere.infra.database.DefaultSchema;
 import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration;
 import org.apache.shardingsphere.sharding.api.config.rule.ShardingTableRuleConfiguration;
+import org.apache.shardingsphere.test.mock.MockedDataSource;
 import org.junit.Test;
-import org.mockito.ArgumentMatchers;
 
 import javax.sql.DataSource;
-import java.sql.Connection;
-import java.sql.DatabaseMetaData;
-import java.sql.ResultSet;
 import java.sql.SQLException;
-import java.sql.Statement;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.Map;
@@ -36,47 +33,34 @@ import java.util.Properties;
 
 import static org.hamcrest.CoreMatchers.is;
 import static org.junit.Assert.assertThat;
-import static org.mockito.ArgumentMatchers.anyString;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
 
 public final class ShardingSphereDataSourceFactoryTest {
     
     @Test
-    public void assertCreateDataSourceWithShardingRuleAndProperties() throws SQLException {
+    public void assertCreateDataSourceWithMultipleActualDataSources() throws SQLException {
         Properties props = new Properties();
         ShardingSphereDataSource dataSource = (ShardingSphereDataSource) ShardingSphereDataSourceFactory.createDataSource(
                 getDataSourceMap(), Collections.singleton(createShardingRuleConfiguration()), props);
         assertThat(dataSource.getMetaDataContexts().getProps().getProps(), is(props));
     }
     
-    private Map<String, DataSource> getDataSourceMap() throws SQLException {
-        DataSource dataSource = mock(DataSource.class);
-        Connection connection = mock(Connection.class);
-        DatabaseMetaData databaseMetaData = mock(DatabaseMetaData.class);
-        Statement statement = mock(Statement.class);
-        ResultSet resultSet = mock(ResultSet.class);
-        when(statement.getResultSet()).thenReturn(resultSet);
-        when(resultSet.next()).thenReturn(false);
-        when(dataSource.getConnection()).thenReturn(connection);
-        when(connection.getMetaData()).thenReturn(databaseMetaData);
-        when(connection.createStatement()).thenReturn(statement);
-        when(statement.executeQuery(anyString())).thenReturn(resultSet);
-        when(statement.getConnection()).thenReturn(connection);
-        when(statement.getConnection().getMetaData().getTables(ArgumentMatchers.any(), ArgumentMatchers.any(), ArgumentMatchers.any(), ArgumentMatchers.any())).thenReturn(resultSet);
-        when(resultSet.next()).thenReturn(false);
-        when(statement.getConnection().getMetaData().getURL()).thenReturn("jdbc:h2:mem:demo_ds;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MySQL");
-        when(statement.getConnection().getMetaData().getColumns(null, null, "table_0", "%")).thenReturn(mock(ResultSet.class));
-        when(statement.getConnection().getMetaData().getPrimaryKeys(null, null, "table_0")).thenReturn(mock(ResultSet.class));
-        when(statement.getConnection().getMetaData().getIndexInfo(null, null, "table_0", false, false)).thenReturn(mock(ResultSet.class));
-        Map<String, DataSource> result = new HashMap<>(1);
-        result.put("ds", dataSource);
+    @Test
+    public void assertCreateDataSourceWithSingleActualDataSource() throws SQLException {
+        Properties props = new Properties();
+        ShardingSphereDataSource dataSource = (ShardingSphereDataSource) ShardingSphereDataSourceFactory.createDataSource(
+                new MockedDataSource(), Collections.singleton(createShardingRuleConfiguration()), props);
+        assertThat(dataSource.getMetaDataContexts().getProps().getProps(), is(props));
+    }
+    
+    private Map<String, DataSource> getDataSourceMap() {
+        Map<String, DataSource> result = new HashMap<>(1, 1);
+        result.put(DefaultSchema.LOGIC_NAME, new MockedDataSource());
         return result;
     }
     
     private ShardingRuleConfiguration createShardingRuleConfiguration() {
         ShardingRuleConfiguration result = new ShardingRuleConfiguration();
-        result.getTables().add(new ShardingTableRuleConfiguration("logicTable", "ds.table_${0..2}"));
+        result.getTables().add(new ShardingTableRuleConfiguration("logicTable", "logic_db.table_${0..2}"));
         return result;
     }
 }