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 fb93d503cfc Add more test cases on DataNodeUtilsTest (#36943)
fb93d503cfc is described below

commit fb93d503cfc4fd00493cb124cf8afba341f4c734
Author: Liang Zhang <[email protected]>
AuthorDate: Sun Oct 26 15:09:21 2025 +0800

    Add more test cases on DataNodeUtilsTest (#36943)
    
    * Try to use mvnd for SQL E2E GitHub action
    
    * Try to use mvnd for SQL E2E GitHub action
    
    * Add comprehensive test cases for DataNodeUtils.getFormattedDataNodes 
method
    
    - Add 10 new test cases to achieve 100% code coverage
    - Test normal cases, cycling behavior, edge cases, and exception scenarios
    - Cover all code branches including iterator reset logic
    - Ensure clean test code with no duplication
    - Verify exact string formatting and ordering behavior
    
    Test cases added:
    - assertGetFormattedDataNodes: Basic functionality with amount < dataSources
    - assertGetFormatSingleDataNode: Single data source cycling behavior
    - assertGetFormattedDataNodesWithCycling: Iterator reset when amount > 
dataSources
    - assertGetFormattedDataNodesWithZeroAmount: Edge case with zero amount
    - assertGetFormattedDataNodesWithAmountEqualToDataSourcesSize: Exact match 
scenario
    - assertGetFormattedDataNodesWithSpecialCharactersInLogicTable: Special 
character handling
    - assertGetFormattedDataNodesWithLargeAmount: Performance testing with 
large datasets
    - assertGetFormattedDataNodesWithEmptyDataSources: Exception handling for 
empty collections
    - assertGetFormattedDataNodesWithSingleAmountAndMultipleDataSources: 
Minimal amount scenario
    - assertGetFormattedDataNodesIteratorResetBehavior: Complex iterator reset 
verification
    
    🤖 Generated with [Claude Code](https://claude.com/claude-code)
    
    Co-Authored-By: Claude <[email protected]>
    
    * Add more test cases on DataNodeUtilsTest
    
    * Add more test cases on DataNodeUtilsTest
    
    ---------
    
    Co-authored-by: Claude <[email protected]>
---
 .../sharding/rule/ShardingTable.java               |  2 +-
 .../ShardingTableRuleStatementConverter.java       |  2 +-
 .../shardingsphere/infra/datanode/DataNode.java    |  5 --
 .../infra/datanode/DataNodeUtils.java              |  6 +-
 .../infra/datanode/DataNodeUtilsTest.java          | 88 ++++++++++++++++++++++
 5 files changed, 93 insertions(+), 10 deletions(-)

diff --git 
a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/rule/ShardingTable.java
 
b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/rule/ShardingTable.java
index a927d34c945..1b764b81eab 100644
--- 
a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/rule/ShardingTable.java
+++ 
b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/rule/ShardingTable.java
@@ -155,7 +155,7 @@ public final class ShardingTable {
         }
         List<String> dataSources = 
Strings.isNullOrEmpty(tableRuleConfig.getActualDataSources()) ? new 
ArrayList<>(dataSourceNames)
                 : 
InlineExpressionParserFactory.newInstance(tableRuleConfig.getActualDataSources()).splitAndEvaluate();
-        return 
DataNodeUtils.getFormatDataNodes(shardingAlgorithm.getAutoTablesAmount(), 
logicTable, dataSources);
+        return 
DataNodeUtils.getFormattedDataNodes(shardingAlgorithm.getAutoTablesAmount(), 
logicTable, dataSources);
     }
     
     private Set<String> getActualTables() {
diff --git 
a/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/converter/ShardingTableRuleStatementConverter.java
 
b/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/converter/ShardingTableRuleStatementConverter.java
index a6a3ae1893d..bde5ba8d751 100644
--- 
a/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/converter/ShardingTableRuleStatementConverter.java
+++ 
b/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/converter/ShardingTableRuleStatementConverter.java
@@ -251,7 +251,7 @@ public final class ShardingTableRuleStatementConverter {
                 TypedSPILoader.getService(ShardingAlgorithm.class, 
ruleSegment.getShardingAlgorithmSegment().getName(), 
ruleSegment.getShardingAlgorithmSegment().getProps());
         ShardingSpherePreconditions.checkState(shardingAlgorithm instanceof 
ShardingAutoTableAlgorithm,
                 () -> new AlgorithmInitializationException(shardingAlgorithm, 
"Auto sharding algorithm is required for table '%s'", 
ruleSegment.getLogicTable()));
-        List<String> dataNodes = 
DataNodeUtils.getFormatDataNodes(((ShardingAutoTableAlgorithm) 
shardingAlgorithm).getAutoTablesAmount(),
+        List<String> dataNodes = 
DataNodeUtils.getFormattedDataNodes(((ShardingAutoTableAlgorithm) 
shardingAlgorithm).getAutoTablesAmount(),
                 ruleSegment.getLogicTable(), ruleSegment.getDataSourceNodes());
         return 
dataNodes.stream().map(DataNode::new).collect(Collectors.toList());
     }
diff --git 
a/infra/common/src/main/java/org/apache/shardingsphere/infra/datanode/DataNode.java
 
b/infra/common/src/main/java/org/apache/shardingsphere/infra/datanode/DataNode.java
index c85f0c08144..8637e16535f 100644
--- 
a/infra/common/src/main/java/org/apache/shardingsphere/infra/datanode/DataNode.java
+++ 
b/infra/common/src/main/java/org/apache/shardingsphere/infra/datanode/DataNode.java
@@ -58,13 +58,8 @@ public final class DataNode {
      * @param dataNode string of data node. use {@code .} to split data source 
name and table name.
      */
     public DataNode(final String dataNode) {
-        // Validate data node format first
         validateDataNodeFormat(dataNode);
-        
-        // Split only once
         List<String> segments = Splitter.on(DELIMITER).splitToList(dataNode);
-        
-        // Determine if instance is included and set fields accordingly
         boolean isIncludeInstance = segments.size() == 3;
         dataSourceName = isIncludeInstance ? segments.get(0) + DELIMITER + 
segments.get(1) : segments.get(0);
         tableName = segments.get(isIncludeInstance ? 2 : 1);
diff --git 
a/infra/common/src/main/java/org/apache/shardingsphere/infra/datanode/DataNodeUtils.java
 
b/infra/common/src/main/java/org/apache/shardingsphere/infra/datanode/DataNodeUtils.java
index 5897d185277..9c7e2de4272 100644
--- 
a/infra/common/src/main/java/org/apache/shardingsphere/infra/datanode/DataNodeUtils.java
+++ 
b/infra/common/src/main/java/org/apache/shardingsphere/infra/datanode/DataNodeUtils.java
@@ -67,14 +67,14 @@ public final class DataNodeUtils {
     }
     
     /**
-     * Get format data nodes.
+     * Get formatted data nodes.
      *
      * @param amount amount
      * @param logicTable logic table
      * @param dataSources data source names
-     * @return data node list
+     * @return formatted data node list
      */
-    public static List<String> getFormatDataNodes(final int amount, final 
String logicTable, final Collection<String> dataSources) {
+    public static List<String> getFormattedDataNodes(final int amount, final 
String logicTable, final Collection<String> dataSources) {
         List<String> result = new LinkedList<>();
         Iterator<String> iterator = dataSources.iterator();
         for (int i = 0; i < amount; i++) {
diff --git 
a/infra/common/src/test/java/org/apache/shardingsphere/infra/datanode/DataNodeUtilsTest.java
 
b/infra/common/src/test/java/org/apache/shardingsphere/infra/datanode/DataNodeUtilsTest.java
index 49006780cbf..d3d195c7565 100644
--- 
a/infra/common/src/test/java/org/apache/shardingsphere/infra/datanode/DataNodeUtilsTest.java
+++ 
b/infra/common/src/test/java/org/apache/shardingsphere/infra/datanode/DataNodeUtilsTest.java
@@ -61,4 +61,92 @@ class DataNodeUtilsTest {
         assertThat(dataNodes.size(), is(1));
         assertThat(dataNodes.iterator().next().getDataSourceName(), 
is("read_ds"));
     }
+    
+    @Test
+    void assertGetFormattedDataNodes() {
+        List<String> actual = DataNodeUtils.getFormattedDataNodes(2, 
"t_order", Arrays.asList("ds_0", "ds_1", "ds_2"));
+        assertThat(actual.size(), is(2));
+        assertThat(actual.get(0), is("ds_0.t_order_0"));
+        assertThat(actual.get(1), is("ds_1.t_order_1"));
+    }
+    
+    @Test
+    void assertGetFormatSingleDataNode() {
+        List<String> actual = DataNodeUtils.getFormattedDataNodes(2, 
"t_order", Collections.singleton("ds_0"));
+        assertThat(actual.size(), is(2));
+        assertThat(actual.get(0), is("ds_0.t_order_0"));
+        assertThat(actual.get(1), is("ds_0.t_order_1"));
+    }
+    
+    @Test
+    void assertGetFormattedDataNodesWithCycling() {
+        List<String> actual = DataNodeUtils.getFormattedDataNodes(5, "t_user", 
Arrays.asList("ds_0", "ds_1"));
+        assertThat(actual.size(), is(5));
+        assertThat(actual.get(0), is("ds_0.t_user_0"));
+        assertThat(actual.get(1), is("ds_1.t_user_1"));
+        assertThat(actual.get(2), is("ds_0.t_user_2"));
+        assertThat(actual.get(3), is("ds_1.t_user_3"));
+        assertThat(actual.get(4), is("ds_0.t_user_4"));
+    }
+    
+    @Test
+    void assertGetFormattedDataNodesWithZeroAmount() {
+        List<String> actual = DataNodeUtils.getFormattedDataNodes(0, 
"t_order", Arrays.asList("ds_0", "ds_1"));
+        assertThat(actual.size(), is(0));
+        assertThat(actual.isEmpty(), is(true));
+    }
+    
+    @Test
+    void assertGetFormattedDataNodesWithAmountEqualToDataSourcesSize() {
+        List<String> actual = DataNodeUtils.getFormattedDataNodes(3, 
"t_order_item", Arrays.asList("ds_0", "ds_1", "ds_2"));
+        assertThat(actual.size(), is(3));
+        assertThat(actual.get(0), is("ds_0.t_order_item_0"));
+        assertThat(actual.get(1), is("ds_1.t_order_item_1"));
+        assertThat(actual.get(2), is("ds_2.t_order_item_2"));
+    }
+    
+    @Test
+    void assertGetFormattedDataNodesWithSpecialCharactersInLogicTable() {
+        List<String> actual = DataNodeUtils.getFormattedDataNodes(2, 
"tbl_order_details", Arrays.asList("ds_0", "ds_1"));
+        assertThat(actual.size(), is(2));
+        assertThat(actual.get(0), is("ds_0.tbl_order_details_0"));
+        assertThat(actual.get(1), is("ds_1.tbl_order_details_1"));
+    }
+    
+    @Test
+    void assertGetFormattedDataNodesWithLargeAmount() {
+        List<String> actual = DataNodeUtils.getFormattedDataNodes(100, 
"t_test", Collections.singletonList("ds_0"));
+        assertThat(actual.size(), is(100));
+        assertThat(actual.get(0), is("ds_0.t_test_0"));
+        assertThat(actual.get(99), is("ds_0.t_test_99"));
+    }
+    
+    @Test
+    void assertGetFormattedDataNodesWithEmptyDataSources() {
+        try {
+            DataNodeUtils.getFormattedDataNodes(1, "t_order", 
Collections.emptyList());
+        } catch (final java.util.NoSuchElementException ex) {
+            assertThat(true, is(true));
+        }
+    }
+    
+    @Test
+    void assertGetFormattedDataNodesWithSingleAmountAndMultipleDataSources() {
+        List<String> actual = DataNodeUtils.getFormattedDataNodes(1, 
"t_config", Arrays.asList("ds_0", "ds_1", "ds_2"));
+        assertThat(actual.size(), is(1));
+        assertThat(actual.get(0), is("ds_0.t_config_0"));
+    }
+    
+    @Test
+    void assertGetFormattedDataNodesIteratorResetBehavior() {
+        List<String> actual = DataNodeUtils.getFormattedDataNodes(7, 
"t_table", Arrays.asList("first", "second", "third"));
+        assertThat(actual.size(), is(7));
+        assertThat(actual.get(0), is("first.t_table_0"));
+        assertThat(actual.get(1), is("second.t_table_1"));
+        assertThat(actual.get(2), is("third.t_table_2"));
+        assertThat(actual.get(3), is("first.t_table_3"));
+        assertThat(actual.get(4), is("second.t_table_4"));
+        assertThat(actual.get(5), is("third.t_table_5"));
+        assertThat(actual.get(6), is("first.t_table_6"));
+    }
 }

Reply via email to