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 412dc76838d Move ShardingSphereDatabaseFactory to common module 
(#34388)
412dc76838d is described below

commit 412dc76838d2d9f86aad84dd3a3e165717e500ef
Author: Liang Zhang <[email protected]>
AuthorDate: Sat Jan 18 22:02:18 2025 +0800

    Move ShardingSphereDatabaseFactory to common module (#34388)
    
    * Refactor ExternalMetaDataFactory
    
    * Refactor ExternalMetaDataFactory
    
    * Move ShardingSphereDatabaseFactory to common module
    
    * Move ShardingSphereDatabaseFactory to common module
    
    * Move ShardingSphereDatabaseFactory to common module
    
    * Move ShardingSphereDatabaseFactory to common module
    
    * Move ShardingSphereDatabaseFactory to common module
---
 .../database/ShardingSphereDatabaseFactory.java    | 64 ++++++++++++++---
 .../ShardingSphereDatabaseFactoryTest.java         | 29 +++++---
 .../mode/metadata/MetaDataContextsFactory.java     | 18 +++--
 .../metadata/factory/InternalMetaDataFactory.java  | 83 ----------------------
 .../mode/metadata/MetaDataContextsFactoryTest.java |  9 +--
 .../factory/InternalMetaDataFactoryTest.java       | 83 ----------------------
 6 files changed, 91 insertions(+), 195 deletions(-)

diff --git 
a/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/factory/ExternalMetaDataFactory.java
 
b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/ShardingSphereDatabaseFactory.java
similarity index 60%
rename from 
mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/factory/ExternalMetaDataFactory.java
rename to 
infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/ShardingSphereDatabaseFactory.java
index 8b26f5c6338..ce04d679569 100644
--- 
a/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/factory/ExternalMetaDataFactory.java
+++ 
b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/ShardingSphereDatabaseFactory.java
@@ -15,7 +15,7 @@
  * limitations under the License.
  */
 
-package org.apache.shardingsphere.mode.metadata.factory;
+package org.apache.shardingsphere.infra.metadata.database;
 
 import lombok.AccessLevel;
 import lombok.NoArgsConstructor;
@@ -25,42 +25,88 @@ import 
org.apache.shardingsphere.infra.database.DatabaseTypeEngine;
 import 
org.apache.shardingsphere.infra.database.core.metadata.database.system.SystemDatabase;
 import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
 import org.apache.shardingsphere.infra.instance.ComputeNodeInstanceContext;
-import 
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
+import 
org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema;
 
 import java.sql.SQLException;
+import java.util.Collection;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.Map.Entry;
 import java.util.concurrent.ConcurrentHashMap;
+import java.util.stream.Collectors;
 
 /**
- * External meta data factory.
+ * ShardingSphere database factory.
  */
 @NoArgsConstructor(access = AccessLevel.PRIVATE)
-public final class ExternalMetaDataFactory {
+public final class ShardingSphereDatabaseFactory {
     
     /**
-     * Create database meta data for db.
+     * Create database.
      *
      * @param databaseName database name
      * @param databaseConfig database configuration
      * @param props configuration properties
      * @param computeNodeInstanceContext compute node instance context
-     * @return database meta data
+     * @return created database
      * @throws SQLException SQL exception
      */
     public static ShardingSphereDatabase create(final String databaseName, 
final DatabaseConfiguration databaseConfig,
                                                 final ConfigurationProperties 
props, final ComputeNodeInstanceContext computeNodeInstanceContext) throws 
SQLException {
-        return ShardingSphereDatabase.create(databaseName, 
DatabaseTypeEngine.getProtocolType(databaseConfig, props), databaseConfig, 
props, computeNodeInstanceContext);
+        DatabaseType protocolType = 
DatabaseTypeEngine.getProtocolType(databaseConfig, props);
+        return ShardingSphereDatabase.create(databaseName, protocolType, 
databaseConfig, props, computeNodeInstanceContext);
     }
     
     /**
-     * Create databases meta data for db.
+     * Create database.
+     *
+     * @param databaseName database name
+     * @param databaseConfig database configuration
+     * @param schemas schemas
+     * @param props configuration properties
+     * @param computeNodeInstanceContext compute node instance context
+     * @return created database
+     */
+    public static ShardingSphereDatabase create(final String databaseName, 
final DatabaseConfiguration databaseConfig, final 
Collection<ShardingSphereSchema> schemas,
+                                                final ConfigurationProperties 
props, final ComputeNodeInstanceContext computeNodeInstanceContext) {
+        DatabaseType protocolType = 
DatabaseTypeEngine.getProtocolType(databaseConfig, props);
+        return ShardingSphereDatabase.create(databaseName, protocolType, 
databaseConfig, computeNodeInstanceContext, schemas);
+    }
+    
+    /**
+     * Create databases.
+     *
+     * @param databaseConfigMap database configuration map
+     * @param schemas schemas
+     * @param props properties
+     * @param computeNodeInstanceContext compute node instance context
+     * @return created databases
+     */
+    public static Map<String, ShardingSphereDatabase> create(final Map<String, 
DatabaseConfiguration> databaseConfigMap,
+                                                             final Map<String, 
Collection<ShardingSphereSchema>> schemas,
+                                                             final 
ConfigurationProperties props, final ComputeNodeInstanceContext 
computeNodeInstanceContext) {
+        DatabaseType protocolType = 
DatabaseTypeEngine.getProtocolType(databaseConfigMap, props);
+        return databaseConfigMap.entrySet().stream()
+                .collect(Collectors.toMap(entry -> 
entry.getKey().toLowerCase(),
+                        entry -> create(entry.getKey(), entry.getValue(), 
protocolType, schemas.get(entry.getKey()), props, computeNodeInstanceContext),
+                        (a, b) -> b, () -> new 
ConcurrentHashMap<>(databaseConfigMap.size(), 1F)));
+    }
+    
+    private static ShardingSphereDatabase create(final String databaseName, 
final DatabaseConfiguration databaseConfig, final DatabaseType protocolType,
+                                                 final 
Collection<ShardingSphereSchema> schemas, final ConfigurationProperties props,
+                                                 final 
ComputeNodeInstanceContext computeNodeInstanceContext) {
+        return databaseConfig.getStorageUnits().isEmpty()
+                ? ShardingSphereDatabase.create(databaseName, protocolType, 
props)
+                : create(databaseName, databaseConfig, schemas, props, 
computeNodeInstanceContext);
+    }
+    
+    /**
+     * Create databases.
      *
      * @param databaseConfigMap database configuration map
      * @param props properties
      * @param computeNodeInstanceContext compute node instance context
-     * @return databases
+     * @return created databases
      * @throws SQLException SQL exception
      */
     public static Map<String, ShardingSphereDatabase> create(final Map<String, 
DatabaseConfiguration> databaseConfigMap,
diff --git 
a/mode/core/src/test/java/org/apache/shardingsphere/mode/metadata/factory/ExternalMetaDataFactoryTest.java
 
b/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/ShardingSphereDatabaseFactoryTest.java
similarity index 65%
rename from 
mode/core/src/test/java/org/apache/shardingsphere/mode/metadata/factory/ExternalMetaDataFactoryTest.java
rename to 
infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/ShardingSphereDatabaseFactoryTest.java
index 11134ced64b..892b3939ad3 100644
--- 
a/mode/core/src/test/java/org/apache/shardingsphere/mode/metadata/factory/ExternalMetaDataFactoryTest.java
+++ 
b/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/ShardingSphereDatabaseFactoryTest.java
@@ -15,13 +15,14 @@
  * limitations under the License.
  */
 
-package org.apache.shardingsphere.mode.metadata.factory;
+package org.apache.shardingsphere.infra.metadata.database;
 
 import org.apache.shardingsphere.infra.config.database.DatabaseConfiguration;
 import 
org.apache.shardingsphere.infra.config.database.impl.DataSourceProvidedDatabaseConfiguration;
 import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
+import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
 import org.apache.shardingsphere.infra.instance.ComputeNodeInstanceContext;
-import 
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
+import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
 import org.junit.jupiter.api.Test;
 
 import java.sql.SQLException;
@@ -34,29 +35,39 @@ import static org.hamcrest.MatcherAssert.assertThat;
 import static org.junit.jupiter.api.Assertions.assertTrue;
 import static org.mockito.Mockito.mock;
 
-class ExternalMetaDataFactoryTest {
+class ShardingSphereDatabaseFactoryTest {
     
     @Test
-    void assertCreateSingleDatabase() throws SQLException {
+    void assertCreateDatabase() throws SQLException {
         DatabaseConfiguration databaseConfig = new 
DataSourceProvidedDatabaseConfiguration(Collections.emptyMap(), 
Collections.emptyList());
-        ShardingSphereDatabase actual = 
ExternalMetaDataFactory.create("foo_db", databaseConfig, new 
ConfigurationProperties(new Properties()), 
mock(ComputeNodeInstanceContext.class));
+        ShardingSphereDatabase actual = 
ShardingSphereDatabaseFactory.create("foo_db", databaseConfig, new 
ConfigurationProperties(new Properties()), 
mock(ComputeNodeInstanceContext.class));
         assertThat(actual.getName(), is("foo_db"));
         assertTrue(actual.getResourceMetaData().getStorageUnits().isEmpty());
     }
     
     @Test
-    void assertCreateDatabaseMap() throws SQLException {
+    void assertCreateDatabaseWithSchemas() {
+        ShardingSphereDatabase database = ShardingSphereDatabaseFactory.create(
+                "foo_db", mock(DatabaseConfiguration.class), 
Collections.emptyList(), new ConfigurationProperties(new Properties()), 
mock(ComputeNodeInstanceContext.class));
+        assertThat(database.getName(), is("foo_db"));
+        assertThat(database.getProtocolType(), 
is(TypedSPILoader.getService(DatabaseType.class, "MySQL")));
+        assertThat(database.getRuleMetaData().getRules().size(), is(1));
+        assertTrue(database.getAllSchemas().isEmpty());
+    }
+    
+    @Test
+    void assertCreateDatabases() throws SQLException {
         DatabaseConfiguration databaseConfig = new 
DataSourceProvidedDatabaseConfiguration(Collections.emptyMap(), 
Collections.emptyList());
-        Map<String, ShardingSphereDatabase> actual = 
ExternalMetaDataFactory.create(
+        Map<String, ShardingSphereDatabase> actual = 
ShardingSphereDatabaseFactory.create(
                 Collections.singletonMap("foo_db", databaseConfig), new 
ConfigurationProperties(new Properties()), 
mock(ComputeNodeInstanceContext.class));
         assertTrue(actual.containsKey("foo_db"));
         
assertTrue(actual.get("foo_db").getResourceMetaData().getStorageUnits().isEmpty());
     }
     
     @Test
-    void assertCreateDatabaseMapWhenConfigUppercaseDatabaseName() throws 
SQLException {
+    void assertCreateDatabasesWhenConfigUppercaseDatabaseName() throws 
SQLException {
         DatabaseConfiguration databaseConfig = new 
DataSourceProvidedDatabaseConfiguration(Collections.emptyMap(), 
Collections.emptyList());
-        Map<String, ShardingSphereDatabase> actual = 
ExternalMetaDataFactory.create(
+        Map<String, ShardingSphereDatabase> actual = 
ShardingSphereDatabaseFactory.create(
                 Collections.singletonMap("FOO_DB", databaseConfig), new 
ConfigurationProperties(new Properties()), 
mock(ComputeNodeInstanceContext.class));
         assertTrue(actual.containsKey("foo_db"));
         
assertTrue(actual.get("foo_db").getResourceMetaData().getStorageUnits().isEmpty());
diff --git 
a/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/MetaDataContextsFactory.java
 
b/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/MetaDataContextsFactory.java
index fc3afde4e40..cbff980db1d 100644
--- 
a/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/MetaDataContextsFactory.java
+++ 
b/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/MetaDataContextsFactory.java
@@ -35,13 +35,13 @@ import 
org.apache.shardingsphere.infra.metadata.database.resource.ResourceMetaDa
 import 
org.apache.shardingsphere.infra.metadata.database.resource.node.StorageNode;
 import 
org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnit;
 import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData;
+import 
org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema;
 import 
org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereDatabaseData;
 import 
org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereSchemaData;
 import 
org.apache.shardingsphere.infra.metadata.statistics.builder.ShardingSphereStatisticsFactory;
 import org.apache.shardingsphere.infra.rule.builder.global.GlobalRulesBuilder;
 import org.apache.shardingsphere.mode.manager.ContextManagerBuilderParameter;
-import org.apache.shardingsphere.mode.metadata.factory.ExternalMetaDataFactory;
-import org.apache.shardingsphere.mode.metadata.factory.InternalMetaDataFactory;
+import 
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabaseFactory;
 import org.apache.shardingsphere.mode.metadata.manager.SwitchingResource;
 import org.apache.shardingsphere.mode.metadata.persist.MetaDataPersistService;
 
@@ -81,7 +81,7 @@ public final class MetaDataContextsFactory {
                                                   final 
ContextManagerBuilderParameter param, final ComputeNodeInstanceContext 
instanceContext) throws SQLException {
         Collection<RuleConfiguration> globalRuleConfigs = 
param.getGlobalRuleConfigs();
         ConfigurationProperties props = new 
ConfigurationProperties(param.getProps());
-        Map<String, ShardingSphereDatabase> databases = 
ExternalMetaDataFactory.create(param.getDatabaseConfigs(), props, 
instanceContext);
+        Map<String, ShardingSphereDatabase> databases = 
ShardingSphereDatabaseFactory.create(param.getDatabaseConfigs(), props, 
instanceContext);
         MetaDataContexts result = newMetaDataContexts(persistService, param, 
globalRuleConfigs, databases, props);
         persistDatabaseConfigurations(result, param, persistService);
         persistMetaData(result, persistService);
@@ -93,8 +93,12 @@ public final class MetaDataContextsFactory {
                 getDatabaseNames(instanceContext, param.getDatabaseConfigs(), 
persistService), param.getDatabaseConfigs(), persistService);
         Collection<RuleConfiguration> globalRuleConfigs = 
persistService.getGlobalRuleService().load();
         ConfigurationProperties props = new 
ConfigurationProperties(persistService.getPropsService().load());
-        Map<String, ShardingSphereDatabase> databases = 
InternalMetaDataFactory.create(persistService, effectiveDatabaseConfigs, props, 
instanceContext);
-        return newMetaDataContexts(persistService, param, globalRuleConfigs, 
databases, props);
+        return newMetaDataContexts(persistService, param, globalRuleConfigs,
+                ShardingSphereDatabaseFactory.create(effectiveDatabaseConfigs, 
loadSchemas(persistService, effectiveDatabaseConfigs.keySet()), props, 
instanceContext), props);
+    }
+    
+    private static Map<String, Collection<ShardingSphereSchema>> 
loadSchemas(final MetaDataPersistService persistService, final 
Collection<String> databaseNames) {
+        return databaseNames.stream().collect(Collectors.toMap(each -> each, 
each -> persistService.getDatabaseMetaDataFacade().getSchema().load(each)));
     }
     
     private static MetaDataContexts newMetaDataContexts(final 
MetaDataPersistService persistService, final ContextManagerBuilderParameter 
param,
@@ -249,8 +253,8 @@ public final class MetaDataContextsFactory {
                                                                 final 
DatabaseConfiguration databaseConfig, final ConfigurationProperties props,
                                                                 final 
ComputeNodeInstanceContext instanceContext) throws SQLException {
         return internalLoadMetaData
-                ? InternalMetaDataFactory.create(databaseName, persistService, 
databaseConfig, props, instanceContext)
-                : ExternalMetaDataFactory.create(databaseName, databaseConfig, 
props, instanceContext);
+                ? ShardingSphereDatabaseFactory.create(databaseName, 
databaseConfig, 
persistService.getDatabaseMetaDataFacade().getSchema().load(databaseName), 
props, instanceContext)
+                : ShardingSphereDatabaseFactory.create(databaseName, 
databaseConfig, props, instanceContext);
     }
     
     private static ResourceMetaData getEffectiveResourceMetaData(final 
ShardingSphereDatabase database, final SwitchingResource resource) {
diff --git 
a/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/factory/InternalMetaDataFactory.java
 
b/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/factory/InternalMetaDataFactory.java
deleted file mode 100644
index e58527ba465..00000000000
--- 
a/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/factory/InternalMetaDataFactory.java
+++ /dev/null
@@ -1,83 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements.  See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License.  You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.shardingsphere.mode.metadata.factory;
-
-import lombok.AccessLevel;
-import lombok.NoArgsConstructor;
-import org.apache.shardingsphere.infra.config.database.DatabaseConfiguration;
-import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
-import org.apache.shardingsphere.infra.database.DatabaseTypeEngine;
-import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
-import org.apache.shardingsphere.infra.instance.ComputeNodeInstanceContext;
-import 
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
-import org.apache.shardingsphere.mode.metadata.persist.MetaDataPersistService;
-
-import java.util.Map;
-import java.util.Map.Entry;
-import java.util.concurrent.ConcurrentHashMap;
-
-/**
- * Internal meta data factory.
- */
-@NoArgsConstructor(access = AccessLevel.PRIVATE)
-public final class InternalMetaDataFactory {
-    
-    /**
-     * Create database meta data from governance center.
-     *
-     * @param databaseName database name
-     * @param persistService meta data persist service
-     * @param databaseConfig database configuration
-     * @param props configuration properties
-     * @param computeNodeInstanceContext compute node instance context
-     * @return database
-     */
-    public static ShardingSphereDatabase create(final String databaseName, 
final MetaDataPersistService persistService, final DatabaseConfiguration 
databaseConfig,
-                                                final ConfigurationProperties 
props, final ComputeNodeInstanceContext computeNodeInstanceContext) {
-        DatabaseType protocolType = 
DatabaseTypeEngine.getProtocolType(databaseConfig, props);
-        return ShardingSphereDatabase.create(databaseName,
-                protocolType, databaseConfig, computeNodeInstanceContext, 
persistService.getDatabaseMetaDataFacade().getSchema().load(databaseName));
-    }
-    
-    /**
-     * Create databases meta data from governance center.
-     *
-     * @param persistService meta data persist service
-     * @param databaseConfigMap database configuration map
-     * @param props properties
-     * @param computeNodeInstanceContext compute node instance context
-     * @return databases
-     */
-    public static Map<String, ShardingSphereDatabase> create(final 
MetaDataPersistService persistService, final Map<String, DatabaseConfiguration> 
databaseConfigMap,
-                                                             final 
ConfigurationProperties props, final ComputeNodeInstanceContext 
computeNodeInstanceContext) {
-        return createDatabases(persistService, databaseConfigMap, 
DatabaseTypeEngine.getProtocolType(databaseConfigMap, props), props, 
computeNodeInstanceContext);
-    }
-    
-    private static Map<String, ShardingSphereDatabase> createDatabases(final 
MetaDataPersistService persistService, final Map<String, DatabaseConfiguration> 
databaseConfigMap,
-                                                                       final 
DatabaseType protocolType, final ConfigurationProperties props,
-                                                                       final 
ComputeNodeInstanceContext computeNodeInstanceContext) {
-        Map<String, ShardingSphereDatabase> result = new 
ConcurrentHashMap<>(databaseConfigMap.size(), 1F);
-        for (Entry<String, DatabaseConfiguration> entry : 
databaseConfigMap.entrySet()) {
-            String databaseName = entry.getKey();
-            result.put(databaseName.toLowerCase(), 
entry.getValue().getStorageUnits().isEmpty()
-                    ? ShardingSphereDatabase.create(databaseName, 
protocolType, props)
-                    : create(databaseName, persistService, entry.getValue(), 
props, computeNodeInstanceContext));
-        }
-        return result;
-    }
-}
diff --git 
a/mode/core/src/test/java/org/apache/shardingsphere/mode/metadata/MetaDataContextsFactoryTest.java
 
b/mode/core/src/test/java/org/apache/shardingsphere/mode/metadata/MetaDataContextsFactoryTest.java
index af2e7a14618..60c6330b335 100644
--- 
a/mode/core/src/test/java/org/apache/shardingsphere/mode/metadata/MetaDataContextsFactoryTest.java
+++ 
b/mode/core/src/test/java/org/apache/shardingsphere/mode/metadata/MetaDataContextsFactoryTest.java
@@ -26,7 +26,7 @@ import 
org.apache.shardingsphere.infra.instance.metadata.jdbc.JDBCInstanceMetaDa
 import 
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
 import org.apache.shardingsphere.infra.rule.builder.global.GlobalRulesBuilder;
 import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
-import org.apache.shardingsphere.mode.metadata.factory.ExternalMetaDataFactory;
+import 
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabaseFactory;
 import org.apache.shardingsphere.mode.metadata.persist.MetaDataPersistService;
 import 
org.apache.shardingsphere.mode.metadata.persist.service.config.database.DatabaseRulePersistService;
 import 
org.apache.shardingsphere.mode.metadata.persist.service.config.global.GlobalRulePersistService;
@@ -63,7 +63,7 @@ import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
 
 @ExtendWith(AutoMockExtension.class)
-@StaticMockSettings({ExternalMetaDataFactory.class, GlobalRulesBuilder.class})
+@StaticMockSettings({ShardingSphereDatabaseFactory.class, 
GlobalRulesBuilder.class})
 @MockitoSettings(strictness = Strictness.LENIENT)
 class MetaDataContextsFactoryTest {
     
@@ -74,7 +74,7 @@ class MetaDataContextsFactoryTest {
     private DatabaseMetaDataPersistFacade databaseMetaDataPersistFacade;
     
     @BeforeEach
-    void setUp() throws SQLException {
+    void setUp() {
         
when(metaDataPersistService.loadDataSourceConfigurations("foo_db")).thenReturn(Collections.emptyMap());
         DatabaseRulePersistService databaseRulePersistService = 
mockDatabaseRulePersistService();
         
when(metaDataPersistService.getDatabaseRulePersistService()).thenReturn(databaseRulePersistService);
@@ -85,9 +85,10 @@ class MetaDataContextsFactoryTest {
         
when(metaDataPersistService.getPropsService()).thenReturn(propertiesPersistService);
         
when(metaDataPersistService.getDatabaseMetaDataFacade()).thenReturn(databaseMetaDataPersistFacade);
         ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, 
RETURNS_DEEP_STUBS);
+        when(database.getName()).thenReturn("foo_db");
         
when(database.getProtocolType()).thenReturn(TypedSPILoader.getService(DatabaseType.class,
 "FIXTURE"));
         
when(database.getRuleMetaData().getRules()).thenReturn(Collections.emptyList());
-        when(ExternalMetaDataFactory.create(anyMap(), any(), 
any())).thenReturn(new HashMap<>(Collections.singletonMap("foo_db", database)));
+        when(ShardingSphereDatabaseFactory.create(anyMap(), anyMap(), any(), 
any())).thenReturn(new HashMap<>(Collections.singletonMap("foo_db", database)));
         when(GlobalRulesBuilder.buildRules(anyCollection(), anyCollection(), 
any(ConfigurationProperties.class))).thenReturn(Collections.singleton(new 
MockedRule()));
     }
     
diff --git 
a/mode/core/src/test/java/org/apache/shardingsphere/mode/metadata/factory/InternalMetaDataFactoryTest.java
 
b/mode/core/src/test/java/org/apache/shardingsphere/mode/metadata/factory/InternalMetaDataFactoryTest.java
deleted file mode 100644
index e5b1852c301..00000000000
--- 
a/mode/core/src/test/java/org/apache/shardingsphere/mode/metadata/factory/InternalMetaDataFactoryTest.java
+++ /dev/null
@@ -1,83 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements.  See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License.  You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.shardingsphere.mode.metadata.factory;
-
-import org.apache.shardingsphere.infra.config.database.DatabaseConfiguration;
-import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
-import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
-import org.apache.shardingsphere.infra.instance.ComputeNodeInstanceContext;
-import 
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
-import 
org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnit;
-import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
-import org.apache.shardingsphere.mode.metadata.persist.MetaDataPersistService;
-import org.apache.shardingsphere.test.fixture.jdbc.MockedDataSource;
-import org.junit.jupiter.api.Test;
-
-import java.util.Collections;
-import java.util.Map;
-import java.util.Properties;
-
-import static org.hamcrest.CoreMatchers.is;
-import static org.hamcrest.MatcherAssert.assertThat;
-import static org.junit.jupiter.api.Assertions.assertTrue;
-import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
-
-class InternalMetaDataFactoryTest {
-    
-    @Test
-    void assertCreateWithDatabaseName() {
-        MetaDataPersistService persistService = 
mock(MetaDataPersistService.class, RETURNS_DEEP_STUBS);
-        
when(persistService.getDatabaseMetaDataFacade().getSchema().load("foo_db")).thenReturn(Collections.emptyList());
-        ShardingSphereDatabase database = InternalMetaDataFactory.create(
-                "foo_db", persistService, mock(DatabaseConfiguration.class), 
new ConfigurationProperties(new Properties()), 
mock(ComputeNodeInstanceContext.class));
-        assertThat(database.getName(), is("foo_db"));
-        assertThat(database.getProtocolType(), 
is(TypedSPILoader.getService(DatabaseType.class, "MySQL")));
-        assertTrue(database.getRuleMetaData().getRules().isEmpty());
-        assertTrue(database.getAllSchemas().isEmpty());
-    }
-    
-    @Test
-    void assertCreateWithDatabasesWithoutStorageUnits() {
-        Map<String, ShardingSphereDatabase> databases = 
InternalMetaDataFactory.create(mock(MetaDataPersistService.class, 
RETURNS_DEEP_STUBS),
-                Collections.singletonMap("foo_db", 
mock(DatabaseConfiguration.class)), new ConfigurationProperties(new 
Properties()), mock(ComputeNodeInstanceContext.class));
-        assertThat(databases.size(), is(1));
-        assertThat(databases.get("foo_db").getName(), is("foo_db"));
-        assertThat(databases.get("foo_db").getProtocolType(), 
is(TypedSPILoader.getService(DatabaseType.class, "MySQL")));
-        
assertTrue(databases.get("foo_db").getRuleMetaData().getRules().isEmpty());
-        assertTrue(databases.get("foo_db").getAllSchemas().isEmpty());
-    }
-    
-    @Test
-    void assertCreateWithDatabasesWithStorageUnits() {
-        MetaDataPersistService persistService = 
mock(MetaDataPersistService.class, RETURNS_DEEP_STUBS);
-        
when(persistService.getDatabaseMetaDataFacade().getSchema().load("foo_db")).thenReturn(Collections.emptyList());
-        DatabaseConfiguration databaseConfig = 
mock(DatabaseConfiguration.class);
-        StorageUnit storageUnit = mock(StorageUnit.class);
-        when(storageUnit.getDataSource()).thenReturn(new MockedDataSource());
-        
when(databaseConfig.getStorageUnits()).thenReturn(Collections.singletonMap("foo_ds",
 storageUnit));
-        Map<String, ShardingSphereDatabase> databases = 
InternalMetaDataFactory.create(
-                persistService, Collections.singletonMap("foo_db", 
databaseConfig), new ConfigurationProperties(new Properties()), 
mock(ComputeNodeInstanceContext.class));
-        assertThat(databases.size(), is(1));
-        assertThat(databases.get("foo_db").getName(), is("foo_db"));
-        assertThat(databases.get("foo_db").getProtocolType(), 
is(TypedSPILoader.getService(DatabaseType.class, "FIXTURE")));
-        
assertTrue(databases.get("foo_db").getRuleMetaData().getRules().isEmpty());
-        assertTrue(databases.get("foo_db").getAllSchemas().isEmpty());
-    }
-}

Reply via email to