This is an automated email from the ASF dual-hosted git repository.

duanzhengqiang 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 ec632056dde Add arguments not null check when creating RouteUnit 
(#33382)
ec632056dde is described below

commit ec632056dde1847b3d5f825267902201a821eb90
Author: ZhangCheng <[email protected]>
AuthorDate: Thu Oct 24 13:47:12 2024 +0800

    Add arguments not null check when creating RouteUnit (#33382)
    
    * Add arguments not null check when creating RouteUnit
    
    * Add arguments not null check when creating RouteUnit
    
    * Add release note for #33357
    
    * Add release note for #33357
---
 RELEASE-NOTES.md                                            |  1 +
 .../executor/sql/context/ExecutionContextBuilderTest.java   | 13 -------------
 .../shardingsphere/infra/route/context/RouteUnit.java       | 10 ++++++++--
 .../shardingsphere/infra/route/context/RouteUnitTest.java   | 11 +++++++++++
 4 files changed, 20 insertions(+), 15 deletions(-)

diff --git a/RELEASE-NOTES.md b/RELEASE-NOTES.md
index cf217ef3f76..e6ec554700c 100644
--- a/RELEASE-NOTES.md
+++ b/RELEASE-NOTES.md
@@ -11,6 +11,7 @@
 1. Proxy: Add query parameters and check for mysql kill processId - 
[#33274](https://github.com/apache/shardingsphere/pull/33274)
 1. SQL Parser: Support parsing Doris INSTR - 
[#33289](https://github.com/apache/shardingsphere/pull/33289)
 1. Agent: Simplify the use of Agent's Docker Image - 
[#33356](https://github.com/apache/shardingsphere/pull/33356)
+1. Add arguments not null check when creating RouteUnit - 
[#33382](https://github.com/apache/shardingsphere/pull/33382)
 
 ### Bug Fixes
 
diff --git 
a/infra/executor/src/test/java/org/apache/shardingsphere/infra/executor/sql/context/ExecutionContextBuilderTest.java
 
b/infra/executor/src/test/java/org/apache/shardingsphere/infra/executor/sql/context/ExecutionContextBuilderTest.java
index cf0870dfe6b..ca4ff31c603 100644
--- 
a/infra/executor/src/test/java/org/apache/shardingsphere/infra/executor/sql/context/ExecutionContextBuilderTest.java
+++ 
b/infra/executor/src/test/java/org/apache/shardingsphere/infra/executor/sql/context/ExecutionContextBuilderTest.java
@@ -89,19 +89,6 @@ class ExecutionContextBuilderTest {
         assertThat(actual, is(expected));
     }
     
-    @Test
-    void assertBuildRouteSQLRewriteResultWithNullTableMappers() {
-        RouteUnit routeUnit = new RouteUnit(new RouteMapper("foo_db", 
"actual_db"), null);
-        SQLRewriteUnit sqlRewriteUnit = new SQLRewriteUnit("sql", 
Collections.singletonList("parameter"));
-        Map<RouteUnit, SQLRewriteUnit> sqlRewriteUnits = 
Collections.singletonMap(routeUnit, sqlRewriteUnit);
-        ResourceMetaData resourceMetaData = new 
ResourceMetaData(Collections.emptyMap());
-        RuleMetaData ruleMetaData = new RuleMetaData(Collections.emptyList());
-        ShardingSphereDatabase database = new 
ShardingSphereDatabase(DefaultDatabase.LOGIC_NAME, mock(DatabaseType.class), 
resourceMetaData, ruleMetaData, buildDatabase());
-        Collection<ExecutionUnit> actual = 
ExecutionContextBuilder.build(database, new 
RouteSQLRewriteResult(sqlRewriteUnits), mock(SQLStatementContext.class));
-        ExecutionUnit expectedUnit = new ExecutionUnit("actual_db", new 
SQLUnit("sql", Collections.singletonList("parameter")));
-        assertThat(actual, is(Collections.singleton(expectedUnit)));
-    }
-    
     @Test
     void assertBuildRouteSQLRewriteResult() {
         RouteUnit routeUnit1 = new RouteUnit(new RouteMapper("foo_db_1", 
"actual_db_1"), Collections.singletonList(new RouteMapper("foo_tbl", 
"actual_tbl")));
diff --git 
a/infra/route/src/main/java/org/apache/shardingsphere/infra/route/context/RouteUnit.java
 
b/infra/route/src/main/java/org/apache/shardingsphere/infra/route/context/RouteUnit.java
index f16d01fe8ec..ed869917703 100644
--- 
a/infra/route/src/main/java/org/apache/shardingsphere/infra/route/context/RouteUnit.java
+++ 
b/infra/route/src/main/java/org/apache/shardingsphere/infra/route/context/RouteUnit.java
@@ -19,8 +19,8 @@ package org.apache.shardingsphere.infra.route.context;
 
 import lombok.EqualsAndHashCode;
 import lombok.Getter;
-import lombok.RequiredArgsConstructor;
 import lombok.ToString;
+import 
org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions;
 
 import java.util.Collection;
 import java.util.HashSet;
@@ -31,7 +31,6 @@ import java.util.stream.Collectors;
 /**
  * Route unit.
  */
-@RequiredArgsConstructor
 @Getter
 @EqualsAndHashCode
 @ToString
@@ -41,6 +40,13 @@ public final class RouteUnit {
     
     private final Collection<RouteMapper> tableMappers;
     
+    public RouteUnit(final RouteMapper dataSourceMapper, final 
Collection<RouteMapper> tableMappers) {
+        ShardingSpherePreconditions.checkNotNull(dataSourceMapper, () -> new 
IllegalArgumentException("`dataSourceMapper` is required"));
+        ShardingSpherePreconditions.checkNotNull(tableMappers, () -> new 
IllegalArgumentException("`tableMappers` is required"));
+        this.dataSourceMapper = dataSourceMapper;
+        this.tableMappers = tableMappers;
+    }
+    
     /**
      * Get logic table names.
      *
diff --git 
a/infra/route/src/test/java/org/apache/shardingsphere/infra/route/context/RouteUnitTest.java
 
b/infra/route/src/test/java/org/apache/shardingsphere/infra/route/context/RouteUnitTest.java
index 0db15571377..397fbe42266 100644
--- 
a/infra/route/src/test/java/org/apache/shardingsphere/infra/route/context/RouteUnitTest.java
+++ 
b/infra/route/src/test/java/org/apache/shardingsphere/infra/route/context/RouteUnitTest.java
@@ -26,6 +26,7 @@ import java.util.Set;
 import static org.hamcrest.CoreMatchers.is;
 import static org.junit.jupiter.api.Assertions.assertFalse;
 import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.jupiter.api.Assertions.assertThrows;
 import static org.junit.jupiter.api.Assertions.assertTrue;
 
 class RouteUnitTest {
@@ -70,4 +71,14 @@ class RouteUnitTest {
     void assertTableMapperNotFound() {
         assertFalse(routeUnit.findTableMapper("invalid_ds", 
"invalid_tbl").isPresent());
     }
+    
+    @Test
+    void assertTableMapperIsNull() {
+        assertThrows(IllegalArgumentException.class, () -> new RouteUnit(new 
RouteMapper(LOGIC_DATA_SOURCE, ACTUAL_DATA_SOURCE), null));
+    }
+    
+    @Test
+    void assertDataSourceMapperIsNull() {
+        assertThrows(IllegalArgumentException.class, () -> new RouteUnit(null, 
Arrays.asList(new RouteMapper(LOGIC_TABLE, ACTUAL_TABLE_0), new 
RouteMapper(LOGIC_TABLE, ACTUAL_TABLE_1))));
+    }
 }

Reply via email to