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 12:24:18 UTC
(shardingsphere) branch master updated: Add more test cases on ShardingSphereConstraintTest and SchemaMetaDataUtilsTest (#38446)
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 7993e6da7fb Add more test cases on ShardingSphereConstraintTest and SchemaMetaDataUtilsTest (#38446)
7993e6da7fb is described below
commit 7993e6da7fbdc5a61359f53dc4b9ec412204d66a
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Sun Mar 15 20:24:12 2026 +0800
Add more test cases on ShardingSphereConstraintTest and SchemaMetaDataUtilsTest (#38446)
* Add more test cases on ShardingSphereConstraintTest
* Add more test cases on SchemaMetaDataUtilsTest
---
.../schema/model/ShardingSphereConstraintTest.java | 34 ++++
.../schema/util/SchemaMetaDataUtilsTest.java | 177 +++++++++++++--------
2 files changed, 144 insertions(+), 67 deletions(-)
diff --git a/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/schema/model/ShardingSphereConstraintTest.java b/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/schema/model/ShardingSphereConstraintTest.java
new file mode 100644
index 00000000000..ec823a6dfbd
--- /dev/null
+++ b/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/schema/model/ShardingSphereConstraintTest.java
@@ -0,0 +1,34 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.infra.metadata.database.schema.model;
+
+import org.apache.shardingsphere.database.connector.core.metadata.data.model.ConstraintMetaData;
+import org.junit.jupiter.api.Test;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.is;
+
+class ShardingSphereConstraintTest {
+
+ @Test
+ void assertConstructor() {
+ ShardingSphereConstraint actual = new ShardingSphereConstraint(new ConstraintMetaData("foo_cst", "foo_tbl"));
+ assertThat(actual.getName(), is("foo_cst"));
+ assertThat(actual.getReferencedTableName(), is("foo_tbl"));
+ }
+}
diff --git a/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/schema/util/SchemaMetaDataUtilsTest.java b/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/schema/util/SchemaMetaDataUtilsTest.java
index 0ff550e3de3..d20398a96e1 100644
--- a/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/schema/util/SchemaMetaDataUtilsTest.java
+++ b/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/schema/util/SchemaMetaDataUtilsTest.java
@@ -17,11 +17,13 @@
package org.apache.shardingsphere.infra.metadata.database.schema.util;
+import org.apache.shardingsphere.database.connector.core.GlobalDataSourceRegistry;
import org.apache.shardingsphere.database.connector.core.metadata.data.loader.MetaDataLoaderMaterial;
import org.apache.shardingsphere.database.connector.core.type.DatabaseType;
import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
import org.apache.shardingsphere.infra.config.props.ConfigurationPropertyKey;
import org.apache.shardingsphere.infra.datanode.DataNode;
+import org.apache.shardingsphere.infra.exception.kernel.metadata.datanode.UnsupportedActualDataNodeStructureException;
import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnit;
import org.apache.shardingsphere.infra.metadata.database.schema.builder.GenericSchemaBuilderMaterial;
import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
@@ -30,114 +32,155 @@ import org.apache.shardingsphere.infra.rule.attribute.datanode.DataNodeRuleAttri
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
import org.apache.shardingsphere.infra.util.props.PropertiesBuilder;
import org.apache.shardingsphere.infra.util.props.PropertiesBuilder.Property;
+import org.junit.jupiter.api.AfterEach;
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 javax.sql.DataSource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
-import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
+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.junit.jupiter.api.Assertions.assertTrue;
-import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
+import static org.hamcrest.Matchers.is;
+import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
class SchemaMetaDataUtilsTest {
- private final DatabaseType databaseType = TypedSPILoader.getService(DatabaseType.class, "FIXTURE");
+ private static final DatabaseType FIXTURE_DATABASE_TYPE = TypedSPILoader.getService(DatabaseType.class, "FIXTURE");
- @Test
- void assertGetMetaDataLoaderMaterialsWhenConfigCheckMetaDataEnable() {
- ConfigurationProperties props = new ConfigurationProperties(PropertiesBuilder.build(new Property(ConfigurationPropertyKey.CHECK_TABLE_METADATA_ENABLED.getKey(), Boolean.TRUE.toString())));
- GenericSchemaBuilderMaterial material = new GenericSchemaBuilderMaterial(
- mockStorageUnits(), Arrays.asList(mockDataNodeRule(Arrays.asList(new DataNode("ds_0.foo_tbl_0"), new DataNode("ds_1.foo_tbl_1"))),
- mock(ShardingSphereRule.class, RETURNS_DEEP_STUBS)),
- props, "foo_db");
+ private static final DatabaseType MYSQL_DATABASE_TYPE = TypedSPILoader.getService(DatabaseType.class, "MySQL");
+
+ @AfterEach
+ void clearCachedDatabaseTables() {
+ GlobalDataSourceRegistry.getInstance().getCachedDatabaseTables().clear();
+ }
+
+ @ParameterizedTest(name = "{0}")
+ @MethodSource("getMetaDataLoaderMaterialsArguments")
+ void assertGetMetaDataLoaderMaterials(final String name, final Map<String, StorageUnit> storageUnits, final Collection<DataNode> dataNodes,
+ final ConfigurationProperties props, final String defaultSchemaName, final List<String> expectedStorageUnitNames,
+ final List<List<String>> expectedActualTableNames, final List<String> expectedDefaultSchemaNames) {
+ GenericSchemaBuilderMaterial material = new GenericSchemaBuilderMaterial(storageUnits, Collections.singleton(mockDataNodeRule(dataNodes)), props, defaultSchemaName);
List<MetaDataLoaderMaterial> actual = new ArrayList<>(SchemaMetaDataUtils.getMetaDataLoaderMaterials(Collections.singleton("foo_tbl"), material));
- assertThat(actual.size(), is(2));
- assertThat(actual.get(0).getDefaultSchemaName(), is("foo_db"));
- assertThat(actual.get(0).getActualTableNames(), is(Collections.singletonList("foo_tbl_0")));
- assertThat(actual.get(1).getDefaultSchemaName(), is("foo_db"));
- assertThat(actual.get(1).getActualTableNames(), is(Collections.singletonList("foo_tbl_1")));
+ assertThat(actual.size(), is(expectedStorageUnitNames.size()));
+ for (int i = 0; i < actual.size(); i++) {
+ assertThat(actual.get(i).getStorageUnitName(), is(expectedStorageUnitNames.get(i)));
+ assertThat(new ArrayList<>(actual.get(i).getActualTableNames()), is(expectedActualTableNames.get(i)));
+ assertThat(actual.get(i).getDefaultSchemaName(), is(expectedDefaultSchemaNames.get(i)));
+ }
}
@Test
- void assertGetMetaDataLoaderMaterialsWhenNotConfigCheckMetaDataEnable() {
- GenericSchemaBuilderMaterial material = new GenericSchemaBuilderMaterial(
- mockStorageUnits(), Arrays.asList(mockDataNodeRule(Arrays.asList(new DataNode("ds_0.foo_tbl_0"), new DataNode("ds_1.foo_tbl_1"))),
- mock(ShardingSphereRule.class, RETURNS_DEEP_STUBS)),
- new ConfigurationProperties(new Properties()), "foo_db");
+ void assertGetMetaDataLoaderMaterialsWithSupportedThreeTierStructure() {
+ DataSource dataSource = mock(DataSource.class);
+ Map<String, StorageUnit> storageUnits = new LinkedHashMap<>(2, 1F);
+ storageUnits.put("ds", mockStorageUnit(MYSQL_DATABASE_TYPE, dataSource));
+ storageUnits.put("ds.foo_db", mockStorageUnit(MYSQL_DATABASE_TYPE, mock(DataSource.class)));
+ GenericSchemaBuilderMaterial material = new GenericSchemaBuilderMaterial(storageUnits,
+ Collections.singleton(mockDataNodeRule(Collections.singleton(new DataNode("ds.foo_db", "foo_db", "foo_tbl")))),
+ createProperties(Boolean.FALSE, null), "foo_db");
List<MetaDataLoaderMaterial> actual = new ArrayList<>(SchemaMetaDataUtils.getMetaDataLoaderMaterials(Collections.singleton("foo_tbl"), material));
assertThat(actual.size(), is(1));
+ assertThat(actual.get(0).getStorageUnitName(), is("ds.foo_db"));
+ assertThat(actual.get(0).getDataSource(), is(dataSource));
+ assertThat(new ArrayList<>(actual.get(0).getActualTableNames()), is(Collections.singletonList("foo_tbl")));
assertThat(actual.get(0).getDefaultSchemaName(), is("foo_db"));
- assertThat(actual.get(0).getActualTableNames(), is(Collections.singletonList("foo_tbl_0")));
+ assertThat(GlobalDataSourceRegistry.getInstance().getCachedDatabaseTables().get("foo_tbl"), is("foo_db"));
}
@Test
- void assertGetMetaDataLoaderMaterialsWhenEmptyDataNodesAndNotEmptyStorageUnits() {
- ConfigurationProperties props = new ConfigurationProperties(PropertiesBuilder.build(new Property(ConfigurationPropertyKey.CHECK_TABLE_METADATA_ENABLED.getKey(), Boolean.TRUE.toString())));
- GenericSchemaBuilderMaterial material = new GenericSchemaBuilderMaterial(mockStorageUnits(), Collections.singleton(mockDataNodeRule(Collections.emptyList())), props, "foo_db");
- Collection<MetaDataLoaderMaterial> actual = SchemaMetaDataUtils.getMetaDataLoaderMaterials(Collections.singleton("foo_tbl"), material);
- assertThat(actual.size(), is(1));
- Iterator<MetaDataLoaderMaterial> iterator = actual.iterator();
- MetaDataLoaderMaterial firstMaterial = iterator.next();
- assertThat(firstMaterial.getStorageUnitName(), is("ds.with.dot"));
- assertThat(firstMaterial.getActualTableNames(), is(Collections.singletonList("foo_tbl")));
+ void assertGetMetaDataLoaderMaterialsWithUnsupportedThreeTierStructure() {
+ Map<String, StorageUnit> storageUnits = new LinkedHashMap<>(2, 1F);
+ storageUnits.put("ds", mockStorageUnit(FIXTURE_DATABASE_TYPE, mock(DataSource.class)));
+ storageUnits.put("ds.foo_db", mockStorageUnit(FIXTURE_DATABASE_TYPE, mock(DataSource.class)));
+ GenericSchemaBuilderMaterial material = new GenericSchemaBuilderMaterial(storageUnits,
+ Collections.singleton(mockDataNodeRule(Collections.singleton(new DataNode("ds.foo_db", "foo_db", "foo_tbl")))),
+ createProperties(Boolean.FALSE, null), "foo_db");
+ UnsupportedActualDataNodeStructureException actual = assertThrows(UnsupportedActualDataNodeStructureException.class,
+ () -> SchemaMetaDataUtils.getMetaDataLoaderMaterials(Collections.singleton("foo_tbl"), material));
+ assertThat(actual.getMessage(), is("Can not support 3-tier structure for actual data node 'ds.foo_db.foo_tbl' with JDBC 'jdbc:mock'."));
}
@Test
- void assertGetMetaDataLoaderMaterialsWithDifferentSchemaName() {
- GenericSchemaBuilderMaterial material = new GenericSchemaBuilderMaterial(mockStorageUnits(),
- Collections.singleton(mockDataNodeRule(Collections.singleton(new DataNode("ds_0", "different_schema", "foo_tbl")))),
- new ConfigurationProperties(new Properties()), "default_schema");
- Collection<MetaDataLoaderMaterial> actual = SchemaMetaDataUtils.getMetaDataLoaderMaterials(Collections.singleton("foo_tbl"), material);
- assertThat(actual.size(), is(1));
- Iterator<MetaDataLoaderMaterial> iterator = actual.iterator();
- MetaDataLoaderMaterial firstMaterial = iterator.next();
- assertThat(firstMaterial.getActualTableNames(), is(Collections.singletonList("foo_tbl")));
+ void assertGetMetaDataLoaderMaterialsWithBatchSplit() {
+ Map<String, StorageUnit> storageUnits = new LinkedHashMap<>(1, 1F);
+ storageUnits.put("ds_0", mockStorageUnit(FIXTURE_DATABASE_TYPE, mock(DataSource.class)));
+ GenericSchemaBuilderMaterial material = new GenericSchemaBuilderMaterial(storageUnits, Collections.singleton(
+ mockDataNodeRule(Arrays.asList(new DataNode("ds_0.foo_tbl_0"), new DataNode("ds_0.foo_tbl_1"), new DataNode("ds_0.foo_tbl_2")))),
+ createProperties(Boolean.TRUE, 2), "foo_db");
+ List<MetaDataLoaderMaterial> actual = new ArrayList<>(SchemaMetaDataUtils.getMetaDataLoaderMaterials(Collections.singleton("foo_tbl"), material));
+ assertThat(actual.size(), is(2));
+ assertThat(actual.get(0).getStorageUnitName(), is("ds_0"));
+ assertThat(new ArrayList<>(actual.get(0).getActualTableNames()), is(Arrays.asList("foo_tbl_0", "foo_tbl_1")));
+ assertThat(new ArrayList<>(actual.get(1).getActualTableNames()), is(Collections.singletonList("foo_tbl_2")));
}
- @Test
- void assertGetMetaDataLoaderMaterialsWithEmptyStorageUnitsAndCheckTableMetadataEnabled() {
- ConfigurationProperties props = new ConfigurationProperties(PropertiesBuilder.build(new Property(ConfigurationPropertyKey.CHECK_TABLE_METADATA_ENABLED.getKey(), Boolean.TRUE.toString())));
- GenericSchemaBuilderMaterial material = new GenericSchemaBuilderMaterial(Collections.emptyMap(), Collections.singleton(mockDataNodeRule(Collections.emptyList())), props, "foo_db");
- assertTrue(SchemaMetaDataUtils.getMetaDataLoaderMaterials(Collections.singleton("foo_tbl"), material).isEmpty());
+ private ShardingSphereRule mockDataNodeRule(final Collection<DataNode> dataNodes) {
+ ShardingSphereRule result = mock(ShardingSphereRule.class);
+ DataNodeRuleAttribute ruleAttribute = mock(DataNodeRuleAttribute.class);
+ when(ruleAttribute.getDataNodesByTableName("foo_tbl")).thenReturn(dataNodes);
+ when(result.getAttributes()).thenReturn(new RuleAttributes(ruleAttribute));
+ return result;
}
- @Test
- void assertGetMetaDataLoaderMaterialsWithEmptyDataNodesAndCheckTableMetadataDisabled() {
- ConfigurationProperties props = new ConfigurationProperties(PropertiesBuilder.build(new Property(ConfigurationPropertyKey.CHECK_TABLE_METADATA_ENABLED.getKey(), Boolean.FALSE.toString())));
- GenericSchemaBuilderMaterial material = new GenericSchemaBuilderMaterial(Collections.emptyMap(), Collections.singleton(mockDataNodeRule(Collections.emptyList())), props, "foo_db");
- assertTrue(SchemaMetaDataUtils.getMetaDataLoaderMaterials(Collections.singleton("foo_tbl"), material).isEmpty());
+ private static Stream<Arguments> getMetaDataLoaderMaterialsArguments() {
+ return Stream.of(
+ Arguments.of("loads all actual tables when metadata check is enabled",
+ mockStorageUnits("ds.with.dot", "ds", "ds_0", "ds_1"),
+ Arrays.asList(new DataNode("ds_0.foo_tbl_0"), new DataNode("ds_1.foo_tbl_1")), createProperties(Boolean.TRUE, null), "foo_db",
+ Arrays.asList("ds_0", "ds_1"),
+ Arrays.asList(Collections.singletonList("foo_tbl_0"), Collections.singletonList("foo_tbl_1")),
+ Arrays.asList("foo_db", "foo_db")),
+ Arguments.of("loads one actual table when metadata check is disabled",
+ mockStorageUnits("ds.with.dot", "ds", "ds_0", "ds_1"),
+ Arrays.asList(new DataNode("ds_0.foo_tbl_0"), new DataNode("ds_1.foo_tbl_1")), new ConfigurationProperties(new Properties()), "foo_db",
+ Collections.singletonList("ds_0"), Collections.singletonList(Collections.singletonList("foo_tbl_0")), Collections.singletonList("foo_db")),
+ Arguments.of("falls back to first storage unit when schema differs",
+ mockStorageUnits("ds.with.dot", "ds", "ds_0", "ds_1"),
+ Collections.singletonList(new DataNode("ds_0", "different_schema", "foo_tbl")), new ConfigurationProperties(new Properties()), "default_schema",
+ Collections.singletonList("ds.with.dot"), Collections.singletonList(Collections.singletonList("foo_tbl")), Collections.singletonList("default_schema")),
+ Arguments.of("falls back to first storage unit when data source is missing",
+ mockStorageUnits("ds.with.dot", "ds", "ds_0", "ds_1"),
+ Collections.singletonList(new DataNode("missing_ds.foo_tbl_0")), new ConfigurationProperties(new Properties()), "foo_db",
+ Collections.singletonList("ds.with.dot"), Collections.singletonList(Collections.singletonList("foo_tbl")), Collections.singletonList("foo_db")),
+ Arguments.of("falls back to first storage unit when data nodes are empty",
+ mockStorageUnits("ds.with.dot", "ds", "ds_0", "ds_1"),
+ Collections.emptyList(), createProperties(Boolean.TRUE, null), "foo_db",
+ Collections.singletonList("ds.with.dot"), Collections.singletonList(Collections.singletonList("foo_tbl")), Collections.singletonList("foo_db")),
+ Arguments.of("returns empty when storage units are empty and metadata check is enabled",
+ Collections.emptyMap(), Collections.emptyList(), createProperties(Boolean.TRUE, null), "foo_db",
+ Collections.emptyList(), Collections.emptyList(), Collections.emptyList()),
+ Arguments.of("returns empty when storage units are empty and metadata check is disabled",
+ Collections.emptyMap(), Collections.emptyList(), createProperties(Boolean.FALSE, null), "foo_db",
+ Collections.emptyList(), Collections.emptyList(), Collections.emptyList()));
}
- private Map<String, StorageUnit> mockStorageUnits() {
- Map<String, StorageUnit> result = new LinkedHashMap<>(4, 1F);
- result.put("ds.with.dot", mockStorageUnit());
- result.put("ds", mockStorageUnit());
- result.put("ds_0", mockStorageUnit());
- result.put("ds_1", mockStorageUnit());
- return result;
+ private static ConfigurationProperties createProperties(final Boolean checkMetaDataEnabled, final Integer loadTableMetadataBatchSize) {
+ return new ConfigurationProperties(PropertiesBuilder.build(
+ new Property(ConfigurationPropertyKey.CHECK_TABLE_METADATA_ENABLED.getKey(), null == checkMetaDataEnabled ? null : checkMetaDataEnabled.toString()),
+ new Property(ConfigurationPropertyKey.LOAD_TABLE_METADATA_BATCH_SIZE.getKey(), null == loadTableMetadataBatchSize ? null : loadTableMetadataBatchSize.toString())));
}
- private StorageUnit mockStorageUnit() {
- StorageUnit result = mock(StorageUnit.class);
- when(result.getStorageType()).thenReturn(databaseType);
- return result;
+ private static Map<String, StorageUnit> mockStorageUnits(final String... dataSourceNames) {
+ return Arrays.stream(dataSourceNames).collect(Collectors.toMap(each -> each, each -> mockStorageUnit(FIXTURE_DATABASE_TYPE, mock(DataSource.class))));
}
- private ShardingSphereRule mockDataNodeRule(final Collection<DataNode> dataNodes) {
- ShardingSphereRule result = mock(ShardingSphereRule.class);
- DataNodeRuleAttribute ruleAttribute = mock(DataNodeRuleAttribute.class);
- when(ruleAttribute.getDataNodesByTableName("foo_tbl")).thenReturn(dataNodes);
- when(result.getAttributes()).thenReturn(new RuleAttributes(ruleAttribute));
+ private static StorageUnit mockStorageUnit(final DatabaseType storageType, final DataSource dataSource) {
+ StorageUnit result = mock(StorageUnit.class);
+ when(result.getStorageType()).thenReturn(storageType);
+ when(result.getDataSource()).thenReturn(dataSource);
return result;
}
}