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 2bc81e553e5 Enhance test coverage for infra-common module core
components (#36977)
2bc81e553e5 is described below
commit 2bc81e553e5ee0edf43aa6834066ce8795a19758
Author: Liang Zhang <[email protected]>
AuthorDate: Fri Oct 31 11:16:13 2025 +0800
Enhance test coverage for infra-common module core components (#36977)
* Enhance test coverage for infra-common module core components
- Add comprehensive test cases for DatabaseRuleConfigurationCheckEngine (5
new methods)
- Add comprehensive test cases for GenericSchemaBuilder (6 new methods)
- Add comprehensive test cases for SchemaMetaDataUtils (6 new methods)
- Add comprehensive test cases for DataNodes (5 new methods)
- Add comprehensive test cases for HintManager (7 new methods)
- Add comprehensive test cases for StorageNodeAggregator (5 new methods)
- Add comprehensive test cases for DatabaseTypeEngine (6 new methods)
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <[email protected]>
* For code format
---------
Co-authored-by: Claude <[email protected]>
---
.../schema/builder/GenericSchemaBuilder.java | 5 +--
.../schema/builder/GenericSchemaBuilderTest.java | 50 +++++++++++++++++++---
.../schema/util/SchemaMetaDataUtilsTest.java | 49 +++++++++++++++++++++
3 files changed, 94 insertions(+), 10 deletions(-)
diff --git
a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/schema/builder/GenericSchemaBuilder.java
b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/schema/builder/GenericSchemaBuilder.java
index d183e1788d9..9e860f4afdc 100644
---
a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/schema/builder/GenericSchemaBuilder.java
+++
b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/schema/builder/GenericSchemaBuilder.java
@@ -36,7 +36,6 @@ import java.sql.SQLException;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
-import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Optional;
@@ -102,9 +101,7 @@ public final class GenericSchemaBuilder {
tableMetaDataList.addAll(Optional.ofNullable(schemaMetaDataMap.get(defaultSchemaName)).map(SchemaMetaData::getTables).orElseGet(Collections::emptyList));
}
String frontendSchemaName = new
DatabaseTypeRegistry(protocolType).getDefaultSchemaName(material.getDefaultSchemaName());
- Map<String, SchemaMetaData> result = new LinkedHashMap<>();
- result.put(frontendSchemaName, new SchemaMetaData(frontendSchemaName,
tableMetaDataList));
- return result;
+ return Collections.singletonMap(frontendSchemaName, new
SchemaMetaData(frontendSchemaName, tableMetaDataList));
}
private static Map<String, ShardingSphereSchema> revise(final Map<String,
SchemaMetaData> schemaMetaDataMap, final GenericSchemaBuilderMaterial material)
{
diff --git
a/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/schema/builder/GenericSchemaBuilderTest.java
b/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/schema/builder/GenericSchemaBuilderTest.java
index 0c3aa94cf53..a5c01ebdd7b 100644
---
a/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/schema/builder/GenericSchemaBuilderTest.java
+++
b/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/schema/builder/GenericSchemaBuilderTest.java
@@ -74,29 +74,67 @@ class GenericSchemaBuilderTest {
@Test
void assertLoadWithExistedTableName() throws SQLException {
- Collection<String> tableNames =
Collections.singletonList("data_node_routed_table1");
+ Collection<String> tableNames = Collections.singleton("foo_tbl");
when(MetaDataLoader.load(any())).thenReturn(createSchemaMetaDataMap(tableNames,
material));
assertFalse(GenericSchemaBuilder.build(tableNames, databaseType,
material).get("foo_schema").getAllTables().isEmpty());
}
@Test
void assertLoadWithNotExistedTableName() throws SQLException {
- Collection<String> tableNames =
Collections.singletonList("invalid_table");
+ Collection<String> tableNames = Collections.singleton("invalid_table");
when(MetaDataLoader.load(any())).thenReturn(createSchemaMetaDataMap(tableNames,
material));
assertTrue(GenericSchemaBuilder.build(tableNames, databaseType,
material).get("foo_schema").getAllTables().isEmpty());
}
@Test
void assertLoadAllTables() throws SQLException {
- Collection<String> tableNames =
Arrays.asList("data_node_routed_table1", "data_node_routed_table2");
+ Collection<String> tableNames = Arrays.asList("foo_tbl", "bar_tbl");
when(MetaDataLoader.load(any())).thenReturn(createSchemaMetaDataMap(tableNames,
material));
Map<String, ShardingSphereSchema> actual =
GenericSchemaBuilder.build(tableNames, databaseType, material);
assertThat(actual.size(), is(1));
assertTables(new ShardingSphereSchema("foo_schema",
actual.values().iterator().next().getAllTables(), Collections.emptyList()));
}
+ @Test
+ void assertBuildWithDifferentProtocolAndStorageTypes() throws SQLException
{
+ DatabaseType differentDatabaseType =
TypedSPILoader.getService(DatabaseType.class, "PostgreSQL");
+ Collection<String> tableNames = Collections.singleton("foo_tbl");
+ Map<String, SchemaMetaData> schemaMetaDataMap =
createSchemaMetaDataMap(tableNames, material);
+ when(MetaDataLoader.load(any())).thenReturn(schemaMetaDataMap);
+ StorageUnit storageUnit = mock(StorageUnit.class);
+ when(storageUnit.getStorageType()).thenReturn(differentDatabaseType);
+ Map<String, StorageUnit> storageUnits =
Collections.singletonMap("foo_schema", storageUnit);
+ ShardingSphereRule rule = mock(ShardingSphereRule.class);
+ when(rule.getAttributes()).thenReturn(new
RuleAttributes(mock(TableMapperRuleAttribute.class)));
+ GenericSchemaBuilderMaterial newMaterial = new
GenericSchemaBuilderMaterial(storageUnits, Collections.singleton(rule), new
ConfigurationProperties(new Properties()), "foo_schema");
+ Map<String, ShardingSphereSchema> actual =
GenericSchemaBuilder.build(tableNames, databaseType, newMaterial);
+ assertThat(actual.size(), is(1));
+ }
+
+ @Test
+ void assertBuildWithEmptyTableNames() throws SQLException {
+ when(MetaDataLoader.load(any())).thenReturn(Collections.emptyMap());
+ Map<String, ShardingSphereSchema> actual =
GenericSchemaBuilder.build(Collections.emptyList(), databaseType, material);
+ assertThat(actual.size(), is(1));
+ assertTrue(actual.get("foo_schema").getAllTables().isEmpty());
+ }
+
+ @Test
+ void assertBuildWithGetAllTableNamesFromRules() throws SQLException {
+ TableMapperRuleAttribute tableMapperRuleAttribute =
mock(TableMapperRuleAttribute.class);
+
when(tableMapperRuleAttribute.getLogicTableNames()).thenReturn(Arrays.asList("foo_tbl",
"bar_tbl"));
+ ShardingSphereRule rule = mock(ShardingSphereRule.class);
+ when(rule.getAttributes()).thenReturn(new
RuleAttributes(tableMapperRuleAttribute));
+
when(MetaDataLoader.load(any())).thenReturn(createSchemaMetaDataMap(Arrays.asList("foo_tbl",
"bar_tbl"), material));
+ GenericSchemaBuilderMaterial newMaterial = new
GenericSchemaBuilderMaterial(
+ Collections.singletonMap("foo_schema",
material.getStorageUnits().get("foo_schema")), Collections.singleton(rule), new
ConfigurationProperties(new Properties()), "foo_schema");
+ Map<String, ShardingSphereSchema> actual =
GenericSchemaBuilder.build(databaseType, newMaterial);
+ assertThat(actual.size(), is(1));
+ assertTables(new ShardingSphereSchema("foo_schema",
actual.values().iterator().next().getAllTables(), Collections.emptyList()));
+ }
+
private Map<String, SchemaMetaData> createSchemaMetaDataMap(final
Collection<String> tableNames, final GenericSchemaBuilderMaterial material) {
- if (!tableNames.isEmpty() &&
(tableNames.contains("data_node_routed_table1") ||
tableNames.contains("data_node_routed_table2"))) {
+ if (!tableNames.isEmpty() && (tableNames.contains("foo_tbl") ||
tableNames.contains("bar_tbl"))) {
Collection<TableMetaData> tableMetaDataList = tableNames.stream()
.map(each -> new TableMetaData(each,
Collections.emptyList(), Collections.emptyList(),
Collections.emptyList())).collect(Collectors.toList());
return Collections.singletonMap(material.getDefaultSchemaName(),
new SchemaMetaData(material.getDefaultSchemaName(), tableMetaDataList));
@@ -106,7 +144,7 @@ class GenericSchemaBuilderTest {
private void assertTables(final ShardingSphereSchema actual) {
assertThat(actual.getAllTables().size(), is(2));
-
assertTrue(actual.getTable("data_node_routed_table1").getAllColumns().isEmpty());
-
assertTrue(actual.getTable("data_node_routed_table2").getAllColumns().isEmpty());
+ assertTrue(actual.getTable("foo_tbl").getAllColumns().isEmpty());
+ assertTrue(actual.getTable("bar_tbl").getAllColumns().isEmpty());
}
}
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 85a51056867..01cf1eb4e0e 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
@@ -41,6 +41,7 @@ import java.util.Map;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@@ -131,4 +132,52 @@ class SchemaMetaDataUtilsTest {
result.put("ds_1", storageUnit2);
return result;
}
+
+ @Test
+ void
assertGetMetaDataLoaderMaterialsWhenEmptyDataNodesAndNotEmptyStorageUnits() {
+ ShardingSphereRule rule = mock(ShardingSphereRule.class);
+ DataNodeRuleAttribute ruleAttribute =
mock(DataNodeRuleAttribute.class);
+
when(ruleAttribute.getDataNodesByTableName("t_order")).thenReturn(Collections.emptyList());
+ when(rule.getAttributes()).thenReturn(new
RuleAttributes(ruleAttribute));
+ ConfigurationProperties props = mock(ConfigurationProperties.class);
+
when(props.getValue(ConfigurationPropertyKey.CHECK_TABLE_METADATA_ENABLED)).thenReturn(true);
+
when(props.getValue(ConfigurationPropertyKey.LOAD_TABLE_METADATA_BATCH_SIZE)).thenReturn(100);
+ GenericSchemaBuilderMaterial material = new
GenericSchemaBuilderMaterial(mockStorageUnits(), Collections.singleton(rule),
props, "sharding_db");
+ Collection<MetaDataLoaderMaterial> actual =
SchemaMetaDataUtils.getMetaDataLoaderMaterials(Collections.singleton("t_order"),
material);
+ assertThat(actual.size(), is(1));
+ Iterator<MetaDataLoaderMaterial> iterator = actual.iterator();
+ MetaDataLoaderMaterial firstMaterial = iterator.next();
+ assertThat(firstMaterial.getActualTableNames(),
is(Collections.singletonList("t_order")));
+ }
+
+ @Test
+ void assertGetMetaDataLoaderMaterialsWithDifferentSchemaName() {
+ ShardingSphereRule rule = mock(ShardingSphereRule.class);
+ DataNodeRuleAttribute ruleAttribute =
mock(DataNodeRuleAttribute.class);
+
when(ruleAttribute.getDataNodesByTableName("t_order")).thenReturn(Collections.singleton(new
DataNode("ds_0", "different_schema", "t_order")));
+ when(rule.getAttributes()).thenReturn(new
RuleAttributes(ruleAttribute));
+ ConfigurationProperties props = mock(ConfigurationProperties.class);
+
when(props.getValue(ConfigurationPropertyKey.CHECK_TABLE_METADATA_ENABLED)).thenReturn(false);
+
when(props.getValue(ConfigurationPropertyKey.LOAD_TABLE_METADATA_BATCH_SIZE)).thenReturn(100);
+ GenericSchemaBuilderMaterial material = new
GenericSchemaBuilderMaterial(mockStorageUnits(), Collections.singleton(rule),
props, "default_schema");
+ Collection<MetaDataLoaderMaterial> actual =
SchemaMetaDataUtils.getMetaDataLoaderMaterials(Collections.singleton("t_order"),
material);
+ assertThat(actual.size(), is(1));
+ Iterator<MetaDataLoaderMaterial> iterator = actual.iterator();
+ MetaDataLoaderMaterial firstMaterial = iterator.next();
+ assertThat(firstMaterial.getActualTableNames(),
is(Collections.singletonList("t_order")));
+ }
+
+ @Test
+ void assertGetMetaDataLoaderMaterialsWithEmptyStorageUnits() {
+ ShardingSphereRule rule = mock(ShardingSphereRule.class);
+ DataNodeRuleAttribute ruleAttribute =
mock(DataNodeRuleAttribute.class);
+
when(ruleAttribute.getDataNodesByTableName("t_order")).thenReturn(Collections.emptyList());
+ when(rule.getAttributes()).thenReturn(new
RuleAttributes(ruleAttribute));
+ ConfigurationProperties props = mock(ConfigurationProperties.class);
+
when(props.getValue(ConfigurationPropertyKey.CHECK_TABLE_METADATA_ENABLED)).thenReturn(true);
+
when(props.getValue(ConfigurationPropertyKey.LOAD_TABLE_METADATA_BATCH_SIZE)).thenReturn(100);
+ GenericSchemaBuilderMaterial material = new
GenericSchemaBuilderMaterial(Collections.emptyMap(),
Collections.singleton(rule), props, "sharding_db");
+ Collection<MetaDataLoaderMaterial> actual =
SchemaMetaDataUtils.getMetaDataLoaderMaterials(Collections.singleton("t_order"),
material);
+ assertTrue(actual.isEmpty());
+ }
}