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 f36e7294fdf Refactor ShardingRule.getShardingTable() (#31055)
f36e7294fdf is described below

commit f36e7294fdfb853221fecb55c6da1b66cf066f5a
Author: Liang Zhang <[email protected]>
AuthorDate: Mon Apr 29 15:05:39 2024 +0800

    Refactor ShardingRule.getShardingTable() (#31055)
    
    * Refactor ShardingRule.getShardingTable()
    
    * Fix ShardingRuleTest
---
 .../java/org/apache/shardingsphere/sharding/rule/ShardingRule.java  | 6 ++----
 .../org/apache/shardingsphere/sharding/rule/ShardingRuleTest.java   | 5 +++--
 2 files changed, 5 insertions(+), 6 deletions(-)

diff --git 
a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/rule/ShardingRule.java
 
b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/rule/ShardingRule.java
index ee2401b244c..2b57d7f7fec 100644
--- 
a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/rule/ShardingRule.java
+++ 
b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/rule/ShardingRule.java
@@ -309,10 +309,8 @@ public final class ShardingRule implements DatabaseRule {
      */
     public ShardingTable getShardingTable(final String logicTableName) {
         Optional<ShardingTable> shardingTable = 
findShardingTable(logicTableName);
-        if (shardingTable.isPresent()) {
-            return shardingTable.get();
-        }
-        throw new 
ShardingTableRuleNotFoundException(Collections.singleton(logicTableName));
+        ShardingSpherePreconditions.checkState(shardingTable.isPresent(), () 
-> new 
ShardingTableRuleNotFoundException(Collections.singleton(logicTableName)));
+        return shardingTable.get();
     }
     
     /**
diff --git 
a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/rule/ShardingRuleTest.java
 
b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/rule/ShardingRuleTest.java
index 8723fa8cd24..f9ca4d85d89 100644
--- 
a/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/rule/ShardingRuleTest.java
+++ 
b/features/sharding/core/src/test/java/org/apache/shardingsphere/sharding/rule/ShardingRuleTest.java
@@ -79,6 +79,7 @@ import java.util.Properties;
 import static org.hamcrest.CoreMatchers.instanceOf;
 import static org.hamcrest.CoreMatchers.is;
 import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
 import static org.junit.jupiter.api.Assertions.assertFalse;
 import static org.junit.jupiter.api.Assertions.assertNull;
 import static org.junit.jupiter.api.Assertions.assertThrows;
@@ -197,7 +198,7 @@ class ShardingRuleTest {
         ruleConfig.getTables().add(versionTableRuleConfig);
         ruleConfig.getShardingAlgorithms().put("order_id_inline", new 
AlgorithmConfiguration("INLINE", PropertiesBuilder.build(new 
Property("algorithm-expression", "t_order_${order_id % 2}"))));
         ruleConfig.getShardingAlgorithms().put("user_id_inline", new 
AlgorithmConfiguration("INLINE", PropertiesBuilder.build(new 
Property("algorithm-expression", "ds_${user_id % 2}"))));
-        new ShardingRule(ruleConfig, Collections.emptyMap(), 
mock(InstanceContext.class, RETURNS_DEEP_STUBS));
+        assertDoesNotThrow(() -> new ShardingRule(ruleConfig, 
Collections.emptyMap(), mock(InstanceContext.class, RETURNS_DEEP_STUBS)));
     }
     
     @Test
@@ -209,7 +210,7 @@ class ShardingRuleTest {
         ruleConfig.getTables().add(tableRuleConfig);
         ruleConfig.getShardingAlgorithms().put("t_order_interval_inline",
                 new AlgorithmConfiguration("INLINE", 
PropertiesBuilder.build(new Property("algorithm-expression", 
"t_order_interval_${order_id.substring(0, 6)}"))));
-        new ShardingRule(ruleConfig, Collections.emptyMap(), 
mock(InstanceContext.class, RETURNS_DEEP_STUBS));
+        assertDoesNotThrow(() -> new ShardingRule(ruleConfig, 
Collections.emptyMap(), mock(InstanceContext.class, RETURNS_DEEP_STUBS)));
     }
     
     @Test

Reply via email to