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 2022/06/14 02:23:45 UTC

[shardingsphere] branch master updated: Remove databaseType input param from SingleTableRule's constructor (#18345)

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 97e574a702f Remove databaseType input param from SingleTableRule's constructor (#18345)
97e574a702f is described below

commit 97e574a702f6205833fffd1cf1b0d5d1ee0b8a8d
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Tue Jun 14 10:23:40 2022 +0800

    Remove databaseType input param from SingleTableRule's constructor (#18345)
    
    * Refactor SingleTableRuleBuilderTest
    
    * Remove databaseType input param from SingleTableRule's constructor
    
    * Fix test case
---
 .../engine/fixture/AbstractRoutingEngineTest.java  |  42 +++----
 .../shardingsphere-single-table-core/pom.xml       |   7 ++
 .../singletable/rule/SingleTableRule.java          |   6 +-
 .../rule/builder/SingleTableRuleBuilder.java       |   3 +-
 .../metadata/SingleTableSchemaBuilderTest.java     |  26 ++--
 .../route/SingleTableSQLRouterTest.java            | 132 +++++++++------------
 ...ingleTableDatabaseBroadcastRouteEngineTest.java |  21 ++--
 .../engine/SingleTableStandardRouteEngineTest.java |  53 +++++----
 .../singletable/rule/SingleTableRuleTest.java      |  73 ++++++------
 .../rule/builder/SingleTableRuleBuilderTest.java   |  22 ++--
 10 files changed, 190 insertions(+), 195 deletions(-)

diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/route/engine/fixture/AbstractRoutingEngineTest.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/route/engine/fixture/AbstractRoutingEngineTest.java
index fbe534f71fd..64e320276de 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/route/engine/fixture/AbstractRoutingEngineTest.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/route/engine/fixture/AbstractRoutingEngineTest.java
@@ -17,11 +17,11 @@
 
 package org.apache.shardingsphere.sharding.route.engine.fixture;
 
+import lombok.SneakyThrows;
 import org.apache.shardingsphere.infra.binder.statement.SQLStatementContext;
 import org.apache.shardingsphere.infra.config.algorithm.ShardingSphereAlgorithmConfiguration;
 import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
 import org.apache.shardingsphere.infra.database.DefaultDatabase;
-import org.apache.shardingsphere.infra.database.type.DatabaseType;
 import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
 import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration;
 import org.apache.shardingsphere.sharding.api.config.rule.ShardingTableRuleConfiguration;
@@ -35,19 +35,23 @@ import org.apache.shardingsphere.sharding.route.engine.condition.value.ShardingC
 import org.apache.shardingsphere.sharding.rule.ShardingRule;
 import org.apache.shardingsphere.singletable.config.SingleTableRuleConfiguration;
 import org.apache.shardingsphere.singletable.rule.SingleTableRule;
+import org.apache.shardingsphere.test.mock.MockedDataSource;
 
 import javax.sql.DataSource;
-import java.util.ArrayList;
+import java.sql.Connection;
+import java.sql.SQLException;
 import java.util.Arrays;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.HashMap;
