Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix NPE of system schema builder rule #29136

Merged
merged 1 commit into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Optional;
import java.util.Properties;

/**
Expand Down Expand Up @@ -76,7 +77,11 @@ private static Collection<String> getSystemSchemas(final String originalDatabase
}

private static Collection<InputStream> getSchemaStreams(final String schemaName, final DatabaseType databaseType) {
SystemSchemaBuilderRule builderRule = SystemSchemaBuilderRule.valueOf(databaseType.getType(), schemaName);
Optional<SystemSchemaBuilderRule> builderRuleOptional = SystemSchemaBuilderRule.findBuilderRule(databaseType.getType(), schemaName);
if (!builderRuleOptional.isPresent()) {
return Collections.emptyList();
}
SystemSchemaBuilderRule builderRule = builderRuleOptional.get();
Collection<InputStream> result = new LinkedList<>();
for (String each : builderRule.getTables()) {
result.add(Thread.currentThread().getContextClassLoader().getResourceAsStream("schema/" + databaseType.getType().toLowerCase() + "/" + schemaName + "/" + each + ".yaml"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -27,6 +26,7 @@
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Optional;

/**
* System schema builder rule.
Expand Down Expand Up @@ -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<SystemSchemaBuilderRule> 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));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<SystemSchemaBuilderRule> 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<SystemSchemaBuilderRule> actualMySQLSchema = SystemSchemaBuilderRule.findBuilderRule("MySQL", "mysql");
assertTrue(actualMySQLSchema.isPresent());
assertThat(actualMySQLSchema.get(), is(SystemSchemaBuilderRule.MYSQL_MYSQL));
assertThat(actualMySQLSchema.get().getTables().size(), is(31));
Optional<SystemSchemaBuilderRule> 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<SystemSchemaBuilderRule> actualSysSchema = SystemSchemaBuilderRule.findBuilderRule("MySQL", "sys");
assertTrue(actualSysSchema.isPresent());
assertThat(actualSysSchema.get(), is(SystemSchemaBuilderRule.MYSQL_SYS));
assertThat(actualSysSchema.get().getTables().size(), is(53));
Optional<SystemSchemaBuilderRule> 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<SystemSchemaBuilderRule> 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<SystemSchemaBuilderRule> 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<SystemSchemaBuilderRule> 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<SystemSchemaBuilderRule> unknownSchema = SystemSchemaBuilderRule.findBuilderRule("UnKnown", "public");
assertFalse(unknownSchema.isPresent());
}

@Test
Expand Down