From f217013ebad7780545ca55fbc3bba01a1474e98a Mon Sep 17 00:00:00 2001 From: Liang Zhang Date: Tue, 3 Dec 2024 20:55:04 +0800 Subject: [PATCH] Add ShardingSphereMetaData's constructor with ShardingSphereDatabase collection (#33900) * Add ShardingSphereMetaData's constructor with ShardingSphereDatabase collection * Add ShardingSphereMetaData's constructor with ShardingSphereDatabase collection * Add ShardingSphereMetaData's constructor with ShardingSphereDatabase collection * Add ShardingSphereMetaData's constructor with ShardingSphereDatabase collection * Add ShardingSphereMetaData's constructor with ShardingSphereDatabase collection * Add ShardingSphereMetaData's constructor with ShardingSphereDatabase collection * Add ShardingSphereMetaData's constructor with ShardingSphereDatabase collection * Add ShardingSphereMetaData's constructor with ShardingSphereDatabase collection * Add ShardingSphereMetaData's constructor with ShardingSphereDatabase collection --- .../EncryptGeneratorFixtureBuilder.java | 14 +++---- .../ShardingRouteCacheableCheckerTest.java | 9 ++--- .../ShardingInsertSupportedCheckerTest.java | 40 +++++++++---------- .../merge/ShardingResultMergerEngineTest.java | 29 +++++--------- .../IteratorStreamMergedResultTest.java | 5 ++- .../ddl/ShardingDDLResultMergerTest.java | 11 +---- .../fetch/FetchStreamMergedResultTest.java | 19 ++++----- .../dql/ShardingDQLResultMergerTest.java | 5 +-- .../GroupByMemoryMergedResultTest.java | 11 +++-- .../dql/groupby/GroupByRowComparatorTest.java | 30 +++++++------- .../GroupByStreamMergedResultTest.java | 10 ++--- .../OrderByStreamMergedResultTest.java | 5 ++- .../merge/dql/orderby/OrderByValueTest.java | 7 ++-- .../LimitDecoratorMergedResultTest.java | 28 +++++-------- .../RowNumberDecoratorMergedResultTest.java | 15 ++++--- ...AndRowNumberDecoratorMergedResultTest.java | 8 +++- .../ShardingStatisticsTableCollectorTest.java | 6 +-- ...LShardingStatisticsTableCollectorTest.java | 3 +- ...sShardingStatisticsTableCollectorTest.java | 6 +-- ...LShardingStatisticsTableCollectorTest.java | 3 +- .../ShardingRemoveTokenGeneratorTest.java | 12 ++---- ...ShardingInsertRouteContextCheckerTest.java | 23 +++++------ .../assertion/ShardingRouteAssert.java | 7 +--- .../pagination/PaginationContextTest.java | 14 +++---- .../engine/ProjectionsContextEngineTest.java | 7 ++-- .../SQLStatementContextFactoryTest.java | 5 +-- .../dml/InsertStatementContextTest.java | 9 ++--- .../dml/SelectStatementContextTest.java | 28 ++++++------- .../metadata/ShardingSphereMetaData.java | 13 +++++- .../metadata/ShardingSphereMetaDataTest.java | 12 +++--- .../mode/metadata/MetaDataContextManager.java | 2 +- .../metadata/MetaDataContextsFactory.java | 6 +-- .../manager/GlobalConfigurationManager.java | 8 ++-- .../mode/metadata/MetaDataContextsTest.java | 7 ++-- ...loneMetaDataManagerPersistServiceTest.java | 17 ++++---- .../DatabaseConnectorFactoryTest.java | 4 +- .../connector/ProxySQLExecutorTest.java | 5 ++- .../StandardDatabaseConnectorTest.java | 22 +++++----- .../datasource/JDBCBackendDataSourceTest.java | 19 ++++----- .../backend/context/ProxyContextTest.java | 29 +++++++------- .../DatabaseAdminQueryBackendHandlerTest.java | 5 ++- .../DefaultDatabaseMetaDataExecutorTest.java | 2 +- .../UnicastDatabaseBackendHandlerTest.java | 15 +++---- ...abaseOperateBackendHandlerFactoryTest.java | 16 ++++---- .../queryable/ExportMetaDataExecutorTest.java | 8 ++-- .../ExportStorageNodesExecutorTest.java | 4 +- .../admin/MySQLAdminExecutorCreatorTest.java | 26 ++++++------ .../ShowCreateDatabaseExecutorTest.java | 14 +++---- .../executor/ShowCurrentUserExecutorTest.java | 2 +- .../executor/ShowDatabasesExecutorTest.java | 14 +++---- .../ShowFunctionStatusExecutorTest.java | 14 +++---- .../ShowProcedureStatusExecutorTest.java | 14 +++---- .../executor/ShowTablesExecutorTest.java | 30 +++++++------- .../executor/UseDatabaseExecutorTest.java | 13 +++--- ...SelectInformationSchemataExecutorTest.java | 4 +- ...ssSystemCatalogAdminQueryExecutorTest.java | 26 ++++++------ .../FrontDatabaseProtocolTypeFactoryTest.java | 24 +++++------ .../MySQLAuthenticationEngineTest.java | 13 +++--- .../OpenGaussAuthenticationEngineTest.java | 5 +-- .../PostgreSQLAuthenticationEngineTest.java | 2 +- .../test/it/rewrite/engine/SQLRewriterIT.java | 5 +-- 61 files changed, 357 insertions(+), 412 deletions(-) diff --git a/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/fixture/EncryptGeneratorFixtureBuilder.java b/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/fixture/EncryptGeneratorFixtureBuilder.java index dd33c889bee61..43a4e689780f7 100644 --- a/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/fixture/EncryptGeneratorFixtureBuilder.java +++ b/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/fixture/EncryptGeneratorFixtureBuilder.java @@ -106,14 +106,13 @@ public static EncryptRule createEncryptRule() { * @return created insert statement context */ public static InsertStatementContext createInsertStatementContext(final List params) { - InsertStatement insertStatement = createInsertStatement(); ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); + when(database.getName()).thenReturn(DefaultDatabase.LOGIC_NAME); ShardingSphereSchema schema = mock(ShardingSphereSchema.class); when(schema.getAllColumnNames("tbl")).thenReturn(Arrays.asList("id", "name", "status", "pwd")); when(database.getSchema(DefaultDatabase.LOGIC_NAME)).thenReturn(schema); - ShardingSphereMetaData metaData = new ShardingSphereMetaData( - Collections.singletonMap(DefaultDatabase.LOGIC_NAME, database), mock(ResourceMetaData.class), mock(RuleMetaData.class), mock(ConfigurationProperties.class)); - return new InsertStatementContext(metaData, params, insertStatement, DefaultDatabase.LOGIC_NAME); + ShardingSphereMetaData metaData = new ShardingSphereMetaData(Collections.singleton(database), mock(ResourceMetaData.class), mock(RuleMetaData.class), mock(ConfigurationProperties.class)); + return new InsertStatementContext(metaData, params, createInsertStatement(), DefaultDatabase.LOGIC_NAME); } private static InsertStatement createInsertStatement() { @@ -210,13 +209,12 @@ private static List createValueExpressions() { * @return created insert select statement context */ public static InsertStatementContext createInsertSelectStatementContext(final List params, final boolean containsInsertColumns) { - InsertStatement insertStatement = createInsertSelectStatement(containsInsertColumns); ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); + when(database.getName()).thenReturn(DefaultDatabase.LOGIC_NAME); ShardingSphereSchema schema = mock(ShardingSphereSchema.class); when(schema.getAllColumnNames("t_user")).thenReturn(Arrays.asList("user_id", "user_name", "pwd")); when(database.getSchema(DefaultDatabase.LOGIC_NAME)).thenReturn(schema); - ShardingSphereMetaData metaData = new ShardingSphereMetaData( - Collections.singletonMap(DefaultDatabase.LOGIC_NAME, database), mock(ResourceMetaData.class), mock(RuleMetaData.class), mock(ConfigurationProperties.class)); - return new InsertStatementContext(metaData, params, insertStatement, DefaultDatabase.LOGIC_NAME); + ShardingSphereMetaData metaData = new ShardingSphereMetaData(Collections.singleton(database), mock(ResourceMetaData.class), mock(RuleMetaData.class), mock(ConfigurationProperties.class)); + return new InsertStatementContext(metaData, params, createInsertSelectStatement(containsInsertColumns), DefaultDatabase.LOGIC_NAME); } } diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/cache/checker/ShardingRouteCacheableCheckerTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/cache/checker/ShardingRouteCacheableCheckerTest.java index b83501905c495..7bed052dfe1dd 100644 --- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/cache/checker/ShardingRouteCacheableCheckerTest.java +++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/cache/checker/ShardingRouteCacheableCheckerTest.java @@ -148,7 +148,9 @@ private ShardingSphereDatabase createDatabase(final ShardingRule shardingRule, f } private QueryContext createQueryContext(final ShardingSphereDatabase database, final String sql, final List params) { - SQLStatementContext sqlStatementContext = new SQLBindEngine(createShardingSphereMetaData(database), DATABASE_NAME, new HintValueContext()).bind(parse(sql), params); + SQLStatementContext sqlStatementContext = new SQLBindEngine( + new ShardingSphereMetaData(Collections.singleton(database), mock(ResourceMetaData.class), mock(RuleMetaData.class), mock(ConfigurationProperties.class)), + DATABASE_NAME, new HintValueContext()).bind(parse(sql), params); return new QueryContext(sqlStatementContext, sql, params, new HintValueContext(), mockConnectionContext(), mock(ShardingSphereMetaData.class)); } @@ -158,11 +160,6 @@ private ConnectionContext mockConnectionContext() { return result; } - private ShardingSphereMetaData createShardingSphereMetaData(final ShardingSphereDatabase database) { - return new ShardingSphereMetaData(Collections.singletonMap(DATABASE_NAME, database), mock(ResourceMetaData.class), - mock(RuleMetaData.class), mock(ConfigurationProperties.class)); - } - private SQLStatement parse(final String sql) { CacheOption cacheOption = new CacheOption(0, 0L); return new SQLStatementParserEngine(databaseType, cacheOption, cacheOption).parse(sql, false); diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/sql/ddl/ShardingInsertSupportedCheckerTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/sql/ddl/ShardingInsertSupportedCheckerTest.java index 0709e2d2d7242..3e2777a67c0f8 100644 --- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/sql/ddl/ShardingInsertSupportedCheckerTest.java +++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/checker/sql/ddl/ShardingInsertSupportedCheckerTest.java @@ -68,7 +68,7 @@ class ShardingInsertSupportedCheckerTest { @Mock - private ShardingRule shardingRule; + private ShardingRule rule; @Mock(answer = Answers.RETURNS_DEEP_STUBS) private ShardingSphereDatabase database; @@ -77,57 +77,57 @@ class ShardingInsertSupportedCheckerTest { void assertCheckWhenInsertMultiTables() { InsertStatementContext sqlStatementContext = createInsertStatementContext(Collections.singletonList(1), createInsertStatement()); Collection tableNames = sqlStatementContext.getTablesContext().getTableNames(); - when(shardingRule.isAllShardingTables(tableNames)).thenReturn(false); - when(shardingRule.containsShardingTable(tableNames)).thenReturn(true); - assertThrows(DMLWithMultipleShardingTablesException.class, () -> new ShardingInsertSupportedChecker().check(shardingRule, database, mock(ShardingSphereSchema.class), sqlStatementContext)); + when(rule.isAllShardingTables(tableNames)).thenReturn(false); + when(rule.containsShardingTable(tableNames)).thenReturn(true); + assertThrows(DMLWithMultipleShardingTablesException.class, () -> new ShardingInsertSupportedChecker().check(rule, database, mock(ShardingSphereSchema.class), sqlStatementContext)); } private InsertStatementContext createInsertStatementContext(final List params, final InsertStatement insertStatement) { + when(database.getName()).thenReturn(DefaultDatabase.LOGIC_NAME); when(database.getSchema(DefaultDatabase.LOGIC_NAME)).thenReturn(mock(ShardingSphereSchema.class)); - ShardingSphereMetaData metaData = new ShardingSphereMetaData(Collections.singletonMap(DefaultDatabase.LOGIC_NAME, database), mock(ResourceMetaData.class), - mock(RuleMetaData.class), mock(ConfigurationProperties.class)); + ShardingSphereMetaData metaData = new ShardingSphereMetaData(Collections.singleton(database), mock(ResourceMetaData.class), mock(RuleMetaData.class), mock(ConfigurationProperties.class)); return new InsertStatementContext(metaData, params, insertStatement, DefaultDatabase.LOGIC_NAME); } @Test void assertCheckWhenInsertSelectWithoutKeyGenerateColumn() { - when(shardingRule.findGenerateKeyColumnName("user")).thenReturn(Optional.of("id")); - when(shardingRule.isGenerateKeyColumn("id", "user")).thenReturn(false); + when(rule.findGenerateKeyColumnName("user")).thenReturn(Optional.of("id")); + when(rule.isGenerateKeyColumn("id", "user")).thenReturn(false); InsertStatementContext sqlStatementContext = createInsertStatementContext(Collections.singletonList(1), createInsertSelectStatement()); sqlStatementContext.getTablesContext().getTableNames().addAll(createSingleTablesContext().getTableNames()); assertThrows(MissingGenerateKeyColumnWithInsertSelectException.class, - () -> new ShardingInsertSupportedChecker().check(shardingRule, database, mock(ShardingSphereSchema.class), sqlStatementContext)); + () -> new ShardingInsertSupportedChecker().check(rule, database, mock(ShardingSphereSchema.class), sqlStatementContext)); } @Test void assertCheckWhenInsertSelectWithKeyGenerateColumn() { - when(shardingRule.findGenerateKeyColumnName("user")).thenReturn(Optional.of("id")); - when(shardingRule.isGenerateKeyColumn("id", "user")).thenReturn(true); + when(rule.findGenerateKeyColumnName("user")).thenReturn(Optional.of("id")); + when(rule.isGenerateKeyColumn("id", "user")).thenReturn(true); InsertStatementContext sqlStatementContext = createInsertStatementContext(Collections.singletonList(1), createInsertSelectStatement()); sqlStatementContext.getTablesContext().getTableNames().addAll(createSingleTablesContext().getTableNames()); - assertDoesNotThrow(() -> new ShardingInsertSupportedChecker().check(shardingRule, database, mock(ShardingSphereSchema.class), sqlStatementContext)); + assertDoesNotThrow(() -> new ShardingInsertSupportedChecker().check(rule, database, mock(ShardingSphereSchema.class), sqlStatementContext)); } @Test void assertCheckWhenInsertSelectWithoutBindingTables() { - when(shardingRule.findGenerateKeyColumnName("user")).thenReturn(Optional.of("id")); - when(shardingRule.isGenerateKeyColumn("id", "user")).thenReturn(true); + when(rule.findGenerateKeyColumnName("user")).thenReturn(Optional.of("id")); + when(rule.isGenerateKeyColumn("id", "user")).thenReturn(true); TablesContext multiTablesContext = createMultiTablesContext(); - when(shardingRule.isAllBindingTables(multiTablesContext.getTableNames())).thenReturn(false); - when(shardingRule.containsShardingTable(multiTablesContext.getTableNames())).thenReturn(true); + when(rule.isAllBindingTables(multiTablesContext.getTableNames())).thenReturn(false); + when(rule.containsShardingTable(multiTablesContext.getTableNames())).thenReturn(true); InsertStatementContext sqlStatementContext = createInsertStatementContext(Collections.singletonList(1), createInsertSelectStatement()); sqlStatementContext.getTablesContext().getTableNames().addAll(multiTablesContext.getTableNames()); - assertThrows(InsertSelectTableViolationException.class, () -> new ShardingInsertSupportedChecker().check(shardingRule, database, mock(ShardingSphereSchema.class), sqlStatementContext)); + assertThrows(InsertSelectTableViolationException.class, () -> new ShardingInsertSupportedChecker().check(rule, database, mock(ShardingSphereSchema.class), sqlStatementContext)); } @Test void assertCheckWhenInsertSelectWithBindingTables() { - when(shardingRule.findGenerateKeyColumnName("user")).thenReturn(Optional.of("id")); - when(shardingRule.isGenerateKeyColumn("id", "user")).thenReturn(true); + when(rule.findGenerateKeyColumnName("user")).thenReturn(Optional.of("id")); + when(rule.isGenerateKeyColumn("id", "user")).thenReturn(true); TablesContext multiTablesContext = createMultiTablesContext(); InsertStatementContext sqlStatementContext = createInsertStatementContext(Collections.singletonList(1), createInsertSelectStatement()); sqlStatementContext.getTablesContext().getTableNames().addAll(multiTablesContext.getTableNames()); - assertDoesNotThrow(() -> new ShardingInsertSupportedChecker().check(shardingRule, database, mock(ShardingSphereSchema.class), sqlStatementContext)); + assertDoesNotThrow(() -> new ShardingInsertSupportedChecker().check(rule, database, mock(ShardingSphereSchema.class), sqlStatementContext)); } private InsertStatement createInsertStatement() { diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/merge/ShardingResultMergerEngineTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/merge/ShardingResultMergerEngineTest.java index fe3da12df0235..c1544eb1be953 100644 --- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/merge/ShardingResultMergerEngineTest.java +++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/merge/ShardingResultMergerEngineTest.java @@ -26,8 +26,6 @@ import org.apache.shardingsphere.infra.merge.engine.merger.impl.TransparentResultMerger; import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData; import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; -import org.apache.shardingsphere.infra.metadata.database.resource.ResourceMetaData; -import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData; import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema; import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader; import org.apache.shardingsphere.sharding.merge.dal.ShardingDALResultMerger; @@ -62,6 +60,8 @@ class ShardingResultMergerEngineTest { + private final DatabaseType databaseType = TypedSPILoader.getService(DatabaseType.class, "MySQL"); + @Test void assertNewInstanceWithSelectStatementForMySQL() { assertNewInstanceWithSelectStatement(new MySQLSelectStatement()); @@ -88,27 +88,21 @@ void assertNewInstanceWithSelectStatementForSQLServer() { } private void assertNewInstanceWithSelectStatement(final SelectStatement selectStatement) { - ConfigurationProperties props = new ConfigurationProperties(new Properties()); ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); + when(database.getName()).thenReturn(DefaultDatabase.LOGIC_NAME); when(database.getSchema(DefaultDatabase.LOGIC_NAME)).thenReturn(mock(ShardingSphereSchema.class)); selectStatement.setProjections(new ProjectionsSegment(0, 0)); - SelectStatementContext sqlStatementContext = new SelectStatementContext(createShardingSphereMetaData(database), + SelectStatementContext sqlStatementContext = new SelectStatementContext(new ShardingSphereMetaData(Collections.singleton(database), mock(), mock(), mock()), Collections.emptyList(), selectStatement, DefaultDatabase.LOGIC_NAME, Collections.emptyList()); - assertThat(new ShardingResultMergerEngine().newInstance(DefaultDatabase.LOGIC_NAME, TypedSPILoader.getService(DatabaseType.class, "MySQL"), null, props, - sqlStatementContext), instanceOf(ShardingDQLResultMerger.class)); - } - - private ShardingSphereMetaData createShardingSphereMetaData(final ShardingSphereDatabase database) { - return new ShardingSphereMetaData(Collections.singletonMap(DefaultDatabase.LOGIC_NAME, database), mock(ResourceMetaData.class), - mock(RuleMetaData.class), mock(ConfigurationProperties.class)); + assertThat(new ShardingResultMergerEngine().newInstance(DefaultDatabase.LOGIC_NAME, databaseType, null, new ConfigurationProperties(new Properties()), sqlStatementContext), + instanceOf(ShardingDQLResultMerger.class)); } @Test void assertNewInstanceWithDALStatement() { ConfigurationProperties props = new ConfigurationProperties(new Properties()); UnknownSQLStatementContext sqlStatementContext = new UnknownSQLStatementContext(new PostgreSQLShowStatement("")); - assertThat(new ShardingResultMergerEngine().newInstance(DefaultDatabase.LOGIC_NAME, TypedSPILoader.getService(DatabaseType.class, "MySQL"), null, props, - sqlStatementContext), instanceOf(ShardingDALResultMerger.class)); + assertThat(new ShardingResultMergerEngine().newInstance(DefaultDatabase.LOGIC_NAME, databaseType, null, props, sqlStatementContext), instanceOf(ShardingDALResultMerger.class)); } @Test @@ -119,21 +113,20 @@ void assertNewInstanceWithOtherStatement() { insertStatement.setInsertColumns(insertColumnsSegment); InsertStatementContext sqlStatementContext = createInsertStatementContext(insertStatement); ConfigurationProperties props = new ConfigurationProperties(new Properties()); - assertThat(new ShardingResultMergerEngine().newInstance(DefaultDatabase.LOGIC_NAME, TypedSPILoader.getService(DatabaseType.class, "MySQL"), null, props, - sqlStatementContext), instanceOf(TransparentResultMerger.class)); + assertThat(new ShardingResultMergerEngine().newInstance(DefaultDatabase.LOGIC_NAME, databaseType, null, props, sqlStatementContext), instanceOf(TransparentResultMerger.class)); } private InsertStatementContext createInsertStatementContext(final InsertStatement insertStatement) { ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); + when(database.getName()).thenReturn(DefaultDatabase.LOGIC_NAME); when(database.getSchema(DefaultDatabase.LOGIC_NAME)).thenReturn(mock(ShardingSphereSchema.class)); - return new InsertStatementContext(createShardingSphereMetaData(database), Collections.emptyList(), insertStatement, DefaultDatabase.LOGIC_NAME); + return new InsertStatementContext(new ShardingSphereMetaData(Collections.singleton(database), mock(), mock(), mock()), Collections.emptyList(), insertStatement, DefaultDatabase.LOGIC_NAME); } @Test void assertNewInstanceWithDDLStatement() { ConfigurationProperties props = new ConfigurationProperties(new Properties()); UnknownSQLStatementContext sqlStatementContext = new UnknownSQLStatementContext(new OpenGaussFetchStatement()); - assertThat(new ShardingResultMergerEngine().newInstance(DefaultDatabase.LOGIC_NAME, TypedSPILoader.getService(DatabaseType.class, "MySQL"), null, props, - sqlStatementContext), instanceOf(ShardingDDLResultMerger.class)); + assertThat(new ShardingResultMergerEngine().newInstance(DefaultDatabase.LOGIC_NAME, databaseType, null, props, sqlStatementContext), instanceOf(ShardingDDLResultMerger.class)); } } diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/merge/common/IteratorStreamMergedResultTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/merge/common/IteratorStreamMergedResultTest.java index b0d783d6067f0..ca0a16a771d6d 100644 --- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/merge/common/IteratorStreamMergedResultTest.java +++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/merge/common/IteratorStreamMergedResultTest.java @@ -64,8 +64,9 @@ void setUp() { } private ShardingSphereMetaData createShardingSphereMetaData() { - return new ShardingSphereMetaData(Collections.singletonMap(DefaultDatabase.LOGIC_NAME, mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS)), mock(ResourceMetaData.class), - mock(RuleMetaData.class), mock(ConfigurationProperties.class)); + ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); + when(database.getName()).thenReturn(DefaultDatabase.LOGIC_NAME); + return new ShardingSphereMetaData(Collections.singleton(database), mock(ResourceMetaData.class), mock(RuleMetaData.class), mock(ConfigurationProperties.class)); } @Test diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/merge/ddl/ShardingDDLResultMergerTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/merge/ddl/ShardingDDLResultMergerTest.java index aa85dfb63714f..e9b205486ad61 100644 --- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/merge/ddl/ShardingDDLResultMergerTest.java +++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/merge/ddl/ShardingDDLResultMergerTest.java @@ -20,14 +20,11 @@ import org.apache.shardingsphere.infra.binder.context.statement.ddl.CursorStatementContext; import org.apache.shardingsphere.infra.binder.context.statement.ddl.FetchStatementContext; import org.apache.shardingsphere.infra.binder.context.statement.dml.SelectStatementContext; -import org.apache.shardingsphere.infra.config.props.ConfigurationProperties; import org.apache.shardingsphere.infra.database.core.type.DatabaseType; import org.apache.shardingsphere.infra.executor.sql.execute.result.query.QueryResult; import org.apache.shardingsphere.infra.merge.result.impl.transparent.TransparentMergedResult; import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData; import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; -import org.apache.shardingsphere.infra.metadata.database.resource.ResourceMetaData; -import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData; import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema; import org.apache.shardingsphere.infra.session.connection.ConnectionContext; import org.apache.shardingsphere.infra.session.connection.cursor.CursorConnectionContext; @@ -76,6 +73,7 @@ void assertMergeWithTransparentMergedResult() throws SQLException { @Test void assertMergeWithFetchStreamMergedResult() throws SQLException { ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); + when(database.getName()).thenReturn("foo_db"); when(database.getSchema("foo_db")).thenReturn(mock(ShardingSphereSchema.class)); ConnectionContext connectionContext = mock(ConnectionContext.class); when(connectionContext.getCursorContext()).thenReturn(new CursorConnectionContext()); @@ -94,7 +92,7 @@ private CursorStatementContext createCursorStatementContext(final ShardingSphere SelectStatement selectStatement = createSelectStatement(); selectStatement.setProjections(new ProjectionsSegment(0, 0)); SelectStatementContext selectStatementContext = new SelectStatementContext( - createShardingSphereMetaData(database), Collections.emptyList(), selectStatement, "foo_db", Collections.emptyList()); + new ShardingSphereMetaData(Collections.singleton(database), mock(), mock(), mock()), Collections.emptyList(), selectStatement, "foo_db", Collections.emptyList()); when(result.getSelectStatementContext()).thenReturn(selectStatementContext); when(result.getSqlStatement().getSelect()).thenReturn(selectStatement); return result; @@ -108,11 +106,6 @@ private SelectStatement createSelectStatement() { return result; } - private ShardingSphereMetaData createShardingSphereMetaData(final ShardingSphereDatabase database) { - return new ShardingSphereMetaData(Collections.singletonMap("foo_db", database), mock(ResourceMetaData.class), - mock(RuleMetaData.class), mock(ConfigurationProperties.class)); - } - private List createQueryResults() throws SQLException { List result = new LinkedList<>(); result.add(createQueryResult()); diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/merge/ddl/fetch/FetchStreamMergedResultTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/merge/ddl/fetch/FetchStreamMergedResultTest.java index 3f381e6753c92..6bb043a312b7c 100644 --- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/merge/ddl/fetch/FetchStreamMergedResultTest.java +++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/merge/ddl/fetch/FetchStreamMergedResultTest.java @@ -19,14 +19,11 @@ import org.apache.shardingsphere.infra.binder.context.statement.ddl.CursorStatementContext; import org.apache.shardingsphere.infra.binder.context.statement.ddl.FetchStatementContext; -import org.apache.shardingsphere.infra.config.props.ConfigurationProperties; import org.apache.shardingsphere.infra.database.core.type.DatabaseType; import org.apache.shardingsphere.infra.executor.sql.execute.result.query.QueryResult; import org.apache.shardingsphere.infra.merge.result.MergedResult; import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData; import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; -import org.apache.shardingsphere.infra.metadata.database.resource.ResourceMetaData; -import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData; import org.apache.shardingsphere.infra.session.connection.ConnectionContext; import org.apache.shardingsphere.infra.session.connection.cursor.CursorConnectionContext; import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader; @@ -71,6 +68,8 @@ @MockitoSettings(strictness = Strictness.LENIENT) class FetchStreamMergedResultTest { + private static final DatabaseType DATABASE_TYPE = TypedSPILoader.getService(DatabaseType.class, "FIXTURE"); + private FetchStatementContext fetchCountStatementContext; private FetchStatementContext fetchAllStatementContext; @@ -103,7 +102,7 @@ private static FetchStatement createFetchStatement(final boolean containsAllDire if (containsAllDirectionType) { when(result.getDirection()).thenReturn(Optional.of(new DirectionSegment(0, 0, DirectionType.ALL))); } - when(result.getDatabaseType()).thenReturn(TypedSPILoader.getService(DatabaseType.class, "FIXTURE")); + when(result.getDatabaseType()).thenReturn(DATABASE_TYPE); return result; } @@ -111,19 +110,17 @@ private static CursorStatementContext mockCursorStatementContext() { CursorStatement cursorStatement = mock(CursorStatement.class); SelectStatement selectStatement = mockSelectStatement(); when(cursorStatement.getSelect()).thenReturn(selectStatement); - when(cursorStatement.getDatabaseType()).thenReturn(TypedSPILoader.getService(DatabaseType.class, "FIXTURE")); - return new CursorStatementContext(createShardingSphereMetaData(mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS)), Collections.emptyList(), cursorStatement, "foo_db"); - } - - private static ShardingSphereMetaData createShardingSphereMetaData(final ShardingSphereDatabase database) { - return new ShardingSphereMetaData(Collections.singletonMap("foo_db", database), mock(ResourceMetaData.class), mock(RuleMetaData.class), mock(ConfigurationProperties.class)); + when(cursorStatement.getDatabaseType()).thenReturn(DATABASE_TYPE); + ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); + when(database.getName()).thenReturn("foo_db"); + return new CursorStatementContext(new ShardingSphereMetaData(Collections.singleton(database), mock(), mock(), mock()), Collections.emptyList(), cursorStatement, "foo_db"); } private static SelectStatement mockSelectStatement() { SelectStatement result = mock(SelectStatement.class); when(result.getProjections()).thenReturn(new ProjectionsSegment(0, 0)); when(result.getFrom()).thenReturn(Optional.of(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("foo_tbl"))))); - when(result.getDatabaseType()).thenReturn(TypedSPILoader.getService(DatabaseType.class, "FIXTURE")); + when(result.getDatabaseType()).thenReturn(DATABASE_TYPE); return result; } diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/merge/dql/ShardingDQLResultMergerTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/merge/dql/ShardingDQLResultMergerTest.java index 202281a8e1533..21c705ca4a92c 100644 --- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/merge/dql/ShardingDQLResultMergerTest.java +++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/merge/dql/ShardingDQLResultMergerTest.java @@ -18,7 +18,6 @@ package org.apache.shardingsphere.sharding.merge.dql; import org.apache.shardingsphere.infra.binder.context.statement.dml.SelectStatementContext; -import org.apache.shardingsphere.infra.config.props.ConfigurationProperties; import org.apache.shardingsphere.infra.database.core.DefaultDatabase; import org.apache.shardingsphere.infra.database.core.metadata.database.enums.NullsOrderType; import org.apache.shardingsphere.infra.database.core.type.DatabaseType; @@ -486,8 +485,8 @@ void assertBuildGroupByMemoryMergedResultWithAggregationOnlyWithSQLServerLimit() } private ShardingSphereMetaData createShardingSphereMetaData(final ShardingSphereDatabase database) { - return new ShardingSphereMetaData(Collections.singletonMap(DefaultDatabase.LOGIC_NAME, database), mock(ResourceMetaData.class), - mock(RuleMetaData.class), mock(ConfigurationProperties.class)); + when(database.getName()).thenReturn(DefaultDatabase.LOGIC_NAME); + return new ShardingSphereMetaData(Collections.singleton(database), mock(), mock(), mock()); } private List createQueryResults() throws SQLException { diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/merge/dql/groupby/GroupByMemoryMergedResultTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/merge/dql/groupby/GroupByMemoryMergedResultTest.java index 345aa8c6809c5..2d003ea6496a6 100644 --- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/merge/dql/groupby/GroupByMemoryMergedResultTest.java +++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/merge/dql/groupby/GroupByMemoryMergedResultTest.java @@ -18,7 +18,6 @@ package org.apache.shardingsphere.sharding.merge.dql.groupby; import org.apache.shardingsphere.infra.binder.context.statement.dml.SelectStatementContext; -import org.apache.shardingsphere.infra.config.props.ConfigurationProperties; import org.apache.shardingsphere.infra.database.core.DefaultDatabase; import org.apache.shardingsphere.infra.database.core.metadata.database.enums.NullsOrderType; import org.apache.shardingsphere.infra.database.core.type.DatabaseType; @@ -26,8 +25,6 @@ import org.apache.shardingsphere.infra.merge.result.MergedResult; import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData; import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; -import org.apache.shardingsphere.infra.metadata.database.resource.ResourceMetaData; -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.database.schema.model.ShardingSphereTable; import org.apache.shardingsphere.infra.session.connection.ConnectionContext; @@ -127,6 +124,7 @@ private SelectStatementContext createSelectStatementContext() { selectStatement.setOrderBy(new OrderBySegment(0, 0, Collections.singletonList(new IndexOrderByItemSegment(0, 0, 3, OrderDirection.DESC, NullsOrderType.FIRST)))); selectStatement.setProjections(projectionsSegment); ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); + when(database.getName()).thenReturn(DefaultDatabase.LOGIC_NAME); when(database.getSchema(DefaultDatabase.LOGIC_NAME)).thenReturn(mock(ShardingSphereSchema.class)); return new SelectStatementContext(createShardingSphereMetaData(database), Collections.emptyList(), selectStatement, DefaultDatabase.LOGIC_NAME, Collections.emptyList()); } @@ -144,8 +142,7 @@ private SelectStatementContext createSelectStatementContext(final ShardingSphere } private ShardingSphereMetaData createShardingSphereMetaData(final ShardingSphereDatabase database) { - return new ShardingSphereMetaData(Collections.singletonMap(DefaultDatabase.LOGIC_NAME, database), mock(ResourceMetaData.class), - mock(RuleMetaData.class), mock(ConfigurationProperties.class)); + return new ShardingSphereMetaData(Collections.singleton(database), mock(), mock(), mock()); } @Test @@ -206,9 +203,9 @@ void assertNextForDistinctShorthandResultSetsEmpty() throws SQLException { when(schema.containsTable("t_order")).thenReturn(true); when(table.getAllColumns()).thenReturn(Collections.emptyList()); ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); + when(database.getName()).thenReturn(DefaultDatabase.LOGIC_NAME); when(database.getSchema(DefaultDatabase.LOGIC_NAME)).thenReturn(schema); when(database.getAllSchemas()).thenReturn(Collections.singleton(schema)); - when(database.getName()).thenReturn(DefaultDatabase.LOGIC_NAME); ShardingDQLResultMerger merger = new ShardingDQLResultMerger(TypedSPILoader.getService(DatabaseType.class, "MySQL")); MergedResult actual = merger.merge(Arrays.asList(queryResult, queryResult, queryResult), createSelectStatementContext(database), database, mock(ConnectionContext.class)); assertFalse(actual.next()); @@ -251,6 +248,7 @@ private SelectStatementContext createSelectStatementContextForCountGroupBy() { selectStatement.setOrderBy(new OrderBySegment(0, 0, Collections.singletonList(new IndexOrderByItemSegment(0, 0, 2, OrderDirection.ASC, NullsOrderType.FIRST)))); selectStatement.setProjections(projectionsSegment); ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); + when(database.getName()).thenReturn(DefaultDatabase.LOGIC_NAME); when(database.getSchema(DefaultDatabase.LOGIC_NAME)).thenReturn(mock(ShardingSphereSchema.class)); return new SelectStatementContext(createShardingSphereMetaData(database), Collections.emptyList(), selectStatement, DefaultDatabase.LOGIC_NAME, Collections.emptyList()); } @@ -263,6 +261,7 @@ private SelectStatementContext createSelectStatementContextForCountGroupByDiffer selectStatement.setOrderBy(new OrderBySegment(0, 0, Collections.singletonList(new IndexOrderByItemSegment(0, 0, 3, OrderDirection.ASC, NullsOrderType.FIRST)))); selectStatement.setProjections(projectionsSegment); ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); + when(database.getName()).thenReturn(DefaultDatabase.LOGIC_NAME); when(database.getSchema(DefaultDatabase.LOGIC_NAME)).thenReturn(mock(ShardingSphereSchema.class)); return new SelectStatementContext(createShardingSphereMetaData(database), Collections.emptyList(), selectStatement, DefaultDatabase.LOGIC_NAME, Collections.emptyList()); } diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/merge/dql/groupby/GroupByRowComparatorTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/merge/dql/groupby/GroupByRowComparatorTest.java index 28ae729bf043d..77d100c19a438 100644 --- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/merge/dql/groupby/GroupByRowComparatorTest.java +++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/merge/dql/groupby/GroupByRowComparatorTest.java @@ -18,16 +18,13 @@ package org.apache.shardingsphere.sharding.merge.dql.groupby; import org.apache.shardingsphere.infra.binder.context.statement.dml.SelectStatementContext; -import org.apache.shardingsphere.infra.config.props.ConfigurationProperties; import org.apache.shardingsphere.infra.database.core.DefaultDatabase; +import org.apache.shardingsphere.infra.database.core.metadata.database.enums.NullsOrderType; import org.apache.shardingsphere.infra.executor.sql.execute.result.query.QueryResult; import org.apache.shardingsphere.infra.merge.result.impl.memory.MemoryQueryResultRow; import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData; import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; -import org.apache.shardingsphere.infra.metadata.database.resource.ResourceMetaData; -import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData; import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema; -import org.apache.shardingsphere.infra.database.core.metadata.database.enums.NullsOrderType; import org.apache.shardingsphere.sql.parser.statement.core.enums.OrderDirection; import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.item.ProjectionsSegment; import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.order.GroupBySegment; @@ -86,6 +83,7 @@ void assertCompareToForAscWithOrderByItemsForSQLServer() throws SQLException { private void assertCompareToForAscWithOrderByItems(final SelectStatement selectStatement) throws SQLException { ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); + when(database.getName()).thenReturn(DefaultDatabase.LOGIC_NAME); when(database.getSchema(DefaultDatabase.LOGIC_NAME)).thenReturn(mock(ShardingSphereSchema.class)); selectStatement.setGroupBy(new GroupBySegment(0, 0, Arrays.asList( new IndexOrderByItemSegment(0, 0, 1, OrderDirection.DESC, NullsOrderType.FIRST), @@ -129,6 +127,7 @@ void assertCompareToForDescWithOrderByItemsForSQLServer() throws SQLException { private void assertCompareToForDescWithOrderByItems(final SelectStatement selectStatement) throws SQLException { ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); + when(database.getName()).thenReturn(DefaultDatabase.LOGIC_NAME); when(database.getSchema(DefaultDatabase.LOGIC_NAME)).thenReturn(mock(ShardingSphereSchema.class)); selectStatement.setGroupBy(new GroupBySegment(0, 0, Arrays.asList( new IndexOrderByItemSegment(0, 0, 1, OrderDirection.ASC, NullsOrderType.FIRST), @@ -172,6 +171,7 @@ void assertCompareToForEqualWithOrderByItemsForSQLServer() throws SQLException { private void assertCompareToForEqualWithOrderByItems(final SelectStatement selectStatement) throws SQLException { ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); + when(database.getName()).thenReturn(DefaultDatabase.LOGIC_NAME); when(database.getSchema(DefaultDatabase.LOGIC_NAME)).thenReturn(mock(ShardingSphereSchema.class)); selectStatement.setGroupBy(new GroupBySegment(0, 0, Arrays.asList( new IndexOrderByItemSegment(0, 0, 1, OrderDirection.DESC, NullsOrderType.FIRST), @@ -215,6 +215,7 @@ void assertCompareToForAscWithGroupByItemsForSQLServer() throws SQLException { private void assertCompareToForAscWithGroupByItems(final SelectStatement selectStatement) throws SQLException { ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); + when(database.getName()).thenReturn(DefaultDatabase.LOGIC_NAME); when(database.getSchema(DefaultDatabase.LOGIC_NAME)).thenReturn(mock(ShardingSphereSchema.class)); selectStatement.setGroupBy(new GroupBySegment(0, 0, Arrays.asList( new IndexOrderByItemSegment(0, 0, 1, OrderDirection.ASC, NullsOrderType.FIRST), @@ -231,39 +232,40 @@ private void assertCompareToForAscWithGroupByItems(final SelectStatement selectS @Test void assertCompareToForDecsWithGroupByItemsForMySQL() throws SQLException { - assertCompareToForDecsWithGroupByItems(new MySQLSelectStatement()); + assertCompareToForDescWithGroupByItems(new MySQLSelectStatement()); } @Test void assertCompareToForDecsWithGroupByItemsForOracle() throws SQLException { - assertCompareToForDecsWithGroupByItems(new OracleSelectStatement()); + assertCompareToForDescWithGroupByItems(new OracleSelectStatement()); } @Test void assertCompareToForDecsWithGroupByItemsForPostgreSQL() throws SQLException { - assertCompareToForDecsWithGroupByItems(new PostgreSQLSelectStatement()); + assertCompareToForDescWithGroupByItems(new PostgreSQLSelectStatement()); } @Test void assertCompareToForDecsWithGroupByItemsForSQL92() throws SQLException { - assertCompareToForDecsWithGroupByItems(new SQL92SelectStatement()); + assertCompareToForDescWithGroupByItems(new SQL92SelectStatement()); } @Test void assertCompareToForDecsWithGroupByItemsForSQLServer() throws SQLException { - assertCompareToForDecsWithGroupByItems(new SQLServerSelectStatement()); + assertCompareToForDescWithGroupByItems(new SQLServerSelectStatement()); } - private void assertCompareToForDecsWithGroupByItems(final SelectStatement selectStatement) throws SQLException { + private void assertCompareToForDescWithGroupByItems(final SelectStatement selectStatement) throws SQLException { ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); + when(database.getName()).thenReturn(DefaultDatabase.LOGIC_NAME); when(database.getSchema(DefaultDatabase.LOGIC_NAME)).thenReturn(mock(ShardingSphereSchema.class)); selectStatement.setGroupBy(new GroupBySegment(0, 0, Arrays.asList( new IndexOrderByItemSegment(0, 0, 1, OrderDirection.DESC, NullsOrderType.FIRST), new IndexOrderByItemSegment(0, 0, 2, OrderDirection.DESC, NullsOrderType.FIRST)))); selectStatement.setOrderBy(new OrderBySegment(0, 0, Collections.emptyList())); selectStatement.setProjections(new ProjectionsSegment(0, 0)); - SelectStatementContext selectStatementContext = new SelectStatementContext(createShardingSphereMetaData(database), Collections.emptyList(), - selectStatement, DefaultDatabase.LOGIC_NAME, Collections.emptyList()); + SelectStatementContext selectStatementContext = new SelectStatementContext( + createShardingSphereMetaData(database), Collections.emptyList(), selectStatement, DefaultDatabase.LOGIC_NAME, Collections.emptyList()); GroupByRowComparator groupByRowComparator = new GroupByRowComparator(selectStatementContext, caseSensitives); MemoryQueryResultRow o1 = new MemoryQueryResultRow(mockQueryResult("1", "2")); MemoryQueryResultRow o2 = new MemoryQueryResultRow(mockQueryResult("3", "4")); @@ -271,8 +273,7 @@ private void assertCompareToForDecsWithGroupByItems(final SelectStatement select } private ShardingSphereMetaData createShardingSphereMetaData(final ShardingSphereDatabase database) { - return new ShardingSphereMetaData(Collections.singletonMap(DefaultDatabase.LOGIC_NAME, database), mock(ResourceMetaData.class), - mock(RuleMetaData.class), mock(ConfigurationProperties.class)); + return new ShardingSphereMetaData(Collections.singleton(database), mock(), mock(), mock()); } @Test @@ -302,6 +303,7 @@ void assertCompareToForEqualWithGroupByItemsForSQLServer() throws SQLException { private void assertCompareToForEqualWithGroupByItems(final SelectStatement selectStatement) throws SQLException { ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); + when(database.getName()).thenReturn(DefaultDatabase.LOGIC_NAME); when(database.getSchema(DefaultDatabase.LOGIC_NAME)).thenReturn(mock(ShardingSphereSchema.class)); selectStatement.setGroupBy(new GroupBySegment(0, 0, Arrays.asList( new IndexOrderByItemSegment(0, 0, 1, OrderDirection.ASC, NullsOrderType.FIRST), diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/merge/dql/groupby/GroupByStreamMergedResultTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/merge/dql/groupby/GroupByStreamMergedResultTest.java index 7aa0966111fd9..d8eea7f0c5e6c 100644 --- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/merge/dql/groupby/GroupByStreamMergedResultTest.java +++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/merge/dql/groupby/GroupByStreamMergedResultTest.java @@ -18,7 +18,6 @@ package org.apache.shardingsphere.sharding.merge.dql.groupby; import org.apache.shardingsphere.infra.binder.context.statement.dml.SelectStatementContext; -import org.apache.shardingsphere.infra.config.props.ConfigurationProperties; import org.apache.shardingsphere.infra.database.core.DefaultDatabase; import org.apache.shardingsphere.infra.database.core.metadata.database.enums.NullsOrderType; import org.apache.shardingsphere.infra.database.core.type.DatabaseType; @@ -169,6 +168,7 @@ private SelectStatementContext createSelectStatementContext() { MySQLSelectStatement selectStatement = new MySQLSelectStatement(); selectStatement.setFrom(tableSegment); ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); + when(database.getName()).thenReturn(DefaultDatabase.LOGIC_NAME); when(database.getSchema(DefaultDatabase.LOGIC_NAME)).thenReturn(mock(ShardingSphereSchema.class)); ProjectionsSegment projectionsSegment = new ProjectionsSegment(0, 0); projectionsSegment.getProjections().add(new AggregationProjectionSegment(0, 0, AggregationType.COUNT, "COUNT(*)")); @@ -176,12 +176,8 @@ private SelectStatementContext createSelectStatementContext() { selectStatement.setProjections(projectionsSegment); selectStatement.setGroupBy(new GroupBySegment(0, 0, Collections.singletonList(new IndexOrderByItemSegment(0, 0, 3, OrderDirection.ASC, NullsOrderType.FIRST)))); selectStatement.setOrderBy(new OrderBySegment(0, 0, Collections.singletonList(new IndexOrderByItemSegment(0, 0, 3, OrderDirection.ASC, NullsOrderType.FIRST)))); - return new SelectStatementContext(createShardingSphereMetaData(database), Collections.emptyList(), selectStatement, DefaultDatabase.LOGIC_NAME, Collections.emptyList()); - } - - private static ShardingSphereMetaData createShardingSphereMetaData(final ShardingSphereDatabase database) { - return new ShardingSphereMetaData(Collections.singletonMap(DefaultDatabase.LOGIC_NAME, database), mock(ResourceMetaData.class), - mock(RuleMetaData.class), mock(ConfigurationProperties.class)); + return new SelectStatementContext( + new ShardingSphereMetaData(Collections.singleton(database), mock(), mock(), mock()), Collections.emptyList(), selectStatement, DefaultDatabase.LOGIC_NAME, Collections.emptyList()); } private ShardingSphereDatabase createDatabase() { diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/merge/dql/orderby/OrderByStreamMergedResultTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/merge/dql/orderby/OrderByStreamMergedResultTest.java index a21a3634526c2..f7340ece00aa6 100644 --- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/merge/dql/orderby/OrderByStreamMergedResultTest.java +++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/merge/dql/orderby/OrderByStreamMergedResultTest.java @@ -80,8 +80,9 @@ void setUp() { } private ShardingSphereMetaData createShardingSphereMetaData() { - return new ShardingSphereMetaData(Collections.singletonMap(DefaultDatabase.LOGIC_NAME, mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS)), mock(ResourceMetaData.class), - mock(RuleMetaData.class), mock(ConfigurationProperties.class)); + ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); + when(database.getName()).thenReturn(DefaultDatabase.LOGIC_NAME); + return new ShardingSphereMetaData(Collections.singleton(database), mock(ResourceMetaData.class), mock(RuleMetaData.class), mock(ConfigurationProperties.class)); } @Test diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/merge/dql/orderby/OrderByValueTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/merge/dql/orderby/OrderByValueTest.java index c2dd776ba01ae..0e9e165fd77fc 100644 --- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/merge/dql/orderby/OrderByValueTest.java +++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/merge/dql/orderby/OrderByValueTest.java @@ -112,9 +112,10 @@ private void assertCompareToForAsc(final SelectStatement selectStatement) throws assertFalse(orderByValue2.getQueryResult().next()); } - private static ShardingSphereMetaData createShardingSphereMetaData() { - return new ShardingSphereMetaData(Collections.singletonMap(DefaultDatabase.LOGIC_NAME, mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS)), mock(ResourceMetaData.class), - mock(RuleMetaData.class), mock(ConfigurationProperties.class)); + private ShardingSphereMetaData createShardingSphereMetaData() { + ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); + when(database.getName()).thenReturn(DefaultDatabase.LOGIC_NAME); + return new ShardingSphereMetaData(Collections.singleton(database), mock(ResourceMetaData.class), mock(RuleMetaData.class), mock(ConfigurationProperties.class)); } @Test diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/merge/dql/pagination/LimitDecoratorMergedResultTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/merge/dql/pagination/LimitDecoratorMergedResultTest.java index 746ad5f37b5ed..52c1c2aac6ca4 100644 --- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/merge/dql/pagination/LimitDecoratorMergedResultTest.java +++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/merge/dql/pagination/LimitDecoratorMergedResultTest.java @@ -18,16 +18,13 @@ package org.apache.shardingsphere.sharding.merge.dql.pagination; import org.apache.shardingsphere.infra.binder.context.statement.dml.SelectStatementContext; -import org.apache.shardingsphere.infra.config.props.ConfigurationProperties; -import org.apache.shardingsphere.infra.metadata.database.resource.ResourceMetaData; -import org.apache.shardingsphere.infra.session.connection.ConnectionContext; import org.apache.shardingsphere.infra.database.core.DefaultDatabase; import org.apache.shardingsphere.infra.database.core.type.DatabaseType; import org.apache.shardingsphere.infra.executor.sql.execute.result.query.QueryResult; import org.apache.shardingsphere.infra.merge.result.MergedResult; import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData; import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; -import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData; +import org.apache.shardingsphere.infra.session.connection.ConnectionContext; import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader; import org.apache.shardingsphere.sharding.merge.dql.ShardingDQLResultMerger; import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.item.ProjectionsSegment; @@ -51,12 +48,12 @@ class LimitDecoratorMergedResultTest { @Test void assertNextForSkipAll() throws SQLException { ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); + when(database.getName()).thenReturn(DefaultDatabase.LOGIC_NAME); MySQLSelectStatement selectStatement = new MySQLSelectStatement(); selectStatement.setProjections(new ProjectionsSegment(0, 0)); selectStatement.setLimit(new LimitSegment(0, 0, new NumberLiteralLimitValueSegment(0, 0, Long.MAX_VALUE), null)); - SelectStatementContext selectStatementContext = - new SelectStatementContext(createShardingSphereMetaData(database), Collections.emptyList(), selectStatement, DefaultDatabase.LOGIC_NAME, Collections.emptyList()); - when(database.getName()).thenReturn(DefaultDatabase.LOGIC_NAME); + SelectStatementContext selectStatementContext = new SelectStatementContext( + new ShardingSphereMetaData(Collections.singleton(database), mock(), mock(), mock()), Collections.emptyList(), selectStatement, DefaultDatabase.LOGIC_NAME, Collections.emptyList()); ShardingDQLResultMerger resultMerger = new ShardingDQLResultMerger(TypedSPILoader.getService(DatabaseType.class, "MySQL")); MergedResult actual = resultMerger.merge(Arrays.asList(mockQueryResult(), mockQueryResult(), mockQueryResult(), mockQueryResult()), selectStatementContext, database, mock(ConnectionContext.class)); @@ -66,12 +63,12 @@ void assertNextForSkipAll() throws SQLException { @Test void assertNextWithoutRowCount() throws SQLException { ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); + when(database.getName()).thenReturn(DefaultDatabase.LOGIC_NAME); MySQLSelectStatement selectStatement = new MySQLSelectStatement(); selectStatement.setProjections(new ProjectionsSegment(0, 0)); selectStatement.setLimit(new LimitSegment(0, 0, new NumberLiteralLimitValueSegment(0, 0, 2L), null)); - SelectStatementContext selectStatementContext = - new SelectStatementContext(createShardingSphereMetaData(database), Collections.emptyList(), selectStatement, DefaultDatabase.LOGIC_NAME, Collections.emptyList()); - when(database.getName()).thenReturn(DefaultDatabase.LOGIC_NAME); + SelectStatementContext selectStatementContext = new SelectStatementContext( + new ShardingSphereMetaData(Collections.singleton(database), mock(), mock(), mock()), Collections.emptyList(), selectStatement, DefaultDatabase.LOGIC_NAME, Collections.emptyList()); ShardingDQLResultMerger resultMerger = new ShardingDQLResultMerger(TypedSPILoader.getService(DatabaseType.class, "MySQL")); MergedResult actual = resultMerger.merge(Arrays.asList(mockQueryResult(), mockQueryResult(), mockQueryResult(), mockQueryResult()), selectStatementContext, database, mock(ConnectionContext.class)); @@ -84,12 +81,12 @@ void assertNextWithoutRowCount() throws SQLException { @Test void assertNextWithRowCount() throws SQLException { ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); + when(database.getName()).thenReturn(DefaultDatabase.LOGIC_NAME); MySQLSelectStatement selectStatement = new MySQLSelectStatement(); selectStatement.setProjections(new ProjectionsSegment(0, 0)); selectStatement.setLimit(new LimitSegment(0, 0, new NumberLiteralLimitValueSegment(0, 0, 2L), new NumberLiteralLimitValueSegment(0, 0, 2L))); - SelectStatementContext selectStatementContext = - new SelectStatementContext(createShardingSphereMetaData(database), Collections.emptyList(), selectStatement, DefaultDatabase.LOGIC_NAME, Collections.emptyList()); - when(database.getName()).thenReturn(DefaultDatabase.LOGIC_NAME); + SelectStatementContext selectStatementContext = new SelectStatementContext( + new ShardingSphereMetaData(Collections.singleton(database), mock(), mock(), mock()), Collections.emptyList(), selectStatement, DefaultDatabase.LOGIC_NAME, Collections.emptyList()); ShardingDQLResultMerger resultMerger = new ShardingDQLResultMerger(TypedSPILoader.getService(DatabaseType.class, "MySQL")); MergedResult actual = resultMerger.merge(Arrays.asList(mockQueryResult(), mockQueryResult(), mockQueryResult(), mockQueryResult()), selectStatementContext, database, mock(ConnectionContext.class)); @@ -98,11 +95,6 @@ void assertNextWithRowCount() throws SQLException { assertFalse(actual.next()); } - private ShardingSphereMetaData createShardingSphereMetaData(final ShardingSphereDatabase database) { - return new ShardingSphereMetaData(Collections.singletonMap(DefaultDatabase.LOGIC_NAME, database), mock(ResourceMetaData.class), - mock(RuleMetaData.class), mock(ConfigurationProperties.class)); - } - private QueryResult mockQueryResult() throws SQLException { QueryResult result = mock(QueryResult.class, RETURNS_DEEP_STUBS); when(result.next()).thenReturn(true, true, false); diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/merge/dql/pagination/RowNumberDecoratorMergedResultTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/merge/dql/pagination/RowNumberDecoratorMergedResultTest.java index 636b6f423e3bc..a53973ca64284 100644 --- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/merge/dql/pagination/RowNumberDecoratorMergedResultTest.java +++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/merge/dql/pagination/RowNumberDecoratorMergedResultTest.java @@ -81,8 +81,8 @@ void assertNextForSkipAll() throws SQLException { selectStatement.setWhere(whereSegment); ShardingDQLResultMerger resultMerger = new ShardingDQLResultMerger(TypedSPILoader.getService(DatabaseType.class, "Oracle")); ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); - SelectStatementContext selectStatementContext = new SelectStatementContext(createShardingSphereMetaData(database), null, selectStatement, DefaultDatabase.LOGIC_NAME, Collections.emptyList()); when(database.getName()).thenReturn(DefaultDatabase.LOGIC_NAME); + SelectStatementContext selectStatementContext = new SelectStatementContext(createShardingSphereMetaData(database), null, selectStatement, DefaultDatabase.LOGIC_NAME, Collections.emptyList()); MergedResult actual = resultMerger.merge(Arrays.asList(mockQueryResult(), mockQueryResult(), mockQueryResult(), mockQueryResult()), selectStatementContext, database, mock(ConnectionContext.class)); assertFalse(actual.next()); @@ -92,10 +92,10 @@ void assertNextForSkipAll() throws SQLException { void assertNextWithoutOffsetWithoutRowCount() throws SQLException { ShardingDQLResultMerger resultMerger = new ShardingDQLResultMerger(TypedSPILoader.getService(DatabaseType.class, "Oracle")); ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); + when(database.getName()).thenReturn(DefaultDatabase.LOGIC_NAME); OracleSelectStatement selectStatement = new OracleSelectStatement(); selectStatement.setProjections(new ProjectionsSegment(0, 0)); SelectStatementContext selectStatementContext = new SelectStatementContext(createShardingSphereMetaData(database), null, selectStatement, DefaultDatabase.LOGIC_NAME, Collections.emptyList()); - when(database.getName()).thenReturn(DefaultDatabase.LOGIC_NAME); MergedResult actual = resultMerger.merge(Arrays.asList(mockQueryResult(), mockQueryResult(), mockQueryResult(), mockQueryResult()), selectStatementContext, database, mock(ConnectionContext.class)); for (int i = 0; i < 8; i++) { @@ -129,8 +129,8 @@ void assertNextForRowCountBoundOpenedFalse() throws SQLException { selectStatement.setWhere(whereSegment); ShardingDQLResultMerger resultMerger = new ShardingDQLResultMerger(TypedSPILoader.getService(DatabaseType.class, "Oracle")); ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); - SelectStatementContext selectStatementContext = new SelectStatementContext(createShardingSphereMetaData(database), null, selectStatement, DefaultDatabase.LOGIC_NAME, Collections.emptyList()); when(database.getName()).thenReturn(DefaultDatabase.LOGIC_NAME); + SelectStatementContext selectStatementContext = new SelectStatementContext(createShardingSphereMetaData(database), null, selectStatement, DefaultDatabase.LOGIC_NAME, Collections.emptyList()); MergedResult actual = resultMerger.merge(Arrays.asList(mockQueryResult(), mockQueryResult(), mockQueryResult(), mockQueryResult()), selectStatementContext, database, mock(ConnectionContext.class)); assertTrue(actual.next()); @@ -163,10 +163,10 @@ void assertNextForRowCountBoundOpenedTrue() throws SQLException { selectStatement.setWhere(whereSegment); ShardingDQLResultMerger resultMerger = new ShardingDQLResultMerger(TypedSPILoader.getService(DatabaseType.class, "Oracle")); ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); - SelectStatementContext selectStatementContext = new SelectStatementContext(createShardingSphereMetaData(database), null, selectStatement, DefaultDatabase.LOGIC_NAME, Collections.emptyList()); when(database.getName()).thenReturn(DefaultDatabase.LOGIC_NAME); - MergedResult actual = resultMerger.merge(Arrays.asList(mockQueryResult(), mockQueryResult(), mockQueryResult(), mockQueryResult()), selectStatementContext, database, - mock(ConnectionContext.class)); + SelectStatementContext selectStatementContext = new SelectStatementContext(createShardingSphereMetaData(database), null, selectStatement, DefaultDatabase.LOGIC_NAME, Collections.emptyList()); + MergedResult actual = + resultMerger.merge(Arrays.asList(mockQueryResult(), mockQueryResult(), mockQueryResult(), mockQueryResult()), selectStatementContext, database, mock(ConnectionContext.class)); assertTrue(actual.next()); assertTrue(actual.next()); assertTrue(actual.next()); @@ -174,8 +174,7 @@ void assertNextForRowCountBoundOpenedTrue() throws SQLException { } private ShardingSphereMetaData createShardingSphereMetaData(final ShardingSphereDatabase database) { - return new ShardingSphereMetaData(Collections.singletonMap(DefaultDatabase.LOGIC_NAME, database), mock(ResourceMetaData.class), - mock(RuleMetaData.class), mock(ConfigurationProperties.class)); + return new ShardingSphereMetaData(Collections.singleton(database), mock(ResourceMetaData.class), mock(RuleMetaData.class), mock(ConfigurationProperties.class)); } private QueryResult mockQueryResult() throws SQLException { diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/merge/dql/pagination/TopAndRowNumberDecoratorMergedResultTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/merge/dql/pagination/TopAndRowNumberDecoratorMergedResultTest.java index 9f3de6d04ba4a..026a139be5eca 100644 --- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/merge/dql/pagination/TopAndRowNumberDecoratorMergedResultTest.java +++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/merge/dql/pagination/TopAndRowNumberDecoratorMergedResultTest.java @@ -61,6 +61,7 @@ void setUp() { @Test void assertNextForSkipAll() throws SQLException { ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); + when(database.getName()).thenReturn(DefaultDatabase.LOGIC_NAME); when(database.getSchema(DefaultDatabase.LOGIC_NAME)).thenReturn(schema); SQLServerSelectStatement sqlStatement = new SQLServerSelectStatement(); sqlStatement.setProjections(new ProjectionsSegment(0, 0)); @@ -77,6 +78,7 @@ void assertNextForSkipAll() throws SQLException { void assertNextWithoutOffsetWithRowCount() throws SQLException { final ShardingDQLResultMerger resultMerger = new ShardingDQLResultMerger(TypedSPILoader.getService(DatabaseType.class, "SQLServer")); ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); + when(database.getName()).thenReturn(DefaultDatabase.LOGIC_NAME); when(database.getSchema(DefaultDatabase.LOGIC_NAME)).thenReturn(schema); SQLServerSelectStatement sqlStatement = new SQLServerSelectStatement(); sqlStatement.setProjections(new ProjectionsSegment(0, 0)); @@ -94,6 +96,7 @@ void assertNextWithoutOffsetWithRowCount() throws SQLException { @Test void assertNextWithOffsetWithoutRowCount() throws SQLException { ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); + when(database.getName()).thenReturn(DefaultDatabase.LOGIC_NAME); when(database.getSchema(DefaultDatabase.LOGIC_NAME)).thenReturn(schema); SQLServerSelectStatement sqlStatement = new SQLServerSelectStatement(); sqlStatement.setProjections(new ProjectionsSegment(0, 0)); @@ -113,6 +116,7 @@ void assertNextWithOffsetWithoutRowCount() throws SQLException { void assertNextWithOffsetBoundOpenedFalse() throws SQLException { final ShardingDQLResultMerger resultMerger = new ShardingDQLResultMerger(TypedSPILoader.getService(DatabaseType.class, "SQLServer")); final ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); + when(database.getName()).thenReturn(DefaultDatabase.LOGIC_NAME); when(database.getSchema(DefaultDatabase.LOGIC_NAME)).thenReturn(schema); SQLServerSelectStatement sqlStatement = new SQLServerSelectStatement(); sqlStatement.setProjections(new ProjectionsSegment(0, 0)); @@ -130,6 +134,7 @@ void assertNextWithOffsetBoundOpenedFalse() throws SQLException { void assertNextWithOffsetBoundOpenedTrue() throws SQLException { final ShardingDQLResultMerger resultMerger = new ShardingDQLResultMerger(TypedSPILoader.getService(DatabaseType.class, "SQLServer")); ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); + when(database.getName()).thenReturn(DefaultDatabase.LOGIC_NAME); when(database.getSchema(DefaultDatabase.LOGIC_NAME)).thenReturn(schema); SQLServerSelectStatement sqlStatement = new SQLServerSelectStatement(); sqlStatement.setProjections(new ProjectionsSegment(0, 0)); @@ -145,8 +150,7 @@ void assertNextWithOffsetBoundOpenedTrue() throws SQLException { } private ShardingSphereMetaData createShardingSphereMetaData(final ShardingSphereDatabase database) { - return new ShardingSphereMetaData(Collections.singletonMap(DefaultDatabase.LOGIC_NAME, database), mock(ResourceMetaData.class), - mock(RuleMetaData.class), mock(ConfigurationProperties.class)); + return new ShardingSphereMetaData(Collections.singleton(database), mock(ResourceMetaData.class), mock(RuleMetaData.class), mock(ConfigurationProperties.class)); } private QueryResult mockQueryResult() throws SQLException { diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/metadata/data/ShardingStatisticsTableCollectorTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/metadata/data/ShardingStatisticsTableCollectorTest.java index 16bb6c65e6fd8..0d176b9e76fcb 100644 --- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/metadata/data/ShardingStatisticsTableCollectorTest.java +++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/metadata/data/ShardingStatisticsTableCollectorTest.java @@ -67,9 +67,10 @@ void setUp() { @Test void assertCollectWithoutShardingRule() throws SQLException { ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); + when(database.getName()).thenReturn("foo_db"); when(database.getProtocolType()).thenReturn(databaseType); ShardingSphereMetaData metaData = new ShardingSphereMetaData( - Collections.singletonMap("foo_db", database), mock(ResourceMetaData.class), mock(RuleMetaData.class), new ConfigurationProperties(new Properties())); + Collections.singleton(database), mock(ResourceMetaData.class), mock(RuleMetaData.class), new ConfigurationProperties(new Properties())); Optional actual = statisticsCollector.collect("foo_db", mock(ShardingSphereTable.class), metaData); assertFalse(actual.isPresent()); } @@ -83,8 +84,7 @@ void assertCollectWithShardingRule() throws SQLException { storageUnits.put("ds_1", mock(StorageUnit.class, RETURNS_DEEP_STUBS)); ShardingSphereDatabase database = new ShardingSphereDatabase( "foo_db", databaseType, new ResourceMetaData(Collections.emptyMap(), storageUnits), new RuleMetaData(Collections.singleton(rule)), Collections.emptyList()); - ShardingSphereMetaData metaData = new ShardingSphereMetaData( - Collections.singletonMap("foo_db", database), mock(ResourceMetaData.class), mock(RuleMetaData.class), new ConfigurationProperties(new Properties())); + ShardingSphereMetaData metaData = new ShardingSphereMetaData(Collections.singleton(database), mock(), mock(), new ConfigurationProperties(new Properties())); Optional actual = statisticsCollector.collect("foo_db", mock(ShardingSphereTable.class), metaData); assertTrue(actual.isPresent()); assertThat(actual.get().getName(), is("sharding_table_statistics")); diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/metadata/data/dialect/type/MySQLShardingStatisticsTableCollectorTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/metadata/data/dialect/type/MySQLShardingStatisticsTableCollectorTest.java index 37fe26eb1b5f9..1db191df427aa 100644 --- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/metadata/data/dialect/type/MySQLShardingStatisticsTableCollectorTest.java +++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/metadata/data/dialect/type/MySQLShardingStatisticsTableCollectorTest.java @@ -75,8 +75,7 @@ void assertCollect() throws SQLException { storageUnits.put("ds_1", mockStorageUnit(mockResultSet())); ShardingSphereDatabase database = new ShardingSphereDatabase( "foo_db", databaseType, new ResourceMetaData(Collections.emptyMap(), storageUnits), new RuleMetaData(Collections.singleton(rule)), Collections.emptyList()); - ShardingSphereMetaData metaData = new ShardingSphereMetaData( - Collections.singletonMap("foo_db", database), mock(ResourceMetaData.class), mock(RuleMetaData.class), new ConfigurationProperties(new Properties())); + ShardingSphereMetaData metaData = new ShardingSphereMetaData(Collections.singleton(database), mock(), mock(), new ConfigurationProperties(new Properties())); Optional actual = statisticsCollector.collect("foo_db", mock(ShardingSphereTable.class), metaData); assertTrue(actual.isPresent()); assertThat(actual.get().getName(), is("sharding_table_statistics")); diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/metadata/data/dialect/type/OpenGaussShardingStatisticsTableCollectorTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/metadata/data/dialect/type/OpenGaussShardingStatisticsTableCollectorTest.java index c5fe41fa48db9..906e88112dcd9 100644 --- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/metadata/data/dialect/type/OpenGaussShardingStatisticsTableCollectorTest.java +++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/metadata/data/dialect/type/OpenGaussShardingStatisticsTableCollectorTest.java @@ -76,8 +76,7 @@ void assertCollectWithoutExistedTables() throws SQLException { storageUnits.put("ds_1", mockStorageUnit(mockResultSet(), false)); ShardingSphereDatabase database = new ShardingSphereDatabase( "foo_db", databaseType, new ResourceMetaData(Collections.emptyMap(), storageUnits), new RuleMetaData(Collections.singleton(rule)), Collections.emptyList()); - ShardingSphereMetaData metaData = new ShardingSphereMetaData( - Collections.singletonMap("foo_db", database), mock(ResourceMetaData.class), mock(RuleMetaData.class), new ConfigurationProperties(new Properties())); + ShardingSphereMetaData metaData = new ShardingSphereMetaData(Collections.singleton(database), mock(), mock(), new ConfigurationProperties(new Properties())); Optional actual = statisticsCollector.collect("foo_db", mock(ShardingSphereTable.class), metaData); assertTrue(actual.isPresent()); assertThat(actual.get().getName(), is("sharding_table_statistics")); @@ -96,8 +95,7 @@ void assertCollectWithExistedTables() throws SQLException { storageUnits.put("ds_1", mockStorageUnit(mockResultSet(), true)); ShardingSphereDatabase database = new ShardingSphereDatabase( "foo_db", databaseType, new ResourceMetaData(Collections.emptyMap(), storageUnits), new RuleMetaData(Collections.singleton(rule)), Collections.emptyList()); - ShardingSphereMetaData metaData = new ShardingSphereMetaData( - Collections.singletonMap("foo_db", database), mock(ResourceMetaData.class), mock(RuleMetaData.class), new ConfigurationProperties(new Properties())); + ShardingSphereMetaData metaData = new ShardingSphereMetaData(Collections.singleton(database), mock(), mock(), new ConfigurationProperties(new Properties())); Optional actual = statisticsCollector.collect("foo_db", mock(ShardingSphereTable.class), metaData); assertTrue(actual.isPresent()); assertThat(actual.get().getName(), is("sharding_table_statistics")); diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/metadata/data/dialect/type/PostgreSQLShardingStatisticsTableCollectorTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/metadata/data/dialect/type/PostgreSQLShardingStatisticsTableCollectorTest.java index 2881ef0802b2b..aa1cfccdb46da 100644 --- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/metadata/data/dialect/type/PostgreSQLShardingStatisticsTableCollectorTest.java +++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/metadata/data/dialect/type/PostgreSQLShardingStatisticsTableCollectorTest.java @@ -75,8 +75,7 @@ void assertCollect() throws SQLException { storageUnits.put("ds_1", mockStorageUnit(mockResultSet())); ShardingSphereDatabase database = new ShardingSphereDatabase( "foo_db", databaseType, new ResourceMetaData(Collections.emptyMap(), storageUnits), new RuleMetaData(Collections.singleton(rule)), Collections.emptyList()); - ShardingSphereMetaData metaData = new ShardingSphereMetaData( - Collections.singletonMap("foo_db", database), mock(ResourceMetaData.class), mock(RuleMetaData.class), new ConfigurationProperties(new Properties())); + ShardingSphereMetaData metaData = new ShardingSphereMetaData(Collections.singleton(database), mock(), mock(), new ConfigurationProperties(new Properties())); Optional actual = statisticsCollector.collect("foo_db", mock(ShardingSphereTable.class), metaData); assertTrue(actual.isPresent()); assertThat(actual.get().getName(), is("sharding_table_statistics")); diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/rewrite/token/generator/impl/ShardingRemoveTokenGeneratorTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/rewrite/token/generator/impl/ShardingRemoveTokenGeneratorTest.java index 906e1c84c5f78..1d6bc8d635c33 100644 --- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/rewrite/token/generator/impl/ShardingRemoveTokenGeneratorTest.java +++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/rewrite/token/generator/impl/ShardingRemoveTokenGeneratorTest.java @@ -19,13 +19,10 @@ import org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext; import org.apache.shardingsphere.infra.binder.context.statement.dml.SelectStatementContext; -import org.apache.shardingsphere.infra.config.props.ConfigurationProperties; import org.apache.shardingsphere.infra.database.core.DefaultDatabase; import org.apache.shardingsphere.infra.database.core.metadata.database.enums.NullsOrderType; import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData; import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; -import org.apache.shardingsphere.infra.metadata.database.resource.ResourceMetaData; -import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData; import org.apache.shardingsphere.infra.rewrite.sql.token.common.pojo.SQLToken; import org.apache.shardingsphere.infra.rewrite.sql.token.common.pojo.generic.RemoveToken; import org.apache.shardingsphere.sql.parser.statement.core.enums.AggregationType; @@ -85,13 +82,10 @@ private SelectStatementContext createSelectStatementContext() { selectStatement.setFrom(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("t_user")))); selectStatement.setGroupBy(new GroupBySegment(0, 0, Collections.singletonList(new IndexOrderByItemSegment(0, 0, 1, OrderDirection.ASC, NullsOrderType.FIRST)))); selectStatement.setProjections(createProjectionsSegment()); + ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); + when(database.getName()).thenReturn(DefaultDatabase.LOGIC_NAME); return new SelectStatementContext( - createShardingSphereMetaData(mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS)), Collections.emptyList(), selectStatement, DefaultDatabase.LOGIC_NAME, Collections.emptyList()); - } - - private ShardingSphereMetaData createShardingSphereMetaData(final ShardingSphereDatabase database) { - return new ShardingSphereMetaData(Collections.singletonMap(DefaultDatabase.LOGIC_NAME, database), mock(ResourceMetaData.class), - mock(RuleMetaData.class), mock(ConfigurationProperties.class)); + new ShardingSphereMetaData(Collections.singleton(database), mock(), mock(), mock()), Collections.emptyList(), selectStatement, DefaultDatabase.LOGIC_NAME, Collections.emptyList()); } private static ProjectionsSegment createProjectionsSegment() { diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/checker/dml/ShardingInsertRouteContextCheckerTest.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/checker/dml/ShardingInsertRouteContextCheckerTest.java index 8349ce1e078cd..31392918784e6 100644 --- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/checker/dml/ShardingInsertRouteContextCheckerTest.java +++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/checker/dml/ShardingInsertRouteContextCheckerTest.java @@ -19,13 +19,10 @@ import org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext; import org.apache.shardingsphere.infra.binder.context.statement.dml.InsertStatementContext; -import org.apache.shardingsphere.infra.config.props.ConfigurationProperties; import org.apache.shardingsphere.infra.database.core.DefaultDatabase; import org.apache.shardingsphere.infra.datanode.DataNode; import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData; import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; -import org.apache.shardingsphere.infra.metadata.database.resource.ResourceMetaData; -import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData; import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema; import org.apache.shardingsphere.infra.route.context.RouteContext; import org.apache.shardingsphere.infra.route.context.RouteMapper; @@ -92,13 +89,13 @@ void assertCheckWhenInsertWithSingleRouting() { SQLStatementContext sqlStatementContext = createInsertStatementContext(Collections.singletonList(1), createInsertStatement()); when(routeContext.isSingleRouting()).thenReturn(true); when(queryContext.getSqlStatementContext()).thenReturn(sqlStatementContext); - assertDoesNotThrow(() -> new ShardingInsertRouteContextChecker(shardingConditions).check(shardingRule, queryContext, database, mock(ConfigurationProperties.class), routeContext)); + assertDoesNotThrow(() -> new ShardingInsertRouteContextChecker(shardingConditions).check(shardingRule, queryContext, database, mock(), routeContext)); } private InsertStatementContext createInsertStatementContext(final List params, final InsertStatement insertStatement) { + when(database.getName()).thenReturn(DefaultDatabase.LOGIC_NAME); when(database.getSchema(DefaultDatabase.LOGIC_NAME)).thenReturn(mock(ShardingSphereSchema.class)); - ShardingSphereMetaData metaData = new ShardingSphereMetaData(Collections.singletonMap(DefaultDatabase.LOGIC_NAME, database), mock(ResourceMetaData.class), - mock(RuleMetaData.class), mock(ConfigurationProperties.class)); + ShardingSphereMetaData metaData = new ShardingSphereMetaData(Collections.singleton(database), mock(), mock(), mock()); return new InsertStatementContext(metaData, params, insertStatement, DefaultDatabase.LOGIC_NAME); } @@ -107,7 +104,7 @@ void assertCheckWhenInsertWithBroadcastTable() { SQLStatementContext sqlStatementContext = createInsertStatementContext(Collections.singletonList(1), createInsertStatement()); when(routeContext.isSingleRouting()).thenReturn(false); when(queryContext.getSqlStatementContext()).thenReturn(sqlStatementContext); - assertDoesNotThrow(() -> new ShardingInsertRouteContextChecker(shardingConditions).check(shardingRule, queryContext, database, mock(ConfigurationProperties.class), routeContext)); + assertDoesNotThrow(() -> new ShardingInsertRouteContextChecker(shardingConditions).check(shardingRule, queryContext, database, mock(), routeContext)); } @Test @@ -116,7 +113,7 @@ void assertCheckWhenInsertWithRoutingToSingleDataNode() { when(routeContext.isSingleRouting()).thenReturn(false); when(routeContext.getOriginalDataNodes()).thenReturn(getSingleRouteDataNodes()); when(queryContext.getSqlStatementContext()).thenReturn(sqlStatementContext); - assertDoesNotThrow(() -> new ShardingInsertRouteContextChecker(shardingConditions).check(shardingRule, queryContext, database, mock(ConfigurationProperties.class), routeContext)); + assertDoesNotThrow(() -> new ShardingInsertRouteContextChecker(shardingConditions).check(shardingRule, queryContext, database, mock(), routeContext)); } @Test @@ -125,8 +122,7 @@ void assertCheckWhenInsertWithRoutingToMultipleDataNodes() { when(routeContext.isSingleRouting()).thenReturn(false); when(routeContext.getOriginalDataNodes()).thenReturn(getMultipleRouteDataNodes()); when(queryContext.getSqlStatementContext()).thenReturn(sqlStatementContext); - assertThrows(DuplicateInsertDataRecordException.class, - () -> new ShardingInsertRouteContextChecker(shardingConditions).check(shardingRule, queryContext, database, mock(ConfigurationProperties.class), routeContext)); + assertThrows(DuplicateInsertDataRecordException.class, () -> new ShardingInsertRouteContextChecker(shardingConditions).check(shardingRule, queryContext, database, mock(), routeContext)); } @Test @@ -137,7 +133,7 @@ void assertCheckWhenNotOnDuplicateKeyUpdateShardingColumn() { InsertStatementContext insertStatementContext = createInsertStatementContext(params, createInsertStatement()); when(queryContext.getSqlStatementContext()).thenReturn(insertStatementContext); when(queryContext.getParameters()).thenReturn(params); - assertDoesNotThrow(() -> new ShardingInsertRouteContextChecker(mock(ShardingConditions.class)).check(shardingRule, queryContext, database, mock(ConfigurationProperties.class), routeContext)); + assertDoesNotThrow(() -> new ShardingInsertRouteContextChecker(mock(ShardingConditions.class)).check(shardingRule, queryContext, database, mock(), routeContext)); } @Test @@ -147,8 +143,7 @@ void assertCheckWhenOnDuplicateKeyUpdateShardingColumnWithSameRouteContext() { InsertStatementContext insertStatementContext = createInsertStatementContext(params, createInsertStatement()); when(queryContext.getSqlStatementContext()).thenReturn(insertStatementContext); when(queryContext.getParameters()).thenReturn(params); - assertDoesNotThrow(() -> new ShardingInsertRouteContextChecker(mock(ShardingConditions.class)).check(shardingRule, queryContext, database, mock(ConfigurationProperties.class), - createSingleRouteContext())); + assertDoesNotThrow(() -> new ShardingInsertRouteContextChecker(mock(ShardingConditions.class)).check(shardingRule, queryContext, database, mock(), createSingleRouteContext())); } @Test @@ -159,7 +154,7 @@ void assertCheckWhenOnDuplicateKeyUpdateShardingColumnWithDifferentRouteContext( when(queryContext.getSqlStatementContext()).thenReturn(insertStatementContext); when(queryContext.getParameters()).thenReturn(params); assertThrows(UnsupportedUpdatingShardingValueException.class, - () -> new ShardingInsertRouteContextChecker(mock(ShardingConditions.class)).check(shardingRule, queryContext, database, mock(ConfigurationProperties.class), createFullRouteContext())); + () -> new ShardingInsertRouteContextChecker(mock()).check(shardingRule, queryContext, database, mock(), createFullRouteContext())); } private void mockShardingRuleForUpdateShardingColumn() { diff --git a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/type/standard/assertion/ShardingRouteAssert.java b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/type/standard/assertion/ShardingRouteAssert.java index db3c9f83ba63b..107e9b7140d05 100644 --- a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/type/standard/assertion/ShardingRouteAssert.java +++ b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/route/engine/type/standard/assertion/ShardingRouteAssert.java @@ -85,7 +85,7 @@ public static RouteContext assertRoute(final String sql, final List para when(broadcastRule.getAttributes().findAttribute(TableMapperRuleAttribute.class)).thenReturn(Optional.of(ruleAttribute)); RuleMetaData ruleMetaData = new RuleMetaData(Arrays.asList(shardingRule, broadcastRule, singleRule, timestampServiceRule)); ShardingSphereDatabase database = new ShardingSphereDatabase(DefaultDatabase.LOGIC_NAME, databaseType, mock(ResourceMetaData.class, RETURNS_DEEP_STUBS), ruleMetaData, buildSchemas()); - ShardingSphereMetaData metaData = createShardingSphereMetaData(database); + ShardingSphereMetaData metaData = new ShardingSphereMetaData(Collections.singleton(database), mock(), mock(), mock()); SQLStatementContext sqlStatementContext = new SQLBindEngine(metaData, DefaultDatabase.LOGIC_NAME, new HintValueContext()).bind(sqlStatementParserEngine.parse(sql, false), params); ConnectionContext connectionContext = new ConnectionContext(Collections::emptySet); connectionContext.setCurrentDatabaseName(DefaultDatabase.LOGIC_NAME); @@ -94,11 +94,6 @@ public static RouteContext assertRoute(final String sql, final List para return new SQLRouteEngine(Arrays.asList(shardingRule, singleRule), props).route(queryContext, mock(RuleMetaData.class), database); } - private static ShardingSphereMetaData createShardingSphereMetaData(final ShardingSphereDatabase database) { - return new ShardingSphereMetaData(Collections.singletonMap(DefaultDatabase.LOGIC_NAME, database), mock(ResourceMetaData.class), - mock(RuleMetaData.class), mock(ConfigurationProperties.class)); - } - private static Collection buildSchemas() { Collection tables = new LinkedList<>(); tables.add(new ShardingSphereTable("t_order", Arrays.asList(new ShardingSphereColumn("order_id", Types.INTEGER, true, false, false, true, false, false), diff --git a/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/context/segment/select/pagination/PaginationContextTest.java b/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/context/segment/select/pagination/PaginationContextTest.java index ab47e7fe806c8..5d991ae48c578 100644 --- a/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/context/segment/select/pagination/PaginationContextTest.java +++ b/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/context/segment/select/pagination/PaginationContextTest.java @@ -44,7 +44,6 @@ import java.util.Arrays; import java.util.Collections; import java.util.List; -import java.util.Map; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; @@ -52,6 +51,7 @@ 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 PaginationContextTest { @@ -167,9 +167,9 @@ void assertGetRevisedRowCountForSQLServer() { private void getRevisedRowCount(final SelectStatement selectStatement) { selectStatement.setProjections(new ProjectionsSegment(0, 0)); - Map databases = Collections.singletonMap(DefaultDatabase.LOGIC_NAME, mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS)); - ShardingSphereMetaData metaData = new ShardingSphereMetaData(databases, mock(ResourceMetaData.class), - mock(RuleMetaData.class), mock(ConfigurationProperties.class)); + ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); + when(database.getName()).thenReturn(DefaultDatabase.LOGIC_NAME); + ShardingSphereMetaData metaData = new ShardingSphereMetaData(Collections.singleton(database), mock(ResourceMetaData.class), mock(RuleMetaData.class), mock(ConfigurationProperties.class)); SelectStatementContext selectStatementContext = new SelectStatementContext(metaData, Collections.emptyList(), selectStatement, DefaultDatabase.LOGIC_NAME, Collections.emptyList()); assertThat(new PaginationContext(getOffsetSegment(), getRowCountSegment(), getParameters()).getRevisedRowCount(selectStatementContext), is(50L)); } @@ -203,9 +203,9 @@ private void getRevisedRowCountWithMax(final SelectStatement selectStatement) { selectStatement.setProjections(new ProjectionsSegment(0, 0)); selectStatement.setGroupBy(new GroupBySegment(0, 0, Collections.singletonList(new IndexOrderByItemSegment(0, 0, 1, OrderDirection.ASC, NullsOrderType.LAST)))); selectStatement.setOrderBy(new OrderBySegment(0, 0, Collections.singletonList(new IndexOrderByItemSegment(0, 0, 1, OrderDirection.DESC, NullsOrderType.LAST)))); - Map databases = Collections.singletonMap(DefaultDatabase.LOGIC_NAME, mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS)); - ShardingSphereMetaData metaData = - new ShardingSphereMetaData(databases, mock(ResourceMetaData.class), mock(RuleMetaData.class), mock(ConfigurationProperties.class)); + ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); + when(database.getName()).thenReturn(DefaultDatabase.LOGIC_NAME); + ShardingSphereMetaData metaData = new ShardingSphereMetaData(Collections.singleton(database), mock(ResourceMetaData.class), mock(RuleMetaData.class), mock(ConfigurationProperties.class)); SelectStatementContext selectStatementContext = new SelectStatementContext(metaData, Collections.emptyList(), selectStatement, DefaultDatabase.LOGIC_NAME, Collections.emptyList()); assertThat(new PaginationContext(getOffsetSegment(), getRowCountSegment(), getParameters()).getRevisedRowCount(selectStatementContext), is((long) Integer.MAX_VALUE)); } diff --git a/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/context/segment/select/projection/engine/ProjectionsContextEngineTest.java b/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/context/segment/select/projection/engine/ProjectionsContextEngineTest.java index aa6b6696b90f6..7cc16b3e2ecb7 100644 --- a/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/context/segment/select/projection/engine/ProjectionsContextEngineTest.java +++ b/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/context/segment/select/projection/engine/ProjectionsContextEngineTest.java @@ -56,13 +56,13 @@ import java.util.Arrays; import java.util.Collections; -import java.util.Map; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.mockito.Mockito.RETURNS_DEEP_STUBS; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; @ExtendWith(MockitoExtension.class) class ProjectionsContextEngineTest { @@ -221,8 +221,9 @@ private void assertCreateProjectionsContextWithoutIndexOrderByItemSegment(final } private SelectStatementContext createSelectStatementContext(final SelectStatement selectStatement) { - Map databases = Collections.singletonMap(DefaultDatabase.LOGIC_NAME, mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS)); - ShardingSphereMetaData metaData = new ShardingSphereMetaData(databases, mock(ResourceMetaData.class), mock(RuleMetaData.class), mock(ConfigurationProperties.class)); + ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); + when(database.getName()).thenReturn(DefaultDatabase.LOGIC_NAME); + ShardingSphereMetaData metaData = new ShardingSphereMetaData(Collections.singleton(database), mock(ResourceMetaData.class), mock(RuleMetaData.class), mock(ConfigurationProperties.class)); return new SelectStatementContext(metaData, Collections.emptyList(), selectStatement, DefaultDatabase.LOGIC_NAME, Collections.emptyList()); } diff --git a/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/context/statement/SQLStatementContextFactoryTest.java b/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/context/statement/SQLStatementContextFactoryTest.java index 87c213b83e689..c1baeebe36ec8 100644 --- a/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/context/statement/SQLStatementContextFactoryTest.java +++ b/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/context/statement/SQLStatementContextFactoryTest.java @@ -56,7 +56,6 @@ import java.util.Collections; import java.util.LinkedList; import java.util.List; -import java.util.Map; import static org.hamcrest.CoreMatchers.instanceOf; import static org.hamcrest.MatcherAssert.assertThat; @@ -152,13 +151,13 @@ void assertNewInstanceForFetchStatement() { private ShardingSphereMetaData mockMetaData() { ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); + when(database.getName()).thenReturn(DefaultDatabase.LOGIC_NAME); when(database.containsSchema(DefaultDatabase.LOGIC_NAME)).thenReturn(true); when(database.containsSchema("public")).thenReturn(true); when(database.getSchema(DefaultDatabase.LOGIC_NAME).containsTable("tbl")).thenReturn(true); when(database.getSchema("public").containsTable("tbl")).thenReturn(true); when(database.containsSchema("dbo")).thenReturn(true); when(database.getSchema("dbo").containsTable("tbl")).thenReturn(true); - Map databases = Collections.singletonMap(DefaultDatabase.LOGIC_NAME, database); - return new ShardingSphereMetaData(databases, mock(ResourceMetaData.class), mock(RuleMetaData.class), mock(ConfigurationProperties.class)); + return new ShardingSphereMetaData(Collections.singleton(database), mock(ResourceMetaData.class), mock(RuleMetaData.class), mock(ConfigurationProperties.class)); } } diff --git a/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/context/statement/dml/InsertStatementContextTest.java b/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/context/statement/dml/InsertStatementContextTest.java index 7262b8408d182..304d31e436706 100644 --- a/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/context/statement/dml/InsertStatementContextTest.java +++ b/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/context/statement/dml/InsertStatementContextTest.java @@ -110,16 +110,13 @@ private void assertInsertStatementContextWithColumnNames(final InsertStatement i private InsertStatementContext createInsertStatementContext(final List params, final InsertStatement insertStatement) { ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); + when(database.getName()).thenReturn(DefaultDatabase.LOGIC_NAME); ShardingSphereSchema schema = mock(ShardingSphereSchema.class); String defaultSchemaName = insertStatement instanceof PostgreSQLStatement || insertStatement instanceof OpenGaussStatement ? "public" : DefaultDatabase.LOGIC_NAME; when(database.getSchema(defaultSchemaName)).thenReturn(schema); when(schema.getVisibleColumnNames("tbl")).thenReturn(Arrays.asList("id", "name", "status")); - return new InsertStatementContext(createShardingSphereMetaData(database), params, insertStatement, DefaultDatabase.LOGIC_NAME); - } - - private ShardingSphereMetaData createShardingSphereMetaData(final ShardingSphereDatabase database) { - return new ShardingSphereMetaData(Collections.singletonMap(DefaultDatabase.LOGIC_NAME, database), mock(ResourceMetaData.class), - mock(RuleMetaData.class), mock(ConfigurationProperties.class)); + return new InsertStatementContext(new ShardingSphereMetaData(Collections.singleton(database), mock(ResourceMetaData.class), mock(RuleMetaData.class), mock(ConfigurationProperties.class)), + params, insertStatement, DefaultDatabase.LOGIC_NAME); } @Test diff --git a/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/context/statement/dml/SelectStatementContextTest.java b/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/context/statement/dml/SelectStatementContextTest.java index 44717f90c38f5..4c714826701ad 100644 --- a/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/context/statement/dml/SelectStatementContextTest.java +++ b/infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/context/statement/dml/SelectStatementContextTest.java @@ -121,6 +121,7 @@ private void assertSetIndexForItemsByIndexOrderBy(final SelectStatement selectSt private ShardingSphereDatabase mockDatabase() { ShardingSphereDatabase result = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); + when(result.getName()).thenReturn(DefaultDatabase.LOGIC_NAME); TableMapperRuleAttribute ruleAttribute = mock(TableMapperRuleAttribute.class, RETURNS_DEEP_STUBS); when(ruleAttribute.getEnhancedTableNames().contains("t_order")).thenReturn(true); when(result.getRuleMetaData().getAttributes(TableMapperRuleAttribute.class)).thenReturn(Collections.singleton(ruleAttribute)); @@ -269,8 +270,8 @@ private void assertIsSameGroupByAndOrderByItems(final SelectStatement selectStat } private SelectStatementContext createSelectStatementContext(final SelectStatement selectStatement) { - ShardingSphereMetaData metaData = new ShardingSphereMetaData(Collections.singletonMap(DefaultDatabase.LOGIC_NAME, mockDatabase()), mock(ResourceMetaData.class), - mock(RuleMetaData.class), mock(ConfigurationProperties.class)); + ShardingSphereMetaData metaData = new ShardingSphereMetaData( + Collections.singleton(mockDatabase()), mock(ResourceMetaData.class), mock(RuleMetaData.class), mock(ConfigurationProperties.class)); return new SelectStatementContext(metaData, Collections.emptyList(), selectStatement, DefaultDatabase.LOGIC_NAME, Collections.emptyList()); } @@ -499,8 +500,8 @@ private void assertContainsSubqueryWhereEmpty(final SelectStatement selectStatem ProjectionsSegment projectionsSegment = new ProjectionsSegment(0, 0); projectionsSegment.getProjections().add(projectionSegment); selectStatement.setProjections(projectionsSegment); - ShardingSphereMetaData metaData = new ShardingSphereMetaData(Collections.singletonMap(DefaultDatabase.LOGIC_NAME, mockDatabase()), mock(ResourceMetaData.class), - mock(RuleMetaData.class), mock(ConfigurationProperties.class)); + ShardingSphereMetaData metaData = new ShardingSphereMetaData( + Collections.singleton(mockDatabase()), mock(ResourceMetaData.class), mock(RuleMetaData.class), mock(ConfigurationProperties.class)); assertTrue(new SelectStatementContext(metaData, Collections.emptyList(), selectStatement, DefaultDatabase.LOGIC_NAME, Collections.emptyList()).isContainsSubquery()); } @@ -533,8 +534,8 @@ private void assertContainsDollarParameterMarker(final SelectStatement selectSta ProjectionsSegment projectionsSegment = new ProjectionsSegment(0, 0); projectionsSegment.getProjections().add(new ParameterMarkerExpressionSegment(0, 0, 0, ParameterMarkerType.DOLLAR)); selectStatement.setProjections(projectionsSegment); - ShardingSphereMetaData metaData = new ShardingSphereMetaData(Collections.singletonMap(DefaultDatabase.LOGIC_NAME, mockDatabase()), mock(ResourceMetaData.class), - mock(RuleMetaData.class), mock(ConfigurationProperties.class)); + ShardingSphereMetaData metaData = new ShardingSphereMetaData( + Collections.singleton(mockDatabase()), mock(ResourceMetaData.class), mock(RuleMetaData.class), mock(ConfigurationProperties.class)); SelectStatementContext selectStatementContext = new SelectStatementContext(metaData, Collections.emptyList(), selectStatement, DefaultDatabase.LOGIC_NAME, Collections.emptyList()); assertTrue(selectStatementContext.isContainsDollarParameterMarker()); selectStatement.setProjections(new ProjectionsSegment(0, 0)); @@ -575,15 +576,14 @@ private void assertContainsPartialDistinctAggregation(final SelectStatement sele projectionsSegment.getProjections().add(new AggregationProjectionSegment(0, 0, AggregationType.COUNT, "COUNT(*)")); projectionsSegment.getProjections().add(new AggregationDistinctProjectionSegment(0, 10, AggregationType.COUNT, "COUNT(1)", "distinctExpression")); selectStatement.setProjections(projectionsSegment); - ShardingSphereMetaData metaData = new ShardingSphereMetaData(Collections.singletonMap(DefaultDatabase.LOGIC_NAME, mockDatabase()), mock(ResourceMetaData.class), - mock(RuleMetaData.class), mock(ConfigurationProperties.class)); + ShardingSphereMetaData metaData = new ShardingSphereMetaData( + Collections.singleton(mockDatabase()), mock(ResourceMetaData.class), mock(RuleMetaData.class), mock(ConfigurationProperties.class)); SelectStatementContext selectStatementContext = new SelectStatementContext(metaData, Collections.emptyList(), selectStatement, DefaultDatabase.LOGIC_NAME, Collections.emptyList()); assertTrue(selectStatementContext.isContainsPartialDistinctAggregation()); } private ShardingSphereMetaData createShardingSphereMetaData(final ShardingSphereDatabase database) { - return new ShardingSphereMetaData(Collections.singletonMap(DefaultDatabase.LOGIC_NAME, database), mock(ResourceMetaData.class), - mock(RuleMetaData.class), mock(ConfigurationProperties.class)); + return new ShardingSphereMetaData(Collections.singleton(database), mock(ResourceMetaData.class), mock(RuleMetaData.class), mock(ConfigurationProperties.class)); } private OrderByItemSegment createOrderByItemSegment(final String type) { @@ -629,8 +629,8 @@ void assertIsContainsEnhancedTable() { SelectStatement selectStatement = new MySQLSelectStatement(); selectStatement.setProjections(projectionsSegment); selectStatement.setFrom(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("t_order")))); - ShardingSphereMetaData metaData = new ShardingSphereMetaData(Collections.singletonMap(DefaultDatabase.LOGIC_NAME, mockDatabase()), mock(ResourceMetaData.class), - mock(RuleMetaData.class), mock(ConfigurationProperties.class)); + ShardingSphereMetaData metaData = new ShardingSphereMetaData( + Collections.singleton(mockDatabase()), mock(ResourceMetaData.class), mock(RuleMetaData.class), mock(ConfigurationProperties.class)); SelectStatementContext actual = new SelectStatementContext(metaData, Collections.emptyList(), selectStatement, DefaultDatabase.LOGIC_NAME, Collections.emptyList()); assertTrue(actual.isContainsEnhancedTable()); } @@ -640,8 +640,8 @@ void assertContainsEnhancedTable() { SelectStatement selectStatement = new MySQLSelectStatement(); selectStatement.setProjections(new ProjectionsSegment(0, 0)); selectStatement.setFrom(new SubqueryTableSegment(0, 0, new SubquerySegment(0, 0, createSubSelectStatement(), ""))); - ShardingSphereMetaData metaData = new ShardingSphereMetaData(Collections.singletonMap(DefaultDatabase.LOGIC_NAME, mockDatabase()), mock(ResourceMetaData.class), - mock(RuleMetaData.class), mock(ConfigurationProperties.class)); + ShardingSphereMetaData metaData = new ShardingSphereMetaData( + Collections.singleton(mockDatabase()), mock(ResourceMetaData.class), mock(RuleMetaData.class), mock(ConfigurationProperties.class)); SelectStatementContext actual = new SelectStatementContext(metaData, Collections.emptyList(), selectStatement, DefaultDatabase.LOGIC_NAME, Collections.emptyList()); assertTrue(actual.containsTableSubquery()); } 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 6a64055d2e288..a562b79880719 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 @@ -38,6 +38,7 @@ import java.util.Optional; import java.util.Properties; import java.util.concurrent.ConcurrentHashMap; +import java.util.stream.Collectors; /** * ShardingSphere meta data. @@ -56,8 +57,7 @@ public final class ShardingSphereMetaData { private final TemporaryConfigurationProperties temporaryProps; public ShardingSphereMetaData() { - this(new CaseInsensitiveMap<>(Collections.emptyMap(), new ConcurrentHashMap<>()), - new ResourceMetaData(Collections.emptyMap()), new RuleMetaData(Collections.emptyList()), new ConfigurationProperties(new Properties())); + this(Collections.emptyList(), new ResourceMetaData(Collections.emptyMap()), new RuleMetaData(Collections.emptyList()), new ConfigurationProperties(new Properties())); } public ShardingSphereMetaData(final Map databases, final ResourceMetaData globalResourceMetaData, @@ -69,6 +69,15 @@ public ShardingSphereMetaData(final Map database temporaryProps = new TemporaryConfigurationProperties(props.getProps()); } + public ShardingSphereMetaData(final Collection databases, final ResourceMetaData globalResourceMetaData, + final RuleMetaData globalRuleMetaData, final ConfigurationProperties props) { + this.databases = new CaseInsensitiveMap<>(databases.stream().collect(Collectors.toMap(ShardingSphereDatabase::getName, each -> each)), new ConcurrentHashMap<>()); + this.globalResourceMetaData = globalResourceMetaData; + this.globalRuleMetaData = globalRuleMetaData; + this.props = props; + temporaryProps = new TemporaryConfigurationProperties(props.getProps()); + } + /** * Get all databases. * 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 21d57c114c689..7e3e0f7d365f9 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 @@ -41,9 +41,9 @@ import javax.sql.DataSource; import java.util.Arrays; +import java.util.Collection; import java.util.Collections; -import java.util.HashMap; -import java.util.Map; +import java.util.LinkedList; import java.util.Properties; import java.util.concurrent.TimeUnit; @@ -69,7 +69,7 @@ void assertAddDatabase() { DatabaseType databaseType = mock(DatabaseType.class); ConfigurationProperties configProps = new ConfigurationProperties(new Properties()); when(ShardingSphereDatabase.create("foo_db", databaseType, configProps)).thenReturn(database); - Map databases = new HashMap<>(Collections.singletonMap("foo_db", database)); + Collection databases = new LinkedList<>(Collections.singleton(database)); ShardingSphereMetaData metaData = new ShardingSphereMetaData(databases, mock(ResourceMetaData.class), new RuleMetaData(Collections.singleton(globalRule)), configProps); metaData.addDatabase("foo_db", databaseType, configProps); assertThat(metaData.getDatabase("foo_db"), is(database)); @@ -84,7 +84,7 @@ void assertDropDatabase() { when(databaseRule1.getAttributes()).thenReturn(new RuleAttributes()); ShardingSphereRule databaseRule2 = mock(ShardingSphereRule.class, withSettings().extraInterfaces(AutoCloseable.class)); when(databaseRule2.getAttributes()).thenReturn(new RuleAttributes()); - ShardingSphereMetaData metaData = new ShardingSphereMetaData(new HashMap<>(Collections.singletonMap("foo_db", mockDatabase(resourceMetaData, dataSource, databaseRule1, databaseRule2))), + ShardingSphereMetaData metaData = new ShardingSphereMetaData(new LinkedList<>(Collections.singleton(mockDatabase(resourceMetaData, dataSource, databaseRule1, databaseRule2))), mock(ResourceMetaData.class), new RuleMetaData(Collections.singleton(globalRule)), new ConfigurationProperties(new Properties())); metaData.dropDatabase("foo_db"); assertTrue(metaData.getDatabases().isEmpty()); @@ -97,7 +97,7 @@ void assertDropDatabase() { void assertContainsDatabase() { ShardingSphereRule globalRule = mock(ShardingSphereRule.class); ShardingSphereDatabase database = mockDatabase(mock(ResourceMetaData.class, RETURNS_DEEP_STUBS), new MockedDataSource(), globalRule); - Map databases = new HashMap<>(Collections.singletonMap("foo_db", database)); + Collection databases = new LinkedList<>(Collections.singleton(database)); ConfigurationProperties configProps = new ConfigurationProperties(new Properties()); ShardingSphereMetaData metaData = new ShardingSphereMetaData(databases, mock(ResourceMetaData.class), new RuleMetaData(Collections.singleton(globalRule)), configProps); assertTrue(metaData.containsDatabase("foo_db")); @@ -113,7 +113,7 @@ void assertNotContainsDatabase() { void assertGetDatabase() { ShardingSphereRule globalRule = mock(ShardingSphereRule.class); ShardingSphereDatabase database = mockDatabase(mock(ResourceMetaData.class, RETURNS_DEEP_STUBS), new MockedDataSource(), globalRule); - Map databases = new HashMap<>(Collections.singletonMap("foo_db", database)); + Collection databases = new LinkedList<>(Collections.singleton(database)); ConfigurationProperties configProps = new ConfigurationProperties(new Properties()); ShardingSphereMetaData metaData = new ShardingSphereMetaData(databases, mock(ResourceMetaData.class), new RuleMetaData(Collections.singleton(globalRule)), configProps); assertThat(metaData.getDatabase("foo_db"), is(database)); diff --git a/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/MetaDataContextManager.java b/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/MetaDataContextManager.java index 0d76a5bec88cf..65a9639b6d029 100644 --- a/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/MetaDataContextManager.java +++ b/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/MetaDataContextManager.java @@ -156,7 +156,7 @@ private MetaDataContexts createMetaDataContexts(final ShardingSphereDatabase dat RuleMetaData changedGlobalMetaData = new RuleMetaData( GlobalRulesBuilder.buildRules(ruleConfigPersistDecorateEngine.restore(metaDataPersistService.getGlobalRuleService().load()), changedDatabases.values(), props)); MetaDataContexts result = MetaDataContextsFactory.create(metaDataPersistService, - new ShardingSphereMetaData(changedDatabases, metaDataContexts.get().getMetaData().getGlobalResourceMetaData(), changedGlobalMetaData, props)); + new ShardingSphereMetaData(changedDatabases.values(), metaDataContexts.get().getMetaData().getGlobalResourceMetaData(), changedGlobalMetaData, props)); switchingResource.closeStaleDataSources(); return result; } 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 5a24febf0289a..f9638a63f7c2f 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 @@ -136,7 +136,7 @@ private static MetaDataContexts newMetaDataContexts(final MetaDataPersistService // TODO load global data sources from persist service ResourceMetaData globalResourceMetaData = new ResourceMetaData(param.getGlobalDataSources()); RuleMetaData globalRuleMetaData = new RuleMetaData(GlobalRulesBuilder.buildRules(globalRuleConfigs, databases.values(), props)); - ShardingSphereMetaData shardingSphereMetaData = new ShardingSphereMetaData(databases, globalResourceMetaData, globalRuleMetaData, props); + ShardingSphereMetaData shardingSphereMetaData = new ShardingSphereMetaData(databases.values(), globalResourceMetaData, globalRuleMetaData, props); ShardingSphereStatistics shardingSphereStatistics = initStatistics(persistService, shardingSphereMetaData); return new MetaDataContexts(shardingSphereMetaData, shardingSphereStatistics); } @@ -272,7 +272,7 @@ public static MetaDataContexts createBySwitchResource(final String databaseName, ConfigurationProperties props = originalMetaDataContexts.getMetaData().getProps(); RuleMetaData changedGlobalMetaData = new RuleMetaData( GlobalRulesBuilder.buildRules(originalMetaDataContexts.getMetaData().getGlobalRuleMetaData().getConfigurations(), changedDatabases.values(), props)); - return create(metaDataPersistService, new ShardingSphereMetaData(changedDatabases, originalMetaDataContexts.getMetaData().getGlobalResourceMetaData(), changedGlobalMetaData, props)); + return create(metaDataPersistService, new ShardingSphereMetaData(changedDatabases.values(), originalMetaDataContexts.getMetaData().getGlobalResourceMetaData(), changedGlobalMetaData, props)); } /** @@ -295,7 +295,7 @@ public static MetaDataContexts createByAlterRule(final String databaseName, fina ConfigurationProperties props = originalMetaDataContexts.getMetaData().getProps(); RuleMetaData changedGlobalMetaData = new RuleMetaData( GlobalRulesBuilder.buildRules(originalMetaDataContexts.getMetaData().getGlobalRuleMetaData().getConfigurations(), changedDatabases.values(), props)); - return create(metaDataPersistService, new ShardingSphereMetaData(changedDatabases, originalMetaDataContexts.getMetaData().getGlobalResourceMetaData(), changedGlobalMetaData, props)); + return create(metaDataPersistService, new ShardingSphereMetaData(changedDatabases.values(), originalMetaDataContexts.getMetaData().getGlobalResourceMetaData(), changedGlobalMetaData, props)); } /** diff --git a/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/manager/GlobalConfigurationManager.java b/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/manager/GlobalConfigurationManager.java index ce25753ae7c44..e60405879b76e 100644 --- a/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/manager/GlobalConfigurationManager.java +++ b/mode/core/src/main/java/org/apache/shardingsphere/mode/metadata/manager/GlobalConfigurationManager.java @@ -69,8 +69,8 @@ public synchronized void alterGlobalRuleConfiguration(final RuleConfiguration ru rules.addAll(GlobalRulesBuilder.buildSingleRules(ruleConfig, metaDataContexts.get().getMetaData().getDatabases(), metaDataContexts.get().getMetaData().getProps())); metaDataContexts.get().getMetaData().getGlobalRuleMetaData().getRules().clear(); metaDataContexts.get().getMetaData().getGlobalRuleMetaData().getRules().addAll(rules); - ShardingSphereMetaData toBeChangedMetaData = new ShardingSphereMetaData(metaDataContexts.get().getMetaData().getDatabases(), metaDataContexts.get().getMetaData().getGlobalResourceMetaData(), - metaDataContexts.get().getMetaData().getGlobalRuleMetaData(), metaDataContexts.get().getMetaData().getProps()); + ShardingSphereMetaData toBeChangedMetaData = new ShardingSphereMetaData(metaDataContexts.get().getMetaData().getAllDatabases(), + metaDataContexts.get().getMetaData().getGlobalResourceMetaData(), metaDataContexts.get().getMetaData().getGlobalRuleMetaData(), metaDataContexts.get().getMetaData().getProps()); metaDataContexts.set(newMetaDataContexts(toBeChangedMetaData)); } @@ -90,8 +90,8 @@ private void closeStaleTransactionRule(final RuleConfiguration ruleConfig) { * @param props properties to be altered */ public synchronized void alterProperties(final Properties props) { - ShardingSphereMetaData toBeChangedMetaData = new ShardingSphereMetaData(metaDataContexts.get().getMetaData().getDatabases(), metaDataContexts.get().getMetaData().getGlobalResourceMetaData(), - metaDataContexts.get().getMetaData().getGlobalRuleMetaData(), new ConfigurationProperties(props)); + ShardingSphereMetaData toBeChangedMetaData = new ShardingSphereMetaData(metaDataContexts.get().getMetaData().getAllDatabases(), + metaDataContexts.get().getMetaData().getGlobalResourceMetaData(), metaDataContexts.get().getMetaData().getGlobalRuleMetaData(), new ConfigurationProperties(props)); metaDataContexts.set(newMetaDataContexts(toBeChangedMetaData)); } diff --git a/mode/core/src/test/java/org/apache/shardingsphere/mode/metadata/MetaDataContextsTest.java b/mode/core/src/test/java/org/apache/shardingsphere/mode/metadata/MetaDataContextsTest.java index 16d0c880bfef2..a0d3e91a97f19 100644 --- a/mode/core/src/test/java/org/apache/shardingsphere/mode/metadata/MetaDataContextsTest.java +++ b/mode/core/src/test/java/org/apache/shardingsphere/mode/metadata/MetaDataContextsTest.java @@ -29,7 +29,6 @@ import org.junit.jupiter.api.Test; import java.util.Collections; -import java.util.Map; import java.util.Properties; import static org.hamcrest.CoreMatchers.is; @@ -42,10 +41,10 @@ class MetaDataContextsTest { @Test void assertGetDefaultMetaData() { ShardingSphereDatabase database = mock(ShardingSphereDatabase.class); + when(database.getName()).thenReturn(DefaultDatabase.LOGIC_NAME); when(database.getProtocolType()).thenReturn(TypedSPILoader.getService(DatabaseType.class, "FIXTURE")); - Map databases = Collections.singletonMap(DefaultDatabase.LOGIC_NAME, database); - ShardingSphereMetaData metaData = new ShardingSphereMetaData(databases, mock(ResourceMetaData.class), - mock(RuleMetaData.class), new ConfigurationProperties(new Properties())); + ShardingSphereMetaData metaData = new ShardingSphereMetaData( + Collections.singleton(database), mock(ResourceMetaData.class), mock(RuleMetaData.class), new ConfigurationProperties(new Properties())); assertThat(MetaDataContextsFactory.create(mock(MetaDataPersistService.class), metaData).getMetaData().getDatabase(DefaultDatabase.LOGIC_NAME), is(database)); } } diff --git a/mode/type/standalone/core/src/test/java/org/apache/shardingsphere/mode/manager/standalone/persist/StandaloneMetaDataManagerPersistServiceTest.java b/mode/type/standalone/core/src/test/java/org/apache/shardingsphere/mode/manager/standalone/persist/StandaloneMetaDataManagerPersistServiceTest.java index 4952148407a37..8f9a33388e6e5 100644 --- a/mode/type/standalone/core/src/test/java/org/apache/shardingsphere/mode/manager/standalone/persist/StandaloneMetaDataManagerPersistServiceTest.java +++ b/mode/type/standalone/core/src/test/java/org/apache/shardingsphere/mode/manager/standalone/persist/StandaloneMetaDataManagerPersistServiceTest.java @@ -23,8 +23,6 @@ import org.apache.shardingsphere.infra.database.core.metadata.database.enums.TableType; import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData; import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; -import org.apache.shardingsphere.infra.metadata.database.resource.ResourceMetaData; -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.database.schema.model.ShardingSphereTable; import org.apache.shardingsphere.infra.metadata.database.schema.pojo.AlterSchemaMetaDataPOJO; @@ -104,8 +102,8 @@ void assertCreateSchema() { @Test void assertAlterSchemaWithEmptyAlteredSchema() { ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); - ShardingSphereMetaData metaData = new ShardingSphereMetaData( - Collections.singletonMap("foo_db", database), mock(ResourceMetaData.class), mock(RuleMetaData.class), new ConfigurationProperties(new Properties())); + when(database.getName()).thenReturn("foo_db"); + ShardingSphereMetaData metaData = new ShardingSphereMetaData(Collections.singleton(database), mock(), mock(), new ConfigurationProperties(new Properties())); when(metaDataContextManager.getMetaDataContexts().get().getMetaData()).thenReturn(metaData); DatabaseMetaDataPersistFacade databaseMetaDataFacade = mock(DatabaseMetaDataPersistFacade.class, RETURNS_DEEP_STUBS); when(metaDataPersistService.getDatabaseMetaDataFacade()).thenReturn(databaseMetaDataFacade); @@ -119,12 +117,12 @@ void assertAlterSchemaWithEmptyAlteredSchema() { @Test void assertAlterSchemaWithNotEmptyAlteredSchema() { ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); + when(database.getName()).thenReturn("foo_db"); ShardingSphereSchema schema = mock(ShardingSphereSchema.class); when(schema.isEmpty()).thenReturn(true); when(database.getSchema("foo_schema")).thenReturn(schema); when(database.getSchema("bar_schema")).thenReturn(schema); - ShardingSphereMetaData metaData = new ShardingSphereMetaData( - Collections.singletonMap("foo_db", database), mock(ResourceMetaData.class), mock(RuleMetaData.class), new ConfigurationProperties(new Properties())); + ShardingSphereMetaData metaData = new ShardingSphereMetaData(Collections.singleton(database), mock(), mock(), new ConfigurationProperties(new Properties())); when(metaDataContextManager.getMetaDataContexts().get().getMetaData()).thenReturn(metaData); DatabaseMetaDataPersistFacade databaseMetaDataFacade = mock(DatabaseMetaDataPersistFacade.class, RETURNS_DEEP_STUBS); when(metaDataPersistService.getDatabaseMetaDataFacade()).thenReturn(databaseMetaDataFacade); @@ -138,8 +136,8 @@ void assertAlterSchemaWithNotEmptyAlteredSchema() { @Test void assertDropSchema() { ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); - ShardingSphereMetaData metaData = new ShardingSphereMetaData( - Collections.singletonMap("foo_db", database), mock(ResourceMetaData.class), mock(RuleMetaData.class), new ConfigurationProperties(new Properties())); + when(database.getName()).thenReturn("foo_db"); + ShardingSphereMetaData metaData = new ShardingSphereMetaData(Collections.singleton(database), mock(), mock(), new ConfigurationProperties(new Properties())); when(metaDataContextManager.getMetaDataContexts().get().getMetaData()).thenReturn(metaData); metaDataManagerPersistService.dropSchema("foo_db", Collections.singleton("foo_schema")); verify(database).dropSchema(any()); @@ -171,8 +169,7 @@ void assertAlterNullRuleConfiguration() throws SQLException { void assertAlterRuleConfiguration() throws SQLException { ShardingSphereDatabase database = mock(ShardingSphereDatabase.class); when(database.getName()).thenReturn("foo_db"); - ShardingSphereMetaData metaData = new ShardingSphereMetaData( - Collections.singletonMap("foo_db", database), mock(ResourceMetaData.class), mock(RuleMetaData.class), new ConfigurationProperties(new Properties())); + ShardingSphereMetaData metaData = new ShardingSphereMetaData(Collections.singleton(database), mock(), mock(), new ConfigurationProperties(new Properties())); when(metaDataContextManager.getMetaDataContexts().get().getMetaData()).thenReturn(metaData); RuleConfiguration ruleConfig = mock(RuleConfiguration.class, RETURNS_DEEP_STUBS); Collection metaDataVersion = Collections.singleton(mock(MetaDataVersion.class)); diff --git a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/connector/DatabaseConnectorFactoryTest.java b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/connector/DatabaseConnectorFactoryTest.java index 42b253c4183ad..78ee7e4379b29 100644 --- a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/connector/DatabaseConnectorFactoryTest.java +++ b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/connector/DatabaseConnectorFactoryTest.java @@ -97,8 +97,7 @@ void assertNewDatabaseConnectorWithParameters() { private ContextManager mockContextManager(final ShardingSphereDatabase database) { MetaDataContexts metaDataContexts = MetaDataContextsFactory.create(mock(MetaDataPersistService.class), - new ShardingSphereMetaData(Collections.singletonMap("foo_db", database), mock(ResourceMetaData.class), - mock(RuleMetaData.class), new ConfigurationProperties(new Properties()))); + new ShardingSphereMetaData(Collections.singleton(database), mock(ResourceMetaData.class), mock(RuleMetaData.class), new ConfigurationProperties(new Properties()))); ContextManager result = mock(ContextManager.class, RETURNS_DEEP_STUBS); when(result.getMetaDataContexts()).thenReturn(metaDataContexts); when(result.getDatabase("foo_db")).thenReturn(database); @@ -107,6 +106,7 @@ private ContextManager mockContextManager(final ShardingSphereDatabase database) private ShardingSphereDatabase mockDatabase() { ShardingSphereDatabase result = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); + when(result.getName()).thenReturn("foo_db"); when(result.containsDataSource()).thenReturn(true); when(result.isComplete()).thenReturn(true); when(result.getProtocolType()).thenReturn(databaseType); diff --git a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/connector/ProxySQLExecutorTest.java b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/connector/ProxySQLExecutorTest.java index 9d98488a8d834..f1c8aa6d6b439 100644 --- a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/connector/ProxySQLExecutorTest.java +++ b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/connector/ProxySQLExecutorTest.java @@ -264,14 +264,14 @@ private CursorStatementContext createCursorStatementContext() { } private ShardingSphereMetaData createShardingSphereMetaData(final ShardingSphereDatabase database) { - return new ShardingSphereMetaData(Collections.singletonMap(DefaultDatabase.LOGIC_NAME, database), mock(ResourceMetaData.class), - mock(RuleMetaData.class), mock(ConfigurationProperties.class)); + return new ShardingSphereMetaData(Collections.singleton(database), mock(ResourceMetaData.class), mock(RuleMetaData.class), mock(ConfigurationProperties.class)); } private InsertStatementContext createMySQLInsertStatementContext() { MySQLInsertStatement sqlStatement = new MySQLInsertStatement(); sqlStatement.setTable(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("t_order")))); ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); + when(database.getName()).thenReturn(DefaultDatabase.LOGIC_NAME); when(database.getSchema(DefaultDatabase.LOGIC_NAME)).thenReturn(mock(ShardingSphereSchema.class)); return new InsertStatementContext(createShardingSphereMetaData(database), Collections.emptyList(), sqlStatement, DefaultDatabase.LOGIC_NAME); } @@ -286,6 +286,7 @@ private InsertStatementContext createPostgreSQLInsertStatementContext() { PostgreSQLInsertStatement sqlStatement = new PostgreSQLInsertStatement(); sqlStatement.setTable(new SimpleTableSegment(new TableNameSegment(0, 0, new IdentifierValue("t_order")))); ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); + when(database.getName()).thenReturn(DefaultDatabase.LOGIC_NAME); when(database.getSchema("public")).thenReturn(mock(ShardingSphereSchema.class)); return new InsertStatementContext(createShardingSphereMetaData(database), Collections.emptyList(), sqlStatement, DefaultDatabase.LOGIC_NAME); } diff --git a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/connector/StandardDatabaseConnectorTest.java b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/connector/StandardDatabaseConnectorTest.java index 575f66be7703e..7aa565ed5cbc9 100644 --- a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/connector/StandardDatabaseConnectorTest.java +++ b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/connector/StandardDatabaseConnectorTest.java @@ -77,7 +77,6 @@ import java.util.Collection; import java.util.Collections; import java.util.List; -import java.util.Map; import java.util.Optional; import java.util.Properties; @@ -125,21 +124,22 @@ private ContextManager mockContextManager() { ShardingSphereDataPersistService shardingSphereDataPersistService = mock(ShardingSphereDataPersistService.class); when(shardingSphereDataPersistService.load(any())).thenReturn(Optional.empty()); when(metaDataPersistService.getShardingSphereDataPersistService()).thenReturn(shardingSphereDataPersistService); - Map databases = mockDatabases(); + ShardingSphereDatabase database = mockDatabase(); MetaDataContexts metaDataContexts = MetaDataContextsFactory.create( - metaDataPersistService, new ShardingSphereMetaData(databases, mock(ResourceMetaData.class), globalRuleMetaData, new ConfigurationProperties(new Properties()))); + metaDataPersistService, new ShardingSphereMetaData(Collections.singleton(database), mock(ResourceMetaData.class), globalRuleMetaData, new ConfigurationProperties(new Properties()))); ContextManager result = mock(ContextManager.class, RETURNS_DEEP_STUBS); when(result.getMetaDataContexts()).thenReturn(metaDataContexts); - when(result.getDatabase("foo_db")).thenReturn(databases.get("foo_db")); + when(result.getDatabase("foo_db")).thenReturn(database); return result; } - private Map mockDatabases() { - ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); - when(database.containsDataSource()).thenReturn(true); - when(database.isComplete()).thenReturn(true); - when(database.getProtocolType()).thenReturn(TypedSPILoader.getService(DatabaseType.class, "H2")); - return Collections.singletonMap("foo_db", database); + private ShardingSphereDatabase mockDatabase() { + ShardingSphereDatabase result = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); + when(result.getName()).thenReturn("foo_db"); + when(result.containsDataSource()).thenReturn(true); + when(result.isComplete()).thenReturn(true); + when(result.getProtocolType()).thenReturn(TypedSPILoader.getService(DatabaseType.class, "H2")); + return result; } @Test @@ -184,7 +184,7 @@ private QueryContext createQueryContext(final SQLStatementContext sqlStatementCo when(connectionContext.getCurrentDatabaseName()).thenReturn(Optional.of(DefaultDatabase.LOGIC_NAME)); ShardingSphereMetaData metaData = mock(ShardingSphereMetaData.class); when(metaData.containsDatabase(DefaultDatabase.LOGIC_NAME)).thenReturn(true); - ShardingSphereDatabase database = mockDatabases().get("foo_db"); + ShardingSphereDatabase database = mockDatabase(); when(metaData.getDatabase(DefaultDatabase.LOGIC_NAME)).thenReturn(database); return new QueryContext(sqlStatementContext, "schemaName", Collections.emptyList(), new HintValueContext(), connectionContext, metaData); } diff --git a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/connector/jdbc/datasource/JDBCBackendDataSourceTest.java b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/connector/jdbc/datasource/JDBCBackendDataSourceTest.java index f2398095694aa..77e6d44396775 100644 --- a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/connector/jdbc/datasource/JDBCBackendDataSourceTest.java +++ b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/connector/jdbc/datasource/JDBCBackendDataSourceTest.java @@ -52,7 +52,6 @@ import java.util.LinkedList; import java.util.List; import java.util.Map; -import java.util.Map.Entry; import java.util.Properties; import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; @@ -85,20 +84,18 @@ void setUp() { private ContextManager mockContextManager() { ContextManager result = mock(ContextManager.class, RETURNS_DEEP_STUBS); - MetaDataContexts metaDataContexts = MetaDataContextsFactory.create(mock(MetaDataPersistService.class), new ShardingSphereMetaData( - createDatabases(), mock(ResourceMetaData.class), - new RuleMetaData(Collections.singleton(mock(TransactionRule.class, RETURNS_DEEP_STUBS))), new ConfigurationProperties(new Properties()))); + MetaDataContexts metaDataContexts = MetaDataContextsFactory.create(mock(MetaDataPersistService.class), new ShardingSphereMetaData(Collections.singleton(mockDatabase()), + mock(ResourceMetaData.class), new RuleMetaData(Collections.singleton(mock(TransactionRule.class, RETURNS_DEEP_STUBS))), new ConfigurationProperties(new Properties()))); when(result.getMetaDataContexts()).thenReturn(metaDataContexts); return result; } - private Map createDatabases() { - ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); - when(database.getProtocolType()).thenReturn(TypedSPILoader.getService(DatabaseType.class, "FIXTURE")); - for (Entry entry : mockDataSources(2).entrySet()) { - when(database.getResourceMetaData().getStorageUnits().get(entry.getKey()).getDataSource()).thenReturn(entry.getValue()); - } - return Collections.singletonMap("schema", database); + private ShardingSphereDatabase mockDatabase() { + ShardingSphereDatabase result = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); + when(result.getName()).thenReturn("schema"); + when(result.getProtocolType()).thenReturn(TypedSPILoader.getService(DatabaseType.class, "FIXTURE")); + mockDataSources(2).forEach((key, value) -> when(result.getResourceMetaData().getStorageUnits().get(key).getDataSource()).thenReturn(value)); + return result; } private Map mockDataSources(final int size) { diff --git a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/context/ProxyContextTest.java b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/context/ProxyContextTest.java index 12a60013ef1fd..8828252b96248 100644 --- a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/context/ProxyContextTest.java +++ b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/context/ProxyContextTest.java @@ -35,11 +35,12 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import java.util.Collection; import java.util.Collections; -import java.util.LinkedHashMap; -import java.util.LinkedHashSet; -import java.util.Map; +import java.util.HashSet; +import java.util.LinkedList; import java.util.Properties; +import java.util.stream.Collectors; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; @@ -80,10 +81,9 @@ void assertInit() { @Test void assertDatabaseExists() { - Map databases = mockDatabases(); ContextManager contextManager = mock(ContextManager.class, RETURNS_DEEP_STUBS); MetaDataContexts metaDataContexts = MetaDataContextsFactory.create(mock(MetaDataPersistService.class), - new ShardingSphereMetaData(databases, mock(ResourceMetaData.class), mock(RuleMetaData.class), new ConfigurationProperties(new Properties()))); + new ShardingSphereMetaData(Collections.singleton(mockDatabase()), mock(ResourceMetaData.class), mock(RuleMetaData.class), new ConfigurationProperties(new Properties()))); when(contextManager.getMetaDataContexts()).thenReturn(metaDataContexts); ProxyContext.init(contextManager); assertTrue(ProxyContext.getInstance().databaseExists("db")); @@ -92,30 +92,31 @@ void assertDatabaseExists() { @Test void assertGetAllDatabaseNames() { - Map databases = createDatabases(); + Collection databases = createDatabases(); ContextManager contextManager = mock(ContextManager.class, RETURNS_DEEP_STUBS); MetaDataContexts metaDataContexts = MetaDataContextsFactory.create(mock(MetaDataPersistService.class), new ShardingSphereMetaData(databases, mock(ResourceMetaData.class), mock(RuleMetaData.class), new ConfigurationProperties(new Properties()))); when(contextManager.getMetaDataContexts()).thenReturn(metaDataContexts); ProxyContext.init(contextManager); - assertThat(new LinkedHashSet<>(ProxyContext.getInstance().getAllDatabaseNames()), is(databases.keySet())); + assertThat(new HashSet<>(ProxyContext.getInstance().getAllDatabaseNames()), is(databases.stream().map(ShardingSphereDatabase::getName).collect(Collectors.toSet()))); } - private Map createDatabases() { - Map result = new LinkedHashMap<>(10, 1F); + private Collection createDatabases() { + Collection result = new LinkedList<>(); for (int i = 0; i < 10; i++) { ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); String databaseName = String.format(SCHEMA_PATTERN, i); when(database.getName()).thenReturn(databaseName); when(database.getProtocolType()).thenReturn(databaseType); - result.put(databaseName, database); + result.add(database); } return result; } - private Map mockDatabases() { - ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); - when(database.getProtocolType()).thenReturn(databaseType); - return Collections.singletonMap("db", database); + private ShardingSphereDatabase mockDatabase() { + ShardingSphereDatabase result = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); + when(result.getName()).thenReturn("db"); + when(result.getProtocolType()).thenReturn(databaseType); + return result; } } diff --git a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/admin/DatabaseAdminQueryBackendHandlerTest.java b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/admin/DatabaseAdminQueryBackendHandlerTest.java index 2ee5bfeac6566..1a56346ec3625 100644 --- a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/admin/DatabaseAdminQueryBackendHandlerTest.java +++ b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/admin/DatabaseAdminQueryBackendHandlerTest.java @@ -112,10 +112,11 @@ void assertGetRowData() throws SQLException { private ContextManager mockContextManager() { ShardingSphereDatabase database = mock(ShardingSphereDatabase.class); + when(database.getName()).thenReturn("foo_db"); when(database.getProtocolType()).thenReturn(databaseType); when(ProxyContext.getInstance().getContextManager().getDatabase("foo_db")).thenReturn(database); - ShardingSphereMetaData metaData = new ShardingSphereMetaData(Collections.singletonMap("foo_db", database), mock(ResourceMetaData.class), - mock(RuleMetaData.class), new ConfigurationProperties(new Properties())); + ShardingSphereMetaData metaData = new ShardingSphereMetaData( + Collections.singleton(database), mock(ResourceMetaData.class), mock(RuleMetaData.class), new ConfigurationProperties(new Properties())); ComputeNodeInstanceContext computeNodeInstanceContext = mock(ComputeNodeInstanceContext.class); when(computeNodeInstanceContext.getModeConfiguration()).thenReturn(mock(ModeConfiguration.class)); return new ContextManager(MetaDataContextsFactory.create(mock(MetaDataPersistService.class), metaData), computeNodeInstanceContext, mock(PersistRepository.class)); 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 6123ee9b41bcf..f4f099ab0b614 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 @@ -150,7 +150,7 @@ private ContextManager mockContextManager(final ShardingSphereDatabase database) AuthorityRule authorityRule = mock(AuthorityRule.class); when(authorityRule.findPrivileges(grantee)).thenReturn(Optional.of(new DatabasePermittedPrivileges(Collections.singleton("auth_db")))); ContextManager result = mock(ContextManager.class, RETURNS_DEEP_STUBS); - MetaDataContexts metaDataContexts = MetaDataContextsFactory.create(mock(MetaDataPersistService.class), new ShardingSphereMetaData(Collections.singletonMap("auth_db", database), + MetaDataContexts metaDataContexts = MetaDataContextsFactory.create(mock(MetaDataPersistService.class), new ShardingSphereMetaData(Collections.singleton(database), mock(ResourceMetaData.class), new RuleMetaData(Collections.singleton(authorityRule)), new ConfigurationProperties(new Properties()))); when(result.getMetaDataContexts()).thenReturn(metaDataContexts); when(result.getDatabase("auth_db")).thenReturn(database); diff --git a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/data/impl/UnicastDatabaseBackendHandlerTest.java b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/data/impl/UnicastDatabaseBackendHandlerTest.java index 217d65d4fbe60..3039e829ecf27 100644 --- a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/data/impl/UnicastDatabaseBackendHandlerTest.java +++ b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/data/impl/UnicastDatabaseBackendHandlerTest.java @@ -54,9 +54,9 @@ import org.mockito.quality.Strictness; import java.sql.SQLException; +import java.util.Collection; import java.util.Collections; -import java.util.HashMap; -import java.util.Map; +import java.util.LinkedList; import java.util.Optional; import java.util.Properties; @@ -121,7 +121,7 @@ private void setBackendHandlerFactory(final DatabaseBackendHandler schemaDatabas void assertExecuteDatabaseBackendHandler() throws SQLException { ContextManager contextManager = mockContextManager(); when(ProxyContext.getInstance().getContextManager()).thenReturn(contextManager); - ShardingSphereDatabase database = createDatabases().get("db_0"); + ShardingSphereDatabase database = createDatabases().iterator().next(); when(contextManager.getDatabase("db_0")).thenReturn(database); ResponseHeader actual = unicastDatabaseBackendHandler.execute(); assertThat(actual, instanceOf(UpdateResponseHeader.class)); @@ -131,7 +131,7 @@ void assertExecuteDatabaseBackendHandler() throws SQLException { void assertDatabaseUsingStream() throws SQLException { ContextManager contextManager = mockContextManager(); when(ProxyContext.getInstance().getContextManager()).thenReturn(contextManager); - ShardingSphereDatabase database = createDatabases().get("db_0"); + ShardingSphereDatabase database = createDatabases().iterator().next(); when(contextManager.getDatabase("db_0")).thenReturn(database); unicastDatabaseBackendHandler.execute(); while (unicastDatabaseBackendHandler.next()) { @@ -147,13 +147,14 @@ private ContextManager mockContextManager() { return result; } - private Map createDatabases() { - Map result = new HashMap<>(10, 1F); + private Collection createDatabases() { + Collection result = new LinkedList<>(); for (int i = 0; i < 10; i++) { ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); + when(database.getName()).thenReturn(String.format(DATABASE_PATTERN, i)); when(database.containsDataSource()).thenReturn(true); when(database.getProtocolType()).thenReturn(TypedSPILoader.getService(DatabaseType.class, "FIXTURE")); - result.put(String.format(DATABASE_PATTERN, i), database); + result.add(database); } return result; } diff --git a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/database/DatabaseOperateBackendHandlerFactoryTest.java b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/database/DatabaseOperateBackendHandlerFactoryTest.java index 3a9ccc62d66a2..78e5fa7ea502f 100644 --- a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/database/DatabaseOperateBackendHandlerFactoryTest.java +++ b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/database/DatabaseOperateBackendHandlerFactoryTest.java @@ -17,14 +17,14 @@ package org.apache.shardingsphere.proxy.backend.handler.database; -import org.apache.shardingsphere.infra.exception.dialect.exception.syntax.database.DatabaseCreateExistsException; import org.apache.shardingsphere.infra.config.props.ConfigurationProperties; import org.apache.shardingsphere.infra.database.core.type.DatabaseType; +import org.apache.shardingsphere.infra.exception.dialect.exception.syntax.database.DatabaseCreateExistsException; +import org.apache.shardingsphere.infra.exception.generic.UnsupportedSQLOperationException; import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData; import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; import org.apache.shardingsphere.infra.metadata.database.resource.ResourceMetaData; import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData; -import org.apache.shardingsphere.infra.exception.generic.UnsupportedSQLOperationException; import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader; import org.apache.shardingsphere.metadata.persist.MetaDataPersistService; import org.apache.shardingsphere.mode.manager.ContextManager; @@ -54,7 +54,6 @@ import java.sql.SQLException; import java.util.Collections; -import java.util.Map; import java.util.Properties; import static org.hamcrest.CoreMatchers.instanceOf; @@ -81,7 +80,7 @@ class DatabaseOperateBackendHandlerFactoryTest { void setUp() { when(connectionSession.getConnectionContext().getGrantee()).thenReturn(null); MetaDataContexts metaDataContexts = MetaDataContextsFactory.create(mock(MetaDataPersistService.class), - new ShardingSphereMetaData(getDatabases(), mock(ResourceMetaData.class), mock(RuleMetaData.class), new ConfigurationProperties(new Properties()))); + new ShardingSphereMetaData(Collections.singleton(mockDatabase()), mock(ResourceMetaData.class), mock(RuleMetaData.class), new ConfigurationProperties(new Properties()))); when(contextManager.getMetaDataContexts()).thenReturn(metaDataContexts); when(ProxyContext.getInstance().getContextManager()).thenReturn(contextManager); when(ProxyContext.getInstance().databaseExists("foo_db")).thenReturn(true); @@ -146,10 +145,11 @@ private void assertExecuteCreateDatabaseContextWithException(final CreateDatabas } } - private Map getDatabases() { - ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); - when(database.getProtocolType()).thenReturn(TypedSPILoader.getService(DatabaseType.class, "FIXTURE")); - return Collections.singletonMap("foo_db", database); + private ShardingSphereDatabase mockDatabase() { + ShardingSphereDatabase result = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); + when(result.getName()).thenReturn("foo_db"); + when(result.getProtocolType()).thenReturn(TypedSPILoader.getService(DatabaseType.class, "FIXTURE")); + return result; } private void setGovernanceMetaDataContexts(final boolean isGovernance) { diff --git a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/queryable/ExportMetaDataExecutorTest.java b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/queryable/ExportMetaDataExecutorTest.java index b733fec53569b..2700c0fc35873 100644 --- a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/queryable/ExportMetaDataExecutorTest.java +++ b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/queryable/ExportMetaDataExecutorTest.java @@ -70,8 +70,8 @@ import java.util.Arrays; import java.util.Collection; import java.util.Collections; -import java.util.HashMap; import java.util.LinkedHashMap; +import java.util.LinkedList; import java.util.Map; import java.util.Map.Entry; import java.util.Properties; @@ -126,8 +126,8 @@ void assertExecuteWithEmptyMetaData() { private ContextManager mockEmptyContextManager() { ContextManager result = mock(ContextManager.class, RETURNS_DEEP_STUBS); - MetaDataContexts metaDataContexts = MetaDataContextsFactory.create(mock(MetaDataPersistService.class), new ShardingSphereMetaData(new HashMap<>(), - new ResourceMetaData(Collections.emptyMap()), new RuleMetaData(Collections.singletonList(new GlobalClockRule(new DefaultGlobalClockRuleConfigurationBuilder().build()))), + MetaDataContexts metaDataContexts = MetaDataContextsFactory.create(mock(MetaDataPersistService.class), new ShardingSphereMetaData(new LinkedList<>(), + new ResourceMetaData(Collections.emptyMap()), new RuleMetaData(Collections.singleton(new GlobalClockRule(new DefaultGlobalClockRuleConfigurationBuilder().build()))), new ConfigurationProperties(new Properties()))); when(result.getMetaDataContexts()).thenReturn(metaDataContexts); return result; @@ -162,7 +162,7 @@ private Map createStorageUnits() { } private ContextManager mockContextManager() { - MetaDataContexts metaDataContexts = MetaDataContextsFactory.create(mock(MetaDataPersistService.class), new ShardingSphereMetaData(Collections.singletonMap(database.getName(), database), + MetaDataContexts metaDataContexts = MetaDataContextsFactory.create(mock(MetaDataPersistService.class), new ShardingSphereMetaData(Collections.singleton(database), new ResourceMetaData(Collections.emptyMap()), new RuleMetaData(Arrays.asList(new AuthorityRule(new DefaultAuthorityRuleConfigurationBuilder().build()), new GlobalClockRule(new DefaultGlobalClockRuleConfigurationBuilder().build()))), diff --git a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/queryable/ExportStorageNodesExecutorTest.java b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/queryable/ExportStorageNodesExecutorTest.java index acec7da7b06f6..870359bffa9ff 100644 --- a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/queryable/ExportStorageNodesExecutorTest.java +++ b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/queryable/ExportStorageNodesExecutorTest.java @@ -112,7 +112,7 @@ void assertExecuteWithEmptyMetaData() { private ContextManager mockEmptyContextManager() { ContextManager result = mock(ContextManager.class, RETURNS_DEEP_STUBS); - MetaDataContexts metaDataContexts = MetaDataContextsFactory.create(mock(MetaDataPersistService.class), new ShardingSphereMetaData(Collections.emptyMap(), + MetaDataContexts metaDataContexts = MetaDataContextsFactory.create(mock(MetaDataPersistService.class), new ShardingSphereMetaData(Collections.emptyList(), new ResourceMetaData(Collections.emptyMap()), new RuleMetaData(Collections.emptyList()), new ConfigurationProperties(new Properties()))); when(result.getMetaDataContexts()).thenReturn(metaDataContexts); return result; @@ -148,7 +148,7 @@ void assertExecuteWithDatabaseName() { private ContextManager mockContextManager() { ContextManager result = mock(ContextManager.class, RETURNS_DEEP_STUBS); - MetaDataContexts metaDataContexts = MetaDataContextsFactory.create(mock(MetaDataPersistService.class), new ShardingSphereMetaData(Collections.singletonMap(database.getName(), database), + MetaDataContexts metaDataContexts = MetaDataContextsFactory.create(mock(MetaDataPersistService.class), new ShardingSphereMetaData(Collections.singleton(database), new ResourceMetaData(Collections.emptyMap()), new RuleMetaData(Collections.singleton(new AuthorityRule(new DefaultAuthorityRuleConfigurationBuilder().build()))), new ConfigurationProperties(PropertiesBuilder.build(new Property(ConfigurationPropertyKey.SQL_SHOW.getKey(), "true"))))); diff --git a/proxy/backend/type/mysql/src/test/java/org/apache/shardingsphere/proxy/backend/mysql/handler/admin/MySQLAdminExecutorCreatorTest.java b/proxy/backend/type/mysql/src/test/java/org/apache/shardingsphere/proxy/backend/mysql/handler/admin/MySQLAdminExecutorCreatorTest.java index 743d24252f1f2..e30987e974d6f 100644 --- a/proxy/backend/type/mysql/src/test/java/org/apache/shardingsphere/proxy/backend/mysql/handler/admin/MySQLAdminExecutorCreatorTest.java +++ b/proxy/backend/type/mysql/src/test/java/org/apache/shardingsphere/proxy/backend/mysql/handler/admin/MySQLAdminExecutorCreatorTest.java @@ -72,8 +72,8 @@ import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mock; +import java.util.Collection; import java.util.Collections; -import java.util.Map; import java.util.Optional; import java.util.Properties; @@ -205,7 +205,7 @@ void assertCreateWithSelectStatementForCurrentUser() { @Test void assertCreateWithSelectStatementForTransactionReadOnly() { - initProxyContext(Collections.emptyMap()); + initProxyContext(Collections.emptyList()); MySQLSelectStatement selectStatement = mock(MySQLSelectStatement.class); when(selectStatement.getFrom()).thenReturn(Optional.empty()); ProjectionsSegment projectionsSegment = mock(ProjectionsSegment.class); @@ -220,7 +220,7 @@ void assertCreateWithSelectStatementForTransactionReadOnly() { @Test void assertCreateWithSelectStatementForTransactionIsolation() { - initProxyContext(Collections.emptyMap()); + initProxyContext(Collections.emptyList()); MySQLSelectStatement selectStatement = mock(MySQLSelectStatement.class); when(selectStatement.getFrom()).thenReturn(Optional.empty()); ProjectionsSegment projectionsSegment = mock(ProjectionsSegment.class); @@ -235,7 +235,7 @@ void assertCreateWithSelectStatementForTransactionIsolation() { @Test void assertCreateWithSelectStatementForShowDatabase() { - initProxyContext(Collections.emptyMap()); + initProxyContext(Collections.emptyList()); MySQLSelectStatement selectStatement = mock(MySQLSelectStatement.class); when(selectStatement.getFrom()).thenReturn(Optional.empty()); ProjectionsSegment projectionsSegment = mock(ProjectionsSegment.class); @@ -249,7 +249,7 @@ void assertCreateWithSelectStatementForShowDatabase() { @Test void assertCreateWithOtherSelectStatementForNoResource() { - initProxyContext(Collections.emptyMap()); + initProxyContext(Collections.emptyList()); MySQLSelectStatement selectStatement = mock(MySQLSelectStatement.class); when(selectStatement.getFrom()).thenReturn(Optional.empty()); ProjectionsSegment projectionsSegment = mock(ProjectionsSegment.class); @@ -265,8 +265,7 @@ void assertCreateWithOtherSelectStatementForNoResource() { void assertCreateWithOtherSelectStatementForDatabaseName() { ResourceMetaData resourceMetaData = new ResourceMetaData(Collections.singletonMap("ds", new MockedDataSource())); ShardingSphereDatabase database = new ShardingSphereDatabase("db_0", databaseType, resourceMetaData, mock(RuleMetaData.class), Collections.emptyList()); - Map result = Collections.singletonMap("db_0", database); - initProxyContext(result); + initProxyContext(Collections.singleton(database)); when(ProxyContext.getInstance().getAllDatabaseNames()).thenReturn(Collections.singleton("db_0")); when(ProxyContext.getInstance().getContextManager().getDatabase("db_0")).thenReturn(database); MySQLSelectStatement selectStatement = mock(MySQLSelectStatement.class); @@ -283,8 +282,7 @@ void assertCreateWithOtherSelectStatementForDatabaseName() { void assertCreateWithOtherSelectStatementForNullDatabaseName() { ResourceMetaData resourceMetaData = new ResourceMetaData(Collections.singletonMap("ds_0", new MockedDataSource())); ShardingSphereDatabase database = new ShardingSphereDatabase("db_0", databaseType, resourceMetaData, mock(RuleMetaData.class), Collections.emptyList()); - Map result = Collections.singletonMap("db_0", database); - initProxyContext(result); + initProxyContext(Collections.singleton(database)); when(ProxyContext.getInstance().getAllDatabaseNames()).thenReturn(Collections.singleton("db_0")); when(ProxyContext.getInstance().getContextManager().getDatabase("db_0")).thenReturn(database); MySQLSelectStatement selectStatement = mock(MySQLSelectStatement.class); @@ -300,7 +298,7 @@ void assertCreateWithOtherSelectStatementForNullDatabaseName() { @Test void assertCreateWithSelectStatementFromInformationSchemaOfDefaultExecutorTables() { - initProxyContext(Collections.emptyMap()); + initProxyContext(Collections.emptyList()); SimpleTableSegment tableSegment = new SimpleTableSegment(new TableNameSegment(10, 13, new IdentifierValue("ENGINES"))); tableSegment.setOwner(new OwnerSegment(7, 8, new IdentifierValue("information_schema"))); MySQLSelectStatement selectStatement = mock(MySQLSelectStatement.class); @@ -313,7 +311,7 @@ void assertCreateWithSelectStatementFromInformationSchemaOfDefaultExecutorTables @Test void assertCreateWithSelectStatementFromInformationSchemaOfSchemaTable() { - initProxyContext(Collections.emptyMap()); + initProxyContext(Collections.emptyList()); SimpleTableSegment tableSegment = new SimpleTableSegment(new TableNameSegment(10, 13, new IdentifierValue("SCHEMATA"))); tableSegment.setOwner(new OwnerSegment(7, 8, new IdentifierValue("information_schema"))); MySQLSelectStatement selectStatement = mock(MySQLSelectStatement.class); @@ -329,7 +327,7 @@ void assertCreateWithSelectStatementFromInformationSchemaOfSchemaTable() { @Test void assertCreateWithSelectStatementFromInformationSchemaOfOtherTable() { - initProxyContext(Collections.emptyMap()); + initProxyContext(Collections.emptyList()); SimpleTableSegment tableSegment = new SimpleTableSegment(new TableNameSegment(10, 13, new IdentifierValue("CHARACTER_SETS"))); tableSegment.setOwner(new OwnerSegment(7, 8, new IdentifierValue("information_schema"))); MySQLSelectStatement selectStatement = mock(MySQLSelectStatement.class); @@ -341,7 +339,7 @@ void assertCreateWithSelectStatementFromInformationSchemaOfOtherTable() { @Test void assertCreateWithSelectStatementFromPerformanceSchema() { - initProxyContext(Collections.emptyMap()); + initProxyContext(Collections.emptyList()); SimpleTableSegment tableSegment = new SimpleTableSegment(new TableNameSegment(10, 13, new IdentifierValue("accounts"))); tableSegment.setOwner(new OwnerSegment(7, 8, new IdentifierValue("performance_schema"))); MySQLSelectStatement selectStatement = mock(MySQLSelectStatement.class); @@ -351,7 +349,7 @@ void assertCreateWithSelectStatementFromPerformanceSchema() { assertFalse(actual.isPresent()); } - private void initProxyContext(final Map databases) { + private void initProxyContext(final Collection databases) { ContextManager contextManager = mock(ContextManager.class, RETURNS_DEEP_STUBS); MetaDataContexts metaDataContexts = MetaDataContextsFactory.create(mock(MetaDataPersistService.class), new ShardingSphereMetaData(databases, mock(ResourceMetaData.class), mock(RuleMetaData.class), new ConfigurationProperties(new Properties()))); diff --git a/proxy/backend/type/mysql/src/test/java/org/apache/shardingsphere/proxy/backend/mysql/handler/admin/executor/ShowCreateDatabaseExecutorTest.java b/proxy/backend/type/mysql/src/test/java/org/apache/shardingsphere/proxy/backend/mysql/handler/admin/executor/ShowCreateDatabaseExecutorTest.java index 0ae2de72ee6dc..38f217ab44d37 100644 --- a/proxy/backend/type/mysql/src/test/java/org/apache/shardingsphere/proxy/backend/mysql/handler/admin/executor/ShowCreateDatabaseExecutorTest.java +++ b/proxy/backend/type/mysql/src/test/java/org/apache/shardingsphere/proxy/backend/mysql/handler/admin/executor/ShowCreateDatabaseExecutorTest.java @@ -38,8 +38,8 @@ import org.junit.jupiter.api.extension.ExtendWith; import java.sql.SQLException; -import java.util.HashMap; -import java.util.Map; +import java.util.Collection; +import java.util.LinkedList; import java.util.Optional; import java.util.Properties; @@ -74,24 +74,24 @@ void assertExecute() throws SQLException { } private ContextManager mockContextManager() { - Map databases = getDatabases(); MetaDataPersistService metaDataPersistService = mock(MetaDataPersistService.class); ShardingSphereDataPersistService shardingSphereDataPersistService = mock(ShardingSphereDataPersistService.class); when(shardingSphereDataPersistService.load(any())).thenReturn(Optional.empty()); when(metaDataPersistService.getShardingSphereDataPersistService()).thenReturn(shardingSphereDataPersistService); MetaDataContexts metaDataContexts = MetaDataContextsFactory.create(metaDataPersistService, - new ShardingSphereMetaData(databases, mock(ResourceMetaData.class), mock(RuleMetaData.class), new ConfigurationProperties(new Properties()))); + new ShardingSphereMetaData(mockDatabases(), mock(ResourceMetaData.class), mock(RuleMetaData.class), new ConfigurationProperties(new Properties()))); ContextManager result = mock(ContextManager.class, RETURNS_DEEP_STUBS); when(result.getMetaDataContexts()).thenReturn(metaDataContexts); return result; } - private Map getDatabases() { - Map result = new HashMap<>(10, 1F); + private Collection mockDatabases() { + Collection result = new LinkedList<>(); for (int i = 0; i < 10; i++) { ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); + when(database.getName()).thenReturn(String.format(DATABASE_PATTERN, i)); when(database.getProtocolType()).thenReturn(TypedSPILoader.getService(DatabaseType.class, "MySQL")); - result.put(String.format(DATABASE_PATTERN, i), database); + result.add(database); } return result; } diff --git a/proxy/backend/type/mysql/src/test/java/org/apache/shardingsphere/proxy/backend/mysql/handler/admin/executor/ShowCurrentUserExecutorTest.java b/proxy/backend/type/mysql/src/test/java/org/apache/shardingsphere/proxy/backend/mysql/handler/admin/executor/ShowCurrentUserExecutorTest.java index 7414da5c5c63d..8f48969f13f5c 100644 --- a/proxy/backend/type/mysql/src/test/java/org/apache/shardingsphere/proxy/backend/mysql/handler/admin/executor/ShowCurrentUserExecutorTest.java +++ b/proxy/backend/type/mysql/src/test/java/org/apache/shardingsphere/proxy/backend/mysql/handler/admin/executor/ShowCurrentUserExecutorTest.java @@ -67,7 +67,7 @@ void assertExecute() throws SQLException { private ContextManager mockContextManager() { ContextManager result = mock(ContextManager.class, RETURNS_DEEP_STUBS); MetaDataContexts metaDataContexts = MetaDataContextsFactory.create(mock(MetaDataPersistService.class), - new ShardingSphereMetaData(Collections.emptyMap(), mock(ResourceMetaData.class), mockRuleMetaData(), new ConfigurationProperties(new Properties()))); + new ShardingSphereMetaData(Collections.emptyList(), mock(ResourceMetaData.class), mockRuleMetaData(), new ConfigurationProperties(new Properties()))); when(result.getMetaDataContexts()).thenReturn(metaDataContexts); return result; } diff --git a/proxy/backend/type/mysql/src/test/java/org/apache/shardingsphere/proxy/backend/mysql/handler/admin/executor/ShowDatabasesExecutorTest.java b/proxy/backend/type/mysql/src/test/java/org/apache/shardingsphere/proxy/backend/mysql/handler/admin/executor/ShowDatabasesExecutorTest.java index 4b3504aa1d2eb..5007ffe98215c 100644 --- a/proxy/backend/type/mysql/src/test/java/org/apache/shardingsphere/proxy/backend/mysql/handler/admin/executor/ShowDatabasesExecutorTest.java +++ b/proxy/backend/type/mysql/src/test/java/org/apache/shardingsphere/proxy/backend/mysql/handler/admin/executor/ShowDatabasesExecutorTest.java @@ -48,7 +48,7 @@ import java.sql.SQLException; import java.util.Collection; import java.util.Collections; -import java.util.LinkedHashMap; +import java.util.LinkedList; import java.util.Map; import java.util.Optional; import java.util.Properties; @@ -189,8 +189,8 @@ private ContextManager mockContextManager() { ShardingSphereDataPersistService shardingSphereDataPersistService = mock(ShardingSphereDataPersistService.class); when(shardingSphereDataPersistService.load(any())).thenReturn(Optional.empty()); when(metaDataPersistService.getShardingSphereDataPersistService()).thenReturn(shardingSphereDataPersistService); - MetaDataContexts metaDataContexts = MetaDataContextsFactory.create(metaDataPersistService, new ShardingSphereMetaData(getDatabases(), - mock(ResourceMetaData.class), globalRuleMetaData, new ConfigurationProperties(new Properties()))); + MetaDataContexts metaDataContexts = MetaDataContextsFactory.create(metaDataPersistService, + new ShardingSphereMetaData(mockDatabases(), mock(ResourceMetaData.class), globalRuleMetaData, new ConfigurationProperties(new Properties()))); when(result.getMetaDataContexts()).thenReturn(metaDataContexts); return result; } @@ -208,13 +208,13 @@ private ShardingSpherePrivileges mockPrivileges() { return result; } - private Map getDatabases() { - Map result = new LinkedHashMap<>(10, 1F); + private Collection mockDatabases() { + Collection result = new LinkedList<>(); for (int i = 0; i < 10; i++) { ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); - when(database.getProtocolType()).thenReturn(TypedSPILoader.getService(DatabaseType.class, "MySQL")); when(database.getName()).thenReturn(String.format(DATABASE_PATTERN, i)); - result.put(String.format(DATABASE_PATTERN, i), database); + when(database.getProtocolType()).thenReturn(TypedSPILoader.getService(DatabaseType.class, "MySQL")); + result.add(database); } return result; } diff --git a/proxy/backend/type/mysql/src/test/java/org/apache/shardingsphere/proxy/backend/mysql/handler/admin/executor/ShowFunctionStatusExecutorTest.java b/proxy/backend/type/mysql/src/test/java/org/apache/shardingsphere/proxy/backend/mysql/handler/admin/executor/ShowFunctionStatusExecutorTest.java index 7c035d71bd616..1a66cdb2ed2a8 100644 --- a/proxy/backend/type/mysql/src/test/java/org/apache/shardingsphere/proxy/backend/mysql/handler/admin/executor/ShowFunctionStatusExecutorTest.java +++ b/proxy/backend/type/mysql/src/test/java/org/apache/shardingsphere/proxy/backend/mysql/handler/admin/executor/ShowFunctionStatusExecutorTest.java @@ -38,8 +38,8 @@ import org.junit.jupiter.api.extension.ExtendWith; import java.sql.SQLException; -import java.util.HashMap; -import java.util.Map; +import java.util.Collection; +import java.util.LinkedList; import java.util.Optional; import java.util.Properties; @@ -66,24 +66,24 @@ void assertExecute() throws SQLException { } private ContextManager mockContextManager() { - Map databases = getDatabases(); MetaDataPersistService metaDataPersistService = mock(MetaDataPersistService.class); ShardingSphereDataPersistService shardingSphereDataPersistService = mock(ShardingSphereDataPersistService.class); when(shardingSphereDataPersistService.load(any())).thenReturn(Optional.empty()); when(metaDataPersistService.getShardingSphereDataPersistService()).thenReturn(shardingSphereDataPersistService); MetaDataContexts metaDataContexts = MetaDataContextsFactory.create(metaDataPersistService, - new ShardingSphereMetaData(databases, mock(ResourceMetaData.class), mock(RuleMetaData.class), new ConfigurationProperties(new Properties()))); + new ShardingSphereMetaData(mockDatabases(), mock(ResourceMetaData.class), mock(RuleMetaData.class), new ConfigurationProperties(new Properties()))); ContextManager result = mock(ContextManager.class, RETURNS_DEEP_STUBS); when(result.getMetaDataContexts()).thenReturn(metaDataContexts); return result; } - private Map getDatabases() { - Map result = new HashMap<>(10, 1F); + private Collection mockDatabases() { + Collection result = new LinkedList<>(); for (int i = 0; i < 10; i++) { ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); + when(database.getName()).thenReturn(String.format(DATABASE_PATTERN, i)); when(database.getProtocolType()).thenReturn(TypedSPILoader.getService(DatabaseType.class, "MySQL")); - result.put(String.format(DATABASE_PATTERN, i), database); + result.add(database); } return result; } diff --git a/proxy/backend/type/mysql/src/test/java/org/apache/shardingsphere/proxy/backend/mysql/handler/admin/executor/ShowProcedureStatusExecutorTest.java b/proxy/backend/type/mysql/src/test/java/org/apache/shardingsphere/proxy/backend/mysql/handler/admin/executor/ShowProcedureStatusExecutorTest.java index 3f943ca250ac8..d74dd78c28c83 100644 --- a/proxy/backend/type/mysql/src/test/java/org/apache/shardingsphere/proxy/backend/mysql/handler/admin/executor/ShowProcedureStatusExecutorTest.java +++ b/proxy/backend/type/mysql/src/test/java/org/apache/shardingsphere/proxy/backend/mysql/handler/admin/executor/ShowProcedureStatusExecutorTest.java @@ -38,8 +38,8 @@ import org.junit.jupiter.api.extension.ExtendWith; import java.sql.SQLException; -import java.util.HashMap; -import java.util.Map; +import java.util.Collection; +import java.util.LinkedList; import java.util.Optional; import java.util.Properties; @@ -66,24 +66,24 @@ void assertExecute() throws SQLException { } private ContextManager mockContextManager() { - Map databases = getDatabases(); MetaDataPersistService metaDataPersistService = mock(MetaDataPersistService.class); ShardingSphereDataPersistService shardingSphereDataPersistService = mock(ShardingSphereDataPersistService.class); when(shardingSphereDataPersistService.load(any())).thenReturn(Optional.empty()); when(metaDataPersistService.getShardingSphereDataPersistService()).thenReturn(shardingSphereDataPersistService); MetaDataContexts metaDataContexts = MetaDataContextsFactory.create(metaDataPersistService, - new ShardingSphereMetaData(databases, mock(ResourceMetaData.class), mock(RuleMetaData.class), new ConfigurationProperties(new Properties()))); + new ShardingSphereMetaData(mockDatabases(), mock(ResourceMetaData.class), mock(RuleMetaData.class), new ConfigurationProperties(new Properties()))); ContextManager result = mock(ContextManager.class, RETURNS_DEEP_STUBS); when(result.getMetaDataContexts()).thenReturn(metaDataContexts); return result; } - private Map getDatabases() { - Map result = new HashMap<>(10, 1F); + private Collection mockDatabases() { + Collection result = new LinkedList<>(); for (int i = 0; i < 10; i++) { ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); + when(database.getName()).thenReturn(String.format(DATABASE_PATTERN, i)); when(database.getProtocolType()).thenReturn(TypedSPILoader.getService(DatabaseType.class, "MySQL")); - result.put(String.format(DATABASE_PATTERN, i), database); + result.add(database); } return result; } diff --git a/proxy/backend/type/mysql/src/test/java/org/apache/shardingsphere/proxy/backend/mysql/handler/admin/executor/ShowTablesExecutorTest.java b/proxy/backend/type/mysql/src/test/java/org/apache/shardingsphere/proxy/backend/mysql/handler/admin/executor/ShowTablesExecutorTest.java index 86fa671fee2db..350dbc3218aad 100644 --- a/proxy/backend/type/mysql/src/test/java/org/apache/shardingsphere/proxy/backend/mysql/handler/admin/executor/ShowTablesExecutorTest.java +++ b/proxy/backend/type/mysql/src/test/java/org/apache/shardingsphere/proxy/backend/mysql/handler/admin/executor/ShowTablesExecutorTest.java @@ -53,9 +53,7 @@ import java.sql.SQLException; import java.util.Collection; import java.util.Collections; -import java.util.HashMap; import java.util.LinkedList; -import java.util.Map; import java.util.Optional; import java.util.Properties; @@ -77,7 +75,7 @@ class ShowTablesExecutorTest { @Test void assertShowTablesExecutorWithoutFilter() throws SQLException { ShowTablesExecutor executor = new ShowTablesExecutor(new MySQLShowTablesStatement(), TypedSPILoader.getService(DatabaseType.class, "MySQL")); - Map databases = getDatabases(); + Collection databases = mockDatabases(); ContextManager contextManager = mockContextManager(databases); when(ProxyContext.getInstance().getContextManager()).thenReturn(contextManager); executor.execute(mockConnectionSession()); @@ -98,7 +96,7 @@ void assertShowTablesExecutorWithFull() throws SQLException { MySQLShowTablesStatement showTablesStatement = mock(MySQLShowTablesStatement.class); when(showTablesStatement.isContainsFull()).thenReturn(true); ShowTablesExecutor executor = new ShowTablesExecutor(showTablesStatement, TypedSPILoader.getService(DatabaseType.class, "MySQL")); - Map databases = getDatabases(); + Collection databases = mockDatabases(); ContextManager contextManager = mockContextManager(databases); when(ProxyContext.getInstance().getContextManager()).thenReturn(contextManager); executor.execute(mockConnectionSession()); @@ -112,7 +110,7 @@ void assertShowTablesExecutorWithLikeFilter() throws SQLException { when(showFilterSegment.getLike()).thenReturn(Optional.of(new ShowLikeSegment(0, 10, "t_account%"))); showTablesStatement.setFilter(showFilterSegment); ShowTablesExecutor executor = new ShowTablesExecutor(showTablesStatement, TypedSPILoader.getService(DatabaseType.class, "MySQL")); - Map databases = getDatabases(); + Collection databases = mockDatabases(); ContextManager contextManager = mockContextManager(databases); when(ProxyContext.getInstance().getContextManager()).thenReturn(contextManager); executor.execute(mockConnectionSession()); @@ -133,7 +131,7 @@ void assertShowTablesExecutorWithSpecificTable() throws SQLException { when(showFilterSegment.getLike()).thenReturn(Optional.of(new ShowLikeSegment(0, 10, "t_account"))); showTablesStatement.setFilter(showFilterSegment); ShowTablesExecutor executor = new ShowTablesExecutor(showTablesStatement, TypedSPILoader.getService(DatabaseType.class, "MySQL")); - Map databases = getDatabases(); + Collection databases = mockDatabases(); ContextManager contextManager = mockContextManager(databases); when(ProxyContext.getInstance().getContextManager()).thenReturn(contextManager); executor.execute(mockConnectionSession()); @@ -150,7 +148,7 @@ void assertShowTablesExecutorWithUpperCase() throws SQLException { when(showFilterSegment.getLike()).thenReturn(Optional.of(new ShowLikeSegment(0, 10, "T_TEST"))); showTablesStatement.setFilter(showFilterSegment); ShowTablesExecutor executor = new ShowTablesExecutor(showTablesStatement, TypedSPILoader.getService(DatabaseType.class, "MySQL")); - Map databases = getDatabases(); + Collection databases = mockDatabases(); ContextManager contextManager = mockContextManager(databases); when(ProxyContext.getInstance().getContextManager()).thenReturn(contextManager); executor.execute(mockConnectionSession()); @@ -167,7 +165,7 @@ void assertShowTablesExecutorWithLowerCase() throws SQLException { when(showFilterSegment.getLike()).thenReturn(Optional.of(new ShowLikeSegment(0, 10, "t_test"))); showTablesStatement.setFilter(showFilterSegment); ShowTablesExecutor executor = new ShowTablesExecutor(showTablesStatement, TypedSPILoader.getService(DatabaseType.class, "MySQL")); - Map databases = getDatabases(); + Collection databases = mockDatabases(); ContextManager contextManager = mockContextManager(databases); when(ProxyContext.getInstance().getContextManager()).thenReturn(contextManager); executor.execute(mockConnectionSession()); @@ -182,7 +180,7 @@ void assertShowTableFromUncompletedDatabase() throws SQLException { MySQLShowTablesStatement showTablesStatement = new MySQLShowTablesStatement(); showTablesStatement.setFromDatabase(new FromDatabaseSegment(0, 0, new DatabaseSegment(0, 0, new IdentifierValue("uncompleted")))); ShowTablesExecutor executor = new ShowTablesExecutor(showTablesStatement, TypedSPILoader.getService(DatabaseType.class, "MySQL")); - ContextManager contextManager = mockContextManager(getDatabases()); + ContextManager contextManager = mockContextManager(mockDatabases()); when(ProxyContext.getInstance().getContextManager()).thenReturn(contextManager); executor.execute(mockConnectionSession()); QueryResultMetaData actualMetaData = executor.getQueryResultMetaData(); @@ -192,7 +190,7 @@ void assertShowTableFromUncompletedDatabase() throws SQLException { assertFalse(actualResult.next()); } - private ContextManager mockContextManager(final Map databases) { + private ContextManager mockContextManager(final Collection databases) { MetaDataPersistService metaDataPersistService = mock(MetaDataPersistService.class); ShardingSphereDataPersistService shardingSphereDataPersistService = mock(ShardingSphereDataPersistService.class); when(shardingSphereDataPersistService.load(any())).thenReturn(Optional.empty()); @@ -201,11 +199,11 @@ private ContextManager mockContextManager(final Map getDatabases() { + private Collection mockDatabases() { Collection tables = new LinkedList<>(); tables.add(new ShardingSphereTable("t_account", Collections.emptyList(), Collections.emptyList(), Collections.emptyList())); tables.add(new ShardingSphereTable("t_account_bak", Collections.emptyList(), Collections.emptyList(), Collections.emptyList())); @@ -213,13 +211,15 @@ private Map getDatabases() { tables.add(new ShardingSphereTable("T_TEST", Collections.emptyList(), Collections.emptyList(), Collections.emptyList())); ShardingSphereSchema schema = new ShardingSphereSchema(DefaultDatabase.LOGIC_NAME, tables, Collections.emptyList()); ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); + when(database.getName()).thenReturn(String.format(DATABASE_PATTERN, 0)); when(database.getSchema(String.format(DATABASE_PATTERN, 0))).thenReturn(schema); when(database.isComplete()).thenReturn(true); when(database.getProtocolType()).thenReturn(TypedSPILoader.getService(DatabaseType.class, "MySQL")); - Map result = new HashMap<>(2, 1F); - result.put(String.format(DATABASE_PATTERN, 0), database); + Collection result = new LinkedList<>(); + result.add(database); ShardingSphereDatabase uncompletedDatabase = mock(ShardingSphereDatabase.class); - result.put("uncompleted", uncompletedDatabase); + when(uncompletedDatabase.getName()).thenReturn("uncompleted"); + result.add(uncompletedDatabase); return result; } diff --git a/proxy/backend/type/mysql/src/test/java/org/apache/shardingsphere/proxy/backend/mysql/handler/admin/executor/UseDatabaseExecutorTest.java b/proxy/backend/type/mysql/src/test/java/org/apache/shardingsphere/proxy/backend/mysql/handler/admin/executor/UseDatabaseExecutorTest.java index 9b83674eafb4c..0483a0eb7a035 100644 --- a/proxy/backend/type/mysql/src/test/java/org/apache/shardingsphere/proxy/backend/mysql/handler/admin/executor/UseDatabaseExecutorTest.java +++ b/proxy/backend/type/mysql/src/test/java/org/apache/shardingsphere/proxy/backend/mysql/handler/admin/executor/UseDatabaseExecutorTest.java @@ -39,8 +39,8 @@ import org.mockito.Answers; import org.mockito.Mock; -import java.util.HashMap; -import java.util.Map; +import java.util.Collection; +import java.util.LinkedList; import java.util.Properties; import static org.junit.jupiter.api.Assertions.assertThrows; @@ -85,17 +85,18 @@ void assertExecuteUseStatementBackendHandlerWhenSchemaNotExist() { private ContextManager mockContextManager() { ContextManager result = mock(ContextManager.class, RETURNS_DEEP_STUBS); MetaDataContexts metaDataContexts = MetaDataContextsFactory.create(mock(MetaDataPersistService.class), - new ShardingSphereMetaData(getDatabases(), mock(ResourceMetaData.class), mock(RuleMetaData.class), new ConfigurationProperties(new Properties()))); + new ShardingSphereMetaData(mockDatabases(), mock(ResourceMetaData.class), mock(RuleMetaData.class), new ConfigurationProperties(new Properties()))); when(result.getMetaDataContexts()).thenReturn(metaDataContexts); return result; } - private Map getDatabases() { - Map result = new HashMap<>(10, 1F); + private Collection mockDatabases() { + Collection result = new LinkedList<>(); for (int i = 0; i < 10; i++) { ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS); + when(database.getName()).thenReturn(String.format(DATABASE_PATTERN, i)); when(database.getProtocolType()).thenReturn(TypedSPILoader.getService(DatabaseType.class, "FIXTURE")); - result.put(String.format(DATABASE_PATTERN, i), database); + result.add(database); } return result; } 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 f5f194bcfe3bd..9c5856b0545b7 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 @@ -166,8 +166,8 @@ private ContextManager mockContextManager(final ShardingSphereDatabase... databa when(shardingSphereDataPersistService.load(any())).thenReturn(Optional.empty()); when(metaDataPersistService.getShardingSphereDataPersistService()).thenReturn(shardingSphereDataPersistService); MetaDataContexts metaDataContexts = MetaDataContextsFactory.create(metaDataPersistService, new ShardingSphereMetaData( - Arrays.stream(databases).collect(Collectors.toMap(ShardingSphereDatabase::getName, each -> each)), - mock(ResourceMetaData.class), new RuleMetaData(Collections.singleton(authorityRule)), new ConfigurationProperties(new Properties()))); + Arrays.stream(databases).collect(Collectors.toList()), mock(ResourceMetaData.class), new RuleMetaData(Collections.singleton(authorityRule)), + new ConfigurationProperties(new Properties()))); when(result.getMetaDataContexts()).thenReturn(metaDataContexts); for (ShardingSphereDatabase each : databases) { when(result.getDatabase(each.getName())).thenReturn(each); diff --git a/proxy/backend/type/opengauss/src/test/java/org/apache/shardingsphere/proxy/backend/opengauss/handler/admin/OpenGaussSystemCatalogAdminQueryExecutorTest.java b/proxy/backend/type/opengauss/src/test/java/org/apache/shardingsphere/proxy/backend/opengauss/handler/admin/OpenGaussSystemCatalogAdminQueryExecutorTest.java index cf42fd4513499..772aa6abd08f9 100644 --- a/proxy/backend/type/opengauss/src/test/java/org/apache/shardingsphere/proxy/backend/opengauss/handler/admin/OpenGaussSystemCatalogAdminQueryExecutorTest.java +++ b/proxy/backend/type/opengauss/src/test/java/org/apache/shardingsphere/proxy/backend/opengauss/handler/admin/OpenGaussSystemCatalogAdminQueryExecutorTest.java @@ -58,7 +58,6 @@ import java.util.Arrays; import java.util.Collection; import java.util.Collections; -import java.util.Map; import java.util.Optional; import java.util.Properties; @@ -86,12 +85,12 @@ void assertExecuteSelectFromPgDatabase() throws SQLException { when(connectionSession.getProtocolType()).thenReturn(databaseType); ConnectionContext connectionContext = mockConnectionContext(); when(connectionSession.getConnectionContext()).thenReturn(connectionContext); - Map databases = createDatabases(); - SQLFederationRule sqlFederationRule = new SQLFederationRule(new SQLFederationRuleConfiguration(false, false, new CacheOption(1, 1L)), databases.values()); + Collection databases = Collections.singleton(createDatabase()); + SQLFederationRule sqlFederationRule = new SQLFederationRule(new SQLFederationRuleConfiguration(false, false, new CacheOption(1, 1L)), databases); when(ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData().getGlobalRuleMetaData()).thenReturn(mock(RuleMetaData.class)); OpenGaussSelectStatement sqlStatement = createSelectStatementForPgDatabase(); - ShardingSphereMetaData metaData = - new ShardingSphereMetaData(databases, mock(ResourceMetaData.class, RETURNS_DEEP_STUBS), new RuleMetaData(Collections.singletonList(sqlFederationRule)), props); + ShardingSphereMetaData metaData = new ShardingSphereMetaData(databases, + mock(ResourceMetaData.class, RETURNS_DEEP_STUBS), new RuleMetaData(Collections.singletonList(sqlFederationRule)), props); when(ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData()).thenReturn(metaData); SelectStatementContext sqlStatementContext = new SelectStatementContext(metaData, Collections.emptyList(), sqlStatement, "sharding_db", Collections.emptyList()); OpenGaussSystemCatalogAdminQueryExecutor executor = new OpenGaussSystemCatalogAdminQueryExecutor(sqlStatementContext, @@ -118,7 +117,7 @@ private OpenGaussSelectStatement createSelectStatementForPgDatabase() { return result; } - private Map createDatabases() { + private ShardingSphereDatabase createDatabase() { Collection columns = Arrays.asList( new ShardingSphereColumn("datname", 12, false, false, false, true, false, false), new ShardingSphereColumn("datdba", -5, false, false, false, true, false, false), @@ -137,8 +136,7 @@ private Map createDatabases() { new ShardingSphereColumn("datminmxid", 1111, false, false, false, true, false, false)); ShardingSphereSchema schema = new ShardingSphereSchema("pg_catalog", Collections.singleton(new ShardingSphereTable("pg_database", columns, Collections.emptyList(), Collections.emptyList())), Collections.emptyList()); - return Collections.singletonMap("sharding_db", - new ShardingSphereDatabase("sharding_db", databaseType, mock(ResourceMetaData.class, RETURNS_DEEP_STUBS), mock(RuleMetaData.class), Collections.singleton(schema))); + return new ShardingSphereDatabase("sharding_db", databaseType, mock(ResourceMetaData.class, RETURNS_DEEP_STUBS), mock(RuleMetaData.class), Collections.singleton(schema)); } @Test @@ -148,8 +146,8 @@ void assertExecuteSelectVersion() throws SQLException { when(ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData().getGlobalRuleMetaData()).thenReturn(ruleMetaData); ConfigurationProperties props = new ConfigurationProperties(new Properties()); when(ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData().getProps()).thenReturn(props); - Map databases = createDatabases(); - SQLFederationRule sqlFederationRule = new SQLFederationRule(new SQLFederationRuleConfiguration(false, false, new CacheOption(1, 1L)), databases.values()); + Collection databases = Collections.singleton(createDatabase()); + SQLFederationRule sqlFederationRule = new SQLFederationRule(new SQLFederationRuleConfiguration(false, false, new CacheOption(1, 1L)), databases); OpenGaussSelectStatement sqlStatement = createSelectStatementForVersion(); ShardingSphereMetaData metaData = new ShardingSphereMetaData(databases, mock(ResourceMetaData.class, RETURNS_DEEP_STUBS), new RuleMetaData(Collections.singletonList(sqlFederationRule)), props); @@ -184,8 +182,8 @@ void assertExecuteSelectGsPasswordDeadlineAndIntervalToNum() throws SQLException when(ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData().getGlobalRuleMetaData()).thenReturn(ruleMetaData); ConfigurationProperties props = new ConfigurationProperties(new Properties()); when(ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData().getProps()).thenReturn(props); - Map databases = createDatabases(); - SQLFederationRule sqlFederationRule = new SQLFederationRule(new SQLFederationRuleConfiguration(false, false, new CacheOption(1, 1L)), databases.values()); + Collection databases = Collections.singleton(createDatabase()); + SQLFederationRule sqlFederationRule = new SQLFederationRule(new SQLFederationRuleConfiguration(false, false, new CacheOption(1, 1L)), databases); OpenGaussSelectStatement sqlStatement = createSelectStatementForGsPasswordDeadlineAndIntervalToNum(); ShardingSphereMetaData metaData = new ShardingSphereMetaData(databases, mock(ResourceMetaData.class, RETURNS_DEEP_STUBS), new RuleMetaData(Collections.singletonList(sqlFederationRule)), props); @@ -222,8 +220,8 @@ void assertExecuteSelectGsPasswordNotifyTime() throws SQLException { when(ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData().getGlobalRuleMetaData()).thenReturn(ruleMetaData); ConfigurationProperties props = new ConfigurationProperties(new Properties()); when(ProxyContext.getInstance().getContextManager().getMetaDataContexts().getMetaData().getProps()).thenReturn(props); - Map databases = createDatabases(); - SQLFederationRule sqlFederationRule = new SQLFederationRule(new SQLFederationRuleConfiguration(false, false, new CacheOption(1, 1L)), databases.values()); + Collection databases = Collections.singleton(createDatabase()); + SQLFederationRule sqlFederationRule = new SQLFederationRule(new SQLFederationRuleConfiguration(false, false, new CacheOption(1, 1L)), databases); OpenGaussSelectStatement sqlStatement = createSelectStatementForGsPasswordNotifyTime(); ShardingSphereMetaData metaData = new ShardingSphereMetaData(databases, mock(ResourceMetaData.class, RETURNS_DEEP_STUBS), new RuleMetaData(Collections.singletonList(sqlFederationRule)), props); diff --git a/proxy/frontend/core/src/test/java/org/apache/shardingsphere/proxy/frontend/protocol/FrontDatabaseProtocolTypeFactoryTest.java b/proxy/frontend/core/src/test/java/org/apache/shardingsphere/proxy/frontend/protocol/FrontDatabaseProtocolTypeFactoryTest.java index dda70c3a2289f..c2468079e3cc5 100644 --- a/proxy/frontend/core/src/test/java/org/apache/shardingsphere/proxy/frontend/protocol/FrontDatabaseProtocolTypeFactoryTest.java +++ b/proxy/frontend/core/src/test/java/org/apache/shardingsphere/proxy/frontend/protocol/FrontDatabaseProtocolTypeFactoryTest.java @@ -24,8 +24,6 @@ import org.apache.shardingsphere.infra.instance.ComputeNodeInstanceContext; import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData; import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; -import org.apache.shardingsphere.infra.metadata.database.resource.ResourceMetaData; -import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData; import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader; import org.apache.shardingsphere.metadata.persist.MetaDataPersistService; import org.apache.shardingsphere.metadata.persist.data.ShardingSphereDataPersistService; @@ -43,8 +41,8 @@ import org.mockito.junit.jupiter.MockitoSettings; import org.mockito.quality.Strictness; +import java.util.Collection; import java.util.Collections; -import java.util.Map; import java.util.Optional; import java.util.Properties; @@ -62,14 +60,14 @@ class FrontDatabaseProtocolTypeFactoryTest { @Test void assertGetDatabaseTypeWhenThrowShardingSphereConfigurationException() { - ContextManager contextManager = mockContextManager(Collections.emptyMap(), new Properties()); + ContextManager contextManager = mockContextManager(Collections.emptyList(), new Properties()); when(ProxyContext.getInstance().getContextManager()).thenReturn(contextManager); assertThat(FrontDatabaseProtocolTypeFactory.getDatabaseType().getType(), is("MySQL")); } @Test void assertGetDatabaseTypeInstanceOfMySQLDatabaseTypeFromMetaDataContextsSchemaName() { - ContextManager contextManager = mockContextManager(mockDatabases(), new Properties()); + ContextManager contextManager = mockContextManager(Collections.singleton(mockDatabase()), new Properties()); when(ProxyContext.getInstance().getContextManager()).thenReturn(contextManager); DatabaseType databaseType = FrontDatabaseProtocolTypeFactory.getDatabaseType(); assertThat(databaseType, instanceOf(DatabaseType.class)); @@ -78,7 +76,7 @@ void assertGetDatabaseTypeInstanceOfMySQLDatabaseTypeFromMetaDataContextsSchemaN @Test void assertGetDatabaseTypeOfPostgreSQLDatabaseTypeFromMetaDataContextsProps() { - ContextManager contextManager = mockContextManager(mockDatabases(), + ContextManager contextManager = mockContextManager(Collections.singleton(mockDatabase()), PropertiesBuilder.build(new Property(ConfigurationPropertyKey.PROXY_FRONTEND_DATABASE_PROTOCOL_TYPE.getKey(), "PostgreSQL"))); when(ProxyContext.getInstance().getContextManager()).thenReturn(contextManager); DatabaseType databaseType = FrontDatabaseProtocolTypeFactory.getDatabaseType(); @@ -86,19 +84,19 @@ void assertGetDatabaseTypeOfPostgreSQLDatabaseTypeFromMetaDataContextsProps() { assertThat(databaseType.getType(), is("PostgreSQL")); } - private Map mockDatabases() { - ShardingSphereDatabase database = mock(ShardingSphereDatabase.class); - when(database.getProtocolType()).thenReturn(TypedSPILoader.getService(DatabaseType.class, "MySQL")); - return Collections.singletonMap("foo_db", database); + private ShardingSphereDatabase mockDatabase() { + ShardingSphereDatabase result = mock(ShardingSphereDatabase.class); + when(result.getName()).thenReturn("foo_db"); + when(result.getProtocolType()).thenReturn(TypedSPILoader.getService(DatabaseType.class, "MySQL")); + return result; } - private ContextManager mockContextManager(final Map databases, final Properties props) { + private ContextManager mockContextManager(final Collection databases, final Properties props) { MetaDataPersistService metaDataPersistService = mock(MetaDataPersistService.class); ShardingSphereDataPersistService shardingSphereDataPersistService = mock(ShardingSphereDataPersistService.class); when(shardingSphereDataPersistService.load(any())).thenReturn(Optional.empty()); when(metaDataPersistService.getShardingSphereDataPersistService()).thenReturn(shardingSphereDataPersistService); - MetaDataContexts metaDataContexts = MetaDataContextsFactory.create(metaDataPersistService, - new ShardingSphereMetaData(databases, mock(ResourceMetaData.class), mock(RuleMetaData.class), new ConfigurationProperties(props))); + MetaDataContexts metaDataContexts = MetaDataContextsFactory.create(metaDataPersistService, new ShardingSphereMetaData(databases, mock(), mock(), new ConfigurationProperties(props))); ComputeNodeInstanceContext computeNodeInstanceContext = mock(ComputeNodeInstanceContext.class); when(computeNodeInstanceContext.getModeConfiguration()).thenReturn(mock(ModeConfiguration.class)); return new ContextManager(metaDataContexts, computeNodeInstanceContext, mock(PersistRepository.class)); diff --git a/proxy/frontend/type/mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/authentication/MySQLAuthenticationEngineTest.java b/proxy/frontend/type/mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/authentication/MySQLAuthenticationEngineTest.java index 7815293df42ce..7495758d63be9 100644 --- a/proxy/frontend/type/mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/authentication/MySQLAuthenticationEngineTest.java +++ b/proxy/frontend/type/mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/authentication/MySQLAuthenticationEngineTest.java @@ -23,6 +23,9 @@ import io.netty.channel.ChannelPipeline; import io.netty.util.Attribute; import lombok.SneakyThrows; +import org.apache.shardingsphere.authentication.Authenticator; +import org.apache.shardingsphere.authentication.AuthenticatorFactory; +import org.apache.shardingsphere.authentication.result.AuthenticationResultBuilder; import org.apache.shardingsphere.authority.model.ShardingSpherePrivileges; import org.apache.shardingsphere.authority.rule.AuthorityRule; import org.apache.shardingsphere.db.protocol.constant.CommonConstants; @@ -53,9 +56,6 @@ import org.apache.shardingsphere.mode.metadata.MetaDataContexts; import org.apache.shardingsphere.mode.metadata.MetaDataContextsFactory; import org.apache.shardingsphere.proxy.backend.context.ProxyContext; -import org.apache.shardingsphere.authentication.result.AuthenticationResultBuilder; -import org.apache.shardingsphere.authentication.Authenticator; -import org.apache.shardingsphere.authentication.AuthenticatorFactory; import org.apache.shardingsphere.proxy.frontend.mysql.ssl.MySQLSSLRequestHandler; import org.apache.shardingsphere.proxy.frontend.ssl.ProxySSLContext; import org.apache.shardingsphere.test.mock.AutoMockExtension; @@ -73,7 +73,6 @@ import java.nio.charset.StandardCharsets; import java.util.Collections; import java.util.List; -import java.util.Map; import java.util.Optional; import java.util.Properties; @@ -289,14 +288,14 @@ void assertAuthenticateSuccess() { private ContextManager mockContextManager(final AuthorityRule rule) { ContextManager result = mock(ContextManager.class, RETURNS_DEEP_STUBS); ShardingSphereDatabase database = mock(ShardingSphereDatabase.class); + when(database.getName()).thenReturn("foo_db"); when(database.getProtocolType()).thenReturn(TypedSPILoader.getService(DatabaseType.class, "MySQL")); - Map databases = Collections.singletonMap("foo_db", database); MetaDataPersistService metaDataPersistService = mock(MetaDataPersistService.class); ShardingSphereDataPersistService shardingSphereDataPersistService = mock(ShardingSphereDataPersistService.class); when(shardingSphereDataPersistService.load(any())).thenReturn(Optional.empty()); when(metaDataPersistService.getShardingSphereDataPersistService()).thenReturn(shardingSphereDataPersistService); - MetaDataContexts metaDataContexts = MetaDataContextsFactory.create(metaDataPersistService, new ShardingSphereMetaData(databases, - mock(ResourceMetaData.class), new RuleMetaData(Collections.singleton(rule)), new ConfigurationProperties(new Properties()))); + MetaDataContexts metaDataContexts = MetaDataContextsFactory.create(metaDataPersistService, new ShardingSphereMetaData( + Collections.singleton(database), mock(ResourceMetaData.class), new RuleMetaData(Collections.singleton(rule)), new ConfigurationProperties(new Properties()))); when(result.getMetaDataContexts()).thenReturn(metaDataContexts); return result; } diff --git a/proxy/frontend/type/opengauss/src/test/java/org/apache/shardingsphere/proxy/frontend/opengauss/authentication/OpenGaussAuthenticationEngineTest.java b/proxy/frontend/type/opengauss/src/test/java/org/apache/shardingsphere/proxy/frontend/opengauss/authentication/OpenGaussAuthenticationEngineTest.java index 6387ec5b8f9eb..ef34073e4a9b1 100644 --- a/proxy/frontend/type/opengauss/src/test/java/org/apache/shardingsphere/proxy/frontend/opengauss/authentication/OpenGaussAuthenticationEngineTest.java +++ b/proxy/frontend/type/opengauss/src/test/java/org/apache/shardingsphere/proxy/frontend/opengauss/authentication/OpenGaussAuthenticationEngineTest.java @@ -132,9 +132,8 @@ void assertAuthenticateWithNonPasswordMessage() { } private ContextManager mockContextManager() { - MetaDataContexts metaDataContexts = MetaDataContextsFactory.create(mock(MetaDataPersistService.class), - new ShardingSphereMetaData(Collections.emptyMap(), mock(ResourceMetaData.class), - buildGlobalRuleMetaData(new UserConfiguration("root", "sharding", "", null, false)), mock(ConfigurationProperties.class))); + MetaDataContexts metaDataContexts = MetaDataContextsFactory.create(mock(MetaDataPersistService.class), new ShardingSphereMetaData( + Collections.emptyList(), mock(ResourceMetaData.class), buildGlobalRuleMetaData(new UserConfiguration("root", "sharding", "", null, false)), mock(ConfigurationProperties.class))); ContextManager result = mock(ContextManager.class, RETURNS_DEEP_STUBS); when(result.getMetaDataContexts()).thenReturn(metaDataContexts); return result; diff --git a/proxy/frontend/type/postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/authentication/PostgreSQLAuthenticationEngineTest.java b/proxy/frontend/type/postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/authentication/PostgreSQLAuthenticationEngineTest.java index 86c57d6725395..076d8929ef00a 100644 --- a/proxy/frontend/type/postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/authentication/PostgreSQLAuthenticationEngineTest.java +++ b/proxy/frontend/type/postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/authentication/PostgreSQLAuthenticationEngineTest.java @@ -200,7 +200,7 @@ private ContextManager mockContextManager() { private MetaDataContexts getMetaDataContexts(final UserConfiguration userConfig) { return MetaDataContextsFactory.create(mock(MetaDataPersistService.class), - new ShardingSphereMetaData(Collections.emptyMap(), mock(ResourceMetaData.class), buildGlobalRuleMetaData(userConfig), new ConfigurationProperties(new Properties()))); + new ShardingSphereMetaData(Collections.emptyList(), mock(ResourceMetaData.class), buildGlobalRuleMetaData(userConfig), new ConfigurationProperties(new Properties()))); } private RuleMetaData buildGlobalRuleMetaData(final UserConfiguration userConfig) { diff --git a/test/it/rewriter/src/test/java/org/apache/shardingsphere/test/it/rewrite/engine/SQLRewriterIT.java b/test/it/rewriter/src/test/java/org/apache/shardingsphere/test/it/rewrite/engine/SQLRewriterIT.java index bab50b16bdf4e..80ce2648e3645 100644 --- a/test/it/rewriter/src/test/java/org/apache/shardingsphere/test/it/rewrite/engine/SQLRewriterIT.java +++ b/test/it/rewriter/src/test/java/org/apache/shardingsphere/test/it/rewrite/engine/SQLRewriterIT.java @@ -75,7 +75,6 @@ import java.sql.SQLException; import java.util.Collection; import java.util.Collections; -import java.util.HashMap; import java.util.LinkedHashMap; import java.util.LinkedList; import java.util.Map; @@ -133,10 +132,8 @@ private Collection createSQLRewriteUnits(final SQLRewriteEngineT Collection databaseRules = createDatabaseRules(databaseConfig, schemaName, sqlStatement, databaseType); RuleMetaData databaseRuleMetaData = new RuleMetaData(databaseRules); ShardingSphereDatabase database = new ShardingSphereDatabase(databaseName, databaseType, resourceMetaData, databaseRuleMetaData, mockSchemas(schemaName)); - Map databases = new HashMap<>(2, 1F); - databases.put(databaseName, database); RuleMetaData globalRuleMetaData = new RuleMetaData(createGlobalRules()); - ShardingSphereMetaData metaData = new ShardingSphereMetaData(databases, mock(ResourceMetaData.class), globalRuleMetaData, mock(ConfigurationProperties.class)); + ShardingSphereMetaData metaData = new ShardingSphereMetaData(Collections.singleton(database), mock(), globalRuleMetaData, mock()); HintValueContext hintValueContext = SQLHintUtils.extractHint(testParams.getInputSQL()); SQLStatementContext sqlStatementContext = new SQLBindEngine(metaData, databaseName, hintValueContext).bind(sqlStatement, Collections.emptyList()); if (sqlStatementContext instanceof ParameterAware) {