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 93c182e08bf Add SchemaRefreshUtilsTest and TableRefreshUtilsTest
(#37198)
93c182e08bf is described below
commit 93c182e08bf68db9362be68329bd7ff5470a55c2
Author: Liang Zhang <[email protected]>
AuthorDate: Thu Nov 27 18:27:00 2025 +0800
Add SchemaRefreshUtilsTest and TableRefreshUtilsTest (#37198)
---
.../refresher/util/SchemaRefreshUtilsTest.java | 57 ++++++++
.../refresher/util/TableRefreshUtilsTest.java | 159 +++++++++++++++++++++
2 files changed, 216 insertions(+)
diff --git
a/mode/core/src/test/java/org/apache/shardingsphere/mode/metadata/refresher/util/SchemaRefreshUtilsTest.java
b/mode/core/src/test/java/org/apache/shardingsphere/mode/metadata/refresher/util/SchemaRefreshUtilsTest.java
new file mode 100644
index 00000000000..d9cfba25c8d
--- /dev/null
+++
b/mode/core/src/test/java/org/apache/shardingsphere/mode/metadata/refresher/util/SchemaRefreshUtilsTest.java
@@ -0,0 +1,57 @@
+/*
+ * 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.mode.metadata.refresher.util;
+
+import org.apache.shardingsphere.database.connector.core.type.DatabaseType;
+import
org.apache.shardingsphere.infra.binder.context.segment.table.TablesContext;
+import
org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext;
+import
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
+import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
+import
org.apache.shardingsphere.sql.parser.statement.core.statement.SQLStatement;
+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.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+class SchemaRefreshUtilsTest {
+
+ @Test
+ void assertGetSchemaNameWithSchemaFromContext() {
+ TablesContext tablesContext = mock(TablesContext.class);
+
when(tablesContext.getSchemaName()).thenReturn(Optional.of("Foo_Schema"));
+ SQLStatementContext sqlStatementContext =
mock(SQLStatementContext.class);
+ when(sqlStatementContext.getTablesContext()).thenReturn(tablesContext);
+ assertThat(SchemaRefreshUtils.getSchemaName(mock(),
sqlStatementContext), is("foo_schema"));
+ }
+
+ @Test
+ void assertGetSchemaNameWithDefaultSchema() {
+ TablesContext tablesContext = mock(TablesContext.class);
+ when(tablesContext.getSchemaName()).thenReturn(Optional.empty());
+ SQLStatementContext sqlStatementContext =
mock(SQLStatementContext.class);
+ when(sqlStatementContext.getTablesContext()).thenReturn(tablesContext);
+ when(sqlStatementContext.getSqlStatement()).thenReturn(new
SQLStatement(TypedSPILoader.getService(DatabaseType.class, "FIXTURE")));
+ ShardingSphereDatabase database = mock(ShardingSphereDatabase.class);
+ when(database.getName()).thenReturn("FOO_DB");
+ assertThat(SchemaRefreshUtils.getSchemaName(database,
sqlStatementContext), is("foo_db"));
+ }
+}
diff --git
a/mode/core/src/test/java/org/apache/shardingsphere/mode/metadata/refresher/util/TableRefreshUtilsTest.java
b/mode/core/src/test/java/org/apache/shardingsphere/mode/metadata/refresher/util/TableRefreshUtilsTest.java
new file mode 100644
index 00000000000..2733c94095f
--- /dev/null
+++
b/mode/core/src/test/java/org/apache/shardingsphere/mode/metadata/refresher/util/TableRefreshUtilsTest.java
@@ -0,0 +1,159 @@
+/*
+ * 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.mode.metadata.refresher.util;
+
+import
org.apache.shardingsphere.database.connector.core.metadata.database.enums.QuoteCharacter;
+import org.apache.shardingsphere.database.connector.core.type.DatabaseType;
+import org.apache.shardingsphere.infra.config.rule.RuleConfiguration;
+import org.apache.shardingsphere.infra.datanode.DataNode;
+import
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
+import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData;
+import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
+import org.apache.shardingsphere.infra.rule.attribute.RuleAttributes;
+import
org.apache.shardingsphere.infra.rule.attribute.datanode.MutableDataNodeRuleAttribute;
+import
org.apache.shardingsphere.infra.rule.attribute.table.TableMapperRuleAttribute;
+import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
+import org.apache.shardingsphere.single.config.SingleRuleConfiguration;
+import org.apache.shardingsphere.single.constant.SingleTableConstants;
+import
org.apache.shardingsphere.sql.parser.statement.core.value.identifier.IdentifierValue;
+import
org.apache.shardingsphere.test.infra.framework.extension.mock.AutoMockExtension;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+
+import java.util.Arrays;
+import java.util.Collections;
+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.assertTrue;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+@ExtendWith(AutoMockExtension.class)
+class TableRefreshUtilsTest {
+
+ private final DatabaseType databaseType =
TypedSPILoader.getService(DatabaseType.class, "FIXTURE");
+
+ @Test
+ void assertGetTableNameFormatsWithIdentifierPattern() {
+ assertThat(TableRefreshUtils.getTableName(new
IdentifierValue("Foo_Table"), databaseType), is("Foo_Table"));
+ }
+
+ @Test
+ void assertGetTableNameWithQuotedIdentifierReturnsOriginal() {
+ assertThat(TableRefreshUtils.getTableName(new
IdentifierValue("FooTable", QuoteCharacter.QUOTE), databaseType),
is("FooTable"));
+ }
+
+ @Test
+ void assertIsSingleTableWhenDistributedTableExists() {
+ TableMapperRuleAttribute tableMapperRuleAttribute =
mock(TableMapperRuleAttribute.class);
+
when(tableMapperRuleAttribute.getDistributedTableNames()).thenReturn(Collections.singleton("foo_tbl"));
+ RuleMetaData ruleMetaData = mock(RuleMetaData.class);
+
when(ruleMetaData.getAttributes(TableMapperRuleAttribute.class)).thenReturn(Collections.singleton(tableMapperRuleAttribute));
+ ShardingSphereDatabase database = mock(ShardingSphereDatabase.class);
+ when(database.getRuleMetaData()).thenReturn(ruleMetaData);
+ assertFalse(TableRefreshUtils.isSingleTable("foo_tbl", database));
+ }
+
+ @Test
+ void assertIsSingleTableWhenNoDistributedTables() {
+ RuleMetaData ruleMetaData = mock(RuleMetaData.class);
+
when(ruleMetaData.getAttributes(TableMapperRuleAttribute.class)).thenReturn(Collections.emptyList());
+ ShardingSphereDatabase database = mock(ShardingSphereDatabase.class);
+ when(database.getRuleMetaData()).thenReturn(ruleMetaData);
+ assertTrue(TableRefreshUtils.isSingleTable("foo_tbl", database));
+ }
+
+ @Test
+ void assertIsNeedRefreshWhenNoMutableRuleAttributes() {
+ ShardingSphereRule rule = mock(ShardingSphereRule.class);
+ when(rule.getAttributes()).thenReturn(new RuleAttributes());
+ assertFalse(TableRefreshUtils.isNeedRefresh(new
RuleMetaData(Collections.singleton(rule)), "foo_schema", "foo_tbl"));
+ }
+
+ @Test
+ void assertIsNeedRefreshWhenRuleConfigurationIsNotSingle() {
+ MutableDataNodeRuleAttribute mutableDataNodeRuleAttribute =
mock(MutableDataNodeRuleAttribute.class);
+ ShardingSphereRule rule = mock(ShardingSphereRule.class);
+
when(rule.getConfiguration()).thenReturn(mock(RuleConfiguration.class));
+ when(rule.getAttributes()).thenReturn(new
RuleAttributes(mutableDataNodeRuleAttribute));
+ assertFalse(TableRefreshUtils.isNeedRefresh(new
RuleMetaData(Collections.singleton(rule)), "foo_schema", "foo_tbl"));
+ }
+
+ @Test
+ void assertIsNeedRefreshWhenAllTablesConfigured() {
+ MutableDataNodeRuleAttribute mutableDataNodeRuleAttribute =
mock(MutableDataNodeRuleAttribute.class);
+ SingleRuleConfiguration ruleConfig = new SingleRuleConfiguration();
+
ruleConfig.setTables(Collections.singletonList(SingleTableConstants.ALL_TABLES));
+ ShardingSphereRule rule = mock(ShardingSphereRule.class);
+ when(rule.getConfiguration()).thenReturn(ruleConfig);
+ when(rule.getAttributes()).thenReturn(new
RuleAttributes(mutableDataNodeRuleAttribute));
+ assertFalse(TableRefreshUtils.isNeedRefresh(new
RuleMetaData(Collections.singleton(rule)), "foo_schema", "foo_tbl"));
+ }
+
+ @Test
+ void assertIsNeedRefreshWhenTableDataNodeMissing() {
+ MutableDataNodeRuleAttribute mutableDataNodeRuleAttribute =
mock(MutableDataNodeRuleAttribute.class);
+ when(mutableDataNodeRuleAttribute.findTableDataNode("foo_schema",
"foo_tbl")).thenReturn(Optional.empty());
+ SingleRuleConfiguration ruleConfig = new SingleRuleConfiguration();
+ ShardingSphereRule rule = mock(ShardingSphereRule.class);
+ when(rule.getConfiguration()).thenReturn(ruleConfig);
+ when(rule.getAttributes()).thenReturn(new
RuleAttributes(mutableDataNodeRuleAttribute));
+ assertFalse(TableRefreshUtils.isNeedRefresh(new
RuleMetaData(Collections.singleton(rule)), "foo_schema", "foo_tbl"));
+ }
+
+ @Test
+ void assertIsNeedRefreshBlockedByDataSourceWildcard() {
+ MutableDataNodeRuleAttribute mutableDataNodeRuleAttribute =
mock(MutableDataNodeRuleAttribute.class);
+ DataNode dataNode = new DataNode("foo_ds", "foo_schema", "foo_tbl");
+ when(mutableDataNodeRuleAttribute.findTableDataNode("foo_schema",
"foo_tbl")).thenReturn(Optional.of(dataNode));
+ SingleRuleConfiguration ruleConfig = new SingleRuleConfiguration();
+ ruleConfig.setTables(Collections.singletonList("foo_ds.*"));
+ ShardingSphereRule rule = mock(ShardingSphereRule.class);
+ when(rule.getConfiguration()).thenReturn(ruleConfig);
+ when(rule.getAttributes()).thenReturn(new
RuleAttributes(mutableDataNodeRuleAttribute));
+ assertFalse(TableRefreshUtils.isNeedRefresh(new
RuleMetaData(Collections.singleton(rule)), "foo_schema", "foo_tbl"));
+ }
+
+ @Test
+ void assertIsNeedRefreshWhenTableShouldBeRefreshed() {
+ MutableDataNodeRuleAttribute mutableDataNodeRuleAttribute =
mock(MutableDataNodeRuleAttribute.class);
+ DataNode dataNode = new DataNode("foo_ds", "foo_schema", "foo_tbl");
+ when(mutableDataNodeRuleAttribute.findTableDataNode("foo_schema",
"foo_tbl")).thenReturn(Optional.of(dataNode));
+ SingleRuleConfiguration ruleConfig = new SingleRuleConfiguration();
+ ShardingSphereRule rule = mock(ShardingSphereRule.class);
+ when(rule.getConfiguration()).thenReturn(ruleConfig);
+ when(rule.getAttributes()).thenReturn(new
RuleAttributes(mutableDataNodeRuleAttribute));
+ assertTrue(TableRefreshUtils.isNeedRefresh(new
RuleMetaData(Collections.singleton(rule)), "foo_schema", "foo_tbl"));
+ }
+
+ @Test
+ void assertIsNeedRefreshWithTableCollection() {
+ MutableDataNodeRuleAttribute mutableDataNodeRuleAttribute =
mock(MutableDataNodeRuleAttribute.class);
+ when(mutableDataNodeRuleAttribute.findTableDataNode("foo_schema",
"bar_tbl")).thenReturn(Optional.empty());
+ DataNode dataNode = new DataNode("foo_ds", "foo_schema", "foo_tbl");
+ when(mutableDataNodeRuleAttribute.findTableDataNode("foo_schema",
"foo_tbl")).thenReturn(Optional.of(dataNode));
+ SingleRuleConfiguration ruleConfig = new SingleRuleConfiguration();
+ ShardingSphereRule rule = mock(ShardingSphereRule.class);
+ when(rule.getConfiguration()).thenReturn(ruleConfig);
+ when(rule.getAttributes()).thenReturn(new
RuleAttributes(mutableDataNodeRuleAttribute));
+ assertTrue(TableRefreshUtils.isNeedRefresh(new
RuleMetaData(Collections.singleton(rule)), "foo_schema",
Arrays.asList("bar_tbl", "foo_tbl")));
+ }
+}