You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by zh...@apache.org on 2026/03/15 03:47:04 UTC

(shardingsphere) branch master updated: Add more test cases on GenericSchemaManagerTest (#38439)

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 13bf1e2ba44 Add more test cases on GenericSchemaManagerTest (#38439)
13bf1e2ba44 is described below

commit 13bf1e2ba4428828058368893ee74aa2cac2966b
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Sun Mar 15 11:46:58 2026 +0800

    Add more test cases on GenericSchemaManagerTest (#38439)
---
 .../database/ShardingSphereDatabaseFactory.java    |  20 ----
 .../ShardingSphereDatabaseFactoryTest.java         |  24 -----
 .../schema/manager/GenericSchemaManagerTest.java   | 112 ++++++++++++++-------
 3 files changed, 78 insertions(+), 78 deletions(-)

diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/ShardingSphereDatabaseFactory.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/ShardingSphereDatabaseFactory.java
index 119c4c37da6..0fcdfda6906 100644
--- a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/ShardingSphereDatabaseFactory.java
+++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/ShardingSphereDatabaseFactory.java
@@ -103,24 +103,4 @@ public final class ShardingSphereDatabaseFactory {
         Collection<ShardingSphereRule> rules = DatabaseRulesBuilder.build(name, protocolType, databaseConfig, computeNodeInstanceContext, resourceMetaData);
         return new ShardingSphereDatabase(name, protocolType, resourceMetaData, new RuleMetaData(rules), schemas);
     }
-    
-    /**
-     * Create database without system schema.
-     *
-     * @param name database name
-     * @param protocolType database protocol type
-     * @param databaseConfig database configuration
-     * @param props configuration properties
-     * @param computeNodeInstanceContext compute node instance context
-     * @return created database
-     * @throws SQLException SQL exception
-     */
-    public static ShardingSphereDatabase createWithoutSystemSchema(final String name, final DatabaseType protocolType, final DatabaseConfiguration databaseConfig,
-                                                                   final ConfigurationProperties props, final ComputeNodeInstanceContext computeNodeInstanceContext) throws SQLException {
-        ResourceMetaData resourceMetaData = new ResourceMetaData(databaseConfig.getDataSources(), databaseConfig.getStorageUnits());
-        Collection<ShardingSphereRule> databaseRules = DatabaseRulesBuilder.build(name, protocolType, databaseConfig, computeNodeInstanceContext, resourceMetaData);
-        Map<String, ShardingSphereSchema> schemas = new ConcurrentHashMap<>(GenericSchemaBuilder.build(protocolType,
-                new GenericSchemaBuilderMaterial(resourceMetaData.getStorageUnits(), databaseRules, props, new DatabaseTypeRegistry(protocolType).getDefaultSchemaName(name))));
-        return new ShardingSphereDatabase(name, protocolType, resourceMetaData, new RuleMetaData(databaseRules), schemas.values());
-    }
 }
diff --git a/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/ShardingSphereDatabaseFactoryTest.java b/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/ShardingSphereDatabaseFactoryTest.java
index 41091a0d404..3058d39532c 100644
--- a/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/ShardingSphereDatabaseFactoryTest.java
+++ b/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/ShardingSphereDatabaseFactoryTest.java
@@ -48,7 +48,6 @@ import java.util.stream.Stream;
 
 import static org.hamcrest.MatcherAssert.assertThat;
 import static org.hamcrest.Matchers.is;
-import static org.junit.jupiter.api.Assertions.assertFalse;
 import static org.junit.jupiter.api.Assertions.assertTrue;
 import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.ArgumentMatchers.eq;
@@ -115,29 +114,6 @@ class ShardingSphereDatabaseFactoryTest {
         assertTrue(actual.containsSchema("foo_schema"));
     }
     
-    @Test
-    void assertCreateWithoutSystemSchema() throws SQLException {
-        DatabaseType protocolType = mock(DatabaseType.class);
-        Map<String, ShardingSphereSchema> genericSchemas = Collections.singletonMap("foo_schema", new ShardingSphereSchema("foo_schema", protocolType));
-        try (
-                MockedConstruction<DatabaseTypeRegistry> ignored = mockConstruction(DatabaseTypeRegistry.class,
-                        (mock, context) -> when(mock.getDefaultSchemaName("foo_db")).thenReturn("foo_schema"));
-                MockedStatic<DatabaseRulesBuilder> mockedRulesBuilder = mockStatic(DatabaseRulesBuilder.class);
-                MockedStatic<GenericSchemaBuilder> mockedGenericSchemaBuilder = mockStatic(GenericSchemaBuilder.class)) {
-            ComputeNodeInstanceContext computeNodeInstanceContext = mock(ComputeNodeInstanceContext.class);
-            mockedRulesBuilder.when(() -> DatabaseRulesBuilder.build(eq("foo_db"), eq(protocolType), eq(databaseConfig), eq(computeNodeInstanceContext), any(ResourceMetaData.class)))
-                    .thenReturn(Collections.singleton(mock(ShardingSphereRule.class)));
-            mockedGenericSchemaBuilder.when(() -> GenericSchemaBuilder.build(eq(protocolType), any(GenericSchemaBuilderMaterial.class))).thenReturn(genericSchemas);
-            ShardingSphereDatabase actual = ShardingSphereDatabaseFactory.createWithoutSystemSchema("foo_db", protocolType, databaseConfig, props, computeNodeInstanceContext);
-            assertThat(actual.getName(), is("foo_db"));
-            assertThat(actual.getProtocolType(), is(protocolType));
-            assertThat(actual.getAllSchemas().size(), is(1));
-            assertThat(actual.getRuleMetaData().getRules().size(), is(1));
-            assertTrue(actual.containsSchema("foo_schema"));
-            assertFalse(actual.containsSchema("system_schema"));
-        }
-    }
-    
     private static Stream<Arguments> createWithSystemDatabaseArguments() {
         return Stream.of(
                 Arguments.of("fixture database adds default schema", TypedSPILoader.getService(DatabaseType.class, "FIXTURE"), "system_db", "system_db", 1),
diff --git a/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/schema/manager/GenericSchemaManagerTest.java b/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/schema/manager/GenericSchemaManagerTest.java
index 4c2faf2b27e..81c22cd262b 100644
--- a/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/schema/manager/GenericSchemaManagerTest.java
+++ b/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/schema/manager/GenericSchemaManagerTest.java
@@ -23,12 +23,18 @@ import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
 import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema;
 import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereTable;
 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 java.util.Arrays;
 import java.util.Collection;
 import java.util.Collections;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
 
-import static org.hamcrest.Matchers.is;
 import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.is;
 import static org.junit.jupiter.api.Assertions.assertTrue;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
@@ -38,58 +44,96 @@ class GenericSchemaManagerTest {
     @Test
     void assertGetToBeAlteredSchemasWithTablesAdded() {
         ShardingSphereDatabase reloadDatabase = mock(ShardingSphereDatabase.class);
-        when(reloadDatabase.getAllSchemas()).thenReturn(Collections.singleton(new ShardingSphereSchema("foo_schema", mock(DatabaseType.class),
-                Collections.singleton(new ShardingSphereTable("foo_tbl", Collections.emptyList(), Collections.emptyList(), Collections.emptyList())), Collections.emptyList())));
-        ShardingSphereSchema currentSchemas = new ShardingSphereSchema("foo_schema", mock(DatabaseType.class), Collections.emptyList(), Collections.emptyList());
+        ShardingSphereSchema reloadSchema = createSchema("foo_schema", createTable("foo_tbl", TableType.TABLE));
+        when(reloadDatabase.getAllSchemas()).thenReturn(Collections.singleton(reloadSchema));
+        ShardingSphereSchema currentSchema = createSchema("foo_schema");
         ShardingSphereDatabase currentDatabase = mock(ShardingSphereDatabase.class);
-        when(currentDatabase.getAllSchemas()).thenReturn(Collections.singleton(currentSchemas));
+        when(currentDatabase.getAllSchemas()).thenReturn(Collections.singleton(currentSchema));
         when(currentDatabase.containsSchema("foo_schema")).thenReturn(true);
-        when(currentDatabase.getSchema("foo_schema")).thenReturn(currentSchemas);
-        Collection<ShardingSphereSchema> actual = GenericSchemaManager.getToBeAlteredSchemasWithTablesAdded(reloadDatabase, currentDatabase);
-        assertThat(actual.size(), is(1));
-        assertThat(actual.iterator().next().getAllTables().size(), is(1));
-        assertTrue(actual.iterator().next().containsTable("foo_tbl"));
+        when(currentDatabase.getSchema("foo_schema")).thenReturn(currentSchema);
+        Collection<ShardingSphereSchema> actualSchemas = GenericSchemaManager.getToBeAlteredSchemasWithTablesAdded(reloadDatabase, currentDatabase);
+        assertThat(actualSchemas.size(), is(1));
+        ShardingSphereSchema actualSchema = actualSchemas.iterator().next();
+        assertThat(actualSchema.getName(), is("foo_schema"));
+        assertThat(actualSchema.getAllTables().size(), is(1));
+        assertTrue(actualSchema.containsTable("foo_tbl"));
+    }
+    
+    @Test
+    void assertGetToBeAlteredSchemasWithTablesAddedWithNewSchema() {
+        ShardingSphereSchema reloadSchema = createSchema("foo_schema", createTable("foo_tbl", TableType.TABLE));
+        ShardingSphereDatabase reloadDatabase = mock(ShardingSphereDatabase.class);
+        when(reloadDatabase.getAllSchemas()).thenReturn(Collections.singleton(reloadSchema));
+        ShardingSphereDatabase currentDatabase = mock(ShardingSphereDatabase.class);
+        when(currentDatabase.getAllSchemas()).thenReturn(Collections.emptyList());
+        when(currentDatabase.containsSchema("foo_schema")).thenReturn(false);
+        Collection<ShardingSphereSchema> actualSchemas = GenericSchemaManager.getToBeAlteredSchemasWithTablesAdded(reloadDatabase, currentDatabase);
+        assertThat(actualSchemas.size(), is(1));
+        ShardingSphereSchema actualSchema = actualSchemas.iterator().next();
+        assertThat(actualSchema.getName(), is("foo_schema"));
+        assertThat(actualSchema.getAllTables().size(), is(1));
+        assertTrue(actualSchema.containsTable("foo_tbl"));
     }
     
     @Test
     void assertGetToBeAlteredSchemasWithTablesDropped() {
         ShardingSphereDatabase reloadDatabase = mock(ShardingSphereDatabase.class);
         when(reloadDatabase.containsSchema("foo_schema")).thenReturn(true);
-        when(reloadDatabase.getSchema("foo_schema")).thenReturn(new ShardingSphereSchema("foo_schema", mock(DatabaseType.class), Collections.emptyList(), Collections.emptyList()));
+        when(reloadDatabase.containsSchema("bar_schema")).thenReturn(false);
+        when(reloadDatabase.getSchema("foo_schema")).thenReturn(createSchema("foo_schema"));
         ShardingSphereDatabase currentDatabase = mock(ShardingSphereDatabase.class);
-        when(currentDatabase.getAllSchemas()).thenReturn(Collections.singleton(new ShardingSphereSchema("foo_schema", mock(DatabaseType.class),
-                Collections.singleton(new ShardingSphereTable("foo_tbl", Collections.emptyList(), Collections.emptyList(), Collections.emptyList())), Collections.emptyList())));
-        Collection<ShardingSphereSchema> actual = GenericSchemaManager.getToBeAlteredSchemasWithTablesDropped(reloadDatabase, currentDatabase);
-        assertThat(actual.size(), is(1));
-        assertThat(actual.iterator().next().getAllTables().size(), is(1));
-        assertTrue(actual.iterator().next().containsTable("foo_tbl"));
+        when(currentDatabase.getAllSchemas()).thenReturn(
+                Arrays.asList(createSchema("foo_schema", createTable("foo_tbl", TableType.TABLE)), createSchema("bar_schema", createTable("bar_tbl", TableType.TABLE))));
+        Collection<ShardingSphereSchema> actualSchemas = GenericSchemaManager.getToBeAlteredSchemasWithTablesDropped(reloadDatabase, currentDatabase);
+        assertThat(actualSchemas.size(), is(1));
+        ShardingSphereSchema actualSchema = actualSchemas.iterator().next();
+        assertThat(actualSchema.getName(), is("foo_schema"));
+        assertThat(actualSchema.getAllTables().size(), is(1));
+        assertTrue(actualSchema.containsTable("foo_tbl"));
     }
     
-    @Test
-    void assertGetToBeAddedTables() {
-        ShardingSphereSchema reloadSchema = new ShardingSphereSchema("foo_schema", mock(DatabaseType.class),
-                Collections.singleton(new ShardingSphereTable("foo_tbl", Collections.emptyList(), Collections.emptyList(), Collections.emptyList(), TableType.TABLE)), Collections.emptyList());
-        Collection<ShardingSphereTable> actual = GenericSchemaManager.getToBeAddedTables(
-                reloadSchema, new ShardingSphereSchema("foo_schema", mock(DatabaseType.class), Collections.emptyList(), Collections.emptyList()));
-        assertThat(actual.size(), is(1));
-        assertThat(actual.iterator().next().getName(), is("foo_tbl"));
+    @ParameterizedTest(name = "{0}")
+    @MethodSource("getToBeAddedTablesArguments")
+    void assertGetToBeAddedTables(final String name, final ShardingSphereSchema reloadSchema, final ShardingSphereSchema currentSchema, final Collection<String> expectedTableNames) {
+        assertThat(GenericSchemaManager.getToBeAddedTables(reloadSchema, currentSchema).stream().map(ShardingSphereTable::getName).collect(Collectors.toSet()), is(expectedTableNames));
     }
     
     @Test
     void assertGetToBeDroppedTables() {
-        ShardingSphereSchema reloadSchema = new ShardingSphereSchema("foo_schema", mock(DatabaseType.class), Collections.emptyList(), Collections.emptyList());
-        ShardingSphereSchema currentSchema = new ShardingSphereSchema("foo_schema", mock(DatabaseType.class),
-                Collections.singleton(new ShardingSphereTable("foo_tbl", Collections.emptyList(), Collections.emptyList(), Collections.emptyList(), TableType.TABLE)), Collections.emptyList());
-        Collection<ShardingSphereTable> actual = GenericSchemaManager.getToBeDroppedTables(reloadSchema, currentSchema);
-        assertThat(actual.size(), is(1));
-        assertThat(actual.iterator().next().getName(), is("foo_tbl"));
+        assertThat(GenericSchemaManager.getToBeDroppedTables(createSchema("foo_schema"), createSchema("foo_schema", createTable("foo_tbl", TableType.TABLE)))
+                .stream().map(ShardingSphereTable::getName).collect(Collectors.toSet()), is(Collections.singleton("foo_tbl")));
+    }
+    
+    @Test
+    void assertGetToBeDroppedTablesWithExistingTable() {
+        assertTrue(GenericSchemaManager.getToBeDroppedTables(
+                createSchema("foo_schema", createTable("foo_tbl", TableType.TABLE)), createSchema("foo_schema", createTable("foo_tbl", TableType.TABLE))).isEmpty());
     }
     
     @Test
     void assertGetToBeDroppedSchemaNames() {
         ShardingSphereDatabase currentDatabase = mock(ShardingSphereDatabase.class);
-        when(currentDatabase.getAllSchemas()).thenReturn(Collections.singleton(new ShardingSphereSchema("foo_schema", mock(DatabaseType.class))));
-        Collection<String> actual = GenericSchemaManager.getToBeDroppedSchemaNames(mock(ShardingSphereDatabase.class), currentDatabase);
-        assertThat(actual, is(Collections.singleton("foo_schema")));
+        when(currentDatabase.getAllSchemas()).thenReturn(Stream.of(createSchema("foo_schema"), createSchema("bar_schema")).collect(Collectors.toList()));
+        ShardingSphereDatabase reloadDatabase = mock(ShardingSphereDatabase.class);
+        when(reloadDatabase.containsSchema("foo_schema")).thenReturn(true);
+        assertThat(GenericSchemaManager.getToBeDroppedSchemaNames(reloadDatabase, currentDatabase), is(Collections.singleton("bar_schema")));
+    }
+    
+    private static Stream<Arguments> getToBeAddedTablesArguments() {
+        return Stream.of(
+                Arguments.of("table absent in current schema",
+                        createSchema("foo_schema", createTable("foo_tbl", TableType.TABLE)), createSchema("foo_schema"), Collections.singleton("foo_tbl")),
+                Arguments.of("same table definition already exists",
+                        createSchema("foo_schema", createTable("foo_tbl", TableType.TABLE)), createSchema("foo_schema", createTable("foo_tbl", TableType.TABLE)), Collections.emptySet()),
+                Arguments.of("same table name but different definition",
+                        createSchema("foo_schema", createTable("foo_tbl", TableType.TABLE)), createSchema("foo_schema", createTable("foo_tbl", TableType.VIEW)), Collections.singleton("foo_tbl")));
+    }
+    
+    private static ShardingSphereSchema createSchema(final String name, final ShardingSphereTable... tables) {
+        return new ShardingSphereSchema(name, mock(DatabaseType.class), Stream.of(tables).collect(Collectors.toList()), Collections.emptyList());
+    }
+    
+    private static ShardingSphereTable createTable(final String name, final TableType type) {
+        return new ShardingSphereTable(name, Collections.emptyList(), Collections.emptyList(), Collections.emptyList(), type);
     }
 }