+import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
 import java.util.Properties;
 
 import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
 import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
 
 public abstract class AbstractRoutingEngineTest {
     
@@ -200,7 +204,7 @@ public abstract class AbstractRoutingEngineTest {
     }
     
     protected final ShardingConditions createShardingConditions(final String tableName) {
-        List<ShardingCondition> result = new ArrayList<>(1);
+        List<ShardingCondition> result = new LinkedList<>();
         ShardingConditionValue shardingConditionValue1 = new ListShardingConditionValue<>("user_id", tableName, Collections.singleton(1L));
         ShardingConditionValue shardingConditionValue2 = new ListShardingConditionValue<>("order_id", tableName, Collections.singleton(1L));
         ShardingCondition shardingCondition = new ShardingCondition();
@@ -211,7 +215,7 @@ public abstract class AbstractRoutingEngineTest {
     }
     
     protected final ShardingConditions createErrorShardingConditions(final String tableName) {
-        List<ShardingCondition> result = new ArrayList<>(1);
+        List<ShardingCondition> result = new LinkedList<>();
         ShardingConditionValue shardingConditionValue1 = new ListShardingConditionValue<>("user_id", tableName, Collections.singleton(1L));
         ShardingConditionValue shardingConditionValue2 = new ListShardingConditionValue<>("order_id", tableName, Collections.singleton(2L));
         ShardingCondition shardingCondition = new ShardingCondition();
@@ -222,7 +226,7 @@ public abstract class AbstractRoutingEngineTest {
     }
     
     protected final ShardingConditions createIntervalShardingConditions(final String tableName) {
-        List<ShardingCondition> result = new ArrayList<>(1);
+        List<ShardingCondition> result = new LinkedList<>();
         ShardingConditionValue shardingConditionValue = new ListShardingConditionValue<>("create_at", tableName, Collections.singleton("2021-01-01 20:20:20"));
         ShardingCondition shardingCondition = new ShardingCondition();
         shardingCondition.getValues().add(shardingConditionValue);
@@ -234,25 +238,21 @@ public abstract class AbstractRoutingEngineTest {
         return Arrays.asList("ds_0", "ds_1");
     }
     
-    private Map<String, DataSource> createDataSourceMapWithMain() {
-        Map<String, DataSource> result = new HashMap<>(3, 1);
-        result.put("ds_0", mock(DataSource.class, RETURNS_DEEP_STUBS));
-        result.put("ds_1", mock(DataSource.class, RETURNS_DEEP_STUBS));
-        result.put("main", mock(DataSource.class, RETURNS_DEEP_STUBS));
+    protected final SingleTableRule createSingleTableRule(final Collection<ShardingSphereRule> rules) {
+        Map<String, DataSource> dataSourceMap = createDataSourceMap();
+        SingleTableRule result = new SingleTableRule(new SingleTableRuleConfiguration(), DefaultDatabase.LOGIC_NAME, dataSourceMap, rules, new ConfigurationProperties(new Properties()));
+        result.put(dataSourceMap.keySet().iterator().next(), DefaultDatabase.LOGIC_NAME, "t_category");
         return result;
     }
     
-    /**
-     * Create single table rule.
-     * 
-     * @param rules rules
-     * @return single table rule
-     */
-    protected SingleTableRule createSingleTableRule(final Collection<ShardingSphereRule> rules) {
-        Map<String, DataSource> dataSourceMap = createDataSourceMapWithMain();
-        SingleTableRule result = new SingleTableRule(new SingleTableRuleConfiguration(),
-                DefaultDatabase.LOGIC_NAME, mock(DatabaseType.class), dataSourceMap, rules, new ConfigurationProperties(new Properties()));
-        result.put(dataSourceMap.keySet().iterator().next(), DefaultDatabase.LOGIC_NAME, "t_category");
+    @SneakyThrows(SQLException.class)
+    private Map<String, DataSource> createDataSourceMap() {
+        Map<String, DataSource> result = new HashMap<>(3, 1);
+        Connection connection = mock(Connection.class, RETURNS_DEEP_STUBS);
+        when(connection.getMetaData().getURL()).thenReturn("jdbc:h2:mem:db");
+        result.put("ds_0", new MockedDataSource(connection));
+        result.put("ds_1", new MockedDataSource(connection));
+        result.put("main", new MockedDataSource(connection));
         return result;
     }
 }
diff --git a/shardingsphere-kernel/shardingsphere-single-table/shardingsphere-single-table-core/pom.xml b/shardingsphere-kernel/shardingsphere-single-table/shardingsphere-single-table-core/pom.xml
index 6013da4b09c..ff50186fd37 100644
--- a/shardingsphere-kernel/shardingsphere-single-table/shardingsphere-single-table-core/pom.xml
+++ b/shardingsphere-kernel/shardingsphere-single-table/shardingsphere-single-table-core/pom.xml
@@ -39,5 +39,12 @@
             <artifactId>shardingsphere-infra-route</artifactId>
             <version>${project.version}</version>
         </dependency>
+        
+        <dependency>
+            <groupId>org.apache.shardingsphere</groupId>
+            <artifactId>shardingsphere-test-common</artifactId>
+            <version>${project.version}</version>
+            <scope>test</scope>
+        </dependency>
     </dependencies>
 </project>
diff --git a/shardingsphere-kernel/shardingsphere-single-table/shardingsphere-single-table-core/src/main/java/org/apache/shardingsphere/singletable/rule/SingleTableRule.java b/shardingsphere-kernel/shardingsphere-single-table/shardingsphere-single-table-core/src/main/java/org/apache/shardingsphere/singletable/rule/SingleTableRule.java
index 20c2e7e765e..8dcc4ec8e0b 100644
--- a/shardingsphere-kernel/shardingsphere-single-table/shardingsphere-single-table-core/src/main/java/org/apache/shardingsphere/singletable/rule/SingleTableRule.java
+++ b/shardingsphere-kernel/shardingsphere-single-table/shardingsphere-single-table-core/src/main/java/org/apache/shardingsphere/singletable/rule/SingleTableRule.java
@@ -19,7 +19,7 @@ package org.apache.shardingsphere.singletable.rule;
 
 import lombok.Getter;
 import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
-import org.apache.shardingsphere.infra.database.type.DatabaseType;
+import org.apache.shardingsphere.infra.database.type.DatabaseTypeEngine;
 import org.apache.shardingsphere.infra.datanode.DataNode;
 import org.apache.shardingsphere.infra.distsql.constant.ExportableConstants;
 import org.apache.shardingsphere.infra.metadata.database.schema.QualifiedTable;
@@ -65,12 +65,12 @@ public final class SingleTableRule implements DatabaseRule, DataNodeContainedRul
     
     private final Map<String, String> tableNames;
     
-    public SingleTableRule(final SingleTableRuleConfiguration config, final String databaseName, final DatabaseType databaseType,
+    public SingleTableRule(final SingleTableRuleConfiguration config, final String databaseName,
                            final Map<String, DataSource> dataSourceMap, final Collection<ShardingSphereRule> builtRules, final ConfigurationProperties props) {
         defaultDataSource = config.getDefaultDataSource().orElse(null);
         Map<String, DataSource> aggregateDataSourceMap = getAggregateDataSourceMap(dataSourceMap, builtRules);
         dataSourceNames = aggregateDataSourceMap.keySet();
-        singleTableDataNodes = SingleTableDataNodeLoader.load(databaseName, databaseType, aggregateDataSourceMap, getExcludedTables(builtRules), props);
+        singleTableDataNodes = SingleTableDataNodeLoader.load(databaseName, DatabaseTypeEngine.getDatabaseType(dataSourceMap.values()), aggregateDataSourceMap, getExcludedTables(builtRules), props);
         tableNames = singleTableDataNodes.entrySet().stream().collect(Collectors.toConcurrentMap(Entry::getKey, entry -> entry.getValue().iterator().next().getTableName()));
     }
     
diff --git a/shardingsphere-kernel/shardingsphere-single-table/shardingsphere-single-table-core/src/main/java/org/apache/shardingsphere/singletable/rule/builder/SingleTableRuleBuilder.java b/shardingsphere-kernel/shardingsphere-single-table/shardingsphere-single-table-core/src/main/java/org/apache/shardingsphere/singletable/rule/builder/SingleTableRuleBuilder.java
index c666efb4153..2742e271a19 100644
--- a/shardingsphere-kernel/shardingsphere-single-table/shardingsphere-single-table-core/src/main/java/org/apache/shardingsphere/singletable/rule/builder/SingleTableRuleBuilder.java
+++ b/shardingsphere-kernel/shardingsphere-single-table/shardingsphere-single-table-core/src/main/java/org/apache/shardingsphere/singletable/rule/builder/SingleTableRuleBuilder.java
@@ -18,7 +18,6 @@
 package org.apache.shardingsphere.singletable.rule.builder;
 
 import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
-import org.apache.shardingsphere.infra.database.type.DatabaseTypeEngine;
 import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
 import org.apache.shardingsphere.infra.rule.builder.schema.DatabaseRuleBuilder;
 import org.apache.shardingsphere.singletable.config.SingleTableRuleConfiguration;
@@ -37,7 +36,7 @@ public final class SingleTableRuleBuilder implements DatabaseRuleBuilder<SingleT
     @Override
     public SingleTableRule build(final SingleTableRuleConfiguration config, final String databaseName,
                                  final Map<String, DataSource> dataSources, final Collection<ShardingSphereRule> builtRules, final ConfigurationProperties props) {
-        return new SingleTableRule(config, databaseName, DatabaseTypeEngine.getDatabaseType(dataSources.values()), dataSources, builtRules, props);
+        return new SingleTableRule(config, databaseName, dataSources, builtRules, props);
     }
     
     @Override
diff --git a/shardingsphere-kernel/shardingsphere-single-table/shardingsphere-single-table-core/src/test/java/org/apache/shardingsphere/singletable/metadata/SingleTableSchemaBuilderTest.java b/shardingsphere-kernel/shardingsphere-single-table/shardingsphere-single-table-core/src/test/java/org/apache/shardingsphere/singletable/metadata/SingleTableSchemaBuilderTest.java
index 26af850ad6d..7282f922b17 100644
--- a/shardingsphere-kernel/shardingsphere-single-table/shardingsphere-single-table-core/src/test/java/org/apache/shardingsphere/singletable/metadata/SingleTableSchemaBuilderTest.java
+++ b/shardingsphere-kernel/shardingsphere-single-table/shardingsphere-single-table-core/src/test/java/org/apache/shardingsphere/singletable/metadata/SingleTableSchemaBuilderTest.java
@@ -17,7 +17,6 @@
 
 package org.apache.shardingsphere.singletable.metadata;
 
-import lombok.SneakyThrows;
 import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
 import org.apache.shardingsphere.infra.database.DefaultDatabase;
 import org.apache.shardingsphere.infra.database.type.DatabaseType;
@@ -28,6 +27,7 @@ import org.apache.shardingsphere.infra.metadata.database.schema.decorator.model.
 import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
 import org.apache.shardingsphere.singletable.config.SingleTableRuleConfiguration;
 import org.apache.shardingsphere.singletable.rule.SingleTableRule;
+import org.apache.shardingsphere.test.mock.MockedDataSource;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.mockito.Answers;
@@ -65,25 +65,22 @@ public final class SingleTableSchemaBuilderTest {
     @Mock(answer = Answers.RETURNS_DEEP_STUBS)
     private DatabaseType databaseType;
     
-    @Mock(answer = Answers.RETURNS_DEEP_STUBS)
-    private DataSource dataSource;
-    
     @Test
     public void assertBuildOfSingleTables() throws SQLException {
         Connection connection = mock(Connection.class, RETURNS_DEEP_STUBS);
-        when(dataSource.getConnection()).thenReturn(connection);
-        Collection<ShardingSphereRule> rules = Collections.singletonList(mockSingleTableRuleLoad(connection));
+        when(connection.getMetaData().getURL()).thenReturn("jdbc:h2:mem:db");
+        DataSource dataSource = new MockedDataSource(connection);
+        Collection<ShardingSphereRule> rules = Collections.singleton(mockSingleTableRuleLoad(dataSource, connection));
         mockSQLLoad(connection);
-        GenericSchemaBuilderMaterials materials = new GenericSchemaBuilderMaterials(databaseType, databaseType,
-                Collections.singletonMap(DefaultDatabase.LOGIC_NAME, dataSource), rules, new ConfigurationProperties(new Properties()), DefaultDatabase.LOGIC_NAME);
+        GenericSchemaBuilderMaterials materials = new GenericSchemaBuilderMaterials(
+                databaseType, databaseType, Collections.singletonMap(DefaultDatabase.LOGIC_NAME, dataSource), rules, new ConfigurationProperties(new Properties()), DefaultDatabase.LOGIC_NAME);
         Map<String, ShardingSphereSchema> actual = GenericSchemaBuilder.build(Arrays.asList(singleTableNames), materials);
         assertThat(actual.size(), is(1));
         assertThat(actual.values().iterator().next().getTables().size(), is(2));
         assertActualOfSingleTables(actual.values().iterator().next().getTables().values());
     }
     
-    @SneakyThrows(SQLException.class)
-    private void mockSQLLoad(final Connection connection) {
+    private void mockSQLLoad(final Connection connection) throws SQLException {
         when(databaseType.formatTableNamePattern("single_table1")).thenReturn("single_table1");
         when(databaseType.getQuoteCharacter().wrap("single_table1")).thenReturn("single_table1");
         when(databaseType.formatTableNamePattern("single_table2")).thenReturn("single_table2");
@@ -108,18 +105,17 @@ public final class SingleTableSchemaBuilderTest {
         when(connection.getMetaData().getColumns(any(), any(), eq("single_table2"), eq("%"))).thenReturn(column2);
     }
     
-    @SneakyThrows(SQLException.class)
-    private SingleTableRule mockSingleTableRuleLoad(final Connection connection) {
+    private SingleTableRule mockSingleTableRuleLoad(final DataSource dataSource, final Connection connection) throws SQLException {
         ResultSet resultSet = mock(ResultSet.class);
         when(connection.getMetaData().getTables(any(), any(), eq(null), any())).thenReturn(resultSet);
         when(resultSet.next()).thenReturn(true, true, true, true, true, true, false);
         when(resultSet.getString(TABLE_NAME)).thenReturn(singleTableNames[0], singleTableNames[1]);
-        return new SingleTableRule(new SingleTableRuleConfiguration(), DefaultDatabase.LOGIC_NAME, databaseType, Collections.singletonMap("logic_db", dataSource),
-                Collections.emptyList(), new ConfigurationProperties(new Properties()));
+        return new SingleTableRule(new SingleTableRuleConfiguration(),
+                DefaultDatabase.LOGIC_NAME, Collections.singletonMap("logic_db", dataSource), Collections.emptyList(), new ConfigurationProperties(new Properties()));
     }
     
     private void assertActualOfSingleTables(final Collection<ShardingSphereTable> actual) {
-        Map<String, ShardingSphereTable> tables = actual.stream().collect(Collectors.toMap(ShardingSphereTable::getName, v -> v));
+        Map<String, ShardingSphereTable> tables = actual.stream().collect(Collectors.toMap(ShardingSphereTable::getName, value -> value));
         assertTrue(tables.containsKey(singleTableNames[0]));
         assertFalse(tables.get(singleTableNames[0]).getColumns().isEmpty());
         assertTrue(tables.containsKey(singleTableNames[1]));
diff --git a/shardingsphere-kernel/shardingsphere-single-table/shardingsphere-single-table-core/src/test/java/org/apache/shardingsphere/singletable/route/SingleTableSQLRouterTest.java b/shardingsphere-kernel/shardingsphere-single-table/shardingsphere-single-table-core/src/test/java/org/apache/shardingsphere/singletable/route/SingleTableSQLRouterTest.java
index 121774474af..99a17281b55 100644
--- a/shardingsphere-kernel/shardingsphere-single-table/shardingsphere-single-table-core/src/test/java/org/apache/shardingsphere/singletable/route/SingleTableSQLRouterTest.java
+++ b/shardingsphere-kernel/shardingsphere-single-table/shardingsphere-single-table-core/src/test/java/org/apache/shardingsphere/singletable/route/SingleTableSQLRouterTest.java
@@ -18,11 +18,9 @@
 package org.apache.shardingsphere.singletable.route;
 
 import org.apache.shardingsphere.infra.binder.LogicSQL;
-import org.apache.shardingsphere.infra.binder.statement.SQLStatementContext;
 import org.apache.shardingsphere.infra.binder.statement.ddl.CreateTableStatementContext;
 import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
 import org.apache.shardingsphere.infra.database.DefaultDatabase;
-import org.apache.shardingsphere.infra.database.type.DatabaseType;
 import org.apache.shardingsphere.infra.datanode.DataNode;
 import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
 import org.apache.shardingsphere.infra.route.context.RouteContext;
@@ -35,9 +33,12 @@ import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.Tab
 import org.apache.shardingsphere.sql.parser.sql.common.statement.ddl.CreateTableStatement;
 import org.apache.shardingsphere.sql.parser.sql.common.value.identifier.IdentifierValue;
 import org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.ddl.MySQLCreateTableStatement;
+import org.apache.shardingsphere.test.mock.MockedDataSource;
 import org.junit.Test;
 
 import javax.sql.DataSource;
+import java.sql.Connection;
+import java.sql.SQLException;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.HashMap;
@@ -57,46 +58,57 @@ import static org.mockito.Mockito.when;
 public final class SingleTableSQLRouterTest {
     
     @Test
-    public void assertCreateRouteContextWithSingleDataSource() {
-        SingleTableRule singleTableRule = new SingleTableRule(new SingleTableRuleConfiguration(), DefaultDatabase.LOGIC_NAME, mock(DatabaseType.class),
-                createSingleDataSourceMap(), Collections.emptyList(), new ConfigurationProperties(new Properties()));
-        singleTableRule.getSingleTableDataNodes().put("t_order", Collections.singletonList(mockDataNode("ds_0")));
-        ShardingSphereDatabase database = mockSingleDataSourceMetaData();
-        RouteContext actual = new SingleTableSQLRouter().createRouteContext(createLogicSQL(), database, singleTableRule, new ConfigurationProperties(new Properties()));
-        List<RouteUnit> routeUnits = new ArrayList<>(actual.getRouteUnits());
-        assertThat(actual.getRouteUnits().size(), is(1));
-        assertThat(routeUnits.get(0).getDataSourceMapper().getLogicName(), is("ds_0"));
-        assertThat(routeUnits.get(0).getDataSourceMapper().getActualName(), is("ds_0"));
-        assertTrue(routeUnits.get(0).getTableMappers().isEmpty());
+    public void assertCreateRouteContextWithSingleDataSource() throws SQLException {
+        SingleTableRule rule = new SingleTableRule(new SingleTableRuleConfiguration(), DefaultDatabase.LOGIC_NAME,
+                Collections.singletonMap("foo_ds", new MockedDataSource(mockConnection())), Collections.emptyList(), new ConfigurationProperties(new Properties()));
+        rule.getSingleTableDataNodes().put("t_order", Collections.singletonList(createDataNode("foo_ds")));
+        ShardingSphereDatabase database = mockSingleDatabase();
+        RouteContext actual = new SingleTableSQLRouter().createRouteContext(createLogicSQL(), database, rule, new ConfigurationProperties(new Properties()));
         assertFalse(actual.isFederated());
+        assertThat(actual.getRouteUnits().size(), is(1));
+        RouteUnit routeUnit = actual.getRouteUnits().iterator().next();
+        assertThat(routeUnit.getDataSourceMapper().getLogicName(), is("foo_ds"));
+        assertThat(routeUnit.getDataSourceMapper().getActualName(), is("foo_ds"));
+        assertTrue(routeUnit.getTableMappers().isEmpty());
+    }
+    
+    private ShardingSphereDatabase mockSingleDatabase() {
+        ShardingSphereDatabase result = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS);
+        when(result.getResource().getDataSources()).thenReturn(Collections.singletonMap("foo_ds", new MockedDataSource()));
+        return result;
     }
     
     @Test
-    public void assertCreateRouteContextWithReadwriteSplittingDataSource() {
-        SingleTableRule singleTableRule = new SingleTableRule(new SingleTableRuleConfiguration(), DefaultDatabase.LOGIC_NAME, mock(DatabaseType.class),
-                createReadwriteSplittingDataSourceMap(), Collections.emptyList(), new ConfigurationProperties(new Properties()));
-        singleTableRule.getSingleTableDataNodes().put("t_order", Collections.singletonList(mockDataNode("write_ds")));
-        ShardingSphereDatabase database = mockReadwriteSplittingDataSourceMetaData();
-        RouteContext actual = new SingleTableSQLRouter().createRouteContext(createLogicSQL(), database, singleTableRule, new ConfigurationProperties(new Properties()));
-        List<RouteUnit> routeUnits = new ArrayList<>(actual.getRouteUnits());
-        assertThat(actual.getRouteUnits().size(), is(1));
-        assertThat(routeUnits.get(0).getDataSourceMapper().getLogicName(), is("readwrite_ds"));
-        assertThat(routeUnits.get(0).getDataSourceMapper().getActualName(), is("write_ds"));
-        assertTrue(routeUnits.get(0).getTableMappers().isEmpty());
+    public void assertCreateRouteContextWithReadwriteSplittingDataSource() throws SQLException {
+        SingleTableRule rule = new SingleTableRule(new SingleTableRuleConfiguration(), DefaultDatabase.LOGIC_NAME,
+                Collections.singletonMap("readwrite_ds", new MockedDataSource(mockConnection())), Collections.emptyList(), new ConfigurationProperties(new Properties()));
+        rule.getSingleTableDataNodes().put("t_order", Collections.singletonList(createDataNode("write_ds")));
+        ShardingSphereDatabase database = mockReadwriteSplittingDatabase();
+        RouteContext actual = new SingleTableSQLRouter().createRouteContext(createLogicSQL(), database, rule, new ConfigurationProperties(new Properties()));
         assertFalse(actual.isFederated());
+        assertThat(actual.getRouteUnits().size(), is(1));
+        RouteUnit routeUnit = actual.getRouteUnits().iterator().next();
+        assertThat(routeUnit.getDataSourceMapper().getLogicName(), is("readwrite_ds"));
+        assertThat(routeUnit.getDataSourceMapper().getActualName(), is("write_ds"));
+        assertTrue(routeUnit.getTableMappers().isEmpty());
+    }
+    
+    private ShardingSphereDatabase mockReadwriteSplittingDatabase() {
+        ShardingSphereDatabase result = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS);
+        when(result.getResource().getDataSources()).thenReturn(Collections.singletonMap("write_ds", new MockedDataSource()));
+        return result;
     }
     
     @Test
-    public void assertCreateRouteContextWithMultiDataSource() {
-        SingleTableRule singleTableRule = new SingleTableRule(new SingleTableRuleConfiguration(), DefaultDatabase.LOGIC_NAME, mock(DatabaseType.class),
+    public void assertCreateRouteContextWithMultiDataSource() throws SQLException {
+        SingleTableRule rule = new SingleTableRule(new SingleTableRuleConfiguration(), DefaultDatabase.LOGIC_NAME,
                 createMultiDataSourceMap(), Collections.emptyList(), new ConfigurationProperties(new Properties()));
-        singleTableRule.getSingleTableDataNodes().put("t_order", Collections.singletonList(mockDataNode("ds_0")));
-        ShardingSphereDatabase database = mockMultiDataSourceMetaData();
-        RouteContext actual = new SingleTableSQLRouter().createRouteContext(createLogicSQL(), database, singleTableRule, new ConfigurationProperties(new Properties()));
+        rule.getSingleTableDataNodes().put("t_order", Collections.singleton(createDataNode("ds_0")));
+        ShardingSphereDatabase database = mockDatabaseWithMultipleResources();
+        RouteContext actual = new SingleTableSQLRouter().createRouteContext(createLogicSQL(), database, rule, new ConfigurationProperties(new Properties()));
         List<RouteUnit> routeUnits = new ArrayList<>(actual.getRouteUnits());
         assertThat(actual.getRouteUnits().size(), is(1));
-        assertThat(routeUnits.get(0).getDataSourceMapper().getLogicName(), is("ds_0"));
-        assertThat(routeUnits.get(0).getDataSourceMapper().getActualName(), is("ds_0"));
+        assertThat(routeUnits.get(0).getDataSourceMapper().getLogicName(), is(routeUnits.get(0).getDataSourceMapper().getActualName()));
         assertThat(routeUnits.get(0).getTableMappers().size(), is(1));
         RouteMapper tableMapper = routeUnits.get(0).getTableMappers().iterator().next();
         assertThat(tableMapper.getActualName(), is("t_order"));
@@ -104,65 +116,39 @@ public final class SingleTableSQLRouterTest {
         assertFalse(actual.isFederated());
     }
     
-    private DataNode mockDataNode(final String dataSourceName) {
-        DataNode result = new DataNode(dataSourceName, "t_order");
-        result.setSchemaName(DefaultDatabase.LOGIC_NAME);
+    private Map<String, DataSource> createMultiDataSourceMap() throws SQLException {
+        Map<String, DataSource> result = new HashMap<>(2, 1);
+        Connection connection = mockConnection();
+        result.put("ds_0", new MockedDataSource(connection));
+        result.put("ds_1", new MockedDataSource(connection));
         return result;
     }
     
-    private ShardingSphereDatabase mockSingleDataSourceMetaData() {
-        ShardingSphereDatabase result = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS);
-        Map<String, DataSource> dataSourceMap = new HashMap<>(2, 1);
-        dataSourceMap.put("ds_0", mock(DataSource.class, RETURNS_DEEP_STUBS));
-        when(result.getResource().getDataSources()).thenReturn(dataSourceMap);
+    private Connection mockConnection() throws SQLException {
+        Connection result = mock(Connection.class, RETURNS_DEEP_STUBS);
+        when(result.getMetaData().getURL()).thenReturn("jdbc:h2:mem:db");
         return result;
     }
     
-    private ShardingSphereDatabase mockReadwriteSplittingDataSourceMetaData() {
-        ShardingSphereDatabase result = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS);
-        Map<String, DataSource> dataSourceMap = new HashMap<>(2, 1);
-        dataSourceMap.put("write_ds", mock(DataSource.class, RETURNS_DEEP_STUBS));
-        when(result.getResource().getDataSources()).thenReturn(dataSourceMap);
+    private DataNode createDataNode(final String dataSourceName) {
+        DataNode result = new DataNode(dataSourceName, "t_order");
+        result.setSchemaName(DefaultDatabase.LOGIC_NAME);
         return result;
     }
     
-    private ShardingSphereDatabase mockMultiDataSourceMetaData() {
+    private ShardingSphereDatabase mockDatabaseWithMultipleResources() {
         ShardingSphereDatabase result = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS);
         Map<String, DataSource> dataSourceMap = new HashMap<>(2, 1);
-        dataSourceMap.put("ds_0", mock(DataSource.class, RETURNS_DEEP_STUBS));
-        dataSourceMap.put("ds_1", mock(DataSource.class, RETURNS_DEEP_STUBS));
+        dataSourceMap.put("ds_0", new MockedDataSource());
+        dataSourceMap.put("ds_1", new MockedDataSource());
         when(result.getResource().getDataSources()).thenReturn(dataSourceMap);
         when(result.getName()).thenReturn(DefaultDatabase.LOGIC_NAME);
         return result;
     }
     
     private LogicSQL createLogicSQL() {
-        IdentifierValue identifierValue = new IdentifierValue("t_order");
-        TableNameSegment tableNameSegment = new TableNameSegment(1, 2, identifierValue);
-        SimpleTableSegment simpleTableSegment = new SimpleTableSegment(tableNameSegment);
         CreateTableStatement createTableStatement = new MySQLCreateTableStatement();
-        createTableStatement.setTable(simpleTableSegment);
-        List<Object> parameters = new LinkedList<>();
-        SQLStatementContext<CreateTableStatement> sqlStatementContext = new CreateTableStatementContext(createTableStatement);
-        return new LogicSQL(sqlStatementContext, "create table", parameters);
-    }
-    
-    private Map<String, DataSource> createSingleDataSourceMap() {
-        Map<String, DataSource> result = new HashMap<>(2, 1);
-        result.put("ds_0", mock(DataSource.class, RETURNS_DEEP_STUBS));
-        return result;
-    }
-    
-    private Map<String, DataSource> createReadwriteSplittingDataSourceMap() {
-        Map<String, DataSource> result = new HashMap<>(2, 1);
-        result.put("readwrite_ds", mock(DataSource.class, RETURNS_DEEP_STUBS));
-        return result;
-    }
-    
-    private Map<String, DataSource> createMultiDataSourceMap() {
-        Map<String, DataSource> result = new HashMap<>(2, 1);
-        result.put("ds_0", mock(DataSource.class, RETURNS_DEEP_STUBS));
-        result.put("ds_1", mock(DataSource.class, RETURNS_DEEP_STUBS));
-        return result;
+        createTableStatement.setTable(new SimpleTableSegment(new TableNameSegment(1, 2, new IdentifierValue("t_order"))));
+        return new LogicSQL(new CreateTableStatementContext(createTableStatement), "CREATE TABLE", new LinkedList<>());
     }
 }
diff --git a/shardingsphere-kernel/shardingsphere-single-table/shardingsphere-single-table-core/src/test/java/org/apache/shardingsphere/singletable/route/engine/SingleTableDatabaseBroadcastRouteEngineTest.java b/shardingsphere-kernel/shardingsphere-single-table/shardingsphere-single-table-core/src/test/java/org/apache/shardingsphere/singletable/route/engine/SingleTableDatabaseBroadcastRouteEngineTest.java
index e9cba12b567..37466c7c91a 100644
--- a/shardingsphere-kernel/shardingsphere-single-table/shardingsphere-single-table-core/src/test/java/org/apache/shardingsphere/singletable/route/engine/SingleTableDatabaseBroadcastRouteEngineTest.java
+++ b/shardingsphere-kernel/shardingsphere-single-table/shardingsphere-single-table-core/src/test/java/org/apache/shardingsphere/singletable/route/engine/SingleTableDatabaseBroadcastRouteEngineTest.java
@@ -19,18 +19,20 @@ package org.apache.shardingsphere.singletable.route.engine;
 
 import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
 import org.apache.shardingsphere.infra.database.DefaultDatabase;
-import org.apache.shardingsphere.infra.database.type.DatabaseType;
 import org.apache.shardingsphere.infra.route.context.RouteContext;
 import org.apache.shardingsphere.infra.route.context.RouteUnit;
 import org.apache.shardingsphere.singletable.config.SingleTableRuleConfiguration;
 import org.apache.shardingsphere.singletable.rule.SingleTableRule;
+import org.apache.shardingsphere.test.mock.MockedDataSource;
 import org.junit.Test;
 
 import javax.sql.DataSource;
+import java.sql.Connection;
+import java.sql.SQLException;
 import java.util.ArrayList;
 import java.util.Collections;
-import java.util.List;
 import java.util.LinkedHashMap;
+import java.util.List;
 import java.util.Map;
 import java.util.Properties;
 
@@ -39,13 +41,14 @@ import static org.junit.Assert.assertThat;
 import static org.junit.Assert.assertTrue;
 import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
 import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
 
 public final class SingleTableDatabaseBroadcastRouteEngineTest {
     
     @Test
-    public void assertRoute() {
-        SingleTableRule singleTableRule = new SingleTableRule(new SingleTableRuleConfiguration(),
-                DefaultDatabase.LOGIC_NAME, mock(DatabaseType.class), createDataSourceMap(), Collections.emptyList(), new ConfigurationProperties(new Properties()));
+    public void assertRoute() throws SQLException {
+        SingleTableRule singleTableRule = new SingleTableRule(
+                new SingleTableRuleConfiguration(), DefaultDatabase.LOGIC_NAME, createDataSourceMap(), Collections.emptyList(), new ConfigurationProperties(new Properties()));
         RouteContext routeContext = new RouteContext();
         SingleTableDatabaseBroadcastRouteEngine engine = new SingleTableDatabaseBroadcastRouteEngine();
         engine.route(routeContext, singleTableRule);
@@ -57,10 +60,12 @@ public final class SingleTableDatabaseBroadcastRouteEngineTest {
         assertTrue(routeUnits.get(1).getTableMappers().isEmpty());
     }
     
-    private Map<String, DataSource> createDataSourceMap() {
+    private Map<String, DataSource> createDataSourceMap() throws SQLException {
         Map<String, DataSource> result = new LinkedHashMap<>(2, 1);
-        result.put("ds_0", mock(DataSource.class, RETURNS_DEEP_STUBS));
-        result.put("ds_1", mock(DataSource.class, RETURNS_DEEP_STUBS));
+        Connection connection = mock(Connection.class, RETURNS_DEEP_STUBS);
+        when(connection.getMetaData().getURL()).thenReturn("jdbc:h2:mem:db");
+        result.put("ds_0", new MockedDataSource(connection));
+        result.put("ds_1", new MockedDataSource(connection));
         return result;
     }
 }
diff --git a/shardingsphere-kernel/shardingsphere-single-table/shardingsphere-single-table-core/src/test/java/org/apache/shardingsphere/singletable/route/engine/SingleTableStandardRouteEngineTest.java b/shardingsphere-kernel/shardingsphere-single-table/shardingsphere-single-table-core/src/test/java/org/apache/shardingsphere/singletable/route/engine/SingleTableStandardRouteEngineTest.java
index 4331d3dfabf..7824aba0242 100644
--- a/shardingsphere-kernel/shardingsphere-single-table/shardingsphere-single-table-core/src/test/java/org/apache/shardingsphere/singletable/route/engine/SingleTableStandardRouteEngineTest.java
+++ b/shardingsphere-kernel/shardingsphere-single-table/shardingsphere-single-table-core/src/test/java/org/apache/shardingsphere/singletable/route/engine/SingleTableStandardRouteEngineTest.java
@@ -19,7 +19,6 @@ package org.apache.shardingsphere.singletable.route.engine;
 
 import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
 import org.apache.shardingsphere.infra.database.DefaultDatabase;
-import org.apache.shardingsphere.infra.database.type.DatabaseType;
 import org.apache.shardingsphere.infra.datanode.DataNode;
 import org.apache.shardingsphere.infra.metadata.database.schema.QualifiedTable;
 import org.apache.shardingsphere.infra.route.context.RouteContext;
@@ -28,9 +27,12 @@ import org.apache.shardingsphere.infra.route.context.RouteUnit;
 import org.apache.shardingsphere.singletable.config.SingleTableRuleConfiguration;
 import org.apache.shardingsphere.singletable.rule.SingleTableRule;
 import org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.ddl.MySQLCreateTableStatement;
+import org.apache.shardingsphere.test.mock.MockedDataSource;
 import org.junit.Test;
 
 import javax.sql.DataSource;
+import java.sql.Connection;
+import java.sql.SQLException;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
@@ -47,18 +49,19 @@ import static org.junit.Assert.assertThat;
 import static org.junit.Assert.assertTrue;
 import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
 import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
 
 public final class SingleTableStandardRouteEngineTest {
     
     @Test
-    public void assertRouteInSameDataSource() {
-        SingleTableStandardRouteEngine singleTableRouteEngine = new SingleTableStandardRouteEngine(mockQualifiedTables(), null);
-        SingleTableRule singleTableRule = new SingleTableRule(new SingleTableRuleConfiguration(), DefaultDatabase.LOGIC_NAME, mock(DatabaseType.class),
-                createDataSourceMap(), Collections.emptyList(), new ConfigurationProperties(new Properties()));
+    public void assertRouteInSameDataSource() throws SQLException {
+        SingleTableStandardRouteEngine engine = new SingleTableStandardRouteEngine(mockQualifiedTables(), null);
+        SingleTableRule singleTableRule = new SingleTableRule(
+                new SingleTableRuleConfiguration(), DefaultDatabase.LOGIC_NAME, createDataSourceMap(), Collections.emptyList(), new ConfigurationProperties(new Properties()));
         singleTableRule.getSingleTableDataNodes().put("t_order", Collections.singletonList(mockDataNode("ds_0", "t_order")));
         singleTableRule.getSingleTableDataNodes().put("t_order_item", Collections.singletonList(mockDataNode("ds_0", "t_order_item")));
         RouteContext routeContext = new RouteContext();
-        singleTableRouteEngine.route(routeContext, singleTableRule);
+        engine.route(routeContext, singleTableRule);
         List<RouteUnit> routeUnits = new ArrayList<>(routeContext.getRouteUnits());
         assertThat(routeContext.getRouteUnits().size(), is(1));
         assertThat(routeUnits.get(0).getDataSourceMapper().getActualName(), is("ds_0"));
@@ -84,14 +87,14 @@ public final class SingleTableStandardRouteEngineTest {
     }
     
     @Test
-    public void assertRouteInDifferentDataSource() {
-        SingleTableStandardRouteEngine singleTableRouteEngine = new SingleTableStandardRouteEngine(mockQualifiedTables(), null);
-        SingleTableRule singleTableRule = new SingleTableRule(new SingleTableRuleConfiguration(), DefaultDatabase.LOGIC_NAME, mock(DatabaseType.class),
-                createDataSourceMap(), Collections.emptyList(), new ConfigurationProperties(new Properties()));
+    public void assertRouteInDifferentDataSource() throws SQLException {
+        SingleTableStandardRouteEngine engine = new SingleTableStandardRouteEngine(mockQualifiedTables(), null);
+        SingleTableRule singleTableRule = new SingleTableRule(
+                new SingleTableRuleConfiguration(), DefaultDatabase.LOGIC_NAME, createDataSourceMap(), Collections.emptyList(), new ConfigurationProperties(new Properties()));
         singleTableRule.getSingleTableDataNodes().put("t_order", Collections.singletonList(mockDataNode("ds_0", "t_order")));
         singleTableRule.getSingleTableDataNodes().put("t_order_item", Collections.singletonList(mockDataNode("ds_1", "t_order_item")));
         RouteContext routeContext = new RouteContext();
-        singleTableRouteEngine.route(routeContext, singleTableRule);
+        engine.route(routeContext, singleTableRule);
         List<RouteUnit> routeUnits = new ArrayList<>(routeContext.getRouteUnits());
         assertThat(routeContext.getRouteUnits().size(), is(2));
         assertThat(routeUnits.get(0).getDataSourceMapper().getActualName(), is("ds_0"));
@@ -108,12 +111,12 @@ public final class SingleTableStandardRouteEngineTest {
     }
     
     @Test
-    public void assertRouteWithoutSingleTableRule() {
-        SingleTableStandardRouteEngine singleTableRouteEngine = new SingleTableStandardRouteEngine(mockQualifiedTables(), new MySQLCreateTableStatement());
-        SingleTableRule singleTableRule = new SingleTableRule(new SingleTableRuleConfiguration(), DefaultDatabase.LOGIC_NAME, mock(DatabaseType.class),
-                createDataSourceMap(), Collections.emptyList(), new ConfigurationProperties(new Properties()));
+    public void assertRouteWithoutSingleTableRule() throws SQLException {
+        SingleTableStandardRouteEngine engine = new SingleTableStandardRouteEngine(mockQualifiedTables(), new MySQLCreateTableStatement());
+        SingleTableRule singleTableRule = new SingleTableRule(
+                new SingleTableRuleConfiguration(), DefaultDatabase.LOGIC_NAME, createDataSourceMap(), Collections.emptyList(), new ConfigurationProperties(new Properties()));
         RouteContext routeContext = new RouteContext();
-        singleTableRouteEngine.route(routeContext, singleTableRule);
+        engine.route(routeContext, singleTableRule);
         List<RouteUnit> routeUnits = new ArrayList<>(routeContext.getRouteUnits());
         assertThat(routeContext.getRouteUnits().size(), is(1));
         assertThat(routeUnits.get(0).getTableMappers().size(), is(1));
@@ -124,12 +127,12 @@ public final class SingleTableStandardRouteEngineTest {
     }
     
     @Test
-    public void assertRouteWithDefaultSingleTableRule() {
-        SingleTableStandardRouteEngine singleTableRouteEngine = new SingleTableStandardRouteEngine(mockQualifiedTables(), new MySQLCreateTableStatement());
-        SingleTableRule singleTableRule = new SingleTableRule(new SingleTableRuleConfiguration("ds_0"), DefaultDatabase.LOGIC_NAME, mock(DatabaseType.class),
-                createDataSourceMap(), Collections.emptyList(), new ConfigurationProperties(new Properties()));
+    public void assertRouteWithDefaultSingleTableRule() throws SQLException {
+        SingleTableStandardRouteEngine engine = new SingleTableStandardRouteEngine(mockQualifiedTables(), new MySQLCreateTableStatement());
+        SingleTableRule singleTableRule = new SingleTableRule(
+                new SingleTableRuleConfiguration("ds_0"), DefaultDatabase.LOGIC_NAME, createDataSourceMap(), Collections.emptyList(), new ConfigurationProperties(new Properties()));
         RouteContext routeContext = new RouteContext();
-        singleTableRouteEngine.route(routeContext, singleTableRule);
+        engine.route(routeContext, singleTableRule);
         List<RouteUnit> routeUnits = new ArrayList<>(routeContext.getRouteUnits());
         assertThat(routeContext.getRouteUnits().size(), is(1));
         assertThat(routeUnits.get(0).getDataSourceMapper().getActualName(), is("ds_0"));
@@ -140,10 +143,12 @@ public final class SingleTableStandardRouteEngineTest {
         assertThat(tableMapper0.getLogicName(), is("t_order"));
     }
     
-    private Map<String, DataSource> createDataSourceMap() {
+    private Map<String, DataSource> createDataSourceMap() throws SQLException {
         Map<String, DataSource> result = new HashMap<>(2, 1);
-        result.put("ds_0", mock(DataSource.class, RETURNS_DEEP_STUBS));
-        result.put("ds_1", mock(DataSource.class, RETURNS_DEEP_STUBS));
+        Connection connection = mock(Connection.class, RETURNS_DEEP_STUBS);
+        when(connection.getMetaData().getURL()).thenReturn("jdbc:h2:mem:db");
+        result.put("ds_0", new MockedDataSource(connection));
+        result.put("ds_1", new MockedDataSource(connection));
         return result;
     }
 }
diff --git a/shardingsphere-kernel/shardingsphere-single-table/shardingsphere-single-table-core/src/test/java/org/apache/shardingsphere/singletable/rule/SingleTableRuleTest.java b/shardingsphere-kernel/shardingsphere-single-table/shardingsphere-single-table-core/src/test/java/org/apache/shardingsphere/singletable/rule/SingleTableRuleTest.java
index a19dca6bf95..0a0610e1799 100644
--- a/shardingsphere-kernel/shardingsphere-single-table/shardingsphere-single-table-core/src/test/java/org/apache/shardingsphere/singletable/rule/SingleTableRuleTest.java
+++ b/shardingsphere-kernel/shardingsphere-single-table/shardingsphere-single-table-core/src/test/java/org/apache/shardingsphere/singletable/rule/SingleTableRuleTest.java
@@ -19,17 +19,18 @@ package org.apache.shardingsphere.singletable.rule;
 
 import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
 import org.apache.shardingsphere.infra.database.DefaultDatabase;
-import org.apache.shardingsphere.infra.database.type.DatabaseType;
 import org.apache.shardingsphere.infra.datanode.DataNode;
 import org.apache.shardingsphere.infra.metadata.database.schema.QualifiedTable;
 import org.apache.shardingsphere.infra.route.context.RouteContext;
 import org.apache.shardingsphere.infra.route.context.RouteMapper;
 import org.apache.shardingsphere.infra.rule.identifier.type.DataNodeContainedRule;
 import org.apache.shardingsphere.singletable.config.SingleTableRuleConfiguration;
+import org.apache.shardingsphere.test.mock.MockedDataSource;
 import org.junit.Before;
 import org.junit.Test;
 
 import javax.sql.DataSource;
+import java.sql.Connection;
 import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.util.Arrays;
@@ -69,8 +70,10 @@ public final class SingleTableRuleTest {
     }
     
     private DataSource mockDataSource(final String dataSourceName, final List<String> tableNames) throws SQLException {
-        DataSource result = mock(DataSource.class, RETURNS_DEEP_STUBS);
-        when(result.getConnection().getCatalog()).thenReturn(dataSourceName);
+        Connection connection = mock(Connection.class, RETURNS_DEEP_STUBS);
+        when(connection.getCatalog()).thenReturn(dataSourceName);
+        when(connection.getMetaData().getURL()).thenReturn(String.format("jdbc:h2:mem:%s", dataSourceName));
+        DataSource result = new MockedDataSource(connection);
         ResultSet resultSet = mockResultSet(tableNames);
         when(result.getConnection().getMetaData().getTables(dataSourceName, null, null, new String[]{TABLE_TYPE, VIEW_TYPE})).thenReturn(resultSet);
         return result;
@@ -91,8 +94,8 @@ public final class SingleTableRuleTest {
     public void assertGetSingleTableDataNodes() {
         DataNodeContainedRule dataNodeContainedRule = mock(DataNodeContainedRule.class);
         when(dataNodeContainedRule.getAllTables()).thenReturn(Arrays.asList("t_order", "t_order_0", "t_order_1"));
-        SingleTableRule singleTableRule = new SingleTableRule(new SingleTableRuleConfiguration(), DefaultDatabase.LOGIC_NAME, mock(DatabaseType.class), dataSourceMap,
-                Collections.singleton(dataNodeContainedRule), new ConfigurationProperties(new Properties()));
+        SingleTableRule singleTableRule = new SingleTableRule(
+                new SingleTableRuleConfiguration(), DefaultDatabase.LOGIC_NAME, dataSourceMap, Collections.singleton(dataNodeContainedRule), new ConfigurationProperties(new Properties()));
         Map<String, Collection<DataNode>> actual = singleTableRule.getSingleTableDataNodes();
         assertThat(actual.size(), is(2));
         assertTrue(actual.containsKey("employee"));
@@ -103,8 +106,8 @@ public final class SingleTableRuleTest {
     public void assertGetSingleTableDataNodesWithUpperCase() {
         DataNodeContainedRule dataNodeContainedRule = mock(DataNodeContainedRule.class);
         when(dataNodeContainedRule.getAllTables()).thenReturn(Arrays.asList("T_ORDER", "T_ORDER_0", "T_ORDER_1"));
-        SingleTableRule singleTableRule = new SingleTableRule(new SingleTableRuleConfiguration(), DefaultDatabase.LOGIC_NAME, mock(DatabaseType.class), dataSourceMap,
-                Collections.singleton(dataNodeContainedRule), new ConfigurationProperties(new Properties()));
+        SingleTableRule singleTableRule = new SingleTableRule(
+                new SingleTableRuleConfiguration(), DefaultDatabase.LOGIC_NAME, dataSourceMap, Collections.singleton(dataNodeContainedRule), new ConfigurationProperties(new Properties()));
         Map<String, Collection<DataNode>> actual = singleTableRule.getSingleTableDataNodes();
         assertThat(actual.size(), is(2));
         assertTrue(actual.containsKey("employee"));
@@ -114,8 +117,8 @@ public final class SingleTableRuleTest {
     @Test
     public void assertFindSingleTableDataNode() {
         DataNodeContainedRule dataNodeContainedRule = mock(DataNodeContainedRule.class);
-        SingleTableRule singleTableRule = new SingleTableRule(new SingleTableRuleConfiguration(), DefaultDatabase.LOGIC_NAME, mock(DatabaseType.class), dataSourceMap,
-                Collections.singleton(dataNodeContainedRule), new ConfigurationProperties(new Properties()));
+        SingleTableRule singleTableRule = new SingleTableRule(
+                new SingleTableRuleConfiguration(), DefaultDatabase.LOGIC_NAME, dataSourceMap, Collections.singleton(dataNodeContainedRule), new ConfigurationProperties(new Properties()));
         Optional<DataNode> actual = singleTableRule.findSingleTableDataNode(DefaultDatabase.LOGIC_NAME, "employee");
         assertTrue(actual.isPresent());
         assertThat(actual.get().getDataSourceName(), is("ds_0"));
@@ -125,8 +128,8 @@ public final class SingleTableRuleTest {
     @Test
     public void assertFindSingleTableDataNodeWithUpperCase() {
         DataNodeContainedRule dataNodeContainedRule = mock(DataNodeContainedRule.class);
-        SingleTableRule singleTableRule = new SingleTableRule(new SingleTableRuleConfiguration(), DefaultDatabase.LOGIC_NAME, mock(DatabaseType.class), dataSourceMap,
-                Collections.singleton(dataNodeContainedRule), new ConfigurationProperties(new Properties()));
+        SingleTableRule singleTableRule = new SingleTableRule(
+                new SingleTableRuleConfiguration(), DefaultDatabase.LOGIC_NAME, dataSourceMap, Collections.singleton(dataNodeContainedRule), new ConfigurationProperties(new Properties()));
         Optional<DataNode> actual = singleTableRule.findSingleTableDataNode(DefaultDatabase.LOGIC_NAME, "EMPLOYEE");
         assertTrue(actual.isPresent());
         assertThat(actual.get().getDataSourceName(), is("ds_0"));
@@ -136,8 +139,8 @@ public final class SingleTableRuleTest {
     @Test
     public void assertIsSingleTablesInSameDataSource() {
         DataNodeContainedRule dataNodeContainedRule = mock(DataNodeContainedRule.class);
-        SingleTableRule singleTableRule = new SingleTableRule(new SingleTableRuleConfiguration(), DefaultDatabase.LOGIC_NAME, mock(DatabaseType.class), dataSourceMap,
-                Collections.singleton(dataNodeContainedRule), new ConfigurationProperties(new Properties()));
+        SingleTableRule singleTableRule = new SingleTableRule(
+                new SingleTableRuleConfiguration(), DefaultDatabase.LOGIC_NAME, dataSourceMap, Collections.singleton(dataNodeContainedRule), new ConfigurationProperties(new Properties()));
         Collection<QualifiedTable> singleTableNames = new LinkedList<>();
         singleTableNames.add(new QualifiedTable(DefaultDatabase.LOGIC_NAME, "employee"));
         assertTrue(singleTableRule.isSingleTablesInSameDataSource(singleTableNames));
@@ -153,8 +156,8 @@ public final class SingleTableRuleTest {
         RouteContext routeContext = new RouteContext();
         routeContext.putRouteUnit(dataSourceMapper, tableMappers);
         DataNodeContainedRule dataNodeContainedRule = mock(DataNodeContainedRule.class);
-        SingleTableRule singleTableRule = new SingleTableRule(new SingleTableRuleConfiguration(), DefaultDatabase.LOGIC_NAME, mock(DatabaseType.class), dataSourceMap,
-                Collections.singleton(dataNodeContainedRule), new ConfigurationProperties(new Properties()));
+        SingleTableRule singleTableRule = new SingleTableRule(
+                new SingleTableRuleConfiguration(), DefaultDatabase.LOGIC_NAME, dataSourceMap, Collections.singleton(dataNodeContainedRule), new ConfigurationProperties(new Properties()));
         assertTrue(singleTableRule.isAllTablesInSameDataSource(routeContext, singleTableNames));
     }
     
@@ -163,16 +166,16 @@ public final class SingleTableRuleTest {
         DataNodeContainedRule dataNodeContainedRule = mock(DataNodeContainedRule.class);
         SingleTableRuleConfiguration singleTableRuleConfig = new SingleTableRuleConfiguration();
         singleTableRuleConfig.setDefaultDataSource("ds_0");
-        SingleTableRule singleTableRule = new SingleTableRule(singleTableRuleConfig, DefaultDatabase.LOGIC_NAME, mock(DatabaseType.class), dataSourceMap,
-                Collections.singleton(dataNodeContainedRule), new ConfigurationProperties(new Properties()));
+        SingleTableRule singleTableRule = new SingleTableRule(
+                singleTableRuleConfig, DefaultDatabase.LOGIC_NAME, dataSourceMap, Collections.singleton(dataNodeContainedRule), new ConfigurationProperties(new Properties()));
         assertThat(singleTableRule.assignNewDataSourceName(), is("ds_0"));
     }
     
     @Test
     public void assertGetSingleTableNames() {
         DataNodeContainedRule dataNodeContainedRule = mock(DataNodeContainedRule.class);
-        SingleTableRule singleTableRule = new SingleTableRule(new SingleTableRuleConfiguration(), DefaultDatabase.LOGIC_NAME, mock(DatabaseType.class), dataSourceMap,
-                Collections.singleton(dataNodeContainedRule), new ConfigurationProperties(new Properties()));
+        SingleTableRule singleTableRule = new SingleTableRule(
+                new SingleTableRuleConfiguration(), DefaultDatabase.LOGIC_NAME, dataSourceMap, Collections.singleton(dataNodeContainedRule), new ConfigurationProperties(new Properties()));
         Collection<QualifiedTable> tableNames = new LinkedList<>();
         tableNames.add(new QualifiedTable(DefaultDatabase.LOGIC_NAME, "employee"));
         assertThat(singleTableRule.getSingleTableNames(tableNames).iterator().next().getSchemaName(), is(DefaultDatabase.LOGIC_NAME));
@@ -182,8 +185,8 @@ public final class SingleTableRuleTest {
     @Test
     public void assertPut() {
         DataNodeContainedRule dataNodeContainedRule = mock(DataNodeContainedRule.class);
-        SingleTableRule singleTableRule = new SingleTableRule(new SingleTableRuleConfiguration(), DefaultDatabase.LOGIC_NAME, mock(DatabaseType.class), dataSourceMap,
-                Collections.singleton(dataNodeContainedRule), new ConfigurationProperties(new Properties()));
+        SingleTableRule singleTableRule = new SingleTableRule(
+                new SingleTableRuleConfiguration(), DefaultDatabase.LOGIC_NAME, dataSourceMap, Collections.singleton(dataNodeContainedRule), new ConfigurationProperties(new Properties()));
         String tableName = "teacher";
         String dataSourceName = "ds_0";
         singleTableRule.put(dataSourceName, DefaultDatabase.LOGIC_NAME, tableName);
@@ -201,8 +204,8 @@ public final class SingleTableRuleTest {
     @Test
     public void assertRemove() {
         DataNodeContainedRule dataNodeContainedRule = mock(DataNodeContainedRule.class);
-        SingleTableRule singleTableRule = new SingleTableRule(new SingleTableRuleConfiguration(), DefaultDatabase.LOGIC_NAME, mock(DatabaseType.class), dataSourceMap,
-                Collections.singleton(dataNodeContainedRule), new ConfigurationProperties(new Properties()));
+        SingleTableRule singleTableRule = new SingleTableRule(
+                new SingleTableRuleConfiguration(), DefaultDatabase.LOGIC_NAME, dataSourceMap, Collections.singleton(dataNodeContainedRule), new ConfigurationProperties(new Properties()));
         String tableName = "employee";
         singleTableRule.remove(DefaultDatabase.LOGIC_NAME, tableName);
         Collection<QualifiedTable> tableNames = new LinkedList<>();
@@ -216,8 +219,8 @@ public final class SingleTableRuleTest {
     @Test
     public void assertGetAllDataNodes() {
         DataNodeContainedRule dataNodeContainedRule = mock(DataNodeContainedRule.class);
-        SingleTableRule singleTableRule = new SingleTableRule(new SingleTableRuleConfiguration(), DefaultDatabase.LOGIC_NAME, mock(DatabaseType.class), dataSourceMap,
-                Collections.singleton(dataNodeContainedRule), new ConfigurationProperties(new Properties()));
+        SingleTableRule singleTableRule = new SingleTableRule(
+                new SingleTableRuleConfiguration(), DefaultDatabase.LOGIC_NAME, dataSourceMap, Collections.singleton(dataNodeContainedRule), new ConfigurationProperties(new Properties()));
         assertTrue(singleTableRule.getAllDataNodes().containsKey("employee"));
         assertTrue(singleTableRule.getAllDataNodes().containsKey("student"));
         assertTrue(singleTableRule.getAllDataNodes().containsKey("t_order_0"));
@@ -227,8 +230,8 @@ public final class SingleTableRuleTest {
     @Test
     public void assertGetDataNodesByTableName() {
         DataNodeContainedRule dataNodeContainedRule = mock(DataNodeContainedRule.class);
-        SingleTableRule singleTableRule = new SingleTableRule(new SingleTableRuleConfiguration(), DefaultDatabase.LOGIC_NAME, mock(DatabaseType.class), dataSourceMap,
-                Collections.singleton(dataNodeContainedRule), new ConfigurationProperties(new Properties()));
+        SingleTableRule singleTableRule = new SingleTableRule(
+                new SingleTableRuleConfiguration(), DefaultDatabase.LOGIC_NAME, dataSourceMap, Collections.singleton(dataNodeContainedRule), new ConfigurationProperties(new Properties()));
         Collection<DataNode> actual = singleTableRule.getDataNodesByTableName("EMPLOYEE");
         assertThat(actual.size(), is(1));
         DataNode dataNode = actual.iterator().next();
@@ -239,8 +242,8 @@ public final class SingleTableRuleTest {
     @Test
     public void assertFindFirstActualTable() {
         DataNodeContainedRule dataNodeContainedRule = mock(DataNodeContainedRule.class);
-        SingleTableRule singleTableRule = new SingleTableRule(new SingleTableRuleConfiguration(), DefaultDatabase.LOGIC_NAME, mock(DatabaseType.class), dataSourceMap,
-                Collections.singleton(dataNodeContainedRule), new ConfigurationProperties(new Properties()));
+        SingleTableRule singleTableRule = new SingleTableRule(
+                new SingleTableRuleConfiguration(), DefaultDatabase.LOGIC_NAME, dataSourceMap, Collections.singleton(dataNodeContainedRule), new ConfigurationProperties(new Properties()));
         String logicTable = "employee";
         assertFalse(singleTableRule.findFirstActualTable(logicTable).isPresent());
     }
@@ -248,16 +251,16 @@ public final class SingleTableRuleTest {
     @Test
     public void assertIsNeedAccumulate() {
         DataNodeContainedRule dataNodeContainedRule = mock(DataNodeContainedRule.class);
-        SingleTableRule singleTableRule = new SingleTableRule(new SingleTableRuleConfiguration(), DefaultDatabase.LOGIC_NAME, mock(DatabaseType.class), dataSourceMap,
-                Collections.singleton(dataNodeContainedRule), new ConfigurationProperties(new Properties()));
+        SingleTableRule singleTableRule = new SingleTableRule(
+                new SingleTableRuleConfiguration(), DefaultDatabase.LOGIC_NAME, dataSourceMap, Collections.singleton(dataNodeContainedRule), new ConfigurationProperties(new Properties()));
         assertFalse(singleTableRule.isNeedAccumulate(Collections.emptyList()));
     }
     
     @Test
     public void assertFindLogicTableByActualTable() {
         DataNodeContainedRule dataNodeContainedRule = mock(DataNodeContainedRule.class);
-        SingleTableRule singleTableRule = new SingleTableRule(new SingleTableRuleConfiguration(), DefaultDatabase.LOGIC_NAME, mock(DatabaseType.class), dataSourceMap,
-                Collections.singleton(dataNodeContainedRule), new ConfigurationProperties(new Properties()));
+        SingleTableRule singleTableRule = new SingleTableRule(
+                new SingleTableRuleConfiguration(), DefaultDatabase.LOGIC_NAME, dataSourceMap, Collections.singleton(dataNodeContainedRule), new ConfigurationProperties(new Properties()));
         String actualTable = "student";
         assertFalse(singleTableRule.findLogicTableByActualTable(actualTable).isPresent());
     }
@@ -265,8 +268,8 @@ public final class SingleTableRuleTest {
     @Test
     public void assertFindActualTableByCatalog() {
         DataNodeContainedRule dataNodeContainedRule = mock(DataNodeContainedRule.class);
-        SingleTableRule singleTableRule = new SingleTableRule(new SingleTableRuleConfiguration(), DefaultDatabase.LOGIC_NAME, mock(DatabaseType.class), dataSourceMap,
-                Collections.singleton(dataNodeContainedRule), new ConfigurationProperties(new Properties()));
+        SingleTableRule singleTableRule = new SingleTableRule(
+                new SingleTableRuleConfiguration(), DefaultDatabase.LOGIC_NAME, dataSourceMap, Collections.singleton(dataNodeContainedRule), new ConfigurationProperties(new Properties()));
         String catalog = "employee";
         String logicTable = "t_order_0";
         assertFalse(singleTableRule.findActualTableByCatalog(catalog, logicTable).isPresent());
diff --git a/shardingsphere-kernel/shardingsphere-single-table/shardingsphere-single-table-core/src/test/java/org/apache/shardingsphere/singletable/rule/builder/SingleTableRuleBuilderTest.java b/shardingsphere-kernel/shardingsphere-single-table/shardingsphere-single-table-core/src/test/java/org/apache/shardingsphere/singletable/rule/builder/SingleTableRuleBuilderTest.java
index 945f6445910..e9f93a1e4a5 100644
--- a/shardingsphere-kernel/shardingsphere-single-table/shardingsphere-single-table-core/src/test/java/org/apache/shardingsphere/singletable/rule/builder/SingleTableRuleBuilderTest.java
+++ b/shardingsphere-kernel/shardingsphere-single-table/shardingsphere-single-table-core/src/test/java/org/apache/shardingsphere/singletable/rule/builder/SingleTableRuleBuilderTest.java
@@ -27,7 +27,6 @@ import org.apache.shardingsphere.singletable.config.SingleTableRuleConfiguration
 import org.apache.shardingsphere.singletable.rule.SingleTableRule;
 import org.junit.Test;
 
-import java.util.Collection;
 import java.util.Collections;
 import java.util.Properties;
 
@@ -40,24 +39,19 @@ public final class SingleTableRuleBuilderTest {
     @SuppressWarnings({"rawtypes", "unchecked"})
     @Test
     public void assertBuild() {
-        Collection<DatabaseRuleBuilder> builders = DatabaseRuleBuilderFactory.getInstances();
-        DatabaseRuleBuilder builder = builders.iterator().next();
-        SingleTableRuleConfiguration config = mock(SingleTableRuleConfiguration.class);
-        ShardingSphereRule shardingSphereRule = mock(ShardingSphereRule.class);
-        DatabaseRule databaseRule = builder.build(config, "", Collections.emptyMap(), Collections.singletonList(shardingSphereRule), new ConfigurationProperties(createProperties()));
-        assertThat(databaseRule, instanceOf(SingleTableRule.class));
+        DatabaseRuleBuilder builder = DatabaseRuleBuilderFactory.getInstances().iterator().next();
+        DatabaseRule actual = builder.build(
+                mock(SingleTableRuleConfiguration.class), "", Collections.emptyMap(), Collections.singleton(mock(ShardingSphereRule.class)), new ConfigurationProperties(createProperties()));
+        assertThat(actual, instanceOf(SingleTableRule.class));
     }
     
     @SuppressWarnings({"rawtypes", "unchecked"})
     @Test
     public void assertBuildWithDefaultDataSource() {
-        ShardingSphereRule shardingSphereRule = mock(ShardingSphereRule.class);
-        Collection<DatabaseRuleBuilder> builders = DatabaseRuleBuilderFactory.getInstances();
-        DatabaseRuleBuilder builder = builders.iterator().next();
-        SingleTableRuleConfiguration config = new SingleTableRuleConfiguration();
-        config.setDefaultDataSource("ds_0");
-        DatabaseRule databaseRule = builder.build(config, "", Collections.emptyMap(), Collections.singletonList(shardingSphereRule), new ConfigurationProperties(createProperties()));
-        assertThat(databaseRule, instanceOf(SingleTableRule.class));
+        DatabaseRuleBuilder builder = DatabaseRuleBuilderFactory.getInstances().iterator().next();
+        DatabaseRule actual = builder.build(
+                new SingleTableRuleConfiguration("foo_ds"), "", Collections.emptyMap(), Collections.singleton(mock(ShardingSphereRule.class)), new ConfigurationProperties(createProperties()));
+        assertThat(actual, instanceOf(SingleTableRule.class));
     }
     
     private Properties createProperties() {