This is an automated email from the ASF dual-hosted git repository.
zhangliang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new 99543574de3 Add more test cases on SystemSchemaManager (#38440)
99543574de3 is described below
commit 99543574de343aaed71e427e7af021f4fb65277d
Author: Liang Zhang <[email protected]>
AuthorDate: Sun Mar 15 13:39:23 2026 +0800
Add more test cases on SystemSchemaManager (#38440)
---
.../schema/manager/SystemSchemaManagerTest.java | 178 ++++++++++++---------
1 file changed, 102 insertions(+), 76 deletions(-)
diff --git
a/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/schema/manager/SystemSchemaManagerTest.java
b/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/schema/manager/SystemSchemaManagerTest.java
index ca3dc435f52..20a81a9255b 100644
---
a/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/schema/manager/SystemSchemaManagerTest.java
+++
b/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/schema/manager/SystemSchemaManagerTest.java
@@ -17,108 +17,134 @@
package org.apache.shardingsphere.infra.metadata.database.schema.manager;
+import lombok.SneakyThrows;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.MethodSource;
+import org.mockito.internal.configuration.plugins.Plugins;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
+import java.util.Map;
+import java.util.stream.Stream;
-import static org.hamcrest.Matchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
-import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
class SystemSchemaManagerTest {
- @Test
- void assertValueOfSchemaPathSuccess() {
- Collection<String> actualInformationSchema =
SystemSchemaManager.getTables("MySQL", "information_schema");
- assertThat(actualInformationSchema.size(), is(95));
- Collection<String> actualMySQLSchema =
SystemSchemaManager.getTables("MySQL", "mysql");
- assertThat(actualMySQLSchema.size(), is(40));
- Collection<String> actualPerformanceSchema =
SystemSchemaManager.getTables("MySQL", "performance_schema");
- assertThat(actualPerformanceSchema.size(), is(114));
- Collection<String> actualSysSchema =
SystemSchemaManager.getTables("MySQL", "sys");
- assertThat(actualSysSchema.size(), is(53));
- Collection<String> actualShardingSphereSchema =
SystemSchemaManager.getTables("MySQL", "shardingsphere");
- assertThat(actualShardingSphereSchema.size(), is(1));
- Collection<String> actualPgInformationSchema =
SystemSchemaManager.getTables("PostgreSQL", "information_schema");
- assertThat(actualPgInformationSchema.size(), is(69));
- Collection<String> actualPgCatalog =
SystemSchemaManager.getTables("PostgreSQL", "pg_catalog");
- assertThat(actualPgCatalog.size(), is(134));
- Collection<String> actualOgInformationSchema =
SystemSchemaManager.getTables("openGauss", "information_schema");
- assertThat(actualOgInformationSchema.size(), is(66));
- Collection<String> actualOgPgCatalog =
SystemSchemaManager.getTables("openGauss", "pg_catalog");
- assertThat(actualOgPgCatalog.size(), is(240));
+ private DialectSystemSchemaManager commonSystemSchemaManager;
+
+ @BeforeEach
+ void setUp() {
+ commonSystemSchemaManager = getSystemSchemaManagerMap().get("common");
}
- @Test
- void assertIsSystemTable() {
- assertTrue(SystemSchemaManager.isSystemTable("information_schema",
"columns"));
- assertTrue(SystemSchemaManager.isSystemTable("pg_catalog",
"pg_database"));
- assertTrue(SystemSchemaManager.isSystemTable("pg_catalog",
"pg_tables"));
- assertTrue(SystemSchemaManager.isSystemTable("pg_catalog",
"pg_aggregate"));
- assertTrue(SystemSchemaManager.isSystemTable("pg_catalog", "pg_am"));
- assertTrue(SystemSchemaManager.isSystemTable("pg_catalog", "pg_amop"));
- assertTrue(SystemSchemaManager.isSystemTable("pg_catalog",
"pg_amproc"));
- assertTrue(SystemSchemaManager.isSystemTable("pg_catalog",
"pg_attrdef"));
- assertTrue(SystemSchemaManager.isSystemTable("pg_catalog",
"pg_attribute"));
- assertTrue(SystemSchemaManager.isSystemTable("pg_catalog",
"pg_auth_members"));
- assertTrue(SystemSchemaManager.isSystemTable("pg_catalog",
"pg_authid"));
- assertTrue(SystemSchemaManager.isSystemTable("pg_catalog",
"pg_available_extension_versions"));
- assertTrue(SystemSchemaManager.isSystemTable("pg_catalog",
"pg_available_extensions"));
- assertTrue(SystemSchemaManager.isSystemTable("pg_catalog",
"pg_backend_memory_contexts"));
- assertTrue(SystemSchemaManager.isSystemTable("pg_catalog", "pg_cast"));
- assertTrue(SystemSchemaManager.isSystemTable("pg_catalog",
"pg_range"));
- assertTrue(SystemSchemaManager.isSystemTable("pg_catalog",
"pg_replication_origin"));
- assertTrue(SystemSchemaManager.isSystemTable("pg_catalog",
"pg_rewrite"));
- assertTrue(SystemSchemaManager.isSystemTable("pg_catalog",
"pg_seclabel"));
- assertTrue(SystemSchemaManager.isSystemTable("pg_catalog",
"pg_sequence"));
- assertTrue(SystemSchemaManager.isSystemTable("pg_catalog",
"pg_roles"));
- assertTrue(SystemSchemaManager.isSystemTable("pg_catalog",
"pg_user_mapping"));
- assertTrue(SystemSchemaManager.isSystemTable("pg_catalog",
"pg_stat_database_conflicts"));
- assertTrue(SystemSchemaManager.isSystemTable("pg_catalog",
"pg_stat_gssapi"));
- assertTrue(SystemSchemaManager.isSystemTable("pg_catalog",
"pg_stat_progress_analyze"));
- assertTrue(SystemSchemaManager.isSystemTable("pg_catalog",
"pg_stat_progress_basebackup"));
- assertTrue(SystemSchemaManager.isSystemTable("pg_catalog",
"pg_stat_progress_cluster"));
- assertFalse(SystemSchemaManager.isSystemTable("sharding_db",
"t_order"));
- assertTrue(SystemSchemaManager.isSystemTable("shardingsphere",
"cluster_information"));
- assertFalse(SystemSchemaManager.isSystemTable("shardingsphere",
"foo_tbl"));
+ @AfterEach
+ void tearDown() {
+ getSystemSchemaManagerMap().put("common", commonSystemSchemaManager);
}
- @Test
- void assertIsSystemTableWithDatabaseTypeAndNullSchema() {
- assertTrue(SystemSchemaManager.isSystemTable("MySQL", null,
"columns"));
- assertTrue(SystemSchemaManager.isSystemTable("PostgreSQL", null,
"pg_database"));
- assertFalse(SystemSchemaManager.isSystemTable("MySQL", null,
"foo_tbl"));
+ @SneakyThrows(ReflectiveOperationException.class)
+ @SuppressWarnings("unchecked")
+ private static Map<String, DialectSystemSchemaManager>
getSystemSchemaManagerMap() {
+ return (Map<String, DialectSystemSchemaManager>)
Plugins.getMemberAccessor()
+
.get(SystemSchemaManager.class.getDeclaredField("DATABASE_TYPE_AND_SYSTEM_SCHEMA_MANAGER_MAP"),
null);
}
- @Test
- void assertIsSystemTableWithDatabaseTypeAndSchema() {
- assertTrue(SystemSchemaManager.isSystemTable("MySQL",
"information_schema", "columns"));
- assertTrue(SystemSchemaManager.isSystemTable("PostgreSQL",
"pg_catalog", "pg_database"));
- assertTrue(SystemSchemaManager.isSystemTable("MySQL",
"shardingsphere", "cluster_information"));
- assertFalse(SystemSchemaManager.isSystemTable("MySQL",
"information_schema", "foo_tbl"));
- assertFalse(SystemSchemaManager.isSystemTable("NO_DB", "foo_schema",
"foo_tbl"));
+ @ParameterizedTest(name = "{0}")
+ @MethodSource("getTablesArguments")
+ void assertGetTables(final String name, final String databaseType, final
String schema, final int expectedSize, final String expectedSampleTable) {
+ Collection<String> actualTables =
SystemSchemaManager.getTables(databaseType, schema);
+ assertThat(actualTables.size(), is(expectedSize));
+ assertTrue(actualTables.contains(expectedSampleTable));
}
- @Test
- void assertIsSystemTableWithTableNames() {
- assertTrue(SystemSchemaManager.isSystemTable("MySQL",
"information_schema", Arrays.asList("columns", "tables", "schemata")));
- assertFalse(SystemSchemaManager.isSystemTable("MySQL",
"information_schema", Arrays.asList("columns", "nonexistent_table")));
- assertTrue(SystemSchemaManager.isSystemTable("PostgreSQL",
"pg_catalog", Arrays.asList("pg_database", "pg_tables")));
- assertFalse(SystemSchemaManager.isSystemTable("NO_DB", "foo_schema",
Collections.singleton("foo_tbl")));
- assertTrue(SystemSchemaManager.isSystemTable("MySQL", "foo_schema",
Collections.emptyList()));
+ @ParameterizedTest(name = "{0}")
+ @MethodSource("isSystemTableArguments")
+ void assertIsSystemTable(final String name, final String schema, final
String tableName, final boolean expectedResult) {
+ assertThat(SystemSchemaManager.isSystemTable(schema, tableName),
is(expectedResult));
+ }
+
+ @ParameterizedTest(name = "{0}")
+ @MethodSource("isSystemTableWithDatabaseTypeArguments")
+ void assertIsSystemTableWithDatabaseType(final String name, final String
databaseType, final String schema,
+ final String tableName, final
boolean commonSchemaManagerAvailable, final boolean expectedResult) {
+ if (!commonSchemaManagerAvailable) {
+ getSystemSchemaManagerMap().remove("common");
+ }
+ assertThat(SystemSchemaManager.isSystemTable(databaseType, schema,
tableName), is(expectedResult));
+ }
+
+ @ParameterizedTest(name = "{0}")
+ @MethodSource("isSystemTableWithTableNamesArguments")
+ void assertIsSystemTableWithTableNames(final String name, final String
databaseType, final String schema,
+ final Collection<String>
tableNames, final boolean commonSchemaManagerAvailable, final boolean
expectedResult) {
+ if (!commonSchemaManagerAvailable) {
+ getSystemSchemaManagerMap().remove("common");
+ }
+ assertThat(SystemSchemaManager.isSystemTable(databaseType, schema,
tableNames), is(expectedResult));
}
@Test
void assertGetAllInputStreams() {
- java.util.Collection<java.io.InputStream> actual =
SystemSchemaManager.getAllInputStreams("MySQL", "information_schema");
- assertThat(actual.size(), is(95));
- for (InputStream each : actual) {
+ Collection<InputStream> actualInputStreams =
SystemSchemaManager.getAllInputStreams("MySQL", "information_schema");
+ assertThat(actualInputStreams.size(), is(95));
+ for (InputStream each : actualInputStreams) {
assertNotNull(each);
}
}
+
+ private static Stream<Arguments> getTablesArguments() {
+ return Stream.of(
+ Arguments.of("mysql information_schema", "MySQL",
"information_schema", 95, "columns"),
+ Arguments.of("mysql schema", "MySQL", "mysql", 40, "user"),
+ Arguments.of("mysql performance_schema", "MySQL",
"performance_schema", 114, "accounts"),
+ Arguments.of("mysql sys", "MySQL", "sys", 53, "host_summary"),
+ Arguments.of("common shardingsphere schema", "MySQL",
"shardingsphere", 1, "cluster_information"),
+ Arguments.of("postgresql information_schema", "PostgreSQL",
"information_schema", 69, "columns"),
+ Arguments.of("postgresql pg_catalog", "PostgreSQL",
"pg_catalog", 134, "pg_database"),
+ Arguments.of("openGauss information_schema", "openGauss",
"information_schema", 66, "columns"),
+ Arguments.of("openGauss pg_catalog", "openGauss",
"pg_catalog", 240, "pg_database"));
+ }
+
+ private static Stream<Arguments> isSystemTableArguments() {
+ return Stream.of(
+ Arguments.of("common information_schema table",
"information_schema", "columns", true),
+ Arguments.of("postgresql catalog table", "pg_catalog",
"pg_database", true),
+ Arguments.of("common shardingsphere table", "shardingsphere",
"cluster_information", true),
+ Arguments.of("non system table", "sharding_db", "t_order",
false),
+ Arguments.of("unknown table in common schema",
"shardingsphere", "foo_tbl", false));
+ }
+
+ private static Stream<Arguments> isSystemTableWithDatabaseTypeArguments() {
+ return Stream.of(
+ Arguments.of("mysql with null schema", "MySQL", null,
"columns", true, true),
+ Arguments.of("postgresql with null schema", "PostgreSQL",
null, "pg_database", true, true),
+ Arguments.of("mysql null schema unknown table", "MySQL", null,
"foo_tbl", true, false),
+ Arguments.of("mysql database-specific schema", "MySQL",
"information_schema", "columns", true, true),
+ Arguments.of("postgresql database-specific schema",
"PostgreSQL", "pg_catalog", "pg_database", true, true),
+ Arguments.of("mysql falls back to common schema", "MySQL",
"shardingsphere", "cluster_information", true, true),
+ Arguments.of("unknown database falls back to common schema",
"NO_DB", "shardingsphere", "cluster_information", true, true),
+ Arguments.of("unknown database without common schema manager",
"NO_DB", "shardingsphere", "cluster_information", false, false),
+ Arguments.of("unknown database unknown table", "NO_DB",
"foo_schema", "foo_tbl", true, false));
+ }
+
+ private static Stream<Arguments> isSystemTableWithTableNamesArguments() {
+ return Stream.of(
+ Arguments.of("mysql schema all tables exist", "MySQL",
"information_schema", Arrays.asList("columns", "tables", "schemata"), true,
true),
+ Arguments.of("mysql schema missing one table", "MySQL",
"information_schema", Arrays.asList("columns", "nonexistent_table"), true,
false),
+ Arguments.of("postgresql catalog tables exist", "PostgreSQL",
"pg_catalog", Arrays.asList("pg_database", "pg_tables"), true, true),
+ Arguments.of("unknown database falls back to common
collection", "NO_DB", "shardingsphere",
Collections.singleton("cluster_information"), true, true),
+ Arguments.of("unknown database without common collection",
"NO_DB", "shardingsphere", Collections.singleton("cluster_information"), false,
false),
+ Arguments.of("unknown database unknown collection", "NO_DB",
"foo_schema", Collections.singleton("foo_tbl"), true, false),
+ Arguments.of("empty table collection", "MySQL", "foo_schema",
Collections.emptyList(), true, true));
+ }
}