diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/schema/builder/SystemSchemaBuilder.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/schema/builder/SystemSchemaBuilder.java index 5edfc20132f42..5f1718f872e99 100644 --- a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/schema/builder/SystemSchemaBuilder.java +++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/schema/builder/SystemSchemaBuilder.java @@ -37,6 +37,7 @@ import java.util.LinkedHashMap; import java.util.LinkedList; import java.util.Map; +import java.util.Optional; import java.util.Properties; /** @@ -76,7 +77,11 @@ private static Collection getSystemSchemas(final String originalDatabase } private static Collection getSchemaStreams(final String schemaName, final DatabaseType databaseType) { - SystemSchemaBuilderRule builderRule = SystemSchemaBuilderRule.valueOf(databaseType.getType(), schemaName); + Optional builderRuleOptional = SystemSchemaBuilderRule.findBuilderRule(databaseType.getType(), schemaName); + if (!builderRuleOptional.isPresent()) { + return Collections.emptyList(); + } + SystemSchemaBuilderRule builderRule = builderRuleOptional.get(); Collection result = new LinkedList<>(); for (String each : builderRule.getTables()) { result.add(Thread.currentThread().getContextClassLoader().getResourceAsStream("schema/" + databaseType.getType().toLowerCase() + "/" + schemaName + "/" + each + ".yaml")); diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/schema/builder/SystemSchemaBuilderRule.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/schema/builder/SystemSchemaBuilderRule.java index 67a9d03f96e6d..11055c7435bf8 100644 --- a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/schema/builder/SystemSchemaBuilderRule.java +++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/schema/builder/SystemSchemaBuilderRule.java @@ -17,7 +17,6 @@ package org.apache.shardingsphere.infra.metadata.database.schema.builder; -import com.google.common.base.Preconditions; import lombok.Getter; import lombok.RequiredArgsConstructor; @@ -27,6 +26,7 @@ import java.util.HashMap; import java.util.HashSet; import java.util.Map; +import java.util.Optional; /** * System schema builder rule. @@ -227,11 +227,9 @@ public enum SystemSchemaBuilderRule { * @param schema schema * @return builder rule */ - public static SystemSchemaBuilderRule valueOf(final String databaseType, final String schema) { + public static Optional findBuilderRule(final String databaseType, final String schema) { String schemaPath = databaseType + "." + schema; - SystemSchemaBuilderRule result = SCHEMA_PATH_SYSTEM_SCHEMA_BUILDER_RULE_MAP.get(schemaPath); - Preconditions.checkNotNull(result, "Can not find builder rule: `%s`", schemaPath); - return result; + return Optional.ofNullable(SCHEMA_PATH_SYSTEM_SCHEMA_BUILDER_RULE_MAP.get(schemaPath)); } /** diff --git a/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/schema/builder/SystemSchemaBuilderRuleTest.java b/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/schema/builder/SystemSchemaBuilderRuleTest.java index c6e90f8746eef..a97f61ed4a165 100644 --- a/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/schema/builder/SystemSchemaBuilderRuleTest.java +++ b/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/schema/builder/SystemSchemaBuilderRuleTest.java @@ -19,45 +19,56 @@ import org.junit.jupiter.api.Test; +import java.util.Optional; + import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; class SystemSchemaBuilderRuleTest { @Test void assertValueOfSchemaPathSuccess() { - SystemSchemaBuilderRule actualInformationSchema = SystemSchemaBuilderRule.valueOf("MySQL", "information_schema"); - assertThat(actualInformationSchema, is(SystemSchemaBuilderRule.MYSQL_INFORMATION_SCHEMA)); - assertThat(actualInformationSchema.getTables().size(), is(61)); - SystemSchemaBuilderRule actualMySQLSchema = SystemSchemaBuilderRule.valueOf("MySQL", "mysql"); - assertThat(actualMySQLSchema, is(SystemSchemaBuilderRule.MYSQL_MYSQL)); - assertThat(actualMySQLSchema.getTables().size(), is(31)); - SystemSchemaBuilderRule actualPerformanceSchema = SystemSchemaBuilderRule.valueOf("MySQL", "performance_schema"); - assertThat(actualPerformanceSchema, is(SystemSchemaBuilderRule.MYSQL_PERFORMANCE_SCHEMA)); - assertThat(actualPerformanceSchema.getTables().size(), is(87)); - SystemSchemaBuilderRule actualSysSchema = SystemSchemaBuilderRule.valueOf("MySQL", "sys"); - assertThat(actualSysSchema, is(SystemSchemaBuilderRule.MYSQL_SYS)); - assertThat(actualSysSchema.getTables().size(), is(53)); - SystemSchemaBuilderRule actualPgInformationSchema = SystemSchemaBuilderRule.valueOf("PostgreSQL", "information_schema"); - assertThat(actualPgInformationSchema, is(SystemSchemaBuilderRule.POSTGRESQL_INFORMATION_SCHEMA)); - assertThat(actualPgInformationSchema.getTables().size(), is(69)); - SystemSchemaBuilderRule actualPgCatalog = SystemSchemaBuilderRule.valueOf("PostgreSQL", "pg_catalog"); - assertThat(actualPgCatalog, is(SystemSchemaBuilderRule.POSTGRESQL_PG_CATALOG)); - assertThat(actualPgCatalog.getTables().size(), is(134)); - SystemSchemaBuilderRule actualOgInformationSchema = SystemSchemaBuilderRule.valueOf("openGauss", "information_schema"); - assertThat(actualOgInformationSchema, is(SystemSchemaBuilderRule.OPEN_GAUSS_INFORMATION_SCHEMA)); - assertThat(actualOgInformationSchema.getTables().size(), is(66)); - SystemSchemaBuilderRule actualOgPgCatalog = SystemSchemaBuilderRule.valueOf("openGauss", "pg_catalog"); - assertThat(actualOgPgCatalog, is(SystemSchemaBuilderRule.OPEN_GAUSS_PG_CATALOG)); - assertThat(actualOgPgCatalog.getTables().size(), is(240)); + Optional actualInformationSchema = SystemSchemaBuilderRule.findBuilderRule("MySQL", "information_schema"); + assertTrue(actualInformationSchema.isPresent()); + assertThat(actualInformationSchema.get(), is(SystemSchemaBuilderRule.MYSQL_INFORMATION_SCHEMA)); + assertThat(actualInformationSchema.get().getTables().size(), is(61)); + Optional actualMySQLSchema = SystemSchemaBuilderRule.findBuilderRule("MySQL", "mysql"); + assertTrue(actualMySQLSchema.isPresent()); + assertThat(actualMySQLSchema.get(), is(SystemSchemaBuilderRule.MYSQL_MYSQL)); + assertThat(actualMySQLSchema.get().getTables().size(), is(31)); + Optional actualPerformanceSchema = SystemSchemaBuilderRule.findBuilderRule("MySQL", "performance_schema"); + assertTrue(actualPerformanceSchema.isPresent()); + assertThat(actualPerformanceSchema.get(), is(SystemSchemaBuilderRule.MYSQL_PERFORMANCE_SCHEMA)); + assertThat(actualPerformanceSchema.get().getTables().size(), is(87)); + Optional actualSysSchema = SystemSchemaBuilderRule.findBuilderRule("MySQL", "sys"); + assertTrue(actualSysSchema.isPresent()); + assertThat(actualSysSchema.get(), is(SystemSchemaBuilderRule.MYSQL_SYS)); + assertThat(actualSysSchema.get().getTables().size(), is(53)); + Optional actualPgInformationSchema = SystemSchemaBuilderRule.findBuilderRule("PostgreSQL", "information_schema"); + assertTrue(actualPgInformationSchema.isPresent()); + assertThat(actualPgInformationSchema.get(), is(SystemSchemaBuilderRule.POSTGRESQL_INFORMATION_SCHEMA)); + assertThat(actualPgInformationSchema.get().getTables().size(), is(69)); + Optional actualPgCatalog = SystemSchemaBuilderRule.findBuilderRule("PostgreSQL", "pg_catalog"); + assertTrue(actualPgCatalog.isPresent()); + assertThat(actualPgCatalog.get(), is(SystemSchemaBuilderRule.POSTGRESQL_PG_CATALOG)); + assertThat(actualPgCatalog.get().getTables().size(), is(134)); + Optional actualOgInformationSchema = SystemSchemaBuilderRule.findBuilderRule("openGauss", "information_schema"); + assertTrue(actualOgInformationSchema.isPresent()); + assertThat(actualOgInformationSchema.get(), is(SystemSchemaBuilderRule.OPEN_GAUSS_INFORMATION_SCHEMA)); + assertThat(actualOgInformationSchema.get().getTables().size(), is(66)); + Optional actualOgPgCatalog = SystemSchemaBuilderRule.findBuilderRule("openGauss", "pg_catalog"); + assertTrue(actualOgPgCatalog.isPresent()); + assertThat(actualOgPgCatalog.get(), is(SystemSchemaBuilderRule.OPEN_GAUSS_PG_CATALOG)); + assertThat(actualOgPgCatalog.get().getTables().size(), is(240)); + } @Test - void assertValueOfSchemaPathFailure() { - assertThrows(NullPointerException.class, () -> SystemSchemaBuilderRule.valueOf("MySQL", "test")); + void assertNullableValueOfSchemaPath() { + Optional unknownSchema = SystemSchemaBuilderRule.findBuilderRule("UnKnown", "public"); + assertFalse(unknownSchema.isPresent()); } @Test