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 3240d98630e Add comprehensive test coverage for SystemSchemaUtils
(#37015)
3240d98630e is described below
commit 3240d98630e1352a3e2ca68e5de85de197ad9a30
Author: Liang Zhang <[email protected]>
AuthorDate: Wed Nov 5 18:22:05 2025 +0800
Add comprehensive test coverage for SystemSchemaUtils (#37015)
* Add more test cases on SystemSchemaUtilsTest
* Add comprehensive test coverage for SystemSchemaUtils
- Add complete test cases for isSystemSchema method covering all database
type and completeness combinations
- Add test cases for isDriverQuerySystemCatalog method covering all branch
conditions:
* Empty driver query system catalog option (MySQL)
* Multiple projections scenario
* Non-ExpressionProjection type checking
* Valid system catalog query expressions (openGauss)
- Rename test methods for better clarity and consistency
- Update isDriverQuerySystemCatalog implementation with improved
conditional logic
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <[email protected]>
---------
Co-authored-by: Claude <[email protected]>
---
.../database/schema/util/SystemSchemaUtils.java | 11 ++--
.../schema/util/SystemSchemaUtilsTest.java | 62 ++++++++++++++++++++++
2 files changed, 70 insertions(+), 3 deletions(-)
diff --git
a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/schema/util/SystemSchemaUtils.java
b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/schema/util/SystemSchemaUtils.java
index f30d4e2ffe5..57e101f2080 100644
---
a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/schema/util/SystemSchemaUtils.java
+++
b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/schema/util/SystemSchemaUtils.java
@@ -79,8 +79,13 @@ public final class SystemSchemaUtils {
*/
public static boolean isDriverQuerySystemCatalog(final DatabaseType
databaseType, final Collection<ProjectionSegment> projections) {
Optional<DialectDriverQuerySystemCatalogOption>
driverQuerySystemCatalogOption = new
DatabaseTypeRegistry(databaseType).getDialectDatabaseMetaData().getDriverQuerySystemCatalogOption();
- return driverQuerySystemCatalogOption.isPresent()
- && 1 == projections.size() && projections.iterator().next()
instanceof ExpressionProjectionSegment
- &&
driverQuerySystemCatalogOption.get().isSystemCatalogQueryExpressions(((ExpressionProjectionSegment)
projections.iterator().next()).getText());
+ if (1 != projections.size()) {
+ return false;
+ }
+ ProjectionSegment firstProjection = projections.iterator().next();
+ if (!(firstProjection instanceof ExpressionProjectionSegment)) {
+ return false;
+ }
+ return driverQuerySystemCatalogOption.map(optional ->
optional.isSystemCatalogQueryExpressions(((ExpressionProjectionSegment)
firstProjection).getText())).orElse(false);
}
}
diff --git
a/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/schema/util/SystemSchemaUtilsTest.java
b/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/schema/util/SystemSchemaUtilsTest.java
index 2a540426306..e5e1e7d3d86 100644
---
a/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/schema/util/SystemSchemaUtilsTest.java
+++
b/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/schema/util/SystemSchemaUtilsTest.java
@@ -20,6 +20,8 @@ package
org.apache.shardingsphere.infra.metadata.database.schema.util;
import org.apache.shardingsphere.database.connector.core.type.DatabaseType;
import
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
+import
org.apache.shardingsphere.sql.parser.statement.core.segment.dml.item.ExpressionProjectionSegment;
+import
org.apache.shardingsphere.sql.parser.statement.core.segment.dml.item.ProjectionSegment;
import org.junit.jupiter.api.Test;
import java.util.Arrays;
@@ -67,6 +69,66 @@ class SystemSchemaUtilsTest {
assertFalse(SystemSchemaUtils.containsSystemSchema(databaseType,
Collections.emptyList(), customizedGaussDBDatabase));
}
+ @Test
+ void assertIsSystemSchemaWithUnCompleteDatabase() {
+ ShardingSphereDatabase informationSchemaDatabase =
mockDatabase("information_schema", false);
+ DatabaseType databaseType =
TypedSPILoader.getService(DatabaseType.class, "MySQL");
+
when(informationSchemaDatabase.getProtocolType()).thenReturn(databaseType);
+
assertTrue(SystemSchemaUtils.isSystemSchema(informationSchemaDatabase));
+ }
+
+ @Test
+ void assertIsSystemSchemaWithCompleteDatabaseAndDefaultSchema() {
+ ShardingSphereDatabase pgCatalogDatabase = mockDatabase("pg_catalog",
true);
+ DatabaseType databaseType =
TypedSPILoader.getService(DatabaseType.class, "PostgreSQL");
+ when(pgCatalogDatabase.getProtocolType()).thenReturn(databaseType);
+ assertTrue(SystemSchemaUtils.isSystemSchema(pgCatalogDatabase));
+ }
+
+ @Test
+ void assertIsSystemSchemaWithEmptyDatabase() {
+ ShardingSphereDatabase userDatabase = mockDatabase("foo_db", true);
+ DatabaseType databaseType =
TypedSPILoader.getService(DatabaseType.class, "FIXTURE");
+ when(userDatabase.getProtocolType()).thenReturn(databaseType);
+ assertFalse(SystemSchemaUtils.isSystemSchema(userDatabase));
+ }
+
+ @Test
+ void assertIsSystemSchemaWithoutDefaultSchema() {
+ ShardingSphereDatabase userDatabase = mockDatabase("foo_db", false);
+ DatabaseType databaseType =
TypedSPILoader.getService(DatabaseType.class, "FIXTURE");
+ when(userDatabase.getProtocolType()).thenReturn(databaseType);
+ assertFalse(SystemSchemaUtils.isSystemSchema(userDatabase));
+ }
+
+ @Test
+ void assertIsDriverQuerySystemCatalogWithoutOption() {
+ DatabaseType databaseType =
TypedSPILoader.getService(DatabaseType.class, "MySQL");
+ ExpressionProjectionSegment projection = new
ExpressionProjectionSegment(0, 10, "version()");
+ assertFalse(SystemSchemaUtils.isDriverQuerySystemCatalog(databaseType,
Collections.singleton(projection)));
+ }
+
+ @Test
+ void assertIsDriverQuerySystemCatalogWithMultipleProjections() {
+ DatabaseType databaseType =
TypedSPILoader.getService(DatabaseType.class, "openGauss");
+ ExpressionProjectionSegment projection1 = new
ExpressionProjectionSegment(0, 10, "version()");
+ ExpressionProjectionSegment projection2 = new
ExpressionProjectionSegment(11, 20, "current_database()");
+ assertFalse(SystemSchemaUtils.isDriverQuerySystemCatalog(databaseType,
Arrays.asList(projection1, projection2)));
+ }
+
+ @Test
+ void assertIsDriverQuerySystemCatalogWithNonExpressionProjection() {
+ DatabaseType databaseType =
TypedSPILoader.getService(DatabaseType.class, "openGauss");
+ assertFalse(SystemSchemaUtils.isDriverQuerySystemCatalog(databaseType,
Collections.singleton(mock(ProjectionSegment.class))));
+ }
+
+ @Test
+ void assertIsDriverQuerySystemCatalogWithValidExpression() {
+ DatabaseType databaseType =
TypedSPILoader.getService(DatabaseType.class, "openGauss");
+ ExpressionProjectionSegment projection = new
ExpressionProjectionSegment(0, 10, "version()");
+ assertTrue(SystemSchemaUtils.isDriverQuerySystemCatalog(databaseType,
Collections.singleton(projection)));
+ }
+
private ShardingSphereDatabase mockDatabase(final String databaseName,
final boolean isComplete) {
ShardingSphereDatabase result = mock(ShardingSphereDatabase.class);
when(result.getName()).thenReturn(databaseName);