diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/ShardingSphereDatabase.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/ShardingSphereDatabase.java index 2af78880a553a..941f270b12ef1 100644 --- a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/ShardingSphereDatabase.java +++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/ShardingSphereDatabase.java @@ -121,13 +121,13 @@ public static ShardingSphereDatabase create(final String name, final DatabaseTyp */ public static ShardingSphereDatabase create(final String name, final DatabaseType protocolType, final DatabaseConfiguration databaseConfig, final Collection rules, final Map schemas) { - ResourceMetaData resourceMetaData = createResourceMetaData(name, databaseConfig.getStorageResource(), databaseConfig.getDataSourcePoolPropertiesMap()); + ResourceMetaData resourceMetaData = createResourceMetaData(databaseConfig.getStorageResource(), databaseConfig.getDataSourcePoolPropertiesMap()); RuleMetaData ruleMetaData = new RuleMetaData(rules); return new ShardingSphereDatabase(name, protocolType, resourceMetaData, ruleMetaData, schemas); } - private static ResourceMetaData createResourceMetaData(final String databaseName, final StorageResource storageResource, final Map propsMap) { - return new ResourceMetaData(databaseName, storageResource.getDataSources(), storageResource.getStorageUnitNodeMap(), propsMap); + private static ResourceMetaData createResourceMetaData(final StorageResource storageResource, final Map propsMap) { + return new ResourceMetaData(storageResource.getDataSources(), storageResource.getStorageUnitNodeMap(), propsMap); } /** diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/ResourceMetaData.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/ResourceMetaData.java index f2b2139c113ed..bd048bd62b70a 100644 --- a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/ResourceMetaData.java +++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/ResourceMetaData.java @@ -51,16 +51,15 @@ public ResourceMetaData(final Map dataSources) { Collectors.toMap(Entry::getKey, entry -> DataSourcePoolPropertiesCreator.create(entry.getValue()), (oldValue, currentValue) -> oldValue, LinkedHashMap::new)); storageUnits = new LinkedHashMap<>(); for (Entry entry : storageNodes.entrySet()) { - storageUnits.put(entry.getKey(), new StorageUnit(null, entry.getValue(), dataSourcePoolPropsMap.get(entry.getKey()), dataSources.get(entry.getValue().getName().getName()))); + storageUnits.put(entry.getKey(), new StorageUnit(entry.getValue(), dataSourcePoolPropsMap.get(entry.getKey()), dataSources.get(entry.getValue().getName().getName()))); } } - public ResourceMetaData(final String databaseName, final Map dataSources, - final Map storageNodes, final Map dataSourcePoolPropsMap) { + public ResourceMetaData(final Map dataSources, final Map storageNodes, final Map dataSourcePoolPropsMap) { this.dataSources = dataSources; storageUnits = new LinkedHashMap<>(); for (Entry entry : storageNodes.entrySet()) { - storageUnits.put(entry.getKey(), new StorageUnit(databaseName, entry.getValue(), dataSourcePoolPropsMap.get(entry.getKey()), dataSources.get(entry.getValue().getName()))); + storageUnits.put(entry.getKey(), new StorageUnit(entry.getValue(), dataSourcePoolPropsMap.get(entry.getKey()), dataSources.get(entry.getValue().getName()))); } } diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/node/StorageNode.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/node/StorageNode.java index fa836f44b44f9..4e0ef8136b32d 100644 --- a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/node/StorageNode.java +++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/node/StorageNode.java @@ -32,8 +32,4 @@ public final class StorageNode { private final String url; private final String catalog; - - public StorageNode(final StorageNodeName name, final String url) { - this(name, url, null); - } } diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/unit/StorageUnit.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/unit/StorageUnit.java index ebaec412936e1..202a391d91fa1 100644 --- a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/unit/StorageUnit.java +++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/unit/StorageUnit.java @@ -26,10 +26,8 @@ import org.apache.shardingsphere.infra.datasource.pool.CatalogSwitchableDataSource; import org.apache.shardingsphere.infra.datasource.pool.props.domain.DataSourcePoolProperties; import org.apache.shardingsphere.infra.metadata.database.resource.node.StorageNode; -import org.apache.shardingsphere.infra.state.datasource.DataSourceStateManager; import javax.sql.DataSource; -import java.util.Collections; import java.util.Map; /** @@ -48,21 +46,17 @@ public final class StorageUnit { private final ConnectionProperties connectionProperties; - public StorageUnit(final String databaseName, final StorageNode storageNode, final DataSourcePoolProperties dataSourcePoolProperties, final DataSource dataSource) { + public StorageUnit(final StorageNode storageNode, final DataSourcePoolProperties dataSourcePoolProperties, final DataSource dataSource) { this.storageNode = storageNode; this.dataSource = new CatalogSwitchableDataSource(dataSource, storageNode.getCatalog(), storageNode.getUrl()); this.dataSourcePoolProperties = dataSourcePoolProperties; storageType = DatabaseTypeFactory.get(storageNode.getUrl()); - boolean isDataSourceEnabled = !DataSourceStateManager.getInstance().getEnabledDataSources(databaseName, Collections.singletonMap(storageNode.getName().getName(), dataSource)).isEmpty(); - connectionProperties = createConnectionProperties(isDataSourceEnabled, storageNode); + connectionProperties = createConnectionProperties(storageNode); } - private ConnectionProperties createConnectionProperties(final boolean isDataSourceEnabled, final StorageNode storageNode) { - if (!isDataSourceEnabled) { - return null; - } + private ConnectionProperties createConnectionProperties(final StorageNode storageNode) { Map standardProps = dataSourcePoolProperties.getConnectionPropertySynonyms().getStandardProperties(); ConnectionPropertiesParser parser = DatabaseTypedSPILoader.getService(ConnectionPropertiesParser.class, storageType); - return parser.parse(standardProps.getOrDefault("url", "").toString(), standardProps.getOrDefault("username", "").toString(), storageNode.getCatalog()); + return parser.parse(storageNode.getUrl(), standardProps.getOrDefault("username", "").toString(), storageNode.getCatalog()); } } diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/unit/StorageUnitNodeMapUtils.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/unit/StorageUnitNodeMapUtils.java index 8217a4ca8486d..67d885f668535 100644 --- a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/unit/StorageUnitNodeMapUtils.java +++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/unit/StorageUnitNodeMapUtils.java @@ -55,7 +55,8 @@ public static Map fromDataSources(final Map storageUnits = new HashMap<>(2, 1F); DataSourcePoolProperties dataSourcePoolProps = mock(DataSourcePoolProperties.class, RETURNS_DEEP_STUBS); when(dataSourcePoolProps.getConnectionPropertySynonyms().getStandardProperties()).thenReturn(Collections.emptyMap()); - storageUnits.put("ds_0", new StorageUnit("foo_db", new StorageNode(mock(StorageNodeName.class), "jdbc:mock://127.0.0.1/ds_0"), dataSourcePoolProps, new MockedDataSource())); - storageUnits.put("ds_1", new StorageUnit("foo_db", new StorageNode(mock(StorageNodeName.class), "jdbc:mock://127.0.0.1/ds_1"), dataSourcePoolProps, new MockedDataSource())); + storageUnits.put("ds_0", new StorageUnit(new StorageNode(mock(StorageNodeName.class), "jdbc:mock://127.0.0.1/ds_0", null), dataSourcePoolProps, new MockedDataSource())); + storageUnits.put("ds_1", new StorageUnit(new StorageNode(mock(StorageNodeName.class), "jdbc:mock://127.0.0.1/ds_1", null), dataSourcePoolProps, new MockedDataSource())); ShardingSphereDatabase result = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); when(result.getResourceMetaData().getStorageUnits()).thenReturn(storageUnits); when(result.getName()).thenReturn(DefaultDatabase.LOGIC_NAME); diff --git a/kernel/transaction/core/src/test/java/org/apache/shardingsphere/transaction/rule/TransactionRuleTest.java b/kernel/transaction/core/src/test/java/org/apache/shardingsphere/transaction/rule/TransactionRuleTest.java index 2119a6a44bff7..9a9459b1dc1ae 100644 --- a/kernel/transaction/core/src/test/java/org/apache/shardingsphere/transaction/rule/TransactionRuleTest.java +++ b/kernel/transaction/core/src/test/java/org/apache/shardingsphere/transaction/rule/TransactionRuleTest.java @@ -102,8 +102,8 @@ private ResourceMetaData createResourceMetaData() { Map storageUnits = new HashMap<>(2, 1F); DataSourcePoolProperties dataSourcePoolProps = mock(DataSourcePoolProperties.class, RETURNS_DEEP_STUBS); when(dataSourcePoolProps.getConnectionPropertySynonyms().getStandardProperties()).thenReturn(Collections.emptyMap()); - storageUnits.put("ds_0", new StorageUnit("foo_db", new StorageNode(mock(StorageNodeName.class), "jdbc:mock://127.0.0.1/ds_0"), dataSourcePoolProps, new MockedDataSource())); - storageUnits.put("ds_1", new StorageUnit("foo_db", new StorageNode(mock(StorageNodeName.class), "jdbc:mock://127.0.0.1/ds_1"), dataSourcePoolProps, new MockedDataSource())); + storageUnits.put("ds_0", new StorageUnit(new StorageNode(mock(StorageNodeName.class), "jdbc:mock://127.0.0.1/ds_0", null), dataSourcePoolProps, new MockedDataSource())); + storageUnits.put("ds_1", new StorageUnit(new StorageNode(mock(StorageNodeName.class), "jdbc:mock://127.0.0.1/ds_1", null), dataSourcePoolProps, new MockedDataSource())); ResourceMetaData result = mock(ResourceMetaData.class, RETURNS_DEEP_STUBS); when(result.getStorageUnits()).thenReturn(storageUnits); return result; @@ -121,8 +121,8 @@ private ResourceMetaData createAddResourceMetaData() { Map storageUnits = new HashMap<>(2, 1F); DataSourcePoolProperties dataSourcePoolProps = mock(DataSourcePoolProperties.class, RETURNS_DEEP_STUBS); when(dataSourcePoolProps.getConnectionPropertySynonyms().getStandardProperties()).thenReturn(Collections.emptyMap()); - storageUnits.put("ds_0", new StorageUnit("foo_db", new StorageNode(mock(StorageNodeName.class), "jdbc:mock://127.0.0.1/ds_0"), dataSourcePoolProps, new MockedDataSource())); - storageUnits.put("ds_1", new StorageUnit("foo_db", new StorageNode(mock(StorageNodeName.class), "jdbc:mock://127.0.0.1/ds_1"), dataSourcePoolProps, new MockedDataSource())); + storageUnits.put("ds_0", new StorageUnit(new StorageNode(mock(StorageNodeName.class), "jdbc:mock://127.0.0.1/ds_0", null), dataSourcePoolProps, new MockedDataSource())); + storageUnits.put("ds_1", new StorageUnit(new StorageNode(mock(StorageNodeName.class), "jdbc:mock://127.0.0.1/ds_1", null), dataSourcePoolProps, new MockedDataSource())); ResourceMetaData result = mock(ResourceMetaData.class, RETURNS_DEEP_STUBS); when(result.getStorageUnits()).thenReturn(storageUnits); return result; diff --git a/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/context/ConfigurationContextManager.java b/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/context/ConfigurationContextManager.java index bf51c6411a17b..2e38aeafa2706 100644 --- a/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/context/ConfigurationContextManager.java +++ b/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/context/ConfigurationContextManager.java @@ -283,7 +283,7 @@ public Map renewDatabase(final ShardingSphereDat Map propsMap = database.getResourceMetaData().getStorageUnits().entrySet().stream() .collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getDataSourcePoolProperties(), (oldValue, currentValue) -> currentValue, LinkedHashMap::new)); return Collections.singletonMap(database.getName().toLowerCase(), new ShardingSphereDatabase(database.getName(), database.getProtocolType(), - new ResourceMetaData(database.getName(), newStorageNodes, newStorageUnitNodeMap, propsMap), database.getRuleMetaData(), database.getSchemas())); + new ResourceMetaData(newStorageNodes, newStorageUnitNodeMap, propsMap), database.getRuleMetaData(), database.getSchemas())); } private Map getNewStorageNodes(final Map currentStorageNodes, final SwitchingResource resource) { diff --git a/mode/core/src/test/java/org/apache/shardingsphere/mode/manager/ContextManagerTest.java b/mode/core/src/test/java/org/apache/shardingsphere/mode/manager/ContextManagerTest.java index bd434f5ced700..f1982c0f42892 100644 --- a/mode/core/src/test/java/org/apache/shardingsphere/mode/manager/ContextManagerTest.java +++ b/mode/core/src/test/java/org/apache/shardingsphere/mode/manager/ContextManagerTest.java @@ -110,7 +110,7 @@ private ShardingSphereDatabase mockDatabase() { DataSourcePoolProperties dataSourcePoolProps = mock(DataSourcePoolProperties.class, RETURNS_DEEP_STUBS); when(dataSourcePoolProps.getConnectionPropertySynonyms().getStandardProperties()).thenReturn(Collections.emptyMap()); Map storageUnits = Collections.singletonMap("foo_ds", - new StorageUnit("foo_db", new StorageNode(mock(StorageNodeName.class), "jdbc:mock://127.0.0.1/foo_db"), dataSourcePoolProps, new MockedDataSource())); + new StorageUnit(new StorageNode(mock(StorageNodeName.class), "jdbc:mock://127.0.0.1/foo_db", null), dataSourcePoolProps, new MockedDataSource())); when(result.getResourceMetaData().getStorageUnits()).thenReturn(storageUnits); return result; } @@ -260,8 +260,7 @@ private ResourceMetaData createOriginalResource() { for (Entry entry : storageUnitNodeMap.entrySet()) { DataSourcePoolProperties dataSourcePoolProps = mock(DataSourcePoolProperties.class, RETURNS_DEEP_STUBS); when(dataSourcePoolProps.getConnectionPropertySynonyms().getStandardProperties()).thenReturn(Collections.emptyMap()); - storageUnits.put(entry.getKey(), new StorageUnit( - "foo_db", storageUnitNodeMap.get(entry.getKey()), dataSourcePoolProps, storageNodeDataSourceMap.get(entry.getValue().getName()))); + storageUnits.put(entry.getKey(), new StorageUnit(storageUnitNodeMap.get(entry.getKey()), dataSourcePoolProps, storageNodeDataSourceMap.get(entry.getValue().getName()))); } ResourceMetaData result = mock(ResourceMetaData.class, RETURNS_DEEP_STUBS); when(result.getStorageUnits()).thenReturn(storageUnits); diff --git a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/util/YamlDatabaseConfigurationImportExecutor.java b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/util/YamlDatabaseConfigurationImportExecutor.java index 9dc1f225d2466..a704756b65a48 100644 --- a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/util/YamlDatabaseConfigurationImportExecutor.java +++ b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/util/YamlDatabaseConfigurationImportExecutor.java @@ -164,7 +164,7 @@ private void addResources(final String databaseName, final Map toBeAddedStorageNode = StorageUnitNodeMapUtils.fromDataSourcePoolProperties(propsMap); for (Entry entry : propsMap.entrySet()) { - storageUnits.put(entry.getKey(), new StorageUnit(databaseName, toBeAddedStorageNode.get(entry.getKey()), entry.getValue(), DataSourcePoolCreator.create(entry.getValue()))); + storageUnits.put(entry.getKey(), new StorageUnit(toBeAddedStorageNode.get(entry.getKey()), entry.getValue(), DataSourcePoolCreator.create(entry.getValue()))); } } diff --git a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/admin/executor/DefaultDatabaseMetaDataExecutorTest.java b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/admin/executor/DefaultDatabaseMetaDataExecutorTest.java index b8e3c5148483c..af7a432d0c1f2 100644 --- a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/admin/executor/DefaultDatabaseMetaDataExecutorTest.java +++ b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/admin/executor/DefaultDatabaseMetaDataExecutorTest.java @@ -125,7 +125,6 @@ private ShardingSphereDatabase createDatabase(final Map expected private Connection mockConnection(final Map expectedResultSetMap) throws SQLException { Connection result = mock(Connection.class, RETURNS_DEEP_STUBS); - when(result.getMetaData().getURL()).thenReturn("jdbc:mysql://localhost:3306/foo_ds"); ResultSet resultSet = mockResultSet(expectedResultSetMap); when(result.prepareStatement(any(String.class)).executeQuery()).thenReturn(resultSet); return result; diff --git a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/ImportMetaDataUpdaterTest.java b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/ImportMetaDataUpdaterTest.java index e065e8f69537f..e7ec23188daa5 100644 --- a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/ImportMetaDataUpdaterTest.java +++ b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/updatable/ImportMetaDataUpdaterTest.java @@ -123,8 +123,8 @@ private Map createStorageUnits() { Map result = new LinkedHashMap<>(2, 1F); DataSourcePoolProperties dataSourcePoolProps = mock(DataSourcePoolProperties.class, RETURNS_DEEP_STUBS); when(dataSourcePoolProps.getConnectionPropertySynonyms().getStandardProperties()).thenReturn(Collections.emptyMap()); - result.put("ds_0", new StorageUnit("foo_db", new StorageNode(mock(StorageNodeName.class), "jdbc:mock://127.0.0.1/ds_0"), dataSourcePoolProps, new MockedDataSource())); - result.put("ds_1", new StorageUnit("foo_db", new StorageNode(mock(StorageNodeName.class), "jdbc:mock://127.0.0.1/ds_1"), dataSourcePoolProps, new MockedDataSource())); + result.put("ds_0", new StorageUnit(new StorageNode(mock(StorageNodeName.class), "jdbc:mock://127.0.0.1/ds_0", null), dataSourcePoolProps, new MockedDataSource())); + result.put("ds_1", new StorageUnit(new StorageNode(mock(StorageNodeName.class), "jdbc:mock://127.0.0.1/ds_1", null), dataSourcePoolProps, new MockedDataSource())); return result; } diff --git a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rdl/storage/unit/UnregisterStorageUnitBackendHandlerTest.java b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rdl/storage/unit/UnregisterStorageUnitBackendHandlerTest.java index 66a438fadff12..cc2fdfdc2cb8e 100644 --- a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rdl/storage/unit/UnregisterStorageUnitBackendHandlerTest.java +++ b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rdl/storage/unit/UnregisterStorageUnitBackendHandlerTest.java @@ -88,7 +88,7 @@ void setUp() { resourceMetaData = mock(ResourceMetaData.class, RETURNS_DEEP_STUBS); DataSourcePoolProperties dataSourcePoolProps = mock(DataSourcePoolProperties.class, RETURNS_DEEP_STUBS); when(dataSourcePoolProps.getConnectionPropertySynonyms().getStandardProperties()).thenReturn(Collections.emptyMap()); - StorageUnit storageUnit = new StorageUnit("foo_db", new StorageNode(mock(StorageNodeName.class), "jdbc:mock://127.0.0.1/foo_db"), dataSourcePoolProps, new MockedDataSource()); + StorageUnit storageUnit = new StorageUnit(new StorageNode(mock(StorageNodeName.class), "jdbc:mock://127.0.0.1/foo_db", null), dataSourcePoolProps, new MockedDataSource()); when(resourceMetaData.getStorageUnits()).thenReturn(Collections.singletonMap("foo_ds", storageUnit)); when(database.getResourceMetaData()).thenReturn(resourceMetaData); contextManager = mockContextManager(); diff --git a/proxy/backend/type/mysql/src/main/java/org/apache/shardingsphere/proxy/backend/mysql/handler/admin/executor/information/SelectInformationSchemataExecutor.java b/proxy/backend/type/mysql/src/main/java/org/apache/shardingsphere/proxy/backend/mysql/handler/admin/executor/information/SelectInformationSchemataExecutor.java index f75b822132f1d..b3a58752502a7 100644 --- a/proxy/backend/type/mysql/src/main/java/org/apache/shardingsphere/proxy/backend/mysql/handler/admin/executor/information/SelectInformationSchemataExecutor.java +++ b/proxy/backend/type/mysql/src/main/java/org/apache/shardingsphere/proxy/backend/mysql/handler/admin/executor/information/SelectInformationSchemataExecutor.java @@ -96,7 +96,7 @@ protected Collection getDatabaseNames(final ConnectionSession connection protected void preProcess(final String databaseName, final Map rows, final Map alias) { ResourceMetaData resourceMetaData = ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData().getDatabase(databaseName).getResourceMetaData(); Collection catalogs = resourceMetaData.getStorageUnits().keySet() - .stream().map(each -> resourceMetaData.getStorageUnits().get(each).getConnectionProperties().getCatalog()).collect(Collectors.toSet()); + .stream().map(each -> resourceMetaData.getStorageUnits().get(each).getStorageNode().getCatalog()).collect(Collectors.toSet()); schemaNameAlias = alias.getOrDefault(SCHEMA_NAME, ""); String rowValue = rows.getOrDefault(schemaNameAlias, "").toString(); queryDatabase = !rowValue.isEmpty(); diff --git a/proxy/backend/type/mysql/src/test/java/org/apache/shardingsphere/proxy/backend/mysql/handler/admin/executor/information/SelectInformationSchemataExecutorTest.java b/proxy/backend/type/mysql/src/test/java/org/apache/shardingsphere/proxy/backend/mysql/handler/admin/executor/information/SelectInformationSchemataExecutorTest.java index 79b4e1ff189c0..cdbb1ee04d400 100644 --- a/proxy/backend/type/mysql/src/test/java/org/apache/shardingsphere/proxy/backend/mysql/handler/admin/executor/information/SelectInformationSchemataExecutorTest.java +++ b/proxy/backend/type/mysql/src/test/java/org/apache/shardingsphere/proxy/backend/mysql/handler/admin/executor/information/SelectInformationSchemataExecutorTest.java @@ -20,7 +20,9 @@ import org.apache.shardingsphere.authority.provider.database.model.privilege.DatabasePermittedPrivileges; import org.apache.shardingsphere.authority.rule.AuthorityRule; import org.apache.shardingsphere.infra.config.props.ConfigurationProperties; +import org.apache.shardingsphere.infra.database.core.metadata.database.DialectDatabaseMetaData; import org.apache.shardingsphere.infra.database.core.type.DatabaseType; +import org.apache.shardingsphere.infra.database.core.type.DatabaseTypeRegistry; import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData; import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; import org.apache.shardingsphere.infra.metadata.database.resource.ResourceMetaData; @@ -43,6 +45,7 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mock; +import org.mockito.MockedConstruction; import org.mockito.junit.jupiter.MockitoSettings; import org.mockito.quality.Strictness; @@ -66,6 +69,7 @@ import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.RETURNS_DEEP_STUBS; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.mockConstruction; import static org.mockito.Mockito.when; @ExtendWith(AutoMockExtension.class) @@ -105,18 +109,25 @@ void assertExecuteWithAuthorizedDatabase() throws SQLException { Map expectedResultSetMap = new HashMap<>(2, 1F); expectedResultSetMap.put("SCHEMA_NAME", "foo_ds"); expectedResultSetMap.put("DEFAULT_COLLATION_NAME", "utf8mb4"); - ShardingSphereDatabase database = createDatabase(expectedResultSetMap); - ContextManager contextManager = mockContextManager(database); - when(ProxyContext.getInstance().getContextManager()).thenReturn(contextManager); - when(ProxyContext.getInstance().getAllDatabaseNames()).thenReturn(Collections.singleton("auth_db")); - when(ProxyContext.getInstance().getDatabase("auth_db")).thenReturn(database); - SelectInformationSchemataExecutor executor = new SelectInformationSchemataExecutor(statement, sql, Collections.emptyList()); - executor.execute(connectionSession); - assertThat(executor.getQueryResultMetaData().getColumnCount(), is(2)); - assertTrue(executor.getMergedResult().next()); - assertThat(executor.getMergedResult().getValue(1, String.class), is("auth_db")); - assertThat(executor.getMergedResult().getValue(2, String.class), is("utf8mb4")); - assertFalse(executor.getMergedResult().next()); + try (MockedConstruction ignored = mockConstruction(DatabaseTypeRegistry.class, (mock, mockContext) -> { + DialectDatabaseMetaData dialectDatabaseMetaData = mock(DialectDatabaseMetaData.class); + when(dialectDatabaseMetaData.isInstanceConnectionAvailable()).thenReturn(true); + when(mock.getDialectDatabaseMetaData()).thenReturn(dialectDatabaseMetaData); + })) { + ShardingSphereDatabase database = createDatabase(expectedResultSetMap); + ContextManager contextManager = mockContextManager(database); + when(ProxyContext.getInstance().getContextManager()).thenReturn(contextManager); + when(ProxyContext.getInstance().getAllDatabaseNames()).thenReturn(Collections.singleton("auth_db")); + when(ProxyContext.getInstance().getDatabase("auth_db")).thenReturn(database); + SelectInformationSchemataExecutor executor = new SelectInformationSchemataExecutor(statement, sql, Collections.emptyList()); + executor.execute(connectionSession); + assertThat(executor.getQueryResultMetaData().getColumnCount(), is(2)); + assertTrue(executor.getMergedResult().next()); + assertThat(executor.getMergedResult().getValue(1, String.class), is("auth_db")); + assertThat(executor.getMergedResult().getValue(2, String.class), is("utf8mb4")); + assertFalse(executor.getMergedResult().next()); + assertFalse(executor.getMergedResult().next()); + } } @Test diff --git a/proxy/bootstrap/src/test/java/org/apache/shardingsphere/proxy/version/ShardingSphereProxyVersionTest.java b/proxy/bootstrap/src/test/java/org/apache/shardingsphere/proxy/version/ShardingSphereProxyVersionTest.java index 4e0e48e543c08..ec052576440aa 100644 --- a/proxy/bootstrap/src/test/java/org/apache/shardingsphere/proxy/version/ShardingSphereProxyVersionTest.java +++ b/proxy/bootstrap/src/test/java/org/apache/shardingsphere/proxy/version/ShardingSphereProxyVersionTest.java @@ -86,7 +86,7 @@ private ResourceMetaData mockResourceMetaData(final String databaseProductName, DataSource dataSource = createDataSource(databaseProductName, databaseProductVersion); DataSourcePoolProperties dataSourcePoolProps = mock(DataSourcePoolProperties.class, RETURNS_DEEP_STUBS); when(dataSourcePoolProps.getConnectionPropertySynonyms().getStandardProperties()).thenReturn(Collections.emptyMap()); - StorageUnit storageUnit = new StorageUnit("foo_db", new StorageNode(mock(StorageNodeName.class), "jdbc:mock://127.0.0.1/foo_db"), dataSourcePoolProps, dataSource); + StorageUnit storageUnit = new StorageUnit(new StorageNode(mock(StorageNodeName.class), "jdbc:mock://127.0.0.1/foo_db", null), dataSourcePoolProps, dataSource); when(result.getStorageUnits()).thenReturn(Collections.singletonMap("foo_ds", storageUnit)); return result; }