From 73a6c78ef7f1dc5ba77b51f76c23a7af55937972 Mon Sep 17 00:00:00 2001 From: Liang Zhang Date: Sun, 24 Sep 2023 15:43:59 +0800 Subject: [PATCH] Refactor ShardingSphereMetaData (#28563) * Refactor ShardingSphereMetaData * Refactor ShardingSphereMetaData --- .../metadata/ShardingSphereMetaData.java | 29 ++++++----------- .../metadata/ShardingSphereMetaDataTest.java | 21 ------------- .../database/ShardingSphereDatabaseTest.java | 31 +++++++++++++++++++ 3 files changed, 40 insertions(+), 41 deletions(-) diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/ShardingSphereMetaData.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/ShardingSphereMetaData.java index 6b01992d6ee24..edc56a025b2ec 100644 --- a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/ShardingSphereMetaData.java +++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/ShardingSphereMetaData.java @@ -51,8 +51,7 @@ public final class ShardingSphereMetaData { private final TemporaryConfigurationProperties temporaryProps; public ShardingSphereMetaData() { - this(new LinkedHashMap<>(), new ResourceMetaData(Collections.emptyMap()), new RuleMetaData(Collections.emptyList()), - new ConfigurationProperties(new Properties())); + this(new LinkedHashMap<>(), new ResourceMetaData(Collections.emptyMap()), new RuleMetaData(Collections.emptyList()), new ConfigurationProperties(new Properties())); } public ShardingSphereMetaData(final Map databases, final ResourceMetaData globalResourceMetaData, @@ -65,19 +64,6 @@ public ShardingSphereMetaData(final Map database temporaryProps = new TemporaryConfigurationProperties(props.getProps()); } - /** - * Add database. - * - * @param databaseName database name - * @param protocolType protocol database type - * @param props configuration properties - */ - public void addDatabase(final String databaseName, final DatabaseType protocolType, final ConfigurationProperties props) { - ShardingSphereDatabase database = ShardingSphereDatabase.create(databaseName, protocolType, props); - putDatabase(database); - globalRuleMetaData.findRules(ResourceHeldRule.class).forEach(each -> each.addResource(database)); - } - /** * Judge contains database from meta data or not. * @@ -99,12 +85,16 @@ public ShardingSphereDatabase getDatabase(final String databaseName) { } /** - * Put database. + * Add database. * - * @param database database + * @param databaseName database name + * @param protocolType protocol database type + * @param props configuration properties */ - public void putDatabase(final ShardingSphereDatabase database) { + public void addDatabase(final String databaseName, final DatabaseType protocolType, final ConfigurationProperties props) { + ShardingSphereDatabase database = ShardingSphereDatabase.create(databaseName, protocolType, props); databases.put(database.getName().toLowerCase(), database); + globalRuleMetaData.findRules(ResourceHeldRule.class).forEach(each -> each.addResource(database)); } /** @@ -113,8 +103,7 @@ public void putDatabase(final ShardingSphereDatabase database) { * @param databaseName database name */ public void dropDatabase(final String databaseName) { - ShardingSphereDatabase toBeRemovedDatabase = databases.remove(databaseName.toLowerCase()); - closeResources(toBeRemovedDatabase); + closeResources(databases.remove(databaseName.toLowerCase())); } private void closeResources(final ShardingSphereDatabase database) { diff --git a/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/ShardingSphereMetaDataTest.java b/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/ShardingSphereMetaDataTest.java index da1846beea8d8..0b97e1b9c906e 100644 --- a/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/ShardingSphereMetaDataTest.java +++ b/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/ShardingSphereMetaDataTest.java @@ -17,16 +17,13 @@ package org.apache.shardingsphere.infra.metadata; -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.InstanceContext; import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; import org.apache.shardingsphere.infra.metadata.database.resource.ResourceMetaData; import org.apache.shardingsphere.infra.metadata.database.resource.storage.StorageUnit; import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData; import org.apache.shardingsphere.infra.rule.identifier.type.ResourceHeldRule; -import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader; import org.apache.shardingsphere.test.fixture.jdbc.MockedDataSource; import org.apache.shardingsphere.test.mock.AutoMockExtension; import org.apache.shardingsphere.test.mock.StaticMockSettings; @@ -36,7 +33,6 @@ import org.mockito.quality.Strictness; import javax.sql.DataSource; -import java.sql.SQLException; import java.util.Collections; import java.util.HashMap; import java.util.Map; @@ -44,7 +40,6 @@ import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; -import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.Mockito.RETURNS_DEEP_STUBS; import static org.mockito.Mockito.mock; @@ -96,20 +91,4 @@ private ShardingSphereDatabase mockDatabase(final ResourceMetaData resourceMetaD when(result.getRuleMetaData()).thenReturn(new RuleMetaData(Collections.singleton(databaseResourceHeldRule))); return result; } - - @Test - void assertGetPostgreSQLDefaultSchema() throws SQLException { - DatabaseType databaseType = TypedSPILoader.getService(DatabaseType.class, "PostgreSQL"); - ShardingSphereDatabase actual = ShardingSphereDatabase.create("foo_db", databaseType, Collections.singletonMap("", databaseType), - mock(DataSourceProvidedDatabaseConfiguration.class), new ConfigurationProperties(new Properties()), mock(InstanceContext.class)); - assertNotNull(actual.getSchema("public")); - } - - @Test - void assertGetMySQLDefaultSchema() throws SQLException { - DatabaseType databaseType = TypedSPILoader.getService(DatabaseType.class, "MySQL"); - ShardingSphereDatabase actual = ShardingSphereDatabase.create("foo_db", databaseType, Collections.singletonMap("", databaseType), - mock(DataSourceProvidedDatabaseConfiguration.class), new ConfigurationProperties(new Properties()), mock(InstanceContext.class)); - assertNotNull(actual.getSchema("foo_db")); - } } diff --git a/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/ShardingSphereDatabaseTest.java b/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/ShardingSphereDatabaseTest.java index 6b8b899ec1b40..a96960679cb8d 100644 --- a/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/ShardingSphereDatabaseTest.java +++ b/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/ShardingSphereDatabaseTest.java @@ -17,27 +17,42 @@ package org.apache.shardingsphere.infra.metadata.database; +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.InstanceContext; import org.apache.shardingsphere.infra.metadata.database.resource.ResourceMetaData; import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData; import org.apache.shardingsphere.infra.rule.ShardingSphereRule; import org.apache.shardingsphere.infra.rule.identifier.type.DataSourceContainedRule; import org.apache.shardingsphere.infra.rule.identifier.type.MutableDataNodeRule; import org.apache.shardingsphere.infra.rule.identifier.type.TableContainedRule; +import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader; import org.apache.shardingsphere.test.fixture.jdbc.MockedDataSource; +import org.apache.shardingsphere.test.mock.AutoMockExtension; +import org.apache.shardingsphere.test.mock.StaticMockSettings; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.junit.jupiter.MockitoSettings; +import org.mockito.quality.Strictness; +import java.sql.SQLException; import java.util.Collection; import java.util.Collections; import java.util.LinkedList; +import java.util.Properties; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.Mockito.RETURNS_DEEP_STUBS; import static org.mockito.Mockito.mock; +@ExtendWith(AutoMockExtension.class) +@StaticMockSettings(ShardingSphereDatabase.class) +@MockitoSettings(strictness = Strictness.LENIENT) class ShardingSphereDatabaseTest { @Test @@ -73,4 +88,20 @@ void assertReloadRules() { database.reloadRules(MutableDataNodeRule.class); assertThat(database.getRuleMetaData().getRules().size(), is(3)); } + + @Test + void assertGetPostgreSQLDefaultSchema() throws SQLException { + DatabaseType databaseType = TypedSPILoader.getService(DatabaseType.class, "PostgreSQL"); + ShardingSphereDatabase actual = ShardingSphereDatabase.create("foo_db", databaseType, Collections.singletonMap("", databaseType), + mock(DataSourceProvidedDatabaseConfiguration.class), new ConfigurationProperties(new Properties()), mock(InstanceContext.class)); + assertNotNull(actual.getSchema("public")); + } + + @Test + void assertGetMySQLDefaultSchema() throws SQLException { + DatabaseType databaseType = TypedSPILoader.getService(DatabaseType.class, "MySQL"); + ShardingSphereDatabase actual = ShardingSphereDatabase.create("foo_db", databaseType, Collections.singletonMap("", databaseType), + mock(DataSourceProvidedDatabaseConfiguration.class), new ConfigurationProperties(new Properties()), mock(InstanceContext.class)); + assertNotNull(actual.getSchema("foo_db")); + } }