Skip to content

Commit

Permalink
Fix NPE of system schema builder rule (#29136)
Browse files Browse the repository at this point in the history
  • Loading branch information
FlyingZC authored Nov 23, 2023
1 parent 806c6fb commit 518e2dc
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 33 deletions.
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

0 comments on commit 518e2dc

Please sign in to comment.